Alphabet.php
1 year ago
ColumnGap.php
1 year ago
ColumnWidth.php
1 year ago
Columns.php
1 year ago
ExcludePosts.php
1 year ago
ExcludeTerms.php
1 year ago
HideEmptyTerms.php
1 year ago
HideEmpty_Deprecated.php
1 year ago
InstanceId.php
1 year ago
ParentPost.php
1 year ago
ParentTermCommon.php
1 year ago
ParentTermId.php
1 year ago
ParentTermSlugOrId.php
1 year ago
PostType.php
1 year ago
PostsTerms.php
1 year ago
SymbolsFirst.php
1 year ago
Taxonomy.php
1 year ago
TermsCommon.php
1 year ago
TermsTerms.php
1 year ago
ExcludeTerms.php
84 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' ); |
| 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 | $exclude_terms = Strings::maybe_mb_split( ',', $value ); |
| 51 | $exclude_terms = array_map( 'trim', $exclude_terms ); |
| 52 | $exclude_terms = array_map( 'intval', $exclude_terms ); |
| 53 | $exclude_terms = array_filter( |
| 54 | $exclude_terms, |
| 55 | function( int $value ): bool { |
| 56 | return 0 < $value; |
| 57 | } |
| 58 | ); |
| 59 | $exclude_terms = array_unique( $exclude_terms ); |
| 60 | |
| 61 | if ( empty( $exclude_terms ) ) { |
| 62 | return $query; |
| 63 | } |
| 64 | |
| 65 | $taxonomy = isset( $attributes['taxonomy'] ) ? $attributes['taxonomy'] : 'category'; |
| 66 | |
| 67 | $tax_query = array( |
| 68 | array( |
| 69 | 'taxonomy' => $taxonomy, |
| 70 | 'field' => 'slug', |
| 71 | 'terms' => $exclude_terms, |
| 72 | 'operator' => 'NOT IN', |
| 73 | ), |
| 74 | ); |
| 75 | |
| 76 | if ( isset( $query['tax_query'] ) ) { |
| 77 | $query['tax_query'] = wp_parse_args( $query['tax_query'], $tax_query ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 78 | } else { |
| 79 | $query['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 80 | } |
| 81 | return $query; |
| 82 | } |
| 83 | } |
| 84 |