Skip to content

Commit 126faf0

Browse files
committed
PHP 8.1 | WP_Scripts::localize(): fix autovivification from false to array bug
This function was previously already problematic as it does not do proper input validation and the function already received tweaks related to PHP 8.0 in [50408] / Trac#52534, which also introduced the `_doing_it_wrong()` 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()` was added for all non-array inputs. The function would also cause a PHP native "Cannot use a scalar value as an array" warning (PHP < 8.0)/error (PHP 8.0) for all scalar values, except `false`. PHP 8.1 deprecated autovivification from `false` to `array`, so now `false` starts throwing a "Automatic conversion of false to array is deprecated" deprecation notice. By rights, the function should just throw an exception when a non-array/string input is received, but BC... So, while I really hate doing this, the current change will maintain the previous behaviour, 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()`, which is the only reason I find this fix even remotely acceptable. Includes adding a test passing an empty array. Includes adding an additional test to the data provider for a `null` input to safeguard the function will not throw a PHP 8.1 "passing null to non-nullable" notice. Fixes: ``` 3) 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 ``` Refs: * https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.autovivification-false
1 parent 0ad353d commit 126faf0

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/wp-includes/class.wp-scripts.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,17 @@ public function localize( $handle, $object_name, $l10n ) {
501501
),
502502
'5.7.0'
503503
);
504+
505+
if ( false === $l10n ) {
506+
// This should really not be needed, but BC...
507+
$l10n = array( $l10n );
508+
}
504509
}
505510

506511
if ( is_string( $l10n ) ) {
507512
$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
508-
} else {
509-
foreach ( (array) $l10n as $key => $value ) {
513+
} elseif ( is_array( $l10n ) ) {
514+
foreach ( $l10n as $key => $value ) {
510515
if ( ! is_scalar( $value ) ) {
511516
continue;
512517
}

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,17 +1428,8 @@ public function test_wp_enqueue_code_editor_when_simple_array_will_be_passed() {
14281428
*
14291429
* @param mixed $l10n_data Localization data passed to wp_localize_script().
14301430
* @param string $expected Expected transformation of localization data.
1431-
* @param string $warning Optional. Whether a PHP native warning/error is expected. Default false.
14321431
*/
1433-
public function test_wp_localize_script_data_formats( $l10n_data, $expected, $warning = false ) {
1434-
if ( $warning ) {
1435-
if ( PHP_VERSION_ID < 80000 ) {
1436-
$this->expectWarning();
1437-
} else {
1438-
$this->expectError();
1439-
}
1440-
}
1441-
1432+
public function test_wp_localize_script_data_formats( $l10n_data, $expected ) {
14421433
if ( ! is_array( $l10n_data ) ) {
14431434
$this->setExpectedIncorrectUsage( 'WP_Scripts::localize' );
14441435
}
@@ -1471,14 +1462,16 @@ public function data_wp_localize_script_data_formats() {
14711462
array( array( 'foo' => array( 'bar' => 'foobar' ) ), '{"foo":{"bar":"foobar"}}' ),
14721463
array( array( 'foo' => 6.6 ), '{"foo":"6.6"}' ),
14731464
array( array( 'foo' => 6 ), '{"foo":"6"}' ),
1465+
array( array(), '[]' ),
14741466

14751467
// Unofficially supported format.
14761468
array( 'string', '"string"' ),
14771469

14781470
// Unsupported formats.
1479-
array( 1.5, '1.5', true ),
1480-
array( 1, '1', true ),
1471+
array( 1.5, '1.5' ),
1472+
array( 1, '1' ),
14811473
array( false, '[""]' ),
1474+
array( null, 'null' ),
14821475
);
14831476
}
14841477

0 commit comments

Comments
 (0)