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 +189 -40 1.52.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 {
@@ -81,8 +98,23 @@
81 98 public function generate( $grid_id )
82 99 {
83 100 $grid = new WPUPG_Grid( $grid_id );
84 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 +
85 117 // Get Posts
86 118 $args = array(
87 119 'post_type' => $grid->post_types(),
88 120 'post_status' => $grid->post_status(),
@@ -87,33 +119,52 @@
87 119 'post_type' => $grid->post_types(),
88 120 'post_status' => $grid->post_status(),
89 121 'posts_per_page' => -1,
90 122 'nopaging' => true,
123 + 'order' => $grid->order(),
91 124 'fields' => 'ids',
92 125 );
93 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 +
94 134 // Images Only
95 135 if( $grid->images_only() ) {
96 - $args['meta_query'] = array(
97 - array(
98 - 'key' => '_thumbnail_id',
99 - 'value' => '0',
100 - 'compare' => '>'
101 - ),
102 - );
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 + }
103 147 }
104 148
105 149 $query = new WP_Query( $args );
106 150 $posts = $query->have_posts() ? $query->posts : array();
151 +
107 152 $post_ids = array_map( 'intval', $posts );
108 153
109 154 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
110 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 +
111 161 $cache = array(
112 162 'all' => $post_ids,
113 163 );
114 164
115 165 $taxonomies = $grid->filter_taxonomies();
166 + $limit_terms = $grid->filter_limit_terms();
116 167
117 168 // Cache arrays
118 169 $posts_per_term = array();
119 170 $terms_per_post = array();
@@ -126,8 +177,9 @@
126 177 foreach( $taxonomies as $taxonomy ) {
127 178 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
128 179
129 180 $terms = wp_get_post_terms( $post_id, $taxonomy );
181 + $terms = is_wp_error( $terms ) ? array() : $terms;
130 182
131 183 // Get parent terms if enabled
132 184 if( $grid->filter_match_parents() ) {
133 185 $parent_ids = array();
@@ -160,32 +212,78 @@
160 212
161 213 $post_taxonomy_term_ids = array();
162 214
163 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 +
164 227 // Make sure we only handle each term once
165 228 if( $grid->filter_match_parents() ) {
166 - if( in_array( $term->slug, $handled_terms ) ) continue;
167 - $handled_terms[] = $term->slug;
229 + if( in_array( $slug, $handled_terms ) ) continue;
230 + $handled_terms[] = $slug;
168 231 }
169 232
170 233 // Posts per term
171 - if( !isset( $posts_per_term[$taxonomy][$term->slug] ) ) $posts_per_term[$taxonomy][$term->slug] = array();
172 - $posts_per_term[$taxonomy][$term->slug][] = $post_id;
234 + if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
235 + $posts_per_term[$taxonomy][$slug][] = $post_id;
173 236
174 237 // Terms per post
175 - $post_taxonomy_term_ids[] = $term->slug;
238 + $post_taxonomy_term_ids[] = $slug;
176 239
177 - // Filter terms
178 - $filter_terms[$term->slug] = array(
179 - 'taxonomy' => $taxonomy,
180 - 'name' => $term->name,
181 - );
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 + }
182 251
252 + $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
253 + }
254 + }
183 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 + ) );
184 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 + }
185 285 }
186 -
187 - $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
188 286 }
189 287 }
190 288
191 289 $cache['taxonomies'] = $posts_per_term;
@@ -193,20 +291,71 @@
193 291
194 292 // Generate Filter
195 293 $filter = '';
196 294
197 - if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
198 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . __( 'All', 'wp-ultimate-post-grid' ) . '</div>';
295 + if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
296 + $filter_style = $grid->filter_style();
199 297
200 - ksort( $filter_terms );
201 - foreach( $filter_terms as $slug => $options ) {
202 - $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>';
203 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 + }
204 350 }
205 351
206 352 // Update Metadata
207 353 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
208 354 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
209 - update_post_meta( $grid_id, 'wpupg_posts', $cache );
210 - update_post_meta( $grid_id, 'wpupg_filter', $filter );
355 +
356 + return array(
357 + 'cache' => $cache,
358 + 'filter' => $filter,
359 + );
211 360 }
212 361 }