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 / TermsQuery.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
TermsQuery.php
152 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 $terms = get_terms( $query ); // @phan-suppress-current-line PhanAccessMethodInternal
60
61 // get_terms() returns array|int|\WP_Error. An unregistered taxonomy yields a
62 // \WP_Error, which would violate this method's `array` return type under
63 // strict_types and fatal the page instead of rendering an empty listing.
64 if ( ! is_array( $terms ) ) {
65 return array();
66 }
67
68 return $terms;
69 }
70
71 /**
72 * Get the item.
73 *
74 * @param mixed $previous The previous item object.
75 * @param mixed $item The item object or ID.
76 * @return \WP_Term The item object.
77 */
78 public function get_item( $previous, $item ) {
79 if ( $previous instanceof \WP_Term ) {
80 return $previous;
81 }
82
83 if ( $item instanceof \WP_Term ) {
84 return $item;
85 }
86
87 return get_term( $item );
88 }
89
90 /**
91 * Get the item ID.
92 *
93 * @param int $item_id The item ID.
94 * @param \WP_Term $item The item object.
95 * @return int The item ID.
96 */
97 public function get_item_id( int $item_id, $item ) {
98 if ( ! $item instanceof \WP_Term ) {
99 $item = get_term( $item );
100 }
101
102 if ( $item instanceof \WP_Term ) {
103 $item_id = $item->term_id;
104 }
105
106 return $item_id;
107 }
108
109 /**
110 * Get the item title.
111 *
112 * @param string $title The item title.
113 * @param \WP_Term $item The item object.
114 * @return string The item title.
115 */
116 public function get_item_title( string $title, $item ) {
117 if ( ! $item instanceof \WP_Term ) {
118 $item = get_term( $item );
119 }
120
121 if ( $item instanceof \WP_Term ) {
122 $title = $item->name;
123 }
124
125 return $title;
126 }
127
128 /**
129 * Get the item permalink.
130 *
131 * @param string $permalink The item permalink.
132 * @param \WP_Term $item The item object.
133 * @return string The item permalink
134 */
135 public function get_item_permalink( string $permalink, $item ) {
136 if ( ! $item instanceof \WP_Term ) {
137 $item = get_term( $item );
138 }
139
140 if ( $item instanceof \WP_Term ) {
141 $link = get_term_link( $item );
142 // get_term_link() returns string|\WP_Error. Storing a \WP_Error here would
143 // only surface much later, as a return-type fatal in Query::get_the_permalink().
144 if ( is_string( $link ) ) {
145 $permalink = $link;
146 }
147 }
148
149 return $permalink;
150 }
151 }
152