Make WordPress Core


Ignore:
Timestamp:
02/22/2021 11:21:56 PM (6 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r49758 r50408  
    485485                }
    486486
    487                 foreach ( (array) $l10n as $key => $value ) {
    488                         if ( ! is_scalar( $value ) ) {
    489                                 continue;
     487                if ( ! is_array( $l10n ) ) {
     488                        _doing_it_wrong(
     489                                __METHOD__,
     490                                sprintf(
     491                                        /* translators: 1: $l10n, 2: wp_add_inline_script() */
     492                                        __( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
     493                                        '<code>$l10n</code>',
     494                                        '<code>wp_add_inline_script()</code>'
     495                                ),
     496                                '5.7.0'
     497                        );
     498                }
     499
     500                if ( is_string( $l10n ) ) {
     501                        $l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' );
     502                } else {
     503                        foreach ( (array) $l10n as $key => $value ) {
     504                                if ( ! is_scalar( $value ) ) {
     505                                        continue;
     506                                }
     507
     508                                $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    490509                        }
    491 
    492                         $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
    493510                }
    494511
Note: See TracChangeset for help on using the changeset viewer.