[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 15:44:31 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 SergeyBiryukov):

 I tend to agree with @jrf, if both #25280 and #29722 were a `wontfix`, it
 seems like a better `_doing_it_wrong()` message instead of a somewhat
 obscure warning would be the preferred path here.

 My concern with the current patch is that it attempts to work around the
 issue for strings, but unnecessarily breaks backward compatibility for
 integers, converting them to an array instead:
 {{{
 1) Tests_Dependencies_Scripts::test_wp_localize_script_data_formats with
 data set #2 (1, '"1"')
 Failed asserting that two strings are identical.
 --- Expected
 +++ Actual
 @@ @@
  '<script type='text/javascript' id='test-example-js-extra'>
  /* <![CDATA[ */
 -var testExample = "1";
 +var testExample = ["1"];
  /* ]]> */
  </script>
  <script type='text/javascript' src='http://example.com' id='test-example-
 js'></script>
  '
 }}}

 Previously, as pointed out in the tickets above, passing an integer also
 caused a PHP warning, but otherwise worked as expected, though the value
 was ultimately converted to a string.

 It seems to me that both strings and integers should be handled
 consistently here, either by replacing the `is_string()` check with
 `is_scalar()`, to match the check within the loop and in
 `_WP_Dependency::add_data()`, or explicitly checking for `! is_array()`,
 which might be even clearer.

 See [attachment:"52534.diff"], which adds a test case for an integer, and
 also a clear `_doing_it_wrong()` message pointing to
 `wp_add_inline_script()` as the alternative.

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


More information about the wp-trac mailing list