| 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 |
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 |
} |
| 246 |
|
| 247 |
$terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 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 |
|
| 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 |
} |
| 267 |
|
| 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 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
$cache['taxonomies'] = $posts_per_term; |
| 285 |
$cache['terms'] = $terms_per_post; |
| 286 |
|
| 287 |
// Generate Filter |
| 288 |
$filter = ''; |
| 289 |
|
| 290 |
if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) { |
| 291 |
$filter_style = $grid->filter_style(); |
| 292 |
|
| 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_keys( $filter_terms ); |
| 299 |
sort( $filter_terms_order ); |
| 300 |
$filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid ); |
| 301 |
|
| 302 |
foreach( $filter_terms_order as $slug ) { |
| 303 |
$options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false; |
| 304 |
|
| 305 |
if( $options ) { |
| 306 |
if( $grid->filter_count() ) { |
| 307 |
$name = $options['name'] . ' (' . $options['count'] . ')'; |
| 308 |
} else { |
| 309 |
$name = $options['name']; |
| 310 |
} |
| 311 |
|
| 312 |
$filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $name . '</div>'; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
// Update Metadata |
| 318 |
$cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid ); |
| 319 |
$filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid ); |
| 320 |
|
| 321 |
return array( |
| 322 |
'cache' => $cache, |
| 323 |
'filter' => $filter, |
| 324 |
); |
| 325 |
} |
| 326 |
} |