PluginProbe
WP Ultimate Post Grid / 1.6
WP Ultimate Post Grid v1.6
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 +33 -164 2.31.6 View file →
@@ -3,20 +3,13 @@
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' ) );
9 7 add_action( 'save_post', array( $this, 'updated_post' ), 11, 2 );
10 8 add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 );
11 9 add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) );
12 10 }
13 11
14 - public function updated_attachment( $id )
15 - {
16 - $this->update_grids_with_post_type( 'attachment' );
17 - }
18 -
19 12 public function updated_post( $id, $post )
20 13 {
21 14 $update_post_post_type = $post->post_type;
22 15
@@ -23,30 +16,25 @@
23 16 if( $update_post_post_type == WPUPG_POST_TYPE )
24 17 {
25 18 $this->generate( $id );
26 19 } else {
27 - $this->update_grids_with_post_type( $update_post_post_type );
28 - }
29 - }
20 + $args = array(
21 + 'post_type' => WPUPG_POST_TYPE,
22 + 'post_status' => 'any',
23 + 'posts_per_page' => -1,
24 + 'nopaging' => true,
25 + );
30 26
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 - );
27 + $query = new WP_Query( $args );
28 + $posts = $query->have_posts() ? $query->posts : array();
39 29
40 - $query = new WP_Query( $args );
41 - $posts = $query->have_posts() ? $query->posts : array();
30 + foreach ( $posts as $grid_post )
31 + {
32 + $grid = new WPUPG_Grid( $grid_post );
42 33
43 - foreach ( $posts as $grid_post )
44 - {
45 - $grid = new WPUPG_Grid( $grid_post );
46 -
47 - if( in_array( $post_type, $grid->post_types() ) ) {
48 - $this->generate( $grid->ID() );
34 + if( in_array( $update_post_post_type, $grid->post_types() ) ) {
35 + $this->generate( $grid->ID() );
36 + }
49 37 }
50 38 }
51 39 }
52 40
@@ -100,16 +88,8 @@
100 88 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
101 89 }
102 90
103 91 public function dynamic_generate( $grid ) {
104 - // Don't cache term grid
105 - if( $grid->type() == 'terms' ) {
106 - return array(
107 - 'cache' => array(),
108 - 'filter' => '',
109 - );
110 - }
111 -
112 92 // Get Posts
113 93 $args = array(
114 94 'post_type' => $grid->post_types(),
115 95 'post_status' => $grid->post_status(),
@@ -114,52 +94,33 @@
114 94 'post_type' => $grid->post_types(),
115 95 'post_status' => $grid->post_status(),
116 96 'posts_per_page' => -1,
117 97 'nopaging' => true,
118 - 'order' => $grid->order(),
119 98 'fields' => 'ids',
120 99 );
121 100
122 - if( $grid->order_by() == 'custom' ) {
123 - $args['meta_key'] = $grid->order_custom_key();
124 - $args['orderby'] = $grid->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
125 - } else {
126 - $args['orderby'] = $grid->order_by();
127 - }
128 -
129 101 // Images Only
130 102 if( $grid->images_only() ) {
131 - if( in_array( 'attachment', $grid->post_types() ) ) {
132 - $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
133 - } else {
134 - $args['meta_query'] = array(
135 - array(
136 - 'key' => '_thumbnail_id',
137 - 'value' => '0',
138 - 'compare' => '>'
139 - ),
140 - );
141 - }
103 + $args['meta_query'] = array(
104 + array(
105 + 'key' => '_thumbnail_id',
106 + 'value' => '0',
107 + 'compare' => '>'
108 + ),
109 + );
142 110 }
143 111
144 112 $query = new WP_Query( $args );
145 113 $posts = $query->have_posts() ? $query->posts : array();
146 -
147 114 $post_ids = array_map( 'intval', $posts );
148 115
149 116 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
150 117
151 - // Limit Total # Posts
152 - if( $grid->limit_posts_number() ) {
153 - $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
154 - }
155 -
156 118 $cache = array(
157 119 'all' => $post_ids,
158 120 );
159 121
160 122 $taxonomies = $grid->filter_taxonomies();
161 - $limit_terms = $grid->filter_limit_terms();
162 123
163 124 // Cache arrays
164 125 $posts_per_term = array();
165 126 $terms_per_post = array();
@@ -172,9 +133,8 @@
172 133 foreach( $taxonomies as $taxonomy ) {
173 134 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
174 135
175 136 $terms = wp_get_post_terms( $post_id, $taxonomy );
176 - $terms = is_wp_error( $terms ) ? array() : $terms;
177 137
178 138 // Get parent terms if enabled
179 139 if( $grid->filter_match_parents() ) {
180 140 $parent_ids = array();
@@ -207,17 +167,8 @@
207 167
208 168 $post_taxonomy_term_ids = array();
209 169
210 170 foreach( $terms as $term ) {
211 - // Check if terms are being limited
212 - if( $grid->filter_limit() ) {
213 - if( $grid->filter_limit_exclude() ) {
214 - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
215 - } else {
216 - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
217 - }
218 - }
219 -
220 171 $slug = urldecode( $term->slug );
221 172
222 173 // Make sure we only handle each term once
223 174 if( $grid->filter_match_parents() ) {
@@ -231,54 +182,19 @@
231 182
232 183 // Terms per post
233 184 $post_taxonomy_term_ids[] = $slug;
234 185
235 - if( !array_key_exists( $slug, $filter_terms ) ) {
236 - // Filter terms
237 - $filter_terms[$slug] = array(
238 - 'taxonomy' => $taxonomy,
239 - 'name' => $term->name,
240 - 'count' => 1,
241 - );
242 - } else {
243 - $filter_terms[$slug]['count']++;
244 - }
245 - }
186 + // Filter terms
187 + $filter_terms[$slug] = array(
188 + 'taxonomy' => $taxonomy,
189 + 'name' => $term->name,
190 + );
246 191
247 - $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
248 - }
249 - }
250 192
251 - // Show Empty Terms
252 - if( $grid->filter_show_empty() ) {
253 - foreach( $taxonomies as $taxonomy ) {
254 - $terms = get_terms( $taxonomy, array(
255 - 'hide_empty' => false,
256 - ) );
257 193
258 - foreach( $terms as $term ) {
259 - // Check if terms are being limited
260 - if( $grid->filter_limit() ) {
261 - if( $grid->filter_limit_exclude() ) {
262 - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
263 - } else {
264 - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
265 - }
266 - }
194 + }
267 195
268 - $slug = urldecode( $term->slug );
269 -
270 - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
271 -
272 - if( !array_key_exists( $slug, $filter_terms ) ) {
273 - // Filter terms
274 - $filter_terms[$slug] = array(
275 - 'taxonomy' => $taxonomy,
276 - 'name' => $term->name,
277 - 'count' => 0,
278 - );
279 - }
280 - }
196 + $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
281 197 }
282 198 }
283 199
284 200 $cache['taxonomies'] = $posts_per_term;
@@ -286,62 +202,15 @@
286 202
287 203 // Generate Filter
288 204 $filter = '';
289 205
290 - if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') {
206 + if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
291 207 $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>';
292 209
293 - // All Button
294 - if( $filter_style['isotope']['all_button_text'] ) {
295 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
296 - }
297 -
298 - $filter_terms_order = array();
299 -
300 - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' ) {
301 - // Order alphatically per taxonomy
302 - $terms_per_taxonomy = array();
303 -
304 - foreach( $filter_terms as $slug => $term ) {
305 - $taxonomy = $term['taxonomy'];
306 - if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) {
307 - $terms_per_taxonomy[$taxonomy] = array();
308 - }
309 - $terms_per_taxonomy[$taxonomy][] = $slug;
310 - }
311 -
312 - foreach( $terms_per_taxonomy as $taxonomy => $terms ) {
313 - sort( $terms );
314 - $filter_terms_order = array_merge( $filter_terms_order, $terms );
315 - }
316 - } else {
317 - // Order alphabetically by default
318 - $filter_terms_order = array_keys( $filter_terms );
319 - sort( $filter_terms_order );
320 - }
321 -
322 - if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' ) {
323 - $filter_terms_order = array_reverse( $filter_terms_order );
324 - }
325 -
326 - $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
327 -
328 - foreach( $filter_terms_order as $slug ) {
329 - $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
330 -
331 - if( $options ) {
332 - if( $grid->filter_count() ) {
333 - $name = $options['name'] . ' (' . $options['count'] . ')';
334 - } else {
335 - $name = $options['name'];
336 - }
337 -
338 - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $name . '</div>';
339 - }
340 - }
341 -
342 - if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) {
343 - $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter();
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>';
344 213 }
345 214 }
346 215
347 216 // Update Metadata