WordPress.org

Make WordPress Core

Opened 8 months ago

Closed 6 months ago

#40344 closed defect (bug) (fixed)

WP_REST_Request::set_param() ignores parameter order.

Reported by: TimothyBlynJacobs Owned by: jnylen0
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.4
Component: REST API Keywords: has-patch has-unit-tests commit
Focuses: rest-api Cc:

Description

WP_Rest_Request::get_param() traverses through the parameter order but WP_Rest_Request::set_param() does not. For JSON requests, this means that changing a parameter with set_param() will have no affect on get_param().

Example Code:

$request = new WP_REST_Request();
$request->add_header( 'content-type', 'application/json' );
$request->set_method( 'POST' );
$request->set_body( wp_json_encode( [
	'param' => 'hi'
] ) );

echo $request->get_param( 'param' ); // hi
$request->set_param( 'param', 'hello' );
echo $request->get_param( 'param' ); // hi

Patch incoming.

Attachments (2)

40344.patch (2.1 KB) - added by TimothyBlynJacobs 8 months ago.
40344.2.diff (2.2 KB) - added by jnylen0 6 months ago.
Use [0] instead of reset; whitespace fixes; use array() instead of []

Download all attachments as: .zip

Change History (10)

#1 @TimothyBlynJacobs
8 months ago

  • Keywords has-patch has-unit-tests added

#2 @swissspidy
8 months ago

  • Milestone changed from Awaiting Review to 4.8
  • Version changed from trunk to 4.4

#3 @rmccue
6 months ago

Good catch! I didn't get to this previously, but good to go for 4.8, because it's a bugfix.

Patch looks good, but I'd rather avoid using reset(), since it modifies the array. We know $order is an indexed array, so $first = $order[0] is fine.

#4 @TimothyBlynJacobs
6 months ago

Great, thanks! Should I upload another patch?

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


6 months ago

@jnylen0
6 months ago

Use [0] instead of reset; whitespace fixes; use array() instead of []

#6 @jnylen0
6 months ago

  • Keywords commit added
  • Owner set to jnylen0
  • Status changed from new to accepted

@TimothyBlynJacobs in general, yes, if a patch has known issues then it is always useful to have a refreshed version on the ticket.

40344.2.diff addresses the following issues:

  • Use [0] instead of reset to get the first element of the array
  • Core (including unit tests) cannot use [ 'key' => 'value' ] array syntax; we still support PHP 5.2
  • Trailing whitespace on blank lines

Patch LGTM as well. I'll commit when tests pass locally.

#7 @TimothyBlynJacobs
6 months ago

Awesome, thanks @jnylen0!

#8 @jnylen0
6 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 40815:

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.

Note: See TracTickets for help on using tickets.