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