mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-04 20:54:29 +00:00
Use json_encode() for adding script data (formerly l10n). Add the same functionality to WP_Styles for adding inline css after a stylesheet has been outputted. See #11520
git-svn-id: https://develop.svn.wordpress.org/trunk@18464 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -47,35 +47,42 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return $this->do_items( $handles, $group );
|
||||
}
|
||||
|
||||
// Deprecated since 3.3, see print_script_data()
|
||||
function print_scripts_l10n( $handle, $echo = true ) {
|
||||
if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
|
||||
_deprecated_function( __FUNCTION__, '3.3', 'print_script_data()' );
|
||||
return $this->print_script_data( $handle, $echo, true );
|
||||
}
|
||||
|
||||
function print_script_data( $handle, $echo = true, $_l10n = false ) {
|
||||
if ( empty($this->registered[$handle]->extra['data']) )
|
||||
return false;
|
||||
|
||||
$object_name = $this->registered[$handle]->extra['l10n'][0];
|
||||
if ( $_l10n ) {
|
||||
$name = $this->registered[$handle]->extra['l10n'][0];
|
||||
$data = $this->registered[$handle]->extra['l10n'][1];
|
||||
$after = '';
|
||||
|
||||
$data = "var $object_name = {\n";
|
||||
$eol = '';
|
||||
foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
|
||||
if ( 'l10n_print_after' == $var ) {
|
||||
$after = $val;
|
||||
continue;
|
||||
if ( is_array($data) && isset($data['l10n_print_after']) ) {
|
||||
$after = $data['l10n_print_after'];
|
||||
unset($data['l10n_print_after']);
|
||||
}
|
||||
$data .= "$eol\t$var: \"" . esc_js( $val ) . '"';
|
||||
$eol = ",\n";
|
||||
}
|
||||
$data .= "\n};\n";
|
||||
$data .= isset($after) ? "$after\n" : '';
|
||||
|
||||
if ( $echo ) {
|
||||
echo "<script type='text/javascript'>\n";
|
||||
echo "/* <![CDATA[ */\n";
|
||||
echo $data;
|
||||
echo "/* ]]> */\n";
|
||||
echo "</script>\n";
|
||||
return true;
|
||||
$output = "var $name = " . json_encode($data) . "; $after\n";
|
||||
} else {
|
||||
return $data;
|
||||
foreach ( (array) $this->registered[$handle]->extra['data'] as $name => $data ) {
|
||||
$output = "var $name = " . json_encode($data) . ";\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ( !$echo )
|
||||
return $output;
|
||||
|
||||
echo "<script type='text/javascript'>\n";
|
||||
echo "/* <![CDATA[ */\n"; // not needed in HTML 5
|
||||
echo $output;
|
||||
echo "\n/* ]]> */";
|
||||
echo "\n</script>\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function do_item( $handle, $group = false ) {
|
||||
@@ -103,7 +110,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
if ( $this->do_concat ) {
|
||||
$srce = apply_filters( 'script_loader_src', $src, $handle );
|
||||
if ( $this->in_default_dir($srce) ) {
|
||||
$this->print_code .= $this->print_scripts_l10n( $handle, false );
|
||||
$this->print_code .= $this->print_script_data( $handle, false );
|
||||
$this->concat .= "$handle,";
|
||||
$this->concat_version .= "$handle$ver";
|
||||
return true;
|
||||
@@ -113,7 +120,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
}
|
||||
}
|
||||
|
||||
$this->print_scripts_l10n( $handle );
|
||||
$this->print_script_data( $handle );
|
||||
if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
|
||||
$src = $this->base_url . $src;
|
||||
}
|
||||
@@ -131,19 +138,36 @@ class WP_Scripts extends WP_Dependencies {
|
||||
}
|
||||
|
||||
/**
|
||||
* Localizes a script
|
||||
* Localizes a script (Deprecated)
|
||||
*
|
||||
* Localizes only if script has already been added
|
||||
*
|
||||
* @param string $handle Script name
|
||||
* @param string $object_name Name of JS object to hold l10n info
|
||||
* @param array $l10n Array of JS var name => localized string
|
||||
* @return bool Successful localization
|
||||
* @since
|
||||
* @deprecated WP 3.3
|
||||
*/
|
||||
function localize( $handle, $object_name, $l10n ) {
|
||||
if ( !$object_name || !$l10n )
|
||||
_deprecated_function( __FUNCTION__, '3.3', 'add_script_data()' );
|
||||
return $this->add_script_data( $handle, $object_name, $l10n );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add extra Javascript
|
||||
*
|
||||
* Only if script has already been added.
|
||||
*
|
||||
* @param string $handle Script name
|
||||
* @param string $name Name of JS object to hold the data
|
||||
* @param array $data Associative array of JS name => value
|
||||
* @return bool Successful or not
|
||||
*/
|
||||
function add_script_data( $handle, $name, $data ) {
|
||||
if ( !$name || !is_array($data) )
|
||||
return false;
|
||||
return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
|
||||
|
||||
if ( !empty( $this->registered[$handle]->extra['data'][$name] ) )
|
||||
$data = array_merge( $data, (array) $this->registered[$handle]->extra['data'][$name] );
|
||||
|
||||
return $this->add_data( $handle, 'data', array( $name => $data ) );
|
||||
}
|
||||
|
||||
function set_group( $handle, $recursion, $group = false ) {
|
||||
|
||||
Reference in New Issue
Block a user