PluginProbe
WP Ultimate Post Grid / 2.0
WP Ultimate Post Grid v2.0
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 +38 -217 2.6.02.0 View file →
@@ -21,9 +21,9 @@
21 21 $update_post_post_type = $post->post_type;
22 22
23 23 if( $update_post_post_type == WPUPG_POST_TYPE )
24 24 {
25 - update_option( 'wpupg_regenerate_grids_check', array( $id ) );
25 + $this->generate( $id );
26 26 } else {
27 27 $this->update_grids_with_post_type( $update_post_post_type );
28 28 }
29 29 }
@@ -39,21 +39,16 @@
39 39
40 40 $query = new WP_Query( $args );
41 41 $posts = $query->have_posts() ? $query->posts : array();
42 42
43 - $grid_ids = array();
44 43 foreach ( $posts as $grid_post )
45 44 {
46 45 $grid = new WPUPG_Grid( $grid_post );
47 46
48 47 if( in_array( $post_type, $grid->post_types() ) ) {
49 - $grid_ids[] = $grid->ID();
48 + $this->generate( $grid->ID() );
50 49 }
51 50 }
52 -
53 - if( count( $grid_ids ) > 0 ) {
54 - update_option( 'wpupg_regenerate_grids_check', $grid_ids );
55 - }
56 51 }
57 52
58 53 public function updated_term( $term_id, $taxonomy )
59 54 {
@@ -89,8 +84,10 @@
89 84 if( $grid_ids ) {
90 85 foreach( $grid_ids as $grid_id ) {
91 86 $this->generate( $grid_id );
92 87 }
88 +
89 + update_option( 'wpupg_regenerate_grids_check', false );
93 90 }
94 91 }
95 92
96 93 public function generate( $grid_id )
@@ -96,86 +93,12 @@
96 93 public function generate( $grid_id )
97 94 {
98 95 $grid = new WPUPG_Grid( $grid_id );
99 96
100 - // Don't cache term grid
101 - if( $grid->type() !== 'terms' ) {
102 - // Check if we're in the middle of generating this grid.
103 - $temp_cache = get_post_meta( $grid->ID(), 'wpupg_temp_cache', true );
97 + $generated = $this->dynamic_generate( $grid );
104 98
105 - if ( ! is_array( $temp_cache ) ) {
106 - // Get posts
107 - $post_ids = $this->generate_cache_post_ids( $grid );
108 -
109 - $temp_cache = array(
110 - 'todo' => $post_ids,
111 - 'all' => $post_ids,
112 - 'posts_per_term' => array(),
113 - 'terms_per_post' => array(),
114 - 'filter_terms' => array(),
115 - );
116 - }
117 -
118 - $doing = array_splice( $temp_cache['todo'], 0, 50 );
119 -
120 - if ( ! empty( $doing ) ) {
121 - $terms = $this->generate_cache_terms( $grid, $doing );
122 -
123 - // Posts per term should combine the same keys.
124 - $posts_per_term = array_merge_recursive( $temp_cache['posts_per_term'], $terms['posts_per_term'] );
125 - $temp_cache['posts_per_term'] = $posts_per_term;
126 -
127 - // Terms per post should keep keys.
128 - $terms_per_post = $temp_cache['terms_per_post'] + $terms['terms_per_post'];
129 - $temp_cache['terms_per_post'] = $terms_per_post;
130 -
131 - // Filter terms requires special attention.
132 - $filter_terms = $terms['filter_terms'];
133 - unset( $terms['filter_terms'] );
134 -
135 - foreach ( $filter_terms as $slug => $term ) {
136 - if ( ! array_key_exists( $slug, $temp_cache['filter_terms'] ) ) {
137 - $temp_cache['filter_terms'][ $slug ] = $term;
138 - } else {
139 - $temp_cache['filter_terms'][ $slug ]['count'] += $term['count'];
140 - }
141 - }
142 - }
143 -
144 - if ( ! empty( $temp_cache['todo'] ) ) {
145 - // Not finished yet, save current temp cache.
146 - update_post_meta( $grid->ID(), 'wpupg_temp_cache', $temp_cache );
147 - } else {
148 - // Finished getting all the terms, generate filter.
149 - $filter_cache = $this->generate_cache_filter( $grid, $temp_cache['filter_terms'] );
150 -
151 - // Apply filters.
152 - $posts_cache = array(
153 - 'all' => $temp_cache['all'],
154 - 'taxonomies' => $temp_cache['posts_per_term'],
155 - 'terms' => $temp_cache['terms_per_post'],
156 - );
157 -
158 - $posts_cache = apply_filters( 'wpupg_grid_cache_posts', $posts_cache, $grid );
159 - $filter_cache = apply_filters( 'wpupg_grid_cache_filter', $filter_cache, $posts_cache, $grid );
160 -
161 - // Save things.
162 - update_post_meta( $grid->ID(), 'wpupg_posts', $posts_cache );
163 - update_post_meta( $grid->ID(), 'wpupg_filter', $filter_cache );
164 -
165 - // Clear temp cache.
166 - update_post_meta( $grid->ID(), 'wpupg_temp_cache', false );
167 -
168 - // Remove from grids to regenerate.
169 - $grid_ids = get_option( 'wpupg_regenerate_grids_check', array() );
170 - $grid_ids_key = array_search( $grid->ID(), $grid_ids );
171 -
172 - if ( false !== $grid_ids_key ) {
173 - unset( $grid_ids[ $grid_ids_key ] );
174 - update_option( 'wpupg_regenerate_grids_check', $grid_ids );
175 - }
176 - }
177 - }
99 + update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
100 + update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
178 101 }
179 102
180 103 public function dynamic_generate( $grid ) {
181 104 // Don't cache term grid
@@ -185,36 +108,9 @@
185 108 'filter' => '',
186 109 );
187 110 }
188 111
189 - // Get posts
190 - $post_ids = $this->generate_cache_post_ids( $grid );
191 -
192 - // Get post terms
193 - $terms_cache = $this->generate_cache_terms( $grid, $post_ids );
194 -
195 - // Cache array
196 - $cache = array(
197 - 'all' => $post_ids,
198 - 'taxonomies' => $terms_cache['posts_per_term'],
199 - 'terms' => $terms_cache['terms_per_post'],
200 - );
201 -
202 - // Generate Filter
203 - $filter_terms = $terms_cache['filter_terms'];
204 - $filter = $this->generate_cache_filter( $grid, $filter_terms );
205 -
206 - // Apply filters
207 - $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
208 - $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
209 -
210 - return array(
211 - 'cache' => $cache,
212 - 'filter' => $filter,
213 - );
214 - }
215 -
216 - public function generate_cache_post_ids( $grid ) {
112 + // Get Posts
217 113 $args = array(
218 114 'post_type' => $grid->post_types(),
219 115 'post_status' => $grid->post_status(),
220 116 'posts_per_page' => -1,
@@ -251,29 +147,25 @@
251 147 $post_ids = array_map( 'intval', $posts );
252 148
253 149 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
254 150
255 - // Offset posts
256 - if( $grid->limit_posts_offset() ) {
257 - $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
258 - }
259 -
260 151 // Limit Total # Posts
261 152 if( $grid->limit_posts_number() ) {
262 153 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
263 154 }
264 155
265 - return $post_ids;
266 - }
156 + $cache = array(
157 + 'all' => $post_ids,
158 + );
267 159
268 - public function generate_cache_terms( $grid, $post_ids ) {
160 + $taxonomies = $grid->filter_taxonomies();
161 + $limit_terms = $grid->filter_limit_terms();
162 +
163 + // Cache arrays
269 164 $posts_per_term = array();
270 165 $terms_per_post = array();
271 166 $filter_terms = array();
272 167
273 - $taxonomies = $grid->filter_taxonomies();
274 - $limit_terms = $grid->filter_limit_terms();
275 -
276 168 // Loop over all terms
277 169 foreach( $post_ids as $post_id ) {
278 170 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
279 171
@@ -279,10 +171,10 @@
279 171
280 172 foreach( $taxonomies as $taxonomy ) {
281 173 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
282 174
283 - $terms = get_the_terms( $post_id, $taxonomy );
284 - $terms = !$terms || is_wp_error( $terms ) ? array() : $terms;
175 + $terms = wp_get_post_terms( $post_id, $taxonomy );
176 + $terms = is_wp_error( $terms ) ? array() : $terms;
285 177
286 178 // Get parent terms if enabled
287 179 if( $grid->filter_match_parents() ) {
288 180 $parent_ids = array();
@@ -339,18 +231,13 @@
339 231
340 232 // Terms per post
341 233 $post_taxonomy_term_ids[] = $slug;
342 234
343 - if( !array_key_exists( $slug, $filter_terms ) ) {
344 - // Filter terms
345 - $filter_terms[$slug] = array(
346 - 'taxonomy' => $taxonomy,
347 - 'name' => $term->name,
348 - 'count' => 1,
349 - );
350 - } else {
351 - $filter_terms[$slug]['count']++;
352 - }
235 + // Filter terms
236 + $filter_terms[$slug] = array(
237 + 'taxonomy' => $taxonomy,
238 + 'name' => $term->name,
239 + );
353 240 }
354 241
355 242 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
356 243 }
@@ -355,87 +242,24 @@
355 242 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
356 243 }
357 244 }
358 245
359 - // Show Empty Terms
360 - if( $grid->filter_show_empty() ) {
361 - foreach( $taxonomies as $taxonomy ) {
362 - $terms = get_terms( $taxonomy, array(
363 - 'hide_empty' => false,
364 - ) );
246 + $cache['taxonomies'] = $posts_per_term;
247 + $cache['terms'] = $terms_per_post;
365 248
366 - foreach( $terms as $term ) {
367 - // Check if terms are being limited
368 - if( $grid->filter_limit() ) {
369 - if( $grid->filter_limit_exclude() ) {
370 - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
371 - } else {
372 - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
373 - }
374 - }
375 -
376 - $slug = urldecode( $term->slug );
377 -
378 - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
379 -
380 - if( !array_key_exists( $slug, $filter_terms ) ) {
381 - // Filter terms
382 - $filter_terms[$slug] = array(
383 - 'taxonomy' => $taxonomy,
384 - 'name' => $term->name,
385 - 'count' => 0,
386 - );
387 - }
388 - }
389 - }
390 - }
391 -
392 - return array(
393 - 'posts_per_term' => $posts_per_term,
394 - 'terms_per_post' => $terms_per_post,
395 - 'filter_terms' => $filter_terms,
396 - );
397 - }
398 -
399 - public function generate_cache_filter( $grid, $filter_terms ) {
249 + // Generate Filter
400 250 $filter = '';
401 251
402 - if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
252 + if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
403 253 $filter_style = $grid->filter_style();
404 254
405 255 // All Button
406 256 if( $filter_style['isotope']['all_button_text'] ) {
407 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active" role="button" tabindex="0">' . $filter_style['isotope']['all_button_text'] . '</div>';
257 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
408 258 }
409 259
410 - $filter_terms_order = array();
411 -
412 - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' ) {
413 - // Order alphatically per taxonomy
414 - $terms_per_taxonomy = array();
415 -
416 - foreach( $filter_terms as $slug => $term ) {
417 - $taxonomy = $term['taxonomy'];
418 - if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) {
419 - $terms_per_taxonomy[$taxonomy] = array();
420 - }
421 - $terms_per_taxonomy[$taxonomy][] = $slug;
422 - }
423 -
424 - foreach( $terms_per_taxonomy as $taxonomy => $terms ) {
425 - sort( $terms );
426 - $filter_terms_order = array_merge( $filter_terms_order, $terms );
427 - }
428 - } else {
429 - // Order alphabetically by default
430 - $filter_terms_order = array_keys( $filter_terms );
431 - sort( $filter_terms_order );
432 - }
433 -
434 - if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' ) {
435 - $filter_terms_order = array_reverse( $filter_terms_order );
436 - }
437 -
260 + $filter_terms_order = array_keys( $filter_terms );
261 + sort( $filter_terms_order );
438 262 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
439 263
440 264 foreach( $filter_terms_order as $slug ) {
441 265 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
@@ -440,22 +264,19 @@
440 264 foreach( $filter_terms_order as $slug ) {
441 265 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
442 266
443 267 if( $options ) {
444 - if( $grid->filter_count() ) {
445 - $name = $options['name'] . ' (' . $options['count'] . ')';
446 - } else {
447 - $name = $options['name'];
448 - }
449 -
450 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-isotope-term-' . $options['taxonomy'] . ' wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '" role="button" tabindex="0">' . $name . '</div>';
268 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
451 269 }
452 270 }
271 + }
453 272
454 - if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
455 - $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
456 - }
457 - }
273 + // Update Metadata
274 + $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
275 + $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
458 276
459 - return $filter;
277 + return array(
278 + 'cache' => $cache,
279 + 'filter' => $filter,
280 + );
460 281 }
461 282 }