[wp-trac] [WordPress Trac] #57405: wp_localize_script() doesn't decode html entities in nested arrays
WordPress Trac
noreply at wordpress.org
Fri Dec 30 09:27:39 UTC 2022
#57405: wp_localize_script() doesn't decode html entities in nested arrays
-------------------------+-----------------------------
Reporter: rilwis | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: I18N | Version:
Severity: normal | Keywords:
Focuses: javascript |
-------------------------+-----------------------------
If we localize a script with an array like this:
{{{
$data = [
'key1' => 'Value 1',
'key2' => [
'subkey1' => 'String contains ' single quote encoded',
'subkey2' => 'String contains " double quote encoded',
],
];
// Encoded strings stay encoded.
wp_localize_script( 'myhandle', 'myname', $data );
}}}
Then the outputted values of `$data` on the front end is exactly the same,
e.g. encoded strings stay encoded:
{{{
array(2) {
["key1"]=>
string(7) "Value 1"
["key2"]=>
array(2) {
["subkey1"]=>
string(43) "String contains ' single quote encoded"
["subkey2"]=>
string(43) "String contains " double quote encoded"
}
}
}}}
However, `wp_localize_script()` decode strings correctly if the array is
not nested. So this code works correctly;
{{{
$data = [
'key1' => 'Value 1',
'key2_1' => 'String contains ' single quote encoded',
'key2_2' => 'String contains " double quote encoded',
];
// Encoded strings are decoded.
wp_localize_script( 'myhandle', 'myname', $data );
}}}
Output:
{{{
array(3) {
["key1"]=>
string(7) "Value 1"
["key2_1"]=>
string(38) "String contains ' single quote encoded"
["key2_2"]=>
string(38) "String contains " double quote encoded"
}
}}}
This inconsistent behavior might cause issues when JS outputs/uses the
text directly.
I'd suggest making the `wp_localize_script` function decoded strings in
all cases.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/57405>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list