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
PostsTerms.php
75 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Taxonomy Terms for posts 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\Strings; |
| 17 | |
| 18 | /** |
| 19 | * Taxonomy Terms Query Part extension for posts |
| 20 | */ |
| 21 | class PostsTerms extends TermsCommon { |
| 22 | /** |
| 23 | * The types of listing this shortcode extension may be used with. |
| 24 | * |
| 25 | * @since 4.0.0 |
| 26 | * @var array<string> |
| 27 | */ |
| 28 | public $display_types = array( 'posts' ); |
| 29 | |
| 30 | /** |
| 31 | * Update the query with this extension's additional configuration. |
| 32 | * |
| 33 | * @param \AlphaListing\Query $query The query. |
| 34 | * @param string $display The display/query type. |
| 35 | * @param string $key The name of the attribute. |
| 36 | * @param mixed $value The shortcode attribute value. |
| 37 | * @param array $attributes The complete set of shortcode attributes. |
| 38 | * @return mixed The updated query. |
| 39 | */ |
| 40 | public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) { |
| 41 | $taxonomies = isset( $attributes['taxonomy'] ) ? Strings::maybe_mb_split( ',', $attributes['taxonomy'] ) : array(); |
| 42 | |
| 43 | $terms = $this->get_terms( $value, $taxonomies ); |
| 44 | |
| 45 | $exclude_terms = $this->get_exclude_terms( $terms ); |
| 46 | $include_terms = $this->get_include_terms( $terms ); |
| 47 | $include_terms = array_diff( $include_terms, $exclude_terms ); |
| 48 | |
| 49 | $tax_query_defaults = array( 'relation' => 'AND' ); |
| 50 | if ( ! empty( $include_terms ) ) { |
| 51 | $tax_query_defaults[] = array( |
| 52 | 'taxonomy' => $attributes['taxonomy'], |
| 53 | 'field' => 'term_id', |
| 54 | 'terms' => $include_terms, |
| 55 | 'operator' => 'IN', |
| 56 | 'include_children' => false, |
| 57 | ); |
| 58 | } |
| 59 | if ( ! empty( $exclude_terms ) ) { |
| 60 | $tax_query_defaults[] = array( |
| 61 | 'taxonomy' => $attributes['taxonomy'], |
| 62 | 'field' => 'term_id', |
| 63 | 'terms' => $exclude_terms, |
| 64 | 'operator' => 'NOT IN', |
| 65 | 'include_children' => false, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $tax_query = isset( $query['tax_query'] ) ? $query['tax_query'] : array(); |
| 70 | |
| 71 | $query['tax_query'] = wp_parse_args( $tax_query, $tax_query_defaults ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 72 | return $query; |
| 73 | } |
| 74 | } |
| 75 |