| @@ -3,20 +3,13 @@ | ||
| 3 | 3 | class WPUPG_Grid_Cache { |
| 4 | 4 | |
| 5 | 5 | public function __construct() |
| 6 | 6 | { |
| 7 | - add_action( 'add_attachment', array( $this, 'updated_attachment' ) ); | |
| 8 | - add_action( 'edit_attachment', array( $this, 'updated_attachment' ) ); | |
| 9 | 7 | add_action( 'save_post', array( $this, 'updated_post' ), 11, 2 ); |
| 10 | 8 | add_action( 'edited_terms', array( $this, 'updated_term' ), 10, 2 ); |
| 11 | 9 | add_action( 'admin_init', array( $this, 'regenerate_grids_check' ) ); |
| 12 | 10 | } |
| 13 | 11 | |
| 14 | - public function updated_attachment( $id ) | |
| 15 | - { | |
| 16 | - $this->update_grids_with_post_type( 'attachment' ); | |
| 17 | - } | |
| 18 | - | |
| 19 | 12 | public function updated_post( $id, $post ) |
| 20 | 13 | { |
| 21 | 14 | $update_post_post_type = $post->post_type; |
| 22 | 15 | |
| @@ -23,30 +16,25 @@ | ||
| 23 | 16 | if( $update_post_post_type == WPUPG_POST_TYPE ) |
| 24 | 17 | { |
| 25 | 18 | $this->generate( $id ); |
| 26 | 19 | } else { |
| 27 | - $this->update_grids_with_post_type( $update_post_post_type ); | |
| 28 | - } | |
| 29 | - } | |
| 20 | + $args = array( | |
| 21 | + 'post_type' => WPUPG_POST_TYPE, | |
| 22 | + 'post_status' => 'any', | |
| 23 | + 'posts_per_page' => -1, | |
| 24 | + 'nopaging' => true, | |
| 25 | + ); | |
| 30 | 26 | |
| 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 | - ); | |
| 27 | + $query = new WP_Query( $args ); | |
| 28 | + $posts = $query->have_posts() ? $query->posts : array(); | |
| 39 | 29 | |
| 40 | - $query = new WP_Query( $args ); | |
| 41 | - $posts = $query->have_posts() ? $query->posts : array(); | |
| 30 | + foreach ( $posts as $grid_post ) | |
| 31 | + { | |
| 32 | + $grid = new WPUPG_Grid( $grid_post ); | |
| 42 | 33 | |
| 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() ); | |
| 34 | + if( in_array( $update_post_post_type, $grid->post_types() ) ) { | |
| 35 | + $this->generate( $grid->ID() ); | |
| 36 | + } | |
| 49 | 37 | } |
| 50 | 38 | } |
| 51 | 39 | } |
| 52 | 40 | |
| @@ -100,16 +88,8 @@ | ||
| 100 | 88 | update_post_meta( $grid->ID(), 'wpupg_filter', $generated['filter'] ); |
| 101 | 89 | } |
| 102 | 90 | |
| 103 | 91 | 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 | 92 | // Get Posts |
| 113 | 93 | $args = array( |
| 114 | 94 | 'post_type' => $grid->post_types(), |
| 115 | 95 | 'post_status' => $grid->post_status(), |
| @@ -114,52 +94,33 @@ | ||
| 114 | 94 | 'post_type' => $grid->post_types(), |
| 115 | 95 | 'post_status' => $grid->post_status(), |
| 116 | 96 | 'posts_per_page' => -1, |
| 117 | 97 | 'nopaging' => true, |
| 118 | - 'order' => $grid->order(), | |
| 119 | 98 | 'fields' => 'ids', |
| 120 | 99 | ); |
| 121 | 100 | |
| 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 | 101 | // Images Only |
| 130 | 102 | 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 | - } | |
| 103 | + $args['meta_query'] = array( | |
| 104 | + array( | |
| 105 | + 'key' => '_thumbnail_id', | |
| 106 | + 'value' => '0', | |
| 107 | + 'compare' => '>' | |
| 108 | + ), | |
| 109 | + ); | |
| 142 | 110 | } |
| 143 | 111 | |
| 144 | 112 | $query = new WP_Query( $args ); |
| 145 | 113 | $posts = $query->have_posts() ? $query->posts : array(); |
| 146 | - | |
| 147 | 114 | $post_ids = array_map( 'intval', $posts ); |
| 148 | 115 | |
| 149 | 116 | $post_ids = apply_filters( 'wpupg_grid_cache_post_ids', $post_ids, $grid ); |
| 150 | 117 | |
| 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 | 118 | $cache = array( |
| 157 | 119 | 'all' => $post_ids, |
| 158 | 120 | ); |
| 159 | 121 | |
| 160 | 122 | $taxonomies = $grid->filter_taxonomies(); |
| 161 | - $limit_terms = $grid->filter_limit_terms(); | |
| 162 | 123 | |
| 163 | 124 | // Cache arrays |
| 164 | 125 | $posts_per_term = array(); |
| 165 | 126 | $terms_per_post = array(); |
| @@ -172,9 +133,8 @@ | ||
| 172 | 133 | foreach( $taxonomies as $taxonomy ) { |
| 173 | 134 | if( !isset( $posts_per_term[$taxonomy] ) ) $posts_per_term[$taxonomy] = array(); |
| 174 | 135 | |
| 175 | 136 | $terms = wp_get_post_terms( $post_id, $taxonomy ); |
| 176 | - $terms = is_wp_error( $terms ) ? array() : $terms; | |
| 177 | 137 | |
| 178 | 138 | // Get parent terms if enabled |
| 179 | 139 | if( $grid->filter_match_parents() ) { |
| 180 | 140 | $parent_ids = array(); |
| @@ -207,17 +167,8 @@ | ||
| 207 | 167 | |
| 208 | 168 | $post_taxonomy_term_ids = array(); |
| 209 | 169 | |
| 210 | 170 | 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 | 171 | $slug = urldecode( $term->slug ); |
| 221 | 172 | |
| 222 | 173 | // Make sure we only handle each term once |
| 223 | 174 | if( $grid->filter_match_parents() ) { |
| @@ -236,8 +187,11 @@ | ||
| 236 | 187 | $filter_terms[$slug] = array( |
| 237 | 188 | 'taxonomy' => $taxonomy, |
| 238 | 189 | 'name' => $term->name, |
| 239 | 190 | ); |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 240 | 194 | } |
| 241 | 195 | |
| 242 | 196 | $terms_per_post[$post_id][$taxonomy] = $post_taxonomy_term_ids; |
| 243 | 197 | } |
| @@ -250,24 +204,13 @@ | ||
| 250 | 204 | $filter = ''; |
| 251 | 205 | |
| 252 | 206 | if( count( $filter_terms ) > 0 && $grid->filter_type() == 'isotope' ) { |
| 253 | 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>'; | |
| 254 | 209 | |
| 255 | - // All Button | |
| 256 | - if( $filter_style['isotope']['all_button_text'] ) { | |
| 257 | - $filter .= '<div class="wpupg-filter-item wpupg-filter-isotope-term wpupg-filter-tag- active">' . $filter_style['isotope']['all_button_text'] . '</div>'; | |
| 258 | - } | |
| 259 | - | |
| 260 | - $filter_terms_order = array_keys( $filter_terms ); | |
| 261 | - sort( $filter_terms_order ); | |
| 262 | - $filter_terms_order = apply_filters( 'wpupg_grid_cache_filter_isotope_term_order', $filter_terms_order, $grid ); | |
| 263 | - | |
| 264 | - foreach( $filter_terms_order as $slug ) { | |
| 265 | - $options = isset( $filter_terms[$slug] ) ? $filter_terms[$slug] : false; | |
| 266 | - | |
| 267 | - if( $options ) { | |
| 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>'; | |
| 269 | - } | |
| 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>'; | |
| 270 | 213 | } |
| 271 | 214 | } |
| 272 | 215 | |
| 273 | 216 | // Update Metadata |