QueryParts
1 month ago
Extension.php
1 month ago
PostsQuery.php
1 month ago
Query.php
1 month ago
TermsQuery.php
1 month ago
Query.php
79 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Query Type extension parent class |
| 4 | * |
| 5 | * @package alphalisting |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace eslin87\AlphaListing\Shortcode; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | use \eslin87\AlphaListing\Extension; |
| 17 | use \eslin87\AlphaListing\Singleton; |
| 18 | |
| 19 | /** |
| 20 | * Query Type extension parent class |
| 21 | */ |
| 22 | abstract class Query extends Singleton implements Extension { |
| 23 | /** |
| 24 | * The display/query type name. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $display; |
| 29 | |
| 30 | /** |
| 31 | * Initialize the extension. |
| 32 | * |
| 33 | * @since 4.0.0 |
| 34 | * @return void |
| 35 | */ |
| 36 | final public function initialize() { |
| 37 | add_filter( 'alphalisting_shortcode_query_types', array( $this, 'add_query_type' ) ); |
| 38 | add_filter( "alphalisting_shortcode_query_for_display__{$this->display}", array( $this, 'apply_query_to_shortcode' ), 5, 2 ); |
| 39 | add_filter( "alphalisting_get_items_for_display__{$this->display}", array( $this, 'get_items' ), 5, 2 ); |
| 40 | add_filter( "alphalisting_get_item_for_display__{$this->display}", array( $this, 'get_item' ), 5, 2 ); |
| 41 | add_filter( "alphalisting_get_item_id_for_display__{$this->display}", array( $this, 'get_item_id' ), 5, 2 ); |
| 42 | add_filter( "alphalisting_get_item_title_for_display__{$this->display}", array( $this, 'get_item_title' ), 5, 2 ); |
| 43 | add_filter( "alphalisting_get_item_permalink_for_display__{$this->display}", array( $this, 'get_item_permalink' ), 5, 2 ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add this query type to the list of supported query types. |
| 48 | * |
| 49 | * @param array<string> $query_types The supported query types. |
| 50 | * @return array<string> The updated query types. |
| 51 | */ |
| 52 | public function add_query_type( array $query_types ): array { |
| 53 | if ( ! in_array( $this->display, $query_types, true ) ) { |
| 54 | $query_types[] = $this->display; |
| 55 | } |
| 56 | return $query_types; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Execute this query extension. |
| 61 | * |
| 62 | * @param mixed $query The query. |
| 63 | * @param array $attributes The complete set of shortcode attributes. |
| 64 | * @return mixed The query. |
| 65 | */ |
| 66 | public function apply_query_to_shortcode( $query, array $attributes ) { |
| 67 | foreach ( $attributes as $key => $value ) { |
| 68 | if ( is_string( $value ) ) { |
| 69 | $value = trim( $value ); |
| 70 | } |
| 71 | if ( ! empty( $value ) ) { |
| 72 | $query = apply_filters( "alphalisting_shortcode_query_for_attribute__{$key}", $query, $this->display, $key, $value, $attributes ); |
| 73 | $query = apply_filters( "alphalisting_shortcode_query_for_display__{$this->display}__and_attribute__{$key}", $query, $this->display, $key, $value, $attributes ); |
| 74 | } |
| 75 | } |
| 76 | return $query; |
| 77 | } |
| 78 | } |
| 79 |