WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/09/2020 07:28:58 PM (11 months ago)
Author:
kadamwhite
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-request.php

    r47122 r47559  
    421421     * Sets a parameter on the request.
    422422     *
     423     * If the given parameter key exists in any parameter type an update will take place,
     424     * otherwise a new param will be created in the first parameter type (respecting
     425     * get_parameter_order()).
     426     *
    423427     * @since 4.4.0
    424428     *
     
    427431     */
    428432    public function set_param( $key, $value ) {
    429         $order                             = $this->get_parameter_order();
    430         $this->params[ $order[0] ][ $key ] = $value;
     433        $order     = $this->get_parameter_order();
     434        $found_key = false;
     435
     436        foreach ( $order as $type ) {
     437            if ( 'defaults' !== $type && array_key_exists( $key, $this->params[ $type ] ) ) {
     438                $this->params[ $type ][ $key ] = $value;
     439                $found_key                     = true;
     440            }
     441        }
     442
     443        if ( ! $found_key ) {
     444            $this->params[ $order[0] ][ $key ] = $value;
     445        }
    431446    }
    432447
Note: See TracChangeset for help on using the changeset viewer.