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
visitor-conditions.php
473 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 | ?><p><?php |
| 116 | sprintf( |
| 117 | wp_kses( |
| 118 | // translators: %s is a URL. Please don’t change it. |
| 119 | __( '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' ), |
| 120 | array( |
| 121 | 'a' => array( |
| 122 | 'href' => array(), |
| 123 | 'target' => array(), |
| 124 | ), |
| 125 | ) |
| 126 | ), |
| 127 | ADVADS_URL . 'add-ons/responsive-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor-responsive' |
| 128 | ); |
| 129 | ?></p><?php |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Callback to display the "is not" condition |
| 135 | * |
| 136 | * @param array $options options of the condition. |
| 137 | * @param int $index index of the condition. |
| 138 | * @param string $form_name name of the form, falls back to class constant. |
| 139 | */ |
| 140 | public static function metabox_is_or_not( $options, $index = 0, $form_name = '' ) { |
| 141 | |
| 142 | if ( ! isset( $options['type'] ) || '' === $options['type'] ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | $type_options = self::get_instance()->conditions; |
| 147 | |
| 148 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // form name basis. |
| 153 | $name = self::get_form_name_with_index( $form_name, $index ); |
| 154 | |
| 155 | // options. |
| 156 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'is'; |
| 157 | |
| 158 | include ADVADS_BASE_PATH . 'admin/views/conditions/condition-is-or-not.php'; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Callback to display the any condition based on a number |
| 163 | * |
| 164 | * @param array $options options of the condition. |
| 165 | * @param int $index index of the condition. |
| 166 | * @param string $form_name name of the form, falls back to class constant. |
| 167 | */ |
| 168 | public static function metabox_number( $options, $index = 0, $form_name = '' ) { |
| 169 | |
| 170 | if ( ! isset( $options['type'] ) || '' === $options['type'] ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | $type_options = self::get_instance()->conditions; |
| 175 | |
| 176 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // form name basis. |
| 181 | $name = self::get_form_name_with_index( $form_name, $index ); |
| 182 | |
| 183 | // options. |
| 184 | $value = isset( $options['value'] ) ? $options['value'] : 0; |
| 185 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'is_equal'; |
| 186 | |
| 187 | include ADVADS_BASE_PATH . 'admin/views/conditions/condition-number.php'; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Callback to display the any condition based on a number |
| 192 | * |
| 193 | * @param array $options options of the condition. |
| 194 | * @param int $index index of the condition. |
| 195 | * @param string $form_name name of the form, falls back to class constant. |
| 196 | */ |
| 197 | public static function metabox_string( $options, $index = 0, $form_name = '' ) { |
| 198 | |
| 199 | if ( ! isset( $options['type'] ) || '' === $options['type'] ) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | $type_options = self::get_instance()->conditions; |
| 204 | |
| 205 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // form name basis. |
| 210 | $name = self::get_form_name_with_index( $form_name, $index ); |
| 211 | |
| 212 | // options. |
| 213 | $value = isset( $options['value'] ) ? $options['value'] : ''; |
| 214 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'contains'; |
| 215 | |
| 216 | include ADVADS_BASE_PATH . 'admin/views/conditions/condition-string.php'; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Controls frontend checks for conditions |
| 221 | * |
| 222 | * @param array $options options of the condition. |
| 223 | * @param bool|object $ad false or Advanced_Ads_Ad. |
| 224 | * |
| 225 | * @return bool false, if ad can’t be delivered |
| 226 | */ |
| 227 | public static function frontend_check( $options = array(), $ad = false ) { |
| 228 | $visitor_conditions = self::get_instance()->conditions; |
| 229 | |
| 230 | if ( is_array( $options ) && isset( $visitor_conditions[ $options['type'] ]['check'] ) ) { |
| 231 | $check = $visitor_conditions[ $options['type'] ]['check']; |
| 232 | } else { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | // call frontend check callback. |
| 237 | if ( method_exists( $check[0], $check[1] ) ) { |
| 238 | return call_user_func( array( $check[0], $check[1] ), $options, $ad ); |
| 239 | } |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Render the list of set visisor conditions |
| 246 | * |
| 247 | * @param array $set_conditions array of existing conditions. |
| 248 | * @param string $list_target ID of the list with the conditions. |
| 249 | * @param string $form_name prefix of the form. |
| 250 | */ |
| 251 | public static function render_condition_list( array $set_conditions, $list_target = '', $form_name = '' ) { |
| 252 | |
| 253 | $conditions = self::get_instance()->get_conditions(); |
| 254 | |
| 255 | // use default form name if not given explicitly. |
| 256 | // @todo create a random form name, in case we have more than one form per page and the parameter is not given. |
| 257 | $form_name = ! $form_name ? self::FORM_NAME : $form_name; |
| 258 | |
| 259 | include ADVADS_BASE_PATH . 'admin/views/conditions/visitor-conditions-list.php'; |
| 260 | |
| 261 | /** |
| 262 | * Prepare condition form |
| 263 | * |
| 264 | * @todo if needed, allow to disable the form to add new conditions |
| 265 | */ |
| 266 | |
| 267 | // add mockup conditions if add-ons are missing. |
| 268 | $pro_conditions = array(); |
| 269 | if ( ! defined( 'AAP_VERSION' ) ) { |
| 270 | $pro_conditions[] = __( 'browser language', 'advanced-ads' ); |
| 271 | $pro_conditions[] = __( 'cookie', 'advanced-ads' ); |
| 272 | $pro_conditions[] = __( 'max. ad clicks', 'advanced-ads' ); |
| 273 | $pro_conditions[] = __( 'max. ad impressions', 'advanced-ads' ); |
| 274 | $pro_conditions[] = __( 'new visitor', 'advanced-ads' ); |
| 275 | $pro_conditions[] = __( 'page impressions', 'advanced-ads' ); |
| 276 | $pro_conditions[] = __( 'referrer url', 'advanced-ads' ); |
| 277 | $pro_conditions[] = __( 'user agent', 'advanced-ads' ); |
| 278 | $pro_conditions[] = __( 'user can (capabilities)', 'advanced-ads' ); |
| 279 | $pro_conditions[] = __( 'user role', 'advanced-ads' ); |
| 280 | } |
| 281 | if ( ! defined( 'AAGT_VERSION' ) ) { |
| 282 | $pro_conditions[] = __( 'geo location', 'advanced-ads' ); |
| 283 | } |
| 284 | if ( ! defined( 'AAR_VERSION' ) ) { |
| 285 | $pro_conditions[] = __( 'browser width', 'advanced-ads' ); |
| 286 | } |
| 287 | asort( $pro_conditions ); |
| 288 | |
| 289 | // the action to call using AJAX. |
| 290 | $action = 'load_visitor_conditions_metabox'; |
| 291 | $connector_default = 'and'; |
| 292 | |
| 293 | include ADVADS_BASE_PATH . 'admin/views/conditions/visitor-conditions-form-top.php'; |
| 294 | include ADVADS_BASE_PATH . 'admin/views/conditions/conditions-form.php'; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Render connector option |
| 299 | * |
| 300 | * @param int $index incremental index of the options. |
| 301 | * @param string $value connector value. |
| 302 | * @param string $form_name name of the form, falls back to class constant. |
| 303 | * |
| 304 | * @return string HTML of the connector option |
| 305 | * @todo combine this with the same function used for Display Conditions |
| 306 | * |
| 307 | * @since 1.7.0.4 |
| 308 | */ |
| 309 | public static function render_connector_option( $index, $value, $form_name ) { |
| 310 | |
| 311 | $label = ( 'or' === $value ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' ); |
| 312 | |
| 313 | $name = self::get_form_name_with_index( $form_name, $index ); |
| 314 | |
| 315 | // create random value to identify the form field. |
| 316 | $rand = md5( $form_name ); |
| 317 | |
| 318 | return '<input type="checkbox" name="' . $name . '[connector]' . '" value="or" id="advads-conditions-' . |
| 319 | $index . '-connector-' . $rand . '"' . |
| 320 | checked( 'or', $value, false ) |
| 321 | . '><label for="advads-conditions-' . $index . '-connector-' . $rand . '">' . $label . '</label>'; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Helper function to the name of a form field. |
| 326 | * falls back to default |
| 327 | * |
| 328 | * @param string $form_name form name if submitted. |
| 329 | * @param int $index index of the condition. |
| 330 | * |
| 331 | * @return string |
| 332 | */ |
| 333 | public static function get_form_name_with_index( $form_name = '', $index = 0 ) { |
| 334 | return ! empty( $form_name ) ? $form_name . '[' . $index . ']' : self::FORM_NAME . '[' . $index . ']'; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Check mobile visitor condition in frontend |
| 339 | * |
| 340 | * @param array $options options of the condition. |
| 341 | * |
| 342 | * @return bool true if can be displayed |
| 343 | */ |
| 344 | public static function check_mobile( $options = array() ) { |
| 345 | |
| 346 | if ( ! isset( $options['operator'] ) ) { |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | switch ( $options['operator'] ) { |
| 351 | case 'is': |
| 352 | if ( ! wp_is_mobile() ) { |
| 353 | return false; |
| 354 | } |
| 355 | break; |
| 356 | case 'is_not': |
| 357 | if ( wp_is_mobile() ) { |
| 358 | return false; |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Check mobile visitor condition in frontend |
| 368 | * |
| 369 | * @param array $options options of the condition. |
| 370 | * |
| 371 | * @return bool true if can be displayed |
| 372 | * @since 1.6.3 |
| 373 | */ |
| 374 | public static function check_logged_in( $options = array() ) { |
| 375 | |
| 376 | if ( ! isset( $options['operator'] ) ) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | switch ( $options['operator'] ) { |
| 381 | case 'is': |
| 382 | if ( ! is_user_logged_in() ) { |
| 383 | return false; |
| 384 | } |
| 385 | break; |
| 386 | case 'is_not': |
| 387 | if ( is_user_logged_in() ) { |
| 388 | return false; |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Helper for check with strings |
| 398 | * |
| 399 | * @param string $string string that is going to be checked. |
| 400 | * @param array $options options of this condition. |
| 401 | * |
| 402 | * @return bool true if ad can be displayed |
| 403 | * @since 1.6.3 |
| 404 | */ |
| 405 | public static function helper_check_string( $string = '', $options = array() ) { |
| 406 | |
| 407 | if ( ! isset( $options['operator'] ) || ! isset( $options['value'] ) || '' === $options['value'] ) { |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | $operator = $options['operator']; |
| 412 | $value = $options['value']; |
| 413 | |
| 414 | // check the condition by mode and bool. |
| 415 | $condition = true; |
| 416 | switch ( $operator ) { |
| 417 | // referrer contains string on any position. |
| 418 | case 'contain': |
| 419 | $condition = stripos( $string, $value ) !== false; |
| 420 | break; |
| 421 | // referrer does not contain string on any position. |
| 422 | case 'contain_not': |
| 423 | $condition = stripos( $string, $value ) === false; |
| 424 | break; |
| 425 | // referrer starts with the string. |
| 426 | case 'start': |
| 427 | $condition = stripos( $string, $value ) === 0; |
| 428 | break; |
| 429 | // referrer does not start with the string. |
| 430 | case 'start_not': |
| 431 | $condition = stripos( $string, $value ) !== 0; |
| 432 | break; |
| 433 | // referrer ends with the string. |
| 434 | case 'end': |
| 435 | $condition = substr( $string, - strlen( $value ) ) === $value; |
| 436 | break; |
| 437 | // referrer does not end with the string. |
| 438 | case 'end_not': |
| 439 | $condition = substr( $string, - strlen( $value ) ) !== $value; |
| 440 | break; |
| 441 | // referrer is equal to the string. |
| 442 | case 'match': |
| 443 | // strings do match, but should not or not match but should. |
| 444 | $condition = strcasecmp( $value, $string ) === 0; |
| 445 | break; |
| 446 | // referrer is not equal to the string. |
| 447 | case 'match_not': |
| 448 | // strings do match, but should not or not match but should. |
| 449 | $condition = strcasecmp( $value, $string ) !== 0; |
| 450 | break; |
| 451 | // string is a regular expression. |
| 452 | case 'regex': |
| 453 | // check regular expression first. |
| 454 | if ( @preg_match( $value, null ) === false ) { |
| 455 | Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); |
| 456 | } else { |
| 457 | $condition = preg_match( $value, $string ); |
| 458 | } |
| 459 | break; |
| 460 | // string is not a regular expression. |
| 461 | case 'regex_not': |
| 462 | if ( @preg_match( $value, null ) === false ) { |
| 463 | Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); |
| 464 | } else { |
| 465 | $condition = ! preg_match( $value, $string ); |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | return $condition; |
| 471 | } |
| 472 | } |
| 473 |