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

236 lines 7.7 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( 'save_post', array( $this, 'updated_post' ), 11, 2 );
8 add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 );
9 add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) );
10 }
11
12 public function updated_post( $id, $post )
13 {
14 $update_post_post_type = $post->post_type;
15
16 if( $update_post_post_type == WPUPG_POST_TYPE )
17 {
18 $this->generate( $id );
19 } else {
20 $args = array(
21 'post_type' => WPUPG_POST_TYPE,
22 'post_status' => 'any',
23 'posts_per_page' => -1,
24 'nopaging' => true,
25 );
26
27 $query = new WP_Query( $args );
28 $posts = $query->have_posts() ? $query->posts : array();
29
30 foreach ( $posts as $grid_post )
31 {
32 $grid = new WPUPG_Grid( $grid_post );
33
34 if( in_array( $update_post_post_type, $grid->post_types() ) ) {
35 $this->generate( $grid->ID() );
36 }
37 }
38 }
39 }
40
41 public function updated_term( $term_id, $taxonomy )
42 {
43 $args = array(
44 'post_type' => WPUPG_POST_TYPE,
45 'post_status' => 'any',
46 'posts_per_page' => -1,
47 'nopaging' => true,
48 );
49
50 $query = new WP_Query( $args );
51 $posts = $query->have_posts() ? $query->posts : array();
52
53 $grid_ids = array();
54 foreach ( $posts as $post )
55 {
56 $grid = new WPUPG_Grid( $post );
57
58 if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) {
59 $grid_ids[] = $grid->ID();
60 }
61 }
62
63 if( count( $grid_ids ) > 0 ) {
64 update_option( 'wpupg_regenerate_grids_check', $grid_ids );
65 }
66 }
67
68 public function regenerate_grids_check()
69 {
70 $grid_ids = get_option( 'wpupg_regenerate_grids_check', false );
71
72 if( $grid_ids ) {
73 foreach( $grid_ids as $grid_id ) {
74 $this->generate( $grid_id );
75 }
76
77 update_option( 'wpupg_regenerate_grids_check', false );
78 }
79 }
80
81 public function generate( $grid_id )
82 {
83 $grid = new WPUPG_Grid( $grid_id );
84
85 $generated = $this->dynamic_generate( $grid );
86
87 update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] );
88 update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] );
89 }
90
91 public function dynamic_generate( $grid ) {
92 // Get Posts
93 $args = array(
94 'post_type' => $grid->post_types(),
95 'post_status' => $grid->post_status(),
96 'posts_per_page' => -1,
97 'nopaging' => true,
98 'orderby' => $grid->order_by(),
99 'order' => $grid->order(),
100 'fields' => 'ids',
101 );
102
103 // Images Only
104 if( $grid->images_only() ) {
105 $args['meta_query'] = array(
106 array(
107 'key' => '_thumbnail_id',
108 'value' => '0',
109 'compare' => '>'
110 ),
111 );
112 }
113
114 $query = new WP_Query( $args );
115 $posts = $query->have_posts() ? $query->posts : array();
116 $post_ids = array_map( 'intval', $posts );
117
118 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
119
120 // Limit Total # Posts
121 if( $grid->limit_posts_number() ) {
122 $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() );
123 }
124
125 $cache = array(
126 'all' => $post_ids,
127 );
128
129 $taxonomies = $grid->filter_taxonomies();
130 $limit_terms = $grid->filter_limit_terms();
131
132 // Cache arrays
133 $posts_per_term = array();
134 $terms_per_post = array();
135 $filter_terms = array();
136
137 // Loop over all terms
138 foreach( $post_ids as $post_id ) {
139 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
140
141 foreach( $taxonomies as $taxonomy ) {
142 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
143
144 $terms = wp_get_post_terms( $post_id, $taxonomy );
145
146 // Get parent terms if enabled
147 if( $grid->filter_match_parents() ) {
148 $parent_ids = array();
149 $parents = array();
150
151 foreach( $terms as $term ) {
152 if( $term->parent != 0 ) {
153 $parent_ids[] = $term->parent;
154 }
155 }
156
157 while( count( $parent_ids ) > 0 )
158 {
159 $children_ids = $parent_ids;
160 $parent_ids = array();
161
162 foreach( $children_ids as $child ) {
163 $term = get_term( $child, $taxonomy );
164 $parents[] = $term;
165
166 if( $term->parent != 0 ) {
167 $parent_ids[] = $term->parent;
168 }
169 }
170 }
171
172 $terms = array_merge( $terms, $parents );
173 $handled_terms = array();
174 }
175
176 $post_taxonomy_term_ids = array();
177
178 foreach( $terms as $term ) {
179 if( !$grid->filter_limit() || ( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) ) {
180 $slug = urldecode( $term->slug );
181
182 // Make sure we only handle each term once
183 if( $grid->filter_match_parents() ) {
184 if( in_array( $slug, $handled_terms ) ) continue;
185 $handled_terms[] = $slug;
186 }
187
188 // Posts per term
189 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
190 $posts_per_term[$taxonomy][$slug][] = $post_id;
191
192 // Terms per post
193 $post_taxonomy_term_ids[] = $slug;
194
195 // Filter terms
196 $filter_terms[$slug] = array(
197 'taxonomy' => $taxonomy,
198 'name' => $term->name,
199 );
200 }
201 }
202
203 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
204 }
205 }
206
207 $cache['taxonomies'] = $posts_per_term;
208 $cache['terms'] = $terms_per_post;
209
210 // Generate Filter
211 $filter = '';
212
213 if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
214 $filter_style = $grid->filter_style();
215
216 // All Button
217 if( $filter_style['isotope']['all_button_text'] ) {
218 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>';
219 }
220
221 ksort( $filter_terms );
222 foreach( $filter_terms as $slug => $options ) {
223 $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>';
224 }
225 }
226
227 // Update Metadata
228 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
229 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
230
231 return array(
232 'cache' => $cache,
233 'filter' => $filter,
234 );
235 }
236 }