: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Abstracts ad selection.
* The class allows to modify 'methods' (named callbacks) to provide ads
* through `advanced-ads-ad-select-methods` filter.
* This can be used to replace default methods, wrap them or add new ones.
* Further allows to provide ad selection attributes
* through `advanced-ads-ad-select-args` filter to influence behaviour of the
* Default methods have a `override` attribute that allows to replace the
* content. This may be used to defer or skip ad codes dynamically.
class Advanced_Ads_Select {
const PLACEMENT = 'placement';
const AD = 'id'; // alias of self::ID
private function __construct() {}
* @var Advanced_Ads_Select
private static $instance;
* @return Advanced_Ads_Select
public static function get_instance()
if ( ! isset(self::$instance) ) {
self::$instance = new self;
public function get_methods()
if ( ! isset($this->methods) ) {
self::AD => [ $this, 'get_ad_by_id' ],
self::GROUP => [ $this, 'get_ad_by_group' ],
self::PLACEMENT => [ $this, 'get_ad_by_placement' ],
$this->methods = apply_filters( 'advanced-ads-ad-select-methods', $methods );
* Advanced ad selection methods should not directly rely on
* current environment factors.
* Prior to actual ad selection the meta is provided to allow for
* serialised, proxied or otherwise defered selection workflows.
public function get_ad_arguments( $method, $id, $args = [] )
$args['previous_method'] = isset( $args['method'] ) ? $args['method'] : null;
$args['previous_id'] = isset( $args['id'] ) ? $args['id'] : null;
if ( $id || ! isset( $args['id'] ) ) $args['id'] = $id;
$args['method'] = $method;
$args = apply_filters( 'advanced-ads-ad-select-args', $args, $method, $id );
public function get_ad_by_method( $id, $method, $args = [] ) {
$methods = $this->get_methods();
if ( ! isset($methods[ $method ]) ) {
if ( ! advads_can_display_ads() ) {
$args = $this->get_ad_arguments( $method, $id, $args );
return call_user_func( $methods[ $method ], $args );
public function get_ad_by_id($args) {
if ( isset($args['override']) ) {
return $args['override'];
if ( ! isset($args['id']) || $args['id'] == 0 ) {
// We can't get the ad from the repository, this is the only instance where the arguments are passed in the constructor.
$ad = new Advanced_Ads_Ad( (int) $args['id'], $args );
if ( false !== ( $override = apply_filters( 'advanced-ads-ad-select-override-by-ad', false, $ad, $args ) ) ) {
if ( $ad->can_display() ) {
public function get_ad_by_group($args) {
if ( isset($args['override']) ) {
return $args['override'];
if ( ! isset($args['id']) || $args['id'] == 0 ) {
$adgroup = new Advanced_Ads_Group( $id, $args );
$ordered_ad_ids = $adgroup->get_ordered_ad_ids();
if ( false !== ( $override = apply_filters( 'advanced-ads-ad-select-override-by-group', false, $adgroup, $ordered_ad_ids, $args ) ) ) {
return $adgroup->output( $ordered_ad_ids );
public function get_ad_by_placement($args) {
if ( isset($args['override']) ) {
return $args['override'];
if ( ! isset($args['id']) || $args['id'] == '' ) {
if ( ! Advanced_Ads_Placements::can_display( $args['id'] ) ) {
return Advanced_Ads_Placements::output( $id, $args );