: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @package Advanced_Ads_Admin
* @author Thomas Maier <support@wpadvancedads.com>
* @link https://wpadvancedads.com
* @copyright 2013-2018 Thomas Maier, Advanced Ads GmbH
use AdvancedAds\Entities;
use AdvancedAds\Utilities\WordPress;
* Plugin class. This class should ideally be used to work with the
* public-facing side of the WordPress site.
* @author Thomas Maier <support@wpadvancedads.com>
* @deprecated 1.47.0 Use \AdvancedAds\Entities::POST_TYPE_AD
const POST_TYPE_SLUG = 'advanced_ads';
* @deprecated 1.47.0 Use \AdvancedAds\Entities::TAXONOMY_AD_GROUP
const AD_GROUP_TAXONOMY = 'advanced_ads_groups';
* Instance of this class.
private static $instance = null;
* Array with ads currently delivered in the frontend
* @var array Ads already loaded in the frontend
public $current_ads = [];
protected $options = false;
protected $adsense_options = null;
* Interal plugin options – set by the plugin
protected $internal_options = false;
* Whether the loop started in an inner `the_content`.
protected $was_in_the_loop = false;
* Signifies Whether the loop has started and the caller is in the loop.
* We need it because Some the "Divi" theme calls the `loop_start/loop_end` hooks
* instead of calling `the_post()` to signify that the caller is in the loop.
protected $in_the_loop = false;
* List of bots and crawlers to exclude from ad impressions
* @var array list of bots
protected $bots = [ 'bot', 'spider', 'crawler', 'scraper', 'parser', '008', 'Accoona-AI-Agent', 'ADmantX', 'alexa', 'appie', 'Apple-PubSub', 'Arachmo', 'Ask Jeeves', 'avira\.com', 'B-l-i-t-z-B-O-T', 'boitho\.com-dc', 'BUbiNG', 'Cerberian Drtrs', 'Charlotte', 'cosmos', 'Covario IDS', 'curl', 'Datanyze', 'DataparkSearch', 'Dataprovider\.com', 'DDG-Android', 'Ecosia', 'expo9', 'facebookexternalhit', 'Feedfetcher-Google', 'FindLinks', 'Firefly', 'froogle', 'Genieo', 'heritrix', 'Holmes', 'htdig', 'https://developers\.google\.com', 'ia_archiver', 'ichiro', 'igdeSpyder', 'InfoSeek', 'inktomi', 'Kraken', 'L\.webis', 'Larbin', 'Linguee', 'LinkWalker', 'looksmart', 'lwp-trivial', 'mabontland', 'Mnogosearch', 'mogimogi', 'Morning Paper', 'MVAClient', 'NationalDirectory', 'NetResearchServer', 'NewsGator', 'NG-Search', 'Nusearch', 'NutchCVS', 'Nymesis', 'oegp', 'Orbiter', 'Peew', 'Pompos', 'PostPost', 'proximic', 'PycURL', 'Qseero', 'rabaz', 'Radian6', 'Reeder', 'savetheworldheritage', 'SBIder', 'Scooter', 'ScoutJet', 'Scrubby', 'SearchSight', 'semanticdiscovery', 'Sensis', 'ShopWiki', 'silk', 'Snappy', 'Spade', 'Sqworm', 'StackRambler', 'TechnoratiSnoop', 'TECNOSEEK', 'Teoma', 'Thumbnail\.CZ', 'TinEye', 'truwoGPS', 'updated', 'Vagabondo', 'voltron', 'Vortex', 'voyager', 'VYU2', 'WebBug', 'webcollage', 'WebIndex', 'Websquash\.com', 'WeSEE:Ads', 'wf84', 'Wget', 'WomlpeFactory', 'WordPress', 'yacy', 'Yahoo! Slurp', 'Yahoo! Slurp China', 'YahooSeeker', 'YahooSeeker-Testing', 'YandexBot', 'YandexMedia', 'YandexBlogs', 'YandexNews', 'YandexCalendar', 'YandexImages', 'Yeti', 'yoogliFetchAgent', 'Zao', 'ZyBorg', 'okhttp', 'ips-agent', 'ltx71', 'Optimizer', 'Daum', 'Qwantify' ];
* Loaded instance of Advanced_Ads_Model
* @var Advanced_Ads_Model
* Loaded instance of Advanced_Ads_Plugin
* @var Advanced_Ads_Plugin
* Loaded instance of Advanced_Ads_Select
* @var Advanced_Ads_Select
* Is the query the main query?, when WP_Query is used
private $number_of_ads = [];
* Reason why are disabled.
public $disabled_reason = null;
* Identifier that supplement the disabled reason.
public $disabled_id = null;
* Initialize frontend features
private function __construct() {
$this->plugin = Advanced_Ads_Plugin::get_instance();
$this->plugin->set_model( $this->get_model() );
$this->ad_selector = Advanced_Ads_Select::get_instance();
// initialize plugin specific functions.
add_action( 'init', [ $this, 'wp_init' ] );
// only when not doing ajax.
Advanced_Ads_Ajax::get_instance();
add_action( 'plugins_loaded', [ $this, 'wp_plugins_loaded' ] );
// allow add-ons to interact.
add_action( 'init', [ $this, 'advanced_ads_loaded' ], 9 );
add_filter( 'the_content', [ $this, 'set_was_in_the_loop' ], ~PHP_INT_MAX );
* Return an instance of this class.
* @return Advanced_Ads 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 the Advanced_Ads_Model responsible for loading ads, groups and placements into the frontend
* @return Advanced_Ads_Model
public function get_model() {
if ( ! isset( $this->model ) ) {
$this->model = new Advanced_Ads_Model( $wpdb );
* Initialize the plugin by setting localization and loading public scripts
public function wp_plugins_loaded() {
// register hook for global constants.
// Wait until BuddyPress assigns `WP_Query->is_404` back to `false`.
add_action( 'template_redirect', [ $this, 'set_disabled_constant' ], 11 );
add_action( 'rest_api_init', [ $this, 'set_disabled_constant' ] );
// setup default ad types.
add_filter( 'advanced-ads-ad-types', [ $this, 'setup_default_ad_types' ], 5 );
// register hooks and filters for auto ad injection.
// add meta robots noindex, nofollow to images, which are part of 'Image ad' ad type.
add_action( 'wp_head', [ $this, 'noindex_attachment_images' ] );
// use custom CSS or other custom header code.
add_action( 'wp_head', [ $this, 'custom_header_code' ] );
// check if ads are disabled in secondary queries.
add_action( 'the_post', [ $this, 'set_query_type' ], 10, 2 );
add_action( 'loop_start', [ $this, 'set_loop_start' ], 10, 0 );
add_action( 'loop_end', [ $this, 'set_loop_end' ], 10, 0 );
// register debug parameter
$this->debug_parameter();
public function advanced_ads_loaded() {
do_action( 'advanced-ads-plugin-loaded' );
* Init / load plugin specific functions and settings
public function wp_init() {
* Define ad types with their options
* name => publically readable name
* description => publically readable description
* editor => kind of editor: text (normal text field), content (WP content field), none (no content field)
* will display text field, if left empty
public function set_ad_types() {
* Load default ad type files
* custom ad types can also be loaded in your own plugin or functions.php
* Developers can add new ad types using this filter
* see classes/ad-type-content.php for an example for an ad type and usage of this filter
$this->ad_types = apply_filters( 'advanced-ads-ad-types', $types );
* Load filters to inject ads into various sections of our site
public function init_injection() {
add_action( 'wp_head', [ $this, 'inject_header' ], 20 );
add_action( 'wp_footer', [ $this, 'inject_footer' ], 20 );
add_filter( 'the_content', [ $this, 'inject_content' ], $this->plugin->get_content_injection_priority() );
* Set global constant that prevents ads from being displayed on the current page view
public function set_disabled_constant() {
global $post, $wp_the_query;
// don't set the constant if already defined.
if ( defined( 'ADVADS_ADS_DISABLED' ) ) {
$options = $this->plugin->options();
// check if ads are disabled completely.
if ( ! empty( $options['disabled-ads']['all'] ) ) {
$this->disable_ads( 'all' );
// Check if ads are disabled in REST API.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
if ( ! empty( $options['disabled-ads']['rest-api'] ) ) {
// check if ads are disabled from 404 pages.
if ( $wp_the_query->is_404() && ! empty( $options['disabled-ads']['404'] ) ) {
$this->disable_ads( '404' );
// check if ads are disabled from non singular pages (often = archives).
if ( ! $wp_the_query->is_singular() && ! empty( $options['disabled-ads']['archives'] ) ) {
$this->disable_ads( 'archive' );
// check if ads are disabled in Feed.
if ( $wp_the_query->is_feed() && ( ! isset( $options['disabled-ads']['feed'] ) || $options['disabled-ads']['feed'] ) ) {
// check if ads are disabled on the current page.
if ( $wp_the_query->is_singular() && isset( $post->ID ) ) {
$post_ad_options = get_post_meta( $post->ID, '_advads_ad_settings', true );
if ( ! empty( $post_ad_options['disable_ads'] ) ) {
$this->disable_ads( 'page', $post->ID );
// "Posts page" set on the WordPress Reading settings page.
if ( $wp_the_query->is_posts_page ) {
$post_ad_options = get_post_meta( $wp_the_query->queried_object_id, '_advads_ad_settings', true );
if ( ! empty( $post_ad_options['disable_ads'] ) ) {
$this->disable_ads( 'page', $wp_the_query->queried_object_id );
* Check if ads are disabled on WooCommerce shop page (and currently on shop page).
* since WooCommerce changes the post ID of the static page selected to be the product overview page, we need to get the original page id from the WC options.
if ( function_exists( 'is_shop' ) && is_shop() ) {
$shop_id = get_option( 'woocommerce_shop_page_id' );
$shop_ad_options = get_post_meta( absint( $shop_id ), '_advads_ad_settings', true );
if ( ! empty( $shop_ad_options['disable_ads'] ) ) {
$this->disable_ads( 'page', $shop_id );
if ( isset( $options['hide-for-user-role'] ) ) {
$hide_for_roles = Advanced_Ads_Utils::maybe_translate_cap_to_role( $options['hide-for-user-role'] );
$user = wp_get_current_user();
if ( $hide_for_roles && is_user_logged_in() && is_array( $user->roles ) && array_intersect( $hide_for_roles, $user->roles ) ) {
// check bots if option is enabled.
if ( isset( $options['block-bots'] ) && $options['block-bots']
&& ! $this->is_cache_bot() && $this->is_bot() ) {
* @param string $reason Reason why are disabled.
* @param int|string $id Identifier that supplement the disabled reason.
private function disable_ads( $reason = null, $id = null ) {
define( 'ADVADS_ADS_DISABLED', true );
$this->disabled_reason = $reason;
$this->disabled_id = $id;
* Return the plugin slug.
* @return string Plugin slug variable.
public function get_plugin_slug() {
return $this->plugin->get_plugin_slug();
* Add plain and content ad types to the default ads of the plugin using a filter
* @param array $types array with ad types.
public function setup_default_ad_types( $types ) {
$types['plain'] = new Advanced_Ads_Ad_Type_Plain(); /* plain text and php code */
$types['dummy'] = new Advanced_Ads_Ad_Type_Dummy(); /* dummy ad */
$types['content'] = new Advanced_Ads_Ad_Type_Content(); /* rich content editor */
$types['image'] = new Advanced_Ads_Ad_Type_Image(); /* image ads */
$types['group'] = new Advanced_Ads_Ad_Type_Group(); /* group ad */
* Log error messages when debug is enabled
* @param string $message error message.
* @link http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
public static function log( $message ) {
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( __( 'Advanced Ads Error following:', 'advanced-ads' ) );
error_log( print_r( $message, true ) );
// translators: %s is an error message generated by the plugin.
$message = sprintf( __( 'Advanced Ads Error: %s', 'advanced-ads' ), $message );
* @return array with plugin options
public function options() {
return $this->plugin->options();
* @return array with adsense options
public function get_adsense_options() {
// we can’t store options if WPML String Translations is enabled, or it would not translate the "Ad Label" option.
if ( ! isset( $this->adsense_options ) || class_exists( 'WPML_ST_String' ) ) {
$this->adsense_options = get_option( 'advanced-ads-adsense', [] );
return $this->adsense_options;
* @return array with internal plugin options
public function internal_options() {
return $this->plugin->internal_options();
* Injected ad into header
public function inject_header() {
$placements = get_option( 'advads-ads-placements', [] );
if ( is_array( $placements ) ) {
foreach ( $placements as $_placement_id => $_placement ) {
if ( isset( $_placement['type'] ) && 'header' === $_placement['type'] ) {
$_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 ads into footer
public function inject_footer() {
$placements = get_option( 'advads-ads-placements', [] );
if ( is_array( $placements ) ) {
foreach ( $placements as $_placement_id => $_placement ) {
if ( isset( $_placement['type'] ) && 'footer' === $_placement['type'] ) {