2017-08-24 18:29:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Products shortcode
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Shortcodes
|
2017-11-16 17:14:36 +00:00
|
|
|
* @version 3.2.4
|
2017-08-24 18:29:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Products shortcode class.
|
|
|
|
*/
|
|
|
|
class WC_Shortcode_Products {
|
|
|
|
|
|
|
|
/**
|
2017-08-25 01:22:42 +00:00
|
|
|
* Shortcode type.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
protected $type = 'products';
|
2017-08-24 18:29:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $query_args = array();
|
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
/**
|
|
|
|
* Set custom visibility.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $custom_visibility = false;
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
|
|
|
* Initialize shortcode.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
2017-08-30 22:30:46 +00:00
|
|
|
* @param array $attributes Shortcode attributes.
|
|
|
|
* @param string $type Shortcode type.
|
2017-08-24 18:29:30 +00:00
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
public function __construct( $attributes = array(), $type = 'products' ) {
|
|
|
|
$this->type = $type;
|
2017-08-25 00:24:05 +00:00
|
|
|
$this->attributes = $this->parse_attributes( $attributes );
|
|
|
|
$this->query_args = $this->parse_query_args();
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-24 23:11:18 +00:00
|
|
|
* Get shortcode attributes.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_attributes() {
|
|
|
|
return $this->attributes;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-24 23:11:18 +00:00
|
|
|
* Get query args.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_query_args() {
|
|
|
|
return $this->query_args;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 23:20:14 +00:00
|
|
|
/**
|
2017-08-25 01:22:42 +00:00
|
|
|
* Get shortcode type.
|
2017-08-24 23:20:14 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
2021-03-21 03:08:36 +00:00
|
|
|
* @return string
|
2017-08-24 23:20:14 +00:00
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
public function get_type() {
|
|
|
|
return $this->type;
|
2017-08-24 23:20:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
2017-08-24 22:33:14 +00:00
|
|
|
* Get shortcode content.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_content() {
|
2017-08-24 18:30:44 +00:00
|
|
|
return $this->product_loop();
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 00:24:05 +00:00
|
|
|
/**
|
|
|
|
* Parse attributes.
|
|
|
|
*
|
2017-08-28 20:45:56 +00:00
|
|
|
* @since 3.2.0
|
2017-08-25 00:24:05 +00:00
|
|
|
* @param array $attributes Shortcode attributes.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_attributes( $attributes ) {
|
2017-08-28 20:45:56 +00:00
|
|
|
$attributes = $this->parse_legacy_attributes( $attributes );
|
|
|
|
|
2018-03-09 15:55:37 +00:00
|
|
|
$attributes = shortcode_atts(
|
|
|
|
array(
|
|
|
|
'limit' => '-1', // Results limit.
|
2018-03-19 13:05:43 +00:00
|
|
|
'columns' => '', // Number of columns.
|
2018-03-09 15:55:37 +00:00
|
|
|
'rows' => '', // Number of rows. If defined, limit will be ignored.
|
2019-12-06 14:18:13 +00:00
|
|
|
'orderby' => '', // menu_order, title, date, rand, price, popularity, rating, or id.
|
2019-12-06 00:23:40 +00:00
|
|
|
'order' => '', // ASC or DESC.
|
2018-03-09 15:55:37 +00:00
|
|
|
'ids' => '', // Comma separated IDs.
|
|
|
|
'skus' => '', // Comma separated SKUs.
|
|
|
|
'category' => '', // Comma separated category slugs or ids.
|
|
|
|
'cat_operator' => 'IN', // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'.
|
|
|
|
'attribute' => '', // Single attribute slug.
|
|
|
|
'terms' => '', // Comma separated term slugs or ids.
|
|
|
|
'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
|
|
|
|
'tag' => '', // Comma separated tag slugs.
|
2019-07-09 17:06:35 +00:00
|
|
|
'tag_operator' => 'IN', // Operator to compare tags. Possible values are 'IN', 'NOT IN', 'AND'.
|
2019-05-20 20:04:24 +00:00
|
|
|
'visibility' => 'visible', // Product visibility setting. Possible values are 'visible', 'catalog', 'search', 'hidden'.
|
2018-03-09 15:55:37 +00:00
|
|
|
'class' => '', // HTML class.
|
|
|
|
'page' => 1, // Page for pagination.
|
|
|
|
'paginate' => false, // Should results be paginated.
|
|
|
|
'cache' => true, // Should shortcode output be cached.
|
2019-01-21 12:31:48 +00:00
|
|
|
),
|
|
|
|
$attributes,
|
|
|
|
$this->type
|
2018-03-09 15:55:37 +00:00
|
|
|
);
|
2018-01-26 15:15:06 +00:00
|
|
|
|
|
|
|
if ( ! absint( $attributes['columns'] ) ) {
|
2018-03-19 13:05:43 +00:00
|
|
|
$attributes['columns'] = wc_get_default_products_per_row();
|
2018-01-26 15:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes;
|
2017-08-25 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 20:45:56 +00:00
|
|
|
/**
|
|
|
|
* Parse legacy attributes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
2017-08-30 21:18:15 +00:00
|
|
|
* @param array $attributes Attributes.
|
2017-08-28 20:45:56 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_legacy_attributes( $attributes ) {
|
|
|
|
$mapping = array(
|
|
|
|
'per_page' => 'limit',
|
|
|
|
'operator' => 'cat_operator',
|
|
|
|
'filter' => 'terms',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $mapping as $old => $new ) {
|
|
|
|
if ( isset( $attributes[ $old ] ) ) {
|
|
|
|
$attributes[ $new ] = $attributes[ $old ];
|
|
|
|
unset( $attributes[ $old ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
|
|
|
* Parse query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_query_args() {
|
2017-08-25 00:24:51 +00:00
|
|
|
$query_args = array(
|
2017-08-24 18:29:30 +00:00
|
|
|
'post_type' => 'product',
|
|
|
|
'post_status' => 'publish',
|
2017-08-25 01:16:42 +00:00
|
|
|
'ignore_sticky_posts' => true,
|
2017-11-09 13:43:15 +00:00
|
|
|
'no_found_rows' => false === wc_string_to_bool( $this->attributes['paginate'] ),
|
2019-11-21 17:19:56 +00:00
|
|
|
'orderby' => empty( $_GET['orderby'] ) ? $this->attributes['orderby'] : wc_clean( wp_unslash( $_GET['orderby'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2017-08-24 18:29:30 +00:00
|
|
|
);
|
|
|
|
|
2018-08-08 11:44:47 +00:00
|
|
|
$orderby_value = explode( '-', $query_args['orderby'] );
|
|
|
|
$orderby = esc_attr( $orderby_value[0] );
|
|
|
|
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : strtoupper( $this->attributes['order'] );
|
|
|
|
$query_args['orderby'] = $orderby;
|
|
|
|
$query_args['order'] = $order;
|
|
|
|
|
2017-11-09 13:44:24 +00:00
|
|
|
if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
|
2019-12-06 14:18:13 +00:00
|
|
|
$this->attributes['page'] = absint( empty( $_GET['product-page'] ) ? 1 : $_GET['product-page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2017-11-09 13:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 16:45:48 +00:00
|
|
|
if ( ! empty( $this->attributes['rows'] ) ) {
|
|
|
|
$this->attributes['limit'] = $this->attributes['columns'] * $this->attributes['rows'];
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:06:41 +00:00
|
|
|
$ordering_args = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] );
|
|
|
|
$query_args['orderby'] = $ordering_args['orderby'];
|
|
|
|
$query_args['order'] = $ordering_args['order'];
|
2017-11-10 17:45:01 +00:00
|
|
|
if ( $ordering_args['meta_key'] ) {
|
2019-05-20 20:06:41 +00:00
|
|
|
$query_args['meta_key'] = $ordering_args['meta_key']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
|
2017-11-10 17:45:01 +00:00
|
|
|
}
|
|
|
|
$query_args['posts_per_page'] = intval( $this->attributes['limit'] );
|
|
|
|
if ( 1 < $this->attributes['page'] ) {
|
2019-05-20 20:06:41 +00:00
|
|
|
$query_args['paged'] = absint( $this->attributes['page'] );
|
2017-11-10 17:45:01 +00:00
|
|
|
}
|
2019-05-20 20:06:41 +00:00
|
|
|
$query_args['meta_query'] = WC()->query->get_meta_query(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
|
|
|
$query_args['tax_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
|
2017-08-24 22:17:18 +00:00
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
// Visibility.
|
|
|
|
$this->set_visibility_query_args( $query_args );
|
|
|
|
|
2017-08-25 00:24:05 +00:00
|
|
|
// SKUs.
|
2017-08-25 22:39:43 +00:00
|
|
|
$this->set_skus_query_args( $query_args );
|
|
|
|
|
|
|
|
// IDs.
|
|
|
|
$this->set_ids_query_args( $query_args );
|
|
|
|
|
|
|
|
// Set specific types query args.
|
|
|
|
if ( method_exists( $this, "set_{$this->type}_query_args" ) ) {
|
|
|
|
$this->{"set_{$this->type}_query_args"}( $query_args );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes.
|
|
|
|
$this->set_attributes_query_args( $query_args );
|
|
|
|
|
|
|
|
// Categories.
|
|
|
|
$this->set_categories_query_args( $query_args );
|
|
|
|
|
2017-11-30 21:43:32 +00:00
|
|
|
// Tags.
|
|
|
|
$this->set_tags_query_args( $query_args );
|
|
|
|
|
2017-11-16 17:14:36 +00:00
|
|
|
$query_args = apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );
|
|
|
|
|
|
|
|
// Always query only IDs.
|
|
|
|
$query_args['fields'] = 'ids';
|
|
|
|
|
2017-12-05 16:42:50 +00:00
|
|
|
return $query_args;
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set skus query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_skus_query_args( &$query_args ) {
|
2017-08-24 18:29:30 +00:00
|
|
|
if ( ! empty( $this->attributes['skus'] ) ) {
|
2017-11-23 18:43:54 +00:00
|
|
|
$skus = array_map( 'trim', explode( ',', $this->attributes['skus'] ) );
|
2017-08-25 00:24:51 +00:00
|
|
|
$query_args['meta_query'][] = array(
|
2017-08-24 18:29:30 +00:00
|
|
|
'key' => '_sku',
|
2017-08-25 01:16:42 +00:00
|
|
|
'value' => 1 === count( $skus ) ? $skus[0] : $skus,
|
|
|
|
'compare' => 1 === count( $skus ) ? '=' : 'IN',
|
2017-08-24 18:29:30 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set ids query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_ids_query_args( &$query_args ) {
|
2017-08-24 18:29:30 +00:00
|
|
|
if ( ! empty( $this->attributes['ids'] ) ) {
|
2017-08-25 01:16:42 +00:00
|
|
|
$ids = array_map( 'trim', explode( ',', $this->attributes['ids'] ) );
|
|
|
|
|
|
|
|
if ( 1 === count( $ids ) ) {
|
|
|
|
$query_args['p'] = $ids[0];
|
|
|
|
} else {
|
|
|
|
$query_args['post__in'] = $ids;
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set attributes query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_attributes_query_args( &$query_args ) {
|
2017-08-29 21:29:29 +00:00
|
|
|
if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['terms'] ) ) {
|
2018-03-01 18:05:22 +00:00
|
|
|
$taxonomy = strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] );
|
2018-03-09 15:55:37 +00:00
|
|
|
$terms = $this->attributes['terms'] ? array_map( 'sanitize_title', explode( ',', $this->attributes['terms'] ) ) : array();
|
|
|
|
$field = 'slug';
|
2018-03-01 18:05:22 +00:00
|
|
|
|
|
|
|
if ( $terms && is_numeric( $terms[0] ) ) {
|
2018-10-15 11:22:12 +00:00
|
|
|
$field = 'term_id';
|
2018-03-01 18:05:22 +00:00
|
|
|
$terms = array_map( 'absint', $terms );
|
2018-10-15 11:22:12 +00:00
|
|
|
// Check numeric slugs.
|
|
|
|
foreach ( $terms as $term ) {
|
|
|
|
$the_term = get_term_by( 'slug', $term, $taxonomy );
|
|
|
|
if ( false !== $the_term ) {
|
|
|
|
$terms[] = $the_term->term_id;
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 18:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no terms were specified get all products that are in the attribute taxonomy.
|
|
|
|
if ( ! $terms ) {
|
2018-03-09 15:55:37 +00:00
|
|
|
$terms = get_terms(
|
|
|
|
array(
|
|
|
|
'taxonomy' => $taxonomy,
|
|
|
|
'fields' => 'ids',
|
|
|
|
)
|
|
|
|
);
|
2018-10-15 11:22:12 +00:00
|
|
|
$field = 'term_id';
|
2018-03-01 18:05:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 10:49:56 +00:00
|
|
|
// We always need to search based on the slug as well, this is to accommodate numeric slugs.
|
2017-08-25 22:07:10 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
2018-03-01 18:05:22 +00:00
|
|
|
'taxonomy' => $taxonomy,
|
|
|
|
'terms' => $terms,
|
|
|
|
'field' => $field,
|
2017-08-28 20:27:35 +00:00
|
|
|
'operator' => $this->attributes['terms_operator'],
|
2017-08-25 22:07:10 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-25 22:07:10 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set categories query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_categories_query_args( &$query_args ) {
|
2017-08-25 14:46:47 +00:00
|
|
|
if ( ! empty( $this->attributes['category'] ) ) {
|
2018-03-01 18:05:22 +00:00
|
|
|
$categories = array_map( 'sanitize_title', explode( ',', $this->attributes['category'] ) );
|
2018-03-09 15:55:37 +00:00
|
|
|
$field = 'slug';
|
2018-03-01 18:05:22 +00:00
|
|
|
|
|
|
|
if ( is_numeric( $categories[0] ) ) {
|
2019-05-20 20:04:24 +00:00
|
|
|
$field = 'term_id';
|
2018-03-01 18:05:22 +00:00
|
|
|
$categories = array_map( 'absint', $categories );
|
2018-10-15 11:22:12 +00:00
|
|
|
// Check numeric slugs.
|
|
|
|
foreach ( $categories as $cat ) {
|
|
|
|
$the_cat = get_term_by( 'slug', $cat, 'product_cat' );
|
|
|
|
if ( false !== $the_cat ) {
|
|
|
|
$categories[] = $the_cat->term_id;
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 18:05:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 14:46:47 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
2018-05-24 15:12:48 +00:00
|
|
|
'taxonomy' => 'product_cat',
|
|
|
|
'terms' => $categories,
|
|
|
|
'field' => $field,
|
|
|
|
'operator' => $this->attributes['cat_operator'],
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When cat_operator is AND, the children categories should be excluded,
|
|
|
|
* as only products belonging to all the children categories would be selected.
|
|
|
|
*/
|
|
|
|
'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true,
|
2017-08-25 14:46:47 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-25 14:46:47 +00:00
|
|
|
|
2017-11-30 21:43:32 +00:00
|
|
|
/**
|
|
|
|
* Set tags query args.
|
|
|
|
*
|
|
|
|
* @since 3.3.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_tags_query_args( &$query_args ) {
|
|
|
|
if ( ! empty( $this->attributes['tag'] ) ) {
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_tag',
|
|
|
|
'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['tag'] ) ),
|
|
|
|
'field' => 'slug',
|
2019-07-09 17:06:35 +00:00
|
|
|
'operator' => $this->attributes['tag_operator'],
|
2017-11-30 21:43:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set sale products query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_sale_products_query_args( &$query_args ) {
|
|
|
|
$query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set best selling products query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_best_selling_products_query_args( &$query_args ) {
|
2019-05-20 20:04:24 +00:00
|
|
|
$query_args['meta_key'] = 'total_sales'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
|
2017-08-25 22:39:43 +00:00
|
|
|
$query_args['order'] = 'DESC';
|
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
|
|
}
|
2019-05-20 19:57:14 +00:00
|
|
|
|
2019-05-18 15:34:13 +00:00
|
|
|
/**
|
|
|
|
* Set top rated products query args.
|
|
|
|
*
|
2019-07-02 15:51:36 +00:00
|
|
|
* @since 3.6.5
|
2019-05-18 15:34:13 +00:00
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_top_rated_products_query_args( &$query_args ) {
|
2019-05-20 20:04:24 +00:00
|
|
|
$query_args['meta_key'] = '_wc_average_rating'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
|
2019-05-18 15:34:13 +00:00
|
|
|
$query_args['order'] = 'DESC';
|
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
|
2017-08-30 21:18:15 +00:00
|
|
|
/**
|
|
|
|
* Set visibility as hidden.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_visibility_hidden_query_args( &$query_args ) {
|
2017-11-23 18:43:54 +00:00
|
|
|
$this->custom_visibility = true;
|
2017-08-30 21:18:15 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'AND',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set visibility as catalog.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_visibility_catalog_query_args( &$query_args ) {
|
2017-11-23 18:43:54 +00:00
|
|
|
$this->custom_visibility = true;
|
2017-08-30 21:18:15 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-search',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-catalog',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set visibility as search.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_visibility_search_query_args( &$query_args ) {
|
2017-11-23 18:43:54 +00:00
|
|
|
$this->custom_visibility = true;
|
2017-08-30 21:18:15 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-catalog',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-search',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set visibility as featured.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_visibility_featured_query_args( &$query_args ) {
|
2019-05-20 20:06:41 +00:00
|
|
|
$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
|
2017-08-30 21:18:15 +00:00
|
|
|
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'featured',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
2017-08-29 21:29:29 +00:00
|
|
|
* Set visibility query args.
|
2017-08-25 22:39:43 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
2017-08-29 21:29:29 +00:00
|
|
|
protected function set_visibility_query_args( &$query_args ) {
|
2017-08-30 21:18:15 +00:00
|
|
|
if ( method_exists( $this, 'set_visibility_' . $this->attributes['visibility'] . '_query_args' ) ) {
|
|
|
|
$this->{'set_visibility_' . $this->attributes['visibility'] . '_query_args'}( $query_args );
|
|
|
|
} else {
|
2019-05-20 20:06:41 +00:00
|
|
|
$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
|
2017-08-29 21:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-31 12:27:14 +00:00
|
|
|
* Set product as visible when querying for hidden products.
|
2017-08-29 21:29:29 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param bool $visibility Product visibility.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function set_product_as_visible( $visibility ) {
|
2017-08-30 22:30:46 +00:00
|
|
|
return $this->custom_visibility ? true : $visibility;
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get wrapper classes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
2020-08-21 17:55:28 +00:00
|
|
|
* @param int $columns Number of columns.
|
2017-08-25 22:39:43 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_wrapper_classes( $columns ) {
|
|
|
|
$classes = array( 'woocommerce' );
|
|
|
|
|
|
|
|
if ( 'product' !== $this->type ) {
|
|
|
|
$classes[] = 'columns-' . $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes[] = $this->attributes['class'];
|
|
|
|
|
|
|
|
return $classes;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-31 12:58:36 +00:00
|
|
|
* Generate and return the transient name for this shortcode based on the query args.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
2017-10-31 12:58:36 +00:00
|
|
|
* @since 3.3.0
|
|
|
|
* @return string
|
2017-08-24 18:29:30 +00:00
|
|
|
*/
|
2017-10-31 12:58:36 +00:00
|
|
|
protected function get_transient_name() {
|
2019-01-21 12:31:48 +00:00
|
|
|
$transient_name = 'wc_product_loop_' . md5( wp_json_encode( $this->query_args ) . $this->type );
|
2017-09-06 13:36:14 +00:00
|
|
|
|
|
|
|
if ( 'rand' === $this->query_args['orderby'] ) {
|
|
|
|
// When using rand, we'll cache a number of random queries and pull those to avoid querying rand on each page load.
|
2019-05-20 20:04:24 +00:00
|
|
|
$rand_index = wp_rand( 0, max( 1, absint( apply_filters( 'woocommerce_product_query_max_rand_cache_count', 5 ) ) ) );
|
2017-09-06 13:36:14 +00:00
|
|
|
$transient_name .= $rand_index;
|
|
|
|
}
|
|
|
|
|
2017-10-31 12:58:36 +00:00
|
|
|
return $transient_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-06 13:13:43 +00:00
|
|
|
* Run the query and return an array of data, including queried ids and pagination information.
|
2017-10-31 12:58:36 +00:00
|
|
|
*
|
2017-12-05 18:45:08 +00:00
|
|
|
* @since 3.3.0
|
2017-12-06 13:13:43 +00:00
|
|
|
* @return object Object with the following props; ids, per_page, found_posts, max_num_pages, current_page
|
2017-10-31 12:58:36 +00:00
|
|
|
*/
|
2017-12-06 13:13:43 +00:00
|
|
|
protected function get_query_results() {
|
2019-01-21 12:31:48 +00:00
|
|
|
$transient_name = $this->get_transient_name();
|
|
|
|
$transient_version = WC_Cache_Helper::get_transient_version( 'product_query' );
|
|
|
|
$cache = wc_string_to_bool( $this->attributes['cache'] ) === true;
|
|
|
|
$transient_value = $cache ? get_transient( $transient_name ) : false;
|
2017-12-06 13:13:43 +00:00
|
|
|
|
2019-01-21 12:31:48 +00:00
|
|
|
if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
|
|
|
|
$results = $transient_value['value'];
|
|
|
|
} else {
|
2019-05-20 19:57:14 +00:00
|
|
|
$query = new WP_Query( $this->query_args );
|
2017-12-05 16:31:50 +00:00
|
|
|
|
2017-12-06 19:03:14 +00:00
|
|
|
$paginated = ! $query->get( 'no_found_rows' );
|
|
|
|
|
2017-12-06 13:13:43 +00:00
|
|
|
$results = (object) array(
|
|
|
|
'ids' => wp_parse_id_list( $query->posts ),
|
2017-12-06 19:03:14 +00:00
|
|
|
'total' => $paginated ? (int) $query->found_posts : count( $query->posts ),
|
|
|
|
'total_pages' => $paginated ? (int) $query->max_num_pages : 1,
|
2017-12-06 13:13:43 +00:00
|
|
|
'per_page' => (int) $query->get( 'posts_per_page' ),
|
2017-12-06 19:03:14 +00:00
|
|
|
'current_page' => $paginated ? (int) max( 1, $query->get( 'paged', 1 ) ) : 1,
|
2017-12-05 16:31:50 +00:00
|
|
|
);
|
2017-08-25 18:55:34 +00:00
|
|
|
|
2017-11-09 13:43:15 +00:00
|
|
|
if ( $cache ) {
|
2019-01-21 12:31:48 +00:00
|
|
|
$transient_value = array(
|
|
|
|
'version' => $transient_version,
|
|
|
|
'value' => $results,
|
|
|
|
);
|
|
|
|
set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
|
2017-11-09 11:24:44 +00:00
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
2019-01-21 12:31:48 +00:00
|
|
|
|
2018-02-08 13:59:03 +00:00
|
|
|
// Remove ordering query arguments which may have been added by get_catalog_ordering_args.
|
|
|
|
WC()->query->remove_ordering_args();
|
2019-01-21 12:31:48 +00:00
|
|
|
|
2020-02-05 18:08:40 +00:00
|
|
|
/**
|
|
|
|
* Filter shortcode products query results.
|
|
|
|
*
|
|
|
|
* @since 4.0.0
|
|
|
|
* @param stdClass $results Query results.
|
|
|
|
* @param WC_Shortcode_Products $this WC_Shortcode_Products instance.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'woocommerce_shortcode_products_query_results', $results, $this );
|
2017-08-29 21:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop over found products.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function product_loop() {
|
2017-12-06 18:10:44 +00:00
|
|
|
$columns = absint( $this->attributes['columns'] );
|
|
|
|
$classes = $this->get_wrapper_classes( $columns );
|
|
|
|
$products = $this->get_query_results();
|
2017-08-25 00:24:05 +00:00
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
ob_start();
|
|
|
|
|
2017-12-06 13:13:43 +00:00
|
|
|
if ( $products && $products->ids ) {
|
2018-05-01 11:31:17 +00:00
|
|
|
// Prime caches to reduce future queries.
|
|
|
|
if ( is_callable( '_prime_post_caches' ) ) {
|
|
|
|
_prime_post_caches( $products->ids );
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-12-06 18:10:44 +00:00
|
|
|
// Setup the loop.
|
2018-03-09 15:55:37 +00:00
|
|
|
wc_setup_loop(
|
|
|
|
array(
|
|
|
|
'columns' => $columns,
|
|
|
|
'name' => $this->type,
|
|
|
|
'is_shortcode' => true,
|
|
|
|
'is_search' => false,
|
|
|
|
'is_paginated' => wc_string_to_bool( $this->attributes['paginate'] ),
|
|
|
|
'total' => $products->total,
|
|
|
|
'total_pages' => $products->total_pages,
|
|
|
|
'per_page' => $products->per_page,
|
|
|
|
'current_page' => $products->current_page,
|
|
|
|
)
|
|
|
|
);
|
2017-12-06 18:10:44 +00:00
|
|
|
|
2017-11-17 20:38:30 +00:00
|
|
|
$original_post = $GLOBALS['post'];
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes );
|
2018-01-31 21:46:52 +00:00
|
|
|
|
|
|
|
// Fire standard shop loop hooks when paginating results so we can show result counts and so on.
|
|
|
|
if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
|
|
|
|
do_action( 'woocommerce_before_shop_loop' );
|
|
|
|
}
|
2017-11-08 18:09:39 +00:00
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
woocommerce_product_loop_start();
|
|
|
|
|
2017-12-06 18:10:44 +00:00
|
|
|
if ( wc_get_loop_prop( 'total' ) ) {
|
|
|
|
foreach ( $products->ids as $product_id ) {
|
2019-12-06 14:18:13 +00:00
|
|
|
$GLOBALS['post'] = get_post( $product_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
|
2017-12-06 18:10:44 +00:00
|
|
|
setup_postdata( $GLOBALS['post'] );
|
2017-08-29 21:29:29 +00:00
|
|
|
|
2017-12-06 18:10:44 +00:00
|
|
|
// Set custom product visibility when quering hidden products.
|
|
|
|
add_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
|
2017-08-29 21:29:29 +00:00
|
|
|
|
2017-12-06 18:10:44 +00:00
|
|
|
// Render product template.
|
|
|
|
wc_get_template_part( 'content', 'product' );
|
2017-08-29 21:29:29 +00:00
|
|
|
|
2017-12-06 18:10:44 +00:00
|
|
|
// Restore product visibility.
|
|
|
|
remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:18:13 +00:00
|
|
|
$GLOBALS['post'] = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
|
2017-08-24 18:29:30 +00:00
|
|
|
woocommerce_product_loop_end();
|
|
|
|
|
2018-01-31 21:46:52 +00:00
|
|
|
// Fire standard shop loop hooks when paginating results so we can show result counts and so on.
|
|
|
|
if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
|
|
|
|
do_action( 'woocommerce_after_shop_loop' );
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );
|
2017-11-17 20:38:30 +00:00
|
|
|
|
|
|
|
wp_reset_postdata();
|
2017-12-06 18:10:44 +00:00
|
|
|
wc_reset_loop();
|
2017-08-24 18:29:30 +00:00
|
|
|
} else {
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes );
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 01:16:42 +00:00
|
|
|
return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
2019-05-20 19:57:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Order by rating.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $args Query args.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function order_by_rating_post_clauses( $args ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$args['where'] .= " AND $wpdb->commentmeta.meta_key = 'rating' ";
|
|
|
|
$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)";
|
|
|
|
$args['orderby'] = "$wpdb->commentmeta.meta_value DESC";
|
|
|
|
$args['groupby'] = "$wpdb->posts.ID";
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|