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
← All changes | helpers/grid_cache.php +163 -38 1.72.4.0 View file →
@@ -3,13 +3,20 @@
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' ) );
7 9 add_action( 'save_post', array( $this, 'updated_post' ), 11, 2 );
8 10 add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 );
9 11 add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) );
10 12 }
11 13
14 + public function updated_attachment( $id )
15 + {
16 + $this->update_grids_with_post_type( 'attachment' );
17 + }
18 +
12 19 public function updated_post( $id, $post )
13 20 {
14 21 $update_post_post_type = $post->post_type;
15 22
@@ -16,27 +23,37 @@
16 23 if( $update_post_post_type == WPUPG_POST_TYPE )
17 24 {
18 25 $this->generate( $id );
19 26 } else {
20 - $args = array(
21 - 'post_type' => WPUPG_POST_TYPE,
22 - 'post_status' => 'any',
23 - 'posts_per_page' => -1,
24 - 'nopaging' => true,
25 - );
27 + $this->update_grids_with_post_type( $update_post_post_type );
28 + }
29 + }
26 30
27 - $query = new WP_Query( $args );
28 - $posts = $query->have_posts() ? $query->posts : array();
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 + );
29 39
30 - foreach ( $posts as $grid_post )
31 - {
32 - $grid = new WPUPG_Grid( $grid_post );
40 + $query = new WP_Query( $args );
41 + $posts = $query->have_posts() ? $query->posts : array();
33 42
34 - if( in_array( $update_post_post_type, $grid->post_types() ) ) {
35 - $this->generate( $grid->ID() );
36 - }
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();
37 50 }
38 51 }
52 +
53 + if( count( $grid_ids ) > 0 ) {
54 + update_option( 'wpupg_regenerate_grids_check', $grid_ids );
55 + }
39 56 }
40 57
41 58 public function updated_term( $term_id, $taxonomy )
42 59 {
@@ -88,8 +105,16 @@
88 105 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
89 106 }
90 107
91 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 +
92 117 // Get Posts
93 118 $args = array(
94 119 'post_type' => $grid->post_types(),
95 120 'post_status' => $grid->post_status(),
@@ -94,26 +119,37 @@
94 119 'post_type' => $grid->post_types(),
95 120 'post_status' => $grid->post_status(),
96 121 'posts_per_page' => -1,
97 122 'nopaging' => true,
98 - 'orderby' => $grid->order_by(),
99 123 'order' => $grid->order(),
100 124 'fields' => 'ids',
101 125 );
102 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 +
103 134 // Images Only
104 135 if( $grid->images_only() ) {
105 - $args['meta_query'] = array(
106 - array(
107 - 'key' => '_thumbnail_id',
108 - 'value' => '0',
109 - 'compare' => '>'
110 - ),
111 - );
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 + }
112 147 }
113 148
114 149 $query = new WP_Query( $args );
115 150 $posts = $query->have_posts() ? $query->posts : array();
151 +
116 152 $post_ids = array_map( 'intval', $posts );
117 153
118 154 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
119 155
@@ -141,8 +177,9 @@
141 177 foreach( $taxonomies as $taxonomy ) {
142 178 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
143 179
144 180 $terms = wp_get_post_terms( $post_id, $taxonomy );
181 + $terms = is_wp_error( $terms ) ? array() : $terms;
145 182
146 183 // Get parent terms if enabled
147 184 if( $grid->filter_match_parents() ) {
148 185 $parent_ids = array();
@@ -175,29 +212,41 @@
175 212
176 213 $post_taxonomy_term_ids = array();
177 214
178 215 foreach( $terms as $term ) {
179 - if( !$grid->filter_limit() || ( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) ) {
180 - $slug = urldecode( $term->slug );
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 + }
181 224
182 - // Make sure we only handle each term once
183 - if( $grid->filter_match_parents() ) {
184 - if( in_array( $slug, $handled_terms ) ) continue;
185 - $handled_terms[] = $slug;
186 - }
225 + $slug = urldecode( $term->slug );
187 226
188 - // Posts per term
189 - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
190 - $posts_per_term[$taxonomy][$slug][] = $post_id;
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 + }
191 232
192 - // Terms per post
193 - $post_taxonomy_term_ids[] = $slug;
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;
194 236
237 + // Terms per post
238 + $post_taxonomy_term_ids[] = $slug;
239 +
240 + if( !array_key_exists( $slug, $filter_terms ) ) {
195 241 // Filter terms
196 242 $filter_terms[$slug] = array(
197 243 'taxonomy' => $taxonomy,
198 244 'name' => $term->name,
245 + 'count' => 1,
199 246 );
247 + } else {
248 + $filter_terms[$slug]['count']++;
200 249 }
201 250 }
202 251
203 252 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
@@ -203,8 +252,41 @@
203 252 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
204 253 }
205 254 }
206 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 +
207 289 $cache['taxonomies'] = $posts_per_term;
208 290 $cache['terms'] = $terms_per_post;
209 291
210 292 // Generate Filter
@@ -209,9 +291,9 @@
209 291
210 292 // Generate Filter
211 293 $filter = '';
212 294
213 - if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
295 + if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
214 296 $filter_style = $grid->filter_style();
215 297
216 298 // All Button
217 299 if( $filter_style['isotope']['all_button_text'] ) {
@@ -217,11 +299,54 @@
217 299 if( $filter_style['isotope']['all_button_text'] ) {
218 300 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
219 301 }
220 302
221 - ksort( $filter_terms );
222 - foreach( $filter_terms as $slug => $options ) {
223 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
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 );
224 349 }
225 350 }
226 351
227 352 // Update Metadata