diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index d9c24e0658..73548d4f38 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -408,7 +408,7 @@ class WP_REST_Request implements ArrayAccess { $order = $this->get_parameter_order(); foreach ( $order as $type ) { - if ( array_key_exists( $key, $this->params[ $type ] ) ) { + if ( is_array( $this->params[ $type ] ) && array_key_exists( $key, $this->params[ $type ] ) ) { return true; } } @@ -433,7 +433,7 @@ class WP_REST_Request implements ArrayAccess { $found_key = false; foreach ( $order as $type ) { - if ( 'defaults' !== $type && array_key_exists( $key, $this->params[ $type ] ) ) { + if ( 'defaults' !== $type && is_array( $this->params[ $type ] ) && array_key_exists( $key, $this->params[ $type ] ) ) { $this->params[ $type ][ $key ] = $value; $found_key = true; } diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index 9cbb0f3994..2a78db4660 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -765,4 +765,18 @@ class Tests_REST_Request extends WP_UnitTestCase { $this->assertEquals( array( 'param' => 'new_value' ), $request->get_json_params() ); $this->assertEquals( array( 'param' => 'new_value' ), $request->get_query_params() ); } + + /** + * @ticket 50786 + */ + public function test_set_param_with_invalid_json() { + $request = new WP_REST_Request(); + $request->add_header( 'content-type', 'application/json' ); + $request->set_method( 'POST' ); + $request->set_body( '' ); + $request->set_param( 'param', 'value' ); + + $this->assertTrue( $request->has_param( 'param' ) ); + $this->assertEquals( 'value', $request->get_param( 'param' ) ); + } }