Opened 14 months ago
Last modified 14 months ago
#20558 new enhancement
allow wp_localize_script data to be added to existing objects
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Performance | Version: | 3.3 |
| Severity: | minor | Keywords: | dev-feedback reporter-feedback 2nd-opinion |
| Cc: |
Description
Re: WP_Scripts::localize() located in wp-includes/class.wp-scripts.php
Currently when WP_Scripts::localize() handles the printing of wp_localize_script data to JavaScript, it starts the string with a var declaration, like this:
$script = "var $object_name = " . json_encode($l10n) . ';';
Because this is printed in the global scope, it becomes a global variable regardless of whether it's preceded by var. As far as JavaScript is concerned the above string would be equivalent to:
$script = $object_name . ' = ' . json_encode($l10n) . ';';
or
$script = 'this.' . $object_name . ' = ' . json_encode($l10n) . ';';
or
$script = 'window.' . $object_name . ' = ' . json_encode($l10n) . ';';
But I suppose it's possible thru hooks to make it so that the localization data prints outside of the global scope, in which case you might want the var to be there (if it we're wrapped in a closure). So I think the overall best solution would to check if the $object_name contains a period . character. If it does, omit the var. In other words, make it so that:
wp_localize_script('myplugin', 'myPluginData', $object )
would print:
var myPluginData = {...};
but that:
`wp_localize_script('myplugin', 'myPlugin.data', $object )`
would print:
myPlugin.data = {...};
By default the localization data runs before any enqueued scripts, in which case myPlugin would not yet be defined, but we should leave that for the JavaScript dev work out. My point is that the flexiblity should be there. Another route would be to apply a filter on that line but I don't think a filter is necessary if the above change is made.
Change History (3)
comment:2
follow-up:
↓ 3
scribu
— 14 months ago
Another future consideration would be if an $object_name is not provided, then expose the data to a single global object called wordpress
How would that help? Plugins would still have to prefix their options.
comment:3
in reply to:
↑ 2
ryanve
— 14 months ago
Replying to scribu:
@scribu The less pollution of JavaScript's global namespace, the better, both for performance and for avoiding conflicts. The script handles already provide a unique identifier for each script. Having a single object could also make it easier to integrate modular code along the lines of http://requirejs.org
Another future consideration would be if an $object_name is not provided, then expose the data to a single global object called wordpress:
window.wordpress = {};so that wp_localize_script($handle, false, $object) would do
You'd have to use bracket notation to accomodate stuff like
wordpress["jquery-ui-core"] = { ... };Should probably do sanitize_key($handle) first to be safe.