$id, 'label' => self::label($key), 'control' => self::field_control($key, $field, $id, $cfg, $named), 'help' => $describe ? self::help($key) : '', )); } } private static function field_control($key, $field, $id, array $cfg, $named) { $name = $named ? $field : ''; $attrs = array('data-field' => $field); $choices = self::choices($key); if (!empty($choices)) { return \Rooster\Admin\Ui::select($name, $choices, (string) $cfg[$key], array('id' => $id, 'attrs' => $attrs)); } if (is_bool($cfg[$key])) { return ''; } if (is_string($cfg[$key])) { return \Rooster\Admin\Ui::input($name, $cfg[$key], array('id' => $id, 'medium' => true, 'attrs' => $attrs)); } return \Rooster\Admin\Ui::input($name, (string) $cfg[$key], array('id' => $id, 'type' => 'number', 'short' => true, 'min' => '0', 'step' => ('cartMin' === $key) ? '0.01' : '1', 'attrs' => $attrs)); } private static function placement_labels() { return array( 'all' => __('Everywhere it is placed', 'ai-eshop-optimizer'), 'front' => __('Front page', 'ai-eshop-optimizer'), 'blog' => __('Blog posts and the blog index', 'ai-eshop-optimizer'), 'page' => __('Pages', 'ai-eshop-optimizer'), 'product' => __('Product pages', 'ai-eshop-optimizer'), 'shop' => __('Shop and product archives', '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 posts or products', 'ai-eshop-optimizer'), ); } public static function render_placement_rows($stored, array $allowed, $opts = array()) { if (!class_exists('AIEO_Display_Conditions')) { return; } $rules = AIEO_Display_Conditions::sanitize_placement($stored); $prefix = isset($opts['prefix']) ? (string) $opts['prefix'] : 'plc_'; $labels = self::placement_labels(); $with_ex = !empty($opts['exclude']); $id_base = isset($opts['id']) ? (string) $opts['id'] : 'aieo-plc-'; $checks = static function ($infix, array $tokens, array $chosen) use ($prefix, $labels) { $html = '
'; foreach ($tokens as $token) { $html .= ''; } return $html . '
'; }; $offered = array_values(array_filter($allowed, static function ($token) use ($labels) { return isset($labels[$token]); })); $pages = in_array('all', $rules['pages'], true) ? $offered : $rules['pages']; $note = !empty($opts['note']) ? \Rooster\Admin\Ui::notice(esc_html((string) $opts['note']), 'warning', array('inline' => true)) : ''; echo \Rooster\Admin\Ui::field_row(array( 'id' => sanitize_html_class($id_base . $prefix . 'in'), 'label' => __('Where it may appear', 'ai-eshop-optimizer'), 'group' => true, 'control' => $checks('in_', $offered, $pages) . $note, 'help' => __('Untick everything you do not want — with nothing ticked it never appears anywhere. Cart, checkout and my-account are refused by the plugin whatever is ticked here: they are conversion pages and nothing promotional belongs on them.', 'ai-eshop-optimizer'), )); if ($with_ex) { echo \Rooster\Admin\Ui::field_row(array( 'id' => sanitize_html_class($id_base . $prefix . 'ex'), 'label' => __('Never on', 'ai-eshop-optimizer'), 'group' => true, 'control' => $checks('ex_', array_values(array_diff($offered, array('all'))), $rules['exclude']), 'help' => __('Checked here wins over the list above.', 'ai-eshop-optimizer'), )); } $url_id = sanitize_html_class($id_base . $prefix . 'url'); echo \Rooster\Admin\Ui::field_row(array( 'id' => $url_id, 'label' => __('Only on URLs containing', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::input('', (string) $rules['url'], array('id' => $url_id, 'medium' => true, 'placeholder' => __('e.g. /summer-sale', 'ai-eshop-optimizer'), 'attrs' => array('data-field' => $prefix . 'url'))), 'help' => __('Optional. Leave empty for no URL restriction. This is the escape hatch for a page type the list above does not name.', 'ai-eshop-optimizer'), )); } public static function collect_placement($row, $stored, array $offered, $prefix = 'plc_') { if (!class_exists('AIEO_Display_Conditions')) { return is_array($stored) ? $stored : array(); } $row = is_array($row) ? $row : array(); $rules = AIEO_Display_Conditions::sanitize_placement($stored); $truthy = static function ($v) { if (is_bool($v)) { return $v; } return in_array((string) $v, array('1', 'true', 'on', 'yes'), true); }; foreach (array('pages' => 'in_', 'exclude' => 'ex_') as $key => $infix) { $kept = array(); foreach ($rules[$key] as $token) { if (!in_array($token, $offered, true)) { $kept[] = $token; } } $touched = false; foreach ($offered as $token) { $field = $prefix . $infix . $token; if (!array_key_exists($field, $row)) { continue; } $touched = true; if ($truthy($row[$field]) && !in_array($token, $kept, true)) { $kept[] = $token; } } if ($touched) { $rules[$key] = $kept; } } if (array_key_exists($prefix . 'url', $row)) { $rules['url'] = $row[$prefix . 'url']; } if (array_key_exists($prefix . 'ids', $row)) { $raw = is_array($row[$prefix . 'ids']) ? $row[$prefix . 'ids'] : preg_split('/[^0-9]+/', (string) $row[$prefix . 'ids'], -1, PREG_SPLIT_NO_EMPTY); $rules['ids'] = is_array($raw) ? $raw : array(); } return AIEO_Display_Conditions::sanitize_placement($rules); } public static function render_control_cell($key, $field, $stored = array()) { if (!class_exists('AIEO_Display_Conditions')) { return; } $cfg = AIEO_Display_Conditions::sanitize(is_array($stored) ? $stored : array()); if (!array_key_exists($key, $cfg)) { return; } self::control($key, esc_attr($field), $cfg, true); } public static function render_field($key, $field, $stored = array(), $opts = array()) { if (!class_exists('AIEO_Display_Conditions')) { return; } $cfg = AIEO_Display_Conditions::sanitize(is_array($stored) ? $stored : array()); if (!array_key_exists($key, $cfg)) { return; } $label = isset($opts['label']) ? (string) $opts['label'] : self::label($key); $help = array_key_exists('help', $opts) ? (string) $opts['help'] : self::help($key); $only = isset($opts['only']) && is_array($opts['only']) ? $opts['only'] : null; echo '


'; $choices = self::choices($key); if (!empty($choices)) { if (null !== $only) { $kept = array(); foreach ($only as $v) { if (isset($choices[$v])) { $kept[$v] = $choices[$v]; } } if (!isset($kept[$cfg[$key]]) && isset($choices[$cfg[$key]])) { $kept[$cfg[$key]] = $choices[$cfg[$key]]; } $choices = $kept; } echo ''; } elseif (is_bool($cfg[$key])) { echo ''; } elseif (is_string($cfg[$key])) { echo ''; } else { $step = ('cartMin' === $key) ? '0.01' : '1'; echo ''; } if ('' !== $help) { echo '
' . esc_html($help) . ''; } echo '

'; } public static function label_for($key) { return self::label($key); } private static function control($key, $field, array $cfg, $named = false) { $choices = self::choices($key); $addr = 'data-field="' . $field . '"' . ($named ? ' name="' . $field . '"' : ''); if (!empty($choices)) { echo ''; return; } if (is_bool($cfg[$key])) { echo ''; return; } if (is_string($cfg[$key])) { echo ''; return; } $step = ('cartMin' === $key) ? '0.01' : '1'; echo ''; } private static function choices($key) { switch ($key) { case 'trigger': return array( 'load' => __('As soon as the page loads', 'ai-eshop-optimizer'), 'delay' => __('After a delay', 'ai-eshop-optimizer'), 'scroll' => __('At a scroll depth', 'ai-eshop-optimizer'), 'idle' => __('After the visitor goes idle', 'ai-eshop-optimizer'), 'exit' => __('On exit intent', 'ai-eshop-optimizer'), 'approach' => __('When it comes into view (recommended)', 'ai-eshop-optimizer'), 'interaction' => __('On the first interaction with the page', 'ai-eshop-optimizer'), ); case 'freq': return array( 'always' => __('Every time', 'ai-eshop-optimizer'), 'session' => __('Once per browser session', 'ai-eshop-optimizer'), 'once' => __('Once ever', 'ai-eshop-optimizer'), 'days' => __('Once every N days', 'ai-eshop-optimizer'), ); case 'device': return array( 'all' => __('All devices', 'ai-eshop-optimizer'), 'desktop' => __('Desktop only', 'ai-eshop-optimizer'), 'mobile' => __('Mobile only', 'ai-eshop-optimizer'), ); case 'visitor': return array( 'all' => __('Everyone', 'ai-eshop-optimizer'), 'guest' => __('Logged-out visitors', 'ai-eshop-optimizer'), 'logged_in' => __('Logged-in customers', 'ai-eshop-optimizer'), 'new' => __('First-time visitors', 'ai-eshop-optimizer'), 'returning' => __('Returning visitors', 'ai-eshop-optimizer'), ); case 'cart': return array( 'any' => __('Any cart', 'ai-eshop-optimizer'), 'empty' => __('Empty cart', 'ai-eshop-optimizer'), 'has_items' => __('Cart has items', 'ai-eshop-optimizer'), 'min_total' => __('Cart is over a minimum', 'ai-eshop-optimizer'), ); case 'instigator': return array( 'any' => __('However they arrived', 'ai-eshop-optimizer'), 'web_push' => __('From a web push notification', 'ai-eshop-optimizer'), 'newsletter' => __('From an email or newsletter', 'ai-eshop-optimizer'), 'paid' => __('From a paid ad', 'ai-eshop-optimizer'), 'referral' => __('From another site or a collaborator', 'ai-eshop-optimizer'), 'direct' => __('Typed the address or used a bookmark', 'ai-eshop-optimizer'), ); } return array(); } private static function label($key) { $map = array( 'trigger' => __('Show it', 'ai-eshop-optimizer'), 'delayMs' => __('Delay (ms)', 'ai-eshop-optimizer'), 'scrollPct' => __('Scroll depth (%)', 'ai-eshop-optimizer'), 'idleS' => __('Idle time (seconds)', 'ai-eshop-optimizer'), 'rootMargin' => __('Load it this early (px)', 'ai-eshop-optimizer'), 'reserveHeight' => __('Hold space for it (px)', 'ai-eshop-optimizer'), 'freq' => __('How often', 'ai-eshop-optimizer'), 'freqDays' => __('Every N days', 'ai-eshop-optimizer'), 'maxImpr' => __('Lifetime limit', 'ai-eshop-optimizer'), 'maxPerSession' => __('Times per session', 'ai-eshop-optimizer'), 'device' => __('Devices', 'ai-eshop-optimizer'), 'visitor' => __('Who sees it', 'ai-eshop-optimizer'), 'cart' => __('Cart state', 'ai-eshop-optimizer'), 'cartMin' => __('Cart minimum', 'ai-eshop-optimizer'), 'audience' => __('Saved audience', 'ai-eshop-optimizer'), 'instigator' => __('What sent them here', 'ai-eshop-optimizer'), 'instigatorRef' => __('Which campaign or collaborator', 'ai-eshop-optimizer'), 'priority' => __('Priority', 'ai-eshop-optimizer'), 'test' => __('Test mode', 'ai-eshop-optimizer'), ); return isset($map[$key]) ? $map[$key] : $key; } private static function checkbox_text($key) { if ('test' === $key) { return __('Show it to me on every page, ignoring every rule below. Records no analytics.', 'ai-eshop-optimizer'); } return __('Enabled', 'ai-eshop-optimizer'); } private static function help($key) { switch ($key) { case 'trigger': return __('"When it comes into view" is the fastest option: a block the visitor never scrolls to costs them nothing at all, because nothing it needs is downloaded until it is nearly on screen.', 'ai-eshop-optimizer'); case 'rootMargin': return __('How far below the fold to start loading, so it is ready by the time the visitor reaches it. Larger is smoother but starts the download sooner.', 'ai-eshop-optimizer'); case 'reserveHeight': return __('Keeps the page from jumping while the content loads. Space is held only when this block is actually going to appear, so a visitor who has already seen it as many times as you allow gets no empty gap. It is used on the FIRST view only: after that the browser remembers the real height, separately for phones and desktops, and uses that instead. Enter roughly how tall it is ON A PHONE if that is where most of your visitors are.', 'ai-eshop-optimizer'); case 'maxPerSession': return __('How many times this block may appear while the visitor keeps their browser open. 0 means no limit. It is counted per BLOCK, not per page view, so pages that never contained it do not use up the allowance.', 'ai-eshop-optimizer'); case 'maxImpr': return __('A cap across the visitor\'s whole history, on top of whichever "How often" rule is chosen. 0 means no limit.', 'ai-eshop-optimizer'); case 'freqDays': return __('Only used when "How often" is set to "Once every N days".', 'ai-eshop-optimizer'); case 'delayMs': return __('1000 ms is one second. Only used by the "After a delay" trigger.', 'ai-eshop-optimizer'); case 'priority': return __('When two blocks want the screen at the same moment, the lower number wins.', 'ai-eshop-optimizer'); case 'audience': return __('The id of a saved audience. Leave at 0 for everybody. Logged-out visitors never match an audience.', 'ai-eshop-optimizer'); case 'cartMin': return __('Only used when "Cart state" is set to "Cart is over a minimum".', 'ai-eshop-optimizer'); case 'instigator': return __('Show this only to visitors who arrived a particular way. The visit is remembered for 30 minutes, so it still applies once they have clicked through to a second page.', 'ai-eshop-optimizer'); case 'instigatorRef': return __('Optional. Narrows the rule to one campaign or collaborator — enter any part of the tag on their links, e.g. the campaign name or their code. Leave empty for all of them. Matching ignores capitals.', 'ai-eshop-optimizer'); } return ''; } public static function collect($row, $stored = array(), $prefix = 'cond_') { $row = is_array($row) ? $row : array(); $merged = is_array($stored) ? $stored : array(); $len = strlen($prefix); foreach ($row as $k => $v) { if (strncmp((string) $k, $prefix, $len) !== 0) { continue; } $merged[substr((string) $k, $len)] = $v; } return AIEO_Display_Conditions::sanitize($merged); } }