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
wp-ultimate-post-grid / helpers / grid_cache.php

grid_cache.php in WP Ultimate Post Grid 1.6, at helpers/grid_cache.php

225 lines 7.0 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 'fields' => 'ids',
99 );
100
101 // Images Only
102 if( $grid->images_only() ) {
103 $args['meta_query'] = array(
104 array(
105 'key' => '_thumbnail_id',
106 'value' => '0',
107 'compare' => '>'
108 ),
109 );
110 }
111
112 $query = new WP_Query( $args );
113 $posts = $query->have_posts() ? $query->posts : array();
114 $post_ids = array_map( 'intval', $posts );
115
116 $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid );
117
118 $cache = array(
119 'all' => $post_ids,
120 );
121
122 $taxonomies = $grid->filter_taxonomies();
123
124 // Cache arrays
125 $posts_per_term = array();
126 $terms_per_post = array();
127 $filter_terms = array();
128
129 // Loop over all terms
130 foreach( $post_ids as $post_id ) {
131 if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array();
132
133 foreach( $taxonomies as $taxonomy ) {
134 if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array();
135
136 $terms = wp_get_post_terms( $post_id, $taxonomy );
137
138 // Get parent terms if enabled
139 if( $grid->filter_match_parents() ) {
140 $parent_ids = array();
141 $parents = array();
142
143 foreach( $terms as $term ) {
144 if( $term->parent != 0 ) {
145 $parent_ids[] = $term->parent;
146 }
147 }
148
149 while( count( $parent_ids ) > 0 )
150 {
151 $children_ids = $parent_ids;
152 $parent_ids = array();
153
154 foreach( $children_ids as $child ) {
155 $term = get_term( $child, $taxonomy );
156 $parents[] = $term;
157
158 if( $term->parent != 0 ) {
159 $parent_ids[] = $term->parent;
160 }
161 }
162 }
163
164 $terms = array_merge( $terms, $parents );
165 $handled_terms = array();
166 }
167
168 $post_taxonomy_term_ids = array();
169
170 foreach( $terms as $term ) {
171 $slug = urldecode( $term->slug );
172
173 // Make sure we only handle each term once
174 if( $grid->filter_match_parents() ) {
175 if( in_array( $slug, $handled_terms ) ) continue;
176 $handled_terms[] = $slug;
177 }
178
179 // Posts per term
180 if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array();
181 $posts_per_term[$taxonomy][$slug][] = $post_id;
182
183 // Terms per post
184 $post_taxonomy_term_ids[] = $slug;
185
186 // Filter terms
187 $filter_terms[$slug] = array(
188 'taxonomy' => $taxonomy,
189 'name' => $term->name,
190 );
191
192
193
194 }
195
196 $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids;
197 }
198 }
199
200 $cache['taxonomies'] = $posts_per_term;
201 $cache['terms'] = $terms_per_post;
202
203 // Generate Filter
204 $filter = '';
205
206 if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) {
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>';
209
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>';
213 }
214 }
215
216 // Update Metadata
217 $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid );
218 $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid );
219
220 return array(
221 'cache' => $cache,
222 'filter' => $filter,
223 );
224 }
225 }