| @@ -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 | } |
| @@ -39,21 +39,16 @@ | ||
| 39 | 39 | |
| 40 | 40 | $query = new WP_Query( $args ); |
| 41 | 41 | $posts = $query->have_posts() ? $query->posts : array(); |
| 42 | 42 | |
| 43 | - $grid_ids = array(); | |
| 44 | 43 | foreach ( $posts as $grid_post ) |
| 45 | 44 | { |
| 46 | - $grid = WPUPG_Grid_Manager::get( $grid_post ); | |
| 45 | + $grid = new WPUPG_Grid( $grid_post ); | |
| 47 | 46 | |
| 48 | 47 | if( in_array( $post_type, $grid->post_types() ) ) { |
| 49 | - $grid_ids[] = $grid->ID(); | |
| 48 | + $this->generate( $grid->ID() ); | |
| 50 | 49 | } |
| 51 | 50 | } |
| 52 | - | |
| 53 | - if( count( $grid_ids ) > 0 ) { | |
| 54 | - update_option( 'wpupg_regenerate_grids_check', $grid_ids ); | |
| 55 | - } | |
| 56 | 51 | } |
| 57 | 52 | |
| 58 | 53 | public function updated_term( $term_id, $taxonomy ) |
| 59 | 54 | { |
| @@ -69,9 +64,9 @@ | ||
| 69 | 64 | |
| 70 | 65 | $grid_ids = array(); |
| 71 | 66 | foreach ( $posts as $post ) |
| 72 | 67 | { |
| 73 | - $grid = WPUPG_Grid_Manager::get( $post ); | |
| 68 | + $grid = new WPUPG_Grid( $post ); | |
| 74 | 69 | |
| 75 | 70 | if( in_array( $taxonomy, $grid->filter_taxonomies() ) ) { |
| 76 | 71 | $grid_ids[] = $grid->ID(); |
| 77 | 72 | } |
| @@ -89,93 +84,21 @@ | ||
| 89 | 84 | if( $grid_ids ) { |
| 90 | 85 | foreach( $grid_ids as $grid_id ) { |
| 91 | 86 | $this->generate( $grid_id ); |
| 92 | 87 | } |
| 88 | + | |
| 89 | + update_option( 'wpupg_regenerate_grids_check', false ); | |
| 93 | 90 | } |
| 94 | 91 | } |
| 95 | 92 | |
| 96 | 93 | public function generate( $grid_id ) |
| 97 | 94 | { |
| 98 | - $grid = WPUPG_Grid_Manager::get( $grid_id ); | |
| 95 | + $grid = new WPUPG_Grid( $grid_id ); | |
| 99 | 96 | |
| 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 ); | |
| 97 | + $generated = $this->dynamic_generate( $grid ); | |
| 104 | 98 | |
| 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 | - } | |
| 99 | + update_post_meta( $grid->ID(), 'wpupg_posts', $generated['cache'] ); | |
| 100 | + update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] ); | |
| 178 | 101 | } |
| 179 | 102 | |
| 180 | 103 | public function dynamic_generate( $grid ) { |
| 181 | 104 | // Don't cache term grid |
| @@ -185,36 +108,9 @@ | ||
| 185 | 108 | 'filter' => '', |
| 186 | 109 | ); |
| 187 | 110 | } |
| 188 | 111 | |
| 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 ) { | |
| 112 | + // Get Posts | |
| 217 | 113 | $args = array( |
| 218 | 114 | 'post_type' => $grid->post_types(), |
| 219 | 115 | 'post_status' => $grid->post_status(), |
| 220 | 116 | 'posts_per_page' => -1, |
| @@ -244,10 +140,8 @@ | ||
| 244 | 140 | ); |
| 245 | 141 | } |
| 246 | 142 | } |
| 247 | 143 | |
| 248 | - $args = apply_filters( 'wpupg_grid_cache_post_ids_args', $args, $grid ); | |
| 249 | - | |
| 250 | 144 | $query = new WP_Query( $args ); |
| 251 | 145 | $posts = $query->have_posts() ? $query->posts : array(); |
| 252 | 146 | |
| 253 | 147 | $post_ids = array_map( 'intval', $posts ); |
| @@ -253,29 +147,25 @@ | ||
| 253 | 147 | $post_ids = array_map( 'intval', $posts ); |
| 254 | 148 | |
| 255 | 149 | $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid ); |
| 256 | 150 | |
| 257 | - // Offset posts | |
| 258 | - if( $grid->limit_posts_offset() ) { | |
| 259 | - $post_ids = array_slice($post_ids, $grid->limit_posts_offset() ); | |
| 260 | - } | |
| 261 | - | |
| 262 | 151 | // Limit Total # Posts |
| 263 | 152 | if( $grid->limit_posts_number() ) { |
| 264 | 153 | $post_ids = array_slice($post_ids, 0, $grid->limit_posts_number() ); |
| 265 | 154 | } |
| 266 | 155 | |
| 267 | - return $post_ids; | |
| 268 | - } | |
| 156 | + $cache = array( | |
| 157 | + 'all' => $post_ids, | |
| 158 | + ); | |
| 269 | 159 | |
| 270 | - public function generate_cache_terms( $grid, $post_ids ) { | |
| 160 | + $taxonomies = $grid->filter_taxonomies(); | |
| 161 | + $limit_terms = $grid->filter_limit_terms(); | |
| 162 | + | |
| 163 | + // Cache arrays | |
| 271 | 164 | $posts_per_term = array(); |
| 272 | 165 | $terms_per_post = array(); |
| 273 | 166 | $filter_terms = array(); |
| 274 | 167 | |
| 275 | - $taxonomies = $grid->filter_taxonomies(); | |
| 276 | - $limit_terms = $grid->filter_limit_terms(); | |
| 277 | - | |
| 278 | 168 | // Loop over all terms |
| 279 | 169 | foreach( $post_ids as $post_id ) { |
| 280 | 170 | if( !isset( $terms_per_post[$post_id] ) ) $terms_per_post[$post_id] = array(); |
| 281 | 171 | |
| @@ -281,10 +171,10 @@ | ||
| 281 | 171 | |
| 282 | 172 | foreach( $taxonomies as $taxonomy ) { |
| 283 | 173 | if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array(); |
| 284 | 174 | |
| 285 | - $terms = get_the_terms( $post_id, $taxonomy ); | |
| 286 | - $terms = !$terms || is_wp_error( $terms ) ? array() : $terms; | |
| 175 | + $terms = wp_get_post_terms( $post_id, $taxonomy ); | |
| 176 | + $terms = is_wp_error( $terms ) ? array() : $terms; | |
| 287 | 177 | |
| 288 | 178 | // Get parent terms if enabled |
| 289 | 179 | if( $grid->filter_match_parents() ) { |
| 290 | 180 | $parent_ids = array(); |
| @@ -341,18 +231,13 @@ | ||
| 341 | 231 | |
| 342 | 232 | // Terms per post |
| 343 | 233 | $post_taxonomy_term_ids[] = $slug; |
| 344 | 234 | |
| 345 | - if( !array_key_exists( $slug, $filter_terms ) ) { | |
| 346 | - // Filter terms | |
| 347 | - $filter_terms[$slug] = array( | |
| 348 | - 'taxonomy' => $taxonomy, | |
| 349 | - 'name' => $term->name, | |
| 350 | - 'count' => 1, | |
| 351 | - ); | |
| 352 | - } else { | |
| 353 | - $filter_terms[$slug]['count']++; | |
| 354 | - } | |
| 235 | + // Filter terms | |
| 236 | + $filter_terms[$slug] = array( | |
| 237 | + 'taxonomy' => $taxonomy, | |
| 238 | + 'name' => $term->name, | |
| 239 | + ); | |
| 355 | 240 | } |
| 356 | 241 | |
| 357 | 242 | $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids; |
| 358 | 243 | } |
| @@ -357,117 +242,41 @@ | ||
| 357 | 242 | $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids; |
| 358 | 243 | } |
| 359 | 244 | } |
| 360 | 245 | |
| 361 | - // Show Empty Terms | |
| 362 | - if( $grid->filter_show_empty() ) { | |
| 363 | - foreach( $taxonomies as $taxonomy ) { | |
| 364 | - $terms = get_terms( $taxonomy, array( | |
| 365 | - 'hide_empty' => false, | |
| 366 | - ) ); | |
| 246 | + $cache['taxonomies'] = $posts_per_term; | |
| 247 | + $cache['terms'] = $terms_per_post; | |
| 367 | 248 | |
| 368 | - foreach( $terms as $term ) { | |
| 369 | - // Check if terms are being limited | |
| 370 | - if( $grid->filter_limit() ) { | |
| 371 | - if( $grid->filter_limit_exclude() ) { | |
| 372 | - if( isset( $limit_terms[$taxonomy] ) && in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue; | |
| 373 | - } else { | |
| 374 | - if( !isset( $limit_terms[$taxonomy] ) || !in_array( $term->term_id, $limit_terms[$taxonomy] ) ) continue; | |
| 375 | - } | |
| 376 | - } | |
| 377 | - | |
| 378 | - $slug = urldecode( $term->slug ); | |
| 379 | - | |
| 380 | - if( !isset( $posts_per_term[$taxonomy][$slug] ) ) $posts_per_term[$taxonomy][$slug] = array(); | |
| 381 | - | |
| 382 | - if( !array_key_exists( $slug, $filter_terms ) ) { | |
| 383 | - // Filter terms | |
| 384 | - $filter_terms[$slug] = array( | |
| 385 | - 'taxonomy' => $taxonomy, | |
| 386 | - 'name' => $term->name, | |
| 387 | - 'count' => 0, | |
| 388 | - ); | |
| 389 | - } | |
| 390 | - } | |
| 391 | - } | |
| 392 | - } | |
| 393 | - | |
| 394 | - return array( | |
| 395 | - 'posts_per_term' => $posts_per_term, | |
| 396 | - 'terms_per_post' => $terms_per_post, | |
| 397 | - 'filter_terms' => $filter_terms, | |
| 398 | - ); | |
| 399 | - } | |
| 400 | - | |
| 401 | - public function generate_cache_filter( $grid, $filter_terms ) { | |
| 249 | + // Generate Filter | |
| 402 | 250 | $filter = ''; |
| 403 | 251 | |
| 404 | - if( count( $filter_terms ) > 0 && ($grid->filter_type() == 'isotope' ) || $grid->filter_type() == 'text_isotope') { | |
| 252 | + if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) { | |
| 405 | 253 | $filter_style = $grid->filter_style(); |
| 406 | 254 | |
| 407 | 255 | // All Button |
| 408 | 256 | if( $filter_style['isotope']['all_button_text'] ) { |
| 409 | - $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>'; | |
| 257 | + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>'; | |
| 410 | 258 | } |
| 411 | 259 | |
| 412 | - $filter_terms_order = array(); | |
| 413 | - | |
| 414 | - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies_grouped' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ) { | |
| 415 | - // Order alphatically per taxonomy | |
| 416 | - $terms_per_taxonomy = array(); | |
| 417 | - | |
| 418 | - foreach( $filter_terms as $slug => $term ) { | |
| 419 | - $taxonomy = $term['taxonomy']; | |
| 420 | - if( !array_key_exists( $taxonomy, $terms_per_taxonomy ) ) { | |
| 421 | - $terms_per_taxonomy[$taxonomy] = array(); | |
| 422 | - } | |
| 423 | - $terms_per_taxonomy[$taxonomy][] = $slug; | |
| 424 | - } | |
| 425 | - | |
| 426 | - foreach( $terms_per_taxonomy as $taxonomy => $terms ) { | |
| 427 | - sort( $terms ); | |
| 428 | - $filter_terms_order = array_merge( $filter_terms_order, $terms ); | |
| 429 | - } | |
| 430 | - } else { | |
| 431 | - // Order alphabetically by default | |
| 432 | - $filter_terms_order = array_keys( $filter_terms ); | |
| 433 | - sort( $filter_terms_order ); | |
| 434 | - } | |
| 435 | - | |
| 436 | - if( $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ) { | |
| 437 | - $filter_terms_order = array_reverse( $filter_terms_order ); | |
| 438 | - } | |
| 439 | - | |
| 260 | + $filter_terms_order = array_keys( $filter_terms ); | |
| 261 | + sort( $filter_terms_order ); | |
| 440 | 262 | $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid ); |
| 441 | 263 | |
| 442 | - $current_taxonomy = ''; | |
| 443 | 264 | foreach( $filter_terms_order as $slug ) { |
| 444 | 265 | $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false; |
| 445 | 266 | |
| 446 | 267 | if( $options ) { |
| 447 | - if( $grid->filter_count() ) { | |
| 448 | - $name = $options['name'] . ' (' . $options['count'] . ')'; | |
| 449 | - } else { | |
| 450 | - $name = $options['name']; | |
| 451 | - } | |
| 452 | - | |
| 453 | - if( $filter_style['isotope']['term_order'] == 'alphabetical_taxonomies_grouped' || $filter_style['isotope']['term_order'] == 'reverse_alphabetical_taxonomies_grouped' ){ | |
| 454 | - if( ( $options['taxonomy'] != $current_taxonomy ) && $current_taxonomy != '' ){ | |
| 455 | - $filter .= '</div>'; | |
| 456 | - } | |
| 457 | - if( $options['taxonomy'] != $current_taxonomy ){ | |
| 458 | - $filter .= '<div class="wpupg-filter-tax-container wpupg-filter-tax-' . $options['taxonomy'] . '">'; | |
| 459 | - } | |
| 460 | - $current_taxonomy = $options['taxonomy']; | |
| 461 | - } | |
| 462 | - $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>'; | |
| 268 | + $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag-' . $slug . '" data-taxonomy="' . $options['taxonomy'] . '" data-filter="' . $slug . '">' . $options['name'] . '</div>'; | |
| 463 | 269 | } |
| 464 | 270 | } |
| 271 | + } | |
| 465 | 272 | |
| 466 | - if( $grid->filter_type() == 'text_isotope' && WPUltimatePostGrid::is_addon_active('filter-text') ) { | |
| 467 | - $filter .= WPUltimatePostGrid::addon('filter-text')->get_filter( $grid ); | |
| 468 | - } | |
| 469 | - } | |
| 273 | + // Update Metadata | |
| 274 | + $cache = apply_filters( 'wpupg_grid_cache_posts', $cache, $grid ); | |
| 275 | + $filter = apply_filters( 'wpupg_grid_cache_filter', $filter, $cache, $grid ); | |
| 470 | 276 | |
| 471 | - return $filter; | |
| 277 | + return array( | |
| 278 | + 'cache' => $cache, | |
| 279 | + 'filter' => $filter, | |
| 280 | + ); | |
| 472 | 281 | } |
| 473 | 282 | } |