REST API: Fix PHP warning about undefined paged argument in various REST API endpoints.
Some checks are pending
Coding Standards / PHP coding standards (push) Waiting to run
Coding Standards / JavaScript coding standards (push) Waiting to run
Coding Standards / Slack Notifications (push) Blocked by required conditions
Coding Standards / Failed workflow tasks (push) Blocked by required conditions
End-to-end Tests / Test with SCRIPT_DEBUG disabled (push) Waiting to run
End-to-end Tests / Test with SCRIPT_DEBUG enabled (push) Waiting to run
End-to-end Tests / Slack Notifications (push) Blocked by required conditions
End-to-end Tests / Failed workflow tasks (push) Blocked by required conditions
JavaScript Tests / QUnit Tests (push) Waiting to run
JavaScript Tests / Slack Notifications (push) Blocked by required conditions
JavaScript Tests / Failed workflow tasks (push) Blocked by required conditions
Performance Tests / Single site (push) Waiting to run
Performance Tests / Multisite (push) Waiting to run
Performance Tests / Slack Notifications (push) Blocked by required conditions
Performance Tests / Failed workflow tasks (push) Blocked by required conditions
PHP Compatibility / Check PHP compatibility (push) Waiting to run
PHP Compatibility / Slack Notifications (push) Blocked by required conditions
PHP Compatibility / Failed workflow tasks (push) Blocked by required conditions
PHPUnit Tests / PHP 7.2 (push) Waiting to run
PHPUnit Tests / PHP 7.3 (push) Waiting to run
PHPUnit Tests / PHP 7.4 (push) Waiting to run
PHPUnit Tests / PHP 8.0 (push) Waiting to run
PHPUnit Tests / PHP 8.1 (push) Waiting to run
PHPUnit Tests / PHP 8.2 (push) Waiting to run
PHPUnit Tests / PHP 8.3 (push) Waiting to run
PHPUnit Tests / PHP 8.4 (push) Waiting to run
PHPUnit Tests / html-api-html5lib-tests (push) Waiting to run
PHPUnit Tests / Slack Notifications (push) Blocked by required conditions
PHPUnit Tests / Failed workflow tasks (push) Blocked by required conditions
Test Build Processes / Core running from build (push) Waiting to run
Test Build Processes / Core running from src (push) Waiting to run
Test Build Processes / Gutenberg running from build (push) Waiting to run
Test Build Processes / Gutenberg running from src (push) Waiting to run
Test Build Processes / Slack Notifications (push) Blocked by required conditions
Test Build Processes / Failed workflow tasks (push) Blocked by required conditions

This bug could occur in `WP_REST_Posts_Controller`, `WP_REST_Global_Styles_Revisions_Controller`, `WP_REST_Revisions_Controller`, and any of their child classes. This changeset fixes it throughout.

Props apermo, pbearne, hemant-ahir, flixos90.
Fixes #62292.


git-svn-id: https://develop.svn.wordpress.org/trunk@59630 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2025-01-15 18:34:35 +00:00
parent aaf760cbdc
commit d2630e00cd
7 changed files with 163 additions and 3 deletions

View File

@ -189,7 +189,7 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Contr
$revisions_query = new WP_Query();
$revisions = $revisions_query->query( $query_args );
$offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_revisions = $revisions_query->found_posts;
if ( $total_revisions < 1 ) {

View File

@ -457,7 +457,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
}
$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_posts = $posts_query->found_posts;
if ( $total_posts < 1 && $page > 1 ) {

View File

@ -294,7 +294,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
$revisions_query = new WP_Query();
$revisions = $revisions_query->query( $query_args );
$offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
$page = (int) $query_args['paged'];
$page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0;
$total_revisions = $revisions_query->found_posts;
if ( $total_revisions < 1 ) {

View File

@ -826,6 +826,47 @@ class WP_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_REST_Contr
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Global_Styles_Controller::get_items
*/
public function test_get_global_styles_revisions_pagination() {
wp_set_current_user( self::$admin_id );
// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 3, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 3, $response->get_headers()['X-WP-TotalPages'] );
// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 3, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 2, $response->get_headers()['X-WP-TotalPages'] );
// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 6 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}
/**
* @doesNotPerformAssertions
*/

View File

@ -5676,6 +5676,45 @@ Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket baco
$this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' );
}
/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Posts_Controller::get_items
*/
public function test_get_posts_with_pagination() {
// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 30, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 30, $response->get_headers()['X-WP-TotalPages'] );
// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 2, $data );
$this->assertEquals( 30, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 15, $response->get_headers()['X-WP-TotalPages'] );
// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 10 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 );
}
/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.

View File

@ -864,4 +864,44 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Revisions_Controller::get_items
*/
public function test_get_template_revisions_pagination() {
wp_set_current_user( self::$editor_id );
// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-TotalPages'] );
// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $this->total_revisions, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( (int) ceil( $this->total_revisions / 2 ), $response->get_headers()['X-WP-TotalPages'] );
// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'page', $this->total_revisions + 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}
}

View File

@ -1065,4 +1065,44 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
),
);
}
/**
* Tests for the pagination.
*
* @ticket 62292
*
* @covers WP_REST_Template_Revisions_Controller::get_items
*/
public function test_get_template_revisions_pagination() {
wp_set_current_user( self::$admin_id );
// Test offset
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'offset', 1 );
$request->set_param( 'per_page', 1 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 4, $response->get_headers()['X-WP-TotalPages'] );
// Test paged
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'page', 2 );
$request->set_param( 'per_page', 2 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertCount( 2, $data );
$this->assertEquals( 4, $response->get_headers()['X-WP-Total'] );
$this->assertEquals( 2, $response->get_headers()['X-WP-TotalPages'] );
// Test out of bounds
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions' );
$request->set_param( 'page', 4 );
$request->set_param( 'per_page', 6 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_invalid_page_number', $response, 400 );
}
}