Ad_Repository.php
1 year ago
Group_Repository.php
1 year ago
ad-ajax.php
1 year ago
ad-debug.php
1 year ago
ad-expiration.php
1 year ago
ad-model.php
1 week ago
ad-select.php
1 year ago
ad.php
1 year ago
ad_group.php
1 year ago
ad_placements.php
1 year ago
ad_type_abstract.php
1 year ago
ad_type_content.php
1 year ago
ad_type_dummy.php
1 year ago
ad_type_group.php
1 year ago
ad_type_image.php
1 year ago
ad_type_plain.php
1 year ago
class-admin.php
1 year ago
class-advanced-ads-plugin.php
1 year ago
gadsense-dummy.php
1 year ago
in-content-injector.php
2 months ago
placements-ad-label-position.php
1 year ago
ad-select.php
151 lines
| 1 | <?php // phpcs:ignoreFilename |
| 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'; |
| 23 | |
| 24 | /** |
| 25 | * Hold methods |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $methods; |
| 30 | |
| 31 | /** |
| 32 | * Get instance of this class. |
| 33 | * |
| 34 | * @return Advanced_Ads_Select |
| 35 | */ |
| 36 | public static function get_instance() { |
| 37 | static $instance; |
| 38 | if ( ! isset( $instance ) ) { |
| 39 | $instance = new self(); |
| 40 | } |
| 41 | |
| 42 | return $instance; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get ad selection methods. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public function get_methods() { |
| 51 | if ( ! isset( $this->methods ) ) { |
| 52 | $methods = [ |
| 53 | self::AD => [ $this, 'get_ad_by_id' ], |
| 54 | self::GROUP => [ $this, 'get_ad_by_group' ], |
| 55 | self::PLACEMENT => [ $this, 'get_ad_by_placement' ], |
| 56 | ]; |
| 57 | |
| 58 | $this->methods = apply_filters( 'advanced-ads-ad-select-methods', $methods ); |
| 59 | } |
| 60 | |
| 61 | return $this->methods; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Advanced ad selection methods should not directly rely on |
| 66 | * current environment factors. |
| 67 | * Prior to actual ad selection the meta is provided to allow for |
| 68 | * serialised, proxied or otherwise defered selection workflows. |
| 69 | * |
| 70 | * @param string $method Ad selection method. |
| 71 | * @param int $id Ad ID. |
| 72 | * @param array $args Ad selection arguments. |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | public function get_ad_arguments( $method, $id, $args = [] ) { |
| 77 | $args = (array) $args; |
| 78 | |
| 79 | $args['previous_id'] = $args['id'] ?? null; |
| 80 | $args['previous_method'] = $args['method'] ?? null; |
| 81 | |
| 82 | if ( $id || ! isset( $args['id'] ) ) { |
| 83 | $args['id'] = $id; |
| 84 | } |
| 85 | |
| 86 | $args['method'] = $method; |
| 87 | |
| 88 | $args = apply_filters( 'advanced-ads-ad-select-args', $args, $method, $id ); |
| 89 | |
| 90 | return $args; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get ad by method. |
| 95 | * |
| 96 | * @param int $id Ad ID. |
| 97 | * @param string $method Ad selection method. |
| 98 | * @param array $args Ad selection arguments. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function get_ad_by_method( $id, $method, $args = [] ) { |
| 103 | $methods = $this->get_methods(); |
| 104 | |
| 105 | if ( ! isset( $methods[ $method ] ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if ( ! advads_can_display_ads() ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $args = $this->get_ad_arguments( $method, $id, $args ); |
| 114 | |
| 115 | return call_user_func( $methods[ $method ], $args ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get ad by ID. |
| 120 | * |
| 121 | * @param array $args Ad selection arguments. |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public function get_ad_by_id( $args ) { |
| 126 | return get_the_ad( $args['id'], '', $args ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get ad by group. |
| 131 | * |
| 132 | * @param array $args Ad selection arguments. |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public function get_ad_by_group( $args ) { |
| 137 | return get_the_group( $args['id'], '', $args ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get ad by placement. |
| 142 | * |
| 143 | * @param array $args Ad selection arguments. |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public function get_ad_by_placement( $args ) { |
| 148 | return get_the_placement( $args['id'], '', $args ); |
| 149 | } |
| 150 | } |
| 151 |