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 +169 -33 1.62.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,33 +119,52 @@
94 119 'post_type' => $grid->post_types(),
95 120 'post_status' => $grid->post_status(),
96 121 'posts_per_page' => -1,
97 122 'nopaging' => true,
123 + 'order' => $grid->order(),
98 124 'fields' => 'ids',
99 125 );
100 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 +
101 134 // Images Only
102 135 if( $grid->images_only() ) {
103 - $args['meta_query'] = array(
104 - array(
105 - 'key' => '_thumbnail_id',
106 - 'value' => '0',
107 - 'compare' => '>'
108 - ),
109 - );
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 + }
110 147 }
111 148
112 149 $query = new WP_Query( $args );
113 150 $posts = $query->have_posts() ? $query->posts : array();
151 +
114 152 $post_ids = array_map( 'intval', $posts );
115 153
116 154 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
117 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 +
118 161 $cache = array(
119 162 'all' => $post_ids,
120 163 );
121 164
122 165 $taxonomies = $grid->filter_taxonomies();
166 + $limit_terms = $grid->filter_limit_terms();
123 167
124 168 // Cache arrays
125 169 $posts_per_term = array();
126 170 $terms_per_post = array();
@@ -133,8 +177,9 @@
133 177 foreach( $taxonomies as $taxonomy ) {
134 178 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
135 179
136 180 $terms = wp_get_post_terms( $post_id, $taxonomy );
181 + $terms = is_wp_error( $terms ) ? array() : $terms;
137 182
138 183 // Get parent terms if enabled
139 184 if( $grid->filter_match_parents() ) {
140 185 $parent_ids = array();
@@ -167,8 +212,17 @@
167 212
168 213 $post_taxonomy_term_ids = array();
169 214
170 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 +
171 225 $slug = urldecode( $term->slug );
172 226
173 227 // Make sure we only handle each term once
174 228 if( $grid->filter_match_parents() ) {
@@ -182,19 +236,54 @@
182 236
183 237 // Terms per post
184 238 $post_taxonomy_term_ids[] = $slug;
185 239
186 - // Filter terms
187 - $filter_terms[$slug] = array(
188 - 'taxonomy' => $taxonomy,
189 - 'name' => $term->name,
190 - );
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 + }
191 251
252 + $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
253 + }
254 + }
192 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 + ) );
193 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 + }
194 285 }
195 -
196 - $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
197 286 }
198 287 }
199 288
200 289 $cache['taxonomies'] = $posts_per_term;
@@ -202,15 +291,62 @@
202 291
203 292 // Generate Filter
204 293 $filter = '';
205 294
206 - if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
295 + if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
207 296 $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>';
209 297
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>';
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 );
213 349 }
214 350 }
215 351
216 352 // Update Metadata