PluginProbe
WP Ultimate Post Grid / 1.6
WP Ultimate Post Grid v1.6
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 +59 -307 2.8.21.6 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
@@ -21,39 +14,29 @@
21 14 $update_post_post_type = $post->post_type;
22 15
23 16 if( $update_post_post_type == WPUPG_POST_TYPE )
24 17 {
25 - update_option( 'wpupg_regenerate_grids_check', array( $id ) );
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 - $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();
34 + if( in_array( $update_post_post_type, $grid->post_types() ) ) {
35 + $this->generate( $grid->ID() );
36 + }
50 37 }
51 38 }
52 -
53 - if( count( $grid_ids ) > 0 ) {
54 - update_option( 'wpupg_regenerate_grids_check', $grid_ids );
55 - }
56 39 }
57 40
58 41 public function updated_term( $term_id, $taxonomy )
59 42 {
@@ -69,9 +52,9 @@
69 52
70 53 $grid_ids = array();
71 54 foreach ( $posts as $post )
72 55 {
73 - $grid = WPUPG_Grid_Manager::get( $post );
56 + $grid = new WPUPG_Grid( $post );
74 57
75 58 if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) {
76 59 $grid_ids[] = $grid->ID();
77 60 }
@@ -89,193 +72,61 @@
89 72 if( $grid_ids ) {
90 73 foreach( $grid_ids as $grid_id ) {
91 74 $this->generate( $grid_id );
92 75 }
76 +
77 + update_option( 'wpupg_regenerate_grids_check', false );
93 78 }
94 79 }
95 80
96 81 public function generate( $grid_id )
97 82 {
98 - $grid = WPUPG_Grid_Manager::get( $grid_id );
83 + $grid = new WPUPG_Grid( $grid_id );
99 84
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 );
85 + $generated = $this->dynamic_generate( $grid );
104 86
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 - }
87 + update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
88 + update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
178 89 }
179 90
180 91 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 ) {
92 + // Get Posts
217 93 $args = array(
218 94 'post_type' => $grid->post_types(),
219 95 'post_status' => $grid->post_status(),
220 96 'posts_per_page' => -1,
221 97 'nopaging' => true,
222 - 'order' => $grid->order(),
223 98 'fields' => 'ids',
224 99 );
225 100
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 101 // Images Only
234 102 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 - }
103 + $args['meta_query'] = array(
104 + array(
105 + 'key' => '_thumbnail_id',
106 + 'value' => '0',
107 + 'compare' => '>'
108 + ),
109 + );
246 110 }
247 111
248 - $args = apply_filters( 'wpupg_grid_cache_post_ids_args', $args, $grid );
249 -
250 112 $query = new WP_Query( $args );
251 113 $posts = $query->have_posts() ? $query->posts : array();
252 -
253 114 $post_ids = array_map( 'intval', $posts );
254 115
255 116 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
256 117
257 - // Offset posts
258 - if( $grid->limit_posts_offset() ) {
259 - $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
260 - }
118 + $cache = array(
119 + 'all' => $post_ids,
120 + );
261 121
262 - // Limit Total # Posts
263 - if( $grid->limit_posts_number() ) {
264 - $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
265 - }
122 + $taxonomies = $grid->filter_taxonomies();
266 123
267 - return $post_ids;
268 - }
269 -
270 - public function generate_cache_terms( $grid, $post_ids ) {
124 + // Cache arrays
271 125 $posts_per_term = array();
272 126 $terms_per_post = array();
273 127 $filter_terms = array();
274 128
275 - $taxonomies = $grid->filter_taxonomies();
276 - $limit_terms = $grid->filter_limit_terms();
277 -
278 129 // Loop over all terms
279 130 foreach( $post_ids as $post_id ) {
280 131 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
281 132
@@ -281,10 +132,9 @@
281 132
282 133 foreach( $taxonomies as $taxonomy ) {
283 134 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
284 135
285 - $terms = get_the_terms( $post_id, $taxonomy );
286 - $terms = !$terms || is_wp_error( $terms ) ? array() : $terms;
136 + $terms = wp_get_post_terms( $post_id, $taxonomy );
287 137
288 138 // Get parent terms if enabled
289 139 if( $grid->filter_match_parents() ) {
290 140 $parent_ids = array();
@@ -317,17 +167,8 @@
317 167
318 168 $post_taxonomy_term_ids = array();
319 169
320 170 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 171 $slug = urldecode( $term->slug );
331 172
332 173 // Make sure we only handle each term once
333 174 if( $grid->filter_match_parents() ) {
@@ -341,133 +182,44 @@
341 182
342 183 // Terms per post
343 184 $post_taxonomy_term_ids[] = $slug;
344 185
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 - }
186 + // Filter terms
187 + $filter_terms[$slug] = array(
188 + 'taxonomy' => $taxonomy,
189 + 'name' => $term->name,
190 + );
356 191
357 - $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
358 - }
359 - }
360 192
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 193
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 - }
194 + }
377 195
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 - }
196 + $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
391 197 }
392 198 }
393 199
394 - return array(
395 - 'posts_per_term' => $posts_per_term,
396 - 'terms_per_post' => $terms_per_post,
397 - 'filter_terms' => $filter_terms,
398 - );
399 - }
200 + $cache['taxonomies'] = $posts_per_term;
201 + $cache['terms'] = $terms_per_post;
400 202
401 - public function generate_cache_filter( $grid, $filter_terms ) {
203 + // Generate Filter
402 204 $filter = '';
403 205
404 - if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
206 + if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
405 207 $filter_style = $grid->filter_style();
208 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
406 209
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>';
210 + ksort( $filter_terms );
211 + foreach( $filter_terms as $slug => $options ) {
212 + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
410 213 }
214 + }
411 215
412 - $filter_terms_order = array();
216 + // Update Metadata
217 + $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
218 + $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
413 219
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;
220 + return array(
221 + 'cache' => $cache,
222 + 'filter' => $filter,
223 + );
472 224 }
473 225 }