\n" . $viewport_code . "\n
\n";
} else $processed_code .= $invisible_label . $viewport_code;
}
}
}
}
return $processed_code;
}
public function ai_processFallbackSeparator ($processed_code) {
global $ai_wp_data;
$this->fallback_names = null;
if (strpos ($processed_code, AD_FALLBACK_SEPARATOR) !== false) {
$codes = explode (AD_FALLBACK_SEPARATOR, $processed_code);
if ($ai_wp_data [AI_FORCE_SERVERSIDE_CODE]) {
// Code for preview
if ($this->fallback_index >= count ($codes)) {
$this->fallback_index = 0;
}
$processed_code = trim ($codes [$this->fallback_index]);
$this->fallback_names []= _x('INACTIVE', 'fallback', 'ad-inserter');
$this->fallback_names []= _x('ACTIVE', 'fallback', 'ad-inserter');
} else {
$processed_code = $codes [0];
if (!$ai_wp_data [AI_WP_AMP_PAGE]) {
// AdSense unfilled
if (strtolower ($this->shortcodes ['fallback'][0]['fallback']) == 'adsense') {
$insert_code = get_dynamic_blocks () == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT;
switch (strtolower ($this->shortcodes ['fallback'][0]['code'])) {
case 'insert':
$insert_code = true;
break;
case 'show':
$insert_code = false;
break;
}
$debug_fallback_code = '';
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$debug_fallback = new ai_block_labels ('ai-debug-fallback');
$debug_fallback_code = $debug_fallback->bar (_x('FALLBACK CODE', 'Block', 'ad-inserter'), '', 'AdSense UNFILLED', ' ');
}
$empty_fallback_code_class = trim ($codes [1]) == '' ? " ai-empty-code" : '';
$processed_code = "" . $processed_code . "
\n";
if ($insert_code) {
$ai_code = $this->base64_encode_w3tc (ai_strip_js_markers ($debug_fallback_code . trim ($codes [1])), false);
$processed_code .= "\n" . $debug_fallback_code . trim ($codes [1]) . "\n
\n";
}
}
}
}
}
return $processed_code;
}
public function ai_getProcessedCode ($force_close_button = false) {
global $ai_wp_data, $ad_inserter_globals, $block_object, $ai_total_hook_php_time, $filter_hooks;
// Clear the codes for cases when the code block is called more than once
$this->additional_code_before = '';
$this->additional_code_after = '';
$this->additional_code_before_block = '';
$this->w3tc_code = '';
$this->w3tc_debug = array ();
$this->no_insertion_text = '';
$this->rotate_names = null;
$not_iframe_or_inside = !$this->get_iframe () || $ai_wp_data [AI_CODE_FOR_IFRAME];
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
// Code for ad label, close button
$label_code = '';
$additional_code = '';
$check_block_code = false;
if ($this->get_iframe () ? ($this->get_label_in_iframe () ? $ai_wp_data [AI_CODE_FOR_IFRAME] : !$ai_wp_data [AI_CODE_FOR_IFRAME]) : true) {
$label_code = $this->ai_getAdLabel ();
}
$close_button = $this->get_close_button ();
$auto_close_time = $this->get_auto_close_time ();
$stay_closed_time = $this->get_stay_closed_time ();
$closed_code = '';
if (!$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$alignment_type = $this->get_alignment_type ();
if ($force_close_button ||
(($close_button != AI_CLOSE_NONE || $auto_close_time) &&
!$ai_wp_data [AI_WP_AMP_PAGE] &&
$alignment_type != AI_ALIGNMENT_NO_WRAPPING &&
!(($alignment_type == AI_ALIGNMENT_STICKY) && $this->get_background ())
)
) {
switch ($close_button) {
case AI_CLOSE_TOP_RIGHT:
$button_class = 'ai-close-button ai-close-unprocessed';
break;
case AI_CLOSE_TOP_LEFT:
$button_class = 'ai-close-button ai-close-unprocessed ai-close-left';
break;
case AI_CLOSE_BOTTOM_RIGHT:
$button_class = 'ai-close-button ai-close-unprocessed ai-close-bottom';
break;
case AI_CLOSE_BOTTOM_LEFT:
$button_class = 'ai-close-button ai-close-unprocessed ai-close-bottom ai-close-left';
break;
default:
$button_class = 'ai-close-button ai-close-unprocessed ai-close-none';
break;
}
$timeout_code = '';
if ($auto_close_time) {
$timeout_code = " data-ai-close-timeout='{$auto_close_time}'";
}
if ($stay_closed_time) {
$closed_code = " data-ai-closed-time='{$stay_closed_time}'";
}
$closed_block_code = '';
// All blocks with close button need block number
// if ($closed_code != '' || $timeout_code != '') {
$closed_block_code = " data-ai-block='{$this->number}'";
// }
$additional_code .= "'."\n" . $additional_code . '
'."\n";
// Block wrapping div needs to be positioned relative for bottom close button positions
if ($additional_code != '') $additional_code_org .= ''."\n" . $additional_code . '
'."\n";
$hook_start_time = microtime (true);
$additional_code = apply_filters ("ai_block_additional_code", $additional_code_org, $this->number);
if ($debug_processing && $additional_code != $additional_code_org) {
$filter_hooks []= array ("ai_block_additional_code", $this->number);
}
$ai_total_hook_php_time += microtime (true) - $hook_start_time;
}
$processed_code = '';
// TODO single CHECK block
// do {
if ($this->get_iframe () && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$width = trim ($this->get_iframe_width ());
$height = trim ($this->get_iframe_height ());
$iframe_style = ($width != '' ? 'width: ' . $width . 'px; ' : 'width: 100%; ') . ($height != '' ? 'height: ' . $height . 'px;' : 'height: 0px;');
$iframe_parameters = '';
if (get_dynamic_blocks () != AI_DYNAMIC_BLOCKS_SERVER_SIDE) {
if (isset ($_SERVER['HTTP_REFERER'])) {
$referer_host = strtolower (parse_url ($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
} else $referer_host = '';
$iframe_parameters .= '&referrer='.urlencode_deep ($referer_host);
}
if ($this->client_side_cookie_check) {
$iframe_parameters .= '&cookie_check=1';
}
if ($this->client_side_cookie_check_url) {
$iframe_parameters .= '&cookie_check_url=1';
}
if ($this->hide_debug_labels) {
$iframe_parameters .= '&hide-debug-labels=1';
}
$iframe_parameters .= '&rnd=' . rand (1, 10000000000);
foreach ($_GET as $url_parameter => $url_parameter_value) {
if (in_array ($url_parameter, array ('action', 'block', 'referrer', 'cookie_check', 'hide-debug-labels', 'rnd'))) continue;
$iframe_parameters .= '&'. $url_parameter . '=' . $url_parameter_value;
}
$attributes = '';
if (!get_disable_js_code () && ($height == '' || ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0)) {
$attributes = ' onload="ai_resize_iframe (this);"';
}
$code = '\n";
} else $processed_code .= "\n
\n";
foreach ($ads as $index => $ad) {
// If AMP separator is present use only code for normal pages
if (strpos ($ad, AD_AMP_SEPARATOR) !== false) {
$codes = explode (AD_AMP_SEPARATOR, $ad);
$ad = trim ($codes [0]);
}
if (strpos ($ad, AD_HEAD_SEPARATOR) !== false) {
$head_body_code = explode (AD_HEAD_SEPARATOR, $ad );
$ad = trim ($head_body_code [1]);
// Insert all HEAD codes for all options into section
// if ($ai_wp_data [AI_SHORTCODES]['head'][0]['group'] != '') {
if ($this->shortcodes ['head'][0]['group'] != '') {
// $ai_wp_data [AI_HEAD_GROUPS][strtolower ($ai_wp_data [AI_SHORTCODES]['head'][0]['group'])] []= trim ($head_body_code [0]);
$ai_wp_data [AI_HEAD_GROUPS][strtolower ($this->shortcodes ['head'][0]['group'])] []= trim ($head_body_code [0]);
} else $ai_wp_data [AI_HEAD_CODES] []= trim ($head_body_code [0]);
}
$ad = $this->ai_processViewportSeparators ($ad);
$ad = $this->ai_processFallbackSeparator ($ad);
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_list = new ai_block_labels ('ai-debug-iframe');
if (preg_match ($groups_marker, $ad, $matches)) {
$current_group_name = implode (', ', json_decode (base64_decode ($matches [1])));
} else $current_group_name = '';
$ad = preg_replace ("#(
)#", '$1' . $debug_list->bar (__('ACTIVATED GROUPS', 'ad-inserter') . ': ' . $current_group_name, '', ''), $ad);
}
$tracking_index_data = ' data-index="'.$tracking_indexes [$index].'"';
$version_name_data = ' data-name="' .base64_encode ($version_names [$index]).'"';
$version_time_data = $version_times [$index] >= 0 ? ' data-time="'.base64_encode ($version_times [$index]).'"' : '';
$version_scheduling_data = $version_scheduling [$index] != - 1 ? ' data-scheduling="'.base64_encode ($version_scheduling [$index]).'"' : '';
$version_group_data = $groups ? ' data-group="'.base64_encode ($version_groups [$index]).'"' : '';
switch ($rotation_dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
switch ($index) {
case 0:
if (defined ('AI_NORMAL_HEADER_STYLES') && AI_NORMAL_HEADER_STYLES && !get_inline_styles ()) {
$processed_code .= "
\n".trim ($ad, "\n\r")."
\n";
} else
$processed_code .= "
\n".trim ($ad, "\n\r")."
\n";
break;
default:
if (defined ('AI_NORMAL_HEADER_STYLES') && AI_NORMAL_HEADER_STYLES && !get_inline_styles ()) {
$processed_code .= "
\n".trim ($ad, "\n\r")."
\n";
} else
$processed_code .= "
\n".trim ($ad, "\n\r")."
\n";
break;
}
break;
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
$version_code_data = ' data-code="'.base64_encode (ai_strip_js_markers ($ad)).'"';
$processed_code .= '
\n
\n";
break;
}
}
$processed_code .= "
\n";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME] && !get_disable_js_code ()) {
// $processed_code .= "\n";
$processed_code .= "\n";
}
break;
case AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC:
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS ROTATE';
}
if ($groups) {
$ad_index_code = ' global $ai_groups; $ai_index = 0; if (isset ($ai_groups) && count ($ai_groups) != 0) {foreach ($ai_groups as $group_name) {foreach (unserialize (base64_decode (\''.
base64_encode (serialize ($version_groups)).'\')) as $index => $version_group) {if ($version_group == trim ($group_name)) {$ai_index = $index + 1; break 2;}}}}';
}
elseif ($shares) {
$ad_index_code = ' $ai_random_threshold = mt_rand (0, 100); $ai_thresholds = unserialize (\''.
serialize ($thresholds).'\'); foreach ($ai_thresholds as $ai_option_index => $ai_threshold) {$ai_index = $ai_option_index + 1; if ($ai_random_threshold <= $ai_threshold) break;}';
}
elseif ($scheduling) {
$ad_index_code = ' $ai_index = 0; $ai_scheduling_data = unserialize (\''.
serialize ($version_scheduling).'\'); foreach ($ai_scheduling_data as $ai_option_index => $ai_scheduling_data_item) {if (check_scheduled_rotation ($ai_scheduling_data_item)) {$ai_index = $ai_option_index + 1; break;}}';
}
elseif ($unique) {
$ad_index_code = ' if (!defined (\'AI_W3TC_ROTATION_SEED\')) define (\'AI_W3TC_ROTATION_SEED\', mt_rand (1, time ()) % '.count ($ads).');';
$ad_index_code .= ' $ai_block_counter = '.(isset ($ad_inserter_globals [AI_BLOCK_COUNTER_NAME . $this->number]) ? $ad_inserter_globals [AI_BLOCK_COUNTER_NAME . $this->number] : 0).';';
$ad_index_code .= ' $ai_index = AI_W3TC_ROTATION_SEED + $ai_block_counter; if ($ai_index > '.count ($ads).') $ai_index -= '.count ($ads).';';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$ad_index_code .= 'ai_w3tc_log_run (\'PROCESS UNIQUE, SEED: \' . AI_W3TC_ROTATION_SEED . \', \' . $ai_block_counter);';
}
}
else $ad_index_code = ' $ai_index = mt_rand (1, '.count ($ads).');';
$this->w3tc_code .= '$ai_code = unserialize (base64_decode (\''.base64_encode (serialize ($ads)).'\'));'.$ad_index_code;
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= 'ai_w3tc_log_run (\'PROCESS ROTATE: \' . $ai_index);';
}
$this->w3tc_code .= ' if ($ai_index != 0) {$ai_dummy1 = $ai_dummy2 = null; $ai_code = ai_w3tc_execute_php ($ai_code [$ai_index - 1], $ai_dummy1, $ai_dummy2); $ai_enabled = true;} else {$ai_code = \'\'; $ai_enabled = false;}';
// Set $ai_version_name according to $ai_index and and $ai_index to the index for tracking
$this->w3tc_code .= ' if ($ai_index != 0) {$version_names = unserialize (base64_decode (\''.base64_encode (serialize ($this->rotate_names)).'\')); $ai_version_name = $version_names [$ai_index - 1]; $tracking_indexes = unserialize (base64_decode (\''.base64_encode (serialize ($tracking_indexes)).'\')); $ai_index = $tracking_indexes [$ai_index - 1];';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' ai_w3tc_log_run (\'TRACKING INDEX: \' . $ai_index . (isset ($ai_version_name) ? \' [\'.$ai_version_name.\']\' : \'\'));';
}
$this->w3tc_code .= '}';
$this->w3tc_code .= ' if ($ai_enabled) {$groups_marker = base64_decode (\'' . base64_encode ($groups_marker) .
'\'); global $ai_groups; if (preg_match ($groups_marker, $ai_code, $matches)) {if (!isset ($ai_groups)) $ai_groups = array (); $ai_groups = array_merge ($ai_groups, json_decode (base64_decode ($matches [1]))); $ai_code = preg_replace ($groups_marker, \'\', $ai_code);}}';
$processed_code = $this->generate_html_from_w3tc_code ();
break;
}
}
// [ADINSERTER AMP]
// [ADINSERTER HEAD]
$this->labels->class = 'ai-debug-default'; // If previously inserted block with AMP separator (multiple CHECK insertions)
$amp_head_dynamic_blocks = $dynamic_blocks;
if ($ai_wp_data [AI_FORCE_SERVERSIDE_CODE]) $amp_head_dynamic_blocks = AI_DYNAMIC_BLOCKS_SERVER_SIDE;
elseif ($amp_head_dynamic_blocks == AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC && $this->w3tc_code == '') $amp_head_dynamic_blocks = AI_DYNAMIC_BLOCKS_SERVER_SIDE;
elseif ($amp_head_dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW) $amp_head_dynamic_blocks = AI_DYNAMIC_BLOCKS_SERVER_SIDE;
elseif ($amp_head_dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT) $amp_head_dynamic_blocks = AI_DYNAMIC_BLOCKS_SERVER_SIDE;
switch ($amp_head_dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_SERVER_SIDE:
if (strpos ($processed_code, AD_AMP_SEPARATOR) !== false) {
$codes = explode (AD_AMP_SEPARATOR, $processed_code);
$code_index = $ai_wp_data [AI_WP_AMP_PAGE] ? 1 : 0;
$this->labels->class = $code_index ? 'ai-debug-amp' : 'ai-debug-default';
if ($code_index == 1) {
ai_log ('BLOCK ' . $this->number . ' AMP CODE USED');
}
$processed_code = trim ($codes [$code_index]);
if ($this->empty_code ($processed_code)) {
ai_log ('AMP SEPARATOR ' . ($ai_wp_data [AI_WP_AMP_PAGE] ? '[AMP]' : '[NORMAL]') . ': EMPTY CODE FOR BLOCK ' . $this->number);
$this->code_empty = true;
$additional_code = '';
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_label = new ai_block_labels ('ai-w3tc-debug');
// translators: %s: NO CODE FOR INSERTION ON NORMAL/AMP PAGES
$processed_code = $debug_label->bar ('', '', sprintf (__('NO CODE FOR INSERTION ON %s PAGES', 'ad-inserter'), $ai_wp_data [AI_WP_AMP_PAGE] ? 'AMP' :
// translators: %s: NO CODE FOR INSERTION ON NORMAL/AMP PAGES
__('NORMAL', 'ad-inserter')));
}
}
} else {
// AMP page but No AMP separator - don't insert the code unless enabled
if ($ai_wp_data [AI_WP_AMP_PAGE]) {
if (!$this->get_enable_amp ()) {
$processed_code = '';
$additional_code = '';
$this->hide_debug_labels = true;
ai_log ('BLOCK ' . $this->number . ' NOT ENABLED FOR AMP PAGES - EMPTY CODE');
$this->code_empty = true;
}
}
}
if (strpos ($processed_code, AD_HEAD_SEPARATOR) !== false) {
ai_log ('BLOCK ' . $this->number . ' HEAD CODE');
$head_body_code = explode (AD_HEAD_SEPARATOR, $processed_code);
$processed_code = trim ($head_body_code [1]);
// $once = isset ($ai_wp_data [AI_SHORTCODES]['head'][0]['head']) && strtolower ($ai_wp_data [AI_SHORTCODES]['head'][0]['head']) == 'once';
$once = isset ($this->shortcodes ['head'][0]['head']) && strtolower ($this->shortcodes ['head'][0]['head']) == 'once';
if (!$once || !$this->head_code_written) {
// if ($ai_wp_data [AI_SHORTCODES]['head'][0]['group'] != '') {
if ($this->shortcodes ['head'][0]['group'] != '') {
// $ai_wp_data [AI_HEAD_GROUPS][strtolower ($ai_wp_data [AI_SHORTCODES]['head'][0]['group'])] []= trim ($head_body_code [0], "\n\r");
$ai_wp_data [AI_HEAD_GROUPS][strtolower ($this->shortcodes ['head'][0]['group'])] []= trim ($head_body_code [0], "\n\r");
} else $ai_wp_data [AI_HEAD_CODES] []= trim ($head_body_code [0]);
$this->head_code_written = true;
}
}
// $processed_code = $additional_code . $processed_code;
break;
case AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC:
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS AMP, HEAD';
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$this->labels->class = $ai_wp_data [AI_WP_AMP_PAGE] ? 'ai-debug-amp' : 'ai-debug-default';
}
$this->w3tc_code .= '$ai_amp_separator = base64_decode (\'' . base64_encode (AD_AMP_SEPARATOR) . '\'); $ai_amp_page = ' . ($ai_wp_data [AI_WP_AMP_PAGE] ? 'true' : 'false') . '; $ai_amp_enabled = ' . $this->get_enable_amp () . ';';
$this->w3tc_code .= '$ai_dummy1 = $ai_dummy2 = null; $ai_code = ai_w3tc_execute_php ($ai_code, $ai_dummy1, $ai_dummy2);';
// Fix to prevent converting && into && on AMP pages
// $this->w3tc_code .= 'if (strpos ($ai_code, $ai_amp_separator) !== false) {$codes = explode ($ai_amp_separator, $ai_code); $ai_code = trim ($codes [$ai_amp_page ? 1 : 0]); } else {if ($ai_amp_page && !$ai_amp_enabled) $ai_code = \'\';} $ai_enabled = true;';
$this->w3tc_code .= 'if (strpos ($ai_code, $ai_amp_separator) !== false) {$codes = explode ($ai_amp_separator, $ai_code); $ai_code = trim ($codes [$ai_amp_page ? 1 : 0]); } else {if ($ai_amp_page + !$ai_amp_enabled == 2) $ai_code = \'\';} $ai_enabled = true;';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' ai_w3tc_log_run (\'PROCESS AMP: \' . ($ai_amp_page ? \'AMP PAGE\' : \'NORMAL PAGE\'));';
}
// Process HEAD separator
$this->w3tc_code .= '$ai_head_separator = base64_decode (\'' . base64_encode (AD_HEAD_SEPARATOR) . '\');';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' ai_w3tc_log_run (\'PROCESS HEAD: \' . (strpos ($ai_code, $ai_head_separator) !== false ? \'CODE\' : \'-\'));';
}
$this->w3tc_code .= 'if (strpos ($ai_code, $ai_head_separator) !== false) {$codes = explode ($ai_head_separator, $ai_code); $ai_code = trim ($codes [1]);} $ai_enabled = true;';
$processed_code = $this->generate_html_from_w3tc_code ();
break;
}
// [ADINSERTER VIEWPORT]
if ($this->w3tc_code != '') {
// if (isset ($ai_wp_data [AI_SHORTCODES]['viewport'])) {
if (isset ($this->shortcodes ['viewport'])) {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS VIEWPORT SEPARATORS';
}
// $this->w3tc_code .= 'if ($ai_enabled) $ai_code = ai_process_viewport_separators ($ai_code, unserialize (base64_decode (\''.base64_encode (serialize ($ai_wp_data [AI_SHORTCODES]['viewport'])).'\')));';
$this->w3tc_code .= 'if ($ai_enabled) $ai_code = ai_process_viewport_separators ($ai_code, unserialize (base64_decode (\''.base64_encode (serialize ($this->shortcodes)).'\')));';
$processed_code = $this->generate_html_from_w3tc_code ();
}
} else $processed_code = $this->ai_processViewportSeparators ($processed_code);
// [ADINSERTER FALLBACK]
if ($this->w3tc_code != '') {
if (isset ($this->shortcodes ['fallback'])) {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS FALLBACK SEPARATOR';
}
$this->w3tc_code .= 'if ($ai_enabled) $ai_code = ai_process_fallback_separator ($ai_code, unserialize (base64_decode (\''.base64_encode (serialize ($this->shortcodes)).'\')));';
$processed_code = $this->generate_html_from_w3tc_code ();
}
} else $processed_code = $this->ai_processFallbackSeparator ($processed_code);
}
if ($this->check_block) {
$this->check_block_additional_code = $additional_code;
$additional_code = '';
}
// Additional code (Ad label, close button, parallax backgrounds)
if ($dynamic_blocks == AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC && $this->w3tc_code != '' && !defined ('AI_NO_W3TC') && $additional_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS ADDITIONAL CODE';
}
$this->w3tc_code .= 'if ($ai_code != \'\') $ai_code = base64_decode (\''.base64_encode ($additional_code).'\') . $ai_code;';
$processed_code = $this->generate_html_from_w3tc_code ();
} else $processed_code = $additional_code . $processed_code;
if (!$ai_wp_data [AI_CODE_FOR_IFRAME] && !$ai_wp_data [AI_FORCE_SERVERSIDE_CODE]) {
// PROTECTED
if (function_exists ('ai_process_protected_code')) $processed_code = ai_process_protected_code ($this, $processed_code);
// LAZY LOADING
if ($this->get_lazy_loading () && !$ai_wp_data [AI_WP_AMP_PAGE]) {
if ($this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS LAZY LOADING';
}
}
$lazy_code = $this->base64_encode_w3tc ($processed_code);
$wrapper_class = base64_encode (get_block_class_name (true));
$processed_code = '
'."\n";
// Recreate W3TC code
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
// MANUAL LOADING ENABLED
if ($this->get_manual_loading () == AI_MANUAL_LOADING_ENABLED && !$ai_wp_data [AI_WP_AMP_PAGE]) {
if ($this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS MANUAL LOADING ALWAYS';
}
}
$manual_code = $this->base64_encode_w3tc ($processed_code);
$wrapper_class = base64_encode (get_block_class_name (true));
$processed_code = '
'."\n";
// Recreate W3TC code
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
}
if ($not_iframe_or_inside) {
if (function_exists ('ai_adb_check') && !$ai_wp_data [AI_FORCE_SERVERSIDE_CODE] && $not_iframe_or_inside) $processed_code = ai_adb_check ($this, $processed_code);
// Reset if multiple block insertions
$this->client_side_list_detection = false;
$lists_dynamic_blocks = $dynamic_blocks; // replace with $this->server_side_check
if ($ai_wp_data [AI_FORCE_SERVERSIDE_CODE] || ($lists_dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW || $lists_dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT) && $ai_wp_data [AI_WP_AMP_PAGE]) $lists_dynamic_blocks = AI_DYNAMIC_BLOCKS_SERVER_SIDE;
// LISTS, COOKIE, SCHEDULING
if ($lists_dynamic_blocks != AI_DYNAMIC_BLOCKS_SERVER_SIDE) {
// Url parameters, cookies, referrers, clients
do {
$scheduling_start_time = '';
$scheduling_end_time = '';
$scheduling_days_in_week = '';
$scheduling_type = null;
$scheduling_fallback_block = 0;
$check_again = false;
if (isset ($this->check_url_parameters) || isset ($this->check_cookies) || isset ($this->check_referers) || isset ($this->check_clients) || isset ($this->check_scheduling_start_time)) {
$url_parameters_raw = '';
$url_parameter_list_type = '';
$cookies_raw = '';
$cookie_list_type = '';
$referers_raw = '';
$referer_list_type = '';
$clients_raw = '';
$client_list_type = '';
if (isset ($this->check_url_parameters)) {
$url_parameters_raw = trim ($this->check_url_parameters);
$url_parameter_list_type = $this->check_url_parameter_list_type;
}
if (isset ($this->check_cookies)) {
$cookies_raw = trim ($this->check_cookies);
$cookie_list_type = $this->check_cookie_list_type;
}
if (isset ($this->check_referers)) {
$referers_raw = trim (str_replace (' ', '', strtolower ($this->check_referers)));
$referer_list_type = $this->check_referers_list_type;
}
if (isset ($this->check_clients)) {
// $clients_raw = trim (str_replace (' ', '', strtolower ($this->check_clients)));
$clients_raw = trim ($this->check_clients);
$client_list_type = $this->check_clients_list_type;
}
if (isset ($this->check_scheduling_start_time)) {
$scheduling_start_time_raw = trim ($this->check_scheduling_start_time);
$scheduling_start_time = base64_encode ($scheduling_start_time_raw);
$scheduling_end_time_raw = trim ($this->check_scheduling_end_time);
$scheduling_end_time = base64_encode ($scheduling_end_time_raw);
$scheduling_days_in_week_raw = $this->check_scheduling_days_in_week;
$scheduling_days_in_week = base64_encode ($scheduling_days_in_week_raw);
$scheduling_type = $this->check_scheduling_type;
$scheduling_fallback_block = $this->check_scheduling_fallback_block;
}
unset ($this->check_url_parameters);
unset ($this->check_url_parameter_list_type);
unset ($this->check_cookies);
unset ($this->check_cookie_list_type);
unset ($this->check_referers);
unset ($this->check_referers_list_type);
unset ($this->check_clients);
unset ($this->check_clients_list_type);
unset ($this->check_scheduling_start_time);
unset ($this->check_scheduling_end_time);
unset ($this->check_scheduling_days_in_week);
unset ($this->check_scheduling_type);
unset ($this->check_scheduling_fallback_block);
$check_again = true;
} else {
$url_parameters_raw = trim ($this->get_url_parameter_list ());
$url_parameter_list_type = $this->get_url_parameter_list_type ();
$cookies_raw = trim ($this->get_cookie_list ());
$cookie_list_type = $this->get_cookie_list_type ();
$referers_raw = trim (str_replace (' ', '', strtolower ($this->get_ad_domain_list ())));
$referer_list_type = $this->get_ad_domain_list_type ();
// $clients_raw = trim (str_replace (' ', '', strtolower ($this->get_client_list ())));
$clients_raw = trim ($this->get_client_list ());
$client_list_type = $this->get_client_list_type ();
switch ($this->get_scheduling ()) {
case AI_SCHEDULING_BETWEEN_DATES:
case AI_SCHEDULING_OUTSIDE_DATES:
$scheduling_start_time_raw = trim ($this->get_schedule_start_date () . ' ' . $this->get_schedule_start_time ());
$scheduling_start_time = base64_encode ($scheduling_start_time_raw);
$scheduling_end_time_raw = trim ($this->get_schedule_end_date () . ' ' . $this->get_schedule_end_time ());
$scheduling_end_time = base64_encode ($scheduling_end_time_raw);
$scheduling_days_in_week_raw = $this->get_schedule_weekdays ();
$scheduling_days_in_week = base64_encode ($scheduling_days_in_week_raw);
$scheduling_type = $this->get_scheduling();
$scheduling_fallback_block = intval ($this->get_scheduling_fallback());
break;
}
}
$url_parameters = base64_encode (html_entity_decode ($url_parameters_raw));
$cookies = base64_encode (html_entity_decode ($cookies_raw));
$referers = base64_encode ($referers_raw);
$clients = base64_encode (html_entity_decode ($clients_raw));
if (($this->client_side_cookie_check_url && $url_parameters != '') || ($this->client_side_cookie_check && $cookies != '') || $referers != '' || $clients != '' || $scheduling_type !== null) {
switch ($dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
if ($url_parameter_list_type == AI_BLACK_LIST) $url_parameter_list_type = 'B'; else $url_parameter_list_type = 'W';
if ($cookie_list_type == AI_BLACK_LIST) $cookie_list_type = 'B'; else $cookie_list_type = 'W';
if ($referer_list_type == AI_BLACK_LIST) $referer_list_type = 'B'; else $referer_list_type = 'W';
if ($client_list_type == AI_BLACK_LIST) $client_list_type = 'B'; else $client_list_type = 'W';
if ($scheduling_type == AI_SCHEDULING_OUTSIDE_DATES) {
$scheduling_type_code = 'B';
$scheduling_type_text = 'OUT';
} else {
$scheduling_type_code = 'W';
$scheduling_type_text = 'IN';
}
$gmt = get_option ('gmt_offset') * 3600 * 1000;
$fallback_code = '';
$fallback_code_data = '';
$fallback_block_data = 0;
$fallback_level = '';
$fallback_tracking_data = '';
if ($scheduling_type !== null) {
$fallback_block = $scheduling_fallback_block;
if ($fallback_block != $this->number && $fallback_block >= 1 && $fallback_block <= 96) {
$globals_name = AI_FALLBACK_DEPTH_NAME;
if (!isset ($ad_inserter_globals [$globals_name])) {
$ad_inserter_globals [$globals_name] = 0;
}
if ($ad_inserter_globals [$globals_name] < 3) {
$ad_inserter_globals [$globals_name] ++;
$fallback_level = $ad_inserter_globals [$globals_name];
$fallback_block_data = $fallback_block;
$fallback_obj = $block_object [$fallback_block];
$fallback_code = $fallback_obj->ai_getProcessedCode ();
$fallback_code_data = " data-fallback-code='" . base64_encode ($fallback_code) . "'";
$fallback_tracking_block = $fallback_obj->get_tracking () ? $fallback_obj->number : 0;
// $fallback_tracking_data = base64_encode ("[{$fallback_tracking_block},{$fallback_obj->code_version},\"{$fallback_obj->get_ad_name ()}\",\"{$fallback_obj->version_name}\"]");
$fallback_tracking_data = base64_encode ("[{$fallback_tracking_block},{$fallback_obj->tracking_index},\"{$fallback_obj->get_ad_name ()}\",\"{$fallback_obj->version_name}\"]");
$ad_inserter_globals [$globals_name] --;
}
}
}
if ($this->client_side_cookie_check_url && $url_parameters != '') $url_parameter_attributes = " parameter-list='$url_parameters' parameter-list-type='$url_parameter_list_type'"; else $url_parameter_attributes = '';
if ($this->client_side_cookie_check && $cookies != '') $cookie_attributes = " cookie-list='$cookies' cookie-list-type='$cookie_list_type'"; else $cookie_attributes = '';
if ( $referers != '') $referer_attributes = " referer-list='$referers' referer-list-type='$referer_list_type'"; else $referer_attributes = '';
if ( $clients != '') $client_attributes = " client-list='$clients' client-list-type='$client_list_type'"; else $client_attributes = '';
if ($scheduling_type !== null ) $scheduling_attributes = " scheduling-start='$scheduling_start_time' scheduling-end='$scheduling_end_time' scheduling-days='$scheduling_days_in_week' scheduling-type='$scheduling_type_code' gmt='$gmt' scheduling-fallback='$fallback_block_data' fallback_level='$fallback_level' fallback-tracking='$fallback_tracking_data'"; else $scheduling_attributes = '';
// Deprecated
$this->client_side_list_detection = true;
// ??? If $ai_wp_data [AI_WP_AMP_PAGE] is set it will not get here
if ($ai_wp_data [AI_WP_AMP_PAGE]) $this->needs_class = true;
// Hide block only when no block height is defined - for check options don't hide the first one
if (trim ($this->get_block_height ()) == '' || isset ($this->check_codes) && $this->check_codes_index != 0) {
$this->wrapping_div_classes []= 'ai-list-block';
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_id = 'ai-debug-info-' . $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
$debug_id_data = " data-debug-info='$debug_id'";
} else $debug_id_data = '';
switch ($dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
if (($url_parameter_attributes != '' || $cookie_attributes != '') && $this->get_manual_loading () == AI_MANUAL_LOADING_AUTO) {
$list_class = ' ai-list-manual';
} else $list_class = '';
$processed_code = "\n
{$processed_code}
\n";
if ($scheduling_type !== null) {
$processed_code .= "
{$fallback_code}
\n";
}
break;
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
$code_data = " data-code='".base64_encode ($processed_code)."'";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$block_id = 'ai-list-' . $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
$list_class = ' ' . $block_id ;
} else $list_class = '';
if (($url_parameter_attributes != '' || $cookie_attributes != '') && $this->get_manual_loading () == AI_MANUAL_LOADING_AUTO) {
$list_class .= ' ai-list-manual';
}
$processed_code = "\n
\n";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME] && !get_disable_js_code ()) {
// $processed_code .= "\n";
$processed_code .= "\n";
}
break;
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
if ($scheduling_type !== null) {
$scheduling_attributes_dbg = sprintf (__ ("start='%s' end='%s' days='%s' type='%s'", 'ad-inserter'), $scheduling_start_time_raw, $scheduling_end_time_raw, $scheduling_days_in_week_raw, $scheduling_type_text);
$debug_scheduling = new ai_block_labels ('ai-debug-scheduling');
$debug_processed_code = $debug_scheduling->bar ($scheduling_attributes_dbg, 'OUT = ' . AI_TEXT_INSERT_OUTSIDE_DATES .', IN = ' . AI_TEXT_INSERT_BETWEEN_DATES, '
', '
');
$processed_code = $debug_processed_code . $processed_code;
}
if ($this->client_side_cookie_check_url && ($url_parameters != '')) $url_parameter_attributes_dbg = sprintf (__ ("parameters='%s' type='%s'", 'ad-inserter'), $url_parameters_raw, $url_parameter_list_type); else $url_parameter_attributes_dbg = '';
if ($this->client_side_cookie_check && ($cookies != '')) $cookie_attributes_dbg = sprintf (__ ("cookies='%s' type='%s'", 'ad-inserter'), $cookies_raw, $cookie_list_type); else $cookie_attributes_dbg = '';
// translators: %s: list parameters and type
if ( $referers != '') $referer_attributes_dbg = sprintf (__ ("referers='%s' type='%s'", 'ad-inserter'), $referers_raw, $referer_list_type); else $referer_attributes_dbg = '';
// translators: %s: list parameters and type
if ( $clients != '') $client_attributes_dbg = sprintf (__ ("clients='%s' type='%s'", 'ad-inserter'), $clients_raw, $client_list_type); else $client_attributes_dbg = '';
if ($url_parameter_attributes_dbg != '' || $cookie_attributes_dbg != '' || $referer_attributes_dbg != '' || $client_attributes_dbg) {
$debug_list = new ai_block_labels ('ai-debug-lists');
$debug_processed_code = $debug_list->bar ($url_parameter_attributes_dbg . ' ' . $cookie_attributes_dbg . ' ' . $referer_attributes_dbg . ' ' . $client_attributes_dbg, 'B = ' . AI_TEXT_BLACK_LIST .', W = ' . AI_TEXT_WHITE_LIST, '
', '
');
$processed_code = $debug_processed_code . $processed_code;
}
if ($url_parameter_attributes_dbg != '' && ($this->get_manual_loading () == AI_MANUAL_LOADING_AUTO || strpos ($url_parameters_raw, 'tcf-') !== false || strpos ($url_parameters_raw, 'euconsent-v2') !== false) ||
$cookie_attributes_dbg != '' && ($this->get_manual_loading () == AI_MANUAL_LOADING_AUTO || strpos ($cookies_raw, 'tcf-') !== false || strpos ($cookies_raw, 'euconsent-v2') !== false)) {
$debug_manual_loading = new ai_block_labels ('ai-debug-lists');
$version_name = $this->version_name == '' ? '' : ' - ' . $this->version_name;
$block_name = $this->number . ' ' . $this->get_ad_name () . '
' . $version_name . '';
$status = $this->get_manual_loading () == AI_MANUAL_LOADING_AUTO ? 'MANUAL LOADING' : 'NOT ENABLED';
// Debugging label before the hidden block in case manual loading is set to Auto and the block has not been enabled yet
$this->additional_code_before_block = $debug_manual_loading->bar ($block_name, '', 'NOT LOADED', $status, '', $debug_id);
}
}
break;
case AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC:
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS LISTS';
}
$this->generate_w3tc_code_from_html ($processed_code);
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' ai_w3tc_log_run (\'PROCESS LISTS\' . ($ai_enabled ? \'\' : \', NOT ENABLED\'));';
}
if ($referers != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' REFERRERS: \\\'\' . base64_decode (\'' . $referers . '\').\'\\\' ' . ($referer_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_referer_list (base64_decode (\''.$referers.'\'), '.($referer_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
if ($clients != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' CLIENTS: \\\'\' . base64_decode (\'' . $clients . '\').\'\\\' ' . ($client_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_client_list (base64_decode (\''.$clients.'\'), '.($client_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
if ($this->client_side_cookie_check_url) {
if ($url_parameters != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' URL PARAMETERS: \\\'\' . base64_decode (\'' . $url_parameters . '\').\'\\\' ' . ($url_parameter_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (" FAILED", "color: red;");';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_cookie_list (base64_decode (\''.$url_parameters.'\'), '.($url_parameter_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
}
if ($this->client_side_cookie_check) {
if ($cookies != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' COOKIES: \\\'\' . base64_decode (\'' . $cookies . '\').\'\\\' ' . ($cookie_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (" FAILED", "color: red;");';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_cookie_list (base64_decode (\''.$cookies.'\'), '.($cookie_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
}
if ($scheduling_type !== null) {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' SCHEDULING: \' . base64_decode (\'' . $scheduling_start_time . '\') . \', \' . base64_decode (\'' . $scheduling_end_time . '\'). \' [\' . base64_decode (\'' . $scheduling_days_in_week . '\') . \'] ' . ($scheduling_type == AI_SCHEDULING_BETWEEN_DATES ? 'IN':'OUT'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$fallback_block = intval ($this->get_scheduling_fallback());
if ($fallback_block != $this->number && $fallback_block >= 1 && $fallback_block <= 96) {
$fallback_obj = $block_object [$fallback_block];
$fallback_obj->hide_debug_labels = true;
$fallback_block_code = base64_encode ($fallback_obj->ai_getProcessedCode ()); // Encode HTML + W3TC PHP
$fallback_obj->hide_debug_labels = false;
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$w3tc_fallback_status = ' ai_w3tc_log_run (\' FALLBACK BLOCK: ' . $fallback_block . '\');';
array_unshift ($fallback_obj->w3tc_debug, 'FALLBACK BLOCK ' . $fallback_block);
$fallback_obj->w3tc_debug []= 'FALLBACK BLOCK END';
$this->w3tc_debug = array_merge ($this->w3tc_debug, $fallback_obj->w3tc_debug);
} else $w3tc_fallback_status = '';
if ($fallback_obj->get_tracking ()) {
$fallback_tracking_block = $fallback_block;
} else $fallback_tracking_block = '0';
$fallback_code = ' if (!$ai_enabled) {'.$w3tc_fallback_status.' $ai_enabled = true; if (!isset ($ai_index)) $ai_index = 0; $ai_fallback = '.$fallback_tracking_block.'; $ai_code = ai_w3tc_execute_php (base64_decode (\''. $fallback_block_code . '\'), $ai_index, $ai_fallback);}';
} else $fallback_code = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_scheduling_time (base64_decode (\''. $scheduling_start_time.'\'), base64_decode (\''.$scheduling_end_time.'\'), base64_decode (\''.$scheduling_days_in_week.'\'), '.($scheduling_type == AI_SCHEDULING_BETWEEN_DATES ? 'true':'false').');' . $w3tc_status . $fallback_code . '};';
}
$processed_code = $this->generate_html_from_w3tc_code ();
break;
}
}
} while ($check_again);
// Countries, IP addresses
do {
$check_again = false;
if (isset ($this->check_countries) || isset ($this->check_ip_addresses)) {
$countries = '';
$country_list_type = '';
$ip_addresses = '';
$ip_address_list_type = '';
if (isset ($this->check_countries)) {
$countries = trim (strtoupper ($this->check_countries));
$country_list_type = $this->check_countries_list_type;
}
if (isset ($this->check_ip_addresses)) {
$ip_addresses = trim (str_replace (' ', '', strtolower ($this->check_ip_addresses)));
$ip_address_list_type = $this->check_ip_addresses_list_type;
}
unset ($this->check_countries);
unset ($this->check_countries_list_type);
unset ($this->check_ip_addresses);
unset ($this->check_ip_addresses_list_type);
$check_again = true;
} else {
$countries = trim (strtoupper ($this->get_ad_country_list (true)));
$country_list_type = $this->get_ad_country_list_type ();
$ip_addresses = trim (str_replace (' ', '', strtolower ($this->get_ad_ip_address_list ())));
$ip_address_list_type = $this->get_ad_ip_address_list_type ();
}
// $countries = trim (strtoupper (isset ($this->check_countries) ? $this->check_countries : $this->get_ad_country_list (true)));
// $country_list_type = isset ($this->check_countries_list_type) ? $this->check_countries_list_type : $this->get_ad_country_list_type ();
// $ip_addresses = trim (str_replace (' ', '', strtolower (isset ($this->check_ip_addresses) ? $this->check_ip_addresses : $this->get_ad_ip_address_list ())));
// $ip_address_list_type = isset ($this->check_ip_addresses_list_type) ? $this->check_ip_addresses_list_type : $this->get_ad_ip_address_list_type ();
if ($countries != '' || $ip_addresses != '') {
switch ($dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
if ($country_list_type == AI_BLACK_LIST) $country_list_type = 'B'; else $country_list_type = 'W';
if ($ip_address_list_type == AI_BLACK_LIST) $ip_address_list_type = 'B'; else $ip_address_list_type = 'W';
if ($countries != '') $country_attributes = "countries='$countries' country-list='$country_list_type'"; else $country_attributes = '';
if ($ip_addresses != '') $ip_address_attributes = "ip-addresses='$ip_addresses' ip-address-list='$ip_address_list_type'"; else $ip_address_attributes = '';
// Deprecated
$this->client_side_list_detection = true;
// ??? If $ai_wp_data [AI_WP_AMP_PAGE] is set it will not get here
if ($ai_wp_data [AI_WP_AMP_PAGE]) $this->needs_class = true;
// Hide block only when no block height is defined - for check options don't hide the first one
if (trim ($this->get_block_height ()) == '' || isset ($this->check_codes) && $this->check_codes_index != 0) {
$this->wrapping_div_classes []= 'ai-list-block-ip';
}
switch ($dynamic_blocks) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
$processed_code = "\n
$processed_code
\n";
break;
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
$code_data = "data-code='".base64_encode ($processed_code)."'";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$block_id = 'ai-ip-' . $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
$ip_class = ' ' . $block_id ;
} else $ip_class = '';
$processed_code = "\n
\n";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME] && function_exists ('add_footer_inline_scripts_2') && !get_disable_js_code ()) {
// $processed_code .= "\n";
$processed_code .= "\n";
}
break;
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
// translators: %s: list parameters and type
if ($countries != '' ) $country_attributes_dbg = sprintf (__("countries='%s' type='%s'", 'ad-inserter'), $countries, $country_list_type); else $country_attributes_dbg = '';
// translators: %s: list parameters and type
if ($ip_addresses != '' ) $ip_address_attributes_dbg = sprintf (__("ip addresses='%s' type='%s'", 'ad-inserter'), $ip_addresses, $ip_address_list_type); else $ip_address_attributes_dbg = '';
$debug_ip = new ai_block_labels ('ai-debug-lists');
$processed_code = $debug_ip->bar ($country_attributes_dbg . ' ' . $ip_address_attributes_dbg, 'B = ' . AI_TEXT_BLACK_LIST .', W = ' . AI_TEXT_WHITE_LIST, '
', '
') . $processed_code;
}
break;
case AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC:
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS IP ADDRESSES';
}
$this->generate_w3tc_code_from_html ($processed_code);
$this->w3tc_code .= ' require_once \''.AD_INSERTER_PLUGIN_DIR.'includes/geo/Ip2Country.php\';';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\'PROCESS IP ADDRESSES\' . ($ai_enabled ? \'\' : \', NOT ENABLED\'));';
}
if ($ip_addresses != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' IP ADDRESSES: \\\'\' . base64_decode (\'' . base64_encode ($ip_addresses) . '\').\'\\\' ' . ($ip_address_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_ip_address_list (base64_decode (\''.base64_encode ($ip_addresses).'\'), '.($ip_address_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
if ($countries != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_enabled) ai_w3tc_log_run (\' COUNTRIES: \\\'\' . base64_decode (\'' . base64_encode ($countries) . '\').\'\\\' ' . ($country_list_type == AI_WHITE_LIST ? 'W':'B'). '\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$this->w3tc_code .= ' if ($ai_enabled) {$ai_enabled = check_country_list (base64_decode (\''.base64_encode ($countries).'\'), '.($country_list_type == AI_WHITE_LIST ? 'true':'false').');'.$w3tc_status.'};';
}
$processed_code = $this->generate_html_from_w3tc_code ();
break;
}
}
} while ($check_again);
}
// Viewports
if (!$ai_wp_data [AI_FORCE_SERVERSIDE_CODE] && !$ai_wp_data [AI_WP_AMP_PAGE]) {
$viewports_code = false;
do {
$check_again = false;
if (isset ($this->check_viewports)) {
$viewports = $this->check_viewports;
$viewport_list_type = $this->check_viewports_list_type;
unset ($this->check_viewports);
unset ($this->check_viewports_list_type);
// Not needed as only check possible is from the CHECK separator
// $check_again = true;
} else {
$viewports = '';
$viewport_list_type = '';
}
if ($viewports != '') {
$separator_viewports = explode (',', strtolower ($viewports));
foreach ($separator_viewports as $index => $separator_viewport) {
$separator_viewports [$index] = trim ($separator_viewport);
}
$viewport_classes = '';
$invisible_viewport_classes = '';
for ($viewport = 1; $viewport <= 6; $viewport ++) {
$viewport_name = strtolower (get_viewport_name ($viewport));
$viewport_width = get_viewport_width ($viewport);
if ($viewport_name != '') {
$viewport_found = in_array ($viewport_name, $separator_viewports);
if ($viewport_list_type == AI_BLACK_LIST) {
$viewport_found = !$viewport_found;
}
if ($viewport_found) {
$viewport_classes .= " ai-viewport-" . $viewport;
} else {
$invisible_viewport_classes .= " ai-viewport-" . $viewport;
}
}
}
if ($ai_wp_data [AI_CODE_FOR_IFRAME]) {
$viewport_classes = '';
$invisible_viewport_classes = 'ai-viewport-0';
}
elseif ($viewport_classes == '') {
$viewport_classes = 'ai-viewport-0';
$invisible_viewport_classes = '';
}
elseif ($invisible_viewport_classes == '') {
$viewport_classes = '';
$invisible_viewport_classes = 'ai-viewport-0';
}
$viewport_classes = trim ($viewport_classes);
$invisible_viewport_classes = trim ($invisible_viewport_classes);
if ($viewport_classes != '') {
$viewport_class = " class='" . $viewport_classes . "'";
} else $viewport_class = '';
$invisible_label = '';
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
if ($viewport_list_type == AI_BLACK_LIST) $list_type = 'B'; else $list_type = 'W';
$invisible_label_classes = $dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT ? '' : $invisible_viewport_classes;
$invisible_debug_viewport = new ai_block_labels ('ai-debug-viewport-invisible '. $invisible_label_classes);
$invisible_label = $invisible_debug_viewport->bar (sprintf (__("viewport='%s' type='%s'", 'ad-inserter'), $viewports, $list_type), '', _x('HIDDEN', 'Block', 'ad-inserter'), ' ');
$debug_viewport = new ai_block_labels ('ai-debug-lists');
$processed_code = $debug_viewport->bar (sprintf (__("viewport='%s' type='%s'", 'ad-inserter'), $viewports, $list_type), '', _x('VISIBLE', 'Block', 'ad-inserter'), ' ') . $processed_code;
}
$viewports_code = true;
// All viewports selected
if ($viewport_classes == '' && $invisible_viewport_classes == 'ai-viewport-0') {
// $processed_code already contains code for insertion
} else
// No viewport selected
if ($viewport_classes == 'ai-viewport-0' && $invisible_viewport_classes == '') {
$processed_code = $invisible_label;
} else
if ($dynamic_blocks == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT) {
$class_id = 'ai-insert-' . $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
if ($viewport_classes != '') {
$viewport_classes = 'ai-viewports ' . $viewport_classes;
}
$ai_code = $this->base64_encode_w3tc (ai_strip_w3tc_markers ($processed_code), false); // Use W3TC code in case W3TC was used before and insert was specified for CHECK
$processed_code = "
\n";
if (!get_disable_js_code ()) {
$js_code = "ai_insert_list_code ('{$class_id}');";
$processed_code .= $this->ai_js_dom_ready ($js_code);
if ($invisible_label != '') {
if ($invisible_viewport_classes != '') {
$invisible_viewport_classes = 'ai-viewports ' . $invisible_viewport_classes . ' ';
}
$ai_dbg_code = base64_encode ($invisible_label);
$processed_code .= "
\n";
$js_code = "ai_insert_code_by_class ('{$class_id}-dbg');";
$processed_code .= $this->ai_js_dom_ready ($js_code);
}
}
} else {
if ($this->get_alignment_type () == AI_ALIGNMENT_NO_WRAPPING || $invisible_label != '') {
if ($viewport_class != '') {
$processed_code = $invisible_label . "
\n" . $processed_code . "\n
\n";
} else $processed_code = $invisible_label . $processed_code;
} else $this->wrapping_div_classes = array_merge ($this->wrapping_div_classes, explode (' ', $viewport_classes));
}
}
} while ($check_again);
// Recreate W3TC code
if ($viewports_code && $this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS VIEWPORT CHECKS';
}
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
// LIMITS + COOKIE
if ($check_block_code && !$ai_wp_data [AI_FORCE_SERVERSIDE_CODE] && !$ai_wp_data [AI_WP_AMP_PAGE]) {
// Check for cookie
if (get_dynamic_blocks () == AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC && !defined ('AI_NO_W3TC')) {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS LIMITS AND COOKIE';
$this->generate_w3tc_code_from_html ($processed_code);
$this->w3tc_code .= 'if ($ai_enabled) ai_w3tc_log_run (\'PROCESS LIMITS AND COOKIE\');';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$fallback_block = intval ($this->get_limits_fallback ());
if ($fallback_block != $this->number && $fallback_block >= 1 && $fallback_block <= 96) {
$fallback_obj = $block_object [$fallback_block];
$fallback_obj->hide_debug_labels = true;
$fallback_block_code = base64_encode ($fallback_obj->ai_getProcessedCode ()); // Encode HTML + W3TC PHP
$fallback_obj->hide_debug_labels = false;
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$w3tc_fallback_status = ' ai_w3tc_log_run (\' FALLBACK BLOCK: ' . $fallback_block . '\');';
array_unshift ($fallback_obj->w3tc_debug, 'FALLBACK BLOCK ' . $fallback_block);
$fallback_obj->w3tc_debug []= 'FALLBACK BLOCK END';
$this->w3tc_debug = array_merge ($this->w3tc_debug, $fallback_obj->w3tc_debug);
} else $w3tc_fallback_status = '';
if ($fallback_obj->get_tracking ()) {
$fallback_tracking_block = $fallback_block;
} else $fallback_tracking_block = '0';
$fallback_code = ' if (!$ai_enabled) {'.$w3tc_fallback_status.' $ai_enabled = true; if (!isset ($ai_index)) $ai_index = 0; $ai_fallback = '.$fallback_tracking_block.'; $ai_code = ai_w3tc_execute_php (base64_decode (\''. $fallback_block_code . '\'), $ai_index, $ai_fallback);}';
} else $fallback_code = '';
$this->generate_w3tc_code_from_html ($processed_code);
$this->w3tc_code .= $ai_check_block_w3tc_code;
$this->w3tc_code .= 'if ($ai_enabled) {$ai_enabled = ai_check_impression_and_click_limits (' . $this->number . ', false); if ($ai_enabled) {$ai_enabled = ai_check_block (' . $this->number . ');}; '. $w3tc_status . $fallback_code . '};';
$processed_code = $this->generate_html_from_w3tc_code ();
} else {
$debug_html_code = '';
$block_id = $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
$fallback_data = '';
$fallback_block = $this->get_limits_fallback ();
if ($fallback_block != $this->number && $fallback_block >= 1 && $fallback_block <= 96) {
$globals_name = AI_FALLBACK_DEPTH_NAME;
if (!isset ($ad_inserter_globals [$globals_name])) {
$ad_inserter_globals [$globals_name] = 0;
}
if ($ad_inserter_globals [$globals_name] < 3) {
$ad_inserter_globals [$globals_name] ++;
$fallback_level = $ad_inserter_globals [$globals_name];
$fallback_obj = $block_object [$fallback_block];
$fallback_code = $fallback_obj->ai_getProcessedCode ();
$fallback_tracking_block = $fallback_obj->get_tracking () ? $fallback_obj->number : 0;
// $fallback_tracking_data = base64_encode ("[{$fallback_tracking_block},{$fallback_obj->code_version},\"{$fallback_obj->get_ad_name ()}\",\"{$fallback_obj->version_name}\"]");
$fallback_tracking_data = base64_encode ("[{$fallback_tracking_block},{$fallback_obj->tracking_index},\"{$fallback_obj->get_ad_name ()}\",\"{$fallback_obj->version_name}\"]");
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_fallback = new ai_block_labels ('ai-debug-fallback');
$debug_fallback_code = $debug_fallback->bar ('', '', _x ('FALLBACK', 'alternative block', 'ad-inserter') . " = {$fallback_block}", '');
} else $debug_fallback_code = '';
$fallback_data = " data-fallback-code='" . base64_encode ($debug_fallback_code . $fallback_code) . "' limits-fallback='$fallback_block' fallback_level='$fallback_level' fallback-tracking='$fallback_tracking_data'";
$ad_inserter_globals [$globals_name] --;
}
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_label = new ai_block_labels ('ai-debug-cookie');
$debug_html_code =
$debug_label->block_start () .
$debug_label->bar ('COOKIE CHECK', '', '
', '
', '', 'ai-check-' . $block_id . '-dbg') .
$debug_label->block_end ();
}
$processed_code =
$debug_html_code .
"
{$ai_check_block_html_code}
\n";
if (!get_disable_js_code ()) {
$js_code = "{$ai_check_block_js_code}ai_check_and_insert_block ({$this->number}, 'ai-check-{$block_id}');";
$processed_code .= $this->ai_js_dom_ready ($js_code);
}
}
}
// FILTER HOOK
if ($this->client_side_filter_hook_check && !$ai_wp_data [AI_FORCE_SERVERSIDE_CODE] && !$ai_wp_data [AI_WP_AMP_PAGE]) {
// Check filter hook
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_label = new ai_block_labels ('ai-debug-filter');
$debug_html_code = $debug_label->bar ('FILTER HOOK CHECK', '', '
', '
ai_block_insertion_check');
} else $debug_html_code = '';
$dynamic_blocks_filetr_check = $dynamic_blocks;
if ($dynamic_blocks_filetr_check == AI_DYNAMIC_BLOCKS_SERVER_SIDE && $this->client_side_filter_hook_check) {
// if filter ai_block_insertion_check returns null on server-side check, it means it needs client-side check
$dynamic_blocks_filetr_check = AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT;
}
switch ($dynamic_blocks_filetr_check) {
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW:
// Hide block only when no block height is defined - for check options don't hide the first one
if (trim ($this->get_block_height ()) == '' || isset ($this->check_codes) && $this->check_codes_index != 0) {
$this->wrapping_div_classes []= 'ai-list-block-filter';
}
$processed_code = $debug_html_code . "\n
$processed_code
\n";
break;
case AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT:
// Hide block only when no block height is defined - for check options don't hide the first one
if (trim ($this->get_block_height ()) == '' || isset ($this->check_codes) && $this->check_codes_index != 0) {
$this->wrapping_div_classes []= 'ai-list-block-filter';
}
$code_data = "data-code='".base64_encode ($processed_code)."'";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME]) {
$block_id = 'ai-filter-' . $this->number . '-' . rand (1000, 9999) . rand (1000, 9999);
$filter_class = ' ' . $block_id ;
} else $filter_class = '';
$processed_code = $debug_html_code . "\n
\n";
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) && !$ai_wp_data [AI_CODE_FOR_IFRAME] && !get_disable_js_code ()) {
// $processed_code .= "\n";
$processed_code .= "\n";
}
break;
case AI_DYNAMIC_BLOCKS_SERVER_SIDE_W3TC:
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS FILTER HOOK';
$this->generate_w3tc_code_from_html ($processed_code);
$this->w3tc_code .= 'if ($ai_enabled) ai_w3tc_log_run (\'PROCESS FILTER HOOK: \' . (ai_check_filter_hook (' . $this->number . ') ? \'ENABLED\' : \'NOT ENABLED\'));';
$w3tc_status = ' if (!$ai_enabled) ai_w3tc_log_run (\' FAILED\', \'color: red;\');';
} else $w3tc_status = '';
$this->generate_w3tc_code_from_html ($processed_code);
$this->w3tc_code .= $ai_check_block_w3tc_code;
$this->w3tc_code .= 'if ($ai_enabled) {$ai_enabled = ai_check_filter_hook (' . $this->number . ');'.$w3tc_status.'};';
$processed_code = $this->generate_html_from_w3tc_code ();
break;
}
}
}
if (!$ai_wp_data [AI_CODE_FOR_IFRAME] && !$ai_wp_data [AI_FORCE_SERVERSIDE_CODE]) {
// DELAY
if ((int) $this->get_delay_time () != 0 && !$ai_wp_data [AI_WP_AMP_PAGE]) {
if ($this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS DELAY';
}
}
$manual_code = $this->base64_encode_w3tc ($processed_code);
$wrapper_class = base64_encode (get_block_class_name (true));
$processed_code = '
'."\n";
// Recreate W3TC code
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
// WAIT FOR INTERACTION
if ($this->get_wait_for_interaction () && !$ai_wp_data [AI_WP_AMP_PAGE]) {
if ($this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS WAIT FOR INTERACTION';
}
}
$manual_code = $this->base64_encode_w3tc ($processed_code);
$wrapper_class = base64_encode (get_block_class_name (true));
$processed_code = '
'."\n";
// Recreate W3TC code
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
// CHECK RECAPTCHA SCORE
if (function_exists ('get_recaptcha_site_key')) {
if ($this->get_check_recaptcha_score () && !$ai_wp_data [AI_WP_AMP_PAGE] && trim (get_recaptcha_site_key ()) != '' && trim (get_recaptcha_secret_key ()) != '' && is_numeric (get_recaptcha_threshold ())) {
if ($this->w3tc_code != '') {
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_debug []= 'PROCESS CHECK FOR RECAPTCHA SCORE';
}
}
$manual_code = $this->base64_encode_w3tc ($processed_code);
$wrapper_class = base64_encode (get_block_class_name (true));
$processed_code = '
'."\n";
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$debug_label = new ai_block_labels ('ai-debug-script');
$processed_code = $debug_label->bar ('reCAPTCHA score threshold = '.get_recaptcha_threshold (), '', '
', 'reCAPTCHA score:
') . $processed_code;
}
// Recreate W3TC code
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
}
}
}
}
// TODO single CHECK block
// } while (is_array ($this->check_codes) && isset ($this->check_codes [$this->check_codes_index + 1]));
if (defined ('AI_DEBUGGING_DEMO') && !$this->demo_debugging) {
$this->hide_debug_labels = true;
}
if ($close_button && ($width = trim ($this->get_block_width ())) != '' && $this->get_alignment_type() != AI_ALIGNMENT_NO_WRAPPING) {
if (is_numeric ($width)) {
$width .= 'px';
}
$style = 'width: ' . $width . ';';
$processed_code = "
\n" . $processed_code ."\n
\n";
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$processed_code = "
\n" . $processed_code ."\n
\n";
}
// Process W3TC filter hook
if ($this->w3tc_code != '') {
$processed_code = $this->regenerate_w3tc_code ($processed_code);
$this->w3tc_code .= ' $ai_code_org = $ai_code; if (!isset ($ai_enabled) || $ai_enabled) {$ai_code = apply_filters ("ai_block_w3tc_code_after_processing", $ai_code_org, ' . $this->number . ');}';
if ($ai_wp_data [AI_W3TC_DEBUGGING]) {
$this->w3tc_code .= ' if ($ai_code != $ai_code_org) {ai_w3tc_log_run ("PROCESS HOOK FILTER ai_block_w3tc_code_after_processing");}';
}
$processed_code = $this->generate_html_from_w3tc_code ();
}
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_BLOCKS) != 0 && !$this->hide_debug_labels) {
$this->ai_generateDebugLabel ();
}
$code = $this->additional_code_before . $processed_code . $this->additional_code_after;
// $this->additional_code_after may contain W3TC code because of ai_adb_block_actions ()
if ($this->w3tc_code != '' || strpos ($this->additional_code_after, '';
} else {
$serverside_insertion_code = str_replace ('[#AI_CODE#]', $block_code, $serverside_insertion_code);
}
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
$serverside_insertion_code_org = $serverside_insertion_code;
$hook_start_time = microtime (true);
$serverside_insertion_code = apply_filters ("ai_block_code_serverside_html_js", $serverside_insertion_code_org, $this->number);
if ($debug_processing && $serverside_insertion_code != $serverside_insertion_code_org) {
$filter_hooks []= array ("ai_block_code_serverside_html_js", $this->number);
}
$ai_total_hook_php_time += microtime (true) - $hook_start_time;
return $serverside_insertion_code;
}
public function get_code_for_serverside_insertion ($include_viewport_classes = true, $hidden_widgets = false, $code_only = false) {
global $ai_wp_data, $ai_total_hook_php_time, $filter_hooks;
if (!isset ($ai_wp_data [AI_NESTING_LEVEL])) $ai_wp_data [AI_NESTING_LEVEL] = 0; else $ai_wp_data [AI_NESTING_LEVEL] ++;
$code = $this->get_html_js_code_for_serverside_insertion ($include_viewport_classes, $hidden_widgets, $code_only);
if (($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_AJAX || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_REST) || $ai_wp_data [AI_NESTING_LEVEL] != 0) {
// For Ajax pages and nested blocks do not extract JS code
$code = ai_strip_js_markers ($code);
} else {
$code = ai_extract_js_code ($code);
}
$ai_wp_data [AI_NESTING_LEVEL] --;
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
$code_org = $code;
$hook_start_time = microtime (true);
$code = apply_filters ("ai_block_code_serverside_insertion", $code_org, $this->number);
if ($debug_processing && $code != $code_org) {
$filter_hooks []= array ("ai_block_code_serverside_insertion", $this->number);
}
$ai_total_hook_php_time += microtime (true) - $hook_start_time;
return $code;
}
public function get_iframe_page () {
global $ai_wp_data, $wp_version, $wp_scripts;
$adb_code = defined ('AI_ADBLOCKING_DETECTION') && AI_ADBLOCKING_DETECTION && $ai_wp_data [AI_ADB_DETECTION] && !isset ($ai_wp_data [AI_ADB_SHORTCODE_DISABLED]);
if (isset ($wp_scripts->registered ['jquery']->ver)) {
$jquery_version = $wp_jquery_ver = $wp_scripts->registered ['jquery']->ver;
} else $jquery_version = $wp_version;
if (isset ($wp_scripts->registered ['jquery-migrate']->ver)) {
$jquery_migrate_version = $wp_jquery_ver = $wp_scripts->registered ['jquery-migrate']->ver;
} else $jquery_migrate_version = $wp_version;
$ai_wp_data [AI_CODE_FOR_IFRAME] = true;
ob_start ();
echo '
';
if (!defined ('AI_NO_JQUERY')) {
echo "\n";
echo "\n";
}
ai_wp_head_hook ();
echo '
Ad Inserter Pro - Advanced WordPress Ads Management Plugin
';
$ai_wp_data [AI_NESTING_LEVEL] = 0;
// $include_viewport_classes = true, $hidden_widgets = false, $code_only = false
echo ai_extract_js_code ($this->get_code_for_insertion (false, false, false));
echo "\n";
ai_set_footer_inline_scripts ();
ai_wp_footer_hook_end_buffering ();
ai_wp_footer_hook ();
echo '
';
$page = ob_get_clean ();
$ai_wp_data [AI_CODE_FOR_IFRAME] = false;
$page = ai_process_head_codes ($page);
return $page;
}
public function get_close_button (){
$option = isset ($this->wp_options [AI_OPTION_CLOSE_BUTTON]) ? $this->wp_options [AI_OPTION_CLOSE_BUTTON] : DEFAULT_CLOSE_BUTTON;
return $option;
}
public function get_auto_close_time () {
$option = isset ($this->wp_options [AI_OPTION_AUTO_CLOSE_TIME]) ? $this->wp_options [AI_OPTION_AUTO_CLOSE_TIME] : DEFAULT_AUTO_CLOSE_TIME;
if ($option == '0') $option = '';
return $option;
}
public function get_stay_closed_time () {
$option = isset ($this->wp_options [AI_OPTION_STAY_CLOSED_TIME]) ? $this->wp_options [AI_OPTION_STAY_CLOSED_TIME] : DEFAULT_STAY_CLOSED_TIME;
if ($option == '0') $option = '';
return $option;
}
public function get_delay_time () {
$option = isset ($this->wp_options [AI_OPTION_DELAY_TIME]) ? $this->wp_options [AI_OPTION_DELAY_TIME] : DEFAULT_DELAY_TIME;
if ($option == '0') $option = '';
return $option;
}
public function get_delay_showing () {
$option = isset ($this->wp_options [AI_OPTION_DELAY_SHOWING]) ? $this->wp_options [AI_OPTION_DELAY_SHOWING] : DEFAULT_DELAY_SHOWING;
if ($option == '0') $option = '';
return $option;
}
public function get_show_every () {
$option = isset ($this->wp_options [AI_OPTION_SHOW_EVERY]) ? $this->wp_options [AI_OPTION_SHOW_EVERY] : DEFAULT_SHOW_EVERY;
if ($option == '0') $option = '';
return $option;
}
public function get_background (){
$option = isset ($this->wp_options [AI_OPTION_BACKGROUND]) ? $this->wp_options [AI_OPTION_BACKGROUND] : DEFAULT_BACKGROUND;
return $option;
}
public function get_background_image (){
$option = isset ($this->wp_options [AI_OPTION_BACKGROUND_IMAGE]) ? $this->wp_options [AI_OPTION_BACKGROUND_IMAGE] : AD_EMPTY_DATA;
return $option;
}
public function get_background_color (){
$option = isset ($this->wp_options [AI_OPTION_BACKGROUND_COLOR]) ? $this->wp_options [AI_OPTION_BACKGROUND_COLOR] : AD_EMPTY_DATA;
return $option;
}
public function get_background_repeat (){
$option = isset ($this->wp_options [AI_OPTION_BACKGROUND_REPEAT]) ? $this->wp_options [AI_OPTION_BACKGROUND_REPEAT] : DEFAULT_BACKGROUND_REPEAT;
return $option;
}
public function get_background_size (){
$option = isset ($this->wp_options [AI_OPTION_BACKGROUND_SIZE]) ? $this->wp_options [AI_OPTION_BACKGROUND_SIZE] : DEFAULT_BACKGROUND_SIZE;
return $option;
}
public function get_set_body_background (){
if (!get_output_buffering ()) {
return false;
}
$option = isset ($this->wp_options [AI_OPTION_SET_BODY_BACKGROUND]) ? $this->wp_options [AI_OPTION_SET_BODY_BACKGROUND] : DEFAULT_SET_BODY_BACKGROUND;
return $option;
}
public function get_parallax_mode () {
$option_name = AI_OPTION_PARALLAX_MODE;
$option = isset ($this->wp_options [$option_name]) ? $this->wp_options [$option_name] : DEFAULT_PARALLAX_MODE;
return $option;
}
public function get_parallax ($index) {
$option_name = AI_OPTION_PARALLAX . '_' . $index;
$option = isset ($this->wp_options [$option_name]) ? $this->wp_options [$option_name] : AI_DISABLED;
return $option;
}
public function get_parallax_image ($index) {
$option_name = AI_OPTION_PARALLAX_IMAGE . '_' . $index;
$option = isset ($this->wp_options [$option_name]) ? $this->wp_options [$option_name] : AD_EMPTY_DATA;
return $option;
}
public function get_parallax_shift ($index) {
$option_name = AI_OPTION_PARALLAX_SHIFT . '_' . $index;
$option = isset ($this->wp_options [$option_name]) ? $this->wp_options [$option_name] : AD_EMPTY_DATA;
if ($option == '0') $option = '';
return $option;
}
public function get_parallax_link () {
$option = isset ($this->wp_options [AI_OPTION_PARALLAX_LINK]) ? $this->wp_options [AI_OPTION_PARALLAX_LINK] : AD_EMPTY_DATA;
return $option;
}
public function get_parallax_link_new_tab () {
$option = isset ($this->wp_options [AI_OPTION_PARALLAX_LINK_NEW_TAB]) ? $this->wp_options [AI_OPTION_PARALLAX_LINK_NEW_TAB] : AD_EMPTY_DATA;
return $option;
}
public function get_visitor_max_impressions () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_MAX_IMPRESSIONS]) ? $this->wp_options [AI_OPTION_VISITOR_MAX_IMPRESSIONS] : DEFAULT_VISITOR_MAX_IMPRESSIONS;
if ($option == '0') $option = '';
return $option;
}
public function get_visitor_limit_impressions_per_time_period () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_LIMIT_IMPRESSIONS_PER_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_VISITOR_LIMIT_IMPRESSIONS_PER_TIME_PERIOD] : DEFAULT_VISITOR_LIMIT_IMPRESSIONS_PER_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_visitor_limit_impressions_time_period () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_LIMIT_IMPRESSIONS_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_VISITOR_LIMIT_IMPRESSIONS_TIME_PERIOD] : DEFAULT_VISITOR_LIMIT_IMPRESSIONS_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_max_impressions () {
$option = isset ($this->wp_options [AI_OPTION_MAX_IMPRESSIONS]) ? $this->wp_options [AI_OPTION_MAX_IMPRESSIONS] : DEFAULT_MAX_IMPRESSIONS;
if ($option == '0') $option = '';
return $option;
}
public function get_limit_impressions_per_time_period () {
$option = isset ($this->wp_options [AI_OPTION_LIMIT_IMPRESSIONS_PER_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_LIMIT_IMPRESSIONS_PER_TIME_PERIOD] : DEFAULT_LIMIT_IMPRESSIONS_PER_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_limit_impressions_time_period () {
$option = isset ($this->wp_options [AI_OPTION_LIMIT_IMPRESSIONS_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_LIMIT_IMPRESSIONS_TIME_PERIOD] : DEFAULT_LIMIT_IMPRESSIONS_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_visitor_max_clicks () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_MAX_CLICKS]) ? $this->wp_options [AI_OPTION_VISITOR_MAX_CLICKS] : DEFAULT_VISITOR_MAX_CLICKS;
if ($option == '0') $option = '';
return $option;
}
public function get_visitor_limit_clicks_per_time_period () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_LIMIT_CLICKS_PER_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_VISITOR_LIMIT_CLICKS_PER_TIME_PERIOD] : DEFAULT_VISITOR_LIMIT_CLICKS_PER_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_visitor_limit_clicks_time_period () {
$option = isset ($this->wp_options [AI_OPTION_VISITOR_LIMIT_CLICKS_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_VISITOR_LIMIT_CLICKS_TIME_PERIOD] : DEFAULT_VISITOR_LIMIT_CLICKS_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_max_clicks () {
$option = isset ($this->wp_options [AI_OPTION_MAX_CLICKS]) ? $this->wp_options [AI_OPTION_MAX_CLICKS] : DEFAULT_MAX_CLICKS;
if ($option == '0') $option = '';
return $option;
}
public function get_limit_clicks_per_time_period () {
$option = isset ($this->wp_options [AI_OPTION_LIMIT_CLICKS_PER_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_LIMIT_CLICKS_PER_TIME_PERIOD] : DEFAULT_LIMIT_CLICKS_PER_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_limit_clicks_time_period () {
$option = isset ($this->wp_options [AI_OPTION_LIMIT_CLICKS_TIME_PERIOD]) ? $this->wp_options [AI_OPTION_LIMIT_CLICKS_TIME_PERIOD] : DEFAULT_LIMIT_CLICKS_TIME_PERIOD;
if ($option == '0') $option = '';
return $option;
}
public function get_trigger_click_fraud_protection () {
$option = isset ($this->wp_options [AI_OPTION_TRIGGER_CLICK_FRAUD_PROTECTION]) ? $this->wp_options [AI_OPTION_TRIGGER_CLICK_FRAUD_PROTECTION] : DEFAULT_TRIGGER_CLICK_FRAUD_PROTECTION;
if ($option == '0') $option = '';
return $option;
}
public function get_limits_fallback () {
$option = isset ($this->wp_options [AI_OPTION_LIMITS_FALLBACK]) ? $this->wp_options [AI_OPTION_LIMITS_FALLBACK] : "";
return $option;
}
public function get_horizontal_margin (){
$option = isset ($this->wp_options [AI_OPTION_HORIZONTAL_MARGIN]) ? $this->wp_options [AI_OPTION_HORIZONTAL_MARGIN] : DEFAULT_HORIZONTAL_MARGIN;
return $option;
}
public function get_vertical_margin () {
$option = isset ($this->wp_options [AI_OPTION_VERTICAL_MARGIN]) ? $this->wp_options [AI_OPTION_VERTICAL_MARGIN] : DEFAULT_VERTICAL_MARGIN;
return $option;
}
public function get_animation () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION]) ? $this->wp_options [AI_OPTION_ANIMATION] : DEFAULT_ANIMATION;
return $option;
}
public function get_animation_trigger () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_TRIGGER]) ? $this->wp_options [AI_OPTION_ANIMATION_TRIGGER] : DEFAULT_ANIMATION_TRIGGER;
return $option;
}
public function get_animation_trigger_value () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_TRIGGER_VALUE]) ? $this->wp_options [AI_OPTION_ANIMATION_TRIGGER_VALUE] : DEFAULT_ANIMATION_TRIGGER_VALUE;
return $option;
}
public function get_animation_trigger_offset () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_TRIGGER_OFFSET]) ? $this->wp_options [AI_OPTION_ANIMATION_TRIGGER_OFFSET] : DEFAULT_ANIMATION_TRIGGER_OFFSET;
return $option;
}
public function get_animation_trigger_delay () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_TRIGGER_DELAY]) ? $this->wp_options [AI_OPTION_ANIMATION_TRIGGER_DELAY] : DEFAULT_ANIMATION_TRIGGER_DELAY;
return $option;
}
// Deprecated
public function get_animation_trigger_once () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_TRIGGER_ONCE]) ? $this->wp_options [AI_OPTION_ANIMATION_TRIGGER_ONCE] : DEFAULT_ANIMATION_TRIGGER_ONCE;
return $option;
}
public function get_animation_out_trigger () {
if ($this->get_animation_trigger_once ()) {
return AI_TRIGGER_DISABLED;
}
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER]) ? $this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER] : AI_TRIGGER_ENABLED;
return $option;
}
public function get_animation_out_trigger_value () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER_VALUE]) ? $this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER_VALUE] : DEFAULT_ANIMATION_TRIGGER_VALUE;
return $option;
}
public function get_animation_out_trigger_offset () {
$option = isset ($this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER_OFFSET]) ? $this->wp_options [AI_OPTION_ANIMATION_OUT_TRIGGER_OFFSET] : DEFAULT_ANIMATION_TRIGGER_OFFSET;
return $option;
}
public function get_ad_general_tag(){
$option = isset ($this->wp_options [AI_OPTION_GENERAL_TAG]) ? $this->wp_options [AI_OPTION_GENERAL_TAG] : DEFAULT_GENERAL_TAG;
// if ($option == '') $option = DEFAULT_GENERAL_TAG;
return $option;
}
public function get_adb_block_action (){
$option = isset ($this->wp_options [AI_OPTION_ADB_BLOCK_ACTION]) ? $this->wp_options [AI_OPTION_ADB_BLOCK_ACTION] : DEFAULT_ADB_BLOCK_ACTION;
return $option;
}
public function get_adb_block_replacement (){
$option = isset ($this->wp_options [AI_OPTION_ADB_BLOCK_REPLACEMENT]) ? $this->wp_options [AI_OPTION_ADB_BLOCK_REPLACEMENT] : AD_EMPTY_DATA;
return $option;
}
public function get_scheduling(){
$option = isset ($this->wp_options [AI_OPTION_SCHEDULING]) ? $this->wp_options [AI_OPTION_SCHEDULING] : "";
// Convert old option
if ($option == '' && intval ($this->get_ad_after_day()) != 0) $option = AI_SCHEDULING_DELAY_FOR;
if ($option == '') $option = AI_SCHEDULING_OFF;
return $option;
}
public function get_ad_after_day(){
$option = isset ($this->wp_options [AI_OPTION_AFTER_DAYS]) ? $this->wp_options [AI_OPTION_AFTER_DAYS] : "";
// if ($option == '') $option = AD_ZERO;
if ($option == '0') $option = '';
return $option;
}
public function get_schedule_start_date(){
$option = isset ($this->wp_options [AI_OPTION_START_DATE]) ? $this->wp_options [AI_OPTION_START_DATE] : "";
return $option;
}
public function get_schedule_end_date(){
$option = isset ($this->wp_options [AI_OPTION_END_DATE]) ? $this->wp_options [AI_OPTION_END_DATE] : "";
return $option;
}
public function get_schedule_start_time(){
$option = isset ($this->wp_options [AI_OPTION_START_TIME]) ? $this->wp_options [AI_OPTION_START_TIME] : "";
return $option;
}
public function get_schedule_end_time(){
$option = isset ($this->wp_options [AI_OPTION_END_TIME]) ? $this->wp_options [AI_OPTION_END_TIME] : "";
return $option;
}
public function get_schedule_weekdays (){
$option = isset ($this->wp_options [AI_OPTION_WEEKDAYS]) ? $this->wp_options [AI_OPTION_WEEKDAYS] : DEFAULT_WEEKDAYS;
return $option;
}
public function get_scheduling_fallback(){
$option = isset ($this->wp_options [AI_OPTION_SCHEDULING_FALLBACK]) ? $this->wp_options [AI_OPTION_SCHEDULING_FALLBACK] : "";
return $option;
}
public function get_maximum_insertions (){
$option = isset ($this->wp_options [AI_OPTION_MAXIMUM_INSERTIONS]) ? $this->wp_options [AI_OPTION_MAXIMUM_INSERTIONS] : "";
if ($option == '0') $option = '';
return $option;
}
public function get_id_list(){
$option = isset ($this->wp_options [AI_OPTION_ID_LIST]) ? $this->wp_options [AI_OPTION_ID_LIST] : "";
return $option;
}
public function get_id_list_type (){
$option = isset ($this->wp_options [AI_OPTION_ID_LIST_TYPE]) ? $this->wp_options [AI_OPTION_ID_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_url_list(){
$option = isset ($this->wp_options [AI_OPTION_URL_LIST]) ? $this->wp_options [AI_OPTION_URL_LIST] : "";
return $option;
}
public function get_ad_url_list_type (){
$option = isset ($this->wp_options [AI_OPTION_URL_LIST_TYPE]) ? $this->wp_options [AI_OPTION_URL_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_url_parameter_list(){
$option = isset ($this->wp_options [AI_OPTION_URL_PARAMETER_LIST]) ? $this->wp_options [AI_OPTION_URL_PARAMETER_LIST] : "";
return $option;
}
public function get_url_parameter_list_type (){
$option = isset ($this->wp_options [AI_OPTION_URL_PARAMETER_LIST_TYPE]) ? $this->wp_options [AI_OPTION_URL_PARAMETER_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_cookie_list(){
$option = isset ($this->wp_options [AI_OPTION_COOKIE_LIST]) ? $this->wp_options [AI_OPTION_COOKIE_LIST] : "";
return $option;
}
public function get_cookie_list_type (){
$option = isset ($this->wp_options [AI_OPTION_COOKIE_LIST_TYPE]) ? $this->wp_options [AI_OPTION_COOKIE_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
return $option;
}
public function get_ad_domain_list(){
$option = isset ($this->wp_options [AI_OPTION_DOMAIN_LIST]) ? $this->wp_options [AI_OPTION_DOMAIN_LIST] : "";
return $option;
}
public function get_ad_domain_list_type (){
$option = isset ($this->wp_options [AI_OPTION_DOMAIN_LIST_TYPE]) ? $this->wp_options [AI_OPTION_DOMAIN_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_client_list(){
$option = isset ($this->wp_options [AI_OPTION_CLIENT_LIST]) ? $this->wp_options [AI_OPTION_CLIENT_LIST] : "";
return $option;
}
public function get_client_list_type (){
$option = isset ($this->wp_options [AI_OPTION_CLIENT_LIST_TYPE]) ? $this->wp_options [AI_OPTION_CLIENT_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
return $option;
}
public function get_ad_ip_address_list (){
$option = isset ($this->wp_options [AI_OPTION_IP_ADDRESS_LIST]) ? $this->wp_options [AI_OPTION_IP_ADDRESS_LIST] : "";
return $option;
}
public function get_ad_ip_address_list_type (){
$option = isset ($this->wp_options [AI_OPTION_IP_ADDRESS_LIST_TYPE]) ? $this->wp_options [AI_OPTION_IP_ADDRESS_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_country_list ($expand = false){
$option = isset ($this->wp_options [AI_OPTION_COUNTRY_LIST]) ? $this->wp_options [AI_OPTION_COUNTRY_LIST] : "";
if ($expand && function_exists ('expanded_country_list')) return expanded_country_list ($option);
return $option;
}
public function get_ad_country_list_type (){
$option = isset ($this->wp_options [AI_OPTION_COUNTRY_LIST_TYPE]) ? $this->wp_options [AI_OPTION_COUNTRY_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_name(){
$option = isset ($this->wp_options [AI_OPTION_BLOCK_NAME]) ? $this->wp_options [AI_OPTION_BLOCK_NAME] : "";
if ($option == '') $option = DEFAULT_AD_NAME. " " . $this->number;
return $option;
}
public function get_ad_block_cat(){
$option = isset ($this->wp_options [AI_OPTION_CATEGORY_LIST]) ? $this->wp_options [AI_OPTION_CATEGORY_LIST] : "";
return $option;
}
public function get_ad_block_cat_type(){
$option = isset ($this->wp_options [AI_OPTION_CATEGORY_LIST_TYPE]) ? $this->wp_options [AI_OPTION_CATEGORY_LIST_TYPE] : AI_BLACK_LIST;
// Update old data
if ($option == ''){
$option = AI_BLACK_LIST;
$this->wp_options [AI_OPTION_CATEGORY_LIST_TYPE] = AI_BLACK_LIST;
}
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_block_tag(){
$option = isset ($this->wp_options [AI_OPTION_TAG_LIST]) ? $this->wp_options [AI_OPTION_TAG_LIST] : "";
return $option;
}
public function get_ad_block_tag_type(){
$option = isset ($this->wp_options [AI_OPTION_TAG_LIST_TYPE]) ? $this->wp_options [AI_OPTION_TAG_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_block_taxonomy(){
$option = isset ($this->wp_options [AI_OPTION_TAXONOMY_LIST]) ? $this->wp_options [AI_OPTION_TAXONOMY_LIST] : "";
return $option;
}
public function get_ad_block_taxonomy_type(){
$option = isset ($this->wp_options [AI_OPTION_TAXONOMY_LIST_TYPE]) ? $this->wp_options [AI_OPTION_TAXONOMY_LIST_TYPE] : AI_BLACK_LIST;
if ($option == '') $option = AI_BLACK_LIST;
elseif ($option == AD_BLACK_LIST) $option = AI_BLACK_LIST;
elseif ($option == AD_WHITE_LIST) $option = AI_WHITE_LIST;
return $option;
}
public function get_ad_enabled_on_which_pages (){
// Old option
if (isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES])) {
$option = isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES]) ? $this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES] : AI_IGNORE_EXCEPTIONS;
if ($option == '') $option = AI_IGNORE_EXCEPTIONS;
elseif ($option == AD_ENABLED_ON_ALL) $option = AI_IGNORE_EXCEPTIONS;
elseif ($option == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED) $option = AI_DEFAULT_INSERTION_ENABLED;
elseif ($option == AD_ENABLED_ONLY_ON_SELECTED) $option = AI_DEFAULT_INSERTION_DISABLED;
return $option;
}
if (!$this->get_exceptions_enabled ()) {
return AI_IGNORE_EXCEPTIONS;
}
return $this->get_exceptions_function ();
}
public function get_ad_enabled_on_which_pages_text ($translated = true){
switch ($this->get_ad_enabled_on_which_pages ()) {
case AI_IGNORE_EXCEPTIONS:
if (!$translated) return AI_TEXT_ENG_NO_INDIVIDUAL_EXCEPTIONS;
return AI_TEXT_PAGES_NO_INDIVIDUAL_EXCEPTIONS;
break;
case AI_DEFAULT_INSERTION_ENABLED:
if (!$translated) return AI_TEXT_ENG_INDIVIDUALLY_DISABLED;
return AI_TEXT_PAGES_INDIVIDUALLY_DISABLED;
break;
case AI_DEFAULT_INSERTION_DISABLED:
if (!$translated) return AI_TEXT_ENG_INDIVIDUALLY_ENABLED;
return AI_TEXT_PAGES_INDIVIDUALLY_ENABLED;
break;
default:
return '';
break;
}
}
public function get_ad_enabled_on_which_posts (){
// Old option
if (isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_POSTS])) {
$option = $this->wp_options [AI_OPTION_ENABLED_ON_WHICH_POSTS];
if ($option == '') $option = AI_IGNORE_EXCEPTIONS;
elseif ($option == AD_ENABLED_ON_ALL) $option = AI_IGNORE_EXCEPTIONS;
elseif ($option == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED) $option = AI_DEFAULT_INSERTION_ENABLED;
elseif ($option == AD_ENABLED_ONLY_ON_SELECTED) $option = AI_DEFAULT_INSERTION_DISABLED;
return $option;
}
if (!$this->get_exceptions_enabled ()) {
return AI_IGNORE_EXCEPTIONS;
}
return $this->get_exceptions_function ();
}
public function get_ad_enabled_on_which_posts_text ($translated = true){
switch ($this->get_ad_enabled_on_which_posts ()) {
case AI_IGNORE_EXCEPTIONS:
if (!$translated) return AI_TEXT_ENG_NO_INDIVIDUAL_EXCEPTIONS;
return AI_TEXT_POSTS_NO_INDIVIDUAL_EXCEPTIONS;
break;
case AI_DEFAULT_INSERTION_ENABLED:
if (!$translated) return AI_TEXT_ENG_INDIVIDUALLY_DISABLED;
return AI_TEXT_POSTS_INDIVIDUALLY_DISABLED;
break;
case AI_DEFAULT_INSERTION_DISABLED:
if (!$translated) return AI_TEXT_ENG_INDIVIDUALLY_ENABLED;
return AI_TEXT_POSTS_INDIVIDUALLY_ENABLED;
break;
default:
return '';
break;
}
}
public function import_old_exception_settings (&$needs_check){
$needs_check = false;
if (isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_POSTS]) || isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES])) {
// Import old settings
$posts = isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_POSTS]) ? $this->get_ad_enabled_on_which_posts () : AI_IGNORE_EXCEPTIONS;
$pages = isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES]) ? $this->get_ad_enabled_on_which_pages () : AI_IGNORE_EXCEPTIONS;
if ($posts == $pages) {
return $posts;
}
if (!$this->get_display_settings_page ()) {
return $posts;
}
if (!$this->get_display_settings_post ()) {
return $pages;
}
$needs_check = true;
// POSTS = AI_DEFAULT_INSERTION_DISABLED NO INSERTION ON PAGES
// POSTS = AI_DEFAULT_INSERTION_ENABLED OK
if ($pages == AI_IGNORE_EXCEPTIONS) {
return $posts;
}
// PAGES = AI_DEFAULT_INSERTION_DISABLED NO INSERTION ON POSTS
// PAGES = AI_DEFAULT_INSERTION_ENABLED OK
if ($posts == AI_IGNORE_EXCEPTIONS) {
return $pages;
}
// POSTS = AI_DEFAULT_INSERTION_DISABLED NO INSERTION ON PAGES
// PAGES = AI_DEFAULT_INSERTION_ENABLED
// POSTS = AI_DEFAULT_INSERTION_ENABLED INVERTED INSERTION ON PAGES
// PAGES = AI_DEFAULT_INSERTION_DISABLED
return $posts;
}
return - 1;
}
public function get_exceptions_enabled (){
$needs_check = false;
$option = $this->import_old_exception_settings ($needs_check);
if ($option >= 0) {
return $option != AI_IGNORE_EXCEPTIONS;
}
$option = isset ($this->wp_options [AI_OPTION_EXCEPTIONS_ENABLED]) ? $this->wp_options [AI_OPTION_EXCEPTIONS_ENABLED] : AI_DISABLED;
return $option;
}
public function get_exceptions_function (){
$needs_check = false;
$option = $this->import_old_exception_settings ($needs_check);
if ($option >= 0) {
if ($option == AI_IGNORE_EXCEPTIONS) $option == AI_DEFAULT_INSERTION_ENABLED;
return $option;
}
$option = isset ($this->wp_options [AI_OPTION_EXCEPTIONS_FUNCTION]) ? $this->wp_options [AI_OPTION_EXCEPTIONS_FUNCTION] : AI_DEFAULT_INSERTION_ENABLED;
return $option;
}
public function get_exceptions_function_text ($translated = true){
switch ($this->get_exceptions_function ()) {
case AI_DEFAULT_INSERTION_ENABLED:
if (!$translated) return AI_TEXT_ENG_ENABLED;
return AI_TEXT_ENABLED;
break;
case AI_DEFAULT_INSERTION_DISABLED:
if (!$translated) return AI_TEXT_ENG_DISABLED;
return AI_TEXT_DISABLED;
break;
default:
return '';
break;
}
}
public function get_name_class () {
$name = $this->get_ad_name ();
if ($name == '') return '';
return strtolower (str_replace ('--', '-', preg_replace ('/[^\-_a-zA-Z0-9]/', '', str_replace (' ', '-', html_entity_decode ($name)))));
}
public function get_viewport_classes () {
global $ai_wp_data;
// if ($ai_wp_data [AI_WP_AMP_PAGE]) return '';
$viewport_classes = "";
if ($this->get_detection_client_side ()) {
$all_viewports = true;
for ($viewport = 1; $viewport <= 6; $viewport ++) {
$viewport_name = get_viewport_name ($viewport);
if ($viewport_name != '') {
if ($this->get_detection_viewport ($viewport)) $viewport_classes .= " ai-viewport-" . $viewport; else $all_viewports = false;
}
}
if ($viewport_classes == "") $viewport_classes = " ai-viewport-0";
elseif ($all_viewports) $viewport_classes = "";
}
return ($viewport_classes);
}
public function get_viewport_names () {
global $ai_wp_data;
if ($ai_wp_data [AI_WP_AMP_PAGE]) return '';
$viewport_names = array ();
if ($this->get_detection_client_side ()) {
for ($viewport = 1; $viewport <= 6; $viewport ++) {
$viewport_name = get_viewport_name ($viewport);
if ($viewport_name != '') {
if ($this->get_detection_viewport ($viewport)) $viewport_names []= $viewport_name;
}
}
}
return (implode (', ', $viewport_names));
}
public function get_alignment_class ($block_class_name = null){
global $ai_wp_data;
if (defined ('AI_AMP_HEADER_STYLES') && AI_AMP_HEADER_STYLES && $ai_wp_data [AI_WP_AMP_PAGE] ||
defined ('AI_NORMAL_HEADER_STYLES') && AI_NORMAL_HEADER_STYLES && !$ai_wp_data [AI_WP_AMP_PAGE] && !get_inline_styles ()) {
return $this->generate_alignment_class ($block_class_name);
}
return '';
}
public function generate_alignment_class ($block_class_name = null){
if ($block_class_name == null) $block_class_name = get_block_class_name (true);
$block_class_name .= '-';
switch ($this->get_alignment_type ()) {
case AI_ALIGNMENT_DEFAULT:
case AI_ALIGNMENT_LEFT:
case AI_ALIGNMENT_RIGHT:
case AI_ALIGNMENT_CENTER:
case AI_ALIGNMENT_FLOAT_LEFT:
case AI_ALIGNMENT_FLOAT_RIGHT:
case AI_ALIGNMENT_STICKY_LEFT:
case AI_ALIGNMENT_STICKY_RIGHT:
case AI_ALIGNMENT_STICKY_TOP:
case AI_ALIGNMENT_STICKY_BOTTOM:
return $block_class_name . str_replace (' ', '-', strtolower ($this->get_alignment_type_text (false)));
break;
case AI_ALIGNMENT_STICKY:
// return $block_class_name . str_replace (' ', '-', strtolower (md5 ($this->alignment_style ($this->get_alignment_type ()))));
return $block_class_name . strtolower (md5 ($this->alignment_style ($this->get_alignment_type ())));
break;
case AI_ALIGNMENT_CUSTOM_CSS:
// return $block_class_name . str_replace (' ', '-', strtolower (md5 ($this->get_custom_css ())));
return $block_class_name . strtolower (md5 ($this->get_custom_css ()));
break;
}
return '';
}
public function get_size_class ($block_class_name = null){
global $ai_wp_data;
if (defined ('AI_AMP_HEADER_STYLES') && AI_AMP_HEADER_STYLES && $ai_wp_data [AI_WP_AMP_PAGE] ||
defined ('AI_NORMAL_HEADER_STYLES') && AI_NORMAL_HEADER_STYLES && !$ai_wp_data [AI_WP_AMP_PAGE] && !get_inline_styles ()) {
return $this->generate_size_class ($block_class_name);
}
return '';
}
public function generate_size_class ($block_class_name = null) {
$size_background_style = $this->size_background_style ();
if ($this->get_alignment_type () != AI_ALIGNMENT_CUSTOM_CSS && $size_background_style != '') {
if ($block_class_name == null) $block_class_name = get_block_class_name (true);
$block_class_name .= '-';
return $block_class_name . ai_css_to_name ($size_background_style);
}
return '';
}
public function before_image ($content, $position_preview = false) {
return $this->before_paragraph ($content, $position_preview, true);
}
public function before_paragraph ($content, $position_preview = false, $before_image = false) {
global $ai_wp_data, $ai_last_check, $special_element_tags, $ad_inserter_globals;
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
$multibyte = $ai_wp_data [AI_MBSTRING_LOADED] && get_paragraph_counting_functions() == AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS;
$paragraph_positions = array ();
$active_paragraph_positions = array ();
$dummy = array ();
if ($before_image) {
$paragraph_tags = 'figure,img,amp-img';
} else $paragraph_tags = trim ($this->get_paragraph_tags());
if ($paragraph_tags == '') return $content;
$paragraph_start_strings = explode (",", $paragraph_tags);
$ai_last_check = AI_CHECK_PARAGRAPH_TAGS;
if (count ($paragraph_start_strings) == 0) return $content;
get_paragraph_start_positions ($content, $multibyte, $dummy, $paragraph_start_strings, $paragraph_positions, $active_paragraph_positions);
if (!isset ($paragraph_end_positions)) {
// Prepare end positions and sort them before sorting start positions
$paragraph_end_positions = array ();
$dummy = array ();
get_paragraph_end_positions ($content, $multibyte, $paragraph_positions, $paragraph_start_strings, $paragraph_end_positions, $dummy);
sort ($paragraph_end_positions);
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_WITH_TAGS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
sort ($paragraph_positions);
ksort ($active_paragraph_positions);
$new_active_paragraph_positions = array ();
foreach ($active_paragraph_positions as $active_paragraph_position) {
$new_active_paragraph_positions [] = $active_paragraph_position;
}
$active_paragraph_positions = $new_active_paragraph_positions;
if (!$this->get_count_inside_blockquote () || $before_image) {
$special_element_offsets = array ();
if ($before_image) {
$special_element_tags_array = array_unique (array_merge ($special_element_tags, array ('figure', 'amp-img')));
} else $special_element_tags_array = $special_element_tags;
foreach ($special_element_tags_array as $special_element_tag) {
preg_match_all ("/<\/?$special_element_tag/i", $content, $special_elements, PREG_OFFSET_CAPTURE);
$nesting = array ();
$special_elements = $special_elements [0];
foreach ($special_elements as $index => $special_element) {
if (isset ($special_elements [$index + 1][0])) {
$tag1 = strtolower ($special_element [0]);
$tag2 = strtolower ($special_elements [$index + 1][0]);
$start_offset = $special_element [1];
$nesting_ended = false;
$tag1_start = $tag1 == "<$special_element_tag";
$tag2_start = $tag2 == "<$special_element_tag";
$tag1_end = $tag1 == "$special_element_tag";
$tag2_end = $tag2 == "$special_element_tag";
if ($tag1_start && $tag2_start) {
array_push ($nesting, $start_offset);
continue;
}
elseif ($tag1_end && $tag2_end) {
$start_offset = array_pop ($nesting);
if (count ($nesting) == 0) $nesting_ended = true;
}
if (count ($nesting) != 0) continue;
if (($nesting_ended || $tag1_start) && $tag2_end) {
if ($multibyte) {
$special_element_offsets []= array (mb_strlen (substr ($content, 0, $start_offset)) + 1, mb_strlen (substr ($content, 0, $special_elements [$index + 1][1])));
} else {
$special_element_offsets []= array ($start_offset + 1, $special_elements [$index + 1][1]);
}
}
}
}
}
if (count ($special_element_offsets) != 0) {
$inside_special_element = array ();
foreach ($special_element_offsets as $special_element_offset) {
foreach ($paragraph_positions as $paragraph_position) {
if ($paragraph_position >= $special_element_offset [0] && $paragraph_position <= $special_element_offset [1]) $inside_special_element [] = $paragraph_position;
}
}
foreach ($paragraph_positions as $index => $paragraph_position) {
if (in_array ($paragraph_position, $inside_special_element)) $active_paragraph_positions [$index] = 0;
}
}
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_NO_COUNTING_INSIDE;
if (array_sum ($active_paragraph_positions) == 0) return $content;
}
$element_tags = $this->get_count_inside_elements ();
if ($element_tags != '') {
$special_element_tags_array = explode (',', str_replace (' ', '', $element_tags));
$count_inside = $this->get_count_inside ();
$element_text_type = $this->get_count_inside_elements_contain ();
$check_only_tag_attributes = $this->get_check_only_tag_attributes ();
$element_text = str_replace (' ', '', html_entity_decode ($this->get_count_inside_elements_text ()));
if (strpos ($element_text, ",") !== false) {
$element_texts = explode (',', $element_text);
}
elseif (trim ($element_text) != '') $element_texts = array (trim ($element_text)); else $element_texts = array ();
$special_element_offsets = array ();
foreach ($special_element_tags_array as $special_element_tag) {
preg_match_all ("/<\/?$special_element_tag/i", $content, $special_elements, PREG_OFFSET_CAPTURE);
$special_elements = $special_elements [0];
if (count ($special_elements) % 2 != 0) {
if ($debug_processing) ai_log ('INVALID TAGS: ODD NUMBER OF ' . $special_element_tag . ' TAGS');
}
$elements = array ();
$nesting = array ();
foreach ($special_elements as $index => $special_element) {
$tag = strtolower ($special_element [0]);
$tag_start = $tag == "<$special_element_tag";
$tag_end = $tag == "$special_element_tag";
if ($tag_start) {
array_push ($nesting, $index);
continue;
}
if ($tag_end) {
if (count ($nesting) != 0) {
$start_tag_index = array_pop ($nesting);
$elements []= array ($special_elements [$start_tag_index], $special_element);
} else if ($debug_processing) ai_log ('INVALID TAGS: MISSING OPENING ' . $special_element_tag);
}
}
foreach ($elements as $element) {
$start_offset = $element [0][1];
$end_offset = $element [1][1];
if ($multibyte) {
$element_offsets = array (mb_strlen (substr ($content, 0, $start_offset)) + 1, mb_strlen (substr ($content, 0, $end_offset)));
} else {
$element_offsets = array ($start_offset + 1, $end_offset);
}
if (!empty ($element_texts)) {
$check = false;
foreach ($element_texts as $element_text) {
$search_content = substr ($content, $element_offsets [0], $element_offsets [1] - $element_offsets [0]);
if ($check_only_tag_attributes) {
preg_match ('#([^>]+?)>#', $search_content, $search_content_tag);
if (isset ($search_content_tag [1])) {
$search_content = $search_content_tag [1];
}
}
if (stripos ($search_content, $element_text)) {
$check = true;
break;
}
}
switch ($element_text_type) {
case AI_CONTAIN:
if ($check) {
$special_element_offsets []= $element_offsets;
}
break;
default:
if (!$check) {
$special_element_offsets []= $element_offsets;
}
break;
}
} else {
$special_element_offsets []= $element_offsets;
}
}
}
$inside_special_element = array ();
if (count ($special_element_offsets) != 0) {
foreach ($special_element_offsets as $special_element_offset) {
foreach ($paragraph_positions as $paragraph_position) {
if ($paragraph_position >= $special_element_offset [0] + 1 && $paragraph_position <= $special_element_offset [1]) $inside_special_element [] = $paragraph_position;
}
}
}
foreach ($paragraph_positions as $index => $paragraph_position) {
$paragraph_inside = in_array ($paragraph_position, $inside_special_element);
switch ($count_inside) {
case AI_COUNT_ONLY:
if (!$paragraph_inside) $active_paragraph_positions [$index] = 0;
break;
default:
if ($paragraph_inside) $active_paragraph_positions [$index] = 0;
break;
}
}
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_NO_COUNTING_INSIDE_ELEMENTS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
}
if (!$before_image) {
$paragraph_min_words = intval ($this->get_minimum_paragraph_words());
$paragraph_max_words = intval ($this->get_maximum_paragraph_words());
if ($paragraph_min_words != 0 || $paragraph_max_words != 0) {
if (!isset ($paragraph_end_positions)) {
$paragraph_end_positions = array ();
$dummy = array ();
get_paragraph_end_positions ($content, $multibyte, $paragraph_positions, $paragraph_start_strings, $paragraph_end_positions, $dummy);
}
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index]) || !isset ($paragraph_end_positions [$index])) continue;
if ($active_paragraph_positions [$index] == 0) continue;
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
} else {
$paragraph_code = substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
}
if (!$this->check_number_of_words_in_paragraph ($paragraph_code, $paragraph_min_words, $paragraph_max_words)) $active_paragraph_positions [$index] = 0;
}
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
$paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
if (!isset ($paragraph_end_positions)) {
$paragraph_end_positions = array ();
$dummy = array ();
get_paragraph_end_positions ($content, $multibyte, $paragraph_positions, $paragraph_start_strings, $paragraph_end_positions, $dummy);
}
$paragraph_text_type = $this->get_paragraph_text_type ();
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($active_paragraph_positions [$index] == 0) continue;
if (!isset ($paragraph_end_positions [$index])) continue;
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
} else {
$paragraph_code = substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
}
if ($paragraph_text_type == AI_CONTAIN) {
$found = true;
foreach ($paragraph_texts as $paragraph_text) {
if (trim ($paragraph_text) == '') continue;
$paragraph_text = trim ($paragraph_text);
$invert = false;
if ($paragraph_text [0] == '^') {
$paragraph_text = substr ($paragraph_text, 1);
$invert = true;
}
if ($multibyte) {
$paragraph_text_found = mb_stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if (!$paragraph_text_found) {
$found = false;
break;
}
} else {
$paragraph_text_found = stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if (!$paragraph_text_found) {
$found = false;
break;
}
}
}
if (!$found) $active_paragraph_positions [$index] = 0;
} elseif ($paragraph_text_type == AI_DO_NOT_CONTAIN) {
$found = false;
foreach ($paragraph_texts as $paragraph_text) {
if (trim ($paragraph_text) == '') continue;
$paragraph_text = trim ($paragraph_text);
$invert = false;
if ($paragraph_text [0] == '^') {
$paragraph_text = substr ($paragraph_text, 1);
$invert = true;
}
if ($multibyte) {
$paragraph_text_found = mb_stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if ($paragraph_text_found) {
$found = true;
break;
}
} else {
$paragraph_text_found = stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if ($paragraph_text_found) {
$found = true;
break;
}
}
}
if ($found) $active_paragraph_positions [$index] = 0;
}
}
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_TEXT;
if (array_sum ($active_paragraph_positions) == 0) return $content;
} // !$before_image
$position_text = trim ($this->get_paragraph_number());
if ($before_image) {
$direction_type = AI_DIRECTION_FROM_TOP;
} else $direction_type = $this->get_direction_type();
// Prepare $paragraph_end_positions
// if ($position_preview || $position_text == '') { // Prepare always
if (!isset ($paragraph_end_positions)) {
$paragraph_end_positions = array ();
$dummy = array ();
get_paragraph_end_positions ($content, $multibyte, $paragraph_positions, $paragraph_start_strings, $paragraph_end_positions, $dummy);
}
$filtered_paragraph_end_positions = array ();
// Use $paragraph_positions for counting as it is checked for consistency
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index]) || !isset ($paragraph_end_positions [$index])) continue;
if ($active_paragraph_positions [$index]) $filtered_paragraph_end_positions [] = $paragraph_end_positions [$index];
}
$paragraph_end_positions = $filtered_paragraph_end_positions;
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$paragraph_end_positions = array_reverse ($paragraph_end_positions);
}
// }
$filtered_paragraph_positions = array ();
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index])) continue;
if ($active_paragraph_positions [$index]) $filtered_paragraph_positions [] = $paragraph_position;
}
$paragraph_positions = $filtered_paragraph_positions;
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$paragraph_positions = array_reverse ($paragraph_positions);
}
// $positions contains indexes in $paragraph_positions
$positions = array ();
if (!$position_preview) {
if (strpos ($position_text, '[') !== false) {
$random_range = false;
for ($i = 0; $i < strlen ($position_text); $i ++) {
switch ($position_text [$i]) {
case '[':
$random_range = true;
break;
case ']':
$random_range = false;
break;
case ',':
if ($random_range) {
$position_text [$i] = '|';
}
break;
}
}
}
$position = $position_text;
if (is_numeric ($position)) {
if ($position > 0 && $position < 1) {
$position = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
}
elseif ($position > - 1 && $position < 0) {
$position = intval (count ($paragraph_positions) + $position * (count ($paragraph_positions) - 1) - 0.5);
}
elseif ($position <= - 1) {
$position = count ($paragraph_positions) + $position;
}
elseif ($position == 0) {
$position = mt_rand (0, count ($paragraph_positions) - 1);
}
else $position --;
$positions = array ($position);
}
elseif (strpos ($position_text, '[') === 0 && strpos ($position_text, ']') === strlen ($position_text) - 1) {
$wanted_paragraphs_text = trim (substr ($position_text, 1, strlen ($position_text) - 2));
$wanted_paragraph_ranges = array ();
$wanted_paragraphs = array ();
if (strpos ($position_text, '|') !== false) {
$items = explode ('|', str_replace (' ', '', $wanted_paragraphs_text));
foreach ($items as $item) {
$wanted_paragraph_ranges []= $item;
}
} else $wanted_paragraph_ranges []= $wanted_paragraphs_text;
foreach ($wanted_paragraph_ranges as $wanted_paragraph_range) {
if (strpos ($wanted_paragraph_range, '-') !== false) {
$limits = explode ('-', $wanted_paragraph_range);
$limit_min = (int) $limits [0];
$limit_max = (int) $limits [1];
} else {
$limit_min = (int) $wanted_paragraph_range;
$limit_max = (int) $wanted_paragraph_range;
}
if ($limit_min < 0) $limit_min = 0;
else if ($limit_min > 9999) $limit_min = 9999;
if ($limit_max < 0) $limit_max = 0;
else if ($limit_max > 9999) $limit_max = 9999;
if ($limit_min > $limit_max) $limit_max = $limit_min;
while ($limit_max >= $limit_min) {
$wanted_paragraphs [] = $limit_max;
$limit_max --;
}
}
$wanted_paragraphs = array_unique ($wanted_paragraphs);
sort ($wanted_paragraphs);
if (count ($wanted_paragraphs) == 0) $wanted_paragraphs = array (0);
$random_paragraph = mt_rand (0, count ($wanted_paragraphs) - 1);
$paragraph = $wanted_paragraphs [$random_paragraph];
if ($paragraph == 0) {
$paragraph = mt_rand (0, count ($paragraph_positions) - 1);
} else $paragraph --;
$positions = array ($paragraph);
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' RANDOM PARAGRAPHS ' . str_replace ('|', ', ', $position_text) . ' => [' . implode (', ', $wanted_paragraphs) . '] of ' . count ($paragraph_positions) . ' => ' . ($paragraph + 1));
}
}
elseif (strpos ($position_text, ',') !== false) {
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' BEFORE PARAGRAPHS ' . str_replace ('|', ', ', $position_text) . ' of ' . count ($paragraph_positions) . ' available');
}
$new_positions = array ();
$positions = explode (',', str_replace (' ', '', $position_text));
foreach ($positions as $index => $position) {
if (isset ($position [0]) && strpos ($position, '[') === 0 && strpos ($position, ']') === strlen ($position) - 1) {
$wanted_paragraphs_text = trim (substr ($position, 1, strlen ($position) - 2));
$wanted_paragraph_ranges = array ();
$wanted_paragraphs = array ();
if (strpos ($position, '|') !== false) {
$items = explode ('|', str_replace (' ', '', $wanted_paragraphs_text));
foreach ($items as $item) {
$wanted_paragraph_ranges []= $item;
}
} else $wanted_paragraph_ranges []= $wanted_paragraphs_text;
foreach ($wanted_paragraph_ranges as $wanted_paragraph_range) {
if (strpos ($wanted_paragraph_range, '-') !== false) {
$limits = explode ('-', $wanted_paragraph_range);
$limit_min = (int) $limits [0];
$limit_max = (int) $limits [1];
} else {
$limit_min = (int) $wanted_paragraph_range;
$limit_max = (int) $wanted_paragraph_range;
}
if ($limit_min < 0) $limit_min = 0;
else if ($limit_min > 9999) $limit_min = 9999;
if ($limit_max < 0) $limit_max = 0;
else if ($limit_max > 9999) $limit_max = 9999;
if ($limit_min > $limit_max) $limit_max = $limit_min;
while ($limit_max >= $limit_min) {
$wanted_paragraphs [] = $limit_max;
$limit_max --;
}
}
$wanted_paragraphs = array_unique ($wanted_paragraphs);
sort ($wanted_paragraphs);
if (count ($wanted_paragraphs) == 0) $wanted_paragraphs = array (0);
$random_paragraph = mt_rand (0, count ($wanted_paragraphs) - 1);
$paragraph = $wanted_paragraphs [$random_paragraph];
if ($paragraph == 0) {
$paragraph = mt_rand (0, count ($paragraph_positions) - 1);
} else $paragraph --;
$positions [$index] = $paragraph;
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' RANDOM PARAGRAPHS ' . str_replace ('|', ', ', $position) . ' => [' . implode (', ', $wanted_paragraphs) . '] of ' . count ($paragraph_positions) . ' => ' . ($paragraph + 1));
}
}
elseif (isset ($position [0]) && $position [0] == '%') {
unset ($positions [$index]);
$position = substr ($position, 1);
$mod_value = $position;
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($index + 1 < $offset_value) continue;
if (($index + 1 - $offset_value) % $mod_value == 0) $new_positions []= $index;
}
}
}
elseif (is_numeric ($position) && $position > 0 && $position < 1) {
$positions [$index] = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
}
elseif (is_numeric ($position) && $position > - 1 && $position < 0) {
$positions [$index] = intval (count ($paragraph_positions) + $position * (count ($paragraph_positions) - 1) - 0.5);
}
elseif (is_numeric ($position) && $position <= - 1) {
$positions [$index] = count ($paragraph_positions) + $position;
}
elseif ($position == 0) {
$positions [$index] = mt_rand (0, count ($paragraph_positions) - 1);
}
else $positions [$index] = (int) $position - 1;
}
$positions = array_unique (array_merge ($positions, $new_positions));
sort ($positions);
}
elseif (isset ($position_text [0]) && $position_text [0] == '%') {
$position_text = substr ($position_text, 1);
$mod_value = $position_text;
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($index + 1 < $offset_value) continue;
if (($index + 1 - $offset_value) % $mod_value == 0) $positions []= $index;
}
}
}
elseif ($position_text == '') {
$positions = array ();
$min_words_above = $this->get_minimum_words_above ();
if (!empty ($min_words_above) && !$before_image) {
$words_above = 0;
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($direction_type != AI_DIRECTION_FROM_BOTTOM) {
if ($words_above >= $min_words_above) {
$positions []= $index;
$words_above = 0;
}
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
} else {
$paragraph_code = substr ($content, $paragraph_position, $paragraph_end_positions [$index] - $paragraph_position + 1);
}
$words_above += number_of_words ($paragraph_code);
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
if ($words_above >= $min_words_above) {
$positions []= $index;
$words_above = 0;
}
}
}
} else
foreach ($paragraph_positions as $index => $paragraph_position) {
$positions []= $index;
}
$filter_settings = trim (str_replace (' ', '', $this->get_call_filter()));
if (!empty ($filter_settings)) {
$filter_counter = $before_image ? AI_FILTER_IMAGES : AI_FILTER_PARAGRAPHS;
if ($this->get_filter_type() == $filter_counter) {
$filter_values = array ();
if (strpos ($filter_settings, ",") !== false) {
$filter_values = explode (",", $filter_settings);
} else $filter_values []= $filter_settings;
$inverted_filter = $this->get_inverted_filter();
$filtered_positions = array ();
foreach ($positions as $index => $position) {
$insert = false;
if (in_array ($index + 1, $filter_values)) {
$insert = true;
} else {
foreach ($filter_values as $filter_value) {
$filter_value = trim ($filter_value);
if (isset ($filter_value [0]) && $filter_value [0] == '%') {
$mod_value = substr ($filter_value, 1);
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
// if (($index + 1) % $mod_value == 0) {
if ($index + 1 >= $offset_value && ($index + 1 - $offset_value) % $mod_value == 0) {
$insert = true;
break;
}
}
}
}
}
if ($insert xor $inverted_filter) $filtered_positions []= $position;
}
$positions = $filtered_positions;
}
}
}
}
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
if (!empty ($positions) && !$before_image) {
// if (!isset ($paragraph_end_positions)) {
// $paragraph_end_positions = array ();
// $dummy = array ();
// get_paragraph_end_positions ($content, $multibyte, $paragraph_positions, $paragraph_start_strings, $paragraph_end_positions, $dummy);
// }
$avoid_paragraphs_above = intval ($this->get_avoid_paragraphs_above());
$avoid_paragraphs_below = intval ($this->get_avoid_paragraphs_below());
$avoid_text_above = $this->get_avoid_text_above();
$avoid_text_below = $this->get_avoid_text_below();
$avoid_paragraph_texts_above = explode (",", html_entity_decode (trim ($avoid_text_above)));
$avoid_paragraph_texts_below = explode (",", html_entity_decode (trim ($avoid_text_below)));
$check_direction = $this->get_avoid_direction();
$max_checks = $this->get_avoid_try_limit();
$check_strictly_inside_paragraphs = false;
$failed_clearance_positions = array ();
foreach ($positions as $position_index => $position) {
$direction = $check_direction;
if (($avoid_paragraphs_above != 0 || $avoid_paragraphs_below != 0) && count ($paragraph_positions) > $position) {
if ($debug_processing && $this->number != 0) {
ai_log ('---------------------------------');
ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1));
}
$checks = $max_checks;
$saved_position = $position;
do {
$found_above = false;
$paragraph_text_found_above = '';
if ($position != 0 && $avoid_paragraphs_above != 0 && $avoid_text_above != "" && is_array ($avoid_paragraph_texts_above) && count ($avoid_paragraph_texts_above) != 0) {
for ($avoid_paragraph_above = $avoid_paragraphs_above; $avoid_paragraph_above > 0; $avoid_paragraph_above --) {
$paragraph_position_above = $position - $avoid_paragraph_above;
if (!$check_strictly_inside_paragraphs) {
if ($paragraph_position_above < 0) {
// If paragraph position is not the first one start with the first paragraph
if ($position != 0) $paragraph_position_above = 0;
// Othewise do not check anything
}
}
if ($paragraph_position_above >= 0) {
if ($check_strictly_inside_paragraphs) {
$start_position_above = $paragraph_positions [$paragraph_position_above];
$end_position_above = $paragraph_end_positions [$paragraph_position_above];
} else {
$start_position_above = $paragraph_positions [$paragraph_position_above];
$end_position_above = $paragraph_positions [$position];
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $start_position_above, $end_position_above - $start_position_above);
} else {
$paragraph_code = substr ($content, $start_position_above, $end_position_above - $start_position_above);
}
foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
if (trim ($paragraph_text_above) == '') continue;
if ($multibyte) {
if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
$found_above = true;
$paragraph_text_found_above = $paragraph_text_above;
break;
}
} else {
if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
$found_above = true;
$paragraph_text_found_above = $paragraph_text_above;
break;
}
}
}
}
if ($found_above || !$check_strictly_inside_paragraphs) break;
}
}
$found_below = false;
$paragraph_text_found_below = '';
if ($avoid_paragraphs_below != 0 && $avoid_text_below != "" && is_array ($avoid_paragraph_texts_below) && count ($avoid_paragraph_texts_below) != 0) {
for ($avoid_paragraph_below = $avoid_paragraphs_below; $avoid_paragraph_below > 0; $avoid_paragraph_below --) {
$paragraph_position_below = $position + $avoid_paragraph_below - 1;
if (!$check_strictly_inside_paragraphs) {
if ($paragraph_position_below >= count ($paragraph_positions)) $paragraph_position_below = count ($paragraph_positions) - 1;
}
if ($paragraph_position_below < count ($paragraph_positions)) {
if ($check_strictly_inside_paragraphs) {
$start_position_below = $paragraph_positions [$paragraph_position_below];
$end_position_below = $paragraph_end_positions [$paragraph_position_below];
} else {
$start_position_below = $paragraph_positions [$position];
$end_position_below = $paragraph_end_positions [$paragraph_position_below];
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $start_position_below, $end_position_below - $start_position_below);
} else {
$paragraph_code = substr ($content, $start_position_below, $end_position_below - $start_position_below);
}
foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
if (trim ($paragraph_text_below) == '') continue;
if ($multibyte) {
if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
$found_below = true;
$paragraph_text_found_below = $paragraph_text_below;
break;
}
} else {
if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
$found_below = true;
$paragraph_text_found_below = $paragraph_text_below;
break;
}
}
}
}
if ($found_below || !$check_strictly_inside_paragraphs) break;
}
}
// echo "position: $position = before #", $position + 1, "
\n";
// echo "checks: $checks
\n";
// echo "direction: $direction
\n";
// if ($found_above)
// echo "found_above
\n";
// if ($found_below)
// echo "found_below
\n";
// echo "=================
\n";
if ($found_above || $found_below) {
if ($debug_processing && $this->number != 0) ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1) .
' FAILED (' . ($found_above ? 'ABOVE: ' . $paragraph_text_found_above .($found_below ? ', ' : '') : '') . ($found_below ? 'BELOW: ' . $paragraph_text_found_below : '') . ')');
$ai_last_check = AI_CHECK_DO_NOT_INSERT;
if ($this->get_avoid_action() == AI_DO_NOT_INSERT) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break;
}
switch ($direction) {
case AI_ABOVE: // Try above
$ai_last_check = AI_CHECK_AD_ABOVE;
// Already at the top - do not insert
if ($position == 0) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position --;
break;
case AI_BELOW: // Try below
$ai_last_check = AI_CHECK_AD_BELOW;
// Already at the bottom - do not insert
if ($position >= count ($paragraph_positions) - 1) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position ++;
break;
case AI_ABOVE_AND_THEN_BELOW: // Try first above and then below
if ($position == 0 || $checks == 0) {
// Try below
$direction = AI_BELOW;
$checks = $max_checks;
$position = $saved_position;
$ai_last_check = AI_CHECK_AD_BELOW;
// Already at the bottom - do not insert
if ($position >= count ($paragraph_positions) - 1) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position ++;
} else $position --;
break;
case AI_BELOW_AND_THEN_ABOVE: // Try first below and then above
if ($position >= count ($paragraph_positions) - 1 || $checks == 0) {
// Try above
$direction = AI_ABOVE;
$checks = $max_checks;
$position = $saved_position;
$ai_last_check = AI_CHECK_AD_ABOVE;
// Already at the top - do not insert
if ($position == 0) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position --;
} else $position ++;
break;
}
} else {
if ($debug_processing && $this->number != 0) ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1) . ' OK');
// Text not found - insert
$positions [$position_index] = $position;
break;
}
// Try next position
if ($checks <= 0) {
// Suitable position not found - do not insert
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break;
}
$checks --;
} while (true);
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_CLEARANCE;
if (count ($paragraph_positions) == 0) return $content;
}
if ($debug_processing && $this->number != 0) ai_log ('---------------------------------');
}
if (!$before_image) {
$no_insertion_first_paragraphs = intval ($this->get_skip_first_paragraphs ());
$no_insertion_last_paragraphs = intval ($this->get_skip_last_paragraphs ());
if ($no_insertion_first_paragraphs != 0 /*&& $no_insertion_first_paragraphs <= count ($paragraph_positions)*/) {
foreach ($positions as $index => $position) {
if ($position < $no_insertion_first_paragraphs) unset ($positions [$index]);
}
$positions = array_values ($positions);
}
if ($no_insertion_last_paragraphs != 0 /*&& $no_insertion_last_paragraphs <= count ($paragraph_positions)*/) {
foreach ($positions as $index => $position) {
if ($position >= count ($paragraph_positions) - $no_insertion_last_paragraphs) unset ($positions [$index]);
}
$positions = array_values ($positions);
}
}
if ($position_preview || !empty ($positions)) {
$offset = 0;
if (!empty ($positions)) $ai_last_check = AI_CHECK_PARAGRAPH_NUMBER;
$real_positions = array ();
foreach ($positions as $position_index) $real_positions []= $position_index >= 0 ? $position_index + 1 : '*';
if ($debug_processing && $this->number != 0 && count ($real_positions) != 0) ai_log ('BLOCK ' . $this->number . ' INSERTION POSITIONS: ' . implode (', ', $real_positions));
$min_paragraphs = intval ($this->get_paragraph_number_minimum());
$max_paragraphs = intval ($this->get_paragraph_number_maximum());
$max_page_blocks_enabled = $this->get_max_page_blocks_enabled ();
foreach ($paragraph_positions as $counter => $paragraph_position) {
if ($position_preview) {
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_position + $offset, $paragraph_end_positions [$counter] - $paragraph_position + 1);
} else {
$paragraph_code = substr ($content, $paragraph_position + $offset, $paragraph_end_positions [$counter] - $paragraph_position + 1);
}
$paragraph_words = number_of_words ($paragraph_code);
$debug_label = $before_image ? 'BI' : 'BP';
$inserted_code = "[[AI_".$debug_label.($counter + 1)."=".$paragraph_words."]]";
}
elseif (!empty ($positions) && in_array ($counter, $positions)) {
$inserted = false;
$ai_last_check = AI_CHECK_PARAGRAPHS_MIN_NUMBER;
if (count ($paragraph_positions) >= $min_paragraphs) {
$ai_last_check = AI_CHECK_PARAGRAPHS_MAX_NUMBER;
if ($max_paragraphs <= 0 || count ($paragraph_positions) <= $max_paragraphs) {
$ai_last_check = AI_CHECK_MAX_PAGE_BLOCKS;
if (!$max_page_blocks_enabled || $ai_wp_data [AI_PAGE_BLOCKS] < get_max_page_blocks ()) {
// Last check before insertion
if ($this->check_and_increment_block_counter ()) {
// Increment page block counter
if ($max_page_blocks_enabled) $ai_wp_data [AI_PAGE_BLOCKS] ++;
$ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
if (!$this->get_debug_disable_insertion ()) {
$inserted_code = $this->get_code_for_serverside_insertion ();
$ai_last_check = AI_CHECK_INSERTED;
$this->clear_code_cache ();
$inserted = true;
}
}
}
}
}
if ($debug_processing) ai_log (ai_log_block_status ($this->number, $ai_last_check));
if (!$inserted) continue;
}
else {
if ($debug_processing && isset ($failed_clearance_positions [$counter])) ai_log (ai_log_block_status ($this->number, $failed_clearance_positions [$counter]));
continue;
}
if ($multibyte) {
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$content = mb_substr ($content, 0, $paragraph_position) . $inserted_code . mb_substr ($content, $paragraph_position);
} else {
$content = mb_substr ($content, 0, $paragraph_position + $offset) . $inserted_code . mb_substr ($content, $paragraph_position + $offset);
$offset += mb_strlen ($inserted_code);
}
} else {
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$content = substr_replace ($content, $inserted_code, $paragraph_position, 0);
} else {
$content = substr_replace ($content, $inserted_code, $paragraph_position + $offset, 0);
$offset += strlen ($inserted_code);
}
}
}
$ai_last_check = AI_CHECK_NONE; // Already logged on each insertion
return $content;
}
// empty $positions
$ai_last_check = AI_CHECK_NO_PARAGRAPHS;
return $content;
}
public function after_image ($content, $position_preview = false) {
return $this->after_paragraph ($content, $position_preview, true);
}
public function after_paragraph ($content, $position_preview = false, $after_image = false) {
global $ai_wp_data, $ai_last_check, $special_element_tags;
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
$multibyte = $ai_wp_data [AI_MBSTRING_LOADED] && get_paragraph_counting_functions() == AI_MULTIBYTE_PARAGRAPH_COUNTING_FUNCTIONS;
$paragraph_positions = array ();
$active_paragraph_positions = array ();
$dummy = array ();
if ($multibyte) {
$last_content_position = mb_strlen ($content) - 1;
} else {
$last_content_position = strlen ($content) - 1;
}
if ($after_image) {
$paragraph_tags = 'figure,img,amp-img';
} else $paragraph_tags = trim ($this->get_paragraph_tags());
if ($paragraph_tags == '') return $content;
$paragraph_end_strings = explode (",", $paragraph_tags);
$ai_last_check = AI_CHECK_PARAGRAPH_TAGS;
if (count ($paragraph_end_strings) == 0) return $content;
$dummy = array ();
get_paragraph_end_positions ($content, $multibyte, $dummy, $paragraph_end_strings, $paragraph_positions, $active_paragraph_positions);
if (!isset ($paragraph_start_positions)) {
// Prepare start positions and sort them before sorting end positions
$paragraph_start_positions = array ();
$dummy = array ();
get_paragraph_start_positions ($content, $multibyte, $paragraph_positions, $paragraph_end_strings, $paragraph_start_positions, $dummy);
sort ($paragraph_start_positions);
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_WITH_TAGS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
sort ($paragraph_positions);
ksort ($active_paragraph_positions);
$new_active_paragraph_positions = array ();
foreach ($active_paragraph_positions as $active_paragraph_position) {
$new_active_paragraph_positions [] = $active_paragraph_position;
}
$active_paragraph_positions = $new_active_paragraph_positions;
if (!$this->get_count_inside_blockquote () || $after_image) {
$special_element_offsets = array ();
if ($after_image) {
$special_element_tags_array = array_unique (array_merge ($special_element_tags, array ('figure', 'amp-img')));
} else $special_element_tags_array = $special_element_tags;
foreach ($special_element_tags_array as $special_element_tag) {
preg_match_all ("/<\/?$special_element_tag/i", $content, $special_elements, PREG_OFFSET_CAPTURE);
$nesting = array ();
$special_elements = $special_elements [0];
foreach ($special_elements as $index => $special_element) {
if (isset ($special_elements [$index + 1][0])) {
$tag1 = strtolower ($special_element [0]);
$tag2 = strtolower ($special_elements [$index + 1][0]);
$start_offset = $special_element [1];
$nesting_ended = false;
$tag1_start = $tag1 == "<$special_element_tag";
$tag2_start = $tag2 == "<$special_element_tag";
$tag1_end = $tag1 == "$special_element_tag";
$tag2_end = $tag2 == "$special_element_tag";
if ($tag1_start && $tag2_start) {
array_push ($nesting, $start_offset);
continue;
}
elseif ($tag1_end && $tag2_end) {
$start_offset = array_pop ($nesting);
if (count ($nesting) == 0) $nesting_ended = true;
}
if (count ($nesting) != 0) continue;
if (($nesting_ended || $tag1_start) && $tag2_end) {
if ($multibyte) {
$special_element_offsets []= array (mb_strlen (substr ($content, 0, $start_offset)), mb_strlen (substr ($content, 0, $special_elements [$index + 1][1])));
} else {
$special_element_offsets []= array ($start_offset, $special_elements [$index + 1][1]);
}
}
}
}
}
if (count ($special_element_offsets) != 0) {
$inside_special_element = array ();
foreach ($special_element_offsets as $special_element_offset) {
foreach ($paragraph_positions as $paragraph_position) {
if ($paragraph_position >= $special_element_offset [0] && $paragraph_position <= $special_element_offset [1]) $inside_special_element [] = $paragraph_position;
}
}
foreach ($paragraph_positions as $index => $paragraph_position) {
if (in_array ($paragraph_position, $inside_special_element)) $active_paragraph_positions [$index] = 0;
}
}
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_NO_COUNTING_INSIDE;
if (array_sum ($active_paragraph_positions) == 0) return $content;
}
$element_tags = $this->get_count_inside_elements ();
if ($element_tags != '') {
$special_element_tags_array = explode (',', str_replace (' ', '', $element_tags));
$count_inside = $this->get_count_inside ();
$element_text_type = $this->get_count_inside_elements_contain ();
$check_only_tag_attributes = $this->get_check_only_tag_attributes ();
$element_text = str_replace (' ', '', html_entity_decode ($this->get_count_inside_elements_text ()));
if (strpos ($element_text, ",") !== false) {
$element_texts = explode (',', $element_text);
}
elseif (trim ($element_text) != '') $element_texts = array (trim ($element_text)); else $element_texts = array ();
$special_element_offsets = array ();
foreach ($special_element_tags_array as $special_element_tag) {
preg_match_all ("/<\/?$special_element_tag/i", $content, $special_elements, PREG_OFFSET_CAPTURE);
$special_elements = $special_elements [0];
if (count ($special_elements) % 2 != 0) {
if ($debug_processing) ai_log ('INVALID TAGS: ODD NUMBER OF ' . $special_element_tag . ' TAGS');
}
$elements = array ();
$nesting = array ();
foreach ($special_elements as $index => $special_element) {
$tag = strtolower ($special_element [0]);
$tag_start = $tag == "<$special_element_tag";
$tag_end = $tag == "$special_element_tag";
if ($tag_start) {
array_push ($nesting, $index);
continue;
}
if ($tag_end) {
if (count ($nesting) != 0) {
$start_tag_index = array_pop ($nesting);
$elements []= array ($special_elements [$start_tag_index], $special_element);
} else if ($debug_processing) ai_log ('INVALID TAGS: MISSING OPENING ' . $special_element_tag);
}
}
foreach ($elements as $element) {
$start_offset = $element [0][1];
$end_offset = $element [1][1];
if ($multibyte) {
$element_offsets = array (mb_strlen (substr ($content, 0, $start_offset)), mb_strlen (substr ($content, 0, $end_offset)));
} else {
$element_offsets = array ($start_offset, $end_offset);
}
if (!empty ($element_texts)) {
$check = false;
foreach ($element_texts as $element_text) {
$search_content = substr ($content, $element_offsets [0], $element_offsets [1] - $element_offsets [0]);
if ($check_only_tag_attributes) {
preg_match ('#([^>]+?)>#', $search_content, $search_content_tag);
if (isset ($search_content_tag [1])) {
$search_content = $search_content_tag [1];
}
}
if (stripos ($search_content, $element_text)) {
$check = true;
break;
}
}
switch ($element_text_type) {
case AI_CONTAIN:
if ($check) {
$special_element_offsets []= $element_offsets;
}
break;
default:
if (!$check) {
$special_element_offsets []= $element_offsets;
}
break;
}
} else {
$special_element_offsets []= $element_offsets;
}
}
}
$inside_special_element = array ();
if (count ($special_element_offsets) != 0) {
foreach ($special_element_offsets as $special_element_offset) {
foreach ($paragraph_positions as $paragraph_position) {
if ($paragraph_position >= $special_element_offset [0] && $paragraph_position <= $special_element_offset [1]) $inside_special_element [] = $paragraph_position;
}
}
}
foreach ($paragraph_positions as $index => $paragraph_position) {
$paragraph_inside = in_array ($paragraph_position, $inside_special_element);
switch ($count_inside) {
case AI_COUNT_ONLY:
if (!$paragraph_inside) $active_paragraph_positions [$index] = 0;
break;
default:
if ($paragraph_inside) $active_paragraph_positions [$index] = 0;
break;
}
}
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_NO_COUNTING_INSIDE_ELEMENTS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
}
if (!$after_image) {
$paragraph_min_words = intval ($this->get_minimum_paragraph_words());
$paragraph_max_words = intval ($this->get_maximum_paragraph_words());
if ($paragraph_min_words != 0 || $paragraph_max_words != 0) {
if (!isset ($paragraph_start_positions)) {
$paragraph_start_positions = array ();
$dummy = array ();
get_paragraph_start_positions ($content, $multibyte, $paragraph_positions, $paragraph_end_strings, $paragraph_start_positions, $dummy);
}
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index]) || !isset ($paragraph_start_positions [$index])) continue;
if ($active_paragraph_positions [$index] == 0) continue;
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
} else {
$paragraph_code = substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
}
if (!$this->check_number_of_words_in_paragraph ($paragraph_code, $paragraph_min_words, $paragraph_max_words)) $active_paragraph_positions [$index] = 0;
}
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_MIN_MAX_WORDS;
if (array_sum ($active_paragraph_positions) == 0) return $content;
$paragraph_texts = explode (",", html_entity_decode ($this->get_paragraph_text()));
if ($this->get_paragraph_text() != "" && count ($paragraph_texts) != 0) {
if (!isset ($paragraph_start_positions)) {
$paragraph_start_positions = array ();
$dummy = array ();
get_paragraph_start_positions ($content, $multibyte, $paragraph_positions, $paragraph_end_strings, $paragraph_start_positions, $dummy);
}
$paragraph_text_type = $this->get_paragraph_text_type ();
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($active_paragraph_positions [$index] == 0) continue;
if (!isset ($paragraph_start_positions [$index])) continue;
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
} else {
$paragraph_code = substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
}
if ($paragraph_text_type == AI_CONTAIN) {
$found = true;
foreach ($paragraph_texts as $paragraph_text) {
if (trim ($paragraph_text) == '') continue;
$paragraph_text = trim ($paragraph_text);
$invert = false;
if ($paragraph_text [0] == '^') {
$paragraph_text = substr ($paragraph_text, 1);
$invert = true;
}
if ($multibyte) {
$paragraph_text_found = mb_stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if (!$paragraph_text_found) {
$found = false;
break;
}
} else {
$paragraph_text_found = stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if (!$paragraph_text_found) {
$found = false;
break;
}
}
}
if (!$found) $active_paragraph_positions [$index] = 0;
} elseif ($paragraph_text_type == AI_DO_NOT_CONTAIN) {
$found = false;
foreach ($paragraph_texts as $paragraph_text) {
if (trim ($paragraph_text) == '') continue;
$paragraph_text = trim ($paragraph_text);
$invert = false;
if ($paragraph_text [0] == '^') {
$paragraph_text = substr ($paragraph_text, 1);
$invert = true;
}
if ($multibyte) {
$paragraph_text_found = mb_stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if ($paragraph_text_found) {
$found = true;
break;
}
} else {
$paragraph_text_found = stripos ($paragraph_code, $paragraph_text) !== false;
if ($invert) {
$paragraph_text_found = !$paragraph_text_found;
}
if ($paragraph_text_found) {
$found = true;
break;
}
}
}
if ($found) $active_paragraph_positions [$index] = 0;
}
}
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_TEXT;
if (array_sum ($active_paragraph_positions) == 0) return $content;
} // !$after_image
$position_text = trim ($this->get_paragraph_number());
if ($after_image) {
$direction_type = AI_DIRECTION_FROM_TOP;
} else $direction_type = $this->get_direction_type();
// Prepare $paragraph_start_positions
// if ($position_preview || $position_text == '') { // Prepare always
if (!isset ($paragraph_start_positions)) {
$paragraph_start_positions = array ();
$dummy = array ();
get_paragraph_start_positions ($content, $multibyte, $paragraph_positions, $paragraph_end_strings, $paragraph_start_positions, $dummy);
}
$filtered_paragraph_positions = array ();
// Use $paragraph_positions for counting as it is checked for consistency
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index]) || !isset ($paragraph_start_positions [$index])) continue;
if ($active_paragraph_positions [$index]) $filtered_paragraph_positions [] = $paragraph_start_positions [$index];
}
$paragraph_start_positions = $filtered_paragraph_positions;
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$paragraph_start_positions = array_reverse ($paragraph_start_positions);
}
// }
$filtered_paragraph_positions = array ();
foreach ($paragraph_positions as $index => $paragraph_position) {
// check for undefined array key
if (!isset ($active_paragraph_positions [$index])) continue;
if ($active_paragraph_positions [$index]) $filtered_paragraph_positions [] = $paragraph_position;
}
$paragraph_positions = $filtered_paragraph_positions;
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$paragraph_positions = array_reverse ($paragraph_positions);
}
// $positions contains indexes in $paragraph_positions
$positions = array ();
if (!$position_preview) {
if (strpos ($position_text, '[') !== false) {
$random_range = false;
for ($i = 0; $i < strlen ($position_text); $i ++) {
switch ($position_text [$i]) {
case '[':
$random_range = true;
break;
case ']':
$random_range = false;
break;
case ',':
if ($random_range) {
$position_text [$i] = '|';
}
break;
}
}
}
$position = $position_text;
if (is_numeric ($position)) {
if ($position > 0 && $position < 1) {
$position = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
}
elseif ($position > - 1 && $position < 0) {
$position = intval (count ($paragraph_positions) + $position * (count ($paragraph_positions) - 1) - 0.5);
}
elseif ($position <= - 1) {
$position = count ($paragraph_positions) + $position;
}
elseif ($position == 0) {
$position = mt_rand (0, count ($paragraph_positions) - 1);
}
else $position --;
$positions = array ($position);
}
elseif (strpos ($position_text, '[') === 0 && strpos ($position_text, ']') === strlen ($position_text) - 1) {
$wanted_paragraphs_text = trim (substr ($position_text, 1, strlen ($position_text) - 2));
$wanted_paragraph_ranges = array ();
$wanted_paragraphs = array ();
if (strpos ($position_text, '|') !== false) {
$items = explode ('|', str_replace (' ', '', $wanted_paragraphs_text));
foreach ($items as $item) {
$wanted_paragraph_ranges []= $item;
}
} else $wanted_paragraph_ranges []= $wanted_paragraphs_text;
foreach ($wanted_paragraph_ranges as $wanted_paragraph_range) {
if (strpos ($wanted_paragraph_range, '-') !== false) {
$limits = explode ('-', $wanted_paragraph_range);
$limit_min = (int) $limits [0];
$limit_max = (int) $limits [1];
} else {
$limit_min = (int) $wanted_paragraph_range;
$limit_max = (int) $wanted_paragraph_range;
}
if ($limit_min < 0) $limit_min = 0;
else if ($limit_min > 9999) $limit_min = 9999;
if ($limit_max < 0) $limit_max = 0;
else if ($limit_max > 9999) $limit_max = 9999;
if ($limit_min > $limit_max) $limit_max = $limit_min;
while ($limit_max >= $limit_min) {
$wanted_paragraphs [] = $limit_max;
$limit_max --;
}
}
$wanted_paragraphs = array_unique ($wanted_paragraphs);
sort ($wanted_paragraphs);
if (count ($wanted_paragraphs) == 0) $wanted_paragraphs = array (0);
$random_paragraph = mt_rand (0, count ($wanted_paragraphs) - 1);
$paragraph = $wanted_paragraphs [$random_paragraph];
if ($paragraph == 0) {
$paragraph = mt_rand (0, count ($paragraph_positions) - 1);
} else $paragraph --;
$positions = array ($paragraph);
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' RANDOM PARAGRAPHS ' . str_replace ('|', ', ', $position_text) . ' => [' . implode (',', $wanted_paragraphs) . '] of ' . count ($paragraph_positions) . ' => ' . ($paragraph + 1));
}
}
elseif (strpos ($position_text, ',') !== false) {
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' AFTER PARAGRAPHS ' . str_replace ('|', ', ', $position_text) . ' of ' . count ($paragraph_positions) . ' available');
}
$new_positions = array ();
$positions = explode (',', str_replace (' ', '', $position_text));
foreach ($positions as $index => $position) {
if (isset ($position [0]) && strpos ($position, '[') === 0 && strpos ($position, ']') === strlen ($position) - 1) {
$wanted_paragraphs_text = trim (substr ($position, 1, strlen ($position) - 2));
$wanted_paragraph_ranges = array ();
$wanted_paragraphs = array ();
if (strpos ($position, '|') !== false) {
$items = explode ('|', str_replace (' ', '', $wanted_paragraphs_text));
foreach ($items as $item) {
$wanted_paragraph_ranges []= $item;
}
} else $wanted_paragraph_ranges []= $wanted_paragraphs_text;
foreach ($wanted_paragraph_ranges as $wanted_paragraph_range) {
if (strpos ($wanted_paragraph_range, '-') !== false) {
$limits = explode ('-', $wanted_paragraph_range);
$limit_min = (int) $limits [0];
$limit_max = (int) $limits [1];
} else {
$limit_min = (int) $wanted_paragraph_range;
$limit_max = (int) $wanted_paragraph_range;
}
if ($limit_min < 0) $limit_min = 0;
else if ($limit_min > 9999) $limit_min = 9999;
if ($limit_max < 0) $limit_max = 0;
else if ($limit_max > 9999) $limit_max = 9999;
if ($limit_min > $limit_max) $limit_max = $limit_min;
while ($limit_max >= $limit_min) {
$wanted_paragraphs [] = $limit_max;
$limit_max --;
}
}
$wanted_paragraphs = array_unique ($wanted_paragraphs);
sort ($wanted_paragraphs);
if (count ($wanted_paragraphs) == 0) $wanted_paragraphs = array (0);
$random_paragraph = mt_rand (0, count ($wanted_paragraphs) - 1);
$paragraph = $wanted_paragraphs [$random_paragraph];
if ($paragraph == 0) {
$paragraph = mt_rand (0, count ($paragraph_positions) - 1);
} else $paragraph --;
$positions [$index] = $paragraph;
if ($debug_processing) {
ai_log ('BLOCK ' . $this->number . ' RANDOM PARAGRAPHS ' . str_replace ('|', ', ', $position) . ' => [' . implode (', ', $wanted_paragraphs) . '] of ' . count ($paragraph_positions) . ' => ' . ($paragraph + 1));
}
}
elseif (isset ($position [0]) && $position [0] == '%') {
unset ($positions [$index]);
$position = substr ($position, 1);
$mod_value = $position;
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($index + 1 < $offset_value) continue;
if (($index + 1 - $offset_value) % $mod_value == 0) $new_positions []= $index;
}
}
}
elseif (is_numeric ($position) && $position > 0 && $position < 1) {
$positions [$index] = intval ($position * (count ($paragraph_positions) - 1) + 0.5);
}
elseif (is_numeric ($position) && $position > - 1 && $position < 0) {
$positions [$index] = intval (count ($paragraph_positions) + $position * (count ($paragraph_positions) - 1) - 0.5);
}
elseif (is_numeric ($position) && $position <= - 1) {
$positions [$index] = count ($paragraph_positions) + $position;
}
elseif ($position == 0) {
$positions [$index] = mt_rand (0, count ($paragraph_positions) - 1);
}
else $positions [$index] = (int) $position - 1;
}
$positions = array_unique (array_merge ($positions, $new_positions));
sort ($positions);
}
elseif (isset ($position_text [0]) && $position_text [0] == '%') {
$position_text = substr ($position_text, 1);
$mod_value = $position_text;
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($index + 1 < $offset_value) continue;
if (($index + 1 - $offset_value) % $mod_value == 0) $positions []= $index;
}
}
}
elseif ($position_text == '') {
$positions = array ();
$min_words_above = $this->get_minimum_words_above ();
if (!empty ($min_words_above) && !$after_image) {
$words_above = 0;
foreach ($paragraph_positions as $index => $paragraph_position) {
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
if ($words_above >= $min_words_above) {
$positions []= $index;
$words_above = 0;
}
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
} else {
$paragraph_code = substr ($content, $paragraph_start_positions [$index], $paragraph_position - $paragraph_start_positions [$index] + 1);
}
$words_above += number_of_words ($paragraph_code);
if ($direction_type != AI_DIRECTION_FROM_BOTTOM) {
if ($words_above >= $min_words_above) {
$positions []= $index;
$words_above = 0;
}
}
}
} else
foreach ($paragraph_positions as $index => $paragraph_position) {
$positions []= $index;
}
$filter_settings = trim (str_replace (' ', '', $this->get_call_filter()));
if (!empty ($filter_settings)) {
$filter_counter = $after_image ? AI_FILTER_IMAGES : AI_FILTER_PARAGRAPHS;
if ($this->get_filter_type() == $filter_counter) {
$filter_values = array ();
if (strpos ($filter_settings, ",") !== false) {
$filter_values = explode (",", $filter_settings);
} else $filter_values []= $filter_settings;
$inverted_filter = $this->get_inverted_filter();
$filtered_positions = array ();
foreach ($positions as $index => $position) {
$insert = false;
if (in_array ($index + 1, $filter_values)) {
$insert = true;
} else {
foreach ($filter_values as $filter_value) {
$filter_value = trim ($filter_value);
if (isset ($filter_value [0]) && $filter_value [0] == '%') {
$mod_value = substr ($filter_value, 1);
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
// if (is_numeric ($mod_value) && $mod_value > 0) {
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
// if (($index + 1) % $mod_value == 0) {
if ($index + 1 >= $offset_value && ($index + 1 - $offset_value) % $mod_value == 0) {
$insert = true;
break;
}
}
}
}
}
if ($insert xor $inverted_filter) $filtered_positions []= $position;
}
$positions = $filtered_positions;
}
}
}
}
$debug_processing = ($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0;
if (!empty ($positions) && !$after_image) {
// Not needed anymore
// if (!isset ($paragraph_start_positions)) {
// $paragraph_start_positions = array ();
// $dummy = array ();
// get_paragraph_start_positions ($content, $multibyte, $paragraph_positions, $paragraph_end_strings, $paragraph_start_positions, $dummy);
// }
$avoid_paragraphs_above = intval ($this->get_avoid_paragraphs_above());
$avoid_paragraphs_below = intval ($this->get_avoid_paragraphs_below());
$avoid_text_above = $this->get_avoid_text_above();
$avoid_text_below = $this->get_avoid_text_below();
$avoid_paragraph_texts_above = explode (",", html_entity_decode (trim ($avoid_text_above)));
$avoid_paragraph_texts_below = explode (",", html_entity_decode (trim ($avoid_text_below)));
$check_direction = $this->get_avoid_direction();
$max_checks = $this->get_avoid_try_limit();
$check_strictly_inside_paragraphs = false;
$failed_clearance_positions = array ();
foreach ($positions as $position_index => $position) {
$direction = $check_direction;
if (($avoid_paragraphs_above != 0 || $avoid_paragraphs_below != 0) && count ($paragraph_positions) > $position) {
if ($debug_processing && $this->number != 0) {
ai_log ('---------------------------------');
ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1));
}
$checks = $max_checks;
$saved_position = $position;
do {
$found_above = false;
$paragraph_text_found_above = '';
if ($avoid_paragraphs_above != 0 && $avoid_text_above != "" && is_array ($avoid_paragraph_texts_above) && count ($avoid_paragraph_texts_above) != 0) {
for ($avoid_paragraph_above = $avoid_paragraphs_above; $avoid_paragraph_above > 0; $avoid_paragraph_above --) {
$paragraph_position_above = $position - $avoid_paragraph_above + 1;
if (!$check_strictly_inside_paragraphs) {
if ($paragraph_position_above < 0) $paragraph_position_above = 0;
}
if ($paragraph_position_above >= 0) {
if ($check_strictly_inside_paragraphs) {
$start_position_above = $paragraph_start_positions [$paragraph_position_above];
$end_position_above = $paragraph_positions [$paragraph_position_above];
} else {
$start_position_above = $paragraph_start_positions [$paragraph_position_above];
$end_position_above = $paragraph_positions [$position];
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $start_position_above, $end_position_above - $start_position_above);
} else {
$paragraph_code = substr ($content, $start_position_above, $end_position_above - $start_position_above);
}
foreach ($avoid_paragraph_texts_above as $paragraph_text_above) {
if (trim ($paragraph_text_above) == '') continue;
if ($multibyte) {
if (mb_stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
$found_above = true;
$paragraph_text_found_above = $paragraph_text_above;
break;
}
} else {
if (stripos ($paragraph_code, trim ($paragraph_text_above)) !== false) {
$found_above = true;
$paragraph_text_found_above = $paragraph_text_above;
break;
}
}
}
}
if ($found_above || !$check_strictly_inside_paragraphs) break;
}
}
$found_below = false;
$paragraph_text_found_below = '';
if ($avoid_paragraphs_below != 0 && $position != count ($paragraph_positions) - 1 && $avoid_text_below != "" && is_array ($avoid_paragraph_texts_below) && count ($avoid_paragraph_texts_below) != 0) {
for ($avoid_paragraph_below = $avoid_paragraphs_below; $avoid_paragraph_below > 0; $avoid_paragraph_below --) {
$paragraph_position_below = $position + $avoid_paragraph_below;
if (!$check_strictly_inside_paragraphs) {
if ($paragraph_position_below >= count ($paragraph_positions)) {
// If paragraph position is not the last one end with the last paragraph
if ($position != count ($paragraph_positions) - 1) $paragraph_position_below = count ($paragraph_positions) - 1;
// Othewise do not check anything
}
}
if ($paragraph_position_below < count ($paragraph_positions)) {
if ($check_strictly_inside_paragraphs) {
$start_position_below = $paragraph_start_positions [$paragraph_position_below];
$end_position_below = $paragraph_positions [$paragraph_position_below];
} else {
$start_position_below = $paragraph_positions [$position];
$end_position_below = $paragraph_positions [$paragraph_position_below];
}
if ($multibyte) {
$paragraph_code = mb_substr ($content, $start_position_below, $end_position_below - $start_position_below);
} else {
$paragraph_code = substr ($content, $start_position_below, $end_position_below - $start_position_below);
}
foreach ($avoid_paragraph_texts_below as $paragraph_text_below) {
if (trim ($paragraph_text_below) == '') continue;
if ($multibyte) {
if (mb_stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
$found_below = true;
$paragraph_text_found_below = $paragraph_text_below;
break;
}
} else {
if (stripos ($paragraph_code, trim ($paragraph_text_below)) !== false) {
$found_below = true;
$paragraph_text_found_below = $paragraph_text_below;
break;
}
}
}
}
if ($found_below || !$check_strictly_inside_paragraphs) break;
}
}
// echo "position: $position = after #", $position + 1, "
\n";
// echo "checks: $checks
\n";
// echo "direction: $direction
\n";
// if ($found_above)
// echo "found_above
\n";
// if ($found_below)
// echo "found_below
\n";
// echo "=================
\n";
if ($found_above || $found_below) {
if ($debug_processing && $this->number != 0) ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1) .
' FAILED (' . ($found_above ? 'ABOVE: ' . $paragraph_text_found_above .($found_below ? ', ' : '') : '') . ($found_below ? 'BELOW: ' . $paragraph_text_found_below : '') . ')');
$ai_last_check = AI_CHECK_DO_NOT_INSERT;
if ($this->get_avoid_action() == AI_DO_NOT_INSERT) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break;
}
switch ($direction) {
case AI_ABOVE: // Try above
$ai_last_check = AI_CHECK_AD_ABOVE;
// Already at the top - do not insert
if ($position == 0) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position --;
break;
case AI_BELOW: // Try below
$ai_last_check = AI_CHECK_AD_BELOW;
// Already at the bottom - do not insert
if ($position >= count ($paragraph_positions) - 1) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position ++;
break;
case AI_ABOVE_AND_THEN_BELOW: // Try first above and then below
if ($position == 0 || $checks == 0) {
// Try below
$direction = AI_BELOW;
$checks = $max_checks;
$position = $saved_position;
$ai_last_check = AI_CHECK_AD_BELOW;
// Already at the bottom - do not insert
if ($position >= count ($paragraph_positions) - 1) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position ++;
} else $position --;
break;
case AI_BELOW_AND_THEN_ABOVE: // Try first below and then above
if ($position >= count ($paragraph_positions) - 1 || $checks == 0) {
// Try above
$direction = AI_ABOVE;
$checks = $max_checks;
$position = $saved_position;
$ai_last_check = AI_CHECK_AD_ABOVE;
// Already at the top - do not insert
if ($position == 0) {
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break 2;
}
$position --;
} else $position ++;
break;
}
} else {
if ($debug_processing && $this->number != 0) ai_log ('BLOCK ' . $this->number . ' CLEARANCE CHECK POSITION ' . ($position + 1) . ' OK');
// Text not found - insert
$positions [$position_index] = $position;
break;
}
// Try next position
if ($checks <= 0) {
// Suitable position not found - do not insert
$failed_clearance_positions [$positions [$position_index]] = $ai_last_check;
$positions [$position_index] = - 1;
break;
}
$checks --;
} while (true);
}
// Nothing to do
$ai_last_check = AI_CHECK_PARAGRAPHS_AFTER_CLEARANCE;
if (count ($paragraph_positions) == 0) return $content;
}
if ($debug_processing && $this->number != 0) ai_log ('---------------------------------');
}
if (!$after_image) {
$no_insertion_first_paragraphs = intval ($this->get_skip_first_paragraphs ());
$no_insertion_last_paragraphs = intval ($this->get_skip_last_paragraphs ());
if ($no_insertion_first_paragraphs != 0 /*&& $no_insertion_first_paragraphs <= count ($paragraph_positions)*/) {
foreach ($positions as $index => $position) {
if ($position < $no_insertion_first_paragraphs) unset ($positions [$index]);
}
$positions = array_values ($positions);
}
if ($no_insertion_last_paragraphs != 0 /*&& $no_insertion_last_paragraphs <= count ($paragraph_positions)*/) {
foreach ($positions as $index => $position) {
if ($position >= count ($paragraph_positions) - $no_insertion_last_paragraphs) unset ($positions [$index]);
}
$positions = array_values ($positions);
}
}
if ($position_preview || !empty ($positions)) {
$offset = 0;
if (!empty ($positions)) $ai_last_check = AI_CHECK_PARAGRAPH_NUMBER;
$real_positions = array ();
foreach ($positions as $position_index) $real_positions []= $position_index >= 0 ? $position_index + 1 : '*';
if ($debug_processing && $this->number != 0 && count ($real_positions) != 0) ai_log ('BLOCK ' . $this->number . ' INSERTION POSITIONS: ' . implode (', ', $real_positions) . ' of ' . count ($paragraph_positions));
$min_paragraphs = intval ($this->get_paragraph_number_minimum());
$max_paragraphs = intval ($this->get_paragraph_number_maximum());
$max_page_blocks_enabled = $this->get_max_page_blocks_enabled ();
foreach ($paragraph_positions as $counter => $paragraph_position) {
$debug_label = $after_image ? 'AI' : 'AP';
if ($position_preview) $inserted_code = "[[AI_".$debug_label.($counter + 1)."]]";
elseif (!empty ($positions) && in_array ($counter, $positions)) {
$inserted = false;
$ai_last_check = AI_CHECK_PARAGRAPHS_MIN_NUMBER;
if (count ($paragraph_positions) >= $min_paragraphs) {
$ai_last_check = AI_CHECK_PARAGRAPHS_MAX_NUMBER;
if ($max_paragraphs <= 0 || count ($paragraph_positions) <= $max_paragraphs) {
$ai_last_check = AI_CHECK_MAX_PAGE_BLOCKS;
if (!$max_page_blocks_enabled || $ai_wp_data [AI_PAGE_BLOCKS] < get_max_page_blocks ()) {
// Last check before insertion
if ($this->check_and_increment_block_counter ()) {
// Increment page block counter
if ($max_page_blocks_enabled) $ai_wp_data [AI_PAGE_BLOCKS] ++;
$ai_last_check = AI_CHECK_DEBUG_NO_INSERTION;
if (!$this->get_debug_disable_insertion ()) {
$inserted_code = $this->get_code_for_serverside_insertion ();
$ai_last_check = AI_CHECK_INSERTED;
$this->clear_code_cache ();
$inserted = true;
}
}
}
}
}
if ($debug_processing) ai_log (ai_log_block_status ($this->number, $ai_last_check));
if (!$inserted) continue;
}
else {
if ($debug_processing && isset ($failed_clearance_positions [$counter])) ai_log (ai_log_block_status ($this->number, $failed_clearance_positions [$counter]));
continue;
}
if ($multibyte) {
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$content = mb_substr ($content, 0, $paragraph_position + 1) . $inserted_code . mb_substr ($content, $paragraph_position + 1);
} else {
$content = mb_substr ($content, 0, $paragraph_position + $offset + 1) . $inserted_code . mb_substr ($content, $paragraph_position + $offset + 1);
$offset += mb_strlen ($inserted_code);
}
} else {
if ($direction_type == AI_DIRECTION_FROM_BOTTOM) {
$content = substr_replace ($content, $inserted_code, $paragraph_position + 1, 0);
} else {
$content = substr_replace ($content, $inserted_code, $paragraph_position + $offset + 1, 0);
$offset += strlen ($inserted_code);
}
}
}
$ai_last_check = AI_CHECK_NONE; // Already logged on each insertion
return $content;
}
// empty $positions
$ai_last_check = AI_CHECK_NO_PARAGRAPHS;
return $content;
}
// Deprecated
function manual ($content){
if (preg_match_all("/{adinserter (.+?)}/", $content, $tags)){
$block_class_name = get_block_class_name ();
$viewport_classes = $this->get_viewport_classes ();
if ($block_class_name != '' || $viewport_classes != '') {
if ($block_class_name =='') $viewport_classes = trim ($viewport_classes);
$class = " class='" . ($block_class_name != '' ? $block_class_name . " " . $block_class_name . "-" . $this->number : '') . $viewport_classes ."'";
} else $class = '';
// $display_for_devices = $this->get_display_for_devices ();
foreach ($tags [1] as $tag) {
$ad_tag = strtolower (trim ($tag));
$ad_name = strtolower (trim ($this->get_ad_name()));
if ($ad_tag == $ad_name || $ad_tag == $this->number) {
if ($this->get_alignment_type() == AI_ALIGNMENT_NO_WRAPPING) $ad_code = $this->ai_getProcessedCode (); else
$ad_code = "
" . $this->ai_getProcessedCode () . "
";
$content = preg_replace ("/{adinserter " . $tag . "}/", $ad_code, $content);
}
}
}
return $content;
}
// Deprecated
function display_disabled ($content){
$ad_name = $this->get_ad_name();
if (preg_match ("//i", $content)) return true;
if (preg_match ("//i", $content)) return true;
if (preg_match ("//i", $content)) return true;
if (strpos ($content, "") != false) return true;
return false;
}
function check_disabled () {
global $ai_wp_data;
if (isset ($ai_wp_data [AI_DISABLED_BLOCKS])) {
if (in_array ($this->number, $ai_wp_data [AI_DISABLED_BLOCKS])) {
return false;
}
if (in_array ('#', $ai_wp_data [AI_DISABLED_BLOCKS])) {
return false;
}
}
return true;
}
function check_category ($categories = null, $cat_type = AI_WHITE_LIST) {
global $ai_wp_data;
if ($categories === null) {
$categories = trim (strtolower ($this->get_ad_block_cat()));
$cat_type = $this->get_ad_block_cat_type();
}
if ($categories == AD_EMPTY_DATA) return true;
switch ($ai_wp_data [AI_WP_PAGE_TYPE]) {
case AI_PT_STATIC:
case AI_PT_POST:
case AI_PT_FEED:
$wp_categories = get_the_category ();
break;
default:
$wp_categories = get_queried_object();
if ($wp_categories instanceof WP_Term) {
$wp_categories = array ($wp_categories);
} else $wp_categories = array ();
break;
}
if ($cat_type == AI_BLACK_LIST) {
// if ($categories == AD_EMPTY_DATA) return true;
$cats_listed = explode (",", $categories);
foreach ($wp_categories as $wp_category) {
if (isset ($wp_category->cat_name) && isset ($wp_category->slug))
foreach ($cats_listed as $cat_disabled) {
$check_parent = true;
$check_childern = false;
$cat_disabled = trim ($cat_disabled);
if (substr ($cat_disabled, - 1) == '*') {
$check_childern = true;
$cat_disabled = rtrim ($cat_disabled, '*');
}
elseif (substr ($cat_disabled, - 1) == '+') {
$check_parent = false;
$check_childern = true;
$cat_disabled = rtrim ($cat_disabled, '+');
}
$wp_category_name = strtolower ($wp_category->cat_name);
$wp_category_slug = strtolower ($wp_category->slug);
if ($check_parent) {
if ($wp_category_name == $cat_disabled || $wp_category_slug == $cat_disabled) {
return false;
}
}
if ($check_childern) {
if (ai_post_is_in_child_categories ($cat_disabled)) {
return false;
}
}
}
}
return true;
} else {
// if ($categories == AD_EMPTY_DATA) return false;
$cats_listed = explode (",", $categories);
foreach ($wp_categories as $wp_category) {
if (isset ($wp_category->cat_name) && isset ($wp_category->slug))
foreach ($cats_listed as $cat_enabled) {
$check_parent = true;
$check_childern = false;
$cat_enabled = trim ($cat_enabled);
if (substr ($cat_enabled, - 1) == '*') {
$check_childern = true;
$cat_enabled = rtrim ($cat_enabled, '*');
}
elseif (substr ($cat_enabled, - 1) == '+') {
$check_parent = false;
$check_childern = true;
$cat_enabled = rtrim ($cat_enabled, '+');
}
$wp_category_name = strtolower ($wp_category->cat_name);
$wp_category_slug = strtolower ($wp_category->slug);
if ($check_parent) {
if ($wp_category_name == $cat_enabled || $wp_category_slug == $cat_enabled) {
return true;
}
}
if ($check_childern) {
if (ai_post_is_in_child_categories ($cat_enabled)) {
return true;
}
}
}
}
return false;
}
}
function check_tag ($tags = null, $tag_type = AI_WHITE_LIST) {
if ($tags === null) {
$tags = $this->get_ad_block_tag();
$tag_type = $this->get_ad_block_tag_type();
}
$tags = trim ($tags);
if ($tags == AD_EMPTY_DATA) return true;
if ($tags == '*') {
$has_any_tag = has_tag ('', ai_get_post_id ());
if ($tag_type == AI_BLACK_LIST) {
return !$has_any_tag;
} else {
return $has_any_tag;
}
}
$tags_listed = explode (",", $tags);
foreach ($tags_listed as $index => $tag_listed) {
$tags_listed [$index] = trim ($tag_listed);
}
$has_any_of_the_given_tags = has_tag ($tags_listed, ai_get_post_id ());
if ($tag_type == AI_BLACK_LIST) {
// if ($tags == AD_EMPTY_DATA) return true;
if (is_tag()) {
foreach ($tags_listed as $tag_listed) {
if (is_tag ($tag_listed)) return false;
}
return true;
}
return !$has_any_of_the_given_tags;
} else {
// if ($tags == AD_EMPTY_DATA) return false;
if (is_tag()) {
foreach ($tags_listed as $tag_listed) {
if (is_tag ($tag_listed)) return true;
}
return false;
}
return $has_any_of_the_given_tags;
}
}
function check_taxonomy ($taxonomies = null, $taxonomy_type = AI_WHITE_LIST) {
global $ai_wp_data;
if ($taxonomies === null) {
$taxonomies = trim ($this->get_ad_block_taxonomy ());
$taxonomy_type = $this->get_ad_block_taxonomy_type();
}
if ($taxonomies == AD_EMPTY_DATA) return true;
if ($taxonomy_type == AI_BLACK_LIST) {
$taxonomies_listed = explode (",", $taxonomies);
foreach ($taxonomies_listed as $taxonomy_disabled) {
$taxonomy_disabled_org = trim ($taxonomy_disabled);
$taxonomy_disabled = strtolower ($taxonomy_disabled_org);
if (strpos ($taxonomy_disabled, 'user:') === 0) {
$current_user = wp_get_current_user();
$terms = explode (':', $taxonomy_disabled);
if ($terms [1] == 'logged-in') {
return !is_user_logged_in ();
}
elseif ($terms [1] == 'not-logged-in') {
return is_user_logged_in ();
}
if ($terms [1] == $current_user->user_login) return false;
}
elseif (strpos ($taxonomy_disabled, 'author:') === 0) {
if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC)
$current_author = strtolower (get_the_author_meta ('user_login')); else
$current_author = '';
$terms = explode (':', $taxonomy_disabled);
if ($terms [1] == $current_author) return false;
}
elseif (strpos ($taxonomy_disabled, 'user-role:') === 0) {
$current_user = wp_get_current_user();
$terms = explode (':', $taxonomy_disabled);
foreach (wp_get_current_user()->roles as $role) {
if ($terms [1] == $role) return false;
}
}
elseif (strpos ($taxonomy_disabled, 'post-type:') === 0) {
$post_type = get_post_type ();
$terms = explode (':', $taxonomy_disabled);
if ($terms [1] == $post_type) return false;
}
elseif (strpos ($taxonomy_disabled, 'primary-category:') === 0) {
$primary_category = explode (':', $taxonomy_disabled);
if ($primary_category [1] == ai_primary_category ()) return false;
}
elseif (strpos ($taxonomy_disabled, 'yoast-primary-category:') === 0) {
$primary_category = explode (':', $taxonomy_disabled);
if ($primary_category [1] == ai_yoast_primary_category ()) return false;
}
elseif (strpos ($taxonomy_disabled, 'multisite:') === 0) {
if (is_multisite () && function_exists ('ai_plugin_settings')) {
$site_id = explode (':', $taxonomy_disabled);
if ($site_id [1] == '#' && is_main_site ()) return false;
if (get_current_blog_id () == $site_id [1]) return false;
} else return true;
}
elseif (strpos ($taxonomy_disabled_org, 'meta:') === 0) {
$meta_data = explode (':', str_replace ('meta:', '', $taxonomy_disabled_org));
if (count ($meta_data) == 2) {
if (get_post_meta (get_the_id (), $meta_data [0], true) == $meta_data [1]) return false;
} else {
if (get_post_meta (get_the_id (), $meta_data [0], true) != '') return false;
}
}
elseif (strpos ($taxonomy_disabled, 'wpml-current-language:') === 0) {
if (has_filter ('wpml_current_language') !== false) {
$wpml_current_language = apply_filters ('wpml_current_language', null);
$current_language = explode (':', $taxonomy_disabled);
if ($current_language [1] == $wpml_current_language) return false;
}
}
$taxonomy_names = get_post_taxonomies ();
foreach ($taxonomy_names as $taxonomy_name) {
$terms = get_the_terms (0, $taxonomy_name);
if (is_array ($terms)) {
foreach ($terms as $term) {
$post_term_name = strtolower ($term->name);
$post_term_slug = strtolower ($term->slug);
$post_taxonomy = strtolower ($term->taxonomy);
if ($post_term_name == $taxonomy_disabled || $post_term_slug == $taxonomy_disabled) return false;
$post_taxonomy = strtolower ($term->taxonomy);
if ($post_taxonomy == $taxonomy_disabled) return false;
$check_parent = true;
$check_childern = false;
if (substr ($taxonomy_disabled, - 1) == '*') {
$check_childern = true;
$taxonomy_disabled = rtrim ($taxonomy_disabled, '*');
}
elseif (substr ($taxonomy_disabled, - 1) == '+') {
$check_parent = false;
$check_childern = true;
$taxonomy_disabled = rtrim ($taxonomy_disabled, '+');
}
if ($check_parent) {
if ($taxonomy_disabled == $post_taxonomy . ':' . $post_term_slug) {
return false;
}
}
if ($check_childern) {
if (strpos ($taxonomy_disabled, ':') !== false) {
$taxonomy_data = explode (':', $taxonomy_disabled);
if (count ($taxonomy_data) == 2 && ai_post_is_in_child_taxonomies ($taxonomy_data [0], $taxonomy_data [1])) {
return false;
}
}
}
}
}
}
}
return true;
} else {
$taxonomies_listed = explode (",", $taxonomies);
foreach ($taxonomies_listed as $taxonomy_enabled) {
$taxonomy_enabled_org = trim ($taxonomy_enabled);
$taxonomy_enabled = strtolower ($taxonomy_enabled_org);
if (strpos ($taxonomy_enabled, 'user:') === 0) {
$current_user = wp_get_current_user();
$terms = explode (':', $taxonomy_enabled);
if ($terms [1] == 'logged-in') {
return is_user_logged_in ();
}
elseif ($terms [1] == 'not-logged-in') {
return !is_user_logged_in ();
}
if ($terms [1] == $current_user->user_login) return true;
}
elseif (strpos ($taxonomy_enabled, 'author:') === 0) {
if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC)
$current_author = strtolower (get_the_author_meta ('user_login')); else
$current_author = '';
$terms = explode (':', $taxonomy_enabled);
if ($terms [1] == $current_author) return true;
}
elseif (strpos ($taxonomy_enabled, 'user-role:') === 0) {
$current_user = wp_get_current_user();
$terms = explode (':', $taxonomy_enabled);
foreach (wp_get_current_user()->roles as $role) {
if ($terms [1] == $role) return true;
}
}
elseif (strpos ($taxonomy_enabled, 'post-type:') === 0) {
$post_type = get_post_type ();
$terms = explode (':', $taxonomy_enabled);
if ($terms [1] == $post_type) return true;
}
elseif (strpos ($taxonomy_enabled, 'primary-category:') === 0) {
$primary_category = explode (':', $taxonomy_enabled);
if ($primary_category [1] == ai_primary_category ()) return true;
}
elseif (strpos ($taxonomy_enabled, 'yoast-primary-category:') === 0) {
$primary_category = explode (':', $taxonomy_enabled);
if ($primary_category [1] == ai_yoast_primary_category ()) return true;
}
elseif (strpos ($taxonomy_enabled, 'multisite:') === 0) {
if (is_multisite () && function_exists ('ai_plugin_settings')) {
$site_id = explode (':', $taxonomy_enabled);
if ($site_id [1] == '#' && is_main_site ()) return true;
if (get_current_blog_id () == $site_id [1]) return true;
} else return false;
}
elseif (strpos ($taxonomy_enabled_org, 'meta:') === 0) {
$meta_data = explode (':', str_replace ('meta:', '', $taxonomy_enabled_org));
if (count ($meta_data) == 2) {
if (get_post_meta (get_the_id (), $meta_data [0], true) == $meta_data [1]) return true;
} else {
if (get_post_meta (get_the_id (), $meta_data [0], true) != '') return true;
}
}
elseif (strpos ($taxonomy_enabled, 'wpml-current-language:') === 0) {
if (has_filter ('wpml_current_language') !== false) {
$wpml_current_language = apply_filters ('wpml_current_language', null);
$current_language = explode (':', $taxonomy_enabled);
if ($current_language [1] == $wpml_current_language) return true;
}
}
$taxonomy_names = get_post_taxonomies ();
foreach ($taxonomy_names as $taxonomy_name) {
$terms = get_the_terms (0, $taxonomy_name);
if (is_array ($terms)) {
foreach ($terms as $term) {
$post_term_name = strtolower ($term->name);
$post_term_slug = strtolower ($term->slug);
$post_taxonomy = strtolower ($term->taxonomy);
if ($post_term_name == $taxonomy_enabled || $post_term_slug == $taxonomy_enabled) return true;
$post_taxonomy = strtolower ($term->taxonomy);
if ($post_taxonomy == $taxonomy_enabled) return true;
$check_parent = true;
$check_childern = false;
if (substr ($taxonomy_enabled, - 1) == '*') {
$check_childern = true;
$taxonomy_enabled = rtrim ($taxonomy_enabled, '*');
}
elseif (substr ($taxonomy_enabled, - 1) == '+') {
$check_parent = false;
$check_childern = true;
$taxonomy_enabled = rtrim ($taxonomy_enabled, '+');
}
if ($check_parent) {
if ($taxonomy_enabled == $post_taxonomy . ':' . $post_term_slug) {
return true;
}
}
if ($check_childern) {
if (strpos ($taxonomy_enabled, ':') !== false) {
$taxonomy_data = explode (':', $taxonomy_enabled);
if (count ($taxonomy_data) == 2 && ai_post_is_in_child_taxonomies ($taxonomy_data [0], $taxonomy_data [1])) {
return true;
}
}
}
}
}
}
}
return false;
}
}
function check_id ($ids = null, $id_type = AI_WHITE_LIST) {
global $ai_wp_data;
// Check post IDs only on posts and static pages
if ($ai_wp_data [AI_WP_PAGE_TYPE] != AI_PT_POST && $ai_wp_data [AI_WP_PAGE_TYPE] != AI_PT_STATIC) return true;
if ($ids === null) {
$ids = trim ($this->get_id_list());
$id_type = $this->get_id_list_type();
}
if ($ids == AD_EMPTY_DATA) return true;
// $page_id = get_the_ID();
$page_id = ai_get_post_id ();
if (($ai_wp_data [AI_WP_DEBUGGING] & AI_DEBUG_PROCESSING) != 0) {
$queried_object_id = '';
$queried_object = get_queried_object ();
if ($queried_object) {
$queried_object_id = $queried_object->ID;
}
ai_log ("ID: " . $page_id . " (get_the_ID: " . get_the_ID () . ", get_queried_object: " . $queried_object_id . (defined ('AI_USE_GET_QUERIED_OBJECT') && AI_USE_GET_QUERIED_OBJECT ? ', AI_USE_GET_QUERIED_OBJECT SET' : "") . ")");
}
if ($id_type == AI_BLACK_LIST) $return = false; else $return = true;
if (/*$ids == AD_EMPTY_DATA ||*/ $page_id === false) {
return !$return;
}
$ids_listed = explode (",", $ids);
foreach ($ids_listed as $index => $id_listed) {
if (trim ($id_listed) == "") unset ($ids_listed [$index]); else
$ids_listed [$index] = trim ($id_listed);
}
foreach ($ids_listed as $index => $id_listed) {
switch ($ai_wp_data [AI_WP_PAGE_TYPE]) {
case AI_PT_POST:
if ($ids_listed [$index] == 'posts') return $return;
break;
case AI_PT_STATIC:
if ($ids_listed [$index] == 'pages') return $return;
break;
}
if (strpos ($ids_listed [$index], '-') !== false) {
$id_limits = explode ('-', str_replace (' ', '', $ids_listed [$index]));
if ($page_id >= $id_limits [0] && $page_id <= $id_limits [1]) return $return;
}
}
if (in_array ($page_id, $ids_listed)) return $return;
return !$return;
}
function check_url ($urls = null, $url_type = AI_WHITE_LIST) {
global $ai_wp_data;
if ($urls === null) {
$urls = trim ($this->get_ad_url_list());
$url_type = $this->get_ad_url_list_type();
}
$page_url = $ai_wp_data [AI_WP_URL];
if ($url_type == AI_BLACK_LIST) $return = false; else $return = true;
if ($urls == AD_EMPTY_DATA) return true;
$list_separator = ',';
if (strpos ($urls, ' ') !== false && strpos ($urls, ',') === false) $list_separator = ' ';
$urls_listed = explode ($list_separator, $urls);
foreach ($urls_listed as $index => $url_listed) {
if (trim ($url_listed) == "") unset ($urls_listed [$index]); else
$urls_listed [$index] = trim ($url_listed);
}
// print_r ($urls_listed);
// echo "
\n";
// echo ' page url: ' . $page_url, "
\n";
// echo ' listed urls: ' . $urls, "\n";
// echo "
\n";
foreach ($urls_listed as $url_listed) {
if ($url_listed == '*') return $return;
if ($url_listed [0] == '*') {
if ($url_listed [strlen ($url_listed) - 1] == '*') {
$url_listed = substr ($url_listed, 1, strlen ($url_listed) - 2);
if (strpos ($page_url, $url_listed) !== false) return $return;
} else {
$url_listed = substr ($url_listed, 1);
if (substr ($page_url, - strlen ($url_listed)) == $url_listed) return $return;
}
}
elseif ($url_listed [strlen ($url_listed) - 1] == '*') {
$url_listed = substr ($url_listed, 0, strlen ($url_listed) - 1);
if (strpos ($page_url, $url_listed) === 0) return $return;
}
elseif ($url_listed == $page_url) return $return;
}
return !$return;
}
function check_scheduling ($server_side_check) {
global $block_object, $ai_wp_data;
switch ($this->get_scheduling()) {
case AI_SCHEDULING_OFF:
return true;
break;
case AI_SCHEDULING_DELAY_FOR:
$after_days = trim ($this->get_ad_after_day());
if ($after_days == '') return true;
$after_days = $after_days;
$post_date = get_the_date ('U');
if ($post_date === false) return true;
return (date ('U', current_time ('timestamp')) >= $post_date + $after_days * 86400);
break;
case AI_SCHEDULING_INSERT_ONLY_FOR:
$after_days = trim ($this->get_ad_after_day());
if ($after_days == '') return false;
$after_days = $after_days;
$post_date = get_the_date ('U');
if ($post_date === false) return false;
return (date ('U', current_time ('timestamp')) < $post_date + $after_days * 86400);
break;
case AI_SCHEDULING_BETWEEN_DATES:
if (!function_exists ('ai_scheduling_options')) return true;
if (!$server_side_check) return true;
$start_time = $this->get_schedule_start_date () . ' ' . $this->get_schedule_start_time ();
$end_time = $this->get_schedule_end_date () . ' ' . $this->get_schedule_end_time ();
$days_in_week = $this->get_schedule_weekdays ();
$insertion_enabled = check_scheduling_time ($start_time, $end_time, $days_in_week, true);
if (!$insertion_enabled) {
if (!isset ($ai_wp_data [AI_FALLBACK_LEVEL])) $ai_wp_data [AI_FALLBACK_LEVEL] = 1; else $ai_wp_data [AI_FALLBACK_LEVEL] ++;
$fallback = intval ($this->get_scheduling_fallback());
if ($fallback != $this->number && $fallback != 0 && $fallback <= 96 && $ai_wp_data [AI_FALLBACK_LEVEL] <= 2) {
$fallback_obj = $block_object [$fallback];
if ($fallback_obj->check_scheduling ($server_side_check) && ai_check_impression_and_click_limits ($fallback, true)) {
$this->fallback = $fallback_obj->fallback != 0 ? $fallback_obj->fallback : $fallback;
$insertion_enabled = true;
}
}
$ai_wp_data [AI_FALLBACK_LEVEL] --;
}
return ($insertion_enabled);
break;
case AI_SCHEDULING_OUTSIDE_DATES:
if (!function_exists ('ai_scheduling_options')) return true;
if (!$server_side_check) return true;
$start_time = $this->get_schedule_start_date () . ' ' . $this->get_schedule_start_time ();
$end_time = $this->get_schedule_end_date () . ' ' . $this->get_schedule_end_time ();
$days_in_week = $this->get_schedule_weekdays ();
$insertion_enabled = check_scheduling_time ($start_time, $end_time, $days_in_week, false);
if (!$insertion_enabled) {
if (!isset ($ai_wp_data [AI_FALLBACK_LEVEL])) $ai_wp_data [AI_FALLBACK_LEVEL] = 1; else $ai_wp_data [AI_FALLBACK_LEVEL] ++;
$fallback = intval ($this->get_scheduling_fallback());
if ($fallback != $this->number && $fallback != 0 && $fallback <= 96 && $ai_wp_data [AI_FALLBACK_LEVEL] <= 2) {
$fallback_obj = $block_object [$fallback];
if ($fallback_obj->check_scheduling ($server_side_check) && ai_check_impression_and_click_limits ($fallback, true)) {
$this->fallback = $fallback_obj->fallback != 0 ? $fallback_obj->fallback : $fallback;
$insertion_enabled = true;
}
}
$ai_wp_data [AI_FALLBACK_LEVEL] --;
}
return ($insertion_enabled);
break;
case AI_SCHEDULING_PUBLISHED_BETWEEN_DATES:
if (!function_exists ('ai_scheduling_options')) return true;
$post_date = get_the_date ('U');
if ($post_date === false) return false;
$post_weekday = date ('w', $post_date);
if ($post_weekday == 0) $post_weekday = 6; else $post_weekday --;
$current_time = current_time ('timestamp');
$current_weekday = date ('w', $current_time);
if ($current_weekday == 0) $current_weekday = 6; else $current_weekday --;
$start_date = strtotime ($this->get_schedule_start_date () . ' ' . $this->get_schedule_start_time (), $current_time);
$end_date = strtotime ($this->get_schedule_end_date () . ' ' . $this->get_schedule_end_time (), $current_time);
$weekdays = explode (',', $this->get_schedule_weekdays ());
if (isset ($weekdays [0]) and $weekdays [0] === '') $weekdays = array ();
$insertion_enabled = $post_date >= $start_date && $post_date < $end_date && in_array ($post_weekday, $weekdays);
if (!$insertion_enabled) {
if (!isset ($ai_wp_data [AI_FALLBACK_LEVEL])) $ai_wp_data [AI_FALLBACK_LEVEL] = 1; else $ai_wp_data [AI_FALLBACK_LEVEL] ++;
$fallback = intval ($this->get_scheduling_fallback());
if ($fallback != $this->number && $fallback != 0 && $fallback <= 96 && $ai_wp_data [AI_FALLBACK_LEVEL] <= 2) {
$this->fallback = $fallback;
return true;
}
$ai_wp_data [AI_FALLBACK_LEVEL] --;
}
return ($insertion_enabled);
break;
case AI_SCHEDULING_PUBLISHED_OUTSIDE_DATES:
if (!function_exists ('ai_scheduling_options')) return true;
$post_date = get_the_date ('U');
if ($post_date === false) return true;
$post_weekday = date ('w', $post_date);
if ($post_weekday == 0) $post_weekday = 6; else $post_weekday --;
$current_time = current_time ('timestamp');
$current_weekday = date ('w', $current_time);
if ($current_weekday == 0) $current_weekday = 6; else $current_weekday --;
$start_date = strtotime ($this->get_schedule_start_date () . ' ' . $this->get_schedule_start_time (), $current_time);
$end_date = strtotime ($this->get_schedule_end_date () . ' ' . $this->get_schedule_end_time (), $current_time);
$weekdays = explode (',', $this->get_schedule_weekdays ());
if (isset ($weekdays [0]) and $weekdays [0] === '') $weekdays = array ();
// echo "
";
// echo "current_time ", $current_time, " ", date ("Y-m-d H:i:s", $current_time), "
";
// echo "start_date ", $start_date, " ", date ("Y-m-d H:i:s", $start_date), "
";
// echo "end_date ", $end_date, " ", date ("Y-m-d H:i:s", $end_date), "
";
// echo "post_date ", $post_date, " ", date ("Y-m-d H:i:s", $post_date), "
";
// echo "";
$insertion_enabled = $post_date < $start_date || $post_date >= $end_date || !in_array ($post_weekday, $weekdays);
if (!$insertion_enabled) {
if (!isset ($ai_wp_data [AI_FALLBACK_LEVEL])) $ai_wp_data [AI_FALLBACK_LEVEL] = 1; else $ai_wp_data [AI_FALLBACK_LEVEL] ++;
$fallback = intval ($this->get_scheduling_fallback());
if ($fallback != $this->number && $fallback != 0 && $fallback <= 96 && $ai_wp_data [AI_FALLBACK_LEVEL] <= 2) {
$this->fallback = $fallback;
return true;
}
$ai_wp_data [AI_FALLBACK_LEVEL] --;
}
return ($insertion_enabled);
break;
default:
return true;
break;
}
}
function check_referer () {
return check_referer_list ($this->get_ad_domain_list(), $this->get_ad_domain_list_type() == AI_WHITE_LIST);
}
function check_client () {
return check_client_list ($this->get_client_list(), $this->get_client_list_type() == AI_WHITE_LIST);
}
function check_number_of_words (&$content = null, $number_of_words = 0) {
global $ai_last_check, $ai_wp_data;
$minimum_words = intval ($this->get_minimum_words());
$maximum_words = intval ($this->get_maximum_words());
if ($minimum_words == 0 && $maximum_words == 0) return true;
// if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC) {
if ($number_of_words == 0) {
if (!isset ($ai_wp_data [AI_WORD_COUNT])) {
if ($content === null) {
$content = '';
$content_post = get_post ();
if (isset ($content_post->post_content)) $content = $content_post->post_content;
}
$number_of_words = number_of_words ($content);
} else $number_of_words = $ai_wp_data [AI_WORD_COUNT];
}
// } else $number_of_words = 0;
// Cache word count only on single pages
if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST || $ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC)
$ai_wp_data [AI_WORD_COUNT] = $number_of_words;
$ai_last_check = AI_CHECK_MIN_NUMBER_OF_WORDS;
if ($number_of_words < $minimum_words) return false;
if ($maximum_words <= 0) $maximum_words = 1000000;
$ai_last_check = AI_CHECK_MAX_NUMBER_OF_WORDS;
if ($number_of_words > $maximum_words) return false;
return true;
}
function check_number_of_words_in_paragraph ($content, $min, $max) {
$number_of_words = number_of_words ($content);
if ($max <= 0) $max = 1000000;
if ($number_of_words < $min || $number_of_words > $max) return false;
return true;
}
function server_side_check () {
global $ai_wp_data;
return get_dynamic_blocks () == AI_DYNAMIC_BLOCKS_SERVER_SIDE || ((get_dynamic_blocks () == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_SHOW || get_dynamic_blocks () == AI_DYNAMIC_BLOCKS_CLIENT_SIDE_INSERT) && $ai_wp_data [AI_WP_AMP_PAGE]);
}
function check_page_types_lists_users ($ignore_page_types = false, $ignore_categories = false, $ignore_tags = false, $ignore_taxnomies = false, $ignore_post_ids = false) {
global $ai_last_check, $ai_wp_data;
if (!$ignore_page_types) {
if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_HOMEPAGE){
$ai_last_check = AI_CHECK_PAGE_TYPE_FRONT_PAGE;
if (!$this->get_display_settings_home()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC){
$ai_last_check = AI_CHECK_PAGE_TYPE_STATIC_PAGE;
if (!$this->get_display_settings_page()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST){
$ai_last_check = AI_CHECK_PAGE_TYPE_POST;
if (!$this->get_display_settings_post()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_CATEGORY){
$ai_last_check = AI_CHECK_PAGE_TYPE_CATEGORY;
if (!$this->get_display_settings_category()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_SEARCH){
$ai_last_check = AI_CHECK_PAGE_TYPE_SEARCH;
if (!$this->get_display_settings_search()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_ARCHIVE){
$ai_last_check = AI_CHECK_PAGE_TYPE_ARCHIVE;
if (!$this->get_display_settings_archive()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_FEED){
$ai_last_check = AI_CHECK_PAGE_TYPE_FEED;
if (!$this->get_enable_feed()) return false;
}
elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_404){
$ai_last_check = AI_CHECK_PAGE_TYPE_404;
if (!$this->get_enable_404()) return false;
}
}
if (!$ignore_categories) {
$ai_last_check = AI_CHECK_CATEGORY;
if (!$this->check_category ()) return false;
}
if (!$ignore_tags) {
$ai_last_check = AI_CHECK_TAG;
if (!$this->check_tag ()) return false;
}
if (!$ignore_taxnomies) {
$ai_last_check = AI_CHECK_TAXONOMY;
if (!$this->check_taxonomy ()) return false;
}
if (!$ignore_post_ids) {
$ai_last_check = AI_CHECK_ID;
if (!$this->check_id ()) return false;
}
$ai_last_check = AI_CHECK_URL;
if (!$this->check_url ()) return false;
$server_side_check = $this->server_side_check ();
$ai_last_check = AI_CHECK_URL_PARAMETER;
switch ($server_side_check) {
case true:
if (!check_url_parameter_and_cookie_list ($this->get_url_parameter_list(), $this->get_url_parameter_list_type() == AI_WHITE_LIST)) return false;
break;
default:
$url_parameter_found = false;
$url_parameter_list_pass = check_url_parameter_list ($this->get_url_parameter_list(), $this->get_url_parameter_list_type() == AI_WHITE_LIST, $url_parameter_found);
if ($url_parameter_found && !$url_parameter_list_pass) return false;
if (!$url_parameter_found) $this->client_side_cookie_check_url = true;
break;
}
$ai_last_check = AI_CHECK_COOKIE;
switch ($server_side_check) {
case true:
if (!check_cookie_list ($this->get_cookie_list(), $this->get_cookie_list_type() == AI_WHITE_LIST)) return false;
break;
default:
$this->client_side_cookie_check = true;
break;
}
if ($server_side_check) {
$ai_last_check = AI_CHECK_REFERER;
if (!$this->check_referer ()) return false;
}
if ($server_side_check) {
$ai_last_check = AI_CHECK_CLIENT;
if (!$this->check_client ()) return false;
}
if (function_exists ('ai_check_lists')) {
if (!ai_check_lists ($this, $server_side_check)) return false;
}
$ai_last_check = AI_CHECK_SCHEDULING;
if (!$this->check_scheduling ($server_side_check)) return false;
$display_for_users = $this->get_display_for_users ();
$ai_last_check = AI_CHECK_LOGGED_IN_USER;
if ($display_for_users == AI_DISPLAY_LOGGED_IN_USERS && ($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) != AI_USER_LOGGED_IN) return false;
$ai_last_check = AI_CHECK_NOT_LOGGED_IN_USER;
if ($display_for_users == AI_DISPLAY_NOT_LOGGED_IN_USERS && ($ai_wp_data [AI_WP_USER] & AI_USER_LOGGED_IN) == AI_USER_LOGGED_IN) return false;
$ai_last_check = AI_CHECK_ADMINISTRATOR;
if ($display_for_users == AI_DISPLAY_ADMINISTRATORS && ($ai_wp_data [AI_WP_USER] & AI_USER_ADMINISTRATOR) != AI_USER_ADMINISTRATOR) return false;
if (function_exists ('ai_check_impression_and_click_limits')) {
if ($server_side_check && !ai_check_impression_and_click_limits ($this->number)) return false;
}
return true;
}
function check_post_page_exceptions ($selected_blocks) {
global $ai_last_check, $ai_wp_data;
if (isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_POSTS]) || isset ($this->wp_options [AI_OPTION_ENABLED_ON_WHICH_PAGES])) {
// Use old settings
if ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_POST) {
$enabled_on = $this->get_ad_enabled_on_which_posts ();
if ($enabled_on == AI_DEFAULT_INSERTION_ENABLED) {
$ai_last_check = AI_CHECK_INDIVIDUALLY_DISABLED;
if (in_array ($this->number, $selected_blocks)) return false;
}
elseif ($enabled_on == AI_DEFAULT_INSERTION_DISABLED) {
$ai_last_check = AI_CHECK_INDIVIDUALLY_ENABLED;
if (!in_array ($this->number, $selected_blocks)) return false;
}
} elseif ($ai_wp_data [AI_WP_PAGE_TYPE] == AI_PT_STATIC) {
$enabled_on = $this->get_ad_enabled_on_which_pages ();
if ($enabled_on == AI_DEFAULT_INSERTION_ENABLED) {
$ai_last_check = AI_CHECK_INDIVIDUALLY_DISABLED;
if (in_array ($this->number, $selected_blocks)) return false;
}
elseif ($enabled_on == AI_DEFAULT_INSERTION_DISABLED) {
$ai_last_check = AI_CHECK_INDIVIDUALLY_ENABLED;
if (!in_array ($this->number, $selected_blocks)) return false;
}
}
return true;
}
if (!$this->get_exceptions_enabled ()) return true;
switch ($this->get_exceptions_function ()) {
case AI_DEFAULT_INSERTION_ENABLED:
$ai_last_check = AI_CHECK_INDIVIDUALLY_DISABLED;
if (in_array ($this->number, $selected_blocks)) return false;
break;
case AI_DEFAULT_INSERTION_DISABLED:
$ai_last_check = AI_CHECK_INDIVIDUALLY_ENABLED;
if (!in_array ($this->number, $selected_blocks)) return false;
break;
}
return true;
}
function check_filter ($counter_for_filter) {
global $ai_last_check, $ad_inserter_globals, $page;
$filter_ok = $this->get_inverted_filter() ? false : true;
$ai_last_check = AI_CHECK_FILTER;
$filter_settings = trim (str_replace (' ', '', $this->get_call_filter()));
if (empty ($filter_settings)) return $filter_ok;
switch ($this->get_filter_type ()) {
case AI_FILTER_PHP_FUNCTION_CALLS:
if (isset ($ad_inserter_globals [AI_PHP_FUNCTION_CALL_COUNTER_NAME . $this->number]))
$counter_for_filter = $ad_inserter_globals [AI_PHP_FUNCTION_CALL_COUNTER_NAME . $this->number]; else return !$filter_ok;
break;
case AI_FILTER_CONTENT_PROCESSING:
if (isset ($ad_inserter_globals [AI_CONTENT_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_CONTENT_COUNTER_NAME]; else return !$filter_ok;
break;
case AI_FILTER_EXCERPT_PROCESSING:
if (isset ($ad_inserter_globals [AI_EXCERPT_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_EXCERPT_COUNTER_NAME]; else return !$filter_ok;
break;
case AI_FILTER_BEFORE_POST_PROCESSING:
if (isset ($ad_inserter_globals [AI_LOOP_BEFORE_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_LOOP_BEFORE_COUNTER_NAME]; else return !$filter_ok;
break;
case AI_FILTER_AFTER_POST_PROCESSING:
if (isset ($ad_inserter_globals [AI_LOOP_AFTER_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_LOOP_AFTER_COUNTER_NAME]; else return !$filter_ok;
break;
case AI_FILTER_WIDGET_DRAWING:
if (isset ($ad_inserter_globals [AI_WIDGET_COUNTER_NAME . $this->number]))
$counter_for_filter = $ad_inserter_globals [AI_WIDGET_COUNTER_NAME . $this->number]; else return !$filter_ok;
break;
case AI_FILTER_SUBPAGES:
if (isset ($page))
$counter_for_filter = $page; else return !$filter_ok;
break;
case AI_FILTER_POSTS:
if (isset ($ad_inserter_globals [AI_POST_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_POST_COUNTER_NAME]; else return !$filter_ok;
break;
case AI_FILTER_PARAGRAPHS:
case AI_FILTER_IMAGES:
return true;
break;
case AI_FILTER_COMMENTS:
if (isset ($ad_inserter_globals [AI_COMMENT_COUNTER_NAME]))
$counter_for_filter = $ad_inserter_globals [AI_COMMENT_COUNTER_NAME]; else return !$filter_ok;
break;
}
$filter_values = array ();
if (strpos ($filter_settings, ",") !== false) {
$filter_values = explode (",", $filter_settings);
} else $filter_values []= $filter_settings;
foreach ($filter_values as $filter_value) {
$filter_value = trim ($filter_value);
if (isset ($filter_value [0]) && $filter_value [0] == '%') {
$mod_value = substr ($filter_value, 1);
$offset_value = 0;
if (strpos ($mod_value, '@') !== false) {
$mod_value_array = explode ('@', $mod_value);
$mod_value = $mod_value_array [0];
$offset_value = $mod_value_array [1];
}
// if (is_numeric ($mod_value) && $mod_value > 0) {
if (is_numeric ($mod_value) && $mod_value > 0 && is_numeric ($offset_value)) {
// if ($counter_for_filter % $mod_value == 0) return $filter_ok;
if ($counter_for_filter >= $offset_value && ($counter_for_filter - $offset_value) % $mod_value == 0) return $filter_ok;
}
}
}
return in_array ($counter_for_filter, $filter_values) xor !$filter_ok;
}
function check_and_increment_block_counter () {
global $ad_inserter_globals, $ai_last_check;
$global_name = AI_BLOCK_COUNTER_NAME . $this->number;
$max_insertions = intval ($this->get_maximum_insertions ());
if (!isset ($ad_inserter_globals [$global_name])) {
$ad_inserter_globals [$global_name] = 0;
}
$ai_last_check = AI_CHECK_MAX_INSERTIONS;
if ($max_insertions != 0 && $ad_inserter_globals [$global_name] >= $max_insertions) return false;
$ad_inserter_globals [$global_name] ++;
return true;
}
function check_block_counter () {
global $ad_inserter_globals, $ai_last_check;
$global_name = AI_BLOCK_COUNTER_NAME . $this->number;
$max_insertions = intval ($this->get_maximum_insertions ());
if (!isset ($ad_inserter_globals [$global_name])) {
$ad_inserter_globals [$global_name] = 0;
}
$ai_last_check = AI_CHECK_MAX_INSERTIONS;
if ($max_insertions != 0 && $ad_inserter_globals [$global_name] >= $max_insertions) return false;
return true;
}
function increment_block_counter () {
global $ad_inserter_globals;
if ($this->number == 0) return;
$global_name = AI_BLOCK_COUNTER_NAME . $this->number;
if (!isset ($ad_inserter_globals [$global_name])) {
$ad_inserter_globals [$global_name] = 0;
}
$ad_inserter_globals [$global_name] ++;
return;
}
function extract_features (){
global $ai_wp_data;
// AI_SERVER_SIDE_DETECTION
// AI_CLIENT_SIDE_DETECTION
// AI_CLIENT_SIDE_INSERTION
// AI_STICK_TO_THE_CONTENT
// AI_TRACKING
// AI_CLOSE_BUTTONS
// AI_IFRAMES
// AI_ANIMATION
// AI_LAZY_LOADING
// AI_GEOLOCATION
// if ($this->get_detection_server_side()) echo "#", $this->number;
// echo "#", $this->number;
// return;
if ($this->get_detection_server_side()) $ai_wp_data [AI_SERVER_SIDE_DETECTION] = true;
if (trim ($this->get_client_list ()) != '') $ai_wp_data [AI_MOBILE_DETECT_JS] = true;
if ($this->get_detection_client_side ()) {
$ai_wp_data [AI_CLIENT_SIDE_DETECTION] = true;
if ($this->get_client_side_action () == AI_CLIENT_SIDE_ACTION_INSERT) {
$ai_wp_data [AI_CLIENT_SIDE_INSERTION] = true;
}
} else {
$code = $this->get_ad_data();
if (stripos ($code, '[adinserter') !== false) {
if (stripos ($code, 'viewport=') !== false) {
$ai_wp_data [AI_CLIENT_SIDE_DETECTION] = true;
$ai_wp_data [AI_CLIENT_SIDE_INSERTION] = true;
}
if (stripos ($code, 'fallback=') !== false) {
$ai_wp_data [AI_CLIENT_SIDE_INSERTION] = true;
}
}
}
if (function_exists ('ai_extract_features_2')) ai_extract_features_2 ($this);
if ($this->stick_to_the_content_class () != '' || $this->get_background ()) $ai_wp_data [AI_STICK_TO_THE_CONTENT] = true;
switch ($this->get_automatic_insertion()) {
case AI_AUTOMATIC_INSERTION_BEFORE_HTML_ELEMENT:
case AI_AUTOMATIC_INSERTION_INSIDE_HTML_ELEMENT:
case AI_AUTOMATIC_INSERTION_AFTER_HTML_ELEMENT:
if ($this->get_html_element_insertion () != AI_HTML_INSERTION_SEREVR_SIDE) $ai_wp_data [AI_CLIENT_SIDE_INSERTION] = true;
break;
}
}
}
class ai_Block extends ai_CodeBlock {
public function __construct ($number) {
parent::__construct();
$this->number = $number;
}
}
class ai_AdH extends ai_BaseCodeBlock {
public function __construct () {
parent::__construct();
$this->number = 'H';
$this->wp_options [AI_OPTION_BLOCK_NAME] = 'HEADER';
}
}
class ai_AdF extends ai_BaseCodeBlock {
public function __construct () {
parent::__construct();
$this->number = 'F';
$this->wp_options [AI_OPTION_BLOCK_NAME] = 'FOOTER';
}
}
class ai_AdA extends ai_BaseCodeBlock {
public function __construct () {
parent::__construct();
$this->number = 'A';
$this->wp_options [AI_OPTION_BLOCK_NAME] = 'AD BLOCKING MESSAGE';
}
public function get_ad_data (){
$option = isset ($this->wp_options [AI_OPTION_CODE]) ? $this->wp_options [AI_OPTION_CODE] : AI_DEFAULT_ADB_MESSAGE;
if ($option == AD_EMPTY_DATA) $option = AI_DEFAULT_ADB_MESSAGE;
return $option;
}
}
class ai_Walker_Comment extends Walker_Comment {
public function comment_callback ($comment, $args, $depth) {
if (($comment->comment_type == 'pingback' || $comment->comment_type == 'trackback') && $args ['short_ping']) {
$this->ping ($comment, $depth, $args);
} elseif ($args['format'] === 'html5') {
$this->html5_comment ($comment, $depth, $args);
} else {
$this->comment ($comment, $depth, $args);
}
}
}
class ai_code_generator {
public function __construct () {
}
public function generate ($data){
$code = '';
switch ($data ['generate-code']) {
case AI_CODE_BANNER:
$code = '';
if (isset ($data ['image']) && $data ['image'] != '') {
$attributes = '';
if (isset ($data ['loading']) && $data ['loading'] != '') {
$attributes .= ' loading="'.esc_html ($data ['loading']).'"';
}
if (isset ($data ['alt']) && $data ['alt'] != '') {
$attributes .= ' alt="'.esc_html ($data ['alt']).'"';
}
$code = '
 . ')
';
}
if (isset ($data ['link']) && $data ['link'] != '') {
$code = '
' . $code . '';
}
break;
case AI_CODE_ADSENSE:
$adsense_size = ($data ['adsense-width'] != '' ? ' width: '. esc_html ($data ['adsense-width']). 'px;' : '') . ($data ['adsense-height'] != '' ? ' height: '.esc_html ($data ['adsense-height']).'px;' : '');
switch ($data ['adsense-type']) {
case AI_ADSENSE_AMP_ONLY:
$code = '';
break;
default:
// $code = '';
$code = '';
if ($data ['adsense-comment']) $code .= "\n";
$adsense_full_width_responsive = $data ['adsense-full-width-responsive'] != '' ? "\n".' data-full-width-responsive="' . esc_html ($data ['adsense-full-width-responsive']) . '"' : '';
break;
}
switch ($data ['adsense-type']) {
case AI_ADSENSE_STANDARD:
switch ($data ['adsense-size']) {
case AI_ADSENSE_SIZE_FIXED:
// Normal
$code .= '
';
break;
case AI_ADSENSE_SIZE_FIXED_BY_VIEWPORT:
$code = $this->adsense_size_styles ($data) . $code;
// Normal
$code .= '
';
break;
case AI_ADSENSE_SIZE_RESPONSIVE:
// Responsive
$code .= '
';
break;
}
break;
case AI_ADSENSE_LINK:
switch ($data ['adsense-size']) {
case AI_ADSENSE_SIZE_FIXED:
// Normal
$code .= '
';
break;
case AI_ADSENSE_SIZE_FIXED_BY_VIEWPORT:
$code = $this->adsense_size_styles ($data) . $code;
// Normal
$code .= '
';
break;
case AI_ADSENSE_SIZE_RESPONSIVE:
// Responsive
$code .= '
';
break;
}
break;
case AI_ADSENSE_IN_ARTICLE:
$code .= '
';
break;
case AI_ADSENSE_IN_FEED:
$code .= '
';
break;
case AI_ADSENSE_MATCHED_CONTENT:
$code .= '
';
break;
case AI_ADSENSE_AUTO:
$code .= '
';
break;
}
if ($data ['adsense-amp'] != AI_ADSENSE_AMP_DISABLED) {
if ($code != '') {
$code .= '
';
}
switch ($data ['adsense-amp']) {
case AI_ADSENSE_AMP_AUTO:
$code .= '[ADINSERTER AMP]
';
break;
case AI_ADSENSE_AMP_ABOVE_THE_FOLD:
$code .= '[ADINSERTER AMP]
';
break;
case AI_ADSENSE_AMP_FIXED:
$code .= '[ADINSERTER AMP]
';
break;
case AI_ADSENSE_AMP_BELOW_THE_FOLD:
$code .= '[ADINSERTER AMP]
';
break;
case AI_ADSENSE_AMP_STICKY:
if ($data ['adsense-width'] == '') {
$data ['adsense-width'] = 320;
}
if ($data ['adsense-height'] == '') {
$data ['adsense-height'] = 50;
}
$code .= '[ADINSERTER AMP]
';
break;
}
}
break;
case AI_CODE_AMAZON:
$code_lines = explode ("\n", stripslashes ($data ['amazon-data']));
$clean_code = '';
$regionurl = '';
$amp_lines = array ();
foreach ($code_lines as $index => $code_line) {
if (strpos ($code_line, 'regionurl') !== false) {
unset ($code_lines [$index]);
$regionurl = trim (str_replace ('regionurl', '', $code_line));
$regionurl = trim (ltrim ($regionurl, '='));
} else {
$amp_lines [] = ' data-' . $code_lines [$index];
$code_lines [$index] = ' ' . $code_lines [$index] . ';';
}
}
$amazon_data = implode ("\n", $code_lines);
$code .= '
';
if ($data ['amazon-amp']) {
$code .= '
[ADINSERTER AMP]
';
}
break;
}
return $code;
}
public function adsense_size_styles ($data){
$code = '
';
return $code;
}
public function import ($code){
if (trim ($code) == '') {
return array ('type' => AI_CODE_UNKNOWN);
}
$code_expanded_shortodes = do_shortcode ($code);
$amp = false;
$adsense_amp = AI_ADSENSE_AMP_DISABLED;
if (strpos ($code_expanded_shortodes, AD_AMP_SEPARATOR) !== false) {
$amp = true;
$adsense_amp = AI_ADSENSE_AMP_ABOVE_THE_FOLD;
}
if (!class_exists ('DOMDocument')) {
echo __('ERROR: class DOMDocument not found. Your webhost needs to install the DOM extension for PHP.', 'ad-inserter');
wp_die ();
}
try {
$dom = new DOMDocument ();
libxml_use_internal_errors (true);
// $dom->loadHTML ($code);
$dom->loadHTML ("
$code");
libxml_clear_errors ();
} catch (Exception $e) {
echo 'ERROR: ', $e->getMessage();
wp_die ();
}
// AdSense
if (strpos ($code, 'data-ad-client') !== false) {
$adsense_script = $dom->getElementsByTagName ('script');
$adsense_script_src = $adsense_script->item (0) != null && $adsense_script->item (0)->getAttribute ('src') != null ? $adsense_script->item (0)->getAttribute ('src') : '';
$adsense_code = $dom->getElementsByTagName ('ins');
$adsense_code_amp = $dom->getElementsByTagName ('amp-ad');
$adsense_code_amp_sticky = $dom->getElementsByTagName ('amp-sticky-ad');
$adsense_code_amp_auto = $dom->getElementsByTagName ('amp-auto-ads');
if ($adsense_code_amp_sticky->length != 0) {
$adsense_amp = AI_ADSENSE_AMP_STICKY;
}
elseif ($adsense_code_amp_auto->length != 0) {
$adsense_amp = AI_ADSENSE_AMP_AUTO;
}
if ($adsense_code_amp->length == 0) {
if ($adsense_code_amp_auto->length != 0) {
$adsense_code_amp = $adsense_code_amp_auto;
}
}
if ($adsense_code_amp->length != 0) {
// $layout = $adsense_code_amp [0]->getAttribute ('layout'); // PHP 5.6.3
$layout = $adsense_code_amp->item (0)->getAttribute ('layout');
if ($adsense_amp != AI_ADSENSE_AMP_DISABLED) {
switch ($layout) {
case 'fixed-height':
$adsense_amp = AI_ADSENSE_AMP_ABOVE_THE_FOLD;
break;
case 'fixed':
$adsense_amp = AI_ADSENSE_AMP_FIXED;
break;
case 'nodisplay':
$adsense_amp = AI_ADSENSE_AMP_STICKY;
break;
default:
$auto_format = $adsense_code_amp->item (0)->getAttribute ('data-auto-format');
if ($auto_format == "rspv") {
$adsense_amp = AI_ADSENSE_AMP_BELOW_THE_FOLD;
}
break;
}
}
if ($adsense_code->length == 0) $adsense_code = $adsense_code_amp;
}
if ($adsense_code->length != 0) {
$data = array (
'type' => AI_CODE_ADSENSE,
'adsense-script' => $adsense_script_src,
'adsense-publisher-id' => '',
'adsense-ad-slot-id' => '',
'adsense-type' => AI_ADSENSE_STANDARD,
'adsense-size' => AI_ADSENSE_SIZE_FIXED,
'adsense-width' => '',
'adsense-height' => '',
'adsense-layout' => '',
'adsense-layout-key' => '',
'adsense-full-width-responsive' => '',
'adsense-comment' => '',
'adsense-amp' => $adsense_amp,
'adsense-amp-block-on-consent' => '#',
);
// $data ['adsense-publisher-id'] = str_replace ('ca-', '', $adsense_code [0]->getAttribute ('data-ad-client'));
$data ['adsense-publisher-id'] = str_replace ('ca-', '', $adsense_code->item (0)->getAttribute ('data-ad-client'));
// $data ['adsense-ad-slot-id'] = $adsense_code [0]->getAttribute ('data-ad-slot');
$data ['adsense-ad-slot-id'] = $adsense_code->item (0)->getAttribute ('data-ad-slot');
if ($data ['adsense-ad-slot-id'] == '') {
$data ['adsense-ad-slot-id'] = $adsense_code->item (0)->getAttribute ('data-slot');
}
// $adsense_style = $adsense_code [0]->getAttribute ('style');
$adsense_style = $adsense_code->item (0)->getAttribute ('style');
$style_width = preg_match ("/width\s*:\s*(\d+)px/", $adsense_style, $width_match);
if ($style_width) $data ['adsense-width'] = $width_match [1];
if ($data ['adsense-width'] == '') {
$data ['adsense-width'] = $adsense_code->item (0)->getAttribute ('width');
}
$style_height = preg_match ("/height\s*:\s*(\d+)px/", $adsense_style, $height_match);
if ($style_height) $data ['adsense-height'] = $height_match [1];
if ($data ['adsense-height'] == '') {
$data ['adsense-height'] = $adsense_code->item (0)->getAttribute ('height');
}
$display = '';
$style_display = preg_match ("/display\s*:\s*([a-z\-]+)/", $adsense_style, $display_match);
if ($style_display) $display = $display_match [1];
$adsense_class = trim ($adsense_code->item (0)->getAttribute ('class'));
$adsense_classes = explode (' ', $adsense_class);
$adsense_size = !$style_width && !$style_height && $display == 'block' ? AI_ADSENSE_SIZE_RESPONSIVE : AI_ADSENSE_SIZE_FIXED;
if (count ($adsense_classes) == 2 && !$style_width && !$style_height) {
$adsense_size = AI_ADSENSE_SIZE_FIXED_BY_VIEWPORT;
$viewport_class = $adsense_classes [1];
$style = preg_match ("##s", $code, $style_match);
$style_lines = explode ("\n", trim ($style_match [1]));
$viewport_data = array ();
for ($viewport = 1; $viewport <= 6; $viewport ++) {
$viewport_name = get_viewport_name ($viewport);
$viewport_width = get_viewport_width ($viewport);
if ($viewport_name != '') {
$viewport_data []= array ('index' => $viewport, 'name' => $viewport_name, 'width' => $viewport_width);
}
}
usort ($viewport_data, 'ai_compare_viewport');
$viewport_data = array_reverse ($viewport_data);
$sizes = array ();
$viewport_widths = array ();
$viewport_indexes = array ();
foreach ($viewport_data as $index => $viewport) {
$viewport_name = $viewport ['name'];
$viewport_width = $viewport ['width'];
$viewport_index = $viewport ['index'];
if ($viewport_name != '') {
$viewport_widths [] = $viewport_width;
$viewport_indexes [] = $viewport_index;
$sizes []= array (0 => '', 1 => '');
}
}
if (count ($style_lines) == count ($sizes)) {
foreach ($style_lines as $index => $style_line) {
$viewport_index = $viewport_indexes [$index] - 1;
if (strpos ($style_line, $viewport_class) !== false) {
$min_width = preg_match ("/min-width\s*:\s*(\d+)px/", $style_line, $min_width_match);
$viewport_width = $min_width ? $min_width_match [1] : 0;
if ($viewport_width == $viewport_widths [$index]) {
$styles = explode ($viewport_class, $style_line);
$style_line = $styles [1];
$style_width = preg_match ("/width\s*:\s*(\d+)px/", $style_line, $width_match);
$adsense_width = $style_width ? $width_match [1] : '';
$style_height = preg_match ("/height\s*:\s*(\d+)px/", $style_line, $height_match);
$adsense_height = $style_height ? $height_match [1] : '';
$sizes [$viewport_index] = array (0 => $adsense_width, 1 => $adsense_height);
}
} else $sizes [$viewport_index] = array ('', '');
}
}
$data ['adsense-sizes'] = $sizes;
}
$data ['adsense-size'] = $adsense_size;
$comment = preg_match ("##", $code, $comment_match);
if ($comment) $data ['adsense-comment'] = trim ($comment_match [1]);
// $adsense_ad_format = $adsense_code [0]->getAttribute ('data-ad-format');
$adsense_ad_format = $adsense_code->item (0)->getAttribute ('data-ad-format');
if ($adsense_amp == AI_ADSENSE_AMP_STICKY) {
$data ['adsense-type'] = AI_ADSENSE_AMP_ONLY;
}
elseif ($adsense_amp == AI_ADSENSE_AMP_AUTO) {
$data ['adsense-type'] = AI_ADSENSE_AMP_ONLY;
} else
switch ($adsense_ad_format) {
case '':
break;
case 'auto':
break;
case 'autorelaxed':
$data ['adsense-type'] = AI_ADSENSE_MATCHED_CONTENT;
break;
// case 'link':
// $data ['adsense-type'] = AI_ADSENSE_LINK;
// break;
case 'fluid':
// $adsense_ad_layout = $adsense_code [0]->getAttribute ('data-ad-layout');
$adsense_ad_layout = $adsense_code->item (0)->getAttribute ('data-ad-layout');
switch ($adsense_ad_layout) {
case 'in-article':
$data ['adsense-type'] = AI_ADSENSE_IN_ARTICLE;
break 2;
}
$data ['adsense-type'] = AI_ADSENSE_IN_FEED;
$data ['adsense-layout'] = $adsense_ad_layout;
// $data ['adsense-layout-key'] = urlencode ($adsense_code [0]->getAttribute ('data-ad-layout-key'));
$data ['adsense-layout-key'] = urlencode ($adsense_code->item (0)->getAttribute ('data-ad-layout-key'));
break;
}
$data ['adsense-full-width-responsive'] = $adsense_code->item (0)->getAttribute ('data-full-width-responsive');
if ($adsense_code_amp->item (0) != null) {
if ($adsense_code_amp->item (0)->hasAttribute ('data-block-on-consent')) {
$data ['adsense-amp-block-on-consent'] = $adsense_code->item (0)->getAttribute ('data-block-on-consent');
}
}
return $data;
}
}
// Old AdSense / AdSense Auto ads
if (strpos ($code, 'google_ad_client') !== false) {
$data = array (
'type' => AI_CODE_ADSENSE,
'adsense-publisher-id' => '',
'adsense-ad-slot-id' => '',
'adsense-type' => AI_ADSENSE_STANDARD,
'adsense-size' => AI_ADSENSE_SIZE_FIXED,
'adsense-width' => '',
'adsense-height' => '',
'adsense-layout' => '',
'adsense-layout-key' => '',
'adsense-amp' => $adsense_amp,
'adsense-amp-block-on-consent' => '#',
);
$comment = preg_match ("##", $code, $comment_match);
if ($comment) $data ['adsense-comment'] = trim ($comment_match [1]);
if (preg_match ("/google_ad_client.+[\"\'](.+?)[\"\']/", $code, $match)) {
$data ['adsense-publisher-id'] = str_replace ('ca-', '', $match [1]);
}
if (preg_match ("/google_ad_slot.+[\"\'](.+?)[\"\']/", $code, $match)) {
$data ['adsense-ad-slot-id'] = $match [1];
}
if (preg_match ("/google_ad_width[^\d]+(\d+)/", $code, $match)) {
$data ['adsense-width'] = $match [1];
}
if (preg_match ("/google_ad_height[^\d]+(\d+)/", $code, $match)) {
$data ['adsense-height'] = $match [1];
}
if (preg_match ("/enable_page_level_ads[^\d]+true/", $code, $match)) {
$data ['adsense-type'] = AI_ADSENSE_AUTO;
}
return $data;
}
// Amazon
if (strpos ($code, 'amzn_assoc_') !== false) {
$data = array (
'type' => AI_CODE_AMAZON,
'amazon-amp' => AI_AMAZON_AMP_DISABLED,
'amazon-width' => '',
'amazon-height' => '200',
'amazon-amp-block-on-consent' => '#',
);
if (strpos ($code, 'data-amzn_assoc_') !== false) {
$data ['amazon-amp'] = AI_AMAZON_AMP_ENABLED;
$amazon_amp_ad = $dom->getElementsByTagName ('amp-ad');
if ($amazon_amp_ad->item (0)->getAttribute ('width') != '') {
$data ['amazon-width'] = $amazon_amp_ad->item (0)->getAttribute ('width');
}
if ($amazon_amp_ad->item (0)->getAttribute ('height') != '') {
$data ['amazon-height'] = $amazon_amp_ad->item (0)->getAttribute ('height');
}
if ($amazon_amp_ad->item (0)->hasAttribute ('data-block-on-consent')) {
$data ['amazon-amp-block-on-consent'] = $amazon_amp_ad->item (0)->getAttribute ('data-block-on-consent');
}
}
if (preg_match_all ("/(amzn_assoc_[^\s=]+?)\s*=\s*[\"](.+?)[\"]/", $code, $matches)) {
$unique_matches = array ();
$unique_matches_values = array ();
foreach ($matches [1] as $index => $match) {
$found = false;
foreach ($unique_matches as $unique_match) {
if ($match == $unique_match) {
$found = true;
break;
}
}
if (!$found) {
$unique_matches [] = $match;
$unique_matches_values [] = $matches [2][$index];
}
}
$amazon_data = '';
foreach ($unique_matches as $index => $unique_match) {
if ($amazon_data != '') $amazon_data .= "\n";
$amazon_data .= $unique_match . ' = "' . $unique_matches_values [$index] . '"';
}
$amazon_script = $dom->getElementsByTagName ('script');
if ($amazon_script-> length >= 2) {
$amazon_data .= "\n".'regionurl' . ' = "' . $amazon_script->item (1)->getAttribute ('src') . '"';
}
$data ['amazon-data'] = $amazon_data;
}
return $data;
}
// Banner
$links = $dom->getElementsByTagName ('a');
$images = $dom->getElementsByTagName ('img');
if ($links->length != 0 || $images->length != 0) {
$data = array ('type' => AI_CODE_BANNER, 'image' => '', 'link' => '', 'target' => '');
if ($images->length != 0) {
// $data ['image'] = $images [0]->getAttribute ('src');
$data ['image'] = $images->item (0)->getAttribute ('src');
$data ['alt'] = $images->item (0)->getAttribute ('alt');
$data ['loading'] = $images->item (0)->getAttribute ('loading');
}
if ($links->length != 0) {
// $data ['link'] = $links [0]->getAttribute ('href');
$data ['link'] = $links->item (0)->getAttribute ('href');
// $data ['target'] = $links [0]->getAttribute ('target');
$data ['target'] = $links->item (0)->getAttribute ('target');
}
return $data;
}
return array ('type' => AI_CODE_UNKNOWN);
}
public function import_rotation ($code, $extract_check_names = false){
global $ai_expand_only_rotate_count_check, $ai_wp_data;
$data = array (
'options' => array (
array (
'code' => $code,
'name' => '',
'share' => '',
'time' => '',
),
),
);
$ai_expand_only_rotate_count_check = true;
unset ($ai_wp_data [AI_SHORTCODES]['rotate']);
$code = do_shortcode ($code);
$ai_expand_only_rotate_count_check = false;
if (strpos ($code, AD_CHECK_SEPARATOR) !== false) {
if (!$extract_check_names) {
return $data;
}
if (function_exists ('ai_check_separators') && isset ($ai_wp_data [AI_SHORTCODES]['check'][0]['check']) && $ai_wp_data [AI_SHORTCODES]['check'][0]['check'] == 'statistics') {
$check_parameters = $ai_wp_data [AI_SHORTCODES]['check'];
$options = explode (AD_CHECK_SEPARATOR, $code);
$data ['options'] = array ();
foreach ($options as $index => $option) {
$option_code = trim ($option, "\n");
$option_name = isset ($check_parameters [$index - 1]['name']) ? $check_parameters [$index - 1]['name'] : '';
$option_index = isset ($check_parameters [$index - 1]['index']) && $check_parameters [$index - 1]['index'] != '' ? (int) $check_parameters [$index - 1]['index'] : $index;
// Skip option before first CHECK shortcode (always inserted)
if ($option_index == 0) continue;
$data ['options'][$option_index - 1] = array ('name' => $option_name);
}
}
return $data;
}
preg_match_all ('/\|count([0-9]+?)\|/', $code, $matches);
if (count ($matches [1]) != 0) {
return $data;
}
preg_match_all ('/\|rotate([0-9]+?)\|/', $code, $matches);
if (count ($matches [1]) != 0) {
$rotate_parameters = array ();
foreach ($matches [1] as $match) {
$rotate_parameters []= $ai_wp_data [AI_SHORTCODES]['rotate'][$match];
}
$code = preg_replace ('/\|rotate([0-9]+?)\|/', AD_ROTATE_SEPARATOR, $code);
} else if (isset ($ai_wp_data [AI_SHORTCODES]['rotate'])) $rotate_parameters = $ai_wp_data [AI_SHORTCODES]['rotate'];
if (strpos ($code, AD_ROTATE_SEPARATOR) !== false) {
$options = explode (AD_ROTATE_SEPARATOR, $code);
$data ['options'] = array ();
foreach ($options as $index => $option) {
$option_code = trim ($option, " \n");
$rotation_groups = 0;
if (isset ($rotate_parameters [$index - 1]['group']) && $rotate_parameters [$index - 1]['group'] != '') {
$rotation_groups = 1;
$option_name = $rotate_parameters [$index - 1]['group'];
$option_share = '';
$option_time = '';
$option_index = '';
$option_scheduling = '';
$option_append_prepend = '';
} else {
$option_share = '';
$option_name = isset ($rotate_parameters [$index - 1]['name']) ? $rotate_parameters [$index - 1]['name'] : '';
$option_append_prepend = isset ($rotate_parameters [$index - 1]['code']) ? $rotate_parameters [$index - 1]['code'] : '';
if (isset ($rotate_parameters [$index - 1]['share'])) {
if (is_numeric ($rotate_parameters [$index - 1]['share'])) {
$option_share = intval ($rotate_parameters [$index - 1]['share']);
}
elseif (strpos (strtolower ($rotate_parameters [$index - 1]['share']), 'ctr') === 0) {
$option_share = $rotate_parameters [$index - 1]['share'];
} else $option_share = '';
}
// $option_share = isset ($rotate_parameters [$index - 1]['share']) && is_numeric ($rotate_parameters [$index - 1]['share']) ? intval ($rotate_parameters [$index - 1]['share']) : '';
$option_time = isset ($rotate_parameters [$index - 1]['time']) && is_numeric ($rotate_parameters [$index - 1]['time']) ? intval ($rotate_parameters [$index - 1]['time']) : '';
$option_index = isset ($rotate_parameters [$index - 1]['index']) && is_numeric ($rotate_parameters [$index - 1]['index']) ? intval ($rotate_parameters [$index - 1]['index']) : '';
$option_scheduling = isset ($rotate_parameters [$index - 1]['scheduling']) && strpos ($rotate_parameters [$index - 1]['scheduling'], '%') !== false && strpos ($rotate_parameters [$index - 1]['scheduling'], '=') !== false ? $rotate_parameters [$index - 1]['scheduling'] : '';
}
if ($index == 0 && $option_code == '') continue;
$data ['options'] []= array ('code' => $option_code, 'name' => $option_name, 'share' => $option_share, 'time' => $option_time, 'index' => $option_index, 'scheduling' => $option_scheduling, 'groups' => $rotation_groups, 'prepend-append' => $option_append_prepend);
}
}
return $data;
}
public function generate_rotation ($rotation_data){
if (count ($rotation_data) == 1) {
$rotation_code = trim ($rotation_data [0]['code']);
} else {
$rotation_code = '';
$rotation_groups = $rotation_data [0]['groups'];
foreach ($rotation_data as $index => $rotation_data_row) {
$name = trim ($rotation_data_row ['name']);
$share = trim ($rotation_data_row ['share']);
$scheduling = trim ($rotation_data_row ['scheduling']);
$time = trim ($rotation_data_row ['time']);
$code = trim ($rotation_data_row ['code'], "\n");
if ($index != 0 || $name != '' || $share != '' || $scheduling != ''|| $time != '') {
$shortcode = "" ;
if ($index != 0) $shortcode .= "\n\n";
$shortcode .= '[ADINSERTER ROTATE';
if ($rotation_groups) {
if ($name != '') $shortcode .= ' group="'.str_replace ('"', '\'', $name).'"';
} else {
if ($name != '') $shortcode .= ' name="'.str_replace ('"', '\'', $name).'"';
if ($share != '') $shortcode .= ' share="'.str_replace ('"', '\'', $share).'"';
if ($scheduling != '') $shortcode .= ' scheduling="'.str_replace ('"', '\'', $scheduling).'"';
if ($time != '') $shortcode .= ' time="'.str_replace ('"', '\'', $time).'"';
}
$shortcode .= "]\n\n";
} else $shortcode = '';
$rotation_code .= $shortcode . $code;
}
}
return $rotation_code;
}
}
class ai_block_labels {
var $class;
var $text_color;
var $left_text;
var $right_text;
public function __construct ($class = '') {
$this->class = $class == '' ? 'ai-debug-default' : $class;
$this->text_color = '';
$this->left_text = '';
$this->right_text = '';
}
public function block_start () {
return "
\n";
}
public function block_end () {
return "\n";
}
public function bar ($left_text, $left_title = '', $center_text = '', $right_text = '', $right_title = '', $additional_class = '') {
return
"
" .
$this->invisible_start () .
$this->bar_text_left ($left_text, $left_title) .
$this->bar_text_center ($center_text) .
$this->bar_text_right ($right_text, $right_title) .
$this->invisible_end () .
"\n";
}
public function bar_hidden_viewport ($left_text, $left_title = '', $center_text = '', $right_text = '', $right_title = '') {
return
"
" .
$this->invisible_start () .
$this->bar_text_left ($left_text, $left_title) .
$this->bar_text_center ($center_text) .
$this->bar_text_right ($right_text, $right_title) .
$this->invisible_end () .
"\n";
}
public function center_bar ($center_text) {
return
"
" . $this->left_text .
$this->invisible_start () .
// "".$this->left_text."" .
$this->bar_text_center ($center_text) .
// "".$this->right_text."" .
$this->invisible_end () .
"\n";
}
public function bar_text_left ($text, $title) {
// $this->left_text = $text;
return "
$text";
}
public function bar_text_center ($text) {
return "
$text ";
}
public function bar_text_right ($text, $title) {
// $this->right_text = $text;
return "
$text";
}
public function invisible_start () {
return '
[AI]';
}
public function invisible_end () {
return '
[/AI]';
}
public function message ($text, $attr = '') {
return
"
" .
$this->invisible_start () .
$text .
$this->invisible_end () .
"\n";
}
public function adb_hidden_section_start () {
return "
";
}
public function adb_hidden_section_end () {
return "\n";
}
public function adb_visible_section_start () {
return "
";
}
public function adb_visible_section_end () {
return "\n";
}
}