PluginProbe
WP Ultimate Post Grid / 1.9
WP Ultimate Post Grid v1.9
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 1.9, at helpers/grid_cache.php

273 lines 9.1 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 // Get Posts
105 $args = array(
106 'post_type' => $grid->post_types(),
107 'post_status' => $grid->post_status(),
108 'posts_per_page' => -1,
109 'nopaging' => true,
110 'order' => $grid->order(),
111 'fields' => 'ids',
112 );
113
114 if( $grid->order_by() == 'custom' ) {
115 $args['meta_key'] = $grid->order_custom_key();
116 $args['orderby'] = $grid->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
117 } else {
118 $args['orderby'] = $grid->order_by();
119 }
120
121 // Images Only
122 if( $grid->images_only() ) {
123 if( in_array( 'attachment', $grid->post_types() ) ) {
124 $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
125 } else {
126 $args['meta_query'] = array(
127 array(
128 'key' => '_thumbnail_id',
129 'value' => '0',
130 'compare' => '>'
131 ),
132 );
133 }
134 }
135
136 $query = new WP_Query( $args );
137 $posts = $query->have_posts() ? $query->posts : array();
138
139 $post_ids = array_map( 'intval', $posts );
140
141 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
142
143 // Limit Total # Posts
144 if( $grid->limit_posts_number() ) {
145 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
146 }
147
148 $cache = array(
149 'all' => $post_ids,
150 );
151
152 $taxonomies = $grid->filter_taxonomies();
153 $limit_terms = $grid->filter_limit_terms();
154
155 // Cache arrays
156 $posts_per_term = array();
157 $terms_per_post = array();
158 $filter_terms = array();
159
160 // Loop over all terms
161 foreach( $post_ids as $post_id ) {
162 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
163
164 foreach( $taxonomies as $taxonomy ) {
165 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
166
167 $terms = wp_get_post_terms( $post_id, $taxonomy );
168
169 // Get parent terms if enabled
170 if( $grid->filter_match_parents() ) {
171 $parent_ids = array();
172 $parents = array();
173
174 foreach( $terms as $term ) {
175 if( $term->parent != 0 ) {
176 $parent_ids[] = $term->parent;
177 }
178 }
179
180 while( count( $parent_ids ) > 0 )
181 {
182 $children_ids = $parent_ids;
183 $parent_ids = array();
184
185 foreach( $children_ids as $child ) {
186 $term = get_term( $child, $taxonomy );
187 $parents[] = $term;
188
189 if( $term->parent != 0 ) {
190 $parent_ids[] = $term->parent;
191 }
192 }
193 }
194
195 $terms = array_merge( $terms, $parents );
196 $handled_terms = array();
197 }
198
199 $post_taxonomy_term_ids = array();
200
201 foreach( $terms as $term ) {
202 // Check if terms are being limited
203 if( $grid->filter_limit() ) {
204 if( $grid->filter_limit_exclude() ) {
205 if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
206 } else {
207 if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue;
208 }
209 }
210
211 $slug = urldecode( $term->slug );
212
213 // Make sure we only handle each term once
214 if( $grid->filter_match_parents() ) {
215 if( in_array( $slug, $handled_terms ) ) continue;
216 $handled_terms[] = $slug;
217 }
218
219 // Posts per term
220 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
221 $posts_per_term[$taxonomy][$slug][] = $post_id;
222
223 // Terms per post
224 $post_taxonomy_term_ids[] = $slug;
225
226 // Filter terms
227 $filter_terms[$slug] = array(
228 'taxonomy' => $taxonomy,
229 'name' => $term->name,
230 );
231 }
232
233 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
234 }
235 }
236
237 $cache['taxonomies'] = $posts_per_term;
238 $cache['terms'] = $terms_per_post;
239
240 // Generate Filter
241 $filter = '';
242
243 if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
244 $filter_style = $grid->filter_style();
245
246 // All Button
247 if( $filter_style['isotope']['all_button_text'] ) {
248 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
249 }
250
251 $filter_terms_order = array_keys( $filter_terms );
252 sort( $filter_terms_order );
253 $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid );
254
255 foreach( $filter_terms_order as $slug ) {
256 $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false;
257
258 if( $options ) {
259 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
260 }
261 }
262 }
263
264 // Update Metadata
265 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
266 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
267
268 return array(
269 'cache' => $cache,
270 'filter' => $filter,
271 );
272 }
273 }