OAuth1
OAuth1 copied to clipboard
PHP 8.0 warning thrown by normalize_parameters
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
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 ) ); }
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
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.
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
No you don't need to change order of params passed because there is only one param ($params is an array).
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?
I think this is the preferred way now https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/.