class-wc-shortcode-cart.php
3 months ago
class-wc-shortcode-checkout.php
3 months ago
class-wc-shortcode-my-account.php
1 month ago
class-wc-shortcode-order-tracking.php
3 years ago
class-wc-shortcode-products.php
3 months ago
class-wc-shortcode-products.php
714 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Products shortcode |
| 4 | * |
| 5 | * @package WooCommerce\Shortcodes |
| 6 | * @version 3.2.4 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\WooCommerce\Enums\CatalogVisibility; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Products shortcode class. |
| 17 | */ |
| 18 | class WC_Shortcode_Products { |
| 19 | |
| 20 | /** |
| 21 | * Shortcode type. |
| 22 | * |
| 23 | * @since 3.2.0 |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $type = 'products'; |
| 27 | |
| 28 | /** |
| 29 | * Attributes. |
| 30 | * |
| 31 | * @since 3.2.0 |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $attributes = array(); |
| 35 | |
| 36 | /** |
| 37 | * Query args. |
| 38 | * |
| 39 | * @since 3.2.0 |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $query_args = array(); |
| 43 | |
| 44 | /** |
| 45 | * Set custom visibility. |
| 46 | * |
| 47 | * @since 3.2.0 |
| 48 | * @var bool |
| 49 | */ |
| 50 | protected $custom_visibility = false; |
| 51 | |
| 52 | /** |
| 53 | * Initialize shortcode. |
| 54 | * |
| 55 | * @since 3.2.0 |
| 56 | * @param array $attributes Shortcode attributes. |
| 57 | * @param string $type Shortcode type. |
| 58 | */ |
| 59 | public function __construct( $attributes = array(), $type = 'products' ) { |
| 60 | $this->type = $type; |
| 61 | $this->attributes = $this->parse_attributes( $attributes ); |
| 62 | $this->query_args = $this->parse_query_args(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get shortcode attributes. |
| 67 | * |
| 68 | * @since 3.2.0 |
| 69 | * @return array |
| 70 | */ |
| 71 | public function get_attributes() { |
| 72 | return $this->attributes; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get query args. |
| 77 | * |
| 78 | * @since 3.2.0 |
| 79 | * @return array |
| 80 | */ |
| 81 | public function get_query_args() { |
| 82 | return $this->query_args; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get shortcode type. |
| 87 | * |
| 88 | * @since 3.2.0 |
| 89 | * @return string |
| 90 | */ |
| 91 | public function get_type() { |
| 92 | return $this->type; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get shortcode content. |
| 97 | * |
| 98 | * @since 3.2.0 |
| 99 | * @return string |
| 100 | */ |
| 101 | public function get_content() { |
| 102 | return $this->product_loop(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Parse attributes. |
| 107 | * |
| 108 | * @since 3.2.0 |
| 109 | * @param array $attributes Shortcode attributes. |
| 110 | * @return array |
| 111 | */ |
| 112 | protected function parse_attributes( $attributes ) { |
| 113 | $attributes = $this->parse_legacy_attributes( $attributes ); |
| 114 | |
| 115 | $attributes = shortcode_atts( |
| 116 | array( |
| 117 | 'limit' => '-1', // Results limit. |
| 118 | 'columns' => '', // Number of columns. |
| 119 | 'rows' => '', // Number of rows. If defined, limit will be ignored. |
| 120 | 'orderby' => '', // menu_order, title, date, rand, price, popularity, rating, or id. |
| 121 | 'order' => '', // ASC or DESC. |
| 122 | 'ids' => '', // Comma separated IDs. |
| 123 | 'skus' => '', // Comma separated SKUs. |
| 124 | 'category' => '', // Comma separated category slugs or ids. |
| 125 | 'cat_operator' => 'IN', // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'. |
| 126 | 'attribute' => '', // Single attribute slug. |
| 127 | 'terms' => '', // Comma separated term slugs or ids. |
| 128 | 'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'. |
| 129 | 'tag' => '', // Comma separated tag slugs. |
| 130 | 'tag_operator' => 'IN', // Operator to compare tags. Possible values are 'IN', 'NOT IN', 'AND'. |
| 131 | 'visibility' => CatalogVisibility::VISIBLE, // Product visibility setting. Possible values are 'visible', 'catalog', 'search', 'hidden'. |
| 132 | 'class' => '', // HTML class. |
| 133 | 'page' => 1, // Page for pagination. |
| 134 | 'paginate' => false, // Should results be paginated. |
| 135 | 'cache' => true, // Should shortcode output be cached. |
| 136 | ), |
| 137 | $attributes, |
| 138 | $this->type |
| 139 | ); |
| 140 | |
| 141 | if ( ! absint( $attributes['columns'] ) ) { |
| 142 | $attributes['columns'] = wc_get_default_products_per_row(); |
| 143 | } |
| 144 | |
| 145 | return $attributes; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Parse legacy attributes. |
| 150 | * |
| 151 | * @since 3.2.0 |
| 152 | * @param array $attributes Attributes. |
| 153 | * @return array |
| 154 | */ |
| 155 | protected function parse_legacy_attributes( $attributes ) { |
| 156 | $mapping = array( |
| 157 | 'per_page' => 'limit', |
| 158 | 'operator' => 'cat_operator', |
| 159 | 'filter' => 'terms', |
| 160 | ); |
| 161 | |
| 162 | foreach ( $mapping as $old => $new ) { |
| 163 | if ( isset( $attributes[ $old ] ) ) { |
| 164 | $attributes[ $new ] = $attributes[ $old ]; |
| 165 | unset( $attributes[ $old ] ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return $attributes; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Parse query args. |
| 174 | * |
| 175 | * @since 3.2.0 |
| 176 | * @return array |
| 177 | */ |
| 178 | protected function parse_query_args() { |
| 179 | $query_args = array( |
| 180 | 'post_type' => 'product', |
| 181 | 'post_status' => 'publish', |
| 182 | 'ignore_sticky_posts' => true, |
| 183 | 'no_found_rows' => false === wc_string_to_bool( $this->attributes['paginate'] ), |
| 184 | 'orderby' => empty( $_GET['orderby'] ) ? $this->attributes['orderby'] : wc_clean( wp_unslash( $_GET['orderby'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 185 | ); |
| 186 | |
| 187 | $orderby_value = explode( '-', $query_args['orderby'] ); |
| 188 | $orderby = esc_attr( $orderby_value[0] ); |
| 189 | $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : strtoupper( $this->attributes['order'] ); |
| 190 | $query_args['orderby'] = $orderby; |
| 191 | $query_args['order'] = $order; |
| 192 | |
| 193 | if ( wc_string_to_bool( $this->attributes['paginate'] ) ) { |
| 194 | $this->attributes['page'] = absint( empty( $_GET['product-page'] ) ? 1 : $_GET['product-page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 195 | } |
| 196 | |
| 197 | if ( ! empty( $this->attributes['rows'] ) ) { |
| 198 | $this->attributes['limit'] = $this->attributes['columns'] * $this->attributes['rows']; |
| 199 | } |
| 200 | |
| 201 | $ordering_args = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] ); |
| 202 | $query_args['orderby'] = $ordering_args['orderby']; |
| 203 | $query_args['order'] = $ordering_args['order']; |
| 204 | if ( $ordering_args['meta_key'] ) { |
| 205 | $query_args['meta_key'] = $ordering_args['meta_key']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 206 | } |
| 207 | $query_args['posts_per_page'] = intval( $this->attributes['limit'] ); |
| 208 | if ( 1 < $this->attributes['page'] ) { |
| 209 | $query_args['paged'] = absint( $this->attributes['page'] ); |
| 210 | } |
| 211 | $query_args['meta_query'] = WC()->query->get_meta_query(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 212 | $query_args['tax_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 213 | |
| 214 | // Visibility. |
| 215 | $this->set_visibility_query_args( $query_args ); |
| 216 | |
| 217 | // SKUs. |
| 218 | $this->set_skus_query_args( $query_args ); |
| 219 | |
| 220 | // IDs. |
| 221 | $this->set_ids_query_args( $query_args ); |
| 222 | |
| 223 | // Set specific types query args. |
| 224 | if ( method_exists( $this, "set_{$this->type}_query_args" ) ) { |
| 225 | $this->{"set_{$this->type}_query_args"}( $query_args ); |
| 226 | } |
| 227 | |
| 228 | // Attributes. |
| 229 | $this->set_attributes_query_args( $query_args ); |
| 230 | |
| 231 | // Categories. |
| 232 | $this->set_categories_query_args( $query_args ); |
| 233 | |
| 234 | // Tags. |
| 235 | $this->set_tags_query_args( $query_args ); |
| 236 | |
| 237 | $query_args = apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type ); |
| 238 | |
| 239 | // Always query only IDs. |
| 240 | $query_args['fields'] = 'ids'; |
| 241 | |
| 242 | return $query_args; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Set skus query args. |
| 247 | * |
| 248 | * @since 3.2.0 |
| 249 | * @param array $query_args Query args. |
| 250 | */ |
| 251 | protected function set_skus_query_args( &$query_args ) { |
| 252 | if ( ! empty( $this->attributes['skus'] ) ) { |
| 253 | $skus = array_map( 'trim', explode( ',', $this->attributes['skus'] ) ); |
| 254 | $query_args['meta_query'][] = array( |
| 255 | 'key' => '_sku', |
| 256 | 'value' => 1 === count( $skus ) ? $skus[0] : $skus, |
| 257 | 'compare' => 1 === count( $skus ) ? '=' : 'IN', |
| 258 | ); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Set ids query args. |
| 264 | * |
| 265 | * @since 3.2.0 |
| 266 | * @param array $query_args Query args. |
| 267 | */ |
| 268 | protected function set_ids_query_args( &$query_args ) { |
| 269 | if ( ! empty( $this->attributes['ids'] ) ) { |
| 270 | $ids = array_map( 'trim', explode( ',', $this->attributes['ids'] ) ); |
| 271 | |
| 272 | if ( 1 === count( $ids ) ) { |
| 273 | $query_args['p'] = $ids[0]; |
| 274 | } else { |
| 275 | $query_args['post__in'] = $ids; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Set attributes query args. |
| 282 | * |
| 283 | * @since 3.2.0 |
| 284 | * @param array $query_args Query args. |
| 285 | */ |
| 286 | protected function set_attributes_query_args( &$query_args ) { |
| 287 | if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['terms'] ) ) { |
| 288 | $taxonomy = strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] ); |
| 289 | $terms = $this->attributes['terms'] ? array_map( 'sanitize_title', explode( ',', $this->attributes['terms'] ) ) : array(); |
| 290 | $field = 'slug'; |
| 291 | |
| 292 | if ( $terms && is_numeric( $terms[0] ) ) { |
| 293 | $field = 'term_id'; |
| 294 | $terms = array_map( 'absint', $terms ); |
| 295 | // Check numeric slugs. |
| 296 | foreach ( $terms as $term ) { |
| 297 | $the_term = get_term_by( 'slug', $term, $taxonomy ); |
| 298 | if ( false !== $the_term ) { |
| 299 | $terms[] = $the_term->term_id; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // If no terms were specified get all products that are in the attribute taxonomy. |
| 305 | if ( ! $terms ) { |
| 306 | $terms = get_terms( |
| 307 | array( |
| 308 | 'taxonomy' => $taxonomy, |
| 309 | 'fields' => 'ids', |
| 310 | ) |
| 311 | ); |
| 312 | $field = 'term_id'; |
| 313 | } |
| 314 | |
| 315 | // We always need to search based on the slug as well, this is to accommodate numeric slugs. |
| 316 | $query_args['tax_query'][] = array( |
| 317 | 'taxonomy' => $taxonomy, |
| 318 | 'terms' => $terms, |
| 319 | 'field' => $field, |
| 320 | 'operator' => $this->attributes['terms_operator'], |
| 321 | ); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Set categories query args. |
| 327 | * |
| 328 | * @since 3.2.0 |
| 329 | * @param array $query_args Query args. |
| 330 | */ |
| 331 | protected function set_categories_query_args( &$query_args ) { |
| 332 | if ( ! empty( $this->attributes['category'] ) ) { |
| 333 | $categories = array_map( 'sanitize_title', explode( ',', $this->attributes['category'] ) ); |
| 334 | $field = 'slug'; |
| 335 | |
| 336 | if ( is_numeric( $categories[0] ) ) { |
| 337 | $field = 'term_id'; |
| 338 | $categories = array_map( 'absint', $categories ); |
| 339 | // Check numeric slugs. |
| 340 | foreach ( $categories as $cat ) { |
| 341 | $the_cat = get_term_by( 'slug', $cat, 'product_cat' ); |
| 342 | if ( false !== $the_cat ) { |
| 343 | $categories[] = $the_cat->term_id; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | $query_args['tax_query'][] = array( |
| 349 | 'taxonomy' => 'product_cat', |
| 350 | 'terms' => $categories, |
| 351 | 'field' => $field, |
| 352 | 'operator' => $this->attributes['cat_operator'], |
| 353 | |
| 354 | /* |
| 355 | * When cat_operator is AND, the children categories should be excluded, |
| 356 | * as only products belonging to all the children categories would be selected. |
| 357 | */ |
| 358 | 'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true, |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Set tags query args. |
| 365 | * |
| 366 | * @since 3.3.0 |
| 367 | * @param array $query_args Query args. |
| 368 | */ |
| 369 | protected function set_tags_query_args( &$query_args ) { |
| 370 | if ( ! empty( $this->attributes['tag'] ) ) { |
| 371 | $query_args['tax_query'][] = array( |
| 372 | 'taxonomy' => 'product_tag', |
| 373 | 'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['tag'] ) ), |
| 374 | 'field' => 'slug', |
| 375 | 'operator' => $this->attributes['tag_operator'], |
| 376 | ); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Set sale products query args. |
| 382 | * |
| 383 | * @since 3.2.0 |
| 384 | * @param array $query_args Query args. |
| 385 | */ |
| 386 | protected function set_sale_products_query_args( &$query_args ) { |
| 387 | $query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Set best selling products query args. |
| 392 | * |
| 393 | * @since 3.2.0 |
| 394 | * @param array $query_args Query args. |
| 395 | */ |
| 396 | protected function set_best_selling_products_query_args( &$query_args ) { |
| 397 | $query_args['meta_key'] = 'total_sales'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 398 | $query_args['order'] = 'DESC'; |
| 399 | $query_args['orderby'] = 'meta_value_num'; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Set top rated products query args. |
| 404 | * |
| 405 | * @since 3.6.5 |
| 406 | * @param array $query_args Query args. |
| 407 | */ |
| 408 | protected function set_top_rated_products_query_args( &$query_args ) { |
| 409 | $query_args['meta_key'] = '_wc_average_rating'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 410 | $query_args['order'] = 'DESC'; |
| 411 | $query_args['orderby'] = 'meta_value_num'; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Set visibility as hidden. |
| 416 | * |
| 417 | * @since 3.2.0 |
| 418 | * @param array $query_args Query args. |
| 419 | */ |
| 420 | protected function set_visibility_hidden_query_args( &$query_args ) { |
| 421 | $this->custom_visibility = true; |
| 422 | $query_args['tax_query'][] = array( |
| 423 | 'taxonomy' => 'product_visibility', |
| 424 | 'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ), |
| 425 | 'field' => 'name', |
| 426 | 'operator' => 'AND', |
| 427 | 'include_children' => false, |
| 428 | ); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Set visibility as catalog. |
| 433 | * |
| 434 | * @since 3.2.0 |
| 435 | * @param array $query_args Query args. |
| 436 | */ |
| 437 | protected function set_visibility_catalog_query_args( &$query_args ) { |
| 438 | $this->custom_visibility = true; |
| 439 | $query_args['tax_query'][] = array( |
| 440 | 'taxonomy' => 'product_visibility', |
| 441 | 'terms' => 'exclude-from-search', |
| 442 | 'field' => 'name', |
| 443 | 'operator' => 'IN', |
| 444 | 'include_children' => false, |
| 445 | ); |
| 446 | $query_args['tax_query'][] = array( |
| 447 | 'taxonomy' => 'product_visibility', |
| 448 | 'terms' => 'exclude-from-catalog', |
| 449 | 'field' => 'name', |
| 450 | 'operator' => 'NOT IN', |
| 451 | 'include_children' => false, |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Set visibility as search. |
| 457 | * |
| 458 | * @since 3.2.0 |
| 459 | * @param array $query_args Query args. |
| 460 | */ |
| 461 | protected function set_visibility_search_query_args( &$query_args ) { |
| 462 | $this->custom_visibility = true; |
| 463 | $query_args['tax_query'][] = array( |
| 464 | 'taxonomy' => 'product_visibility', |
| 465 | 'terms' => 'exclude-from-catalog', |
| 466 | 'field' => 'name', |
| 467 | 'operator' => 'IN', |
| 468 | 'include_children' => false, |
| 469 | ); |
| 470 | $query_args['tax_query'][] = array( |
| 471 | 'taxonomy' => 'product_visibility', |
| 472 | 'terms' => 'exclude-from-search', |
| 473 | 'field' => 'name', |
| 474 | 'operator' => 'NOT IN', |
| 475 | 'include_children' => false, |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Set visibility as featured. |
| 481 | * |
| 482 | * @since 3.2.0 |
| 483 | * @param array $query_args Query args. |
| 484 | */ |
| 485 | protected function set_visibility_featured_query_args( &$query_args ) { |
| 486 | $query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 487 | |
| 488 | $query_args['tax_query'][] = array( |
| 489 | 'taxonomy' => 'product_visibility', |
| 490 | 'terms' => 'featured', |
| 491 | 'field' => 'name', |
| 492 | 'operator' => 'IN', |
| 493 | 'include_children' => false, |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Set visibility query args. |
| 499 | * |
| 500 | * @since 3.2.0 |
| 501 | * @param array $query_args Query args. |
| 502 | */ |
| 503 | protected function set_visibility_query_args( &$query_args ) { |
| 504 | if ( method_exists( $this, 'set_visibility_' . $this->attributes['visibility'] . '_query_args' ) ) { |
| 505 | $this->{'set_visibility_' . $this->attributes['visibility'] . '_query_args'}( $query_args ); |
| 506 | } else { |
| 507 | $query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Set product as visible when querying for hidden products. |
| 513 | * |
| 514 | * @since 3.2.0 |
| 515 | * @param bool $visibility Product visibility. |
| 516 | * @return bool |
| 517 | */ |
| 518 | public function set_product_as_visible( $visibility ) { |
| 519 | return $this->custom_visibility ? true : $visibility; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Get wrapper classes. |
| 524 | * |
| 525 | * @since 3.2.0 |
| 526 | * @param int $columns Number of columns. |
| 527 | * @return array |
| 528 | */ |
| 529 | protected function get_wrapper_classes( $columns ) { |
| 530 | $classes = array( 'woocommerce' ); |
| 531 | |
| 532 | if ( 'product' !== $this->type ) { |
| 533 | $classes[] = 'columns-' . $columns; |
| 534 | } |
| 535 | |
| 536 | $classes[] = $this->attributes['class']; |
| 537 | |
| 538 | return $classes; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Generate and return the transient name for this shortcode based on the query args. |
| 543 | * |
| 544 | * @since 3.3.0 |
| 545 | * @return string |
| 546 | */ |
| 547 | protected function get_transient_name() { |
| 548 | $transient_name = 'wc_product_loop_' . md5( wp_json_encode( $this->query_args ) . $this->type ); |
| 549 | |
| 550 | if ( 'rand' === $this->query_args['orderby'] ) { |
| 551 | // When using rand, we'll cache a number of random queries and pull those to avoid querying rand on each page load. |
| 552 | $rand_index = wp_rand( 0, max( 1, absint( apply_filters( 'woocommerce_product_query_max_rand_cache_count', 5 ) ) ) ); |
| 553 | $transient_name .= $rand_index; |
| 554 | } |
| 555 | |
| 556 | return $transient_name; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Run the query and return an array of data, including queried ids and pagination information. |
| 561 | * |
| 562 | * @since 3.3.0 |
| 563 | * @return object Object with the following props; ids, per_page, found_posts, max_num_pages, current_page |
| 564 | */ |
| 565 | protected function get_query_results() { |
| 566 | $transient_name = $this->get_transient_name(); |
| 567 | $transient_version = WC_Cache_Helper::get_transient_version( 'product_query' ); |
| 568 | $cache = wc_string_to_bool( $this->attributes['cache'] ) === true; |
| 569 | $transient_value = $cache ? get_transient( $transient_name ) : false; |
| 570 | |
| 571 | if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) { |
| 572 | $results = $transient_value['value']; |
| 573 | } else { |
| 574 | $query = new WP_Query( $this->query_args ); |
| 575 | |
| 576 | $paginated = ! $query->get( 'no_found_rows' ); |
| 577 | |
| 578 | $results = (object) array( |
| 579 | 'ids' => wp_parse_id_list( $query->posts ), |
| 580 | 'total' => $paginated ? (int) $query->found_posts : count( $query->posts ), |
| 581 | 'total_pages' => $paginated ? (int) $query->max_num_pages : 1, |
| 582 | 'per_page' => (int) $query->get( 'posts_per_page' ), |
| 583 | 'current_page' => $paginated ? (int) max( 1, $query->get( 'paged', 1 ) ) : 1, |
| 584 | ); |
| 585 | |
| 586 | if ( $cache ) { |
| 587 | $transient_value = array( |
| 588 | 'version' => $transient_version, |
| 589 | 'value' => $results, |
| 590 | ); |
| 591 | set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 ); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // Remove ordering query arguments which may have been added by get_catalog_ordering_args. |
| 596 | WC()->query->remove_ordering_args(); |
| 597 | |
| 598 | /** |
| 599 | * Filter shortcode products query results. |
| 600 | * |
| 601 | * @since 4.0.0 |
| 602 | * @param stdClass $results Query results. |
| 603 | * @param WC_Shortcode_Products $this WC_Shortcode_Products instance. |
| 604 | */ |
| 605 | return apply_filters( 'woocommerce_shortcode_products_query_results', $results, $this ); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Loop over found products. |
| 610 | * |
| 611 | * @since 3.2.0 |
| 612 | * @return string |
| 613 | */ |
| 614 | protected function product_loop() { |
| 615 | $columns = absint( $this->attributes['columns'] ); |
| 616 | $classes = $this->get_wrapper_classes( $columns ); |
| 617 | $products = $this->get_query_results(); |
| 618 | |
| 619 | ob_start(); |
| 620 | |
| 621 | if ( $products && $products->ids ) { |
| 622 | // Prime caches to reduce future queries. |
| 623 | _prime_post_caches( $products->ids ); |
| 624 | |
| 625 | // Setup the loop. |
| 626 | wc_setup_loop( |
| 627 | array( |
| 628 | 'columns' => $columns, |
| 629 | 'name' => $this->type, |
| 630 | 'is_shortcode' => true, |
| 631 | 'is_search' => false, |
| 632 | 'is_paginated' => wc_string_to_bool( $this->attributes['paginate'] ), |
| 633 | 'total' => $products->total, |
| 634 | 'total_pages' => $products->total_pages, |
| 635 | 'per_page' => $products->per_page, |
| 636 | 'current_page' => $products->current_page, |
| 637 | ) |
| 638 | ); |
| 639 | |
| 640 | $original_post = $GLOBALS['post']; |
| 641 | |
| 642 | do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes ); |
| 643 | |
| 644 | if ( wc_string_to_bool( $this->attributes['paginate'] ) ) { |
| 645 | /** |
| 646 | * Fire the standard shop hooks when paginating so we can display result counts etc. |
| 647 | * If the pagination is not enabled, this hook will not be fired. |
| 648 | * |
| 649 | * @since 3.3.1 |
| 650 | */ |
| 651 | do_action( 'woocommerce_before_shop_loop' ); |
| 652 | } |
| 653 | |
| 654 | woocommerce_product_loop_start(); |
| 655 | |
| 656 | if ( wc_get_loop_prop( 'total' ) ) { |
| 657 | foreach ( $products->ids as $product_id ) { |
| 658 | $GLOBALS['post'] = get_post( $product_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 659 | setup_postdata( $GLOBALS['post'] ); |
| 660 | |
| 661 | // Set custom product visibility when querying hidden products. |
| 662 | add_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) ); |
| 663 | |
| 664 | // Render product template. |
| 665 | wc_get_template_part( 'content', 'product' ); |
| 666 | |
| 667 | // Restore product visibility. |
| 668 | remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) ); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | $GLOBALS['post'] = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 673 | woocommerce_product_loop_end(); |
| 674 | |
| 675 | if ( wc_string_to_bool( $this->attributes['paginate'] ) ) { |
| 676 | /** |
| 677 | * Fire the standard shop hooks when paginating so we can display the pagination. |
| 678 | * If the pagination is not enabled, this hook will not be fired. |
| 679 | * |
| 680 | * @since 3.3.1 |
| 681 | */ |
| 682 | do_action( 'woocommerce_after_shop_loop' ); |
| 683 | } |
| 684 | |
| 685 | do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes ); |
| 686 | |
| 687 | wp_reset_postdata(); |
| 688 | wc_reset_loop(); |
| 689 | } else { |
| 690 | do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes ); |
| 691 | } |
| 692 | |
| 693 | return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>'; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Order by rating. |
| 698 | * |
| 699 | * @since 3.2.0 |
| 700 | * @param array $args Query args. |
| 701 | * @return array |
| 702 | */ |
| 703 | public static function order_by_rating_post_clauses( $args ) { |
| 704 | global $wpdb; |
| 705 | |
| 706 | $args['where'] .= " AND $wpdb->commentmeta.meta_key = 'rating' "; |
| 707 | $args['join'] .= "LEFT JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)"; |
| 708 | $args['orderby'] = "$wpdb->commentmeta.meta_value DESC"; |
| 709 | $args['groupby'] = "$wpdb->posts.ID"; |
| 710 | |
| 711 | return $args; |
| 712 | } |
| 713 | } |
| 714 |