OAuth1 icon indicating copy to clipboard operation
OAuth1 copied to clipboard

PHP 8.0 warning thrown by normalize_parameters

Open szaqal83 opened this issue 4 years ago • 7 comments

After switching from PHP 7.4 to 8.0 a warning is thrown:

[16-Dec-2020 10:43:09 UTC] PHP Warning: WP_REST_OAuth1::normalize_parameters(): Argument #2 ($value) must be passed by reference, value given in /.../wp-content/plugins/rest-api-oauth1/lib/class-wp-rest-oauth1.php on line 672

szaqal83 avatar Dec 16 '20 10:12 szaqal83

Proper callback should look like this:

protected function normalize_parameters( &$value, $key ) { // $key = self::urlencode_rfc3986( rawurldecode( $key ) ); // doesn't make any effect $value = self::urlencode_rfc3986( rawurldecode( $value ) ); }

szaqal83 avatar Dec 29 '20 13:12 szaqal83

Are you sure you have the params in the correct order? Previously, it was $key first, $value second:

https://github.com/WP-API/OAuth1/blob/master/lib/class-wp-rest-oauth1.php#L754

scottfennell-toptal avatar Aug 25 '22 12:08 scottfennell-toptal

Params order is fine, normalize_parameters is called here: /.../wp-content/plugins/rest-api-oauth1/lib/class-wp-rest-oauth1.php on line 672

array_walk_recursive( $params, array( $this, 'normalize_parameters' ) );

according to PHP docs https://www.php.net/manual/en/function.array-walk-recursive.php: "Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second."

So declaration of normalize_parameters function in: /.../wp-content/plugins/rest-api-oauth1/lib/class-wp-rest-oauth1.php on line 754 has wrong params order, it is:

protected function normalize_parameters( &$key, &$value ) { ... }

it should:

protected function normalize_parameters( &$value, $key ) { ... }

also there's no need to pass $key by reference and normalize it because any $key changes won't be made in array.

szaqal83 avatar Aug 26 '22 05:08 szaqal83

But if you change the order of the params in the function definition, don't you need to also change their order when calling the function?

Maybe in this case you don't, because of the way array_walk_recursive() works, but I can't quite make out why. The code is a bit overly clever :D

scottfennell-toptal avatar Aug 26 '22 12:08 scottfennell-toptal

No you don't need to change order of params passed because there is only one param ($params is an array).

szaqal83 avatar Aug 26 '22 12:08 szaqal83

Thank you for the response. It makes sense now.

As an aside -- I still use and depend on this plugin heavily. Obviously it is completely abandoned. I don't understand what the replacement for it was. Do you?

scottfennell-toptal avatar Aug 26 '22 13:08 scottfennell-toptal

I think this is the preferred way now https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/.

szaqal83 avatar Aug 29 '22 06:08 szaqal83