EDD_SL_Plugin_Updater.php
4 years ago
ad-ajax.php
5 years ago
ad-debug.php
6 years ago
ad-expiration.php
4 years ago
ad-health-notices.php
5 years ago
ad-model.php
5 years ago
ad-select.php
9 years ago
ad.php
4 years ago
ad_ajax_callbacks.php
5 years ago
ad_group.php
4 years ago
ad_placements.php
4 years ago
ad_type_abstract.php
5 years ago
ad_type_content.php
5 years ago
ad_type_dummy.php
5 years ago
ad_type_group.php
5 years ago
ad_type_image.php
5 years ago
ad_type_plain.php
4 years ago
checks.php
4 years ago
compatibility.php
4 years ago
display-conditions.php
4 years ago
filesystem.php
8 years ago
frontend-notices.php
6 years ago
frontend_checks.php
4 years ago
in-content-injector.php
4 years ago
inline-css.php
4 years ago
plugin.php
4 years ago
upgrades.php
6 years ago
utils.php
4 years ago
visitor-conditions.php
4 years ago
widget.php
4 years ago
ad-select.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Abstracts ad selection. |
| 5 | * |
| 6 | * The class allows to modify 'methods' (named callbacks) to provide ads |
| 7 | * through `advanced-ads-ad-select-methods` filter. |
| 8 | * This can be used to replace default methods, wrap them or add new ones. |
| 9 | * |
| 10 | * Further allows to provide ad selection attributes |
| 11 | * through `advanced-ads-ad-select-args` filter to influence behaviour of the |
| 12 | * selection method. |
| 13 | * Default methods have a `override` attribute that allows to replace the |
| 14 | * content. This may be used to defer or skip ad codes dynamically. |
| 15 | * |
| 16 | * @since 1.5.0 |
| 17 | */ |
| 18 | class Advanced_Ads_Select { |
| 19 | |
| 20 | const PLACEMENT = 'placement'; |
| 21 | const GROUP = 'group'; |
| 22 | const AD = 'id'; // alias of self::ID |
| 23 | const ID = 'id'; |
| 24 | |
| 25 | protected $methods; |
| 26 | |
| 27 | private function __construct() {} |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @var Advanced_Ads_Select |
| 32 | */ |
| 33 | private static $instance; |
| 34 | |
| 35 | /** |
| 36 | * |
| 37 | * @return Advanced_Ads_Select |
| 38 | */ |
| 39 | public static function get_instance() |
| 40 | { |
| 41 | if ( ! isset(self::$instance) ) { |
| 42 | self::$instance = new self; |
| 43 | } |
| 44 | |
| 45 | return self::$instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public function get_methods() |
| 53 | { |
| 54 | if ( ! isset($this->methods) ) { |
| 55 | $methods = array( |
| 56 | self::AD => array( $this, 'get_ad_by_id' ), |
| 57 | self::GROUP => array( $this, 'get_ad_by_group' ), |
| 58 | self::PLACEMENT => array( $this, 'get_ad_by_placement' ), |
| 59 | ); |
| 60 | |
| 61 | $this->methods = apply_filters( 'advanced-ads-ad-select-methods', $methods ); |
| 62 | } |
| 63 | |
| 64 | return $this->methods; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Advanced ad selection methods should not directly rely on |
| 69 | * current environment factors. |
| 70 | * Prior to actual ad selection the meta is provided to allow for |
| 71 | * serialised, proxied or otherwise defered selection workflows. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function get_ad_arguments( $method, $id, $args = array() ) |
| 76 | { |
| 77 | $args = (array) $args; |
| 78 | |
| 79 | $args['previous_method'] = isset( $args['method'] ) ? $args['method'] : null; |
| 80 | $args['previous_id'] = isset( $args['id'] ) ? $args['id'] : null; |
| 81 | |
| 82 | if ( $id || ! isset( $args['id'] ) ) $args['id'] = $id; |
| 83 | $args['method'] = $method; |
| 84 | |
| 85 | $args = apply_filters( 'advanced-ads-ad-select-args', $args, $method, $id ); |
| 86 | |
| 87 | return $args; |
| 88 | } |
| 89 | |
| 90 | public function get_ad_by_method( $id, $method, $args = array() ) { |
| 91 | |
| 92 | $methods = $this->get_methods(); |
| 93 | if ( ! isset($methods[ $method ]) ) { |
| 94 | return ; |
| 95 | } |
| 96 | if ( ! advads_can_display_ads() ) { |
| 97 | return ; |
| 98 | } |
| 99 | $args = $this->get_ad_arguments( $method, $id, $args ); |
| 100 | |
| 101 | return call_user_func( $methods[ $method ], $args ); |
| 102 | } |
| 103 | |
| 104 | // internal |
| 105 | public function get_ad_by_id($args) { |
| 106 | if ( isset($args['override']) ) { |
| 107 | return $args['override']; |
| 108 | } |
| 109 | if ( ! isset($args['id']) || $args['id'] == 0 ) { |
| 110 | return ; |
| 111 | } |
| 112 | |
| 113 | // get ad |
| 114 | $ad = new Advanced_Ads_Ad( (int) $args['id'], $args ); |
| 115 | |
| 116 | if ( false !== ( $override = apply_filters( 'advanced-ads-ad-select-override-by-ad', false, $ad, $args ) ) ) { |
| 117 | return $override; |
| 118 | } |
| 119 | |
| 120 | // check conditions |
| 121 | if ( $ad->can_display() ) { |
| 122 | return $ad->output(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // internal |
| 127 | public function get_ad_by_group($args) { |
| 128 | if ( isset($args['override']) ) { |
| 129 | return $args['override']; |
| 130 | } |
| 131 | if ( ! isset($args['id']) || $args['id'] == 0 ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // get ad |
| 136 | $id = (int) $args['id']; |
| 137 | $adgroup = new Advanced_Ads_Group( $id, $args ); |
| 138 | $ordered_ad_ids = $adgroup->get_ordered_ad_ids(); |
| 139 | |
| 140 | if ( false !== ( $override = apply_filters( 'advanced-ads-ad-select-override-by-group', false, $adgroup, $ordered_ad_ids, $args ) ) ) { |
| 141 | return $override; |
| 142 | } |
| 143 | |
| 144 | return $adgroup->output( $ordered_ad_ids ); |
| 145 | } |
| 146 | |
| 147 | // internal |
| 148 | public function get_ad_by_placement($args) { |
| 149 | if ( isset($args['override']) ) { |
| 150 | return $args['override']; |
| 151 | } |
| 152 | if ( ! isset($args['id']) || $args['id'] == '' ) { |
| 153 | return ; |
| 154 | } |
| 155 | |
| 156 | // check conditions |
| 157 | if ( ! Advanced_Ads_Placements::can_display( $args['id'] ) ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // get placement content |
| 162 | $id = $args['id']; |
| 163 | return Advanced_Ads_Placements::output( $id, $args ); |
| 164 | } |
| 165 | } |
| 166 |