WordPress.org

Make WordPress Core

Changeset 40815


Ignore:
Timestamp:
05/22/2017 04:15:25 PM (14 months ago)
Author:
jnylen0
Message:

REST API: Fix changing parameters with set_param() for some requests.

Prior to this commit, WP_Rest_Request::get_param() traversed through the parameter order but WP_Rest_Request::set_param() did not. For JSON requests (and likely other situations as well), this meant that changing a parameter with set_param() would have no effect on get_param().

Props TimothyBlynJacobs.
Fixes #40344.

Location:
trunk
Files:
2 edited

Legend:

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

    r40577 r40815  
    355355    protected function get_parameter_order() {
    356356        $order = array();
    357         $order[] = 'JSON';
     357
     358        $content_type = $this->get_content_type();
     359        if ( $content_type['value'] === 'application/json' ) {
     360            $order[] = 'JSON';
     361        }
    358362
    359363        $this->parse_json_params();
     
    425429     */
    426430    public function set_param( $key, $value ) {
    427         switch ( $this->method ) {
    428             case 'POST':
    429                 $this->params['POST'][ $key ] = $value;
    430                 break;
    431 
    432             default:
    433                 $this->params['GET'][ $key ] = $value;
    434                 break;
    435         }
     431        $order = $this->get_parameter_order();
     432        $this->params[ $order[0] ][ $key ] = $value;
    436433    }
    437434
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r40577 r40815  
    581581        $this->assertFalse( $request );
    582582    }
     583
     584    public function test_set_param() {
     585        $request = new WP_REST_Request();
     586        $request->set_param( 'param', 'value' );
     587        $this->assertEquals( 'value', $request->get_param( 'param' ) );
     588    }
     589
     590    public function test_set_param_follows_parameter_order() {
     591        $request = new WP_REST_Request();
     592        $request->add_header( 'content-type', 'application/json' );
     593        $request->set_method( 'POST' );
     594        $request->set_body( wp_json_encode( array(
     595            'param' => 'value'
     596        ) ) );
     597        $this->assertEquals( 'value', $request->get_param( 'param' ) );
     598        $this->assertEquals(
     599            array( 'param' => 'value' ),
     600            $request->get_json_params()
     601        );
     602
     603        $request->set_param( 'param', 'new_value' );
     604        $this->assertEquals( 'new_value', $request->get_param( 'param' ) );
     605        $this->assertEquals(
     606            array( 'param' => 'new_value' ),
     607            $request->get_json_params()
     608        );
     609    }
    583610}
Note: See TracChangeset for help on using the changeset viewer.