Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD] feat: default segments #809

Closed
wants to merge 7 commits into from
91 changes: 89 additions & 2 deletions includes/class-newspack-popups-segmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ final class Newspack_Popups_Segmentation {
*/
const SEGMENTS_OPTION_NAME = 'newspack_popups_segments';

/**
* Name of the option to store default segment IDs.
*/
const DEFAULT_SEGMENTS_OPTION_NAME = 'newspack_popups_default_segments';

/**
* Main Newspack Segmentation Plugin Instance.
* Ensures only one instance of Newspack Segmentation Plugin Instance is loaded or can be loaded.
Expand All @@ -53,6 +58,7 @@ public static function instance() {
*/
public function __construct() {
add_action( 'init', [ __CLASS__, 'create_database_table' ] );
add_action( 'init', [ __CLASS__, 'new_site_setup' ] );
add_action( 'wp_footer', [ __CLASS__, 'insert_amp_analytics' ], 20 );

add_filter( 'newspack_custom_dimensions', [ __CLASS__, 'register_custom_dimensions' ] );
Expand Down Expand Up @@ -343,6 +349,17 @@ public static function create_database_table() {
}
}

/**
* Create default segments for sites that don't have any existing segments.
*/
public static function new_site_setup() {
$segments = self::get_segments();
$default_segments = self::get_default_segments();
if ( empty( $segments ) && empty( $default_segments ) ) {
self::create_default_segments();
}
}

/**
* Get all configured segments.
*
Expand Down Expand Up @@ -386,6 +403,70 @@ function( $segment ) {
return $segments;
}

/**
* Get array of default segments by type.
*
* @return array Array of default segments.
*/
public static function get_default_segments() {
return get_option( self::DEFAULT_SEGMENTS_OPTION_NAME, false );
}

/**
* Generate default segments. If they've already been generated, rebuild them with default parameters.
*
* @return array Array of all segments.
*/
public static function create_default_segments() {
$default_segments = get_option( self::DEFAULT_SEGMENTS_OPTION_NAME, [] );
$default_options = [
'donors' => [
'name' => __( 'Donors', 'newspack-popups' ),
'configuration' => [
'is_donor' => true,
],
],
'frequent_readers' => [
'name' => __( 'Frequent Readers', 'newspack-popups' ),
'configuration' => [
'min_posts' => 5,
],
],
'subscribers' => [
'name' => __( 'Newsletter Subscribers', 'newspack-popups' ),
'configuration' => [
'is_subscribed' => true,
],
],
'everyone_else' => [
'name' => __( 'Other Readers', 'newspack-popups' ),
'configuration' => [
'is_not_donor' => true,
'is_not_subscribed' => true,
],
],
];

foreach ( $default_options as $key => $segment ) {
require_once dirname( __FILE__ ) . '/../api/campaigns/class-campaign-data-utils.php';

// Ensure all config objects contain all option keys.
$segment['configuration'] = (array) Campaign_Data_Utils::canonize_segment( $segment['configuration'] );

// If the segment was already created and still exists, update it. Otherwise, create it.
if ( isset( $default_segments[ $key ] ) && self::get_segment( $default_segments[ $key ] ) ) {
$segment['id'] = $default_segments[ $key ];
self::update_segment( $segment );
} else {
$segment_id = self::create_segment( $segment, true );
$default_segments[ $key ] = $segment_id;
}
}

update_option( self::DEFAULT_SEGMENTS_OPTION_NAME, $default_segments );
return self::reindex_segments( self::get_segments() );
}

/**
* Get a single segment by ID.
*
Expand Down Expand Up @@ -424,16 +505,22 @@ function( $segment ) {
/**
* Create a segment.
*
* @param object $segment A segment.
* @param object $segment A segment.
* @param boolean $return_id If true, return the created segment ID. Otherwise, return all segments.
*/
public static function create_segment( $segment ) {
public static function create_segment( $segment, $return_id = false ) {
$segments = self::get_segments();
$segment['id'] = uniqid();
$segment['created_at'] = gmdate( 'Y-m-d' );
$segment['updated_at'] = gmdate( 'Y-m-d' );
$segments[] = $segment;

update_option( self::SEGMENTS_OPTION_NAME, $segments );

if ( $return_id ) {
return $segment['id'];
}

return self::get_segments();
}

Expand Down