PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.5.0
AlphaListing v4.5.0
4.5.0 trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / Query.php
alphalisting / src / Shortcode Last commit date
QueryParts 2 weeks ago Extension.php 2 weeks ago PostsQuery.php 2 weeks ago Query.php 2 weeks ago TermsQuery.php 2 weeks ago
Query.php
78 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\Singleton;
17
18 /**
19 * Query Type extension parent class
20 */
21 abstract class Query extends Singleton {
22 /**
23 * The display/query type name.
24 *
25 * @var string
26 */
27 public $display;
28
29 /**
30 * Initialize the extension.
31 *
32 * @since 4.0.0
33 * @return void
34 */
35 final public function initialize() {
36 add_filter( 'alphalisting_shortcode_query_types', array( $this, 'add_query_type' ) );
37 add_filter( "alphalisting_shortcode_query_for_display__{$this->display}", array( $this, 'apply_query_to_shortcode' ), 5, 2 );
38 add_filter( "alphalisting_get_items_for_display__{$this->display}", array( $this, 'get_items' ), 5, 2 );
39 add_filter( "alphalisting_get_item_for_display__{$this->display}", array( $this, 'get_item' ), 5, 2 );
40 add_filter( "alphalisting_get_item_id_for_display__{$this->display}", array( $this, 'get_item_id' ), 5, 2 );
41 add_filter( "alphalisting_get_item_title_for_display__{$this->display}", array( $this, 'get_item_title' ), 5, 2 );
42 add_filter( "alphalisting_get_item_permalink_for_display__{$this->display}", array( $this, 'get_item_permalink' ), 5, 2 );
43 }
44
45 /**
46 * Add this query type to the list of supported query types.
47 *
48 * @param array<string> $query_types The supported query types.
49 * @return array<string> The updated query types.
50 */
51 public function add_query_type( array $query_types ): array {
52 if ( ! in_array( $this->display, $query_types, true ) ) {
53 $query_types[] = $this->display;
54 }
55 return $query_types;
56 }
57
58 /**
59 * Execute this query extension.
60 *
61 * @param mixed $query The query.
62 * @param array $attributes The complete set of shortcode attributes.
63 * @return mixed The query.
64 */
65 public function apply_query_to_shortcode( $query, array $attributes ) {
66 foreach ( $attributes as $key => $value ) {
67 if ( is_string( $value ) ) {
68 $value = trim( $value );
69 }
70 if ( ! empty( $value ) ) {
71 $query = apply_filters( "alphalisting_shortcode_query_for_attribute__{$key}", $query, $this->display, $key, $value, $attributes );
72 $query = apply_filters( "alphalisting_shortcode_query_for_display__{$this->display}__and_attribute__{$key}", $query, $this->display, $key, $value, $attributes );
73 }
74 }
75 return $query;
76 }
77 }
78