: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$_options = isset( $_placement['options'] ) ? $_placement['options'] : [];
// injecting ad code so we don’t run escaping here.
echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
* Injected ad into content (before and after)
* @param string $content post content.
public function inject_content( $content = '' ) {
$options = $this->plugin->options();
// do not inject in content when on a BuddyPress profile upload page (avatar & cover image).
if ( ( function_exists( 'bp_is_user_change_avatar' ) && bp_is_user_change_avatar() ) || ( function_exists( 'bp_is_user_change_cover_image' ) && bp_is_user_change_cover_image() ) ) {
// do not inject ads multiple times, e.g., when the_content is applied multiple times.
if ( $this->has_many_the_content() ) {
// Check if ads are disabled in secondary queries.
if ( ! empty( $options['disabled-ads']['secondary'] ) ) {
// this function was called by ajax (in secondary query).
// get out of wp_router_page post type if ads are disabled in secondary queries.
if ( 'wp_router_page' === get_post_type() ) {
// No need to inject ads because all tags are stripped from excepts.
if ( doing_filter( 'get_the_excerpt' ) ) {
// run only within the loop on single pages of public post types.
$public_post_types = get_post_types(
'publicly_queryable' => true,
// make sure that no ad is injected into another ad.
if ( get_post_type() === Entities::POST_TYPE_AD ) {
// Do not inject on admin pages.
if ( is_admin() && ! wp_doing_ajax() ) {
// Do not inject in writing REST requests.
if ( WordPress::is_gutenberg_writing_request() && WordPress::is_rest_request() ) {
// check if admin allows injection in all places.
$enabled = $options['content-injection-enabled'] ?? 'off';
if ( $enabled === 'off' || ! isset( $options['content-injection-everywhere'] ) || 0 === $options['content-injection-everywhere'] ) {
// check if this is a singular page within the loop or an AMP page.
$is_amp = advads_is_amp();
if ( ( ! is_singular( $public_post_types ) && ! is_feed() ) || ( ! $is_amp && ! $this->in_the_loop() && ! $this->was_in_the_loop ) ) {
if ( is_main_query() && 'true' !== $options['content-injection-everywhere'] && isset( $wp_query->current_post ) && $wp_query->current_post >= ( $options['content-injection-everywhere'] ) ) {
$placements = get_option( 'advads-ads-placements', [] );
if ( ! apply_filters( 'advanced-ads-can-inject-into-content', true, $content, $placements ) ) {
if ( is_array( $placements ) ) {
foreach ( $placements as $_placement_id => $_placement ) {
if ( empty( $_placement['item'] ) || ! isset( $_placement['type'] ) ) {
$_options = isset( $_placement['options'] ) ? $_placement['options'] : [];
// check if injection is ok for a specific placement ID.
if ( ! apply_filters( 'advanced-ads-can-inject-into-content-' . $_placement_id, true, $content, $_placement_id ) ) {
switch ( $_placement['type'] ) {
$content = Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ) . $content;
$content .= Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options );
$content = Advanced_Ads_Placements::inject_in_content( $_placement_id, $_options, $content );
if ( ! empty( $_COOKIE['advads_frontend_picker'] ) ) {
// Make possible to know where the content starts and ends.
$content = '<ins style="display: none;" class="advads-frontend-picker-boundary-helper"></ins>
* Load all ads based on WP_Query conditions
* @deprecated 1.4.8 use model class
* @param array $args WP_Query arguments that are more specific that default.
* @return array $ads array with post objects
public static function get_ads( $args = [] ) {
return self::get_instance()->get_model()->get_ads( $args );
* @deprecated 1.4.8 use model class
* @param array $args array with options.
* @return array $groups array with ad groups
* @link http://codex.wordpress.org/Function_Reference/get_terms
public static function get_ad_groups( $args = [] ) {
return self::get_instance()->get_model()->get_ad_groups( $args );
* Get the array with ad placements
* @deprecated 1.4.8 use model
* @return array $ad_placements
public static function get_ad_placements_array() {
return self::get_instance()->get_model()->get_ad_placements_array();
* @deprecated 1.4.8 use model
public static function get_ad_conditions() {
return self::get_instance()->get_model()->get_ad_conditions();
* General check if ads can be displayed for the whole page impression
* @return bool true, if ads can be displayed.
* @todo move this to set_disabled_constant().
public function can_display_ads() {
// check global constant if ads are enabled or disabled.
if ( defined( 'ADVADS_ADS_DISABLED' ) ) {
$options = $this->options();
// check if ads are disabled in secondary queries.
// and this is not main query and this is not ajax (because main query does not exist in ajax but ad needs to be shown).
if ( ! empty( $options['disabled-ads']['secondary'] ) && ! $this->is_main_query() && ! wp_doing_ajax() ) {
* Check if the current user agent is given or a bot
* @return bool true if the current user agent is empty or a bot.
public function is_bot() {
// show ads on AMP version also for bots in order to allow Google (and maybe others) to cache the page.
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
$bots = apply_filters( 'advanced-ads-bots', $this->bots );
$bots = implode( '|', $bots );
// Make sure delimiters in regex are escaped.
$bots = preg_replace( '/(.*?)(?<!\\\)' . preg_quote( '/', '/' ) . '(.*?)/', '$1\\/$2', $bots );
return preg_match( sprintf( '/%s/i', $bots ), wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
* Get the array of known bots.
* @param bool $filter Whether to apply filters.
public function get_bots( $filter = true ) {
return (array) ( $filter ? apply_filters( 'advanced-ads-bots', $this->bots ) : $this->bots );
* Check if the current user is a bot prepopulating the cache
* Ads should be loaded for the bot, because they should show up on the cached site
public function is_cache_bot() {
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && '' !== $_SERVER['HTTP_USER_AGENT'] ) {
$current = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
if ( false !== strpos( $current, 'wprocketbot' ) ) {
$wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
if ( $current === $wp_useragent ) {
// LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents.
if ( false !== strpos( $current, 'lscache_' ) ) {
* Add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type
public function noindex_attachment_images() {
if ( is_attachment() && is_object( $post ) && isset( $post->post_parent ) ) {
$post_parent = get_post( $post->post_parent );
$parent_is_ad = $post_parent && Entities::POST_TYPE_AD === $post_parent->post_type;
// if the image was not attached to any post and if at least one image ad contains the image. Needed for backward compatibility.
$parent_is_image_ad = ( empty( $post->post_parent ) && 0 < get_post_meta( get_the_ID(), '_advanced-ads_parent_id', true ) );
if ( $parent_is_ad || $parent_is_image_ad ) {
echo '<meta name="robots" content="noindex,nofollow" />';
* Show custom CSS in the header
public function custom_header_code(){
if ( ! defined( 'ADVANCED_ADS_DISABLE_EDIT_BAR' ) && WordPress::user_can( 'advanced_ads_edit_ads' ) ) {
div.advads-edit-bar{position:absolute;height:0;display:none;z-index:10000;animation:advads-edit-appear 2s linear 1;}
@keyframes advads-edit-appear {
0% {opacity: 0.0;pointer-events: none;}
a.advads-edit-button{position:absolute;top:0;left:0;text-decoration:none !important;box-shadow:none;border-bottom:none;color:#0074a2;margin-top:-5px;}
a.advads-edit-button span{top:10px;line-height:25px;margin-left:-5px;width:26px;height:26px;border-radius:13px;border:solid 1px #0074a2;background:#fff}
'div[class^="%s"]:hover > div.advads-edit-bar {display: inline-block; vertical-align: top;}',
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Advanced_Ads_Plugin::get_instance()->get_frontend_prefix()
* Supports the "$this->is_main_query=true" while main query is being executed
* @param WP_Post $post The Post object (passed by reference).
* @param WP_Query $query The current Query object (passed by reference).
public function set_query_type( $post, $query = null ) {
if ( $query instanceof WP_Query ) {
$this->is_main_query = $query->is_main_query();
* Check if main query is being executed
* @return bool true while main query is being executed or not in the loop, false otherwise
public function is_main_query() {
if ( ! $this->in_the_loop() ) {
// the secondary query check only designed for within post content.
return true === $this->is_main_query;
* Sets whether the loop has started.
public function set_loop_start() {
$this->in_the_loop = true;
* Sets whether the loop has ended.
public function set_loop_end() {
$this->in_the_loop = false;
* Whether the loop has started and the caller is in the loop.
public function in_the_loop() {
if ( $this->in_the_loop ) {
* Find the calls to `the_content` inside functions hooked to `the_content`.
public function has_many_the_content() {
global $wp_current_filter;
if ( count( array_keys( $wp_current_filter, 'the_content', true ) ) > 1 ) {
// More then one `the_content` in the stack.
* Get an "Advertisement" label to use before single ad or before first ad in a group
* @param string $placement_state default/enabled/disabled.
* @return string label, empty string if label should not be displayed.
public function get_label( $placement_state = 'default' ) {
if ( 'disabled' === $placement_state ) {
$advads_options = self::get_instance()->options();
if ( 'enabled' !== $placement_state && empty( $advads_options['custom-label']['enabled'] ) ) {
$label = ! empty( $advads_options['custom-label']['text'] ) ? esc_html( $advads_options['custom-label']['text'] ) : _x( 'Advertisements', 'label above ads', 'advanced-ads' );
$template = sprintf( '<div class="%s">%s</div>', Advanced_Ads_Plugin::get_instance()->get_frontend_prefix() . 'adlabel', $label );
return apply_filters( 'advanced-ads-custom-label', $template, $label );
* Retrieve the number of ads in any status
* excludes trash status by default
* @param string|array $post_status default post status.
* @return int number of ads.
public static function get_number_of_ads( $post_status = 'any' ) {
$key = md5( serialize( $post_status ) );
// query number of ads only, if not retrieved, yet.
if ( ! isset( self::get_instance()->number_of_ads[ $key ] ) ) {
$args = [ 'post_status' => $post_status ];
$recent_ads = self::get_instance()->get_model()->get_ads( $args );
self::get_instance()->number_of_ads[ $key ] = count( $recent_ads );
return self::get_instance()->number_of_ads[ $key ];
* Switch the current blog.
* @param int $blog_id ID of the blog in the WP network.
public function switch_to_blog( $blog_id ) {
switch_to_blog( $blog_id );
self::get_instance()->get_model()->reset_placement_array();
* Restore the current blog.
public function restore_current_blog() {
self::get_instance()->get_model()->reset_placement_array();
* Store whether the loop started in an inner `the_content`.
* If so, let us assume that we are in the loop when we are in the outermost `the_content`.
* Makes sense only when a hooked to `the_content` function that produces an inner `the_content` has
* lesser priority then `$this->plugin->get_content_injection_priority()`.
* @param string $content Post content (unchanged).
public function set_was_in_the_loop( $content ) {
if ( self::get_instance()->has_many_the_content() ) {
$this->was_in_the_loop = $this->was_in_the_loop || $this->in_the_loop();
// Next top level `the_content`, forget that the loop started.
$this->was_in_the_loop = false;
* Listen to URL parameters for debugging
private function debug_parameter() {
if ( wp_doing_ajax() && isset( $_SERVER['HTTP_REFERER'] ) ) {
$query_string = wp_parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY );
parse_str( $query_string, $query );
if ( empty( $query['aa-debug'] ) ) {
$debug_query = $query['aa-debug'];
if ( empty( $_GET['aa-debug'] ) ) {
$debug_query = $_GET['aa-debug'];
$parameters = explode( ',', sanitize_text_field( $debug_query ) );
foreach ( $parameters as $parameter ) {
switch ( trim( $parameter ) ) {
// switch all ads to "dummy"
add_filter( 'advanced-ads-ad-option-type', function() {
// disable ad visitor conditions
add_filter( 'advanced-ads-ad-option-visitors', '__return_empty_array' );
// disable cache-busting for all ads
add_filter( 'advanced-ads-ad-select-args', function( $args ) {
$args['cache-busting'] = 'ignore';