Do not override pagination args if `woocommerce_hpos_pre_query` doesn't override query (#40551)

This commit is contained in:
Barry Hughes 2023-10-02 17:10:46 -07:00 committed by GitHub
commit 0048341046
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 20 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix backwards compatibility issue with `wc_get_orders()` when HPOS is active and the pagination bit is set.

View File

@ -230,21 +230,26 @@ class OrdersTableQuery {
* @param OrdersTableQuery $query The OrdersTableQuery instance.
* @param string $sql The OrdersTableQuery instance.
*/
list( $this->orders, $this->found_orders, $this->max_num_pages ) = apply_filters( 'woocommerce_hpos_pre_query', null, $this, $this->sql );
// If the filter set the orders, make sure the others values are set as well and skip running the query.
if ( is_array( $this->orders ) ) {
if ( ! is_int( $this->found_orders ) || $this->found_orders < 1 ) {
$this->found_orders = count( $this->orders );
}
if ( ! is_int( $this->max_num_pages ) || $this->max_num_pages < 1 ) {
if ( ! $this->arg_isset( 'limit' ) || ! is_int( $this->args['limit'] ) || $this->args['limit'] < 1 ) {
$this->args['limit'] = 10;
}
$this->max_num_pages = (int) ceil( $this->found_orders / $this->args['limit'] );
}
return true;
$pre_query = apply_filters( 'woocommerce_hpos_pre_query', null, $this, $this->sql );
if ( ! $pre_query || ! isset( $pre_query[0] ) || ! is_array( $pre_query[0] ) ) {
return false;
}
return false;
// If the filter set the orders, make sure the others values are set as well and skip running the query.
list( $this->orders, $this->found_orders, $this->max_num_pages ) = $pre_query;
if ( ! is_int( $this->found_orders ) || $this->found_orders < 1 ) {
$this->found_orders = count( $this->orders );
}
if ( ! is_int( $this->max_num_pages ) || $this->max_num_pages < 1 ) {
if ( ! $this->arg_isset( 'limit' ) || ! is_int( $this->args['limit'] ) || $this->args['limit'] < 1 ) {
$this->args['limit'] = 10;
}
$this->max_num_pages = (int) ceil( $this->found_orders / $this->args['limit'] );
}
return true;
}
/**

View File

@ -351,16 +351,22 @@ class OrdersTableQueryTests extends WC_Unit_Test_Case {
* @testdox A regular query will still work even if the pre-query escape hook returns null for the whole 3-tuple.
*/
public function test_pre_query_escape_hook_return_null() {
add_filter( 'woocommerce_hpos_pre_query', '__return_null', 10, 3 );
// Query with no results.
$query = new OrdersTableQuery();
$this->assertNotNull( $query->orders );
$this->assertNotNull( $query->found_orders );
$this->assertNotNull( $query->max_num_pages );
$this->assertCount( 0, $query->orders );
$this->assertEquals( 0, $query->found_orders );
$this->assertEquals( 0, $query->max_num_pages );
// Query with 1 result.
$order1 = new \WC_Order();
$order1->set_date_created( time() - HOUR_IN_SECONDS );
$order1->save();
$callback = function( $result, $query_object, $sql ) use ( $order1 ) {
// Just return null.
return null;
};
add_filter( 'woocommerce_hpos_pre_query', $callback, 10, 3 );
$query = new OrdersTableQuery();
$this->assertCount( 1, $query->orders );
$this->assertEquals( 1, $query->found_orders );