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