diff --git a/includes/class-newspack-popups-segmentation.php b/includes/class-newspack-popups-segmentation.php index 06b566e9..4f63cc42 100644 --- a/includes/class-newspack-popups-segmentation.php +++ b/includes/class-newspack-popups-segmentation.php @@ -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. @@ -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' ] ); @@ -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. * @@ -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. * @@ -424,9 +505,10 @@ 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' ); @@ -434,6 +516,11 @@ public static function create_segment( $segment ) { $segments[] = $segment; update_option( self::SEGMENTS_OPTION_NAME, $segments ); + + if ( $return_id ) { + return $segment['id']; + } + return self::get_segments(); }