'disabled', 'cache-busting' => 'on', 'ad_label' => 'default', 'placement_position' => '', 'item_adblocker' => '', 'pro_minimum_length' => '0', 'words_between_repeats' => '0', 'previous_method' => null, 'previous_id' => null, 'wp_the_query' => [ 'term_id' => '', 'taxonomy' => '', 'is_main_query' => true, 'is_rest_api' => false, 'page' => 1, 'numpages' => 1, 'is_archive' => false, 'is_search' => false, 'is_home' => false, 'is_404' => false, 'is_attachment' => false, 'is_singular' => true, 'is_front_page' => false, 'is_feed' => false, ], 'global_output' => true, ]; /** * One argument in this array may belong to several AJAX requests. * * @var array */ private $ajax_queries_args = []; /** * Is module enabled. * * @var bool */ private $is_enabled = false; /** * Lazy load enabled. * * @var bool */ private $lazy_load_enabled = false; /** * The fallback method. * * @var string */ public $fallback_method = 'ajax'; /** * Lazy load offset. * * @var integer */ public $lazy_load_offset = 0; /** * Holds the server info class. * * @var Advanced_Ads_Pro_Cache_Busting_Server_Info */ public $server_info = null; /** * Constructor */ private function __construct() { if ( is_admin() ) { new Advanced_Ads_Pro_Module_Cache_Busting_Admin_UI(); } add_action( 'init', [ $this, 'init' ], 30 ); } /** * Cache busting initialization * * @return void */ public function init() { $options = Advanced_Ads_Pro::get_instance()->get_options(); if ( isset( $options['cache-busting'] ) ) { $this->options = $options['cache-busting']; } $this->is_enabled = $this->options['enabled'] ?? false; if ( ! $this->should_init_cb() ) { // CB not needed, abort. add_action( 'wp_enqueue_scripts', [ $this, 'check_for_tcf_privacy' ] ); return; } $this->lazy_load_enabled = $options['lazy-load']['enabled'] ?? false; $this->lazy_load_offset = absint( $options['lazy-load']['offset'] ?? 0 ); // An AJAX request but not necessarily to `/admin-ajax.php`. $this->is_ajax = wp_doing_ajax() || 'XMLHttpRequest' === Params::server( 'HTTP_X_REQUESTED_WITH' ); if ( ! $this->is_ajax && ! is_admin() ) { add_action( 'wp', [ $this, 'init_frontend' ] ); // Load Advads Tracking header scripts. add_filter( 'advanced-ads-tracking-load-header-scripts', [ $this, 'load_tracking_scripts' ], 10, 1 ); } $this->fallback_method = ( ! isset( $this->options['default_fallback_method'] ) || $this->options['default_fallback_method'] === 'ajax' ) ? 'ajax' : 'off'; if ( 'ajax' === $this->fallback_method ) { $this->server_info = new Advanced_Ads_Pro_Cache_Busting_Server_Info( $this, $this->options ); } add_filter( 'advanced-ads-ad-output-debug-content', [ $this, 'add_debug_content' ], 10, 2 ); add_filter( 'advanced-ads-ajax-ad-select-arguments', [ $this, 'add_default_ajax_arguments' ], 10, 2 ); add_action( 'advanced-ads-placement-options-after-advanced', [ $this, 'placement_options' ], 10, 2 ); } /** * Append empty cache busting options to placement advanced options list * * @param string $slug current placement slug. * @param Placement $placement current placement data. * * @return void */ public function placement_options( $slug, $placement ) { WordPress::render_option( 'placement-empty-cache-busting', __( 'Hide when empty', 'advanced-ads-pro' ), sprintf( '', checked( $placement->get_prop( 'cache_busting_empty' ) ? true : false, true, false ), esc_html__( 'Remove the placeholder if unfilled.', 'advanced-ads-pro' ) ), sprintf( '%s %s%s%s', __( 'Deleting an empty placement might lead to a layout shift.', 'advanced-ads-pro' ), '', __( 'Manual', 'advanced-ads-pro' ), '' ) ); } /** * Check if cache-busting should be initialized. * * Even when the module is disabled, we partially (i.e .conditions are not checked for every visitor of a cached page) * use cache-busting functionality to deliver Custom Position placements so that they do not appear in the footer when * selectors do not exist. * * @see self::add_simple_js_item */ public function should_init_cb() { if ( $this->is_enabled ) { return true; } foreach ( wp_advads_get_placements() as $placement ) { if ( $placement->is_type( 'custom_position' ) && ! empty( $placement->get_item() ) && Advanced_Ads_Pro::get_instance()->get_options()['placement-positioning'] !== 'php' ) { return true; } } return false; } /** * Return an instance of this class. * * @return obj A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Init cache-busting frontend after the `parse_query` hook. * Not ajax, not admin. */ public function init_frontend() { global $wp_the_query; if ( apply_filters( 'advanced-ads-pro-cb-frontend-disable', false ) // Disable cache-busting on AMP pages. || ( function_exists( 'advads_is_amp' ) && advads_is_amp() ) || $wp_the_query->is_feed() ) { return; } add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); add_action( 'wp_head', [ $this, 'watch_wp_head'], PHP_INT_MAX ); add_filter( 'advanced-ads-ad-output', [ $this, 'watch_ad_output' ], 100, 2 ); add_filter( 'advanced-ads-group-output', [ $this, 'watch_group_output' ], 100, 2 ); add_filter( 'advanced-ads-ad-select-override-by-ad', [ $this, 'override_ad_select_by_ad' ], 10, 3 ); add_filter( 'advanced-ads-ad-select-override-by-group', [ $this, 'override_ad_select_by_group' ], 10, 4 ); add_action( 'wp_footer', [ $this, 'passive_cache_busting_output' ], 21 ); add_filter( 'advanced-ads-can-display-ad', [ $this, 'can_display_by_display_limit' ], 10, 3 ); if ( ! $this->is_enabled ) { return; } add_filter( 'advanced-ads-ad-select-args', [ $this, 'override_ad_select' ], 100 ); add_filter( 'advanced-ads-ad-select-args', [ $this, 'disable_global_output' ], 101 ); add_action( 'advanced-ads-can-display-placement', [ $this, 'placement_can_display' ], 12, 2 ); } /** * Output passive cache-busting array */ public function passive_cache_busting_output() { $arrays = [ 'window.advads_placement_tests' => Advanced_Ads_Pro_Placement_Tests::get_instance()->get_placement_tests_js( false ), 'window.advads_passive_ads' => $this->passive_cache_busting_ads, 'window.advads_passive_groups' => $this->passive_cache_busting_groups, 'window.advads_passive_placements' => $this->passive_cache_busting_placements, 'window.advads_ajax_queries' => self::$ajax_queries, 'window.advads_has_ads' => $this->has_ads, 'window.advads_js_items' => $this->js_items, 'window.advads_ajax_queries_args' => $this->ajax_queries_args, ]; $content = ''; foreach ( $arrays as $name => $array ) { if ( $array ) { $has_data = true; $content .= $name . ' = ' . json_encode( $array ) . ";\n"; } } if ( ! $content ) { return; } $content = ''; if ( class_exists( 'Advanced_Ads_Utils' ) && method_exists( 'Advanced_Ads_Utils', 'get_inline_asset' ) ) { $content = Advanced_Ads_Utils::get_inline_asset( $content ); } echo $content; } /** * Enqueue frontend scripts * * @return void */ public function enqueue_scripts() { wp_advads()->json->add( [ 'frontendPrefix' => wp_advads()->get_frontend_prefix() ] ); wp_enqueue_script( 'advanced-ads-pro/postscribe', AA_PRO_BASE_URL . 'assets/js/postscribe.js', [], AAP_VERSION, true ); $dependencies = [ 'advanced-ads-pro/postscribe', 'jquery' ]; // If the privacy module is active, add advanced-js as a dependency. if ( ! empty( Advanced_Ads_Privacy::get_instance()->options()['enabled'] ) ) { $dependencies[] = ADVADS_SLUG . '-advanced-js'; } // Include in footer to prevent conflict when Autoptimize and NextGen Gallery are used at the same time. wp_register_script( 'advanced-ads-pro/cache_busting', AA_PRO_BASE_URL . 'assets/js/front/front.js', $dependencies, AAP_VERSION, true ); $info = [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'lazy_load_module_enabled' => $this->lazy_load_enabled, 'lazy_load' => [ 'default_offset' => $this->lazy_load_offset, 'offsets' => apply_filters( 'advanced-ads-lazy-load-placement-offsets', [] ), ], 'moveintohidden' => defined( 'ADVANCED_ADS_PRO_CUSTOM_POSITION_MOVE_INTO_HIDDEN' ), 'wp_timezone_offset' => Advanced_Ads_Utils::get_wp_timezone()->getOffset( date_create() ), 'the_id' => get_the_ID(), 'is_singular' => is_singular(), ]; if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $current_lang = apply_filters( 'wpml_current_language', null ); $info['ajax_url'] = add_query_arg( 'wpml_lang', $current_lang, $info['ajax_url'] ); } wp_localize_script( 'advanced-ads-pro/cache_busting', 'advanced_ads_pro_ajax_object', $info ); wp_enqueue_script( 'advanced-ads-pro/cache_busting' ); } /** * Provide current_ad propery to client. * * @param string $content * @param Ad $ad * * @return string */ public function watch_ad_output( $content, $ad = null ) { if ( isset( $ad ) && is_an_ad( $ad ) ) { // build content (arguments are: id, method, title) if ( ! empty( $ad->get_prop( 'ad_args.global_output' ) ) ) { $this->has_ads[] = [ "{$ad->get_id()}", 'ad', $ad->get_title(), 'off' ]; } if ( $this->collecting_js_items ) { $this->has_js_items[] = [ 'id' => $ad->get_id(), 'type' => 'ad', 'title' => $ad->get_title(), 'blog_id' => get_current_blog_id() ]; } } return $content; } /** * Provide current group propery to client. * * @param string $content * @param Group $group * * @return string */ public function watch_group_output( $content, Group $group ) { if ( $this->collecting_js_items ) { $this->has_js_items[] = [ 'id' => $group->get_id(), 'type' => 'group', 'title' => $group->get_id() ]; } return $content; } /** * Turn off head optimisation. */ public function watch_wp_head() { $this->isHead = false; } /** * Replace ad content with placeholder. * * @param array $arguments * * @return array */ public function override_ad_select( $arguments ) { // placements and not Feed only $not_feed = empty( $arguments['wp_the_query']['is_feed'] ); if ( $arguments['method'] === Constants::ENTITY_PLACEMENT && $not_feed ) { $placement = wp_advads_get_placement( $arguments['id'] ); if ( empty( $placement) ) { return $arguments; } if ( empty( $placement->get_item() ) ) { // placement was created but no item was selected in dropdown unset( $arguments['override'] ); return $arguments; } $arguments['placement_type'] = $placement->get_type(); $options = $placement->get_data(); foreach ( $options as $_k => $_v ) { if ( ! isset( $arguments[ $_k ] ) ) { $arguments[ $_k ] = $_v; } } $query = self::build_js_query( $arguments ); // allow to disable feature if ( $this->can_override( $query ) ) { $arguments['override'] = $this->get_override_content( $query ); } } return $arguments; } /** * Disable global output for cache-busting. * We neither track ads nor show them in the "Ads" section of Admin Bar until we show them to the user. * * @param array $arguments Arguments passed to ads and groups from top level placements/ads/groups. * @return array */ public function disable_global_output( $arguments ) { if ( isset( $arguments['global_output'] ) ) { return $arguments; } if ( // Custom position. ( isset( $arguments['placement_type'] ) && $arguments['placement_type'] === 'custom_position' && Advanced_Ads_Pro::get_instance()->get_options()['placement-positioning'] !== 'php' ) // Cache Busting "ajax" or "auto". || ( isset( $arguments['placement_type'] ) && ( ! isset( $arguments['cache-busting'] ) || $arguments['cache-busting'] !== self::OPTION_OFF ) ) // Force passive cache-busting. || ( ! isset( $arguments['placement_type'] ) && ! empty( $this->options['passive_all'] ) ) ) { $arguments['global_output'] = false; return $arguments; } $arguments['global_output'] = true; return $arguments; } /** * Return ad, prepared for js handler if the conditions are met. * * @param bool|string $overriden_ad Ad content to override. * @param Ad $ad Ad object. * @param array $args Arguments passed to ads and groups from top level placements/ads/groups. * @return bool|string Ad content prepared for js handler if the conditions are met. */ public function override_ad_select_by_ad( $overriden_ad, Ad $ad, $args ) { if ( ! $this->can_override_passive( $args ) ) { return $overriden_ad; } if ( $this->is_enabled ) { // Cache busting 'auto'. $overriden_ad = $this->cache_busting_auto_for_ad( $overriden_ad, $ad, $args ); } if ( false === $overriden_ad ) { // The cache-busting module is disabled or the 'off' fallback has been aplied. $overriden_ad = $this->get_simple_js_ad( $overriden_ad, $ad, $args ); } return $overriden_ad; } /** * return group, prepared for js handler if the conditions are met * * @param string $overriden_group group content to override * @param obj $group Group * @param array/null $ordered_ad_ids ordered ids of the ads that belong to the group * @param array $args argument passed to the 'get_ad_by_group' function * @return string/false group content prepared for js handler if the conditions are met */ public function override_ad_select_by_group( $overriden_group, Group $group, $ordered_ad_ids, $args ) { if ( ! $this->can_override_passive( $args ) ) { return $overriden_group; } if ( $this->is_enabled ) { // Cache busting 'auto'. $overriden_group = $this->cache_busting_auto_for_group( $overriden_group, $group, $ordered_ad_ids, $args ); } if ( false === $overriden_group ) { // The cache-busting module is disabled or the 'off' fallback has been aplied. $overriden_group = $this->get_simple_js_group( $overriden_group, $group, $ordered_ad_ids, $args ); } return $overriden_group; } /** * Prevents serverside check of visitor conditions for passive ads * * @param array $check_options can display check options. * * @return array */ public function bypass_can_display_check( array $check_options ) { return array_merge( $check_options, [ 'passive_cache_busting' => true ] ); } /** * Return passive ad, prepared for js handler if the conditions are met. * * @param bool|string $overriden_ad Ad content to override. * @param Ad $ad Ad object. * @param array $args Arguments passed to ads and groups from top level placements/ads/groups. * @return bool|string */ public function cache_busting_auto_for_ad( $overriden_ad, Ad $ad, $args ) { // If it was requested by placement; if cache-busting option does not exist yet, or exist and = 'auto'. $cache_busting_auto = isset( $args['placement_type'] ) && ( ! isset( $args['cache-busting'] ) || $args['cache-busting'] === self::OPTION_AUTO ); $cache_busting_off = isset( $args['cache-busting'] ) && $args['cache-busting'] === self::OPTION_OFF; $prev_is_placement = isset( $args['previous_method'] ) && $args['previous_method'] === 'placement' && isset( $args['previous_id'] ); $test_id = isset( $args['test_id'] ) ? $args['test_id'] : null; $is_passive_all = ! empty( $this->options['passive_all'] ); if ( $cache_busting_auto && ! $this->is_passive_method_used() ) { // ajax method // ad was requested by group `placement->group->ad` or `group->ad` if ( isset( $args['previous_method'] ) && $args['previous_method'] === 'group' && isset( $args['previous_id'] ) ) { return $ad; } $ad_args = $ad->get_prop( 'ad_args' ); $ad->set_prop_temp( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_ON, 'cache-busting-orig' => self::OPTION_AUTO, ] ) ); $overriden_ad = $this->get_overridden_ajax_ad( $ad, $args ); if ( false === $overriden_ad ) { // static and not test return $this->return_ad_with_cb_off( $overriden_ad, $ad, $args ); } return $overriden_ad; } elseif ( ! $cache_busting_off && ( $cache_busting_auto || $is_passive_all ) ) { // passive method // Ad was requested by group `placement->group->ad` or `group->ad`. if ( isset( $args['previous_method'] ) && $args['previous_method'] === 'group' && isset( $args['previous_id'] ) ) { return $ad; } $needs_backend = $this->ad_needs_backend_request( $ad ); // ad was requested by placement `placement->ad` or `ad` // check if ad can be delivered without any cache-busting if ( 'static' === $needs_backend && ! $is_passive_all && ! $test_id ) { return $this->return_ad_with_cb_off( $overriden_ad, $ad, $args ); } // check if ad cannot be delivered with passive cache-busting if ( 'off' === $needs_backend || 'ajax' === $needs_backend ) { $is_ajax_fallbback = 'ajax' === $needs_backend; if ( isset( $args['output']['placement_id'] ) && ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { // prevent selection of this placement using JavaScript if ( $test_id ){ Advanced_Ads_Pro_Placement_Tests::get_instance()->no_cb_fallbacks[] = $args['previous_id']; } return ''; } if ( $is_ajax_fallbback && $cache_busting_auto ) { $ad->set_prop( 'cache-busting', self::OPTION_ON ); $ad->set_prop( 'cache-busting-orig', self::OPTION_AUTO ); return $this->get_overridden_ajax_ad( $ad, $args ); } // `No cache-busting` fallback if ( $test_id ) { if ( in_array( $args['previous_id'], Advanced_Ads_Pro_Placement_Tests::get_instance()->get_random_placements() ) ) { Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests[ $args['previous_id'] ] = $test_id; } else { // prevent selection of this placement using JavaScript Advanced_Ads_Pro_Placement_Tests::get_instance()->no_cb_fallbacks[] = $args['previous_id']; return ''; } } return $this->return_ad_with_cb_off( $overriden_ad, $ad, $args ); } if ( ! $ad->can_display( [ 'passive_cache_busting' => true ] ) ) { if ( $test_id && array_key_exists( $args['previous_id'], Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests ) ) { Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests[ $args['previous_id'] ] = $test_id; } return ''; } // deliver ad using passive cache-busting // add new info to the passive cache-busting array $overriden_ad = $this->get_passive_overriden_ad( $ad, $args ); } if ( $prev_is_placement && false === $overriden_ad && $test_id ) { Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests[ $args['previous_id'] ] = $test_id ; } return $overriden_ad; } /** * Return ad with cache-busting "off" when it is not needed. * * @param bool|string $overriden_ad Ad content to override. * @param Ad $ad Ad object. * @param array $args Arguments passed to ads and groups from top level placements/ads/groups. * * @return bool|string */ private function return_ad_with_cb_off( $overriden_ad, Ad $ad, $args ) { $ad_args = $ad->get_prop( 'ad_args' ); $ad->set_prop_temp( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_OFF, 'cache-busting-orig' => self::OPTION_AUTO, 'global_output' => true, ] ) ); if ( isset( $args['output']['placement_id'] ) ) { if ( ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { return ''; } } return $overriden_ad; } public function cache_busting_auto_for_group( $overriden_group, Group $group, $ordered_ad_ids, $args ) { $prev_is_placement = isset( $args['previous_method'] ) && $args['previous_method'] === 'placement' && isset( $args['previous_id'] ); $cache_busting_auto = isset( $args['placement_type'] ) && ( ! isset( $args['cache-busting'] ) || $args['cache-busting'] === self::OPTION_AUTO ); $test_id = isset( $args['test_id'] ) ? $args['test_id'] : null; $is_passive_all = ! empty( $this->options['passive_all'] ); $cache_busting_off = isset( $args['cache-busting'] ) && $args['cache-busting'] === self::OPTION_OFF; if ( $cache_busting_auto && ! $this->is_passive_method_used() ) { // ajax method $group_ads = $this->request_passive_ads_of_group( $group, $ordered_ad_ids, $args ); $ad_args = $group->get_prop( 'ad_args' ); if ( $test_id || ! $this->group_ads_static( $group_ads, $group ) ) { $group->set_prop_temp( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_ON, 'cache-busting-orig' => self::OPTION_AUTO, ] ) ); $query = self::build_js_query( $args ); $overriden_group = $this->get_override_content( $query ); } if ( false === $overriden_group ) { // Static and does not belong to a test. unset( $ad_args['cache_busting_elementid'] ); $group->set_prop_temp( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_OFF, 'cache-busting-orig' => self::OPTION_AUTO, 'global_output' => true, ] ) ); unset( $args['cache_busting_elementid'] ); if ( isset( $args['output']['placement_id'] ) ) { if ( ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { return ''; } } } return $overriden_group; } elseif ( ! $cache_busting_off && ( $cache_busting_auto || $is_passive_all ) ) { // passive method if ( is_array( $ordered_ad_ids ) && count( $ordered_ad_ids ) > 0 ) { // add info about the group to the passive cache-busting array $uniq_key = ++self::$adOffset; $group_ads = $this->request_passive_ads_of_group( $group, $ordered_ad_ids, $args ); foreach ( $group_ads as $ad ) { $needs_backend = $this->ad_needs_backend_request( $ad ); if ( 'off' === $needs_backend || 'ajax' === $needs_backend ) { $is_ajax_fallbback = 'ajax' === $needs_backend; // delete info from the passive cache-busting array $this->delete_passive_group( $group, $args, $uniq_key ); if ( isset( $args['output']['placement_id'] ) && ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { // prevent selection of this placement using JavaScript if ( $test_id ){ Advanced_Ads_Pro_Placement_Tests::get_instance()->no_cb_fallbacks[] = $args['previous_id']; } return ''; } if ( $is_ajax_fallbback && $cache_busting_auto ) { $group->set_prop( 'cache-busting', self::OPTION_ON ); $group->set_prop( 'cache-busting-orig', self::OPTION_AUTO ); $query = self::build_js_query( $args); return $this->get_override_content( $query ); } else { // `No cache-busting` fallback if ( $test_id ) { if ( in_array( $args['previous_id'], Advanced_Ads_Pro_Placement_Tests::get_instance()->get_random_placements() ) ) { Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests[ $args['previous_id'] ] = $test_id; } else { // prevent selection of this placement using JavaScript Advanced_Ads_Pro_Placement_Tests::get_instance()->no_cb_fallbacks[] = $args['previous_id']; return ''; } } $ad_args = $group->get_prop( 'ad_args' ); unset( $ad_args['cache_busting_elementid'] ); $group->set_prop_temp( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_OFF, 'cache-busting-orig' => self::OPTION_AUTO, 'global_output' => true, ] ) ); unset( $args['cache_busting_elementid'] ); if ( isset( $args['output']['placement_id'] ) ) { if ( ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { return ''; } } return $overriden_group; } } } if ( $this->group_ads_static( $group_ads, $group ) && ! $is_passive_all && ! $test_id ) { $ad_args = $group->get_prop( 'ad_args' ); unset( $ad_args['cache_busting_elementid'] ); $group->set_prop( 'ad_args', array_merge( $ad_args, [ 'cache-busting' => self::OPTION_OFF, 'cache-busting-orig' => self::OPTION_AUTO, 'global_output' => true, ] ) ); unset( $args['cache_busting_elementid'] ); if ( isset( $args['output']['placement_id'] ) ) { if ( ! $this->placement_can_display_not_passive( $args['output']['placement_id'] ) ) { return ''; } } return $overriden_group; } $output_string = $this->get_passive_overriden_group( $group, $ordered_ad_ids, $args, $uniq_key, $group_ads ); $overriden_group = $output_string; } } if ( $prev_is_placement && false === $overriden_group && $test_id ) { Advanced_Ads_Pro_Placement_Tests::get_instance()->delivered_tests[ $args['previous_id'] ] = $test_id; } return $overriden_group; } /** * Request passive ads of a group. * * @param Group $group Group * @param array|null $ordered_ad_ids ordered ids of the ads that belong to the group * @param array $args argument passed to the 'get_ad_by_group' function */ private function request_passive_ads_of_group( $group, $ordered_ad_ids, $args ) { $args['global_output'] = false; $args['is_top_level'] = false; $args['ad_label'] = 'disabled'; $args['group_info'] = [ 'passive_cb' => true, 'id' => $group->get_id(), 'name' => $group->get_title(), 'type' => $group->get_type(), 'refresh_enabled' => Advanced_Ads_Pro_Group_Refresh::is_enabled( $group ), ]; $ordered_ad_ids = is_array( $ordered_ad_ids ) ? $ordered_ad_ids : []; $group_ads = []; foreach ( $ordered_ad_ids as $_ad_id ) { // get result from the 'override_ad_select_by_ad' method $ad = get_the_ad( $_ad_id, '', $args ); // Ignore ads that are hidden for all users. if ( ! is_an_ad( $ad ) || ! $ad->can_display( [ 'passive_cache_busting' => true ] ) ) { continue; } $ad->set_parent( $group ); $group_ads[] = $ad; } return $group_ads; } /** * Get simple js ad. * Conditions are not checked for every visitor of a cached page. * * @param bool|string $overriden_ad Ad content to override. * @param Ad $ad Ad object. * @param array $args Arguments passed to ads and groups from top level placements/ads/groups. * @return string Ad content prepared for js handler if the conditions are met */ public function get_simple_js_ad( $overriden_ad, Ad $ad, $args ) { $cp_placement = isset( $args['placement_type'] ) && $args['placement_type'] === 'custom_position'; if ( ! $cp_placement // Check if collecting of simple ads has been started. || $this->collecting_js_items || Advanced_Ads_Pro::get_instance()->get_options()['placement-positioning'] === 'php' ) { return $overriden_ad; } $this->collecting_js_items = true; $elementid = $this->generate_elementid(); $args['cache_busting_elementid'] = $elementid; $ad->set_prop( 'cache_busting_elementid', $elementid ); $overriden_ad = ''; if ( $ad->can_display() ) { // Disable global output because the ads will be tracked using an AJAX request. $ad_args = $ad->get_prop( 'ad_args' ); $ad_args['global_output'] = false; $ad->set_prop_temp( 'ad_args', $ad_args ); $l = count( $this->has_js_items ); $overriden_ad = $this->add_simple_js_item( $elementid, $ad->output(), $l, $args ); $ad_args = $ad->get_prop( 'ad_args' ); $ad_args['global_output'] = true; $ad->set_prop_temp( 'ad_args', $ad_args ); } $this->collecting_js_items = false; return $overriden_ad; } /** * Get simple js group. * Conditions are not checked for every visitor of a cached page. * * @param bool|string $overriden_group Group content to override. * @param Group $group Group object. * @param int[] $ordered_ad_ids ids of the ads that belong to the group ordered by their injection order. * @param array $args Arguments passed to ads and groups from top level placements/ads/groups. * @return bool|string $overriden_group Overriden group content if conditions are met. */ public function get_simple_js_group( $overriden_group, Group $group, $ordered_ad_ids, $args ) { $cp_placement = isset( $args['placement_type'] ) && $args['placement_type'] === 'custom_position'; if ( ! $cp_placement // Check if collecting of simple ads has been started. || $this->collecting_js_items || Advanced_Ads_Pro::get_instance()->get_options()['placement-positioning'] === 'php' ) { return $overriden_group; } $this->collecting_js_items = true; $elementid = $this->generate_elementid(); $args['cache_busting_elementid'] = $elementid; $ad_args = $group->get_prop( 'ad_args' ); // Disable global output because the ads will be tracked using an AJAX request. $ad_args['global_output'] = false; $ad_args['cache_busting_elementid'] = $elementid; $group->set_prop_temp( 'ad_args', $ad_args ); $l = count( $this->has_js_items ); $overriden_group = $this->add_simple_js_item( $elementid, $group->output( $ordered_ad_ids ), $l, $args ); $ad_args = $group->get_prop( 'ad_args' ); $ad_args['global_output'] = true; $group->set_prop_temp( 'ad_args', $ad_args ); $this->collecting_js_items = false; return $overriden_group; } /** * Add simple js item. * * @param string $elementid Wrapper id. * @param string $output Ad/Group output. * @param int $l Number of existing simple js items. * @param array $args Placement options. * @return string Wrapper id. */ function add_simple_js_item( $elementid, $output, $l, $args ) { if ( isset( $args['output']["placement_id"] ) ) { $placement = wp_advads_get_placement( $args['output']['placement_id'] ); $this->has_js_items[] = [ 'id' => $args['output']["placement_id"], 'type' => 'placement', 'title' => $placement->get_title() ?? '', 'blog_id' => get_current_blog_id() ]; } $js_item = [ 'output' => $output, 'elementid' => $elementid, 'args' => $args, 'has_js_items' => array_slice( $this->has_js_items, $l ), ]; $js_item = apply_filters( 'advanced-ads-cache-busting-item', $js_item, [ 'method' => 'placement', 'args' => $args ] ); $this->js_items[] = $js_item; /** * Collect blog data before `restore_current_blog` is called. */ if ( class_exists( Data::class, false ) ) { Data::collect_blog_data(); } $placement_id = ! empty( $args['output']['placement_id'] ) ? $args['output']['placement_id'] : ''; return $this->create_wrapper( $elementid, $placement_id, $args ); } /** * add data related to ad and ad placement to js array * * @param obj $ad Ad * @param array $args argument passed to the 'get_ad_by_id' function * @return string */ private function get_passive_overriden_ad( Ad $ad, $args ) { $cache_busting_auto = isset( $args['placement_type'] ) && ( ! isset( $args['cache-busting'] ) || $args['cache-busting'] === self::OPTION_AUTO ); if ( $cache_busting_auto ) { $js_array = & $this->passive_cache_busting_placements; $id = $args['previous_id']; } else { $js_array = & $this->passive_cache_busting_ads; $id = $args['id']; } $uniq_key = $id . '_' . ++self::$adOffset; $not_head = ! $this->isHead || ( isset( $args['placement_type'] ) && $args['placement_type'] !== 'header' ); $elementid = $not_head ? $this->generate_elementid() : null; $args['cache_busting_elementid'] = $elementid; $ad->set_prop( 'cache_busting_elementid', $elementid ); $placement_id = ! empty( $args['output']['placement_id'] ) ? $args['output']['placement_id'] : ''; $output_string = $not_head ? $this->create_wrapper( $elementid, $placement_id, $args ) : ''; $js_array[ $uniq_key ] = [ 'elementid' => [ $elementid ], 'ads' => [ $ad->get_id() => $this->get_passive_cb_for_ad( $ad ) ], // only 1 ad ]; if ( $cache_busting_auto ) { $test_id = isset( $args['test_id'] ) ? $args['test_id'] : null; $js_array[ $uniq_key ]['type'] = 'ad'; $js_array[ $uniq_key ]['id'] = $ad->get_id(); $js_array[ $uniq_key ]['placement_info'] = $this->get_placement_info( $id ); $js_array[ $uniq_key ]['test_id'] = $test_id; $item_for_ab = Advanced_Ads_Pro_Module_Ads_For_Adblockers::get_item_for_adblocker( $ad ); if ( is_an_ad( $item_for_ab ) ) { $js_array[ $uniq_key ]['ads_for_ab'] = [ $item_for_ab->get_id() => $this->get_passive_cb_for_ad( $item_for_ab ) ]; } if ( is_a_group( $item_for_ab ) ) { $js_array[ $uniq_key ]['groups_for_ab'] = [ 'id' => $item_for_ab->get_id(), 'name' => $item_for_ab->get_title(), 'weights' => $item_for_ab->get_ad_weights(), 'type' => $item_for_ab->get_type(), 'ordered_ad_ids' => $item_for_ab->get_ordered_ad_ids(), 'ad_count' => $item_for_ab->get_ad_count(), ]; $ads_for_ab = $item_for_ab->get_ads(); $js_array[ $uniq_key ]['groups_for_ab']['ads'] = []; foreach ( $ads_for_ab as $item ) { $js_array[ $uniq_key ]['groups_for_ab']['ads'][ $item->get_id() ] = $this->get_passive_cb_for_ad( $item ); } } if ( 'ajax' === $this->fallback_method ) { $ajax_info = $this->server_info->get_ajax_for_passive_placement( $ad, $args, $elementid ); if ( $ajax_info ) { $js_array[ $uniq_key ] = array_merge( $js_array[ $uniq_key ], $ajax_info ); } } } $js_array[ $uniq_key ] = apply_filters( 'advanced-ads-cache-busting-item', $js_array[ $uniq_key ], [ 'method' => $cache_busting_auto ? 'placement' : 'ad', 'args' => $args ] ); return $output_string; } /** * Add data related to group and group placement to js array * * @param Group $group the group. * @param array|null $ordered_ad_ids ordered ids of the ads that belong to the group. * @param array $args argument passed to the 'get_ad_by_group' function. * @param string $uniq_key Property name in JS array. * @param array $group_ads Group ads. * * @return string */ private function get_passive_overriden_group( Group $group, $ordered_ad_ids, $args, $uniq_key, $group_ads ) { $cache_busting_auto = isset( $args['placement_type'] ) && ( ! isset( $args['cache-busting'] ) || $args['cache-busting'] === self::OPTION_AUTO ); if ( $cache_busting_auto ) { $js_array = & $this->passive_cache_busting_placements; $id = $args['previous_id']; } else { $js_array = & $this->passive_cache_busting_groups; $id = $args['id']; } $uniq_key = $id . '_' . $uniq_key; $not_head = ! $this->isHead || ( isset( $args['placement_type'] ) && $args['placement_type'] !== 'header' ); $elementid = $not_head ? $this->generate_elementid() : null; $args['cache_busting_elementid'] = $elementid; $group->set_prop( 'cache_busting_elementid', $elementid ); $placement_id = ! empty( $args['output']['placement_id'] ) ? $args['output']['placement_id'] : ''; $output_string = $not_head ? $this->create_wrapper( $elementid, $placement_id, $args ) : ''; if ( ( $ad_count = apply_filters( 'advanced-ads-group-ad-count', $group->get_ad_count(), $group ) ) === 'all' ) { $ad_count = 999; } $passive_ads = []; foreach ( $group_ads as $group_ad ) { $passive_ads[ $group_ad->get_id() ] = $this->get_passive_cb_for_ad( $group_ad ); } $js_array[ $uniq_key ] = [ 'type'=> 'group', 'id' => $group->get_id(), 'elementid' => [ $elementid ], 'ads' => $passive_ads, 'group_info' => [ 'id' => $group->get_id(), 'name' => $group->get_title(), 'weights' => $group->get_ad_weights( $ordered_ad_ids ), 'type' => $group->get_type(), 'ordered_ad_ids' => $ordered_ad_ids, 'ad_count' => $ad_count, ], ]; // deprecated after Advaned Ads Slider > 1.3.1 if ( $group->is_type( 'slider' ) && defined( 'AAS_VERSION' ) && version_compare( AAS_VERSION, '1.3.1', '<=' ) ) { $slider_options = Frontend::get_slider_options( $group ); $js_array[ $uniq_key ]['group_info']['slider_options'] = $slider_options; } if ( Advanced_Ads_Pro_Group_Refresh::is_enabled( $group ) ) { $js_array[ $uniq_key ]['group_info']['refresh_enabled'] = true; $js_array[ $uniq_key ]['group_info']['refresh_interval_for_ads'] = Advanced_Ads_Pro_Group_Refresh::get_ad_intervals( $group ); } $label = ''; if ( method_exists( Advanced_Ads::get_instance(), 'get_label' ) ) { $placement_state = isset( $args['ad_label'] ) ? $args['ad_label'] : 'default'; $label = Advanced_Ads::get_instance()->get_label( $group, $placement_state ); } if ( $cache_busting_auto ) { $js_array[ $uniq_key ]['placement_info'] = $this->get_placement_info( $id ); $js_array[ $uniq_key ]['test_id'] = isset( $args['test_id'] ) ? $args['test_id'] : null; $placement = $group->get_root_placement(); $item_for_ab = false; if ( $placement ) { $placement->set_prop_temp( 'ad_label', false ); $item_for_ab = Advanced_Ads_Pro_Module_Ads_For_Adblockers::get_item_for_adblocker( $placement ); } if ( is_an_ad( $item_for_ab ) ) { $js_array[ $uniq_key ]['ads_for_ab'] = [ $item_for_ab->get_id() => $this->get_passive_cb_for_ad( $item_for_ab ) ]; } if ( is_a_group( $item_for_ab ) ) { $js_array[ $uniq_key ]['groups_for_ab'] = [ 'id' => $item_for_ab->get_id(), 'name' => $item_for_ab->get_title(), 'weights' => $item_for_ab->get_ad_weights(), 'type' => $item_for_ab->get_type(), 'ordered_ad_ids' => $item_for_ab->get_ordered_ad_ids(), 'ad_count' => $item_for_ab->get_ad_count(), ]; $ads_for_ab = $item_for_ab->get_ads(); $js_array[ $uniq_key ]['groups_for_ab']['ads'] = []; foreach ( $ads_for_ab as $item ) { $js_array[ $uniq_key ]['groups_for_ab']['ads'][ $item->get_id() ] = $this->get_passive_cb_for_ad( $item ); } } if ( 'ajax' === $this->fallback_method ) { $ajax_info = $this->server_info->get_ajax_for_passive_placement( $group_ads, $args, $elementid ); if ( $ajax_info ) { $js_array[ $uniq_key ] = array_merge( $js_array[ $uniq_key ], $ajax_info ); } } } $js_array[ $uniq_key ] = apply_filters( 'advanced-ads-pro-passive-cb-group-data', $js_array[ $uniq_key ], $group, $elementid ); // Add wrapper around group. $wrapper = $group->create_wrapper(); if ( ( ! empty( $wrapper ) || $label ) && is_array( $wrapper ) && class_exists( 'Advanced_Ads_Utils' ) && method_exists( 'Advanced_Ads_Utils' , 'build_html_attributes' ) ) { $before = '