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