Merge pull request #16725 from woocommerce/update/14260

Shortcode random sorting
This commit is contained in:
Claudio Sanches 2017-10-31 11:56:19 -02:00 committed by GitHub
commit 42ae05265b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 1 deletions

View File

@ -429,6 +429,26 @@ class WC_Shortcode_Products {
return $classes;
}
/**
* Generate and return the transient name for this shortcode based on the query args.
*
* @since 3.3.0
* @return string
*/
protected function get_transient_name() {
$transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 );
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.
$rand_index = rand( 0, max( 1, absint( apply_filters( 'woocommerce_product_query_max_rand_cache_count', 5 ) ) ) );
$transient_name .= $rand_index;
}
$transient_name .= WC_Cache_Helper::get_transient_version( 'product_query' );
return $transient_name;
}
/**
* Get products.
*
@ -436,7 +456,7 @@ class WC_Shortcode_Products {
* @return WP_Query
*/
protected function get_products() {
$transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' );
$transient_name = $this->get_transient_name();
$products = get_transient( $transient_name );
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {