PluginProbe
WP Ultimate Post Grid / 2.4.0
WP Ultimate Post Grid v2.4.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 +34 -146 2.8.22.4.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 }
@@ -42,9 +42,9 @@
42 42
43 43 $grid_ids = array();
44 44 foreach ( $posts as $grid_post )
45 45 {
46 - $grid = WPUPG_Grid_Manager::get( $grid_post );
46 + $grid = new WPUPG_Grid( $grid_post );
47 47
48 48 if( in_array( $post_type, $grid->post_types() ) ) {
49 49 $grid_ids[] = $grid->ID();
50 50 }
@@ -69,9 +69,9 @@
69 69
70 70 $grid_ids = array();
71 71 foreach ( $posts as $post )
72 72 {
73 - $grid = WPUPG_Grid_Manager::get( $post );
73 + $grid = new WPUPG_Grid( $post );
74 74
75 75 if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) {
76 76 $grid_ids[] = $grid->ID();
77 77 }
@@ -89,93 +89,21 @@
89 89 if( $grid_ids ) {
90 90 foreach( $grid_ids as $grid_id ) {
91 91 $this->generate( $grid_id );
92 92 }
93 +
94 + update_option( 'wpupg_regenerate_grids_check', false );
93 95 }
94 96 }
95 97
96 98 public function generate( $grid_id )
97 99 {
98 - $grid = WPUPG_Grid_Manager::get( $grid_id );
100 + $grid = new WPUPG_Grid( $grid_id );
99 101
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 );
102 + $generated = $this->dynamic_generate( $grid );
104 103
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 - }
104 + update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
105 + update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
178 106 }
179 107
180 108 public function dynamic_generate( $grid ) {
181 109 // Don't cache term grid
@@ -185,36 +113,9 @@
185 113 'filter' => '',
186 114 );
187 115 }
188 116
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 ) {
117 + // Get Posts
217 118 $args = array(
218 119 'post_type' => $grid->post_types(),
219 120 'post_status' => $grid->post_status(),
220 121 'posts_per_page' => -1,
@@ -244,10 +145,8 @@
244 145 );
245 146 }
246 147 }
247 148
248 - $args = apply_filters( 'wpupg_grid_cache_post_ids_args', $args, $grid );
249 -
250 149 $query = new WP_Query( $args );
251 150 $posts = $query->have_posts() ? $query->posts : array();
252 151
253 152 $post_ids = array_map( 'intval', $posts );
@@ -253,29 +152,25 @@
253 152 $post_ids = array_map( 'intval', $posts );
254 153
255 154 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
256 155
257 - // Offset posts
258 - if( $grid->limit_posts_offset() ) {
259 - $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
260 - }
261 -
262 156 // Limit Total # Posts
263 157 if( $grid->limit_posts_number() ) {
264 158 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
265 159 }
266 160
267 - return $post_ids;
268 - }
161 + $cache = array(
162 + 'all' => $post_ids,
163 + );
269 164
270 - public function generate_cache_terms( $grid, $post_ids ) {
165 + $taxonomies = $grid->filter_taxonomies();
166 + $limit_terms = $grid->filter_limit_terms();
167 +
168 + // Cache arrays
271 169 $posts_per_term = array();
272 170 $terms_per_post = array();
273 171 $filter_terms = array();
274 172
275 - $taxonomies = $grid->filter_taxonomies();
276 - $limit_terms = $grid->filter_limit_terms();
277 -
278 173 // Loop over all terms
279 174 foreach( $post_ids as $post_id ) {
280 175 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
281 176
@@ -281,10 +176,10 @@
281 176
282 177 foreach( $taxonomies as $taxonomy ) {
283 178 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
284 179
285 - $terms = get_the_terms( $post_id, $taxonomy );
286 - $terms = !$terms || is_wp_error( $terms ) ? array() : $terms;
180 + $terms = wp_get_post_terms( $post_id, $taxonomy );
181 + $terms = is_wp_error( $terms ) ? array() : $terms;
287 182
288 183 // Get parent terms if enabled
289 184 if( $grid->filter_match_parents() ) {
290 185 $parent_ids = array();
@@ -390,16 +285,12 @@
390 285 }
391 286 }
392 287 }
393 288
394 - return array(
395 - 'posts_per_term' => $posts_per_term,
396 - 'terms_per_post' => $terms_per_post,
397 - 'filter_terms' => $filter_terms,
398 - );
399 - }
289 + $cache['taxonomies'] = $posts_per_term;
290 + $cache['terms'] = $terms_per_post;
400 291
401 - public function generate_cache_filter( $grid, $filter_terms ) {
292 + // Generate Filter
402 293 $filter = '';
403 294
404 295 if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
405 296 $filter_style = $grid->filter_style();
@@ -405,14 +296,14 @@
405 296 $filter_style = $grid->filter_style();
406 297
407 298 // All Button
408 299 if( $filter_style['isotope']['all_button_text'] ) {
409 - $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>';
300 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
410 301 }
411 302
412 303 $filter_terms_order = array();
413 304
414 - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies_grouped' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ) {
305 + if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' ) {
415 306 // Order alphatically per taxonomy
416 307 $terms_per_taxonomy = array();
417 308
418 309 foreach( $filter_terms as $slug => $term ) {
@@ -432,15 +323,14 @@
432 323 $filter_terms_order = array_keys( $filter_terms );
433 324 sort( $filter_terms_order );
434 325 }
435 326
436 - if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ) {
327 + if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' ) {
437 328 $filter_terms_order = array_reverse( $filter_terms_order );
438 329 }
439 330
440 331 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
441 332
442 - $current_taxonomy = '';
443 333 foreach( $filter_terms_order as $slug ) {
444 334 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
445 335
446 336 if( $options ) {
@@ -449,18 +339,9 @@
449 339 } else {
450 340 $name = $options['name'];
451 341 }
452 342
453 - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies_grouped' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ){
454 - if( ( $options['taxonomy'] != $current_taxonomy ) && $current_taxonomy != '' ){
455 - $filter .= '</div>';
456 - }
457 - if( $options['taxonomy'] != $current_taxonomy ){
458 - $filter .= '<div class="wpupg-filter-tax-container wpupg-filter-tax-' . $options['taxonomy'] . '">';
459 - }
460 - $current_taxonomy = $options['taxonomy'];
461 - }
462 - $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>';
343 + $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 . '">' . $name . '</div>';
463 344 }
464 345 }
465 346
466 347 if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
@@ -467,7 +348,14 @@
467 348 $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
468 349 }
469 350 }
470 351
471 - return $filter;
352 + // Update Metadata
353 + $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
354 + $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
355 +
356 + return array(
357 + 'cache' => $cache,
358 + 'filter' => $filter,
359 + );
472 360 }
473 361 }