Skip to content

Commit 9c09fe0

Browse files
committed
REST API: exclude rest_route from get_params() if pretty permalinks are disabled.
This changeset introduces a modification to the `get_params()` method within the WordPress REST API. The change ensures that the `rest_route` parameter is excluded from the parameters returned when pretty permalinks are not enabled. This update enhances the developer experience by ensuring that the parameters returned by `get_params()` are relevant and do not include unnecessary values, thereby reducing potential confusion and errors. Props westonruter, TimothyBlynJacobs, audrasjb, debarghyabanerjee, dilip2615, shanemuir, peterwilsoncc. Fixes #62163. git-svn-id: https://develop.svn.wordpress.org/trunk@60073 602fd350-edb4-49c9-b593-d223f7449a82
1 parent eded474 commit 9c09fe0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/wp-includes/rest-api/class-wp-rest-request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ public function get_params() {
494494
}
495495
}
496496

497+
// Exclude rest_route if pretty permalinks are not enabled.
498+
if ( ! get_option( 'permalink_structure' ) ) {
499+
unset( $params['rest_route'] );
500+
}
501+
497502
return $params;
498503
}
499504

tests/phpunit/tests/rest-api/rest-request.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,4 +1126,37 @@ public function data_is_method_should_detect_method_ignoring_case() {
11261126
'HEAD wrong method' => array( 'HEAD', 'GET', false ),
11271127
);
11281128
}
1129+
1130+
/**
1131+
* @ticket 62163
1132+
*/
1133+
public function test_get_params_without_pretty_permalink() {
1134+
update_option( 'permalink_structure', '' );
1135+
1136+
$request = new WP_REST_Request();
1137+
$request->set_param( 'rest_route', '/wp/v2/posts' );
1138+
$request->set_param( 'some_param', 'foobar' );
1139+
1140+
$params = $request->get_params();
1141+
1142+
$this->assertArrayNotHasKey( 'rest_route', $params );
1143+
$this->assertArrayHasKey( 'some_param', $params );
1144+
}
1145+
1146+
/**
1147+
* @ticket 62163
1148+
*/
1149+
public function test_get_params_with_pretty_permalinks() {
1150+
update_option( 'permalink_structure', '/%postname%/' );
1151+
1152+
$request = new WP_REST_Request();
1153+
1154+
$request->set_param( 'rest_route', '/wp/v2/posts' );
1155+
$request->set_param( 'some_param', 'foobar' );
1156+
1157+
$params = $request->get_params();
1158+
1159+
$this->assertArrayHasKey( 'rest_route', $params );
1160+
$this->assertArrayHasKey( 'some_param', $params );
1161+
}
11291162
}

0 commit comments

Comments
 (0)