PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.7
AlphaListing v4.3.7
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / TermsQuery.php
alphalisting / src / Shortcode Last commit date
QueryParts 6 months ago Extension.php 6 months ago PostsQuery.php 6 months ago Query.php 6 months ago TermsQuery.php 6 months ago
TermsQuery.php
138 lines
1 <?php
2 /**
3 * Terms Query 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 /**
17 * TermsQuery
18 */
19 class TermsQuery extends Query {
20 /**
21 * The display/query type name.
22 *
23 * @var string
24 */
25 public $display = 'terms';
26
27 /**
28 * Execute this query extension.
29 *
30 * @param mixed $query The query.
31 * @param array $attributes The complete set of shortcode attributes.
32 * @return mixed The query.
33 */
34 public function apply_query_to_shortcode( $query, array $attributes ) {
35 $query = wp_parse_args(
36 (array) $query,
37 array(
38 'hide_empty' => 0,
39 'taxonomy' => 'category',
40 )
41 );
42
43 return parent::apply_query_to_shortcode( $query, $attributes );
44 }
45
46 /**
47 * Get the items for the query.
48 *
49 * @since 4.0.0
50 * @param array $items The items.
51 * @param mixed $query The query.
52 * @return array<\WP_Term> The items.
53 */
54 public function get_items( array $items, $query ): array {
55 if ( is_array( $items ) && 0 < count( $items ) ) {
56 return $items;
57 }
58
59 return get_terms( $query ); // @phan-suppress-current-line PhanAccessMethodInternal
60 }
61
62 /**
63 * Get the item.
64 *
65 * @param mixed $previous The previous item object.
66 * @param mixed $item The item object or ID.
67 * @return \WP_Term The item object.
68 */
69 public function get_item( $previous, $item ) {
70 if ( $previous instanceof \WP_Term ) {
71 return $previous;
72 }
73
74 if ( $item instanceof \WP_Term ) {
75 return $item;
76 }
77
78 return get_term( $item );
79 }
80
81 /**
82 * Get the item ID.
83 *
84 * @param int $item_id The item ID.
85 * @param \WP_Term $item The item object.
86 * @return int The item ID.
87 */
88 public function get_item_id( int $item_id, $item ) {
89 if ( ! $item instanceof \WP_Term ) {
90 $item = get_term( $item );
91 }
92
93 if ( $item instanceof \WP_Term ) {
94 $item_id = $item->term_id;
95 }
96
97 return $item_id;
98 }
99
100 /**
101 * Get the item title.
102 *
103 * @param string $title The item title.
104 * @param \WP_Term $item The item object.
105 * @return string The item title.
106 */
107 public function get_item_title( string $title, $item ) {
108 if ( ! $item instanceof \WP_Term ) {
109 $item = get_term( $item );
110 }
111
112 if ( $item instanceof \WP_Term ) {
113 $title = $item->name;
114 }
115
116 return $title;
117 }
118
119 /**
120 * Get the item permalink.
121 *
122 * @param string $permalink The item permalink.
123 * @param \WP_Term $item The item object.
124 * @return string The item permalink
125 */
126 public function get_item_permalink( string $permalink, $item ) {
127 if ( ! $item instanceof \WP_Term ) {
128 $item = get_term( $item );
129 }
130
131 if ( $item instanceof \WP_Term ) {
132 $permalink = get_term_link( $item );
133 }
134
135 return $permalink;
136 }
137 }
138