Normalise get_query_results names and add new param for pagination

This commit is contained in:
Mike Jolley 2017-12-06 13:13:43 +00:00
parent 535d014ef9
commit 8dcffa660e
5 changed files with 84 additions and 58 deletions

View File

@ -71,9 +71,10 @@ class WC_Query {
public function init_query_vars() {
// Query vars to add to WP.
$this->query_vars = array(
'product-page' => '',
// Checkout actions.
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
// My account actions.
'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),
'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),

View File

@ -175,7 +175,7 @@ class WC_Shortcode_Products {
);
if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
$this->attributes['page'] = get_query_var( 'paged', 1 );
$this->attributes['page'] = get_query_var( 'product-page', 1 );
}
if ( ! empty( $this->attributes['rows'] ) ) {
@ -488,42 +488,42 @@ class WC_Shortcode_Products {
}
/**
* Get products data.
* Run the query and return an array of data, including queried ids and pagination information.
*
* @since 3.3.0
* @return array See $data variable for keys and contents.
* @return object Object with the following props; ids, per_page, found_posts, max_num_pages, current_page
*/
protected function get_products_data() {
protected function get_query_results() {
$transient_name = $this->get_transient_name();
$cache = true === wc_string_to_bool( $this->attributes['cache'] );
$cache = wc_string_to_bool( $this->attributes['cache'] ) === true;
$data = $cache ? get_transient( $transient_name ) : false;
if ( false === $data ) {
if ( 'top_rated_products' === $this->type ) {
add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
$products = new WP_Query( $this->query_args );
$query = new WP_Query( $this->query_args );
remove_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
} else {
$products = new WP_Query( $this->query_args );
$query = new WP_Query( $this->query_args );
}
$data = array(
'ids' => wp_parse_id_list( $products->posts ),
'per_page' => $products->get( 'posts_per_page' ),
'found_posts' => $products->found_posts,
'max_num_pages' => $products->max_num_pages,
'current' => max( 1, $products->get( 'paged', 1 ) ),
'is_search' => intval( $products->is_search() ),
$results = (object) array(
'ids' => wp_parse_id_list( $query->posts ),
'total' => (int) $query->found_posts,
'total_pages' => (int) $query->max_num_pages,
'per_page' => (int) $query->get( 'posts_per_page' ),
'current_page' => (int) max( 1, $query->get( 'paged', 1 ) ),
);
if ( $cache ) {
set_transient( $transient_name, $data, DAY_IN_SECONDS * 30 );
set_transient( $transient_name, $results, DAY_IN_SECONDS * 30 );
}
}
// Remove ordering query arguments.
if ( ! empty( $this->attributes['category'] ) ) {
WC()->query->remove_ordering_args();
}
return $data;
return $results;
}
/**
@ -535,18 +535,19 @@ class WC_Shortcode_Products {
protected function product_loop() {
global $woocommerce_loop;
$columns = absint( $this->attributes['columns'] );
$classes = $this->get_wrapper_classes( $columns );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $this->type;
$products_data = $this->get_products_data();
$columns = absint( $this->attributes['columns'] );
$classes = $this->get_wrapper_classes( $columns );
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $this->type;
$woocommerce_loop['shortcode'] = true;
$products = $this->get_query_results();
ob_start();
if ( $products_data && $products_data['ids'] ) {
if ( $products && $products->ids ) {
// Prime meta cache to reduce future queries.
update_meta_cache( 'post', $products_data['ids'] );
update_object_term_cache( $products_data['ids'], 'product' );
update_meta_cache( 'post', $products->ids );
update_object_term_cache( $products->ids, 'product' );
$original_post = $GLOBALS['post'];
@ -554,16 +555,16 @@ class WC_Shortcode_Products {
if ( true === wc_string_to_bool( $this->attributes['paginate'] ) ) {
woocommerce_result_count( array(
'total' => intval( $products_data['found_posts'] ),
'per_page' => intval( $products_data['per_page'] ),
'current' => intval( $products_data['current'] ),
'total' => $products->total,
'per_page' => $products->per_page,
'current' => $products->current_page,
) );
woocommerce_catalog_ordering( (bool) $products_data['is_search'] );
woocommerce_catalog_ordering();
}
woocommerce_product_loop_start();
foreach ( $products_data['ids'] as $product_id ) {
foreach ( $products->ids as $product_id ) {
$GLOBALS['post'] = get_post( $product_id ); // WPCS: override ok.
setup_postdata( $GLOBALS['post'] );
@ -582,9 +583,11 @@ class WC_Shortcode_Products {
if ( true === wc_string_to_bool( $this->attributes['paginate'] ) ) {
woocommerce_pagination( array(
'total' => intval( $products_data['max_num_pages'] ),
'current' => intval( $products_data['current'] ),
), '?product-page=%#%' );
'total' => $products->total_pages,
'current' => $products->current_page,
'base' => esc_url_raw( add_query_arg( 'product-page', '%#%', false ) ),
'format' => '?product-page=%#%',
) );
}
do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );

View File

@ -276,6 +276,21 @@ if ( ! function_exists( 'is_filtered' ) ) {
}
}
if ( ! function_exists( 'is_shortcode_loop' ) ) {
/**
* Returns true when called within a loop generated by a products shortcode.
*
* @since 3.3.0
* @return bool
*/
function is_shortcode_loop() {
global $woocommerce_loop;
return apply_filters( 'woocommerce_is_shortcode_loop', ! empty( $woocommerce_loop['shortcode'] ) );
}
}
if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) {
/**

View File

@ -875,14 +875,20 @@ if ( ! function_exists( 'woocommerce_result_count' ) ) {
if ( ! woocommerce_products_will_display() ) {
return;
}
if ( empty( $args ) ) {
$query = $GLOBALS['wp_query'];
$args = array(
'total' => $query->found_posts,
'per_page' => $query->get( 'posts_per_page' ),
'current' => max( 1, $query->get( 'paged', 1 ) ),
);
}
$query = $GLOBALS['wp_query'];
$default_args = array(
'total' => $query->found_posts,
'per_page' => $query->get( 'posts_per_page' ),
'current' => max( 1, $query->get( 'paged', 1 ) ),
);
$args = wp_parse_args( $args, $default_args );
$args = wp_parse_args( $args, array(
'total' => 0,
'per_page' => 0,
'current' => 0,
) );
wc_get_template( 'loop/result-count.php', $args );
}
@ -892,10 +898,8 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
/**
* Output the product sorting options.
*
* @param null|bool $is_search Whether this is the catalog ordering for search results. Default is set from global $query.
*/
function woocommerce_catalog_ordering( $is_search = null ) {
function woocommerce_catalog_ordering() {
if ( ! woocommerce_products_will_display() ) {
return;
}
@ -911,11 +915,7 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),
) );
if ( null === $is_search ) {
$is_search = $query->is_search();
}
if ( $is_search ) {
if ( ! is_shortcode_loop() && $GLOBALS['wp_query']->is_search() ) {
$catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
unset( $catalog_orderby_options['menu_order'] );
if ( 'menu_order' === $orderby ) {
@ -947,13 +947,20 @@ if ( ! function_exists( 'woocommerce_pagination' ) ) {
* @param array $args Pass an associative array of parameters. Uses this if passed, otherwise uses global $wp_query.
*/
function woocommerce_pagination( $args = array() ) {
$query = $GLOBALS['wp_query'];
if ( empty( $args ) ) {
$query = $GLOBALS['wp_query'];
$args = array(
'total' => $query->max_num_pages,
'current' => max( 1, $query->get( 'paged', 1 ) ),
);
}
$default_args = array(
'total' => $query->max_num_pages,
'current' => max( 1, $query->get( 'paged', 1 ) ),
);
$args = wp_parse_args( $args, $default_args );
$args = wp_parse_args( $args, array(
'total' => 1,
'current' => 1,
'base' => esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
) );
wc_get_template( 'loop/pagination.php', $args );
}
@ -1703,7 +1710,7 @@ if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
* @return bool
*/
function woocommerce_products_will_display() {
if ( is_search() || is_filtered() || is_paged() ) {
if ( is_search() || is_filtered() || is_paged() || is_shortcode_loop() ) {
return true;
}

View File

@ -27,8 +27,8 @@ if ( $total <= 1 ) {
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'base' => $base,
'format' => $format,
'add_args' => false,
'current' => max( 1, $current ),
'total' => $total,