Alphabet.php
2 weeks ago
BackToTop.php
2 weeks ago
ColumnGap.php
2 weeks ago
ColumnWidth.php
2 weeks ago
Columns.php
2 weeks ago
CssLengthCommon.php
2 weeks ago
ExcludePosts.php
2 weeks ago
ExcludeTerms.php
2 weeks ago
GroupBy.php
2 weeks ago
HideEmptyTerms.php
2 weeks ago
HideEmpty_Deprecated.php
2 weeks ago
InstanceId.php
2 weeks ago
ParentPost.php
2 weeks ago
ParentTermCommon.php
2 weeks ago
ParentTermId.php
2 weeks ago
ParentTermSlugOrId.php
2 weeks ago
PostType.php
2 weeks ago
PostsTerms.php
2 weeks ago
SymbolsFirst.php
2 weeks ago
Taxonomy.php
2 weeks ago
TermsCommon.php
2 weeks ago
TermsTerms.php
2 weeks ago
ExcludeTerms.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Exclude Terms Query Part. |
| 4 | * |
| 5 | * @package alphalisting |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace eslin87\AlphaListing\Shortcode\QueryParts; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | use \eslin87\AlphaListing\Shortcode\Extension; |
| 17 | use \eslin87\AlphaListing\Strings; |
| 18 | |
| 19 | /** |
| 20 | * Exclude Terms Query Part extension |
| 21 | */ |
| 22 | class ExcludeTerms extends Extension { |
| 23 | /** |
| 24 | * The attribute for this Query Part. |
| 25 | * |
| 26 | * @since 4.0.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | public $attribute_name = 'exclude-terms'; |
| 30 | |
| 31 | /** |
| 32 | * The types of listing this shortcode extension may be used with. |
| 33 | * |
| 34 | * @since 4.0.0 |
| 35 | * @var array<string> |
| 36 | */ |
| 37 | public $display_types = array( 'posts', 'terms' ); |
| 38 | |
| 39 | /** |
| 40 | * Update the query with this extension's additional configuration. |
| 41 | * |
| 42 | * @param \eslin87\AlphaListing\Query $query The query. |
| 43 | * @param string $display The display/query type. |
| 44 | * @param string $key The name of the attribute. |
| 45 | * @param mixed $value The shortcode attribute value. |
| 46 | * @param array $attributes The complete set of shortcode attributes. |
| 47 | * @return mixed The updated query. |
| 48 | */ |
| 49 | public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) { |
| 50 | $exclude_terms = $this->normalize_term_ids( $value ); |
| 51 | |
| 52 | if ( empty( $exclude_terms ) ) { |
| 53 | return $query; |
| 54 | } |
| 55 | |
| 56 | if ( 'terms' === $display ) { |
| 57 | $existing_exclusions = array(); |
| 58 | |
| 59 | if ( isset( $query['exclude'] ) ) { |
| 60 | $existing_exclusions = $this->normalize_term_ids( $query['exclude'] ); |
| 61 | } |
| 62 | |
| 63 | $query['exclude'] = array_values( |
| 64 | array_unique( array_merge( $existing_exclusions, $exclude_terms ) ) |
| 65 | ); |
| 66 | |
| 67 | return $query; |
| 68 | } |
| 69 | |
| 70 | $taxonomy = isset( $attributes['taxonomy'] ) ? $attributes['taxonomy'] : 'category'; |
| 71 | |
| 72 | $exclude_clause = array( |
| 73 | 'taxonomy' => $taxonomy, |
| 74 | 'field' => 'term_id', |
| 75 | 'terms' => $exclude_terms, |
| 76 | 'operator' => 'NOT IN', |
| 77 | ); |
| 78 | |
| 79 | $tax_query = isset( $query['tax_query'] ) && is_array( $query['tax_query'] ) ? $query['tax_query'] : array(); |
| 80 | |
| 81 | if ( isset( $tax_query['relation'] ) ) { |
| 82 | $existing_relation = strtoupper( (string) $tax_query['relation'] ); |
| 83 | if ( in_array( $existing_relation, array( 'AND', 'OR' ), true ) ) { |
| 84 | $tax_query['relation'] = $existing_relation; |
| 85 | } else { |
| 86 | unset( $tax_query['relation'] ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | $tax_query[] = $exclude_clause; |
| 91 | |
| 92 | $query['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 93 | return $query; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Convert a value into an array of positive term IDs. |
| 98 | * |
| 99 | * @param mixed $value The raw value. |
| 100 | * @return array<int> The term IDs. |
| 101 | */ |
| 102 | private function normalize_term_ids( $value ): array { |
| 103 | if ( is_string( $value ) ) { |
| 104 | $value = Strings::maybe_mb_split( ',', $value ); |
| 105 | } elseif ( is_int( $value ) ) { |
| 106 | $value = array( $value ); |
| 107 | } elseif ( ! is_array( $value ) ) { |
| 108 | $value = array(); |
| 109 | } |
| 110 | |
| 111 | $value = array_map( 'intval', $value ); |
| 112 | $value = array_filter( |
| 113 | $value, |
| 114 | function ( int $term_id ): bool { |
| 115 | return 0 < $term_id; |
| 116 | } |
| 117 | ); |
| 118 | |
| 119 | return array_values( array_unique( $value ) ); |
| 120 | } |
| 121 | } |
| 122 |