Tribe__Assets::filter_add_localization_data( string $tag, string $handle )
Handles adding localization data, when attached to script_loader_tag which allows dependencies to load in their localization data as well.
Contents
Parameters
- $tag
-
(string) (Required) Tag we are filtering.
- $handle
-
(string) (Required) Which is the ID/Handle of the tag we are about to print.
Return
(string) Script tag with the localization variable HTML attached to it.
Source
File: src/Tribe/Assets.php
public function filter_add_localization_data( $tag, $handle ) {
// Only filter for own own filters.
if ( ! $asset = $this->get( $handle ) ) {
return $tag;
}
// Bail when not dealing with JS assets.
if ( 'js' !== $asset->type ) {
return $tag;
}
// Only localize on JS and if we have data.
if ( empty( $asset->localize ) ) {
return $tag;
}
global $wp_scripts;
// Makes sure we have an Array of Localize data.
if ( is_object( $asset->localize ) ) {
$localization = [ $asset->localize ];
} else {
$localization = (array) $asset->localize;
}
/**
* Check to ensure we haven't already localized it before.
*
* @since 4.5.8
*/
foreach ( $localization as $localize ) {
if ( in_array( $localize->name, $this->localized ) ) {
continue;
}
// If we have a Callable as the Localize data we execute it.
if ( is_callable( $localize->data ) ) {
$localize->data = call_user_func( $localize->data, $asset );
}
wp_localize_script( $asset->slug, $localize->name, $localize->data );
$this->localized[] = $localize->name;
}
// Fetch the HTML for all the localized data.
ob_start();
$wp_scripts->print_extra_script( $asset->slug, true );
$localization_html = ob_get_clean();
// After printing it remove data;|
$wp_scripts->add_data( $asset->slug, 'data', '' );
return $localization_html . $tag;
}
Changelog
| Version | Description |
|---|---|
| 4.13.0 | Introduced. |