PluginProbe
WP Ultimate Post Grid / 1.7
WP Ultimate Post Grid v1.7
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
← All changes | helpers/grid_cache.php +37 -127 2.21.7 View file →
@@ -3,20 +3,13 @@
3 3 class WPUPG_Grid_Cache {
4 4
5 5 public function __construct()
6 6 {
7 - add_action( 'add_attachment', array( $this, 'updated_attachment' ) );
8 - add_action( 'edit_attachment', array( $this, 'updated_attachment' ) );
9 7 add_action( 'save_post', array( $this, 'updated_post' ), 11, 2 );
10 8 add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 );
11 9 add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) );
12 10 }
13 11
14 - public function updated_attachment( $id )
15 - {
16 - $this->update_grids_with_post_type( 'attachment' );
17 - }
18 -
19 12 public function updated_post( $id, $post )
20 13 {
21 14 $update_post_post_type = $post->post_type;
22 15
@@ -23,30 +16,25 @@
23 16 if( $update_post_post_type == WPUPG_POST_TYPE )
24 17 {
25 18 $this->generate( $id );
26 19 } else {
27 - $this->update_grids_with_post_type( $update_post_post_type );
28 - }
29 - }
20 + $args = array(
21 + 'post_type' => WPUPG_POST_TYPE,
22 + 'post_status' => 'any',
23 + 'posts_per_page' => -1,
24 + 'nopaging' => true,
25 + );
30 26
31 - public function update_grids_with_post_type( $post_type )
32 - {
33 - $args = array(
34 - 'post_type' => WPUPG_POST_TYPE,
35 - 'post_status' => 'any',
36 - 'posts_per_page' => -1,
37 - 'nopaging' => true,
38 - );
27 + $query = new WP_Query( $args );
28 + $posts = $query->have_posts() ? $query->posts : array();
39 29
40 - $query = new WP_Query( $args );
41 - $posts = $query->have_posts() ? $query->posts : array();
30 + foreach ( $posts as $grid_post )
31 + {
32 + $grid = new WPUPG_Grid( $grid_post );
42 33
43 - foreach ( $posts as $grid_post )
44 - {
45 - $grid = new WPUPG_Grid( $grid_post );
46 -
47 - if( in_array( $post_type, $grid->post_types() ) ) {
48 - $this->generate( $grid->ID() );
34 + if( in_array( $update_post_post_type, $grid->post_types() ) ) {
35 + $this->generate( $grid->ID() );
36 + }
49 37 }
50 38 }
51 39 }
52 40
@@ -100,16 +88,8 @@
100 88 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
101 89 }
102 90
103 91 public function dynamic_generate( $grid ) {
104 - // Don't cache term grid
105 - if( $grid->type() == 'terms' ) {
106 - return array(
107 - 'cache' => array(),
108 - 'filter' => '',
109 - );
110 - }
111 -
112 92 // Get Posts
113 93 $args = array(
114 94 'post_type' => $grid->post_types(),
115 95 'post_status' => $grid->post_status(),
@@ -114,37 +94,26 @@
114 94 'post_type' => $grid->post_types(),
115 95 'post_status' => $grid->post_status(),
116 96 'posts_per_page' => -1,
117 97 'nopaging' => true,
98 + 'orderby' => $grid->order_by(),
118 99 'order' => $grid->order(),
119 100 'fields' => 'ids',
120 101 );
121 102
122 - if( $grid->order_by() == 'custom' ) {
123 - $args['meta_key'] = $grid->order_custom_key();
124 - $args['orderby'] = $grid->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
125 - } else {
126 - $args['orderby'] = $grid->order_by();
127 - }
128 -
129 103 // Images Only
130 104 if( $grid->images_only() ) {
131 - if( in_array( 'attachment', $grid->post_types() ) ) {
132 - $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
133 - } else {
134 - $args['meta_query'] = array(
135 - array(
136 - 'key' => '_thumbnail_id',
137 - 'value' => '0',
138 - 'compare' => '>'
139 - ),
140 - );
141 - }
105 + $args['meta_query'] = array(
106 + array(
107 + 'key' => '_thumbnail_id',
108 + 'value' => '0',
109 + 'compare' => '>'
110 + ),
111 + );
142 112 }
143 113
144 114 $query = new WP_Query( $args );
145 115 $posts = $query->have_posts() ? $query->posts : array();
146 -
147 116 $post_ids = array_map( 'intval', $posts );
148 117
149 118 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
150 119
@@ -172,9 +141,8 @@
172 141 foreach( $taxonomies as $taxonomy ) {
173 142 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
174 143
175 144 $terms = wp_get_post_terms( $post_id, $taxonomy );
176 - $terms = is_wp_error( $terms ) ? array() : $terms;
177 145
178 146 // Get parent terms if enabled
179 147 if( $grid->filter_match_parents() ) {
180 148 $parent_ids = array();
@@ -207,41 +175,29 @@
207 175
208 176 $post_taxonomy_term_ids = array();
209 177
210 178 foreach( $terms as $term ) {
211 - // Check if terms are being limited
212 - if( $grid->filter_limit() ) {
213 - if( $grid->filter_limit_exclude() ) {
214 - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
215 - } else {
216 - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
179 + if( !$grid->filter_limit() || ( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) ) {
180 + $slug = urldecode( $term->slug );
181 +
182 + // Make sure we only handle each term once
183 + if( $grid->filter_match_parents() ) {
184 + if( in_array( $slug, $handled_terms ) ) continue;
185 + $handled_terms[] = $slug;
217 186 }
218 - }
219 187
220 - $slug = urldecode( $term->slug );
188 + // Posts per term
189 + if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
190 + $posts_per_term[$taxonomy][$slug][] = $post_id;
221 191
222 - // Make sure we only handle each term once
223 - if( $grid->filter_match_parents() ) {
224 - if( in_array( $slug, $handled_terms ) ) continue;
225 - $handled_terms[] = $slug;
226 - }
192 + // Terms per post
193 + $post_taxonomy_term_ids[] = $slug;
227 194
228 - // Posts per term
229 - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
230 - $posts_per_term[$taxonomy][$slug][] = $post_id;
231 -
232 - // Terms per post
233 - $post_taxonomy_term_ids[] = $slug;
234 -
235 - if( !array_key_exists( $slug, $filter_terms ) ) {
236 195 // Filter terms
237 196 $filter_terms[$slug] = array(
238 197 'taxonomy' => $taxonomy,
239 198 'name' => $term->name,
240 - 'count' => 1,
241 199 );
242 - } else {
243 - $filter_terms[$slug]['count']++;
244 200 }
245 201 }
246 202
247 203 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
@@ -247,41 +203,8 @@
247 203 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
248 204 }
249 205 }
250 206
251 - // Show Empty Terms
252 - if( $grid->filter_show_empty() ) {
253 - foreach( $taxonomies as $taxonomy ) {
254 - $terms = get_terms( $taxonomy, array(
255 - 'hide_empty' => false,
256 - ) );
257 -
258 - foreach( $terms as $term ) {
259 - // Check if terms are being limited
260 - if( $grid->filter_limit() ) {
261 - if( $grid->filter_limit_exclude() ) {
262 - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
263 - } else {
264 - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
265 - }
266 - }
267 -
268 - $slug = urldecode( $term->slug );
269 -
270 - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
271 -
272 - if( !array_key_exists( $slug, $filter_terms ) ) {
273 - // Filter terms
274 - $filter_terms[$slug] = array(
275 - 'taxonomy' => $taxonomy,
276 - 'name' => $term->name,
277 - 'count' => 0,
278 - );
279 - }
280 - }
281 - }
282 - }
283 -
284 207 $cache['taxonomies'] = $posts_per_term;
285 208 $cache['terms'] = $terms_per_post;
286 209
287 210 // Generate Filter
@@ -294,24 +217,11 @@
294 217 if( $filter_style['isotope']['all_button_text'] ) {
295 218 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
296 219 }
297 220
298 - $filter_terms_order = array_keys( $filter_terms );
299 - sort( $filter_terms_order );
300 - $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
301 -
302 - foreach( $filter_terms_order as $slug ) {
303 - $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
304 -
305 - if( $options ) {
306 - if( $grid->filter_count() ) {
307 - $name = $options['name'] . ' (' . $options['count'] . ')';
308 - } else {
309 - $name = $options['name'];
310 - }
311 -
312 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $name . '</div>';
313 - }
221 + ksort( $filter_terms );
222 + foreach( $filter_terms as $slug => $options ) {
223 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
314 224 }
315 225 }
316 226
317 227 // Update Metadata