PluginProbe
WP Ultimate Post Grid / 2.0
WP Ultimate Post Grid v2.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.0, at helpers/grid_cache.php

282 lines 9.3 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 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() );
49 }
50 }
51 }
52
53 public function updated_term( $term_id, $taxonomy )
54 {
55 $args = array(
56 'post_type' => WPUPG_POST_TYPE,
57 'post_status' => 'any',
58 'posts_per_page' => -1,
59 'nopaging' => true,
60 );
61
62 $query = new WP_Query( $args );
63 $posts = $query->have_posts() ? $query->posts : array();
64
65 $grid_ids = array();
66 foreach ( $posts as $post )
67 {
68 $grid = new WPUPG_Grid( $post );
69
70 if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) {
71 $grid_ids[] = $grid->ID();
72 }
73 }
74
75 if( count( $grid_ids ) > 0 ) {
76 update_option( 'wpupg_regenerate_grids_check', $grid_ids );
77 }
78 }
79
80 public function regenerate_grids_check()
81 {
82 $grid_ids = get_option( 'wpupg_regenerate_grids_check', false );
83
84 if( $grid_ids ) {
85 foreach( $grid_ids as $grid_id ) {
86 $this->generate( $grid_id );
87 }
88
89 update_option( 'wpupg_regenerate_grids_check', false );
90 }
91 }
92
93 public function generate( $grid_id )
94 {
95 $grid = new WPUPG_Grid( $grid_id );
96
97 $generated = $this->dynamic_generate( $grid );
98
99 update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
100 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
101 }
102
103 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 // Get Posts
113 $args = array(
114 'post_type' => $grid->post_types(),
115 'post_status' => $grid->post_status(),
116 'posts_per_page' => -1,
117 'nopaging' => true,
118 'order' => $grid->order(),
119 'fields' => 'ids',
120 );
121
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 // Images Only
130 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 }
142 }
143
144 $query = new WP_Query( $args );
145 $posts = $query->have_posts() ? $query->posts : array();
146
147 $post_ids = array_map( 'intval', $posts );
148
149 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
150
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 $cache = array(
157 'all' => $post_ids,
158 );
159
160 $taxonomies = $grid->filter_taxonomies();
161 $limit_terms = $grid->filter_limit_terms();
162
163 // Cache arrays
164 $posts_per_term = array();
165 $terms_per_post = array();
166 $filter_terms = array();
167
168 // Loop over all terms
169 foreach( $post_ids as $post_id ) {
170 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
171
172 foreach( $taxonomies as $taxonomy ) {
173 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
174
175 $terms = wp_get_post_terms( $post_id, $taxonomy );
176 $terms = is_wp_error( $terms ) ? array() : $terms;
177
178 // Get parent terms if enabled
179 if( $grid->filter_match_parents() ) {
180 $parent_ids = array();
181 $parents = array();
182
183 foreach( $terms as $term ) {
184 if( $term->parent != 0 ) {
185 $parent_ids[] = $term->parent;
186 }
187 }
188
189 while( count( $parent_ids ) > 0 )
190 {
191 $children_ids = $parent_ids;
192 $parent_ids = array();
193
194 foreach( $children_ids as $child ) {
195 $term = get_term( $child, $taxonomy );
196 $parents[] = $term;
197
198 if( $term->parent != 0 ) {
199 $parent_ids[] = $term->parent;
200 }
201 }
202 }
203
204 $terms = array_merge( $terms, $parents );
205 $handled_terms = array();
206 }
207
208 $post_taxonomy_term_ids = array();
209
210 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 $slug = urldecode( $term->slug );
221
222 // Make sure we only handle each term once
223 if( $grid->filter_match_parents() ) {
224 if( in_array( $slug, $handled_terms ) ) continue;
225 $handled_terms[] = $slug;
226 }
227
228 // Posts per term
229 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
230 $posts_per_term[$taxonomy][$slug][] = $post_id;
231
232 // Terms per post
233 $post_taxonomy_term_ids[] = $slug;
234
235 // Filter terms
236 $filter_terms[$slug] = array(
237 'taxonomy' => $taxonomy,
238 'name' => $term->name,
239 );
240 }
241
242 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
243 }
244 }
245
246 $cache['taxonomies'] = $posts_per_term;
247 $cache['terms'] = $terms_per_post;
248
249 // Generate Filter
250 $filter = '';
251
252 if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
253 $filter_style = $grid->filter_style();
254
255 // All Button
256 if( $filter_style['isotope']['all_button_text'] ) {
257 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
258 }
259
260 $filter_terms_order = array_keys( $filter_terms );
261 sort( $filter_terms_order );
262 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
263
264 foreach( $filter_terms_order as $slug ) {
265 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
266
267 if( $options ) {
268 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
269 }
270 }
271 }
272
273 // Update Metadata
274 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
275 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
276
277 return array(
278 'cache' => $cache,
279 'filter' => $filter,
280 );
281 }
282 }