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