PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.2.15
Nested Pages v3.2.15
3.2.15 3.2.14 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Entities / Listing / ListingQuery.php
wp-nested-pages / app / Entities / Listing Last commit date
Listing.php 2 years ago ListingActions.php 8 years ago ListingQuery.php 3 years ago ListingRepository.php 2 days ago
ListingQuery.php
219 lines
1 <?php
2 namespace NestedPages\Entities\Listing;
3
4 use NestedPages\Entities\Listing\ListingRepository;
5 use NestedPages\Entities\PostType\PostTypeRepository;
6 use NestedPages\Config\SettingsRepository;
7 use NestedPages\Entities\PluginIntegration\IntegrationFactory;
8
9 /**
10 * Performs the query for the page listing and formats into a multidemensional array
11 */
12 class ListingQuery
13 {
14 private $sort_options;
15
16 private $listing_repo;
17
18 private $post_type_repo;
19
20 private $settings;
21
22 private $integrations;
23
24 private $post_type;
25
26 private $h_taxonomies;
27
28 private $f_taxonomies;
29
30 public function __construct()
31 {
32 $this->listing_repo = new ListingRepository;
33 $this->post_type_repo = new PostTypeRepository;
34 $this->settings = new SettingsRepository;
35 $this->integrations = new IntegrationFactory;
36 }
37
38 /**
39 * Set the Sort Options
40 */
41 private function setSortOptions()
42 {
43 $this->sort_options = new \StdClass();
44 $this->setOrderBy();
45 $this->setOrder();
46 $this->sort_options->author = isset($_GET['author'])
47 ? sanitize_text_field($_GET['author'])
48 : null;
49 }
50
51 /**
52 * Set Order By
53 */
54 private function setOrderBy()
55 {
56 $orderby = ( isset($_GET['orderby']) && $_GET['orderby'] !== "" ) ? sanitize_text_field($_GET['orderby']) : 'menu_order';
57 $initial_orderby = $this->post_type_repo->defaultSortOption($this->post_type->name, 'orderby');
58 if ( $initial_orderby && !isset($_GET['orderby']) ) $orderby = $initial_orderby;
59 $this->sort_options->orderby = $orderby;
60 }
61
62 /**
63 * Set Order
64 */
65 private function setOrder()
66 {
67 $order = ( isset($_GET['order']) && $_GET['order'] !== "" ) ? sanitize_text_field($_GET['order']) : 'ASC';
68 $initial_order = $this->post_type_repo->defaultSortOption($this->post_type->name, 'order');
69 if ( $initial_order && !isset($_GET['order']) ) $order = $initial_order;
70 $this->sort_options->order = $order;
71 }
72
73 /**
74 * Get the Posts
75 */
76 public function getPosts($post_type, $h_taxonomies = [], $f_taxonomies = [])
77 {
78 $this->post_type = $post_type;
79
80 $this->setSortOptions();
81 $wpml = $this->integrations->plugins->wpml->installed;
82 $this->h_taxonomies = $h_taxonomies;
83 $this->f_taxonomies = $f_taxonomies;
84
85 $this->setTaxonomyFilters();
86
87 if ( $this->post_type->name == 'page' ) {
88 $post_type = ['page'];
89 if ( !$this->settings->menusDisabled() && !$wpml ) $post_type[] = 'np-redirect';
90 } else {
91 $post_type = [$post_type->name];
92 }
93
94 $statuses = ['publish', 'pending', 'draft', 'private', 'future', 'trash'];
95 $post_type_settings = $this->post_type_repo->getSinglePostType($post_type[0]);
96 if ( isset($post_type_settings->custom_statuses) ) $statuses = array_merge($statuses, $post_type_settings->custom_statuses);
97
98 $query_args = [
99 'post_type' => $post_type,
100 'posts_per_page' => -1,
101 'author' => $this->sort_options->author,
102 'orderby' => $this->sort_options->orderby,
103 'post_status' => apply_filters('nestedpages_listing_statuses', $statuses, $this->post_type),
104 'order' => $this->sort_options->order
105 ];
106
107 if ( $this->listing_repo->isSearch() ) $query_args = $this->searchParams($query_args);
108 if ( $this->listing_repo->isFiltered() ) $query_args = $this->filterParams($query_args);
109 if ( $this->sort_options->tax_query ) $query_args['tax_query'] = $this->sort_options->tax_query;
110
111 $query_args = apply_filters('nestedpages_page_listing', $query_args, $this->post_type);
112
113 add_filter( 'posts_clauses', [$this, 'queryFilter']);
114 $all_posts = new \WP_Query($query_args);
115 remove_filter( 'posts_clauses', [$this, 'queryFilter']);
116
117 if ( $all_posts->have_posts() ) :
118 return $all_posts->posts;
119 endif; wp_reset_postdata();
120 return null;
121 }
122
123 /**
124 * Search Paramaters
125 */
126 private function searchParams($query_args)
127 {
128 $query_args['s'] = sanitize_text_field($_GET['search']);
129 unset($query_args['post_parent']);
130 return $query_args;
131 }
132
133 /**
134 * Filter Posts
135 */
136 private function filterParams($query_args)
137 {
138 if ( !isset($_GET['category']) ) return $query_args;
139 $query_args['cat'] = sanitize_text_field($_GET['category']);
140 return $query_args;
141 }
142
143 /**
144 * Add Taxonomy Filters to the sort options if applicable
145 */
146 private function setTaxonomyFilters()
147 {
148 $taxonomies = array_merge($this->h_taxonomies, $this->f_taxonomies);
149 $tax_query = [];
150 foreach ( $taxonomies as $tax ) :
151 if ( $this->post_type_repo->sortOptionEnabled($this->post_type->name, $tax->name, true) && isset($_GET[$tax->name]) ) :
152 $tax_query[] = [
153 'taxonomy' => $tax->name,
154 'fields' => 'term_id',
155 'terms' => sanitize_text_field($_GET[$tax->name])
156 ];
157 endif;
158 endforeach;
159 $this->sort_options->tax_query = ( !empty($tax_query) ) ? $tax_query : false;
160 }
161
162 /**
163 * Query filter to add taxonomies to return data
164 * Fixes N+1 problem with taxonomies, eliminating need to query on every post
165 */
166 public function queryFilter($pieces)
167 {
168 global $wpdb;
169
170 // Add Hierarchical Categories
171 $c = 0;
172 foreach($this->h_taxonomies as $tax){
173 $name = $tax->name;
174 $name_simple = sanitize_text_field(str_replace('-', '', $tax->name));
175 if ( $c == 0 ) $tr = 'tr_' . $name_simple;
176 $tt = 'tt_' . $name_simple;
177 $t = 't_' . $name_simple;
178
179 if ( $c == 0 ) :
180 $pieces['join'] .= "
181 LEFT JOIN `$wpdb->term_relationships` AS $tr ON $tr.object_id = $wpdb->posts.ID
182 LEFT JOIN `$wpdb->term_taxonomy` AS $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id AND $tt.taxonomy = '$name'
183 LEFT JOIN `$wpdb->terms` AS $t ON $t.term_id = $tt.term_id";
184 else :
185 $pieces['join'] .= "
186 LEFT JOIN `$wpdb->term_taxonomy` AS $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id AND $tt.taxonomy = '$name'
187 LEFT JOIN `$wpdb->terms` AS $t ON $t.term_id = $tt.term_id";
188 endif ;
189 $pieces['fields'] .= ", GROUP_CONCAT(DISTINCT $t.term_id SEPARATOR ',') AS '$name'";
190 $c++;
191 }
192
193 // Add Flat Categories
194 $c = 0;
195 foreach($this->f_taxonomies as $tax){
196 $name = $tax->name;
197 $name_simple = sanitize_text_field(str_replace('-', '', $tax->name));
198 if ( $c == 0 ) $tr = 'tr_' . $name_simple;
199 $tt = 'tt_' . $name_simple;
200 $t = 't_' . $name_simple;
201
202 if ( $c == 0 ) :
203 $pieces['join'] .= "
204 LEFT JOIN `$wpdb->term_relationships` AS $tr ON $tr.object_id = $wpdb->posts.ID
205 LEFT JOIN `$wpdb->term_taxonomy` AS $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id AND $tt.taxonomy = '$name'
206 LEFT JOIN `$wpdb->terms` AS $t ON $t.term_id = $tt.term_id";
207 else :
208 $pieces['join'] .= "
209 LEFT JOIN `$wpdb->term_taxonomy` AS $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id AND $tt.taxonomy = '$name'
210 LEFT JOIN `$wpdb->terms` AS $t ON $t.term_id = $tt.term_id";
211 endif ;
212 $pieces['fields'] .= ",GROUP_CONCAT(DISTINCT $t.term_id SEPARATOR ',') AS '$name'";
213 $c++;
214 }
215
216 $pieces['groupby'] = "$wpdb->posts.ID";
217 return $pieces;
218 }
219 }