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
374 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * visitor conditions under which to (not) show an ad |
| 5 | * |
| 6 | * @since 1.5.4 |
| 7 | * |
| 8 | */ |
| 9 | class Advanced_Ads_Visitor_Conditions { |
| 10 | |
| 11 | /** |
| 12 | * |
| 13 | * @var Advanced_Ads_Visitor_Conditions |
| 14 | */ |
| 15 | protected static $instance; |
| 16 | |
| 17 | /** |
| 18 | * registered visitor conditions |
| 19 | */ |
| 20 | public $conditions; |
| 21 | |
| 22 | /** |
| 23 | * start of name in form elements |
| 24 | */ |
| 25 | const FORM_NAME = 'advanced_ad[visitors]'; |
| 26 | |
| 27 | public function __construct() { |
| 28 | |
| 29 | // register conditions |
| 30 | $this->conditions = apply_filters( 'advanced-ads-visitor-conditions', array( |
| 31 | 'mobile' => array( // type of the condition |
| 32 | 'label' => __( 'device', 'advanced-ads' ), |
| 33 | 'description' => __( 'Display ads only on mobile devices or hide them.', 'advanced-ads' ), |
| 34 | 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'mobile_is_or_not' ), // callback to generate the metabox |
| 35 | 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_mobile' ), // callback for frontend check |
| 36 | '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 |
| 37 | ), |
| 38 | 'loggedin' => array( |
| 39 | 'label' => __( 'logged in visitor', 'advanced-ads' ), |
| 40 | 'description' => __( 'Whether the visitor has to be logged in or not in order to see the ads.', 'advanced-ads' ), |
| 41 | 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'metabox_is_or_not' ), // callback to generate the metabox |
| 42 | 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_logged_in' ), // callback for frontend check |
| 43 | 'passive_info' => array( 'hash_fields' => null, 'remove' => 'login', 'function' => 'is_user_logged_in' ) |
| 44 | ), |
| 45 | )); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @return Advanced_Ads_Plugin |
| 51 | */ |
| 52 | public static function get_instance() { |
| 53 | // If the single instance hasn't been set, set it now. |
| 54 | if ( null === self::$instance ) { |
| 55 | self::$instance = new self; |
| 56 | } |
| 57 | |
| 58 | return self::$instance; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * get the conditions array alphabetically by label |
| 64 | * |
| 65 | * @since 1.8.12 |
| 66 | */ |
| 67 | public function get_conditions(){ |
| 68 | uasort( $this->conditions, 'Advanced_Ads_Admin::sort_condition_array_by_label' ); |
| 69 | |
| 70 | return $this->conditions; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * callback to render the mobile condition using the "is not" condition |
| 75 | * |
| 76 | * @param arr $options options of the condition |
| 77 | * @param int $index index of the condition |
| 78 | */ |
| 79 | static function mobile_is_or_not( $options, $index = 0 ){ |
| 80 | |
| 81 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 82 | |
| 83 | $type_options = self::get_instance()->conditions; |
| 84 | |
| 85 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // form name basis |
| 90 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 91 | |
| 92 | // options |
| 93 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'is'; |
| 94 | |
| 95 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 96 | <select name="<?php echo $name; ?>[operator]"> |
| 97 | <option value="is" <?php selected( 'is', $operator ); ?>><?php _e( 'Mobile (including tablets)', 'advanced-ads' ); ?></option> |
| 98 | <option value="is_not" <?php selected( 'is_not', $operator ); ?>><?php _e( 'Desktop', 'advanced-ads' ); ?></option> |
| 99 | </select> |
| 100 | <p class="description"><?php echo $type_options[ $options['type'] ]['description']; |
| 101 | if( isset( $type_options[ $options['type'] ]['helplink'] ) ) : ?> |
| 102 | <a href="<?php echo $type_options[ $options['type'] ]['helplink']; ?>" target="_blank"><?php |
| 103 | _e( 'Manual and Troubleshooting', 'advanced-ads' ); |
| 104 | ?></a><?php endif; ?></p><?php |
| 105 | |
| 106 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 107 | |
| 108 | if( ! defined( 'AAR_SLUG' ) ){ |
| 109 | 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>'; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * callback to display the "is not" condition |
| 115 | * |
| 116 | * @param arr $options options of the condition |
| 117 | * @param int $index index of the condition |
| 118 | */ |
| 119 | static function metabox_is_or_not( $options, $index = 0 ){ |
| 120 | |
| 121 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 122 | |
| 123 | $type_options = self::get_instance()->conditions; |
| 124 | |
| 125 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // form name basis |
| 130 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 131 | |
| 132 | // options |
| 133 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'is'; |
| 134 | |
| 135 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 136 | <select name="<?php echo $name; ?>[operator]"> |
| 137 | <option value="is" <?php selected( 'is', $operator ); ?>><?php _e( 'is', 'advanced-ads' ); ?></option> |
| 138 | <option value="is_not" <?php selected( 'is_not', $operator ); ?>><?php _e( 'is not', 'advanced-ads' ); ?></option> |
| 139 | </select> |
| 140 | <p class="description"><?php echo $type_options[ $options['type'] ]['description']; |
| 141 | if( isset( $type_options[ $options['type'] ]['helplink'] ) ) : ?> |
| 142 | <a href="<?php echo $type_options[ $options['type'] ]['helplink']; ?>" target="_blank"><?php |
| 143 | _e( 'Manual and Troubleshooting', 'advanced-ads' ); |
| 144 | ?></a><?php endif; ?></p><?php |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * callback to display the any condition based on a number |
| 149 | * |
| 150 | * @param arr $options options of the condition |
| 151 | * @param int $index index of the condition |
| 152 | */ |
| 153 | static function metabox_number( $options, $index = 0 ){ |
| 154 | |
| 155 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 156 | |
| 157 | $type_options = self::get_instance()->conditions; |
| 158 | |
| 159 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // form name basis |
| 164 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 165 | |
| 166 | // options |
| 167 | $value = isset( $options['value'] ) ? $options['value'] : 0; |
| 168 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'is_equal'; |
| 169 | |
| 170 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 171 | <select name="<?php echo $name; ?>[operator]"> |
| 172 | <option value="is_equal" <?php selected( 'is_equal', $operator ); ?>><?php _e( 'equal', 'advanced-ads' ); ?></option> |
| 173 | <option value="is_higher" <?php selected( 'is_higher', $operator ); ?>><?php _e( 'equal or higher', 'advanced-ads' ); ?></option> |
| 174 | <option value="is_lower" <?php selected( 'is_lower', $operator ); ?>><?php _e( 'equal or lower', 'advanced-ads' ); ?></option> |
| 175 | </select><input type="number" name="<?php echo $name; ?>[value]" value="<?php echo absint( $value ); ?>"/> |
| 176 | <p class="description"><?php echo $type_options[ $options['type'] ]['description']; ?></p><?php |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * callback to display the any condition based on a number |
| 181 | * |
| 182 | * @param arr $options options of the condition |
| 183 | * @param int $index index of the condition |
| 184 | */ |
| 185 | static function metabox_string( $options, $index = 0 ){ |
| 186 | |
| 187 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 188 | |
| 189 | $type_options = self::get_instance()->conditions; |
| 190 | |
| 191 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // form name basis |
| 196 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 197 | |
| 198 | // options |
| 199 | $value = isset( $options['value'] ) ? $options['value'] : ''; |
| 200 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'contains'; |
| 201 | |
| 202 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 203 | <div class="advads-condition-line-wrap"> |
| 204 | <?php include( ADVADS_BASE_PATH . 'admin/views/ad-conditions-string-operators.php' ); ?> |
| 205 | <input type="text" name="<?php echo $name; ?>[value]" value="<?php echo $value; ?>"/> |
| 206 | </div> |
| 207 | <p class="description"><?php echo $type_options[ $options['type'] ]['description']; ?></p><?php |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * controls frontend checks for conditions |
| 212 | * |
| 213 | * @param arr $options options of the condition |
| 214 | * @param ob $ad Advanced_Ads_Ad |
| 215 | * @return bool false, if ad can’t be delivered |
| 216 | */ |
| 217 | static function frontend_check( $options = array(), $ad = false ){ |
| 218 | $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; |
| 219 | |
| 220 | if ( is_array( $options ) && isset( $visitor_conditions[ $options['type'] ]['check'] ) ) { |
| 221 | $check = $visitor_conditions[ $options['type'] ]['check']; |
| 222 | } else { |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | // call frontend check callback |
| 227 | if ( method_exists( $check[0], $check[1] ) ) { |
| 228 | return call_user_func( array( $check[0], $check[1] ), $options, $ad ); |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * render connector option |
| 236 | * |
| 237 | * @since 1.7.0.4 |
| 238 | * @param int $index |
| 239 | */ |
| 240 | static function render_connector_option( $index = 0, $value = 'or' ){ |
| 241 | |
| 242 | $label = ( $value === 'or' ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' ); |
| 243 | |
| 244 | return '<input type="checkbox" name="' . self::FORM_NAME . '[' . $index . '][connector]' . '" value="or" id="advads-visitor-conditions-' . |
| 245 | $index . '-connector"' . |
| 246 | checked( 'or', $value, false ) |
| 247 | .'><label for="advads-visitor-conditions-' . $index . '-connector">' . $label . '</label>'; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * check mobile visitor condition in frontend |
| 252 | * |
| 253 | * @param arr $options options of the condition |
| 254 | * @return bool true if can be displayed |
| 255 | */ |
| 256 | static function check_mobile( $options = array() ){ |
| 257 | |
| 258 | if ( ! isset( $options['operator'] ) ) { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | switch ( $options['operator'] ){ |
| 263 | case 'is' : |
| 264 | if ( ! wp_is_mobile() ) { return false; } |
| 265 | break; |
| 266 | case 'is_not' : |
| 267 | if ( wp_is_mobile() ) { return false; } |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * check mobile visitor condition in frontend |
| 276 | * |
| 277 | * @since 1.6.3 |
| 278 | * @param arr $options options of the condition |
| 279 | * @return bool true if can be displayed |
| 280 | */ |
| 281 | static function check_logged_in( $options = array() ){ |
| 282 | |
| 283 | if ( ! isset( $options['operator'] ) ) { |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | switch ( $options['operator'] ){ |
| 288 | case 'is' : |
| 289 | if ( ! is_user_logged_in() ) { return false; } |
| 290 | break; |
| 291 | case 'is_not' : |
| 292 | if ( is_user_logged_in() ) { return false; } |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * helper for check with strings |
| 301 | * |
| 302 | * @since 1.6.3 |
| 303 | * @param str $string string that is going to be checked |
| 304 | * @return bool true if ad can be displayed |
| 305 | */ |
| 306 | static function helper_check_string( $string = '', $options = array() ){ |
| 307 | |
| 308 | if ( ! isset( $options['operator'] ) || ! isset( $options['value'] ) || '' === $options['value'] ){ |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | $operator = $options['operator']; |
| 313 | $value = $options['value']; |
| 314 | |
| 315 | // check the condition by mode and bool |
| 316 | $condition = true; |
| 317 | switch ( $operator ){ |
| 318 | // referrer contains string on any position |
| 319 | case 'contain' : |
| 320 | $condition = stripos( $string, $value ) !== false; |
| 321 | break; |
| 322 | // referrer does not contain string on any position |
| 323 | case 'contain_not' : |
| 324 | $condition = stripos( $string, $value ) === false; |
| 325 | break; |
| 326 | // referrer starts with the string |
| 327 | case 'start' : |
| 328 | $condition = stripos( $string, $value ) === 0; |
| 329 | break; |
| 330 | // referrer does not start with the string |
| 331 | case 'start_not' : |
| 332 | $condition = stripos( $string, $value ) !== 0; |
| 333 | break; |
| 334 | // referrer ends with the string |
| 335 | case 'end' : |
| 336 | $condition = $value === substr( $string, -strlen( $value ) ); |
| 337 | break; |
| 338 | // referrer does not end with the string |
| 339 | case 'end_not' : |
| 340 | $condition = $value !== substr( $string, -strlen( $value ) ); |
| 341 | break; |
| 342 | // referrer is equal to the string |
| 343 | case 'match' : |
| 344 | // strings do match, but should not or not match but should |
| 345 | $condition = strcasecmp($value, $string) === 0; |
| 346 | break; |
| 347 | // referrer is not equal to the string |
| 348 | case 'match_not' : |
| 349 | // strings do match, but should not or not match but should |
| 350 | $condition = strcasecmp($value, $string) !== 0; |
| 351 | break; |
| 352 | // string is a regular expression |
| 353 | case 'regex' : |
| 354 | // check regular expression first |
| 355 | if( @preg_match( $value, null ) === false ){ |
| 356 | Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); |
| 357 | } else { |
| 358 | $condition = preg_match( $value, $string ); |
| 359 | } |
| 360 | break; |
| 361 | // string is not a regular expression |
| 362 | case 'regex_not' : |
| 363 | if( @preg_match( $value, null ) === false ){ |
| 364 | Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); |
| 365 | } else { |
| 366 | $condition = ! preg_match( $value, $string ); |
| 367 | } |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | return $condition; |
| 372 | } |
| 373 | } |
| 374 |