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
wp-ultimate-post-grid / helpers / grid_cache.php

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

361 lines 12.8 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 // Limit Total # Posts
157 if( $grid->limit_posts_number() ) {
158 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
159 }
160
161 $cache = array(
162 'all' => $post_ids,
163 );
164
165 $taxonomies = $grid->filter_taxonomies();
166 $limit_terms = $grid->filter_limit_terms();
167
168 // Cache arrays
169 $posts_per_term = array();
170 $terms_per_post = array();
171 $filter_terms = array();
172
173 // Loop over all terms
174 foreach( $post_ids as $post_id ) {
175 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
176
177 foreach( $taxonomies as $taxonomy ) {
178 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
179
180 $terms = wp_get_post_terms( $post_id, $taxonomy );
181 $terms = is_wp_error( $terms ) ? array() : $terms;
182
183 // Get parent terms if enabled
184 if( $grid->filter_match_parents() ) {
185 $parent_ids = array();
186 $parents = array();
187
188 foreach( $terms as $term ) {
189 if( $term->parent != 0 ) {
190 $parent_ids[] = $term->parent;
191 }
192 }
193
194 while( count( $parent_ids ) > 0 )
195 {
196 $children_ids = $parent_ids;
197 $parent_ids = array();
198
199 foreach( $children_ids as $child ) {
200 $term = get_term( $child, $taxonomy );
201 $parents[] = $term;
202
203 if( $term->parent != 0 ) {
204 $parent_ids[] = $term->parent;
205 }
206 }
207 }
208
209 $terms = array_merge( $terms, $parents );
210 $handled_terms = array();
211 }
212
213 $post_taxonomy_term_ids = array();
214
215 foreach( $terms as $term ) {
216 // Check if terms are being limited
217 if( $grid->filter_limit() ) {
218 if( $grid->filter_limit_exclude() ) {
219 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
220 } else {
221 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
222 }
223 }
224
225 $slug = urldecode( $term->slug );
226
227 // Make sure we only handle each term once
228 if( $grid->filter_match_parents() ) {
229 if( in_array( $slug, $handled_terms ) ) continue;
230 $handled_terms[] = $slug;
231 }
232
233 // Posts per term
234 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
235 $posts_per_term[$taxonomy][$slug][] = $post_id;
236
237 // Terms per post
238 $post_taxonomy_term_ids[] = $slug;
239
240 if( !array_key_exists( $slug, $filter_terms ) ) {
241 // Filter terms
242 $filter_terms[$slug] = array(
243 'taxonomy' => $taxonomy,
244 'name' => $term->name,
245 'count' => 1,
246 );
247 } else {
248 $filter_terms[$slug]['count']++;
249 }
250 }
251
252 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
253 }
254 }
255
256 // Show Empty Terms
257 if( $grid->filter_show_empty() ) {
258 foreach( $taxonomies as $taxonomy ) {
259 $terms = get_terms( $taxonomy, array(
260 'hide_empty' => false,
261 ) );
262
263 foreach( $terms as $term ) {
264 // Check if terms are being limited
265 if( $grid->filter_limit() ) {
266 if( $grid->filter_limit_exclude() ) {
267 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
268 } else {
269 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
270 }
271 }
272
273 $slug = urldecode( $term->slug );
274
275 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
276
277 if( !array_key_exists( $slug, $filter_terms ) ) {
278 // Filter terms
279 $filter_terms[$slug] = array(
280 'taxonomy' => $taxonomy,
281 'name' => $term->name,
282 'count' => 0,
283 );
284 }
285 }
286 }
287 }
288
289 $cache['taxonomies'] = $posts_per_term;
290 $cache['terms'] = $terms_per_post;
291
292 // Generate Filter
293 $filter = '';
294
295 if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
296 $filter_style = $grid->filter_style();
297
298 // All Button
299 if( $filter_style['isotope']['all_button_text'] ) {
300 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
301 }
302
303 $filter_terms_order = array();
304
305 if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' ) {
306 // Order alphatically per taxonomy
307 $terms_per_taxonomy = array();
308
309 foreach( $filter_terms as $slug => $term ) {
310 $taxonomy = $term['taxonomy'];
311 if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) {
312 $terms_per_taxonomy[$taxonomy] = array();
313 }
314 $terms_per_taxonomy[$taxonomy][] = $slug;
315 }
316
317 foreach( $terms_per_taxonomy as $taxonomy => $terms ) {
318 sort( $terms );
319 $filter_terms_order = array_merge( $filter_terms_order, $terms );
320 }
321 } else {
322 // Order alphabetically by default
323 $filter_terms_order = array_keys( $filter_terms );
324 sort( $filter_terms_order );
325 }
326
327 if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' ) {
328 $filter_terms_order = array_reverse( $filter_terms_order );
329 }
330
331 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
332
333 foreach( $filter_terms_order as $slug ) {
334 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
335
336 if( $options ) {
337 if( $grid->filter_count() ) {
338 $name = $options['name'] . ' (' . $options['count'] . ')';
339 } else {
340 $name = $options['name'];
341 }
342
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>';
344 }
345 }
346
347 if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
348 $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid );
349 }
350 }
351
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 );
360 }
361 }