| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to build up query args. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Models; |
| 9 |
|
| 10 |
// Do not allow directly accessing this file. |
| 11 |
use RadiusTheme\SB\Helpers\Fns; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit( 'This script cannot be accessed directly.' ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Query Args Class |
| 19 |
*/ |
| 20 |
class QueryArgs { |
| 21 |
|
| 22 |
/** |
| 23 |
* Query Args. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $args = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Meta values. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $meta = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Method to build args |
| 38 |
* |
| 39 |
* @param array $meta Meta values. |
| 40 |
* @param string $type Query type. |
| 41 |
* @param bool $isCarousel Layout type. |
| 42 |
* |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function buildArgs( array $meta, string $type = 'product', bool $isCarousel = false ) { |
| 46 |
$this->meta = $meta; |
| 47 |
|
| 48 |
if ( 'product' === $type ) { |
| 49 |
$this |
| 50 |
->post_params() |
| 51 |
->order_params() |
| 52 |
->pagination_params( $isCarousel ) |
| 53 |
->tax_params(); |
| 54 |
|
| 55 |
} else { |
| 56 |
$this |
| 57 |
->get_tax_type() |
| 58 |
->cat_params() |
| 59 |
->cat_order_params(); |
| 60 |
} |
| 61 |
|
| 62 |
return apply_filters( 'rtsb/elements/' . $type . '_query_args', $this->args, $this->meta ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Post type. |
| 67 |
* |
| 68 |
* @return QueryArgs |
| 69 |
*/ |
| 70 |
private function get_tax_type() { |
| 71 |
$this->args['taxonomy'] = [ 'product_cat' ]; |
| 72 |
$this->args['post_status'] = 'publish'; |
| 73 |
$this->args['hierarchical'] = 1; |
| 74 |
|
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* WC Post parameters. |
| 80 |
* |
| 81 |
* @return QueryArgs |
| 82 |
*/ |
| 83 |
private function post_params() { |
| 84 |
$post_in = isset( $this->meta['post_in'] ) ? sanitize_text_field( implode( ',', $this->meta['post_in'] ) ) : null; |
| 85 |
$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; |
| 87 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 88 |
$offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0; |
| 89 |
|
| 90 |
if ( $post_in ) { |
| 91 |
$post_in = explode( ',', $post_in ); |
| 92 |
$this->args['include'] = $post_in; |
| 93 |
} |
| 94 |
|
| 95 |
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 |
| 98 |
} |
| 99 |
|
| 100 |
if ( $author_in ) { |
| 101 |
$this->args['author'] = $author_in; |
| 102 |
} |
| 103 |
|
| 104 |
$this->args['limit'] = $limit; |
| 105 |
|
| 106 |
if ( $offset ) { |
| 107 |
$this->args['offset'] = $offset; |
| 108 |
} |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Category parameters. |
| 115 |
* |
| 116 |
* @return QueryArgs |
| 117 |
*/ |
| 118 |
private function cat_params() { |
| 119 |
$cats_in = isset( $this->meta['include_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['include_cats'] ) ) : null; |
| 120 |
$cat_ids_in = isset( $this->meta['select_cat_ids'] ) ? esc_html( $this->meta['select_cat_ids'] ) : null; |
| 121 |
$cats_not_in = isset( $this->meta['exclude_cats'] ) ? sanitize_text_field( implode( ', ', $this->meta['exclude_cats'] ) ) : null; |
| 122 |
$limit = ( ( empty( $this->meta['cats_limit'] ) || '-1' === $this->meta['cats_limit'] ) ? 10000000 : absint( $this->meta['cats_limit'] ) ); |
| 123 |
|
| 124 |
if ( $cats_in ) { |
| 125 |
$cats_in = explode( ',', $cats_in ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( $cat_ids_in ) { |
| 129 |
$cats_in = explode( ',', $cat_ids_in ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! $this->meta['show_uncategorized'] ) { |
| 133 |
$cats_not_in .= ',' . get_option( 'default_product_cat', 0 ); |
| 134 |
} |
| 135 |
|
| 136 |
if ( $cats_not_in ) { |
| 137 |
$cats_not_in = array_filter( explode( ',', $cats_not_in ) ); |
| 138 |
$this->args['exclude'] = $cats_not_in; // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude |
| 139 |
} |
| 140 |
|
| 141 |
$this->args['number'] = $limit; |
| 142 |
$this->args['hide_empty'] = $this->meta['show_empty'] ? 0 : 1; |
| 143 |
|
| 144 |
if ( ! $this->meta['show_subcats'] ) { |
| 145 |
$this->args['parent'] = 0; |
| 146 |
} |
| 147 |
|
| 148 |
switch ( $this->meta['display_cat_by'] ) { |
| 149 |
case 'specific_parent': |
| 150 |
unset( $this->args['exclude'] ); |
| 151 |
unset( $this->args['parent'] ); |
| 152 |
|
| 153 |
if ( $this->meta['show_top_level_cats'] ) { |
| 154 |
$this->args['parent'] = absint( $this->meta['select_parent_cat'] ); |
| 155 |
} else { |
| 156 |
$this->args['child_of'] = absint( $this->meta['select_parent_cat'] ); |
| 157 |
} |
| 158 |
break; |
| 159 |
case 'cat_ids': |
| 160 |
case 'selection': |
| 161 |
unset( $this->args['exclude'] ); |
| 162 |
unset( $this->args['parent'] ); |
| 163 |
|
| 164 |
$this->args['include'] = $cats_in; |
| 165 |
break; |
| 166 |
default: |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
return $this; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* WC Order & Orderby parameters. |
| 175 |
* |
| 176 |
* @return QueryArgs |
| 177 |
*/ |
| 178 |
private function order_params() { |
| 179 |
$order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null ); |
| 180 |
$order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null ); |
| 181 |
|
| 182 |
if ( $order ) { |
| 183 |
$this->args['order'] = $order; |
| 184 |
} |
| 185 |
|
| 186 |
if ( $order_by ) { |
| 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 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return $this; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Order & Orderby parameters. |
| 204 |
* |
| 205 |
* @return QueryArgs |
| 206 |
*/ |
| 207 |
private function cat_order_params() { |
| 208 |
$order_by = ( isset( $this->meta['cats_order_by'] ) ? esc_html( $this->meta['cats_order_by'] ) : 'name' ); |
| 209 |
$order = ( isset( $this->meta['cats_order'] ) ? esc_html( $this->meta['cats_order'] ) : 'DESC' ); |
| 210 |
|
| 211 |
if ( $order ) { |
| 212 |
$this->args['order'] = $order; |
| 213 |
} |
| 214 |
|
| 215 |
if ( $order_by ) { |
| 216 |
$this->args['orderby'] = $order_by; |
| 217 |
} |
| 218 |
|
| 219 |
if ( 'menu_order' === $order_by ) { |
| 220 |
$this->args['menu_order'] = $order; |
| 221 |
} |
| 222 |
|
| 223 |
return $this; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Pagination parameters. |
| 228 |
* |
| 229 |
* @param bool $isCarousel Layout type. |
| 230 |
* |
| 231 |
* @return QueryArgs |
| 232 |
*/ |
| 233 |
private function pagination_params( $isCarousel ) { |
| 234 |
$pagination = ! empty( $this->meta['pagination'] ); |
| 235 |
$limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) ); |
| 236 |
|
| 237 |
if ( $pagination ) { |
| 238 |
unset( $this->args['offset'] ); |
| 239 |
|
| 240 |
$posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit ); |
| 241 |
|
| 242 |
if ( $posts_per_page > $limit ) { |
| 243 |
$posts_per_page = $limit; |
| 244 |
} |
| 245 |
|
| 246 |
$this->args['paginate'] = true; |
| 247 |
$this->args['limit'] = $posts_per_page; |
| 248 |
|
| 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' ); |
| 254 |
} |
| 255 |
if ( ! $paged ) { |
| 256 |
$paged = 1; |
| 257 |
} |
| 258 |
|
| 259 |
$offset = $posts_per_page * ( (int) $paged - 1 ); |
| 260 |
$this->args['page'] = $paged; |
| 261 |
|
| 262 |
if ( absint( $this->args['limit'] ) > $limit - $offset ) { |
| 263 |
$this->args['limit'] = $limit - $offset; |
| 264 |
$this->args['offset'] = $offset; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if ( $isCarousel ) { |
| 269 |
$this->args['limit'] = $limit; |
| 270 |
} |
| 271 |
|
| 272 |
return $this; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Taxonomy parameters. |
| 277 |
* |
| 278 |
* @return void |
| 279 |
*/ |
| 280 |
private function tax_params(): void { |
| 281 |
$categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : []; |
| 282 |
$brands = isset( $this->meta['brands'] ) ? array_filter( $this->meta['brands'] ) : []; |
| 283 |
$tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : []; |
| 284 |
$attributes = isset( $this->meta['attributes'] ) ? array_filter( $this->meta['attributes'] ) : []; |
| 285 |
|
| 286 |
if ( ! empty( $categories ) ) { |
| 287 |
$this->args['product_category_id'] = $categories; |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! empty( $brands ) ) { |
| 291 |
$this->args['product_brand_id'] = $brands; |
| 292 |
} |
| 293 |
|
| 294 |
if ( ! empty( $tags ) ) { |
| 295 |
$this->args['product_tag_id'] = $tags; |
| 296 |
} |
| 297 |
|
| 298 |
if ( ! empty( $attributes ) ) { |
| 299 |
$this->args['product_attribute_id'] = $attributes; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|