PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / frontend / class-ad-display-condition.php
advanced-ads / includes / frontend Last commit date
class-ad-display-condition.php 1 year ago class-ad-renderer.php 2 weeks ago class-debug-ads.php 1 year ago class-manager.php 1 year ago class-scripts.php 2 months ago class-stats.php 1 year ago
class-ad-display-condition.php
267 lines
1 <?php
2 /**
3 * This class is responsible for handling the display conditions of ads.
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 * @since 1.50.0
8 */
9
10 namespace AdvancedAds\Frontend;
11
12 use Advanced_Ads_Utils;
13 use AdvancedAds\Options;
14 use AdvancedAds\Utilities\WordPress;
15 use AdvancedAds\Utilities\Conditional;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Page display condition class.
21 */
22 class Ad_Display_Condition {
23 /**
24 * Identifier that supplement the disabled reason.
25 *
26 * @var int|string
27 */
28 public $disabled_id = '';
29
30 /**
31 * Reason why are disabled.
32 *
33 * @var string
34 */
35 public $disabled_reason = '';
36
37 /**
38 * Get the disabled id.
39 *
40 * @return int|string
41 */
42 public function get_disabled_id() {
43 return $this->disabled_id;
44 }
45
46 /**
47 * Get the reason why ads are disabled.
48 *
49 * @return string
50 */
51 public function get_disabled_reason(): string {
52 return $this->disabled_reason ?? '';
53 }
54
55 /**
56 * Runs checks and set disabled constants accordingly.
57 *
58 * @return void
59 */
60 public function run_checks(): void {
61 global $wp_the_query;
62
63 // Early bail!!
64 if ( Conditional::is_ad_disabled() ) {
65 return;
66 }
67
68 $is_rest = Conditional::is_rest_request();
69
70 $options = Options::instance()->get( 'advanced-ads' );
71 $checks = [
72 // Check if ads are disabled completely.
73 'all' => [
74 'check' => ! $is_rest && ! is_feed() && ! empty( $options['disabled-ads']['all'] ),
75 'args' => [ 'all' ],
76 ],
77 // Check if ads are disabled in REST API.
78 'rest' => [
79 'check' => $is_rest && ! empty( $options['disabled-ads']['rest-api'] ),
80 'args' => [ 'rest-api' ],
81 ],
82 // Check if ads are disabled from 404 pages.
83 'error404' => [
84 'check' => $wp_the_query->is_404() && ! empty( $options['disabled-ads']['404'] ),
85 'args' => [ '404' ],
86 ],
87 // Check if ads are disabled from non-singular frontend pages (often = archives).
88 'archive' => [
89 'check' => ! is_feed() && ! $is_rest && ! $wp_the_query->is_singular() && ! empty( $options['disabled-ads']['archives'] ),
90 'args' => [ 'archive' ],
91 ],
92 // Check if ads are disabled in Feed.
93 'feed' => [
94 'check' => $wp_the_query->is_feed() && ( $options['disabled-ads']['feed'] ?? false ),
95 'args' => [ 'feed' ],
96 ],
97 'current_page' => [ $this, 'check_current_page' ],
98 'posts_page' => [ $this, 'check_posts_page' ],
99 'shop' => [ $this, 'check_shop' ],
100 'user_roles' => [ $this, 'check_user_roles' ],
101 'bots' => [ $this, 'check_bots' ],
102 'ip_addresses' => [ $this, 'check_ip_addresses' ],
103 ];
104
105 /**
106 * Allows experienced user to customize the rules that disable ads on a page
107 *
108 * @param array $checks list of the rules that will be checked.
109 * @param array $options plugin options.
110 */
111 $checks = apply_filters( 'advanced-ads-ad-display-check', $checks, $options );
112
113 if ( ! is_array( $checks ) ) {
114 $checks = [];
115 }
116
117 foreach ( $checks as $check ) {
118 if ( isset( $check['check'] ) && $check['check'] ) {
119 $this->disable_ads( ...$check['args'] );
120 return;
121 }
122
123 if ( is_callable( $check ) ) {
124 $truthiness = call_user_func( $check );
125 if ( $truthiness ) {
126 return;
127 }
128 }
129 }
130 }
131
132 /**
133 * Check if ads are disabled on the current page.
134 *
135 * @return bool
136 */
137 private function check_current_page(): bool {
138 global $post, $wp_the_query;
139
140 if ( $wp_the_query->is_singular() && isset( $post->ID ) ) {
141 $settings = get_post_meta( $post->ID, '_advads_ad_settings', true );
142
143 if ( ! empty( $settings['disable_ads'] ) ) {
144 $this->disable_ads( 'page', $post->ID );
145 return true;
146 }
147 }
148
149 return false;
150 }
151
152 /**
153 * Check if ads are disabled on "Posts page" set on the WordPress Reading settings page.
154 *
155 * @return bool
156 */
157 private function check_posts_page(): bool {
158 global $wp_the_query;
159
160 if ( $wp_the_query->is_posts_page ) {
161 $settings = get_post_meta( $wp_the_query->queried_object_id, '_advads_ad_settings', true );
162
163 if ( ! empty( $settings['disable_ads'] ) ) {
164 $this->disable_ads( 'page', $wp_the_query->queried_object_id );
165 return true;
166 }
167 }
168
169 return false;
170 }
171
172 /**
173 * Check if ads are disabled on WooCommerce shop page (and currently on shop page).
174 *
175 * @return bool
176 */
177 private function check_shop(): bool {
178 if ( function_exists( 'is_shop' ) && is_shop() ) {
179 $shop_id = wc_get_page_id( 'shop' );
180 $settings = get_post_meta( $shop_id, '_advads_ad_settings', true );
181 if ( ! empty( $settings['disable_ads'] ) ) {
182 $this->disable_ads( 'page', $shop_id );
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 /**
191 * Check if ads are disabled for current user role.
192 *
193 * @return bool
194 */
195 private function check_user_roles(): bool {
196 $options = Options::instance()->get( 'advanced-ads' );
197 $current_user = wp_get_current_user();
198 $hide_for_roles = isset( $options['hide-for-user-role'] )
199 ? Advanced_Ads_Utils::maybe_translate_cap_to_role( $options['hide-for-user-role'] )
200 : [];
201
202 if (
203 $hide_for_roles && is_user_logged_in() && is_array( $current_user->roles ) &&
204 array_intersect( $hide_for_roles, $current_user->roles )
205 ) {
206 $this->disable_ads( 'user-role' );
207 return true;
208 }
209
210 return false;
211 }
212
213 /**
214 * Check if ads are disabled for bots.
215 *
216 * @return bool
217 */
218 private function check_bots(): bool {
219 $options = Options::instance()->get( 'advanced-ads' );
220 if (
221 isset( $options['block-bots'] ) && $options['block-bots'] &&
222 ! WordPress::is_cache_bot() && Conditional::is_ua_bot()
223 ) {
224 $this->disable_ads();
225 return true;
226 }
227
228 return false;
229 }
230
231 /**
232 * Check if ads are disabled for IP addresses.
233 *
234 * @return bool
235 */
236 private function check_ip_addresses(): bool {
237 $options = Options::instance()->get( 'advanced-ads' );
238
239 if ( isset( $options['hide-for-ip-address']['enabled'] ) ) {
240 $ip_addresses = isset( $options['hide-for-ip-address']['ips'] )
241 ? explode( "\n", $options['hide-for-ip-address']['ips'] )
242 : [];
243 $ip_addresses = array_map( 'trim', $ip_addresses );
244 $user_ip = get_user_ip_address();
245
246 if ( $user_ip && ! empty( $ip_addresses ) && in_array( $user_ip, $ip_addresses, true ) ) {
247 $this->disable_ads( 'ip-address' );
248 return true;
249 }
250 }
251
252 return false;
253 }
254
255 /**
256 * Disable ads.
257 *
258 * @param string $reason Reason why are disabled.
259 * @param int|string $id Identifier that supplement the disabled reason.
260 */
261 private function disable_ads( $reason = null, $id = null ) {
262 $this->disabled_id = $id;
263 $this->disabled_reason = $reason;
264 define( 'ADVADS_ADS_DISABLED', true );
265 }
266 }
267