| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace RadiusTheme\SB\Models; |
| 9 | 9 | |
| 10 | 10 | // Do not allow directly accessing this file. |
| 11 | +use RadiusTheme\SB\Helpers\Fns; | |
| 12 | + | |
| 11 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 14 | exit( 'This script cannot be accessed directly.' ); |
| 13 | 15 | } |
| 14 | 16 | |
| @@ -43,23 +45,19 @@ | ||
| 43 | 45 | public function buildArgs( array $meta, string $type = 'product', bool $isCarousel = false ) { |
| 44 | 46 | $this->meta = $meta; |
| 45 | 47 | |
| 46 | 48 | if ( 'product' === $type ) { |
| 47 | - // Post Type. | |
| 48 | - $this->getPostType(); | |
| 49 | - | |
| 50 | - // Building Args. | |
| 51 | 49 | $this |
| 52 | - ->postParams() | |
| 53 | - ->orderParams() | |
| 54 | - ->paginationParams( $isCarousel ) | |
| 55 | - ->taxParams(); | |
| 50 | + ->post_params() | |
| 51 | + ->order_params() | |
| 52 | + ->pagination_params( $isCarousel ) | |
| 53 | + ->tax_params(); | |
| 56 | 54 | |
| 57 | 55 | } else { |
| 58 | 56 | $this |
| 59 | - ->getTaxType() | |
| 60 | - ->catParams() | |
| 61 | - ->catOrderParams(); | |
| 57 | + ->get_tax_type() | |
| 58 | + ->cat_params() | |
| 59 | + ->cat_order_params(); | |
| 62 | 60 | } |
| 63 | 61 | |
| 64 | 62 | return apply_filters( 'rtsb/elements/' . $type . '_query_args', $this->args, $this->meta ); |
| 65 | 63 | } |
| @@ -66,21 +64,11 @@ | ||
| 66 | 64 | |
| 67 | 65 | /** |
| 68 | 66 | * Post type. |
| 69 | 67 | * |
| 70 | - * @return void | |
| 71 | - */ | |
| 72 | - private function getPostType() { | |
| 73 | - $this->args['post_type'] = [ rtsb()->post_type ]; | |
| 74 | - $this->args['post_status'] = 'publish'; | |
| 75 | - } | |
| 76 | - | |
| 77 | - /** | |
| 78 | - * Post type. | |
| 79 | - * | |
| 80 | 68 | * @return QueryArgs |
| 81 | 69 | */ |
| 82 | - private function getTaxType() { | |
| 70 | + private function get_tax_type() { | |
| 83 | 71 | $this->args['taxonomy'] = [ 'product_cat' ]; |
| 84 | 72 | $this->args['post_status'] = 'publish'; |
| 85 | 73 | $this->args['hierarchical'] = 1; |
| 86 | 74 | |
| @@ -87,35 +75,34 @@ | ||
| 87 | 75 | return $this; |
| 88 | 76 | } |
| 89 | 77 | |
| 90 | 78 | /** |
| 91 | - * Post parameters. | |
| 79 | + * WC Post parameters. | |
| 92 | 80 | * |
| 93 | 81 | * @return QueryArgs |
| 94 | 82 | */ |
| 95 | - private function postParams() { | |
| 96 | - $post_in = isset( $this->meta['post_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['post_in'] ) ) : null; | |
| 83 | + private function post_params() { | |
| 84 | + $post_in = isset( $this->meta['post_in'] ) ? sanitize_text_field( implode( ',', $this->meta['post_in'] ) ) : null; | |
| 97 | 85 | $post_not_in = isset( $this->meta['post_not_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['post_not_in'] ) ) : null; |
| 98 | - $author_in = isset( $this->meta['author_in'] ) ? sanitize_text_field( implode( ', ', $this->meta['author_in'] ) ) : null; | |
| 86 | + $author_in = isset( $this->meta['author_in'] ) ? sanitize_text_field( implode( ',', $this->meta['author_in'] ) ) : null; | |
| 99 | 87 | $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 100 | 88 | $offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0; |
| 101 | 89 | |
| 102 | 90 | if ( $post_in ) { |
| 103 | - $post_in = explode( ',', $post_in ); | |
| 104 | - $this->args['post__in'] = $post_in; | |
| 91 | + $post_in = explode( ',', $post_in ); | |
| 92 | + $this->args['include'] = $post_in; | |
| 105 | 93 | } |
| 106 | 94 | |
| 107 | 95 | if ( $post_not_in ) { |
| 108 | - $post_not_in = explode( ',', $post_not_in ); | |
| 109 | - $this->args['post__not_in'] = $post_not_in; | |
| 96 | + $post_not_in = explode( ',', $post_not_in ); | |
| 97 | + $this->args['exclude'] = $post_not_in; // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude | |
| 110 | 98 | } |
| 111 | 99 | |
| 112 | 100 | if ( $author_in ) { |
| 113 | - $author_in = explode( ',', $author_in ); | |
| 114 | - $this->args['author__in'] = $author_in; | |
| 101 | + $this->args['author'] = $author_in; | |
| 115 | 102 | } |
| 116 | 103 | |
| 117 | - $this->args['posts_per_page'] = $limit; | |
| 104 | + $this->args['limit'] = $limit; | |
| 118 | 105 | |
| 119 | 106 | if ( $offset ) { |
| 120 | 107 | $this->args['offset'] = $offset; |
| 121 | 108 | } |
| @@ -127,9 +114,9 @@ | ||
| 127 | 114 | * Category parameters. |
| 128 | 115 | * |
| 129 | 116 | * @return QueryArgs |
| 130 | 117 | */ |
| 131 | - private function catParams() { | |
| 118 | + private function cat_params() { | |
| 132 | 119 | $cats_in = isset( $this->meta['include_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['include_cats'] ) ) : null; |
| 133 | 120 | $cat_ids_in = isset( $this->meta['select_cat_ids'] ) ? esc_html( $this->meta['select_cat_ids'] ) : null; |
| 134 | 121 | $cats_not_in = isset( $this->meta['exclude_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['exclude_cats'] ) ) : null; |
| 135 | 122 | $limit = ( ( empty( $this->meta['cats_limit'] ) || '-1' === $this->meta['cats_limit'] ) ? 10000000 : absint( $this->meta['cats_limit'] ) ); |
| @@ -147,9 +134,9 @@ | ||
| 147 | 134 | } |
| 148 | 135 | |
| 149 | 136 | if ( $cats_not_in ) { |
| 150 | 137 | $cats_not_in = array_filter( explode( ',', $cats_not_in ) ); |
| 151 | - $this->args['exclude'] = $cats_not_in; | |
| 138 | + $this->args['exclude'] = $cats_not_in; // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude | |
| 152 | 139 | } |
| 153 | 140 | |
| 154 | 141 | $this->args['number'] = $limit; |
| 155 | 142 | $this->args['hide_empty'] = $this->meta['show_empty'] ? 0 : 1; |
| @@ -183,13 +170,13 @@ | ||
| 183 | 170 | return $this; |
| 184 | 171 | } |
| 185 | 172 | |
| 186 | 173 | /** |
| 187 | - * Order & Orderby parameters. | |
| 174 | + * WC Order & Orderby parameters. | |
| 188 | 175 | * |
| 189 | 176 | * @return QueryArgs |
| 190 | 177 | */ |
| 191 | - private function orderParams() { | |
| 178 | + private function order_params() { | |
| 192 | 179 | $order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null ); |
| 193 | 180 | $order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null ); |
| 194 | 181 | |
| 195 | 182 | if ( $order ) { |
| @@ -197,8 +184,17 @@ | ||
| 197 | 184 | } |
| 198 | 185 | |
| 199 | 186 | if ( $order_by ) { |
| 200 | 187 | $this->args['orderby'] = $order_by; |
| 188 | + | |
| 189 | + if ( 'menu_order' === $order_by ) { | |
| 190 | + $this->args['orderby'] = 'menu_order title'; | |
| 191 | + } | |
| 192 | + | |
| 193 | + if ( 'price' === $order_by ) { | |
| 194 | + $this->args['orderby'] = 'meta_value_num'; | |
| 195 | + $this->args['meta_key'] = '_price'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 196 | + } | |
| 201 | 197 | } |
| 202 | 198 | |
| 203 | 199 | return $this; |
| 204 | 200 | } |
| @@ -207,9 +203,9 @@ | ||
| 207 | 203 | * Order & Orderby parameters. |
| 208 | 204 | * |
| 209 | 205 | * @return QueryArgs |
| 210 | 206 | */ |
| 211 | - private function catOrderParams() { | |
| 207 | + private function cat_order_params() { | |
| 212 | 208 | $order_by = ( isset( $this->meta['cats_order_by'] ) ? esc_html( $this->meta['cats_order_by'] ) : 'name' ); |
| 213 | 209 | $order = ( isset( $this->meta['cats_order'] ) ? esc_html( $this->meta['cats_order'] ) : 'DESC' ); |
| 214 | 210 | |
| 215 | 211 | if ( $order ) { |
| @@ -233,13 +229,15 @@ | ||
| 233 | 229 | * @param bool $isCarousel Layout type. |
| 234 | 230 | * |
| 235 | 231 | * @return QueryArgs |
| 236 | 232 | */ |
| 237 | - private function paginationParams( $isCarousel ) { | |
| 233 | + private function pagination_params( $isCarousel ) { | |
| 238 | 234 | $pagination = ! empty( $this->meta['pagination'] ); |
| 239 | 235 | $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 240 | 236 | |
| 241 | 237 | if ( $pagination ) { |
| 238 | + unset( $this->args['offset'] ); | |
| 239 | + | |
| 242 | 240 | $posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit ); |
| 243 | 241 | |
| 244 | 242 | if ( $posts_per_page > $limit ) { |
| 245 | 243 | $posts_per_page = $limit; |
| @@ -244,9 +242,10 @@ | ||
| 244 | 242 | if ( $posts_per_page > $limit ) { |
| 245 | 243 | $posts_per_page = $limit; |
| 246 | 244 | } |
| 247 | 245 | |
| 248 | - $this->args['posts_per_page'] = $posts_per_page; | |
| 246 | + $this->args['paginate'] = true; | |
| 247 | + $this->args['limit'] = $posts_per_page; | |
| 249 | 248 | |
| 250 | 249 | if ( is_front_page() ) { |
| 251 | 250 | $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; |
| 252 | 251 | } else { |
| @@ -252,19 +251,19 @@ | ||
| 252 | 251 | } else { |
| 253 | 252 | $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; |
| 254 | 253 | } |
| 255 | 254 | |
| 256 | - $offset = $posts_per_page * ( (int) $paged - 1 ); | |
| 257 | - $this->args['paged'] = $paged; | |
| 255 | + $offset = $posts_per_page * ( (int) $paged - 1 ); | |
| 256 | + $this->args['page'] = $paged; | |
| 258 | 257 | |
| 259 | - if ( absint( $this->args['posts_per_page'] ) > $limit - $offset ) { | |
| 260 | - $this->args['posts_per_page'] = $limit - $offset; | |
| 261 | - $this->args['offset'] = $offset; | |
| 258 | + if ( absint( $this->args['limit'] ) > $limit - $offset ) { | |
| 259 | + $this->args['limit'] = $limit - $offset; | |
| 260 | + $this->args['offset'] = $offset; | |
| 262 | 261 | } |
| 263 | 262 | } |
| 264 | 263 | |
| 265 | 264 | if ( $isCarousel ) { |
| 266 | - $this->args['posts_per_page'] = $limit; | |
| 265 | + $this->args['limit'] = $limit; | |
| 267 | 266 | } |
| 268 | 267 | |
| 269 | 268 | return $this; |
| 270 | 269 | } |
| @@ -271,57 +270,24 @@ | ||
| 271 | 270 | |
| 272 | 271 | /** |
| 273 | 272 | * Taxonomy parameters. |
| 274 | 273 | * |
| 275 | - * @return QueryArgs | |
| 274 | + * @return void | |
| 276 | 275 | */ |
| 277 | - private function taxParams() { | |
| 276 | + private function tax_params(): void { | |
| 278 | 277 | $categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : []; |
| 279 | 278 | $tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : []; |
| 280 | 279 | $attributes = isset( $this->meta['attributes'] ) ? array_filter( $this->meta['attributes'] ) : []; |
| 281 | - $taxQ = []; | |
| 282 | 280 | |
| 283 | - if ( is_array( $categories ) && ! empty( $categories ) ) { | |
| 284 | - $taxQ[] = [ | |
| 285 | - 'taxonomy' => 'product_cat', | |
| 286 | - 'field' => 'term_id', | |
| 287 | - 'terms' => $categories, | |
| 288 | - 'operator' => 'IN', | |
| 289 | - ]; | |
| 281 | + if ( ! empty( $categories ) ) { | |
| 282 | + $this->args['product_category_id'] = $categories; | |
| 290 | 283 | } |
| 291 | 284 | |
| 292 | - if ( ! empty( $tags ) && is_array( $tags ) ) { | |
| 293 | - $taxQ[] = [ | |
| 294 | - 'taxonomy' => 'product_tag', | |
| 295 | - 'field' => 'term_id', | |
| 296 | - 'terms' => $tags, | |
| 297 | - 'operator' => 'IN', | |
| 298 | - ]; | |
| 285 | + if ( ! empty( $tags ) ) { | |
| 286 | + $this->args['product_tag_id'] = $tags; | |
| 299 | 287 | } |
| 300 | 288 | |
| 301 | - if ( ! empty( $attributes ) && is_array( $attributes ) ) { | |
| 302 | - $attribute_tax = null; | |
| 303 | - | |
| 304 | - foreach ( $attributes as $atts ) { | |
| 305 | - $attribute_tax = get_term( $atts )->taxonomy; | |
| 306 | - | |
| 307 | - $taxQ[] = [ | |
| 308 | - 'taxonomy' => $attribute_tax, | |
| 309 | - 'field' => 'term_id', | |
| 310 | - 'terms' => [ $atts ], | |
| 311 | - 'operator' => 'IN', | |
| 312 | - ]; | |
| 313 | - } | |
| 289 | + if ( ! empty( $attributes ) ) { | |
| 290 | + $this->args['product_attribute_id'] = $attributes; | |
| 314 | 291 | } |
| 315 | - | |
| 316 | - if ( count( $taxQ ) >= 2 ) { | |
| 317 | - $taxQ['relation'] = $this->meta['relation']; | |
| 318 | - } | |
| 319 | - | |
| 320 | - if ( ! empty( $taxQ ) ) { | |
| 321 | - $this->args['tax_query'] = $taxQ; | |
| 322 | - } | |
| 323 | - | |
| 324 | - return $this; | |
| 325 | 292 | } |
| 326 | - | |
| 327 | 293 | } |