[wp-trac] [WordPress Trac] #52534: PHP 8: wp_localize_script() throws a warning if third parameter is a string.

WordPress Trac noreply at wordpress.org
Tue Feb 16 16:54:39 UTC 2021


#52534: PHP 8: wp_localize_script() throws a warning if third parameter is a
string.
-------------------------------------------+------------------------------
 Reporter:  peterwilsoncc                  |       Owner:  (none)
     Type:  defect (bug)                   |      Status:  new
 Priority:  normal                         |   Milestone:  Awaiting Review
Component:  Script Loader                  |     Version:
 Severity:  normal                         |  Resolution:
 Keywords:  php8 has-patch has-unit-tests  |     Focuses:
-------------------------------------------+------------------------------

Comment (by jrf):

 I have had another look and aside from what I said before, both currently
 proposed patches (as well as the one I proposed above) all constitute BC
 breaks in one way or another in the output to JS, which in turn can have
 side-effects and cause quite a lot of follow-on bugs.

 See: https://3v4l.org/ODNk5

 So realistically, I think that the only course of action open to us
 without introducing BC-breaks is to add a `_doing_it_wrong` warning and
 explicitly handle strings, but not change the behaviour in any way for
 other types.
 For any non-string, non-array input, that means there will be PHP
 notices/warnings, but as there will be a helpful "doing it wrong" and
 we're talking programmer errors anyway, I think that is acceptable.

 Something along the lines of:
 {{{#!php
 <?php
 if ( ! is_array( $l10n ) ) {
         // Throw the _doing_it_wrong_warning.

 }

 if ( is_string( $l10n ) ) {
         $l10n = html_entity_decode( (string) $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' );
         }
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52534#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list