From 47b11120d881576f1a47e9de0562671252723e22 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 13 Sep 2022 15:58:12 +0000 Subject: [PATCH] Code Modernization: Fix autovivification from `false` to `array` in `WP_Scripts::localize()`. This function was previously already problematic as it does not do proper input validation, and it has already received tweaks related to PHP 8.0 in [50408] / #52534, which also introduced a `_doing_it_wrong()` notice and added tests. The short of it is: * The function expects to receive an `array` for the `$l10n` parameter; * ...but silently supported the parameter being passed as a `string`; * ...and would expect PHP to gracefully handle everything else or throw appropriate warnings/errors. In the previous fix, a `_doing_it_wrong()` notice was added for any non-array inputs. The function would also cause a PHP native "Cannot use a scalar value as an array" warning (PHP < 8.0) or error (PHP 8.0+) for all scalar values, except `false`. PHP 8.1 deprecated autovivification from `false` to `array`, so now `false` starts throwing an "Automatic conversion of false to array is deprecated" notice. By rights, the function should just throw an exception when a non-array/string input is received, but that would be a backward compatibility break. So the current change will maintain the previous behavior, but will prevent both the "Cannot use a scalar value as an array" warning/error as well as the "Automatic conversion of false to array" deprecation notice for invalid inputs. Invalid inputs ''will'' still receive a `_doing_it_wrong()` notice, which is the reason this fix is considered acceptable. Includes: * Adding a test passing an empty array. * Adding a test to the data provider for a `null` input to make sure that the function will not throw a PHP 8.1 "passing null to non-nullable" notice. This solves the following PHP 8.1 test error: {{{ Tests_Dependencies_Scripts::test_wp_localize_script_data_formats with data set #8 (false, '[""]') Automatic conversion of false to array is deprecated /var/www/src/wp-includes/class.wp-scripts.php:514 /var/www/src/wp-includes/functions.wp-scripts.php:221 /var/www/tests/phpunit/tests/dependencies/scripts.php:1447 /var/www/vendor/bin/phpunit:123 }}} Reference: [https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.autovivification-false PHP Manual: PHP 8.1 Deprecations: Autovivification from false]. Follow-up to [7970], [18464], [18490], [19217], [50408]. Props jrf, costdev. See #55656. Built from https://develop.svn.wordpress.org/trunk@54142 git-svn-id: http://core.svn.wordpress.org/trunk@53701 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class.wp-scripts.php | 9 +++++++-- wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php index 649f086d60..f20964ead4 100644 --- a/wp-includes/class.wp-scripts.php +++ b/wp-includes/class.wp-scripts.php @@ -501,12 +501,17 @@ class WP_Scripts extends WP_Dependencies { ), '5.7.0' ); + + if ( false === $l10n ) { + // This should really not be needed, but is necessary for backward compatibility. + $l10n = array( $l10n ); + } } if ( is_string( $l10n ) ) { $l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' ); - } else { - foreach ( (array) $l10n as $key => $value ) { + } elseif ( is_array( $l10n ) ) { + foreach ( $l10n as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 498b9a5143..315e4b97d6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54141'; +$wp_version = '6.1-alpha-54142'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.