REST API: Handle parameter types consistently within set_param().

A request has multiple parameter types, including "query" and "json." Updating a parameter could previously modify a key's value in the wrong parameter type, leading to confusing and self-contradictory response objects.

Props mnelson4, TimothyBlynJacobs, vagios, jnylen0.
Fixes #40838.

Built from https://develop.svn.wordpress.org/trunk@47559


git-svn-id: http://core.svn.wordpress.org/trunk@47334 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
K. Adam White 2020-04-09 19:30:07 +00:00
parent f927fdedb4
commit 951b1d89db
2 changed files with 18 additions and 3 deletions

View File

@ -420,6 +420,10 @@ class WP_REST_Request implements ArrayAccess {
/**
* Sets a parameter on the request.
*
* If the given parameter key exists in any parameter type an update will take place,
* otherwise a new param will be created in the first parameter type (respecting
* get_parameter_order()).
*
* @since 4.4.0
*
* @param string $key Parameter name.
@ -427,8 +431,19 @@ class WP_REST_Request implements ArrayAccess {
*/
public function set_param( $key, $value ) {
$order = $this->get_parameter_order();
$found_key = false;
foreach ( $order as $type ) {
if ( 'defaults' !== $type && array_key_exists( $key, $this->params[ $type ] ) ) {
$this->params[ $type ][ $key ] = $value;
$found_key = true;
}
}
if ( ! $found_key ) {
$this->params[ $order[0] ][ $key ] = $value;
}
}
/**
* Retrieves merged parameters from the request.

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-47558';
$wp_version = '5.5-alpha-47559';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.