| @@ -21,9 +21,9 @@ | ||
| 21 | 21 | $update_post_post_type = $post->post_type; |
| 22 | 22 | |
| 23 | 23 | if( $update_post_post_type == WPUPG_POST_TYPE ) |
| 24 | 24 | { |
| 25 | - update_option( 'wpupg_regenerate_grids_check', array( $id ) ); | |
| 25 | + $this->generate( $id ); | |
| 26 | 26 | } else { |
| 27 | 27 | $this->update_grids_with_post_type( $update_post_post_type ); |
| 28 | 28 | } |
| 29 | 29 | } |
| @@ -89,8 +89,10 @@ | ||
| 89 | 89 | if( $grid_ids ) { |
| 90 | 90 | foreach( $grid_ids as $grid_id ) { |
| 91 | 91 | $this->generate( $grid_id ); |
| 92 | 92 | } |
| 93 | + | |
| 94 | + update_option( 'wpupg_regenerate_grids_check', false ); | |
| 93 | 95 | } |
| 94 | 96 | } |
| 95 | 97 | |
| 96 | 98 | public function generate( $grid_id ) |
| @@ -96,86 +98,12 @@ | ||
| 96 | 98 | public function generate( $grid_id ) |
| 97 | 99 | { |
| 98 | 100 | $grid = new WPUPG_Grid( $grid_id ); |
| 99 | 101 | |
| 100 | - // Don't cache term grid | |
| 101 | - if( $grid->type() !== 'terms' ) { | |
| 102 | - // Check if we're in the middle of generating this grid. | |
| 103 | - $temp_cache = get_post_meta( $grid->ID(), 'wpupg_temp_cache', true ); | |
| 102 | + $generated = $this->dynamic_generate( $grid ); | |
| 104 | 103 | |
| 105 | - if ( ! is_array( $temp_cache ) ) { | |
| 106 | - // Get posts | |
| 107 | - $post_ids = $this->generate_cache_post_ids( $grid ); | |
| 108 | - | |
| 109 | - $temp_cache = array( | |
| 110 | - 'todo' => $post_ids, | |
| 111 | - 'all' => $post_ids, | |
| 112 | - 'posts_per_term' => array(), | |
| 113 | - 'terms_per_post' => array(), | |
| 114 | - 'filter_terms' => array(), | |
| 115 | - ); | |
| 116 | - } | |
| 117 | - | |
| 118 | - $doing = array_splice( $temp_cache['todo'], 0, 50 ); | |
| 119 | - | |
| 120 | - if ( ! empty( $doing ) ) { | |
| 121 | - $terms = $this->generate_cache_terms( $grid, $doing ); | |
| 122 | - | |
| 123 | - // Posts per term should combine the same keys. | |
| 124 | - $posts_per_term = array_merge_recursive( $temp_cache['posts_per_term'], $terms['posts_per_term'] ); | |
| 125 | - $temp_cache['posts_per_term'] = $posts_per_term; | |
| 126 | - | |
| 127 | - // Terms per post should keep keys. | |
| 128 | - $terms_per_post = $temp_cache['terms_per_post'] + $terms['terms_per_post']; | |
| 129 | - $temp_cache['terms_per_post'] = $terms_per_post; | |
| 130 | - | |
| 131 | - // Filter terms requires special attention. | |
| 132 | - $filter_terms = $terms['filter_terms']; | |
| 133 | - unset( $terms['filter_terms'] ); | |
| 134 | - | |
| 135 | - foreach ( $filter_terms as $slug => $term ) { | |
| 136 | - if ( ! array_key_exists( $slug, $temp_cache['filter_terms'] ) ) { | |
| 137 | - $temp_cache['filter_terms'][ $slug ] = $term; | |
| 138 | - } else { | |
| 139 | - $temp_cache['filter_terms'][ $slug ]['count'] += $term['count']; | |
| 140 | - } | |
| 141 | - } | |
| 142 | - } | |
| 143 | - | |
| 144 | - if ( ! empty( $temp_cache['todo'] ) ) { | |
| 145 | - // Not finished yet, save current temp cache. | |
| 146 | - update_post_meta( $grid->ID(), 'wpupg_temp_cache', $temp_cache ); | |
| 147 | - } else { | |
| 148 | - // Finished getting all the terms, generate filter. | |
| 149 | - $filter_cache = $this->generate_cache_filter( $grid, $temp_cache['filter_terms'] ); | |
| 150 | - | |
| 151 | - // Apply filters. | |
| 152 | - $posts_cache = array( | |
| 153 | - 'all' => $temp_cache['all'], | |
| 154 | - 'taxonomies' => $temp_cache['posts_per_term'], | |
| 155 | - 'terms' => $temp_cache['terms_per_post'], | |
| 156 | - ); | |
| 157 | - | |
| 158 | - $posts_cache = apply_filters( 'wpupg_grid_cache_posts', $posts_cache, $grid ); | |
| 159 | - $filter_cache = apply_filters( 'wpupg_grid_cache_filter', $filter_cache, $posts_cache, $grid ); | |
| 160 | - | |
| 161 | - // Save things. | |
| 162 | - update_post_meta( $grid->ID(), 'wpupg_posts', $posts_cache ); | |
| 163 | - update_post_meta( $grid->ID(), 'wpupg_filter', $filter_cache ); | |
| 164 | - | |
| 165 | - // Clear temp cache. | |
| 166 | - update_post_meta( $grid->ID(), 'wpupg_temp_cache', false ); | |
| 167 | - | |
| 168 | - // Remove from grids to regenerate. | |
| 169 | - $grid_ids = get_option( 'wpupg_regenerate_grids_check', array() ); | |
| 170 | - $grid_ids_key = array_search( $grid->ID(), $grid_ids ); | |
| 171 | - | |
| 172 | - if ( false !== $grid_ids_key ) { | |
| 173 | - unset( $grid_ids[ $grid_ids_key ] ); | |
| 174 | - update_option( 'wpupg_regenerate_grids_check', $grid_ids ); | |
| 175 | - } | |
| 176 | - } | |
| 177 | - } | |
| 104 | + update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] ); | |
| 105 | + update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] ); | |
| 178 | 106 | } |
| 179 | 107 | |
| 180 | 108 | public function dynamic_generate( $grid ) { |
| 181 | 109 | // Don't cache term grid |
| @@ -185,36 +113,9 @@ | ||
| 185 | 113 | 'filter' => '', |
| 186 | 114 | ); |
| 187 | 115 | } |
| 188 | 116 | |
| 189 | - // Get posts | |
| 190 | - $post_ids = $this->generate_cache_post_ids( $grid ); | |
| 191 | - | |
| 192 | - // Get post terms | |
| 193 | - $terms_cache = $this->generate_cache_terms( $grid, $post_ids ); | |
| 194 | - | |
| 195 | - // Cache array | |
| 196 | - $cache = array( | |
| 197 | - 'all' => $post_ids, | |
| 198 | - 'taxonomies' => $terms_cache['posts_per_term'], | |
| 199 | - 'terms' => $terms_cache['terms_per_post'], | |
| 200 | - ); | |
| 201 | - | |
| 202 | - // Generate Filter | |
| 203 | - $filter_terms = $terms_cache['filter_terms']; | |
| 204 | - $filter = $this->generate_cache_filter( $grid, $filter_terms ); | |
| 205 | - | |
| 206 | - // Apply filters | |
| 207 | - $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid ); | |
| 208 | - $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid ); | |
| 209 | - | |
| 210 | - return array( | |
| 211 | - 'cache' => $cache, | |
| 212 | - 'filter' => $filter, | |
| 213 | - ); | |
| 214 | - } | |
| 215 | - | |
| 216 | - public function generate_cache_post_ids( $grid ) { | |
| 117 | + // Get Posts | |
| 217 | 118 | $args = array( |
| 218 | 119 | 'post_type' => $grid->post_types(), |
| 219 | 120 | 'post_status' => $grid->post_status(), |
| 220 | 121 | 'posts_per_page' => -1, |
| @@ -251,29 +152,25 @@ | ||
| 251 | 152 | $post_ids = array_map( 'intval', $posts ); |
| 252 | 153 | |
| 253 | 154 | $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid ); |
| 254 | 155 | |
| 255 | - // Offset posts | |
| 256 | - if( $grid->limit_posts_offset() ) { | |
| 257 | - $post_ids = array_slice($post_ids, $grid->limit_posts_offset() ); | |
| 258 | - } | |
| 259 | - | |
| 260 | 156 | // Limit Total # Posts |
| 261 | 157 | if( $grid->limit_posts_number() ) { |
| 262 | 158 | $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() ); |
| 263 | 159 | } |
| 264 | 160 | |
| 265 | - return $post_ids; | |
| 266 | - } | |
| 161 | + $cache = array( | |
| 162 | + 'all' => $post_ids, | |
| 163 | + ); | |
| 267 | 164 | |
| 268 | - public function generate_cache_terms( $grid, $post_ids ) { | |
| 165 | + $taxonomies = $grid->filter_taxonomies(); | |
| 166 | + $limit_terms = $grid->filter_limit_terms(); | |
| 167 | + | |
| 168 | + // Cache arrays | |
| 269 | 169 | $posts_per_term = array(); |
| 270 | 170 | $terms_per_post = array(); |
| 271 | 171 | $filter_terms = array(); |
| 272 | 172 | |
| 273 | - $taxonomies = $grid->filter_taxonomies(); | |
| 274 | - $limit_terms = $grid->filter_limit_terms(); | |
| 275 | - | |
| 276 | 173 | // Loop over all terms |
| 277 | 174 | foreach( $post_ids as $post_id ) { |
| 278 | 175 | if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array(); |
| 279 | 176 | |
| @@ -279,10 +176,10 @@ | ||
| 279 | 176 | |
| 280 | 177 | foreach( $taxonomies as $taxonomy ) { |
| 281 | 178 | if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array(); |
| 282 | 179 | |
| 283 | - $terms = get_the_terms( $post_id, $taxonomy ); | |
| 284 | - $terms = !$terms || is_wp_error( $terms ) ? array() : $terms; | |
| 180 | + $terms = wp_get_post_terms( $post_id, $taxonomy ); | |
| 181 | + $terms = is_wp_error( $terms ) ? array() : $terms; | |
| 285 | 182 | |
| 286 | 183 | // Get parent terms if enabled |
| 287 | 184 | if( $grid->filter_match_parents() ) { |
| 288 | 185 | $parent_ids = array(); |
| @@ -388,16 +285,12 @@ | ||
| 388 | 285 | } |
| 389 | 286 | } |
| 390 | 287 | } |
| 391 | 288 | |
| 392 | - return array( | |
| 393 | - 'posts_per_term' => $posts_per_term, | |
| 394 | - 'terms_per_post' => $terms_per_post, | |
| 395 | - 'filter_terms' => $filter_terms, | |
| 396 | - ); | |
| 397 | - } | |
| 289 | + $cache['taxonomies'] = $posts_per_term; | |
| 290 | + $cache['terms'] = $terms_per_post; | |
| 398 | 291 | |
| 399 | - public function generate_cache_filter( $grid, $filter_terms ) { | |
| 292 | + // Generate Filter | |
| 400 | 293 | $filter = ''; |
| 401 | 294 | |
| 402 | 295 | if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') { |
| 403 | 296 | $filter_style = $grid->filter_style(); |
| @@ -403,9 +296,9 @@ | ||
| 403 | 296 | $filter_style = $grid->filter_style(); |
| 404 | 297 | |
| 405 | 298 | // All Button |
| 406 | 299 | if( $filter_style['isotope']['all_button_text'] ) { |
| 407 | - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active" role="button" tabindex="0">' . $filter_style['isotope']['all_button_text'] . '</div>'; | |
| 300 | + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>'; | |
| 408 | 301 | } |
| 409 | 302 | |
| 410 | 303 | $filter_terms_order = array(); |
| 411 | 304 | |
| @@ -446,9 +339,9 @@ | ||
| 446 | 339 | } else { |
| 447 | 340 | $name = $options['name']; |
| 448 | 341 | } |
| 449 | 342 | |
| 450 | - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-isotope-term-' . $options['taxonomy'] . ' wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '" role="button" tabindex="0">' . $name . '</div>'; | |
| 343 | + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-isotope-term-' . $options['taxonomy'] . ' wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $name . '</div>'; | |
| 451 | 344 | } |
| 452 | 345 | } |
| 453 | 346 | |
| 454 | 347 | if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) { |
| @@ -455,7 +348,14 @@ | ||
| 455 | 348 | $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid ); |
| 456 | 349 | } |
| 457 | 350 | } |
| 458 | 351 | |
| 459 | - return $filter; | |
| 352 | + // Update Metadata | |
| 353 | + $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid ); | |
| 354 | + $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid ); | |
| 355 | + | |
| 356 | + return array( | |
| 357 | + 'cache' => $cache, | |
| 358 | + 'filter' => $filter, | |
| 359 | + ); | |
| 460 | 360 | } |
| 461 | 361 | } |