From 1714aa569e62047884becd2eaf88c48823bbdd66 Mon Sep 17 00:00:00 2001 From: audrasjb Date: Tue, 25 Mar 2025 00:35:20 +0100 Subject: [PATCH] Assemble together the two proposed PR --- .../rest-api/class-wp-rest-request.php | 5 +++ tests/phpunit/tests/rest-api/rest-request.php | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index 3257194e547d2..a0e67e5d4affa 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -494,6 +494,11 @@ public function get_params() { } } + // Exclude rest_route if pretty permalinks are not enabled. + if ( ! get_option( 'permalink_structure' ) ) { + unset( $params['rest_route'] ); + } + return $params; } diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index 1067739ed2469..d689f9d18d4d0 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -1126,4 +1126,37 @@ public function data_is_method_should_detect_method_ignoring_case() { 'HEAD wrong method' => array( 'HEAD', 'GET', false ), ); } + + /** + * @ticket 62163 + */ + public function test_get_params_without_pretty_permalink() { + update_option( 'permalink_structure', '' ); + + $request = new WP_REST_Request(); + $request->set_param( 'rest_route', '/wp/v2/posts' ); + $request->set_param( 'some_param', 'foobar' ); + + $params = $request->get_params(); + + $this->assertArrayNotHasKey( 'rest_route', $params ); + $this->assertArrayHasKey( 'some_param', $params ); + } + + /** + * @ticket 62163 + */ + public function test_get_params_with_pretty_permalinks() { + update_option( 'permalink_structure', '/%postname%/' ); + + $request = new WP_REST_Request(); + + $request->set_param( 'rest_route', '/wp/v2/posts' ); + $request->set_param( 'some_param', 'foobar' ); + + $params = $request->get_params(); + + $this->assertArrayHasKey( 'rest_route', $params ); + $this->assertArrayHasKey( 'some_param', $params ); + } }