[wp-trac] [WordPress Trac] #25280: wp_localize_script unexpectedly convert numbers to strings

WordPress Trac noreply at wordpress.org
Wed Sep 11 16:15:53 UTC 2013


#25280: wp_localize_script unexpectedly convert numbers to strings
-----------------------------+------------------------------------
 Reporter:  adamsilverstein  |      Owner:
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  I18N             |    Version:  trunk
 Severity:  normal           |   Keywords:  has-patch dev-feedback
-----------------------------+------------------------------------
 Running wp_localize_script on an an array has the unexpected result of
 converting numbers into strings. Core WordPress files work around this
 issue by explicitly casting variables to int, for example in
 [http://core.trac.wordpress.org/browser/trunk/src/wp-
 admin/js/revisions.js#L36 revisions.js].

 Although it is easy to work around the issue (if you are expecting it),
 JavaScript is loosely typed and will merrily add numbers as strings
 without any warning - introducing potential
 [http://core.trac.wordpress.org/changeset/23247 bugs] when code that
 expects a number is instead passed a string. Properly passing numbers as
 numbers would resolve this issue.

 The root of the problem is that in PHP versions before 5.3.3, json_encode
 encoded numbers as strings. PHP 5.3.3 includes an option
 [http://php.net/manual/en/function.json-encode.php#refsect1-function.json-
 encode-changelog JSON_NUMERIC_CHECK] that eliminates this issue.

 For example & testing this bug, add the following code to your template's
 setup function:

 {{{
         wp_enqueue_script( 'localize_test', get_template_directory_uri() .
 '/localize_script.js', array(), '1.0.0', true );
         $array_to_localize = array(
                 'a_string' => __( 'Some String' ),
                 'a_number' => 10,
                 );

         wp_localize_script( 'localize_test', 'localized_variable',
 $array_to_localize );
 }}}

 Then add a file named localize_script.js in your theme folder containing
 the following code:
 {{{
 console.log(localized_variable);
 }}}

 Reloading shows that the number has been converted into a String
 ([http://cl.ly/image/243R3C3X0H2S console screenshot]).

 The attached patch corrects the issue by checking for the presence of the
 JSON_NUMERIC_CHECK constant and if present, passes it to json_encode -
 telling it to encode numbers as numbers and resulting in the expected
 behavior ([http://cl.ly/image/1V3O0v1T0R0D console screenshot]). I checked
 this under PHP 5.2.17 verifying that no errors are created (although the
 old behavior of converting numbers still occurs). Under PHP 5.3.3 the
 variable is now properly passed as a number. The
 [http://codex.wordpress.org/Function_Reference/wp_localize_script Codex
 documentation] needs to be updated if this patch is accepted.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25280>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list