Fix taxes endpoint returning incorrect X-WP-Total and X-WP-TotalPages headers #30890
This commit is contained in:
parent
727ccd0dd3
commit
05fbbc591e
|
@ -275,24 +275,31 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
|
||||||
|
|
||||||
$response = rest_ensure_response( $taxes );
|
$response = rest_ensure_response( $taxes );
|
||||||
|
|
||||||
// Store pagination values for headers then unset for count query.
|
|
||||||
$per_page = (int) $prepared_args['number'];
|
$per_page = (int) $prepared_args['number'];
|
||||||
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
|
||||||
|
|
||||||
// Query only for ids.
|
// Unset LIMIT args.
|
||||||
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
|
array_splice( $wpdb_prepare_args, -2 );
|
||||||
$query = str_replace( 'SELECT *', 'SELECT tax_rate_id', $query );
|
|
||||||
$wpdb->get_results(
|
// Count query.
|
||||||
$wpdb->prepare(
|
$query = str_replace(
|
||||||
$query,
|
array(
|
||||||
$wpdb_prepare_args
|
'SELECT *',
|
||||||
)
|
'LIMIT %d, %d',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'SELECT COUNT(*)',
|
||||||
|
'',
|
||||||
|
),
|
||||||
|
$query
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
|
||||||
|
$total_taxes = (int) $wpdb->get_var( empty( $wpdb_prepare_args ) ? $query : $wpdb->prepare( $query, $wpdb_prepare_args ) );
|
||||||
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
|
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
|
||||||
|
|
||||||
// Calculate totals.
|
// Calculate totals.
|
||||||
$total_taxes = (int) $wpdb->num_rows;
|
$response->header( 'X-WP-Total', $total_taxes );
|
||||||
$response->header( 'X-WP-Total', (int) $total_taxes );
|
|
||||||
$max_pages = ceil( $total_taxes / $per_page );
|
$max_pages = ceil( $total_taxes / $per_page );
|
||||||
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
$response->header( 'X-WP-TotalPages', (int) $max_pages );
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue