PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.4.0
AlphaListing v4.4.0
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / PostsQuery.php
alphalisting / src / Shortcode Last commit date
QueryParts 1 month ago Extension.php 1 month ago PostsQuery.php 1 month ago Query.php 1 month ago TermsQuery.php 1 month ago
PostsQuery.php
166 lines
1 <?php
2 /**
3 * Posts 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 * PostsQuery
18 */
19 class PostsQuery extends Query {
20 /**
21 * The display/query type name.
22 *
23 * @var string
24 */
25 public $display = 'posts';
26
27 /**
28 * Get the items for the query.
29 *
30 * @since 4.0.0
31 * @param array $items The items.
32 * @param mixed $query The query.
33 * @return array<\WP_Post> The items.
34 */
35 public function get_items( array $items, $query ): array {
36 if ( is_array( $items ) && 0 < count( $items ) ) {
37 return $items;
38 }
39
40 add_filter( 'posts_fields', array( $this, 'wp_query_fields' ), 10, 2 );
41 if ( $query instanceof \WP_Query ) {
42 $items = $query->posts;
43 } else {
44 if ( isset( $query['child_of'] ) ) {
45 if ( is_array( $query['post_type'] ) ) {
46 // We set post_type as an array of types even with a single value. `get_posts` does
47 // not work with an array of post types, so we fetch each type's posts separately.
48 $items = array();
49 foreach ( $query['post_type'] as $post_type ) {
50 $partial_query = $query;
51 $partial_query['post_type'] = $post_type;
52 $partial_items = get_pages( $partial_query );
53 if ( false != $partial_items ) {
54 $items = array_merge( $items, $partial_items );
55 }
56 }
57 } else {
58 $items = get_pages( $query );
59 }
60 } else {
61 $query = wp_parse_args(
62 $query,
63 array(
64 'posts_per_page' => -1,
65 'nopaging' => true,
66 )
67 );
68 $items = ( new \WP_Query( $query ) )->posts;
69 }
70 }
71 remove_filter( 'posts_fields', array( $this, 'wp_query_fields' ) );
72
73 return empty( $items ) ? array() : $items;
74 }
75
76 /**
77 * Set the fields we require on \WP_Query.
78 *
79 * @since 3.0.0 Introduced.
80 * @since 4.0.0 Converted to static function, and moved to \AlphaListing\Shortcode\PostsQuery.
81 * @param string $fields The current fields in SQL format.
82 * @param \WP_Query $query The \WP_Query instance.
83 * @return string The new fields in SQL format.
84 */
85 public static function wp_query_fields( string $fields, \WP_Query $query ): string {
86 global $wpdb;
87 return "{$wpdb->posts}.ID, {$wpdb->posts}.post_title, {$wpdb->posts}.post_type, {$wpdb->posts}.post_name, {$wpdb->posts}.post_parent, {$wpdb->posts}.post_date";
88 }
89
90 /**
91 * Get the item.
92 *
93 * @param mixed $previous The previous item object or ID.
94 * @param mixed $item The item object or ID.
95 * @return \WP_Post The item object.
96 */
97 public function get_item( $previous, $item ) {
98 if ( $previous instanceof \WP_Post ) {
99 return $previous;
100 }
101
102 if ( $item instanceof \WP_Post ) {
103 return $item;
104 }
105
106 return get_post( $item );
107 }
108
109 /**
110 * Get the item ID.
111 *
112 * @param int $item_id The item ID.
113 * @param \WP_Post $item The item object.
114 * @return int The item ID.
115 */
116 public function get_item_id( int $item_id, $item ) {
117 if ( ! $item instanceof \WP_Post ) {
118 $item = get_post( $item );
119 }
120
121 if ( $item instanceof \WP_Post ) {
122 $item_id = $item->ID;
123 }
124
125 return $item_id;
126 }
127
128 /**
129 * Get the item title.
130 *
131 * @param string $title The item title.
132 * @param \WP_Post $item The item object.
133 * @return string The item title.
134 */
135 public function get_item_title( string $title, $item ) {
136 if ( ! $item instanceof \WP_Post ) {
137 $item = get_post( $item );
138 }
139
140 if ( $item instanceof \WP_Post ) {
141 $title = get_the_title( $item );
142 }
143
144 return $title;
145 }
146
147 /**
148 * Get the item permalink.
149 *
150 * @param string $permalink The item permalink.
151 * @param \WP_Post $item The item object.
152 * @return string The item permalink
153 */
154 public function get_item_permalink( string $permalink, $item ) {
155 if ( ! $item instanceof \WP_Post ) {
156 $item = get_post( $item );
157 }
158
159 if ( $item instanceof \WP_Post ) {
160 $permalink = get_the_permalink( $item );
161 }
162
163 return $permalink;
164 }
165 }
166