Alphabet.php
9 months ago
ColumnGap.php
9 months ago
ColumnWidth.php
9 months ago
Columns.php
9 months ago
ExcludePosts.php
9 months ago
ExcludeTerms.php
9 months ago
HideEmptyTerms.php
9 months ago
HideEmpty_Deprecated.php
9 months ago
InstanceId.php
9 months ago
ParentPost.php
9 months ago
ParentTermCommon.php
9 months ago
ParentTermId.php
9 months ago
ParentTermSlugOrId.php
9 months ago
PostType.php
9 months ago
PostsTerms.php
9 months ago
SymbolsFirst.php
9 months ago
Taxonomy.php
9 months ago
TermsCommon.php
9 months ago
TermsTerms.php
9 months ago
ExcludeTerms.php
119 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 = ["posts", "terms"]; |
| 38 | |
| 39 | /** |
| 40 | * Update the query with this extension's additional configuration. |
| 41 | * |
| 42 | * @param \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 | |
| 51 | $exclude_terms = $this->normalize_term_ids($value); |
| 52 | |
| 53 | if (empty($exclude_terms)) { |
| 54 | return $query; |
| 55 | } |
| 56 | |
| 57 | if ("terms" === $display) { |
| 58 | $existing_exclusions = []; |
| 59 | |
| 60 | if (isset($query["exclude"])) { |
| 61 | $existing_exclusions = $this->normalize_term_ids( |
| 62 | $query["exclude"], |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $query["exclude"] = array_values( |
| 67 | array_unique(array_merge($existing_exclusions, $exclude_terms)), |
| 68 | ); |
| 69 | |
| 70 | return $query; |
| 71 | } |
| 72 | |
| 73 | $taxonomy = isset($attributes["taxonomy"]) |
| 74 | ? $attributes["taxonomy"] |
| 75 | : "category"; |
| 76 | |
| 77 | $tax_query = [ |
| 78 | [ |
| 79 | "taxonomy" => $taxonomy, |
| 80 | "field" => "term_id", |
| 81 | "terms" => $exclude_terms, |
| 82 | "operator" => "NOT IN", |
| 83 | ], |
| 84 | ]; |
| 85 | |
| 86 | if (isset($query["tax_query"])) { |
| 87 | $query["tax_query"] = wp_parse_args( |
| 88 | $query["tax_query"], |
| 89 | $tax_query, |
| 90 | ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 91 | } else { |
| 92 | $query["tax_query"] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 93 | } |
| 94 | return $query; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Convert a value into an array of positive term IDs. |
| 99 | * |
| 100 | * @param mixed $value The raw value. |
| 101 | * @return array<int> |
| 102 | */ |
| 103 | private function normalize_term_ids($value): array { |
| 104 | if (is_string($value)) { |
| 105 | $value = Strings::maybe_mb_split(",", $value); |
| 106 | } elseif (is_int($value)) { |
| 107 | $value = [$value]; |
| 108 | } elseif (!is_array($value)) { |
| 109 | $value = []; |
| 110 | } |
| 111 | |
| 112 | $value = array_map("intval", $value); |
| 113 | $value = array_filter($value, function (int $term_id): bool { |
| 114 | return 0 < $term_id; |
| 115 | }); |
| 116 | |
| 117 | return array_values(array_unique($value)); |
| 118 | } |
| 119 | } |