PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.16.1
Advanced Ads – Ad Manager & AdSense v1.16.1
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 / classes / visitor-conditions.php
advanced-ads / classes Last commit date
EDD_SL_Plugin_Updater.php 6 years ago ad-ajax.php 6 years ago ad-debug.php 8 years ago ad-health-notices.php 6 years ago ad-model.php 8 years ago ad-select.php 9 years ago ad.php 6 years ago ad_ajax_callbacks.php 6 years ago ad_group.php 6 years ago ad_placements.php 6 years ago ad_type_abstract.php 8 years ago ad_type_content.php 6 years ago ad_type_dummy.php 6 years ago ad_type_group.php 8 years ago ad_type_image.php 6 years ago ad_type_plain.php 6 years ago checks.php 6 years ago compatibility.php 6 years ago display-conditions.php 6 years ago filesystem.php 8 years ago frontend_checks.php 6 years ago plugin.php 6 years ago upgrades.php 6 years ago utils.php 6 years ago visitor-conditions.php 6 years ago widget.php 6 years ago
visitor-conditions.php
460 lines
1 <?php
2
3 /**
4 * Visitor conditions under which to (not) show an ad
5 *
6 * @since 1.5.4
7 */
8 class Advanced_Ads_Visitor_Conditions {
9
10 /**
11 * Instance of Advanced_Ads_Visitor_Conditions
12 *
13 * @var Advanced_Ads_Visitor_Conditions
14 */
15 protected static $instance;
16
17 /**
18 * Registered visitor conditions
19 *
20 * @var array $conditions
21 */
22 public $conditions;
23
24 /**
25 * Start of name in form elements
26 */
27 const FORM_NAME = 'advanced_ad[visitors]';
28
29 /**
30 * Advanced_Ads_Visitor_Conditions constructor
31 */
32 public function __construct() {
33
34 // register conditions.
35 $this->conditions = apply_filters(
36 'advanced-ads-visitor-conditions',
37 array(
38 'mobile' => array(
39 // type of the condition.
40 'label' => __( 'device', 'advanced-ads' ),
41 'description' => __( 'Display ads only on mobile devices or hide them.', 'advanced-ads' ),
42 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'mobile_is_or_not' ), // callback to generate the metabox.
43 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_mobile' ), // callback for frontend check.
44 'helplink' => ADVADS_URL . 'manual/display-ads-either-on-mobile-or-desktop/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor-mobile', // link to help section.
45 ),
46 'loggedin' => array(
47 'label' => __( 'logged-in visitor', 'advanced-ads' ),
48 'description' => __( 'Whether the visitor has to be logged in or not in order to see the ads.', 'advanced-ads' ),
49 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'metabox_is_or_not' ), // callback to generate the metabox.
50 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_logged_in' ), // callback for frontend check.
51 'passive_info' => array(
52 'hash_fields' => null,
53 'remove' => 'login',
54 'function' => 'is_user_logged_in',
55 ),
56 ),
57 )
58 );
59 }
60
61 /**
62 * Load instance of Advanced_Ads_Visitor_Conditions
63 *
64 * @return Advanced_Ads_Visitor_Conditions
65 */
66 public static function get_instance() {
67 // If the single instance hasn't been set, set it now.
68 if ( null === self::$instance ) {
69 self::$instance = new self();
70 }
71
72 return self::$instance;
73 }
74
75
76 /**
77 * Get the conditions array alphabetically by label
78 *
79 * @since 1.8.12
80 */
81 public function get_conditions() {
82 uasort( $this->conditions, 'Advanced_Ads_Admin::sort_condition_array_by_label' );
83
84 return $this->conditions;
85 }
86
87 /**
88 * Callback to render the mobile condition using the "is not" condition
89 *
90 * @param array $options options of the condition.
91 * @param int $index index of the condition.
92 * @param string $form_name name of the form, falls back to class constant.
93 */
94 public static function mobile_is_or_not( $options, $index = 0, $form_name = '' ) {
95
96 if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
97 return;
98 }
99
100 $type_options = self::get_instance()->conditions;
101
102 if ( ! isset( $type_options[ $options['type'] ] ) ) {
103 return;
104 }
105
106 // form name basis.
107 $name = self::get_form_name_with_index( $form_name, $index );
108
109 // options.
110 $operator = isset( $options['operator'] ) ? $options['operator'] : 'is';
111
112 include ADVADS_BASE_PATH . 'admin/views/conditions/condition-device.php';
113
114 if ( ! defined( 'AAR_SLUG' ) ) {
115 // translators: %s is a URL. Please don’t change it.
116 echo '<p>' . sprintf( __( 'Display ads by the available space on the device or target tablets with the <a href="%s" target="_blank">Responsive add-on</a>', 'advanced-ads' ), ADVADS_URL . 'add-ons/responsive-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor-responsive' ) . '</p>';
117 }
118 }
119
120 /**
121 * Callback to display the "is not" condition
122 *
123 * @param array $options options of the condition.
124 * @param int $index index of the condition.
125 * @param string $form_name name of the form, falls back to class constant.
126 */
127 public static function metabox_is_or_not( $options, $index = 0, $form_name = '' ) {
128
129 if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
130 return;
131 }
132
133 $type_options = self::get_instance()->conditions;
134
135 if ( ! isset( $type_options[ $options['type'] ] ) ) {
136 return;
137 }
138
139 // form name basis.
140 $name = self::get_form_name_with_index( $form_name, $index );
141
142 // options.
143 $operator = isset( $options['operator'] ) ? $options['operator'] : 'is';
144
145 include ADVADS_BASE_PATH . 'admin/views/conditions/condition-is-or-not.php';
146 }
147
148 /**
149 * Callback to display the any condition based on a number
150 *
151 * @param array $options options of the condition.
152 * @param int $index index of the condition.
153 * @param string $form_name name of the form, falls back to class constant.
154 */
155 public static function metabox_number( $options, $index = 0, $form_name = '' ) {
156
157 if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
158 return;
159 }
160
161 $type_options = self::get_instance()->conditions;
162
163 if ( ! isset( $type_options[ $options['type'] ] ) ) {
164 return;
165 }
166
167 // form name basis.
168 $name = self::get_form_name_with_index( $form_name, $index );
169
170 // options.
171 $value = isset( $options['value'] ) ? $options['value'] : 0;
172 $operator = isset( $options['operator'] ) ? $options['operator'] : 'is_equal';
173
174 include ADVADS_BASE_PATH . 'admin/views/conditions/condition-number.php';
175 }
176
177 /**
178 * Callback to display the any condition based on a number
179 *
180 * @param array $options options of the condition.
181 * @param int $index index of the condition.
182 * @param string $form_name name of the form, falls back to class constant.
183 */
184 public static function metabox_string( $options, $index = 0, $form_name = '' ) {
185
186 if ( ! isset( $options['type'] ) || '' === $options['type'] ) {
187 return;
188 }
189
190 $type_options = self::get_instance()->conditions;
191
192 if ( ! isset( $type_options[ $options['type'] ] ) ) {
193 return;
194 }
195
196 // form name basis.
197 $name = self::get_form_name_with_index( $form_name, $index );
198
199 // options.
200 $value = isset( $options['value'] ) ? $options['value'] : '';
201 $operator = isset( $options['operator'] ) ? $options['operator'] : 'contains';
202
203 include ADVADS_BASE_PATH . 'admin/views/conditions/condition-string.php';
204 }
205
206 /**
207 * Controls frontend checks for conditions
208 *
209 * @param array $options options of the condition.
210 * @param object $ad Advanced_Ads_Ad.
211 *
212 * @return bool false, if ad can’t be delivered
213 */
214 public static function frontend_check( $options = array(), $ad = false ) {
215 $visitor_conditions = self::get_instance()->conditions;
216
217 if ( is_array( $options ) && isset( $visitor_conditions[ $options['type'] ]['check'] ) ) {
218 $check = $visitor_conditions[ $options['type'] ]['check'];
219 } else {
220 return true;
221 }
222
223 // call frontend check callback.
224 if ( method_exists( $check[0], $check[1] ) ) {
225 return call_user_func( array( $check[0], $check[1] ), $options, $ad );
226 }
227
228 return true;
229 }
230
231 /**
232 * Render the list of set visisor conditions
233 *
234 * @param array $set_conditions array of existing conditions.
235 * @param string $list_target ID of the list with the conditions.
236 * @param string $form_name prefix of the form.
237 */
238 public static function render_condition_list( array $set_conditions, $list_target = '', $form_name = '' ) {
239
240 $conditions = self::get_instance()->get_conditions();
241
242 // use default form name if not given explicitly.
243 // @todo create a random form name, in case we have more than one form per page and the parameter is not given.
244 $form_name = ! $form_name ? self::FORM_NAME : $form_name;
245
246 include ADVADS_BASE_PATH . 'admin/views/conditions/visitor-conditions-list.php';
247
248 /**
249 * Prepare condition form
250 *
251 * @todo if needed, allow to disable the form to add new conditions
252 */
253
254 // add mockup conditions if add-ons are missing.
255 $pro_conditions = array();
256 if ( ! defined( 'AAP_VERSION' ) ) {
257 $pro_conditions[] = __( 'browser language', 'advanced-ads' );
258 $pro_conditions[] = __( 'cookie', 'advanced-ads' );
259 $pro_conditions[] = __( 'max. ad clicks', 'advanced-ads' );
260 $pro_conditions[] = __( 'max. ad impressions', 'advanced-ads' );
261 $pro_conditions[] = __( 'new visitor', 'advanced-ads' );
262 $pro_conditions[] = __( 'page impressions', 'advanced-ads' );
263 $pro_conditions[] = __( 'referrer url', 'advanced-ads' );
264 $pro_conditions[] = __( 'user agent', 'advanced-ads' );
265 $pro_conditions[] = __( 'user can (capabilities)', 'advanced-ads' );
266 $pro_conditions[] = __( 'user role', 'advanced-ads' );
267 }
268 if ( ! defined( 'AAGT_VERSION' ) ) {
269 $pro_conditions[] = __( 'geo location', 'advanced-ads' );
270 }
271 if ( ! defined( 'AAR_VERSION' ) ) {
272 $pro_conditions[] = __( 'browser width', 'advanced-ads' );
273 }
274 asort( $pro_conditions );
275
276 // the action to call using AJAX.
277 $action = 'load_visitor_conditions_metabox';
278 $connector_default = 'and';
279
280 include ADVADS_BASE_PATH . 'admin/views/conditions/visitor-conditions-form-top.php';
281 include ADVADS_BASE_PATH . 'admin/views/conditions/conditions-form.php';
282 }
283
284 /**
285 * Render connector option
286 *
287 * @param int $index incremental index of the options.
288 * @param string $value connector value.
289 * @param string $form_name name of the form, falls back to class constant.
290 *
291 * @return string HTML of the connector option
292 * @todo combine this with the same function used for Display Conditions
293 *
294 * @since 1.7.0.4
295 */
296 public static function render_connector_option( $index = 0, $value = 'or', $form_name ) {
297
298 $label = ( 'or' === $value ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' );
299
300 $name = self::get_form_name_with_index( $form_name, $index );
301
302 // create random value to identify the form field.
303 $rand = md5( $form_name );
304
305 return '<input type="checkbox" name="' . $name . '[connector]' . '" value="or" id="advads-conditions-' .
306 $index . '-connector-' . $rand . '"' .
307 checked( 'or', $value, false )
308 . '><label for="advads-conditions-' . $index . '-connector-' . $rand . '">' . $label . '</label>';
309 }
310
311 /**
312 * Helper function to the name of a form field.
313 * falls back to default
314 *
315 * @param string $form_name form name if submitted.
316 * @param int $index index of the condition.
317 *
318 * @return string
319 */
320 public static function get_form_name_with_index( $form_name = '', $index = 0 ) {
321 return ! empty( $form_name ) ? $form_name . '[' . $index . ']' : self::FORM_NAME . '[' . $index . ']';
322 }
323
324 /**
325 * Check mobile visitor condition in frontend
326 *
327 * @param array $options options of the condition.
328 *
329 * @return bool true if can be displayed
330 */
331 public static function check_mobile( $options = array() ) {
332
333 if ( ! isset( $options['operator'] ) ) {
334 return true;
335 }
336
337 switch ( $options['operator'] ) {
338 case 'is':
339 if ( ! wp_is_mobile() ) {
340 return false;
341 }
342 break;
343 case 'is_not':
344 if ( wp_is_mobile() ) {
345 return false;
346 }
347 break;
348 }
349
350 return true;
351 }
352
353 /**
354 * Check mobile visitor condition in frontend
355 *
356 * @param array $options options of the condition.
357 *
358 * @return bool true if can be displayed
359 * @since 1.6.3
360 */
361 public static function check_logged_in( $options = array() ) {
362
363 if ( ! isset( $options['operator'] ) ) {
364 return true;
365 }
366
367 switch ( $options['operator'] ) {
368 case 'is':
369 if ( ! is_user_logged_in() ) {
370 return false;
371 }
372 break;
373 case 'is_not':
374 if ( is_user_logged_in() ) {
375 return false;
376 }
377 break;
378 }
379
380 return true;
381 }
382
383 /**
384 * Helper for check with strings
385 *
386 * @param string $string string that is going to be checked.
387 * @param array $options options of this condition.
388 *
389 * @return bool true if ad can be displayed
390 * @since 1.6.3
391 */
392 public static function helper_check_string( $string = '', $options = array() ) {
393
394 if ( ! isset( $options['operator'] ) || ! isset( $options['value'] ) || '' === $options['value'] ) {
395 return true;
396 }
397
398 $operator = $options['operator'];
399 $value = $options['value'];
400
401 // check the condition by mode and bool.
402 $condition = true;
403 switch ( $operator ) {
404 // referrer contains string on any position.
405 case 'contain':
406 $condition = stripos( $string, $value ) !== false;
407 break;
408 // referrer does not contain string on any position.
409 case 'contain_not':
410 $condition = stripos( $string, $value ) === false;
411 break;
412 // referrer starts with the string.
413 case 'start':
414 $condition = stripos( $string, $value ) === 0;
415 break;
416 // referrer does not start with the string.
417 case 'start_not':
418 $condition = stripos( $string, $value ) !== 0;
419 break;
420 // referrer ends with the string.
421 case 'end':
422 $condition = substr( $string, - strlen( $value ) ) === $value;
423 break;
424 // referrer does not end with the string.
425 case 'end_not':
426 $condition = substr( $string, - strlen( $value ) ) !== $value;
427 break;
428 // referrer is equal to the string.
429 case 'match':
430 // strings do match, but should not or not match but should.
431 $condition = strcasecmp( $value, $string ) === 0;
432 break;
433 // referrer is not equal to the string.
434 case 'match_not':
435 // strings do match, but should not or not match but should.
436 $condition = strcasecmp( $value, $string ) !== 0;
437 break;
438 // string is a regular expression.
439 case 'regex':
440 // check regular expression first.
441 if ( @preg_match( $value, null ) === false ) {
442 Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." );
443 } else {
444 $condition = preg_match( $value, $string );
445 }
446 break;
447 // string is not a regular expression.
448 case 'regex_not':
449 if ( @preg_match( $value, null ) === false ) {
450 Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." );
451 } else {
452 $condition = ! preg_match( $value, $string );
453 }
454 break;
455 }
456
457 return $condition;
458 }
459 }
460