: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Display Conditions under which to (not) show an ad
class Advanced_Ads_Display_Conditions {
* Advanced_Ads_Display_Conditions
* @var Advanced_Ads_Display_Conditions
protected static $instance;
* Registered display conditions.
* Start of name in form elements
const FORM_NAME = 'advanced_ad[conditions]';
* List of query var values
protected static $query_var_keys = [
* List of options for the General Conditions
protected static $default_general_keys = [
private function __construct() {
add_filter( 'advanced-ads-ad-select-args', [ $this, 'ad_select_args_callback' ] );
add_filter( 'advanced-ads-can-display', [ $this, 'can_display' ], 10, 2 );
// register conditions with init hook, register as late as possible so other plugins can use the same hook to add new taxonomies.
add_action( 'init', [ $this, 'register_conditions' ], 100 );
* Register display conditions
public function register_conditions() {
'label' => __( 'post type', 'advanced-ads' ),
'description' => __( 'Choose the public post types on which to display the ad.', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_post_type' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_post_type' ], // callback for frontend check.
'label' => __( 'specific pages', 'advanced-ads' ),
'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_post_ids' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_post_ids' ], // callback for frontend check.
'label' => __( 'general conditions', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_general' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_general' ], // callback for frontend check.
'label' => __( 'author', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_author' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_author' ], // callback for frontend check.
// display ads only in content older or younger than a specific age.
'label' => __( 'content age', 'advanced-ads' ),
'description' => __( 'Display ads based on age of the page.', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_content_age' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_content_age' ], // callback for frontend check.
// condition for taxonomies in general.
'label' => __( 'taxonomy', 'advanced-ads' ),
'description' => __( 'Display ads based on the taxonomy of an archive page.', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_taxonomies' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_taxonomy' ], // callback for frontend check.
// register a condition for each taxonomy for posts.
$taxonomies = get_taxonomies(
'publicly_queryable' => true,
foreach ( $taxonomies as $_tax ) :
if ( in_array( $_tax->name, [ 'advanced_ads_groups' ], true ) ) {
* Count names of taxonomies and adjust label if there are duplicates.
* we can’t use `array_count_values` here because "label" might not always be a simple string (though it should be)
$tax_label_counts[ $_tax->label ] = isset( $tax_label_counts[ $_tax->label ] ) ? $tax_label_counts[ $_tax->label ] + 1 : $tax_label_counts[ $_tax->label ] = 1;
// add tax type to label if we find it multiple times.
if ( $tax_label_counts[ $_tax->label ] < 2 ) {
$archive_label = $_tax->label;
$label = sprintf( '%s (%s)', $_tax->label, $_tax->name );
$archive_label = sprintf( '%s (%s)', $_tax->labels->singular_name, $_tax->name );
$conditions[ 'taxonomy_' . $_tax->name ] = [
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_taxonomies' ], // callback for frontend check.
'taxonomy' => $_tax->name, // unique for this type: the taxonomy name.
$conditions[ 'archive_' . $_tax->name ] = [
// translators: %s is a label of an archive page.
__( 'archive: %s', 'advanced-ads' ),
'metabox' => [ 'Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms' ], // callback to generate the metabox.
'check' => [ 'Advanced_Ads_Display_Conditions', 'check_taxonomy_archive' ], // callback for frontend check.
'taxonomy' => $_tax->name, // unique for this type: the taxonomy name.
$this->conditions = apply_filters( 'advanced-ads-display-conditions', $conditions );
* Instance of Advanced_Ads_Display_Conditions
* @return Advanced_Ads_Display_Conditions
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
* Get the conditions array alphabetically by label
public function get_conditions() {
uasort( $this->conditions, 'Advanced_Ads_Admin::sort_condition_array_by_label' );
return $this->conditions;
* Controls frontend checks for conditions
* @param array $options options of the condition.
* @param mixed $ad false or Advanced_Ads_Ad object.
* @return bool false, if ad can’t be delivered
public static function frontend_check( $options = [], $ad = false ) {
$display_conditions = self::get_instance()->conditions;
if ( is_array( $options ) && isset( $options['type'] ) && isset( $display_conditions[ $options['type'] ]['check'] ) ) {
$check = $display_conditions[ $options['type'] ]['check'];
// call frontend check callback.
if ( method_exists( $check[0], $check[1] ) ) {
return call_user_func( [ $check[0], $check[1] ], $options, $ad );
* Render the list of set display conditions.
* @param array $set_conditions array of existing conditions.
* @param string $list_target ID of the list with the conditions.
* @param string $form_name prefix of the form field name attribute.
* @param array $options {
* Optional. Render options.
* @type string/array $in May be:
* - set to `all` (default) to include all conditions, including not global.
* - set to `global` to include all conditions, except for not global.
* - set to array of condition ID's to include only them.
public static function render_condition_list( array $set_conditions, $list_target = '', $form_name = '', $options = [] ) {
$conditions = self::get_instance()->get_conditions();
if ( isset( $options['in'] ) ) {
if ( 'global' === $options['in'] ) {
$conditions = array_filter(
function ( $condition ) {
return ! isset( $condition['options']['global'] ) || $condition['options']['global'];
} elseif ( is_array( $options['in'] ) ) {
// Include already set condition types.
foreach ( $set_conditions as $set_condition ) {
if ( isset( $set_condition['type'] ) ) {
$set_types[] = $set_condition['type'];
$in = array_merge( $options['in'], $set_types );
$conditions = array_intersect_key( $conditions, array_flip( $in ) );
// use default form name if not given explicitly.
// @todo create a random form name, in case we have more than one form per page and the parameter is not given.
$form_name = ! $form_name ? self::FORM_NAME : $form_name;
include ADVADS_ABSPATH . 'admin/views/conditions/display-conditions-list.php';
* @todo if needed, allow to disable the form to add new conditions
// add mockup conditions if add-ons are missing.
if ( ! defined( 'AAP_VERSION' ) ) {
$pro_conditions[] = __( 'parent page', 'advanced-ads' );
$pro_conditions[] = __( 'post meta', 'advanced-ads' );
$pro_conditions[] = __( 'page template', 'advanced-ads' );
$pro_conditions[] = __( 'url parameters', 'advanced-ads' );
if ( ! defined( 'AAR_VERSION' ) ) {
$pro_conditions[] = __( 'accelerated mobile pages', 'advanced-ads' );
asort( $pro_conditions );
// the action to call using AJAX.
$action = 'load_display_conditions_metabox';
$connector_default = 'or';
$empty_options = ! is_array( $set_conditions ) || ! count( $set_conditions );
include ADVADS_ABSPATH . 'admin/views/conditions/conditions-form.php';
* Render connector option
* @param int $index incremental index of the options.
* @param string $value connector value.
* @param string $form_name name of the form, falls back to class constant.
* @return string HTML for connector option.
public static function render_connector_option( $index, $value, $form_name ) {
$label = ( 'or' === $value ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' );
$name = self::get_form_name_with_index( $form_name, $index );
// create random value to identify the form field.
$rand = md5( $form_name );
"<input style='display:none' type='checkbox' name='%s[connector]' value='or' id='%s' %s><label for='%s'>%s</label>",
esc_attr( "advads-conditions-$index-connector-$rand" ),
checked( 'or', $value, false ),
esc_attr( "advads-conditions-$index-connector-$rand" ),
* @param string $type type of the current condition.
* @param string $name name of the form, falls back to class constant.
public static function render_type_field( $type, $name ) {
<input type="hidden" name="<?php echo esc_attr( $name ); ?>[type]" value="<?php echo esc_attr( $type ); ?>"/>
* Helper function to the name of a form field.
* @param string $form_name form name if submitted.
* @param int $index index of the condition.
public static function get_form_name_with_index( $form_name = '', $index = 0 ) {
return ! empty( $form_name ) ? $form_name . '[' . $index . ']' : self::FORM_NAME . '[' . $index . ']';
* Callback to display the metabox for the post type condition
* @param array $options options of the condition.
* @param int $index index of the condition.
* @param string $form_name name of the form, falls back to class constant.
public static function metabox_post_type( $options, $index = 0, $form_name = '' ) {
if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
$type_options = self::get_instance()->conditions;
if ( ! isset( $type_options[ $options['type'] ] ) ) {
// get values and select operator based on previous settings.
$operator = ( isset( $options['operator'] ) && 'is_not' === $options['operator'] ) ? 'is_not' : 'is';
$values = ( isset( $options['value'] ) && is_array( $options['value'] ) ) ? $options['value'] : [];
$name = self::get_form_name_with_index( $form_name, $index );
self::render_type_field( $options['type'], $name );
// load operator template.
include ADVADS_ABSPATH . 'admin/views/conditions/condition-operator.php';
$post_types = get_post_types(
'publicly_queryable' => true,
<div class="advads-conditions-single advads-buttonset">
$type_label_counts = array_count_values( wp_list_pluck( $post_types, 'label' ) );
foreach ( $post_types as $_type_id => $_type ) {
if ( in_array( $_type_id, $values, true ) ) {
if ( $type_label_counts[ $_type->label ] < 2 ) {
$_label = sprintf( '%s (%s)', $_type->label, $_type_id );
$field_id = "advads-conditions-$_type_id-$rand";
"<label class='button' for='%s'>%s</label><input type='checkbox' id='%s' name='%s' %s value='%s'>",
esc_attr( "{$name}[value][]" ),
checked( $_val, 1, false ),
include ADVADS_ABSPATH . 'admin/views/conditions/not-selected.php';
* Callback to display the metabox for the author condition
* @param array $options options of the condition.
* @param int $index index of the condition.
* @param string $form_name name of the form, falls back to class constant.
public static function metabox_author( $options, $index = 0, $form_name = '' ) {
if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
$type_options = self::get_instance()->conditions;
if ( ! isset( $type_options[ $options['type'] ] ) ) {
// get values and select operator based on previous settings.
$operator = ( isset( $options['operator'] ) && 'is_not' === $options['operator'] ) ? 'is_not' : 'is';
$values = ( isset( $options['value'] ) && is_array( $options['value'] ) ) ? $options['value'] : [];
$name = self::get_form_name_with_index( $form_name, $index );
self::render_type_field( $options['type'], $name );
// load operator template.
include ADVADS_ABSPATH . 'admin/views/conditions/condition-operator.php';
$max_authors = absint( apply_filters( 'advanced-ads-admin-max-terms', 50 ) );
if ( version_compare( get_bloginfo( 'version' ), '5.9' ) > -1 ) {
$args['capability'] = [ 'edit_posts' ];
$args['who'] = 'authors';
$authors = get_users( $args );
include ADVADS_ABSPATH . 'admin/views/conditions/condition-author.php';
* Callback to display the metabox for the taxonomy archive pages
* @param array $options options of the condition.
* @param int $index index of the condition.
* @param string $form_name name of the form, falls back to class constant.
public static function metabox_taxonomy_terms( $options, $index = 0, $form_name = '' ) {
if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
$type_options = self::get_instance()->conditions;
// don’t use if this is not a taxonomy.
if ( ! isset( $type_options[ $options['type'] ] ) || ! isset( $type_options[ $options['type'] ]['taxonomy'] ) ) {