array( 'name' => __('Popups', 'ai-eshop-optimizer'), 'singular_name' => __('Popup', 'ai-eshop-optimizer'), 'add_new_item' => __('Add Popup', 'ai-eshop-optimizer'), 'edit_item' => __('Edit Popup', 'ai-eshop-optimizer'), 'menu_name' => __('Popups', 'ai-eshop-optimizer'), ), 'public' => false, 'publicly_queryable' => true, 'exclude_from_search' => true, 'has_archive' => false, 'rewrite' => false, 'show_ui' => true, 'show_in_menu' => false, 'show_in_rest' => true, 'menu_icon' => 'dashicons-external', 'supports' => array('title', 'editor', 'revisions', 'page-attributes'), 'template' => array(array('aieo/popup-panel')), )); $auth = static function () { return current_user_can('edit_posts'); }; foreach (array(self::META_START, self::META_END, self::META_TRIGGER, self::META_FREQ, self::META_DEVICE, self::META_VISITOR, self::META_CART, self::META_URL_CONTAINS, self::META_COUPON, self::META_SURFACE, self::META_DATA_DELAY, self::META_TEST, self::META_INSTIGATOR, self::META_INSTIGATOR_REF) as $key) { register_post_meta(self::POST_TYPE, $key, array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'auth_callback' => $auth, )); } foreach (array(self::META_DELAY_MS, self::META_SCROLL_PCT, self::META_IDLE_S, self::META_FREQ_DAYS, self::META_MAX_IMPR, self::META_AUDIENCE_ID, self::META_MAX_PER_SESSION) as $key) { register_post_meta(self::POST_TYPE, $key, array( 'type' => 'integer', 'single' => true, 'show_in_rest' => true, 'auth_callback' => $auth, )); } register_post_meta(self::POST_TYPE, self::META_CART_MIN, array( 'type' => 'number', 'single' => true, 'show_in_rest' => true, 'auth_callback' => $auth, )); foreach (array(self::META_PAGES, self::META_EXCLUDE_PAGES) as $key) { register_post_meta(self::POST_TYPE, $key, array( 'type' => 'array', 'single' => true, 'auth_callback' => $auth, 'show_in_rest' => array('schema' => array('type' => 'array', 'items' => array('type' => 'string'))), )); } register_post_meta(self::POST_TYPE, self::META_POST_IDS, array( 'type' => 'array', 'single' => true, 'auth_callback' => $auth, 'show_in_rest' => array('schema' => array('type' => 'array', 'items' => array('type' => 'integer'))), )); } public static function defaults() { return array( self::META_START => '', self::META_END => '', self::META_TRIGGER => 'load', self::META_DELAY_MS => 1500, self::META_SCROLL_PCT => 50, self::META_IDLE_S => 20, self::META_DATA_DELAY => 'no', self::META_TEST => 'no', self::META_FREQ => 'session', self::META_FREQ_DAYS => 7, self::META_MAX_IMPR => 0, self::META_MAX_PER_SESSION => 0, self::META_INSTIGATOR => 'any', self::META_INSTIGATOR_REF => '', self::META_PAGES => array('all'), self::META_EXCLUDE_PAGES => array(), self::META_POST_IDS => array(), self::META_URL_CONTAINS => '', self::META_DEVICE => 'all', self::META_VISITOR => 'all', self::META_CART => 'any', self::META_CART_MIN => 0, self::META_AUDIENCE_ID => 0, self::META_COUPON => '', ); } public static function coupon_code($post_id) { return AIEO_Popup_Offer::code_for((int) $post_id); } public static function get($post_id, $key) { $defaults = self::defaults(); $default = isset($defaults[$key]) ? $defaults[$key] : ''; $raw = get_post_meta((int) $post_id, $key, true); if (is_array($default)) { return is_array($raw) ? $raw : $default; } if ($raw === '' || $raw === null) { return $default; } if (is_int($default)) { return (int) $raw; } if (is_float($default)) { return (float) $raw; } return $raw; } public static function schedule_state($post_id) { return self::state_from( (string) self::get($post_id, self::META_START), (string) self::get($post_id, self::META_END) ); } public static function state_from($start, $end) { $now = current_time('mysql'); if ($start !== '' && $now < $start) { return 'before'; } if ($end !== '' && $now > $end) { return 'ended'; } return 'live'; } public static function to_epoch($mysql_dt) { $mysql_dt = trim((string) $mysql_dt); if ($mysql_dt === '') { return 0; } $gmt = get_gmt_from_date($mysql_dt, 'U'); return $gmt ? (int) $gmt : 0; } private static function load_dt_helpers() { if (class_exists('AIEO_Competition_CPT')) { return true; } if (defined('AIEO_DIR') && file_exists(AIEO_DIR . 'includes/dmm/competitions/class-aieo-competition-cpt.php')) { require_once AIEO_DIR . 'includes/dmm/competitions/class-aieo-competition-cpt.php'; } return class_exists('AIEO_Competition_CPT'); } public static function normalize_dt($v) { if (self::load_dt_helpers()) { return AIEO_Competition_CPT::normalize_dt($v); } $v = str_replace('T', ' ', trim((string) $v)); return preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/', $v) ? (strlen($v) === 16 ? $v . ':00' : $v) : ''; } public static function to_input($mysql_dt) { if (self::load_dt_helpers()) { return AIEO_Competition_CPT::to_input($mysql_dt); } $mysql_dt = trim((string) $mysql_dt); return $mysql_dt === '' ? '' : str_replace(' ', 'T', substr($mysql_dt, 0, 16)); } public static function fmt_dt($mysql_dt) { if (self::load_dt_helpers()) { return AIEO_Competition_CPT::fmt_dt($mysql_dt); } $mysql_dt = trim((string) $mysql_dt); return $mysql_dt; } public static function surface_tag($post_id) { $post_id = (int) $post_id; $stored = (string) get_post_meta($post_id, self::META_SURFACE, true); if ($stored !== '') { return $stored; } return self::mint_surface_tag($post_id); } public static function mint_surface_tag($post_id) { $post_id = (int) $post_id; $stored = (string) get_post_meta($post_id, self::META_SURFACE, true); if ($stored !== '') { return $stored; } $slug = sanitize_key((string) get_post_field('post_name', $post_id)); if ($slug === '') { $slug = 'p' . $post_id; } if (strlen($slug) > self::SLUG_MAX) { $slug = substr($slug, 0, self::SLUG_MAX - strlen((string) $post_id) - 1) . '-' . $post_id; } $tag = 'popup_' . $slug; update_post_meta($post_id, self::META_SURFACE, $tag); return $tag; } public static function placement_rules($post_id) { return array( 'pages' => array_values((array) self::get($post_id, self::META_PAGES)), 'exclude' => array_values((array) self::get($post_id, self::META_EXCLUDE_PAGES)), 'ids' => array_map('intval', (array) self::get($post_id, self::META_POST_IDS)), 'url' => trim((string) self::get($post_id, self::META_URL_CONTAINS)), 'advertises' => class_exists('AIEO_Popup_Offer', false) && AIEO_Popup_Offer::advertises((int) $post_id) ? (int) $post_id : 0, ); } public static function placement_matches(array $rules) { $matches = class_exists('AIEO_Display_Conditions') ? AIEO_Display_Conditions::placement_matches($rules, false) : self::placement_matches_legacy($rules); if ($matches && !empty($rules['advertises']) && class_exists('AIEO_Popup_Offer', false)) { return AIEO_Popup_Offer::shows((int) $rules['advertises']); } return $matches; } private static function placement_matches_legacy(array $rules) { $include = isset($rules['pages']) ? (array) $rules['pages'] : array('all'); $exclude = isset($rules['exclude']) ? (array) $rules['exclude'] : array(); $ids = isset($rules['ids']) ? (array) $rules['ids'] : array(); $needle = isset($rules['url']) ? (string) $rules['url'] : ''; if ($needle !== '') { $uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''; if (stripos($uri, $needle) === false) { return false; } } foreach ($exclude as $token) { if (self::token_matches($token, $ids)) { return false; } } if (in_array('all', $include, true)) { return true; } foreach ($include as $token) { if (self::token_matches($token, $ids)) { return true; } } return false; } private static function token_matches($token, array $ids) { switch ($token) { case 'all': return true; case 'front': return is_front_page(); case 'blog': return is_home() || is_singular('post') || is_category() || is_tag(); case 'shop': return function_exists('is_shop') && (is_shop() || (function_exists('is_product_taxonomy') && is_product_taxonomy())); case 'product': return function_exists('is_product') && is_product(); case 'product_cat': return function_exists('is_product_category') && is_product_category(); case 'cart': return function_exists('is_cart') && is_cart(); case 'checkout': return function_exists('is_checkout') && is_checkout(); case 'account': return function_exists('is_account_page') && is_account_page(); case 'ids': return $ids && is_singular() && in_array((int) get_queried_object_id(), array_map('intval', $ids), true); } return false; } private static function conditions_from_meta($post_id) { $post_id = (int) $post_id; $offer = class_exists('AIEO_Popup_Offer', false) ? AIEO_Popup_Offer::window_for($post_id) : array(0, 0, 0); $start = (int) self::to_epoch(self::get($post_id, self::META_START)); $end = (int) self::to_epoch(self::get($post_id, self::META_END)); $own = (int) self::get($post_id, self::META_AUDIENCE_ID); return array( 'start' => max($start, (int) $offer[0]), 'end' => $end > 0 && $offer[1] > 0 ? min($end, (int) $offer[1]) : max($end, (int) $offer[1]), 'trigger' => (string) self::get($post_id, self::META_TRIGGER), 'delayMs' => (int) self::get($post_id, self::META_DELAY_MS), 'scrollPct' => (int) self::get($post_id, self::META_SCROLL_PCT), 'idleS' => (int) self::get($post_id, self::META_IDLE_S), 'freq' => (string) self::get($post_id, self::META_FREQ), 'freqDays' => (int) self::get($post_id, self::META_FREQ_DAYS), 'maxImpr' => (int) self::get($post_id, self::META_MAX_IMPR), 'maxPerSession' => (int) self::get($post_id, self::META_MAX_PER_SESSION), 'instigator' => (string) self::get($post_id, self::META_INSTIGATOR), 'instigatorRef' => (string) self::get($post_id, self::META_INSTIGATOR_REF), 'device' => (string) self::get($post_id, self::META_DEVICE), 'visitor' => (string) self::get($post_id, self::META_VISITOR), 'cart' => (string) self::get($post_id, self::META_CART), 'cartMin' => (float) self::get($post_id, self::META_CART_MIN), 'audience' => $own > 0 ? $own : (int) $offer[2], 'test' => self::get($post_id, self::META_TEST) === 'yes', 'priority' => $post_id > 0 ? (int) get_post_field('menu_order', $post_id) : 10, ); } public static function client_config($post_id) { $post_id = (int) $post_id; $post = get_post($post_id); $identity = array( 'id' => $post_id, 'surface' => self::surface_tag($post_id), 'v' => $post ? (int) strtotime($post->post_modified_gmt . ' UTC') : 0, ); if (!class_exists('AIEO_Display_Conditions')) { return self::with_offer($post_id, array_merge($identity, self::conditions_from_meta($post_id))); } return self::with_offer($post_id, AIEO_Display_Conditions::client_config( self::conditions_from_meta($post_id), $identity )); } private static function with_offer($post_id, array $cfg) { if (class_exists('AIEO_Popup_Offer', false) && AIEO_Popup_Offer::advertises((int) $post_id)) { $cfg['offer'] = (int) get_post_meta((int) $post_id, self::META_OFFER_ID, true); $cfg['offerCart'] = esc_url_raw(rest_url('wc/store/v1/cart')); } return $cfg; } public static function on_save($post_id, $post) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (wp_is_post_revision($post_id)) { return; } if (isset($post->post_status) && $post->post_status === 'publish') { self::mint_surface_tag($post_id); } self::purge_caches(); self::arm_boundaries($post_id); } public static function on_delete($post_id) { if (get_post_type($post_id) !== self::POST_TYPE) { return; } wp_clear_scheduled_hook(self::BOUNDARY_HOOK, array((int) $post_id)); self::purge_caches(); AIEO_Popup_Offer::on_delete((int) $post_id); } public static function arm_boundaries($post_id) { $post_id = (int) $post_id; $args = array($post_id); wp_clear_scheduled_hook(self::BOUNDARY_HOOK, $args); $now = time(); foreach (array(self::META_START, self::META_END) as $key) { $ts = self::to_epoch(self::get($post_id, $key)); if ($ts > $now) { wp_schedule_single_event($ts + 5, self::BOUNDARY_HOOK, $args); } } $offer_end = AIEO_Popup_Offer::end_epoch($post_id); if (null !== $offer_end && $offer_end > $now) { wp_schedule_single_event($offer_end + 5, self::BOUNDARY_HOOK, $args); } } public static function purge_caches() { do_action('aieo_purge_page_cache'); do_action('aieo_popup_caches_purged'); } public static function no_canonical_redirect($redirect, $requested = '') { if (is_singular(self::POST_TYPE)) { return false; } if ('' !== $requested && false !== strpos($requested, self::POST_TYPE . '=')) { return false; } return $redirect; } public static function gate_single_view() { if (!is_singular(self::POST_TYPE)) { return; } $cap = defined('AIEO_CAPABILITY') ? AIEO_CAPABILITY : 'manage_options'; if (current_user_can($cap) || current_user_can('edit_posts')) { return; } global $wp_query; $wp_query->set_404(); status_header(404); nocache_headers(); include get_query_template('404'); exit; } public static function bare_template($template) { if (!is_singular(self::POST_TYPE)) { return $template; } add_filter('wp_robots', 'wp_robots_no_robots'); $bare = defined('AIEO_DIR') ? AIEO_DIR . 'includes/dmm/popups/template-popup-bare.php' : ''; return ($bare && file_exists($bare)) ? $bare : $template; } public static function enqueue_editor_assets() { $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (!$screen || $screen->post_type !== self::POST_TYPE) { return; } wp_enqueue_media(); if (function_exists('rooster_admin_table_assets')) { rooster_admin_table_assets(); } } public static function add_metabox() { add_meta_box( 'aieo-popup-display', __('Popup display', 'ai-eshop-optimizer'), array(__CLASS__, 'render_metabox'), self::POST_TYPE, 'normal', 'high' ); } private static function notice($html, $tone, $detail = '') { return AIEO_DMM_UI::notice($html, $tone, array('detail' => $detail)); } public static function render_metabox($post) { wp_nonce_field(self::NONCE, 'aieo_popup_meta_nonce'); $id = (int) $post->ID; $UI = 'AIEO_Display_Conditions_UI'; if (!class_exists($UI)) { echo '
' . esc_html__('The shared display controls are unavailable. Reactivate Rooster One to edit this popup\'s rules.', 'ai-eshop-optimizer') . '
'; return; } $cond = self::conditions_from_meta($id); $page_labels = array( 'all' => __('Everywhere', 'ai-eshop-optimizer'), 'front' => __('Front page', 'ai-eshop-optimizer'), 'blog' => __('Blog (posts, categories, tags)', 'ai-eshop-optimizer'), 'shop' => __('Shop and product archives', 'ai-eshop-optimizer'), 'product' => __('Product pages', 'ai-eshop-optimizer'), 'product_cat' => __('Product categories', 'ai-eshop-optimizer'), 'cart' => __('Cart', 'ai-eshop-optimizer'), 'checkout' => __('Checkout', 'ai-eshop-optimizer'), 'account' => __('My account', 'ai-eshop-optimizer'), 'ids' => __('Specific page/post IDs', 'ai-eshop-optimizer'), ); $start = (string) self::get($id, self::META_START); $end = (string) self::get($id, self::META_END); $state = self::schedule_state($id); echo ''; echo '