From 874bffc0db6a7c9e0a54d40a3fc5418d22e8bef6 Mon Sep 17 00:00:00 2001 From: Michael Pretty Date: Fri, 1 Dec 2023 06:44:56 -0500 Subject: [PATCH] Fix 40075/avoid count query unless after first page (#40092) * Avoid running a second query to account for incorrect totals if pagination is set too high if pagination isn't used. Fixes #40075 * Adding changelog entry --- .../fix-40075-avoid-count-query-unless-after-first-page | 4 ++++ .../Controllers/Version3/class-wc-rest-crud-controller.php | 2 +- .../Controllers/Version3/class-wc-rest-posts-controller.php | 2 +- .../woocommerce/src/Admin/API/Reports/Stock/Controller.php | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-40075-avoid-count-query-unless-after-first-page diff --git a/plugins/woocommerce/changelog/fix-40075-avoid-count-query-unless-after-first-page b/plugins/woocommerce/changelog/fix-40075-avoid-count-query-unless-after-first-page new file mode 100644 index 00000000000..76f50a1f25a --- /dev/null +++ b/plugins/woocommerce/changelog/fix-40075-avoid-count-query-unless-after-first-page @@ -0,0 +1,4 @@ +Significance: patch +Type: performance + +Prevent second query used to determine real post count from running if paging isn't being used. diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php index a29852c78ca..66a1c8bd2b6 100644 --- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php +++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php @@ -354,7 +354,7 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller { $result = $query->query( $query_args ); $total_posts = $query->found_posts; - if ( $total_posts < 1 ) { + if ( $total_posts < 1 && isset( $query_args['paged'] ) && absint( $query_args['paged'] ) > 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php index 59757f300d9..6ac4744134e 100644 --- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php +++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-posts-controller.php @@ -367,7 +367,7 @@ abstract class WC_REST_Posts_Controller extends WC_REST_Controller { $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; - if ( $total_posts < 1 ) { + if ( $total_posts < 1 && $page > 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); diff --git a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php index d083fd95315..72cb2f66c74 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Stock/Controller.php @@ -93,7 +93,7 @@ class Controller extends GenericController implements ExportableInterface { $result = $query->query( $query_args ); $total_posts = $query->found_posts; - if ( $total_posts < 1 ) { + if ( $total_posts < 1 && isset( $query_args['paged'] ) && absint( $query_args['paged'] ) > 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new \WP_Query();