From 032aec8df2bf2ebdfd46dd19c6df5dd470a9ee4b Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 22 Feb 2021 23:23:07 +0000 Subject: [PATCH] Script Loader: Prevent `wp_localize_script()` warnings. Prevent `wp_localize_script()` (via `WP_Scripts::localize()`) throwing warnings in PHP 8 when the translation data is passed as a string. This maintains backward compatibility with earlier versions of PHP. Introduce a `_doing_it_wrong()` notice to `WP_Scripts::localize()` if the translation data is not passed as an array. Props jrf, peterwilsoncc, SergeyBiryukov. Fixes #52534. Built from https://develop.svn.wordpress.org/trunk@50408 git-svn-id: http://core.svn.wordpress.org/trunk@50019 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class.wp-scripts.php | 27 ++++++++++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php index a629248df4..4d7bd9e1b2 100644 --- a/wp-includes/class.wp-scripts.php +++ b/wp-includes/class.wp-scripts.php @@ -484,12 +484,29 @@ class WP_Scripts extends WP_Dependencies { unset( $l10n['l10n_print_after'] ); } - foreach ( (array) $l10n as $key => $value ) { - if ( ! is_scalar( $value ) ) { - continue; - } + if ( ! is_array( $l10n ) ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: 1: $l10n, 2: wp_add_inline_script() */ + __( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ), + '$l10n', + 'wp_add_inline_script()' + ), + '5.7.0' + ); + } - $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); + if ( is_string( $l10n ) ) { + $l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' ); + } else { + foreach ( (array) $l10n as $key => $value ) { + if ( ! is_scalar( $value ) ) { + continue; + } + + $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); + } } $script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; diff --git a/wp-includes/version.php b/wp-includes/version.php index 38d737fa55..e579cecae4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.7-beta3-50407'; +$wp_version = '5.7-beta3-50408'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.