| 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 |
// Get Posts |
| 86 |
$args = array( |
| 87 |
'post_type' => $grid->post_types(), |
| 88 |
'post_status' => $grid->post_status(), |
| 89 |
'posts_per_page' => -1, |
| 90 |
'nopaging' => true, |
| 91 |
'fields' => 'ids', |
| 92 |
); |
| 93 |
|
| 94 |
// Images Only |
| 95 |
if( $grid->images_only() ) { |
| 96 |
$args['meta_query'] = array( |
| 97 |
array( |
| 98 |
'key' => '_thumbnail_id', |
| 99 |
'value' => '0', |
| 100 |
'compare' => '>' |
| 101 |
), |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
$query = new WP_Query( $args ); |
| 106 |
$posts = $query->have_posts() ? $query->posts : array(); |
| 107 |
$post_ids = array_map( 'intval', $posts ); |
| 108 |
|
| 109 |
$post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid ); |
| 110 |
|
| 111 |
$cache = array( |
| 112 |
'all' => $post_ids, |
| 113 |
); |
| 114 |
|
| 115 |
$taxonomies = $grid->filter_taxonomies(); |
| 116 |
|
| 117 |
// Cache arrays |
| 118 |
$posts_per_term = array(); |
| 119 |
$terms_per_post = array(); |
| 120 |
$filter_terms = array(); |
| 121 |
|
| 122 |
// Loop over all terms |
| 123 |
foreach( $post_ids as $post_id ) { |
| 124 |
if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array(); |
| 125 |
|
| 126 |
foreach( $taxonomies as $taxonomy ) { |
| 127 |
if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array(); |
| 128 |
|
| 129 |
$terms = wp_get_post_terms( $post_id, $taxonomy ); |
| 130 |
|
| 131 |
// Get parent terms if enabled |
| 132 |
if( $grid->filter_match_parents() ) { |
| 133 |
$parent_ids = array(); |
| 134 |
$parents = array(); |
| 135 |
|
| 136 |
foreach( $terms as $term ) { |
| 137 |
if( $term->parent != 0 ) { |
| 138 |
$parent_ids[] = $term->parent; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
while( count( $parent_ids ) > 0 ) |
| 143 |
{ |
| 144 |
$children_ids = $parent_ids; |
| 145 |
$parent_ids = array(); |
| 146 |
|
| 147 |
foreach( $children_ids as $child ) { |
| 148 |
$term = get_term( $child, $taxonomy ); |
| 149 |
$parents[] = $term; |
| 150 |
|
| 151 |
if( $term->parent != 0 ) { |
| 152 |
$parent_ids[] = $term->parent; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
$terms = array_merge( $terms, $parents ); |
| 158 |
$handled_terms = array(); |
| 159 |
} |
| 160 |
|
| 161 |
$post_taxonomy_term_ids = array(); |
| 162 |
|
| 163 |
foreach( $terms as $term ) { |
| 164 |
// Make sure we only handle each term once |
| 165 |
if( $grid->filter_match_parents() ) { |
| 166 |
if( in_array( $term->slug, $handled_terms ) ) continue; |
| 167 |
$handled_terms[] = $term->slug; |
| 168 |
} |
| 169 |
|
| 170 |
// Posts per term |
| 171 |
if( !isset( $posts_per_term[$taxonomy][$term->slug] ) ) $posts_per_term[$taxonomy][$term->slug] = array(); |
| 172 |
$posts_per_term[$taxonomy][$term->slug][] = $post_id; |
| 173 |
|
| 174 |
// Terms per post |
| 175 |
$post_taxonomy_term_ids[] = $term->slug; |
| 176 |
|
| 177 |
// Filter terms |
| 178 |
$filter_terms[$term->slug] = array( |
| 179 |
'taxonomy' => $taxonomy, |
| 180 |
'name' => $term->name, |
| 181 |
); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
$terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
$cache['taxonomies'] = $posts_per_term; |
| 192 |
$cache['terms'] = $terms_per_post; |
| 193 |
|
| 194 |
// Generate Filter |
| 195 |
$filter = ''; |
| 196 |
|
| 197 |
if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) { |
| 198 |
$filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . __( 'All', 'wp-ultimate-post-grid' ) . '</div>'; |
| 199 |
|
| 200 |
ksort( $filter_terms ); |
| 201 |
foreach( $filter_terms as $slug => $options ) { |
| 202 |
$filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>'; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Update Metadata |
| 207 |
$cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid ); |
| 208 |
$filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid ); |
| 209 |
update_post_meta( $grid_id, 'wpupg_posts', $cache ); |
| 210 |
update_post_meta( $grid_id, 'wpupg_filter', $filter ); |
| 211 |
} |
| 212 |
} |