PluginProbe
WP Ultimate Post Grid / 2.8.0
WP Ultimate Post Grid v2.8.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
wp-ultimate-post-grid / helpers / grid_cache.php

grid_cache.php in WP Ultimate Post Grid 2.8.0, at helpers/grid_cache.php

473 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPUPG_Grid_Cache {
4
5 public function __construct()
6 {
7 add_action( 'add_attachment', array( $this, 'updated_attachment' ) );
8 add_action( 'edit_attachment', array( $this, 'updated_attachment' ) );
9 add_action( 'save_post', array( $this, 'updated_post' ), 11, 2 );
10 add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 );
11 add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) );
12 }
13
14 public function updated_attachment( $id )
15 {
16 $this->update_grids_with_post_type( 'attachment' );
17 }
18
19 public function updated_post( $id, $post )
20 {
21 $update_post_post_type = $post->post_type;
22
23 if( $update_post_post_type == WPUPG_POST_TYPE )
24 {
25 update_option( 'wpupg_regenerate_grids_check', array( $id ) );
26 } else {
27 $this->update_grids_with_post_type( $update_post_post_type );
28 }
29 }
30
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 );
39
40 $query = new WP_Query( $args );
41 $posts = $query->have_posts() ? $query->posts : array();
42
43 $grid_ids = array();
44 foreach ( $posts as $grid_post )
45 {
46 $grid = WPUPG_Grid_Manager::get( $grid_post );
47
48 if( in_array( $post_type, $grid->post_types() ) ) {
49 $grid_ids[] = $grid->ID();
50 }
51 }
52
53 if( count( $grid_ids ) > 0 ) {
54 update_option( 'wpupg_regenerate_grids_check', $grid_ids );
55 }
56 }
57
58 public function updated_term( $term_id, $taxonomy )
59 {
60 $args = array(
61 'post_type' => WPUPG_POST_TYPE,
62 'post_status' => 'any',
63 'posts_per_page' => -1,
64 'nopaging' => true,
65 );
66
67 $query = new WP_Query( $args );
68 $posts = $query->have_posts() ? $query->posts : array();
69
70 $grid_ids = array();
71 foreach ( $posts as $post )
72 {
73 $grid = WPUPG_Grid_Manager::get( $post );
74
75 if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) {
76 $grid_ids[] = $grid->ID();
77 }
78 }
79
80 if( count( $grid_ids ) > 0 ) {
81 update_option( 'wpupg_regenerate_grids_check', $grid_ids );
82 }
83 }
84
85 public function regenerate_grids_check()
86 {
87 $grid_ids = get_option( 'wpupg_regenerate_grids_check', false );
88
89 if( $grid_ids ) {
90 foreach( $grid_ids as $grid_id ) {
91 $this->generate( $grid_id );
92 }
93 }
94 }
95
96 public function generate( $grid_id )
97 {
98 $grid = WPUPG_Grid_Manager::get( $grid_id );
99
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 );
104
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 }
178 }
179
180 public function dynamic_generate( $grid ) {
181 // Don't cache term grid
182 if( $grid->type() == 'terms' ) {
183 return array(
184 'cache' => array(),
185 'filter' => '',
186 );
187 }
188
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 ) {
217 $args = array(
218 'post_type' => $grid->post_types(),
219 'post_status' => $grid->post_status(),
220 'posts_per_page' => -1,
221 'nopaging' => true,
222 'order' => $grid->order(),
223 'fields' => 'ids',
224 );
225
226 if( $grid->order_by() == 'custom' ) {
227 $args['meta_key'] = $grid->order_custom_key();
228 $args['orderby'] = $grid->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
229 } else {
230 $args['orderby'] = $grid->order_by();
231 }
232
233 // Images Only
234 if( $grid->images_only() ) {
235 if( in_array( 'attachment', $grid->post_types() ) ) {
236 $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
237 } else {
238 $args['meta_query'] = array(
239 array(
240 'key' => '_thumbnail_id',
241 'value' => '0',
242 'compare' => '>'
243 ),
244 );
245 }
246 }
247
248 $args = apply_filters( 'wpupg_grid_cache_post_ids_args', $args, $grid );
249
250 $query = new WP_Query( $args );
251 $posts = $query->have_posts() ? $query->posts : array();
252
253 $post_ids = array_map( 'intval', $posts );
254
255 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
256
257 // Offset posts
258 if( $grid->limit_posts_offset() ) {
259 $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
260 }
261
262 // Limit Total # Posts
263 if( $grid->limit_posts_number() ) {
264 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
265 }
266
267 return $post_ids;
268 }
269
270 public function generate_cache_terms( $grid, $post_ids ) {
271 $posts_per_term = array();
272 $terms_per_post = array();
273 $filter_terms = array();
274
275 $taxonomies = $grid->filter_taxonomies();
276 $limit_terms = $grid->filter_limit_terms();
277
278 // Loop over all terms
279 foreach( $post_ids as $post_id ) {
280 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
281
282 foreach( $taxonomies as $taxonomy ) {
283 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
284
285 $terms = get_the_terms( $post_id, $taxonomy );
286 $terms = !$terms || is_wp_error( $terms ) ? array() : $terms;
287
288 // Get parent terms if enabled
289 if( $grid->filter_match_parents() ) {
290 $parent_ids = array();
291 $parents = array();
292
293 foreach( $terms as $term ) {
294 if( $term->parent != 0 ) {
295 $parent_ids[] = $term->parent;
296 }
297 }
298
299 while( count( $parent_ids ) > 0 )
300 {
301 $children_ids = $parent_ids;
302 $parent_ids = array();
303
304 foreach( $children_ids as $child ) {
305 $term = get_term( $child, $taxonomy );
306 $parents[] = $term;
307
308 if( $term->parent != 0 ) {
309 $parent_ids[] = $term->parent;
310 }
311 }
312 }
313
314 $terms = array_merge( $terms, $parents );
315 $handled_terms = array();
316 }
317
318 $post_taxonomy_term_ids = array();
319
320 foreach( $terms as $term ) {
321 // Check if terms are being limited
322 if( $grid->filter_limit() ) {
323 if( $grid->filter_limit_exclude() ) {
324 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
325 } else {
326 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
327 }
328 }
329
330 $slug = urldecode( $term->slug );
331
332 // Make sure we only handle each term once
333 if( $grid->filter_match_parents() ) {
334 if( in_array( $slug, $handled_terms ) ) continue;
335 $handled_terms[] = $slug;
336 }
337
338 // Posts per term
339 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
340 $posts_per_term[$taxonomy][$slug][] = $post_id;
341
342 // Terms per post
343 $post_taxonomy_term_ids[] = $slug;
344
345 if( !array_key_exists( $slug, $filter_terms ) ) {
346 // Filter terms
347 $filter_terms[$slug] = array(
348 'taxonomy' => $taxonomy,
349 'name' => $term->name,
350 'count' => 1,
351 );
352 } else {
353 $filter_terms[$slug]['count']++;
354 }
355 }
356
357 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
358 }
359 }
360
361 // Show Empty Terms
362 if( $grid->filter_show_empty() ) {
363 foreach( $taxonomies as $taxonomy ) {
364 $terms = get_terms( $taxonomy, array(
365 'hide_empty' => false,
366 ) );
367
368 foreach( $terms as $term ) {
369 // Check if terms are being limited
370 if( $grid->filter_limit() ) {
371 if( $grid->filter_limit_exclude() ) {
372 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
373 } else {
374 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
375 }
376 }
377
378 $slug = urldecode( $term->slug );
379
380 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
381
382 if( !array_key_exists( $slug, $filter_terms ) ) {
383 // Filter terms
384 $filter_terms[$slug] = array(
385 'taxonomy' => $taxonomy,
386 'name' => $term->name,
387 'count' => 0,
388 );
389 }
390 }
391 }
392 }
393
394 return array(
395 'posts_per_term' => $posts_per_term,
396 'terms_per_post' => $terms_per_post,
397 'filter_terms' => $filter_terms,
398 );
399 }
400
401 public function generate_cache_filter( $grid, $filter_terms ) {
402 $filter = '';
403
404 if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
405 $filter_style = $grid->filter_style();
406
407 // All Button
408 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>';
410 }
411
412 $filter_terms_order = array();
413
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' ) {
415 // Order alphatically per taxonomy
416 $terms_per_taxonomy = array();
417
418 foreach( $filter_terms as $slug => $term ) {
419 $taxonomy = $term['taxonomy'];
420 if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) {
421 $terms_per_taxonomy[$taxonomy] = array();
422 }
423 $terms_per_taxonomy[$taxonomy][] = $slug;
424 }
425
426 foreach( $terms_per_taxonomy as $taxonomy => $terms ) {
427 sort( $terms );
428 $filter_terms_order = array_merge( $filter_terms_order, $terms );
429 }
430 } else {
431 // Order alphabetically by default
432 $filter_terms_order = array_keys( $filter_terms );
433 sort( $filter_terms_order );
434 }
435
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' ) {
437 $filter_terms_order = array_reverse( $filter_terms_order );
438 }
439
440 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
441
442 $current_taxonomy = '';
443 foreach( $filter_terms_order as $slug ) {
444 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
445
446 if( $options ) {
447 if( $grid->filter_count() ) {
448 $name = $options['name'] . ' (' . $options['count'] . ')';
449 } else {
450 $name = $options['name'];
451 }
452
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>';
463 }
464 }
465
466 if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
467 $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
468 }
469 }
470
471 return $filter;
472 }
473 }