class-opt-in-condition-abstract.php
5 months ago
class-opt-in-condition-archive-pages.php
5 months ago
class-opt-in-condition-categories.php
5 months ago
class-opt-in-condition-cookie-set.php
3 years ago
class-opt-in-condition-cpt.php
4 years ago
class-opt-in-condition-from-referrer.php
4 years ago
class-opt-in-condition-on-browser.php
3 years ago
class-opt-in-condition-on-url.php
5 months ago
class-opt-in-condition-page-templates.php
4 years ago
class-opt-in-condition-pages.php
5 months ago
class-opt-in-condition-posts.php
4 years ago
class-opt-in-condition-shown-less-than.php
3 years ago
class-opt-in-condition-source-of-arrival.php
5 months ago
class-opt-in-condition-tags.php
5 months ago
class-opt-in-condition-user-registration.php
5 months ago
class-opt-in-condition-user-roles.php
5 months ago
class-opt-in-condition-visitor-commented.php
5 years ago
class-opt-in-condition-visitor-country.php
5 years ago
class-opt-in-condition-visitor-device.php
6 years ago
class-opt-in-condition-visitor-logged-in-status.php
5 months ago
class-opt-in-condition-wc-archive-pages.php
5 months ago
class-opt-in-condition-wc-categories.php
5 months ago
class-opt-in-condition-wc-pages.php
5 months ago
class-opt-in-condition-wc-static-pages.php
5 months ago
class-opt-in-condition-wc-tags.php
5 months ago
class-opt-in-condition-wp-conditions.php
5 months ago
class-opt-in-condition-categories.php
76 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Opt_In_Condition_Categories class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since unwknown |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Class Opt_In_Condition_Categories. |
| 11 | * Handles posts categories. |
| 12 | * |
| 13 | * @since unknown |
| 14 | */ |
| 15 | class Opt_In_Condition_Categories extends Opt_In_Condition_Abstract { |
| 16 | |
| 17 | /** |
| 18 | * Returns whether the condition was met. |
| 19 | * |
| 20 | * @since unknwon |
| 21 | */ |
| 22 | public function is_allowed() { |
| 23 | |
| 24 | $selected_categories = ! empty( $this->args->categories ) ? (array) $this->args->categories : array(); |
| 25 | $filter_type = isset( $this->args->filter_type ) && in_array( $this->args->filter_type, array( 'only', 'except' ), true ) |
| 26 | ? $this->args->filter_type : 'except'; |
| 27 | |
| 28 | $current_categories = $this->get_current_categories(); |
| 29 | |
| 30 | // There was an error retrieving the categories. |
| 31 | if ( is_null( $current_categories ) ) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | if ( 'except' === $filter_type ) { |
| 36 | |
| 37 | // The current post has no categories. |
| 38 | // Matching "all categories except: {any|none}". We're matching only posts with at least 1 category. |
| 39 | if ( empty( $current_categories ) ) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | return array() === array_intersect( $current_categories, $selected_categories ); |
| 44 | |
| 45 | } else { |
| 46 | |
| 47 | // No categories were selected and the current post has no categories. |
| 48 | // Matching "only these categories: {none}". We're matching only posts with no categories. |
| 49 | if ( empty( $current_categories ) && empty( $selected_categories ) ) { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return array() !== array_intersect( $current_categories, $selected_categories ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns categories of current page|post |
| 59 | * |
| 60 | * @since 2.0.0 |
| 61 | * @return null|array |
| 62 | */ |
| 63 | private function get_current_categories() { |
| 64 | $post = self::get_post(); |
| 65 | if ( ! isset( $post ) || ! ( $post instanceof WP_Post ) |
| 66 | || ! in_array( $post->post_type, array( 'page', 'post' ), true ) |
| 67 | || ! self::check( 'is_singular' ) ) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | $terms = get_the_terms( $post, 'category' ); |
| 72 | $term_ids = $terms && ! is_wp_error( $terms ) ? wp_list_pluck( $terms, 'term_id' ) : array(); |
| 73 | return array_map( 'strval', $term_ids ); |
| 74 | } |
| 75 | } |
| 76 |