PluginProbe
WP Ultimate Post Grid / 2.6.0
WP Ultimate Post Grid v2.6.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.6.0, at helpers/grid_cache.php

461 lines 16.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 = new WPUPG_Grid( $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 = new WPUPG_Grid( $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 = new WPUPG_Grid( $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 $query = new WP_Query( $args );
249 $posts = $query->have_posts() ? $query->posts : array();
250
251 $post_ids = array_map( 'intval', $posts );
252
253 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
254
255 // Offset posts
256 if( $grid->limit_posts_offset() ) {
257 $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
258 }
259
260 // Limit Total # Posts
261 if( $grid->limit_posts_number() ) {
262 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
263 }
264
265 return $post_ids;
266 }
267
268 public function generate_cache_terms( $grid, $post_ids ) {
269 $posts_per_term = array();
270 $terms_per_post = array();
271 $filter_terms = array();
272
273 $taxonomies = $grid->filter_taxonomies();
274 $limit_terms = $grid->filter_limit_terms();
275
276 // Loop over all terms
277 foreach( $post_ids as $post_id ) {
278 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
279
280 foreach( $taxonomies as $taxonomy ) {
281 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
282
283 $terms = get_the_terms( $post_id, $taxonomy );
284 $terms = !$terms || is_wp_error( $terms ) ? array() : $terms;
285
286 // Get parent terms if enabled
287 if( $grid->filter_match_parents() ) {
288 $parent_ids = array();
289 $parents = array();
290
291 foreach( $terms as $term ) {
292 if( $term->parent != 0 ) {
293 $parent_ids[] = $term->parent;
294 }
295 }
296
297 while( count( $parent_ids ) > 0 )
298 {
299 $children_ids = $parent_ids;
300 $parent_ids = array();
301
302 foreach( $children_ids as $child ) {
303 $term = get_term( $child, $taxonomy );
304 $parents[] = $term;
305
306 if( $term->parent != 0 ) {
307 $parent_ids[] = $term->parent;
308 }
309 }
310 }
311
312 $terms = array_merge( $terms, $parents );
313 $handled_terms = array();
314 }
315
316 $post_taxonomy_term_ids = array();
317
318 foreach( $terms as $term ) {
319 // Check if terms are being limited
320 if( $grid->filter_limit() ) {
321 if( $grid->filter_limit_exclude() ) {
322 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
323 } else {
324 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
325 }
326 }
327
328 $slug = urldecode( $term->slug );
329
330 // Make sure we only handle each term once
331 if( $grid->filter_match_parents() ) {
332 if( in_array( $slug, $handled_terms ) ) continue;
333 $handled_terms[] = $slug;
334 }
335
336 // Posts per term
337 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
338 $posts_per_term[$taxonomy][$slug][] = $post_id;
339
340 // Terms per post
341 $post_taxonomy_term_ids[] = $slug;
342
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 }
353 }
354
355 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
356 }
357 }
358
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 ) );
365
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 ) {
400 $filter = '';
401
402 if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
403 $filter_style = $grid->filter_style();
404
405 // All Button
406 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>';
408 }
409
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
438 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
439
440 foreach( $filter_terms_order as $slug ) {
441 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
442
443 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>';
451 }
452 }
453
454 if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
455 $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
456 }
457 }
458
459 return $filter;
460 }
461 }