PluginProbe
WP Ultimate Post Grid / 2.5.0
WP Ultimate Post Grid v2.5.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.5.0, at helpers/grid_cache.php

366 lines 12.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 $this->generate( $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 update_option( 'wpupg_regenerate_grids_check', false );
95 }
96 }
97
98 public function generate( $grid_id )
99 {
100 $grid = new WPUPG_Grid( $grid_id );
101
102 $generated = $this->dynamic_generate( $grid );
103
104 update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
105 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
106 }
107
108 public function dynamic_generate( $grid ) {
109 // Don't cache term grid
110 if( $grid->type() == 'terms' ) {
111 return array(
112 'cache' => array(),
113 'filter' => '',
114 );
115 }
116
117 // Get Posts
118 $args = array(
119 'post_type' => $grid->post_types(),
120 'post_status' => $grid->post_status(),
121 'posts_per_page' => -1,
122 'nopaging' => true,
123 'order' => $grid->order(),
124 'fields' => 'ids',
125 );
126
127 if( $grid->order_by() == 'custom' ) {
128 $args['meta_key'] = $grid->order_custom_key();
129 $args['orderby'] = $grid->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
130 } else {
131 $args['orderby'] = $grid->order_by();
132 }
133
134 // Images Only
135 if( $grid->images_only() ) {
136 if( in_array( 'attachment', $grid->post_types() ) ) {
137 $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
138 } else {
139 $args['meta_query'] = array(
140 array(
141 'key' => '_thumbnail_id',
142 'value' => '0',
143 'compare' => '>'
144 ),
145 );
146 }
147 }
148
149 $query = new WP_Query( $args );
150 $posts = $query->have_posts() ? $query->posts : array();
151
152 $post_ids = array_map( 'intval', $posts );
153
154 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
155
156 // Offset posts
157 if( $grid->limit_posts_offset() ) {
158 $post_ids = array_slice($post_ids, $grid->limit_posts_offset() );
159 }
160
161 // Limit Total # Posts
162 if( $grid->limit_posts_number() ) {
163 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
164 }
165
166 $cache = array(
167 'all' => $post_ids,
168 );
169
170 $taxonomies = $grid->filter_taxonomies();
171 $limit_terms = $grid->filter_limit_terms();
172
173 // Cache arrays
174 $posts_per_term = array();
175 $terms_per_post = array();
176 $filter_terms = array();
177
178 // Loop over all terms
179 foreach( $post_ids as $post_id ) {
180 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
181
182 foreach( $taxonomies as $taxonomy ) {
183 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
184
185 $terms = wp_get_post_terms( $post_id, $taxonomy );
186 $terms = is_wp_error( $terms ) ? array() : $terms;
187
188 // Get parent terms if enabled
189 if( $grid->filter_match_parents() ) {
190 $parent_ids = array();
191 $parents = array();
192
193 foreach( $terms as $term ) {
194 if( $term->parent != 0 ) {
195 $parent_ids[] = $term->parent;
196 }
197 }
198
199 while( count( $parent_ids ) > 0 )
200 {
201 $children_ids = $parent_ids;
202 $parent_ids = array();
203
204 foreach( $children_ids as $child ) {
205 $term = get_term( $child, $taxonomy );
206 $parents[] = $term;
207
208 if( $term->parent != 0 ) {
209 $parent_ids[] = $term->parent;
210 }
211 }
212 }
213
214 $terms = array_merge( $terms, $parents );
215 $handled_terms = array();
216 }
217
218 $post_taxonomy_term_ids = array();
219
220 foreach( $terms as $term ) {
221 // Check if terms are being limited
222 if( $grid->filter_limit() ) {
223 if( $grid->filter_limit_exclude() ) {
224 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
225 } else {
226 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
227 }
228 }
229
230 $slug = urldecode( $term->slug );
231
232 // Make sure we only handle each term once
233 if( $grid->filter_match_parents() ) {
234 if( in_array( $slug, $handled_terms ) ) continue;
235 $handled_terms[] = $slug;
236 }
237
238 // Posts per term
239 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
240 $posts_per_term[$taxonomy][$slug][] = $post_id;
241
242 // Terms per post
243 $post_taxonomy_term_ids[] = $slug;
244
245 if( !array_key_exists( $slug, $filter_terms ) ) {
246 // Filter terms
247 $filter_terms[$slug] = array(
248 'taxonomy' => $taxonomy,
249 'name' => $term->name,
250 'count' => 1,
251 );
252 } else {
253 $filter_terms[$slug]['count']++;
254 }
255 }
256
257 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
258 }
259 }
260
261 // Show Empty Terms
262 if( $grid->filter_show_empty() ) {
263 foreach( $taxonomies as $taxonomy ) {
264 $terms = get_terms( $taxonomy, array(
265 'hide_empty' => false,
266 ) );
267
268 foreach( $terms as $term ) {
269 // Check if terms are being limited
270 if( $grid->filter_limit() ) {
271 if( $grid->filter_limit_exclude() ) {
272 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
273 } else {
274 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
275 }
276 }
277
278 $slug = urldecode( $term->slug );
279
280 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
281
282 if( !array_key_exists( $slug, $filter_terms ) ) {
283 // Filter terms
284 $filter_terms[$slug] = array(
285 'taxonomy' => $taxonomy,
286 'name' => $term->name,
287 'count' => 0,
288 );
289 }
290 }
291 }
292 }
293
294 $cache['taxonomies'] = $posts_per_term;
295 $cache['terms'] = $terms_per_post;
296
297 // Generate Filter
298 $filter = '';
299
300 if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
301 $filter_style = $grid->filter_style();
302
303 // All Button
304 if( $filter_style['isotope']['all_button_text'] ) {
305 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
306 }
307
308 $filter_terms_order = array();
309
310 if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' ) {
311 // Order alphatically per taxonomy
312 $terms_per_taxonomy = array();
313
314 foreach( $filter_terms as $slug => $term ) {
315 $taxonomy = $term['taxonomy'];
316 if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) {
317 $terms_per_taxonomy[$taxonomy] = array();
318 }
319 $terms_per_taxonomy[$taxonomy][] = $slug;
320 }
321
322 foreach( $terms_per_taxonomy as $taxonomy => $terms ) {
323 sort( $terms );
324 $filter_terms_order = array_merge( $filter_terms_order, $terms );
325 }
326 } else {
327 // Order alphabetically by default
328 $filter_terms_order = array_keys( $filter_terms );
329 sort( $filter_terms_order );
330 }
331
332 if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' ) {
333 $filter_terms_order = array_reverse( $filter_terms_order );
334 }
335
336 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
337
338 foreach( $filter_terms_order as $slug ) {
339 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
340
341 if( $options ) {
342 if( $grid->filter_count() ) {
343 $name = $options['name'] . ' (' . $options['count'] . ')';
344 } else {
345 $name = $options['name'];
346 }
347
348 $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>';
349 }
350 }
351
352 if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
353 $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
354 }
355 }
356
357 // Update Metadata
358 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
359 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
360
361 return array(
362 'cache' => $cache,
363 'filter' => $filter,
364 );
365 }
366 }