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.


git-svn-id: https://develop.svn.wordpress.org/trunk@47559 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White
2020-04-09 19:28:58 +00:00
parent e4b83cf509
commit 3abe80eea1
2 changed files with 132 additions and 2 deletions
@@ -420,14 +420,29 @@ 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.
* @param mixed $value Parameter value.
*/
public function set_param( $key, $value ) {
$order = $this->get_parameter_order();
$this->params[ $order[0] ][ $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;
}
}
/**