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
display-conditions.php
1191 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Display Conditions under which to (not) show an ad |
| 5 | * |
| 6 | * @since 1.7 |
| 7 | * |
| 8 | */ |
| 9 | class Advanced_Ads_Display_Conditions { |
| 10 | |
| 11 | /** |
| 12 | * |
| 13 | * @var Advanced_Ads_Display_Conditions |
| 14 | */ |
| 15 | protected static $instance; |
| 16 | |
| 17 | /** |
| 18 | * registered display conditions |
| 19 | */ |
| 20 | public $conditions; |
| 21 | |
| 22 | /** |
| 23 | * start of name in form elements |
| 24 | */ |
| 25 | const FORM_NAME = 'advanced_ad[conditions]'; |
| 26 | |
| 27 | protected static $query_var_keys = array( |
| 28 | // 'is_single', |
| 29 | 'is_archive', |
| 30 | 'is_search', |
| 31 | 'is_home', |
| 32 | 'is_404', |
| 33 | 'is_attachment', |
| 34 | 'is_singular', |
| 35 | 'is_front_page', |
| 36 | 'is_feed' |
| 37 | ); |
| 38 | |
| 39 | // this is how the general conditions should look like by default |
| 40 | protected static $default_general_keys = array( |
| 41 | 'is_front_page', |
| 42 | 'is_singular', |
| 43 | 'is_archive', |
| 44 | 'is_search', |
| 45 | 'is_404', |
| 46 | 'is_attachment', |
| 47 | 'is_main_query', |
| 48 | 'is_feed' |
| 49 | ); |
| 50 | |
| 51 | private function __construct() { |
| 52 | |
| 53 | // register filter |
| 54 | add_filter('advanced-ads-ad-select-args', array($this, 'ad_select_args_callback')); |
| 55 | add_filter('advanced-ads-can-display', array($this, 'can_display'), 10, 2); |
| 56 | |
| 57 | // register conditions with init hook, register as late as possible so other plugins can use the same hook to add new taxonomies |
| 58 | add_action( 'init', array($this, 'register_conditions'), 100 ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * register display conditions |
| 63 | * |
| 64 | * @since 1.7.1.4 |
| 65 | */ |
| 66 | public function register_conditions(){ |
| 67 | $conditions = array( |
| 68 | 'posttypes' => array(// post types condition |
| 69 | 'label' => __('post type', 'advanced-ads'), |
| 70 | 'description' => __('Choose the public post types on which to display the ad.', 'advanced-ads'), |
| 71 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_post_type'), // callback to generate the metabox |
| 72 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_post_type'), // callback for frontend check |
| 73 | // '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 |
| 74 | ), |
| 75 | 'postids' => array(// post id condition |
| 76 | 'label' => __('specific pages', 'advanced-ads'), |
| 77 | 'description' => __('Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads'), |
| 78 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_post_ids'), // callback to generate the metabox |
| 79 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_post_ids'), // callback for frontend check |
| 80 | ), |
| 81 | 'general' => array(// general conditions |
| 82 | 'label' => __('general conditions', 'advanced-ads'), |
| 83 | // 'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ), |
| 84 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_general'), // callback to generate the metabox |
| 85 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_general'), // callback for frontend check |
| 86 | ), |
| 87 | 'author' => array(// author conditions |
| 88 | 'label' => __('author', 'advanced-ads'), |
| 89 | // 'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ), |
| 90 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_author'), // callback to generate the metabox |
| 91 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_author'), // callback for frontend check |
| 92 | ), |
| 93 | /** |
| 94 | * display ads only in content older or younger than a specific age |
| 95 | */ |
| 96 | 'content_age' => array( |
| 97 | 'label' => __( 'content age', 'advanced-ads' ), |
| 98 | 'description' => __( 'Display ads based on age of the page.', 'advanced-ads' ), |
| 99 | 'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_content_age' ), // callback to generate the metabox |
| 100 | 'check' => array( 'Advanced_Ads_Display_Conditions', 'check_content_age' ) // callback for frontend check |
| 101 | ), |
| 102 | /** |
| 103 | * condition for taxonomies in general |
| 104 | */ |
| 105 | 'taxonomy' => array( |
| 106 | 'label' => __( 'taxonomy', 'advanced-ads' ), |
| 107 | 'description' => __( 'Display ads based on the taxonomy of an archive page.', 'advanced-ads' ), |
| 108 | 'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_taxonomies' ), // callback to generate the metabox |
| 109 | 'check' => array( 'Advanced_Ads_Display_Conditions', 'check_taxonomy' ) // callback for frontend check |
| 110 | ), |
| 111 | ); |
| 112 | |
| 113 | // register a condition for each taxonomy for posts. |
| 114 | $taxonomies = get_taxonomies(array('public' => true, 'publicly_queryable' => true), 'objects', 'or'); |
| 115 | |
| 116 | $tax_label_counts = array(); |
| 117 | |
| 118 | foreach ($taxonomies as $_tax) : |
| 119 | if ( in_array( $_tax->name, array( 'advanced_ads_groups' ) ) ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Count names of taxonomies and adjust label if there are duplicates. |
| 125 | * we can’t use `array_count_values` here because "label" might not always be a simple string (though it should be) |
| 126 | */ |
| 127 | $tax_label_counts[ $_tax->label ] = isset( $tax_label_counts[ $_tax->label ] ) ? $tax_label_counts[ $_tax->label ] + 1 : $tax_label_counts[ $_tax->label ] = 1; |
| 128 | |
| 129 | // add tax type to label if we find it multiple times. |
| 130 | if ( $tax_label_counts[ $_tax->label ] < 2 ) { |
| 131 | $label = $_tax->label; |
| 132 | $archive_label = $_tax->labels->singular_name; |
| 133 | } else { |
| 134 | $label = sprintf( '%s (%s)', $_tax->label, $_tax->name ); |
| 135 | $archive_label = sprintf( '%s (%s)', $_tax->labels->singular_name, $_tax->name ); |
| 136 | } |
| 137 | |
| 138 | $conditions['taxonomy_' . $_tax->name] = array( |
| 139 | 'label' => $label, |
| 140 | // 'description' => sprintf(__( 'Choose terms from the %s taxonomy a post must belong to for showing or hiding ads.', 'advanced-ads' ), $_tax->label ), |
| 141 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms'), // callback to generate the metabox |
| 142 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_taxonomies'), // callback for frontend check |
| 143 | 'taxonomy' => $_tax->name, // unique for this type: the taxonomy name |
| 144 | ); |
| 145 | |
| 146 | $conditions['archive_' . $_tax->name] = array( |
| 147 | 'label' => sprintf(__('archive: %s', 'advanced-ads'), $archive_label), |
| 148 | // 'description' => sprintf(__( 'Choose on which %s archive page ads are hidden or displayeds.', 'advanced-ads' ), $_tax->label ), |
| 149 | 'metabox' => array('Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms'), // callback to generate the metabox |
| 150 | 'check' => array('Advanced_Ads_Display_Conditions', 'check_taxonomy_archive'), // callback for frontend check |
| 151 | 'taxonomy' => $_tax->name, // unique for this type: the taxonomy name |
| 152 | ); |
| 153 | endforeach; |
| 154 | |
| 155 | $this->conditions = apply_filters('advanced-ads-display-conditions', $conditions); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * |
| 160 | * @return Advanced_Ads_Plugin |
| 161 | */ |
| 162 | public static function get_instance() { |
| 163 | // If the single instance hasn't been set, set it now. |
| 164 | if (null === self::$instance) { |
| 165 | self::$instance = new self; |
| 166 | } |
| 167 | |
| 168 | return self::$instance; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * get the conditions array alphabetically by label |
| 174 | * |
| 175 | * @since 1.8.12 |
| 176 | */ |
| 177 | public function get_conditions(){ |
| 178 | uasort( $this->conditions, 'Advanced_Ads_Admin::sort_condition_array_by_label' ); |
| 179 | |
| 180 | return $this->conditions; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * controls frontend checks for conditions |
| 185 | * |
| 186 | * @param arr $options options of the condition |
| 187 | * @param ob $ad Advanced_Ads_Ad |
| 188 | * @return bool false, if ad can’t be delivered |
| 189 | */ |
| 190 | static function frontend_check($options = array(), $ad = false) { |
| 191 | $display_conditions = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 192 | |
| 193 | if (is_array($options) && isset( $options['type'] ) && isset($display_conditions[$options['type']]['check'])) { |
| 194 | $check = $display_conditions[$options['type']]['check']; |
| 195 | } else { |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | // call frontend check callback |
| 200 | if (method_exists($check[0], $check[1])) { |
| 201 | return call_user_func(array($check[0], $check[1]), $options, $ad); |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * render connector option |
| 209 | * |
| 210 | * @since 1.7.0.4 |
| 211 | * @param int $index |
| 212 | */ |
| 213 | static function render_connector_option( $index = 0, $value = 'or' ){ |
| 214 | |
| 215 | $label = ( $value === 'or' ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' ); |
| 216 | |
| 217 | return '<input style="display:none;" type="checkbox" name="' . self::FORM_NAME . '[' . $index . '][connector]' . '" value="or" id="advads-conditions-' . |
| 218 | $index . '-connector"' . |
| 219 | checked( 'or', $value, false ) |
| 220 | .'><label for="advads-conditions-' . $index . '-connector">' . $label . '</label>'; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * callback to display the metabox for the post type condition |
| 225 | * |
| 226 | * @param arr $options options of the condition |
| 227 | * @param int $index index of the condition |
| 228 | */ |
| 229 | static function metabox_post_type($options, $index = 0) { |
| 230 | if (!isset($options['type']) || '' === $options['type']) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | $type_options = self::get_instance()->conditions; |
| 235 | |
| 236 | if (!isset($type_options[$options['type']])) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // get values and select operator based on previous settings |
| 241 | $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; |
| 242 | $values = ( isset($options['value'] ) && is_array( $options['value'] ) ) ? $options['value'] : array(); |
| 243 | |
| 244 | // form name basis |
| 245 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 246 | |
| 247 | // options |
| 248 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 249 | <select name="<?php echo $name; ?>[operator]"> |
| 250 | <option value="is" <?php selected('is', $operator); ?>><?php _e('is', 'advanced-ads'); ?></option> |
| 251 | <option value="is_not" <?php selected('is_not', $operator); ?>><?php _e('is not', 'advanced-ads' ); ?></option> |
| 252 | </select><?php |
| 253 | |
| 254 | // set defaults |
| 255 | $post_types = get_post_types(array('public' => true, 'publicly_queryable' => true), 'object', 'or'); |
| 256 | ?><div class="advads-conditions-single advads-buttonset"><?php |
| 257 | $type_label_counts = array_count_values( wp_list_pluck( $post_types, 'label' ) ); |
| 258 | |
| 259 | foreach ($post_types as $_type_id => $_type) { |
| 260 | if ( in_array($_type_id, $values)) { |
| 261 | $_val = 1; |
| 262 | } else { |
| 263 | $_val = 0; |
| 264 | } |
| 265 | |
| 266 | if ( $type_label_counts[ $_type->label ] < 2 ) { |
| 267 | $_label = $_type->label; |
| 268 | } else { |
| 269 | $_label = sprintf( '%s (%s)', $_type->label, $_type_id ); |
| 270 | } |
| 271 | ?><label class="button" for="advads-conditions-<?php echo $index; ?>-<?php echo $_type_id; |
| 272 | ?>"><?php echo $_label ?></label><input type="checkbox" id="advads-conditions-<?php echo $index; ?>-<?php echo $_type_id; ?>" name="<?php echo $name; ?>[value][]" <?php checked($_val, 1); ?> value="<?php echo $_type_id; ?>"><?php |
| 273 | } |
| 274 | ?><p class="advads-conditions-not-selected advads-error-message"><?php _ex( 'Please select some items.', 'Error message shown when no display condition term is selected', 'advanced-ads' ); ?></p></div><?php |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * callback to display the metabox for the author condition |
| 279 | * |
| 280 | * @param arr $options options of the condition |
| 281 | * @param int $index index of the condition |
| 282 | */ |
| 283 | static function metabox_author($options, $index = 0) { |
| 284 | |
| 285 | if (!isset($options['type']) || '' === $options['type']) { |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | $type_options = self::get_instance()->conditions; |
| 290 | |
| 291 | if (!isset($type_options[$options['type']])) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | // get values and select operator based on previous settings |
| 296 | $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; |
| 297 | $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); |
| 298 | |
| 299 | // form name basis |
| 300 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 301 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 302 | <select name="<?php echo $name; ?>[operator]"> |
| 303 | <option value="is" <?php selected('is', $operator); ?>><?php _e('is', 'advanced-ads'); ?></option> |
| 304 | <option value="is_not" <?php selected('is_not', $operator); ?>><?php _e('is not', 'advanced-ads'); ?></option> |
| 305 | </select><?php |
| 306 | // set defaults |
| 307 | $max_authors = absint( apply_filters( 'advanced-ads-admin-max-terms', 50 ) ); |
| 308 | $authors = get_users(array('who' => 'authors', 'orderby' => 'nicename', 'number' => $max_authors ) ); |
| 309 | ?><div class="advads-conditions-single advads-buttonset"><?php |
| 310 | foreach ($authors as $_author) { |
| 311 | if ( in_array($_author->ID, $values ) ) { |
| 312 | $_val = 1; |
| 313 | } else { |
| 314 | $_val = 0; |
| 315 | } |
| 316 | ?><label class="button ui-button" for="advads-conditions-<?php echo $index; ?>-<?php echo $_author->ID; |
| 317 | ?>"><?php echo $_author->display_name; ?></label><input type="checkbox" id="advads-conditions-<?php echo $index; ?>-<?php echo $_author->ID; ?>" name="<?php echo $name; ?>[value][]" <?php checked($_val, 1); ?> value="<?php echo $_author->ID; ?>"><?php |
| 318 | } |
| 319 | ?><p class="advads-conditions-not-selected advads-error-message"><?php _ex( 'Please select some items.', 'Error message shown when no display condition term is selected', 'advanced-ads' ); ?></p></div> |
| 320 | <?php if( count( $authors ) >= $max_authors ) : ?><p class="advads-error-message"><?php printf( __( 'Only %d elements are displayed above. Use the <code>advanced-ads-admin-max-terms</code> filter to change this limit according to <a href="%s" target="_blank">this page</a>.', 'advanced-ads' ), $max_authors, ADVADS_URL . 'codex/filter-hooks//#utm_source=advanced-ads&utm_medium=link&utm_campaign=author-term-limit' ); ?></p><?php endif; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * callback to display the metabox for the taxonomy archive pages |
| 325 | * |
| 326 | * @param arr $options options of the condition |
| 327 | * @param int $index index of the condition |
| 328 | */ |
| 329 | static function metabox_taxonomy_terms($options, $index = 0) { |
| 330 | |
| 331 | if (!isset($options['type']) || '' === $options['type']) { |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | $type_options = self::get_instance()->conditions; |
| 336 | |
| 337 | // don’t use if this is not a taxonomy |
| 338 | if (!isset($type_options[$options['type']]) || !isset($type_options[$options['type']]['taxonomy'])) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | $taxonomy = get_taxonomy($type_options[$options['type']]['taxonomy']); |
| 343 | if (false == $taxonomy) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | // get values and select operator based on previous settings |
| 348 | $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; |
| 349 | $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); |
| 350 | |
| 351 | // limit the number of terms so many terms don’t break the admin page |
| 352 | $max_terms = absint(apply_filters('advanced-ads-admin-max-terms', 50)); |
| 353 | |
| 354 | // form name basis |
| 355 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 356 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 357 | <select name="<?php echo $name; ?>[operator]"> |
| 358 | <option value="is" <?php selected('is', $operator); ?>><?php _e('is', 'advanced-ads'); ?></option> |
| 359 | <option value="is_not" <?php selected('is_not', $operator); ?>><?php _e('is not', 'advanced-ads'); ?></option> |
| 360 | </select><?php |
| 361 | ?><div class="advads-conditions-single advads-buttonset"><?php |
| 362 | self::display_term_list($taxonomy, $values, $name . '[value][]', $max_terms, $index); |
| 363 | ?></div><?php |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * callback to display the metabox for the taxonomies |
| 368 | * |
| 369 | * @param arr $options options of the condition |
| 370 | * @param int $index index of the condition |
| 371 | */ |
| 372 | static function metabox_taxonomies($options, $index = 0) { |
| 373 | |
| 374 | if (!isset($options['type']) || '' === $options['type']) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | $taxonomies = get_taxonomies( array( 'public' => 1 ), 'objects' ); |
| 379 | |
| 380 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 381 | |
| 382 | // get values and select operator based on previous settings |
| 383 | $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; |
| 384 | $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); |
| 385 | |
| 386 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 387 | <div class="advads-conditions-single advads-buttonset"><?php |
| 388 | $tax_label_counts = array_count_values( wp_list_pluck( $taxonomies, 'label' ) ); |
| 389 | |
| 390 | foreach ($taxonomies as $_taxonomy_id => $_taxonomy) : |
| 391 | |
| 392 | if ( in_array($_taxonomy_id, $values)) { |
| 393 | $_val = 1; |
| 394 | } else { |
| 395 | $_val = 0; |
| 396 | } |
| 397 | |
| 398 | if ( $tax_label_counts[ $_taxonomy->label ] < 2 ) { |
| 399 | $_label = $_taxonomy->label; |
| 400 | } else { |
| 401 | $_label = sprintf( '%s (%s)', $_taxonomy->label, $_taxonomy_id ); |
| 402 | } |
| 403 | ?><label class="button" for="advads-conditions-<?php echo $index; ?>-<?php echo $_taxonomy_id; |
| 404 | ?>"><?php echo $_label ?></label><input type="checkbox" id="advads-conditions-<?php echo $index; ?>-<?php echo $_taxonomy_id; ?>" name="<?php echo $name; ?>[value][]" <?php checked($_val, 1); ?> value="<?php echo $_taxonomy_id; ?>"><?php |
| 405 | endforeach; |
| 406 | ?><p class="advads-conditions-not-selected advads-error-message"><?php _ex( 'Please select some items.', 'Error message shown when no display condition term is selected', 'advanced-ads' ); ?></p></div><?php |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * display terms of a taxonomy for choice |
| 411 | * |
| 412 | * @param obj $taxonomy taxonomy object |
| 413 | * @param arr $checked ids of checked terms |
| 414 | * @param str $inputname name of the input field |
| 415 | * @param int $max_terms maximum number of terms to show |
| 416 | * @param int $index index of the conditions group |
| 417 | */ |
| 418 | public static function display_term_list($taxonomy, $checked = array(), $inputname = '', $max_terms = 50, $index = 0) { |
| 419 | |
| 420 | $terms = get_terms($taxonomy->name, array('hide_empty' => false, 'number' => $max_terms)); |
| 421 | |
| 422 | if (!empty($terms) && !is_wp_error($terms)): |
| 423 | // display search field if the term limit is reached |
| 424 | if (count($terms) == $max_terms) : |
| 425 | |
| 426 | // query active terms |
| 427 | if (is_array($checked) && count($checked)) { |
| 428 | $args = array('hide_empty' => false); |
| 429 | $args['include'] = $checked; |
| 430 | $checked_terms = get_terms($taxonomy->name, $args); |
| 431 | ?><div class="advads-conditions-terms-buttons dynamic-search"><?php |
| 432 | foreach ($checked_terms as $_checked_term) : |
| 433 | ?><label class="button ui-state-active"><?php echo $_checked_term->name; |
| 434 | ?><input type="hidden" name="<?php echo $inputname; ?>" value="<?php echo $_checked_term->term_id; ?>"></label><?php |
| 435 | endforeach; |
| 436 | ?></div><?php |
| 437 | } else { |
| 438 | ?><div class="advads-conditions-terms-buttons dynamic-search"></div><?php |
| 439 | } |
| 440 | ?><span class="advads-conditions-terms-show-search button" title="<?php |
| 441 | _ex('add more terms', 'display the terms search field on ad edit page', 'advanced-ads'); |
| 442 | ?>">+</span><br/><input type="text" class="advads-conditions-terms-search" data-tag-name="<?php echo $taxonomy->name; |
| 443 | ?>" data-input-name="<?php echo $inputname; ?>" placeholder="<?php _e('term name or id', 'advanced-ads'); ?>"/><?php |
| 444 | else : |
| 445 | ?><div class="advads-conditions-terms-buttons advads-buttonset"><?php |
| 446 | foreach ($terms as $_term) : |
| 447 | $field_id = "advads-conditions-$index-$_term->term_id"; |
| 448 | ?><input type="checkbox" id="<?php echo $field_id; ?>" name="<?php echo $inputname; ?>" value="<?php echo $_term->term_id; ?>" <?php checked(in_array($_term->term_id, $checked), true); ?>><label for="<?php echo $field_id; ?>"><?php echo $_term->name; ?></label><?php |
| 449 | endforeach; |
| 450 | ?><p class="advads-conditions-not-selected advads-error-message"><?php _ex( 'Please select some items.', 'Error message shown when no display condition term is selected', 'advanced-ads' ); ?></p></div><?php |
| 451 | endif; |
| 452 | endif; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * callback to display the metabox for the taxonomy archive pages |
| 457 | * |
| 458 | * @param arr $options options of the condition |
| 459 | * @param int $index index of the condition |
| 460 | */ |
| 461 | static function metabox_post_ids($options, $index = 0) { |
| 462 | |
| 463 | if (!isset($options['type']) || '' === $options['type']) { |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | // get values and select operator based on previous settings |
| 468 | $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; |
| 469 | $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); |
| 470 | |
| 471 | // form name basis |
| 472 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 473 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 474 | <select name="<?php echo $name; ?>[operator]"> |
| 475 | <option value="is" <?php selected('is', $operator); ?>><?php _e('is', 'advanced-ads'); ?></option> |
| 476 | <option value="is_not" <?php selected('is_not', $operator); ?>><?php _e('is not', 'advanced-ads'); ?></option> |
| 477 | </select><?php ?><div class="advads-conditions-single advads-buttonset advads-conditions-postid-buttons"><?php |
| 478 | // query active post ids |
| 479 | if ($values != array()) { |
| 480 | $args = array( |
| 481 | 'post_type' => 'any', |
| 482 | // 'post_status' => 'publish', |
| 483 | 'post__in' => $values, |
| 484 | 'posts_per_page' => -1, |
| 485 | // 'ignore_sticky_posts' => 1, |
| 486 | ); |
| 487 | |
| 488 | $the_query = new WP_Query($args); |
| 489 | while ($the_query->have_posts()) { |
| 490 | $the_query->next_post(); |
| 491 | ?><label class="button ui-state-active"><?php echo get_the_title($the_query->post->ID) . ' (' . $the_query->post->post_type . ')'; |
| 492 | ?><input type="hidden" name="<?php echo $name; ?>[value][]" value="<?php echo $the_query->post->ID; ?>"></label><?php |
| 493 | } |
| 494 | } |
| 495 | ?><span class="advads-conditions-postids-show-search button" <?php |
| 496 | if (!count($values)) { |
| 497 | echo 'style="display:none;"'; |
| 498 | } |
| 499 | ?>>+</span> |
| 500 | <p class="advads-conditions-postids-search-line"> |
| 501 | <input type="text" class="advads-display-conditions-individual-post" <?php if (count($values)) { |
| 502 | echo 'style="display:none;"'; |
| 503 | } ?> |
| 504 | placeholder="<?php _e('title or id', 'advanced-ads'); ?>" |
| 505 | data-field-name="<?php echo $name; ?>"/><?php |
| 506 | wp_nonce_field('internal-linking', '_ajax_linking_nonce', false); |
| 507 | ?></p></div><?php |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * callback to display the metabox for the general display conditions |
| 512 | * |
| 513 | * @param arr $options options of the condition |
| 514 | * @param int $index index of the condition |
| 515 | */ |
| 516 | static function metabox_general($options, $index = 0) { |
| 517 | |
| 518 | // general conditions array |
| 519 | $conditions = self::get_instance()->general_conditions(); |
| 520 | if (!isset($options['type']) || '' === $options['type']) { |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | $name = self::FORM_NAME . '[' . $index . ']'; |
| 525 | $values = isset($options['value']) ? $options['value'] : array(); |
| 526 | ?><div class="advads-conditions-single advads-buttonset"> |
| 527 | <input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/><?php |
| 528 | foreach ($conditions as $_key => $_condition) : |
| 529 | |
| 530 | // activate by default |
| 531 | $value = ( $values === array() || in_array($_key, $values) ) ? 1 : 0; |
| 532 | |
| 533 | $field_id = "advads-conditions-$index-$_key"; |
| 534 | ?><input type="checkbox" id="<?php echo $field_id; ?>" name="<?php echo $name; ?>[value][]" value="<?php echo $_key; ?>" <?php checked(1, $value); ?>><label for="<?php echo $field_id; ?>"><?php echo $_condition['label']; ?></label><?php |
| 535 | endforeach; |
| 536 | ?></div><?php |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * retrieve the array with general conditions |
| 542 | * |
| 543 | * @return arr $conditions |
| 544 | * |
| 545 | */ |
| 546 | static function general_conditions() { |
| 547 | return $conditions = apply_filters( 'advanced-ads-display-conditions-general', array( |
| 548 | 'is_front_page' => array( |
| 549 | 'label' => __('Home Page', 'advanced-ads'), |
| 550 | 'description' => __('show on Home page', 'advanced-ads'), |
| 551 | 'type' => 'radio', |
| 552 | ), |
| 553 | 'is_singular' => array( |
| 554 | 'label' => __('Singular Pages', 'advanced-ads'), |
| 555 | 'description' => __('show on singular pages/posts', 'advanced-ads'), |
| 556 | 'type' => 'radio', |
| 557 | ), |
| 558 | 'is_archive' => array( |
| 559 | 'label' => __('Archive Pages', 'advanced-ads'), |
| 560 | 'description' => __('show on any type of archive page (category, tag, author and date)', 'advanced-ads'), |
| 561 | 'type' => 'radio', |
| 562 | ), |
| 563 | 'is_search' => array( |
| 564 | 'label' => __('Search Results', 'advanced-ads'), |
| 565 | 'description' => __('show on search result pages', 'advanced-ads'), |
| 566 | 'type' => 'radio', |
| 567 | ), |
| 568 | 'is_404' => array( |
| 569 | 'label' => __('404 Page', 'advanced-ads'), |
| 570 | 'description' => __('show on 404 error page', 'advanced-ads'), |
| 571 | 'type' => 'radio', |
| 572 | ), |
| 573 | 'is_attachment' => array( |
| 574 | 'label' => __('Attachment Pages', 'advanced-ads'), |
| 575 | 'description' => __('show on attachment pages', 'advanced-ads'), |
| 576 | 'type' => 'radio', |
| 577 | ), |
| 578 | 'is_main_query' => array( |
| 579 | 'label' => __('Secondary Queries', 'advanced-ads'), |
| 580 | 'description' => __('allow ads in secondary queries', 'advanced-ads'), |
| 581 | 'type' => 'radio', |
| 582 | ), |
| 583 | 'is_feed' => array( |
| 584 | 'label' => __('RSS Feed', 'advanced-ads'), |
| 585 | 'description' => __('allow ads in RSS Feed', 'advanced-ads'), |
| 586 | 'type' => 'radio', |
| 587 | ) |
| 588 | ) ); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Callback to display the 'content age' condition |
| 593 | * |
| 594 | * @param arr $options options of the condition |
| 595 | * @param int $index index of the condition |
| 596 | */ |
| 597 | static function metabox_content_age( $options, $index = 0 ){ |
| 598 | if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } |
| 599 | |
| 600 | $type_options = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 601 | |
| 602 | if ( ! isset( $type_options[ $options['type'] ] ) ) { |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | // form name basis |
| 607 | $name = Advanced_Ads_Display_Conditions::FORM_NAME . '[' . $index . ']'; |
| 608 | |
| 609 | $operator = isset( $options['operator'] ) ? $options['operator'] : 'older_than'; |
| 610 | $value = ( isset( $options['value'] ) && is_numeric( $options['value'] ) ) ? floatval( $options['value'] ) : 0; |
| 611 | ?><input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/> |
| 612 | <select name="<?php echo $name; ?>[operator]"> |
| 613 | <option value="older_than" <?php selected( 'older_than', $operator ); ?>><?php _e( 'older than', 'advanced-ads'); ?></option> |
| 614 | <option value="younger_than" <?php selected( 'younger_than', $operator ); ?>><?php _e( 'younger than', 'advanced-ads' ); ?></option> |
| 615 | </select><input type="text" name="<?php echo $name; ?>[value]" value="<?php echo $value; ?>"/> <?php _e( 'days', 'advanced-ads' ); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * check post type display condition in frontend |
| 620 | * |
| 621 | * @param arr $options options of the condition |
| 622 | * @param obj $ad Advanced_Ads_Ad |
| 623 | * @return bool true if can be displayed |
| 624 | */ |
| 625 | static function check_post_type($options = array(), Advanced_Ads_Ad $ad) { |
| 626 | if (!isset($options['value']) || !is_array($options['value'])) { |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 631 | $operator = 'is_not'; |
| 632 | } else { |
| 633 | $operator = 'is'; |
| 634 | } |
| 635 | |
| 636 | $ad_options = $ad->options(); |
| 637 | $query = $ad_options['wp_the_query']; |
| 638 | $post = isset($ad_options['post']) ? $ad_options['post'] : null; |
| 639 | $post_type = isset($post['post_type']) ? $post['post_type'] : false; |
| 640 | |
| 641 | if ( ! self::can_display_ids( $post_type, $options['value'], $operator ) ) { |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | return true; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * check author display condition in frontend |
| 650 | * |
| 651 | * @param arr $options options of the condition |
| 652 | * @param obj $ad Advanced_Ads_Ad |
| 653 | * @return bool true if can be displayed |
| 654 | */ |
| 655 | static function check_author($options = array(), Advanced_Ads_Ad $ad) { |
| 656 | |
| 657 | if (!isset($options['value']) || !is_array($options['value'])) { |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 662 | $operator = 'is_not'; |
| 663 | } else { |
| 664 | $operator = 'is'; |
| 665 | } |
| 666 | |
| 667 | $ad_options = $ad->options(); |
| 668 | $post = isset($ad_options['post']) ? $ad_options['post'] : null; |
| 669 | $post_author = isset($post['author']) ? $post['author'] : false; |
| 670 | |
| 671 | if (!self::can_display_ids($post_author, $options['value'], $operator)) { |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * check taxonomies display condition in frontend |
| 680 | * |
| 681 | * @param arr $options options of the condition |
| 682 | * @return bool true if can be displayed |
| 683 | */ |
| 684 | static function check_taxonomies($options = array(), Advanced_Ads_Ad $ad) { |
| 685 | |
| 686 | if( !isset( $options['value']) ){ |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 691 | $operator = 'is_not'; |
| 692 | } else { |
| 693 | $operator = 'is'; |
| 694 | } |
| 695 | |
| 696 | $ad_options = $ad->options(); |
| 697 | $query = $ad_options['wp_the_query']; |
| 698 | $post_id = isset($ad_options['post']['id']) ? $ad_options['post']['id'] : null; |
| 699 | |
| 700 | // get terms of the current taxonomy |
| 701 | $type_options = self::get_instance()->conditions; |
| 702 | if (!isset($options['type']) || !isset($type_options[$options['type']]['taxonomy'])) { |
| 703 | return true; |
| 704 | } |
| 705 | $taxonomy = $type_options[$options['type']]['taxonomy']; |
| 706 | |
| 707 | $terms = get_the_terms($post_id, $taxonomy); |
| 708 | |
| 709 | if ( is_array($terms) ) { |
| 710 | foreach ($terms as $term) { |
| 711 | $term_ids[] = $term->term_id; |
| 712 | } |
| 713 | } elseif( false === $terms && 'is' === $operator ) { |
| 714 | // don’t show if should show only for a specific tag |
| 715 | return false; |
| 716 | } else { |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | if( 'is' === $operator && ( ! isset($query['is_singular']) || ! $query['is_singular'] ) ){ |
| 721 | return false; |
| 722 | } elseif (isset($query['is_singular']) && $query['is_singular'] && !self::can_display_ids($options['value'], $term_ids, $operator) |
| 723 | ) { |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * check taxonomy archive display condition in frontend |
| 732 | * |
| 733 | * @param arr $options options of the condition |
| 734 | * @return bool true if can be displayed |
| 735 | */ |
| 736 | static function check_taxonomy_archive($options = array(), Advanced_Ads_Ad $ad) { |
| 737 | |
| 738 | if( !isset( $options['value']) ){ |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 743 | $operator = 'is_not'; |
| 744 | } else { |
| 745 | $operator = 'is'; |
| 746 | } |
| 747 | |
| 748 | $ad_options = $ad->options(); |
| 749 | $query = $ad_options['wp_the_query']; |
| 750 | |
| 751 | // return false if operator is "is", but important query vars are not given |
| 752 | if( 'is' === $operator && ( empty( $query['term_id'] ) || empty($query['is_archive']) ) ){ |
| 753 | return false; |
| 754 | } elseif ( isset($query['term_id']) && isset($query['is_archive']) && $query['is_archive'] && !self::can_display_ids($query['term_id'], $options['value'], $operator) |
| 755 | ) { |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * check if a specific archive belongs to a taxonomy in general (not a specific term) |
| 764 | * |
| 765 | * @param arr $options options of the condition |
| 766 | * @return bool true if can be displayed |
| 767 | */ |
| 768 | static function check_taxonomy( $options = array(), Advanced_Ads_Ad $ad ) { |
| 769 | |
| 770 | if( !isset( $options['value']) ){ |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 775 | $operator = 'is_not'; |
| 776 | } else { |
| 777 | $operator = 'is'; |
| 778 | } |
| 779 | |
| 780 | $ad_options = $ad->options(); |
| 781 | $query = $ad_options['wp_the_query']; |
| 782 | |
| 783 | // return false if operator is "is", but important query vars are not given |
| 784 | if( 'is' === $operator && ( empty( $query['taxonomy'] ) || empty($query['is_archive']) ) ){ |
| 785 | return false; |
| 786 | } elseif ( isset($query['taxonomy']) && isset($query['is_archive']) && $query['is_archive'] && !self::can_display_ids($query['taxonomy'], $options['value'], $operator) |
| 787 | ) { |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * check post ids display condition in frontend |
| 796 | * |
| 797 | * @param arr $options options of the condition |
| 798 | * @return bool true if can be displayed |
| 799 | */ |
| 800 | static function check_post_ids($options = array(), Advanced_Ads_Ad $ad) { |
| 801 | |
| 802 | if (isset($options['operator']) && $options['operator'] === 'is_not') { |
| 803 | $operator = 'is_not'; |
| 804 | } else { |
| 805 | $operator = 'is'; |
| 806 | } |
| 807 | |
| 808 | $ad_options = $ad->options(); |
| 809 | $query = $ad_options['wp_the_query']; |
| 810 | $post_id = isset($ad_options['post']['id']) ? $ad_options['post']['id'] : null; |
| 811 | |
| 812 | //fixes page id on BuddyPress pages |
| 813 | if ( $post_id == 0 && class_exists( 'BuddyPress' ) && function_exists( 'bp_current_component' )){ |
| 814 | $component = bp_current_component(); |
| 815 | $bp_pages = get_option( 'bp-pages' ); |
| 816 | if ( isset( $bp_pages[$component] ) ){ |
| 817 | $post_id = $bp_pages[$component]; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * WooCommerce Store page fix |
| 823 | * since WooCommerce changes the post ID of the static page selected to be the product overview page, we need to get the original page id from the WC options |
| 824 | */ |
| 825 | if ( function_exists( 'is_shop' ) && is_shop() && isset( $options['value'] ) && is_array( $options['value'] ) ) { |
| 826 | $post_id = get_option( 'woocommerce_shop_page_id' ); |
| 827 | return self::can_display_ids($post_id, $options['value'], $operator); |
| 828 | } |
| 829 | |
| 830 | if( empty( $ad_options['wp_the_query']['is_singular'] ) ){ |
| 831 | if( 'is_not' === $operator ){ |
| 832 | return true; |
| 833 | } else { |
| 834 | return false; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if ( ! isset( $options['value'] ) || ! is_array( $options['value'] ) || ! $post_id ) { |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | return self::can_display_ids($post_id, $options['value'], $operator); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * check general display conditions in frontend |
| 847 | * |
| 848 | * @param arr $options options of the condition |
| 849 | * @param obj $ad Advanced_Ads_Ad |
| 850 | * @return bool true if can be displayed |
| 851 | */ |
| 852 | static function check_general($options = array(), Advanced_Ads_Ad $ad) { |
| 853 | |
| 854 | // display by default |
| 855 | if (!isset($options['value']) || !is_array($options['value']) || !count($options['value'])) { |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | // check general conditions added by other add-ons |
| 860 | if ( null !== ( $result = apply_filters( 'advanced-ads-display-conditions-check-general', null, $options['value'] ) ) ) { |
| 861 | return $result; |
| 862 | } |
| 863 | |
| 864 | // skip checks, if general conditions are unchanged |
| 865 | if( self::$default_general_keys === $options['value'] ){ |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | // get plugin options |
| 870 | $plugin_options = Advanced_Ads_Plugin::get_instance()->options(); |
| 871 | |
| 872 | // error_log(print_r($options, true)); |
| 873 | // error_log(print_r(debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), true)); |
| 874 | |
| 875 | $ad_options = $ad->options(); |
| 876 | $query = $ad_options['wp_the_query']; |
| 877 | |
| 878 | // check main query |
| 879 | if ( isset( $query['is_main_query'] ) && ! $query['is_main_query'] && ! in_array('is_main_query', $options['value'] ) ) { |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | // check home page |
| 884 | if ( ( ( isset($query['is_front_page']) && $query['is_front_page'] ) |
| 885 | || ( isset($query['is_home']) && $query['is_home'] ) ) |
| 886 | && in_array('is_front_page', $options['value']) |
| 887 | ) { |
| 888 | return true; |
| 889 | } elseif (isset($query['is_front_page']) && $query['is_front_page'] && ( |
| 890 | !in_array('is_front_page', $options['value']) |
| 891 | )) { |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | // check common tests |
| 896 | foreach (self::$query_var_keys as $_type) { |
| 897 | if ('is_main_query' !== $_type && isset($query[$_type]) && $query[$_type] && |
| 898 | in_array($_type, $options['value'])) { |
| 899 | return true; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Check 'content age' condition in frontend. |
| 908 | * |
| 909 | * @param arr $options options of the condition |
| 910 | * @param obj $ad Advanced_Ads_Ad |
| 911 | * @return bool true if can be displayed |
| 912 | */ |
| 913 | static function check_content_age( $options = array(), Advanced_Ads_Ad $ad ) { |
| 914 | global $post; |
| 915 | |
| 916 | $operator = ( isset($options['operator']) && $options['operator'] === 'younger_than' ) ? 'younger_than' : 'older_than'; |
| 917 | $value = isset( $options['value'] ) ? $options['value'] : ''; |
| 918 | |
| 919 | if ( empty( $post->ID ) && empty( $value ) ) { |
| 920 | return true; |
| 921 | } |
| 922 | |
| 923 | // get post publish date in unix timestamp |
| 924 | $publish_time = get_the_time( 'U', $post->ID ); |
| 925 | |
| 926 | // get difference from now |
| 927 | $diff_from_now = time() - $publish_time; |
| 928 | |
| 929 | // check against entered age |
| 930 | $value_in_seconds = DAY_IN_SECONDS * $value; |
| 931 | |
| 932 | if( $operator === 'younger_than' ){ |
| 933 | return $diff_from_now < $value_in_seconds; |
| 934 | } else { |
| 935 | return $diff_from_now > $value_in_seconds; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * helper function to check for in array values |
| 941 | * |
| 942 | * @param mixed $id scalar (key) or array of keys as needle |
| 943 | * @param array $ids haystack |
| 944 | * |
| 945 | * @return boolean void if either argument is empty |
| 946 | */ |
| 947 | static function in_array($id, $ids) { |
| 948 | // empty? |
| 949 | if (!isset($id) || $id === array()) { |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | // invalid? |
| 954 | if (!is_array($ids)) { |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | return is_array($id) ? array_intersect($id, $ids) !== array() : in_array($id, $ids); |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * helper to compare ids |
| 963 | * |
| 964 | * @param arr $needle ids that should be searched for in haystack |
| 965 | * @param arr $haystack reference ids |
| 966 | * @param str $operator whether it should be included or not |
| 967 | * @return boolean |
| 968 | */ |
| 969 | static function can_display_ids($needle, $haystack, $operator = 'is') { |
| 970 | |
| 971 | if ('is' === $operator && self::in_array($needle, $haystack) === false) { |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | if ('is_not' === $operator && self::in_array($needle, $haystack) === true) { |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | return true; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * check display conditions |
| 984 | * |
| 985 | * @since 1.1.0 moved here from can_display() |
| 986 | * @since 1.7.0 moved here from display-by-query module |
| 987 | * @return bool $can_display true if can be displayed in frontend |
| 988 | */ |
| 989 | public function can_display($can_display, $ad) { |
| 990 | if (!$can_display) { |
| 991 | return false; |
| 992 | } |
| 993 | |
| 994 | $options = $ad->options(); |
| 995 | if ( |
| 996 | // test if anything is to be limited at all |
| 997 | !isset($options['conditions']) || !is_array($options['conditions']) |
| 998 | // query arguments required |
| 999 | || !isset($options['wp_the_query']) |
| 1000 | ) { |
| 1001 | return true; |
| 1002 | } |
| 1003 | // get conditions with rebased index keys |
| 1004 | $conditions = array_values( $options['conditions'] ); |
| 1005 | $query = $options['wp_the_query']; |
| 1006 | $post = isset($options['post']) ? $options['post'] : null; |
| 1007 | |
| 1008 | |
| 1009 | $last_result = false; |
| 1010 | $length = count( $conditions ); |
| 1011 | |
| 1012 | for($i = 0; $i < $length; ++$i) { |
| 1013 | $_condition = current( $conditions ); |
| 1014 | $next = next( $conditions ); |
| 1015 | $next_key = key( $conditions ); |
| 1016 | |
| 1017 | /** |
| 1018 | * force next condition’s connector to OR if |
| 1019 | * not set to OR already |
| 1020 | * this condition and the next are from the same taxonomy |
| 1021 | * the conditions don’t have the same condition type |
| 1022 | * they are both set to SHOW |
| 1023 | */ |
| 1024 | $tax = ( isset( $_condition['type'] ) && isset( $this->conditions[ $_condition['type'] ]['taxonomy'] ) ) ? $this->conditions[ $_condition['type'] ]['taxonomy'] : false; |
| 1025 | $next_tax = ( isset( $next['type'] ) && isset( $this->conditions[ $next['type'] ]['taxonomy'] ) ) ? $this->conditions[ $next['type'] ]['taxonomy'] : false; |
| 1026 | if( $tax && $next_tax && $next_key |
| 1027 | && $next_tax === $tax |
| 1028 | && ( ! isset( $next['connector'] ) || $next['connector'] !== 'or' ) |
| 1029 | && 'is' === $_condition['operator'] && 'is' === $next['operator'] |
| 1030 | && $_condition['type'] !== $next['type'] ){ |
| 1031 | // error_log(print_r('force_or', true)); |
| 1032 | $next['connector'] = 'or'; |
| 1033 | $conditions[ $next_key ]['connector'] = 'or'; |
| 1034 | } |
| 1035 | |
| 1036 | // ignore OR if last result was true |
| 1037 | if( $last_result && isset( $_condition['connector'] ) && 'or' === $_condition['connector'] ){ |
| 1038 | continue; |
| 1039 | } |
| 1040 | |
| 1041 | $last_result = $result = self::frontend_check($_condition, $ad); |
| 1042 | if( ! $result ) { |
| 1043 | // return false only, if the next condition doesn’t have an OR operator |
| 1044 | if( ! isset( $next['connector'] ) || $next['connector'] !== 'or' ) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * On demand provide current query arguments to ads. |
| 1055 | * |
| 1056 | * Existing arguments must not be overridden. |
| 1057 | * Some arguments might be cachable. |
| 1058 | * |
| 1059 | * @param array $args |
| 1060 | * |
| 1061 | * @return array |
| 1062 | */ |
| 1063 | public function ad_select_args_callback($args) { |
| 1064 | global $post, $wp_the_query, $wp_query, $numpages; |
| 1065 | |
| 1066 | if (isset($post)) { |
| 1067 | if (!isset($args['post'])) { |
| 1068 | $args['post'] = array(); |
| 1069 | } |
| 1070 | if (!isset($args['post']['id'])) { |
| 1071 | |
| 1072 | // if currently on a single site, use the main query information just in case a custom query is broken |
| 1073 | if( isset( $wp_the_query->post->ID ) && $wp_the_query->is_single() ){ |
| 1074 | $args['post']['id'] = $wp_the_query->post->ID; |
| 1075 | } else { |
| 1076 | $args['post']['id'] = $post->ID; |
| 1077 | } |
| 1078 | } |
| 1079 | if (!isset($args['post']['author'])) { |
| 1080 | // if currently on a single site, use the main query information just in case a custom query is broken |
| 1081 | if( isset( $wp_the_query->post->post_author ) && $wp_the_query->is_single() ){ |
| 1082 | $args['post']['author'] = $wp_the_query->post->post_author; |
| 1083 | } else { |
| 1084 | $args['post']['author'] = $post->post_author; |
| 1085 | } |
| 1086 | } |
| 1087 | if (!isset($args['post']['post_type'])) { |
| 1088 | // if currently on a single site, use the main query information just in case a custom query is broken |
| 1089 | if( isset( $wp_the_query->post->post_type ) && $wp_the_query->is_single() ){ |
| 1090 | $args['post']['post_type'] = $wp_the_query->post->post_type; |
| 1091 | } else { |
| 1092 | $args['post']['post_type'] = $post->post_type; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | // pass query arguments |
| 1098 | if (isset($wp_the_query)) { |
| 1099 | if (!isset($args['wp_the_query'])) { |
| 1100 | $args['wp_the_query'] = array(); |
| 1101 | } |
| 1102 | $query = $wp_the_query->get_queried_object(); |
| 1103 | // term_id exists only for taxonomy archive pages |
| 1104 | if (!isset($args['wp_the_query']['term_id']) && $query) { |
| 1105 | $args['wp_the_query']['term_id'] = isset($query->term_id) ? $query->term_id : ''; |
| 1106 | } |
| 1107 | // taxonomy |
| 1108 | if (!isset($args['wp_the_query']['taxonomy']) && $query) { |
| 1109 | $args['wp_the_query']['taxonomy'] = isset($query->taxonomy) ? $query->taxonomy : ''; |
| 1110 | } |
| 1111 | |
| 1112 | // query type/ context |
| 1113 | if (!isset($args['wp_the_query']['is_main_query'])) { |
| 1114 | $args['wp_the_query']['is_main_query'] = Advanced_Ads::get_instance()->is_main_query(); |
| 1115 | } |
| 1116 | |
| 1117 | // `<!-- nextpage -->` tags |
| 1118 | if ( ! isset( $args['wp_the_query']['page'] ) ) { |
| 1119 | $args['wp_the_query']['page'] = isset( $wp_the_query->query_vars['page'] ) && $wp_the_query->query_vars['page'] ? $wp_the_query->query_vars['page'] : 1; |
| 1120 | $args['wp_the_query']['numpages'] = isset( $numpages ) ? $numpages : 1; |
| 1121 | } |
| 1122 | |
| 1123 | // query vars |
| 1124 | foreach (self::$query_var_keys as $key) { |
| 1125 | if (!isset($args['wp_the_query'][$key])) { |
| 1126 | $args['wp_the_query'][$key] = $wp_the_query->$key(); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | return $args; |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * modify post search query to search by post_title or ID |
| 1136 | * |
| 1137 | * @param array $query |
| 1138 | * @return string |
| 1139 | */ |
| 1140 | public static function modify_post_search( $query ) { |
| 1141 | |
| 1142 | // use ID and not search field if ID given |
| 1143 | if( 0 !== absint( $query['s'] ) && strlen( $query['s'] ) == strlen ( absint( $query['s'] ) ) ){ |
| 1144 | $query['post__in'] = array( absint( $query['s'] )); |
| 1145 | unset( $query['s'] ); |
| 1146 | } |
| 1147 | |
| 1148 | $query['suppress_filters'] = false; |
| 1149 | $query['orderby'] = 'post_title'; |
| 1150 | $query['post_status'] = array( 'publish', 'pending', 'draft', 'future' ); |
| 1151 | return $query; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * modify post search sql to search only in post title |
| 1156 | * |
| 1157 | * @param string $sql |
| 1158 | * @return string |
| 1159 | */ |
| 1160 | public static function modify_post_search_sql( $sql ) { |
| 1161 | global $wpdb; |
| 1162 | |
| 1163 | // $sql = preg_replace_callback( "/{$wpdb->posts}.post_(content|excerpt)( NOT)? LIKE '%(.*?)%'/", array( 'Advanced_Ads_Display_Conditions', 'modify_post_search_sql_callback' ), $sql ); |
| 1164 | |
| 1165 | // removes the search in content and excerpt columns |
| 1166 | $sql = preg_replace( "/OR \({$wpdb->posts}.post_(content|excerpt)( NOT)? LIKE '(.*?)'\)/", '', $sql ); |
| 1167 | |
| 1168 | return $sql; |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * preg_replace callback used in modify_post_search_sql() |
| 1173 | * |
| 1174 | * @param array $matches |
| 1175 | * @return string |
| 1176 | * @deprecated since version 1.8.16 |
| 1177 | */ |
| 1178 | public static function modify_post_search_sql_callback( $matches ) { |
| 1179 | global $wpdb; |
| 1180 | if ( $matches[1] === 'content' && preg_match( '@^([0-9]+)$@', $matches[3], $matches_id ) ) { |
| 1181 | $equals_op = $matches[2] === ' NOT' ? '!=' : '='; |
| 1182 | return "{$wpdb->posts}.ID$equals_op$matches_id[1]"; |
| 1183 | } else if ( $matches[2] === ' NOT' ) { |
| 1184 | return '1=1'; |
| 1185 | } else { |
| 1186 | return '1=0'; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | } |
| 1191 |