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: http://svn.automattic.com/wordpress/trunk@18464 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-07-25 00:36:06 +00:00
parent adbcddd6eb
commit 21058cf460
9 changed files with 233 additions and 153 deletions

View File

@ -125,7 +125,10 @@ foreach( $load as $handle ) {
$content .= get_file($rtl_path) . "\n"; $content .= get_file($rtl_path) . "\n";
} }
$out .= str_replace( '../images/', 'images/', $content ); if ( strpos( $style->src, '/wp-includes/css/' ) === 0 )
$out .= str_replace( '../images/', '../wp-includes/images/', $content );
else
$out .= str_replace( '../images/', 'images/', $content );
} }
header('Content-Type: text/css'); header('Content-Type: text/css');

View File

@ -47,35 +47,42 @@ class WP_Scripts extends WP_Dependencies {
return $this->do_items( $handles, $group ); return $this->do_items( $handles, $group );
} }
// Deprecated since 3.3, see print_script_data()
function print_scripts_l10n( $handle, $echo = true ) { 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; 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"; if ( is_array($data) && isset($data['l10n_print_after']) ) {
$eol = ''; $after = $data['l10n_print_after'];
foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) { unset($data['l10n_print_after']);
if ( 'l10n_print_after' == $var ) {
$after = $val;
continue;
} }
$data .= "$eol\t$var: \"" . esc_js( $val ) . '"'; $output = "var $name = " . json_encode($data) . "; $after\n";
$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;
} else { } 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 ) { function do_item( $handle, $group = false ) {
@ -103,7 +110,7 @@ class WP_Scripts extends WP_Dependencies {
if ( $this->do_concat ) { if ( $this->do_concat ) {
$srce = apply_filters( 'script_loader_src', $src, $handle ); $srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir($srce) ) { 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 .= "$handle,";
$this->concat_version .= "$handle$ver"; $this->concat_version .= "$handle$ver";
return true; 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) ) ) { if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
$src = $this->base_url . $src; $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 * Localizes only if script has already been added
* *
* @param string $handle Script name * @since
* @param string $object_name Name of JS object to hold l10n info * @deprecated WP 3.3
* @param array $l10n Array of JS var name => localized string
* @return bool Successful localization
*/ */
function localize( $handle, $object_name, $l10n ) { 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 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 ) { function set_group( $handle, $recursion, $group = false ) {

View File

@ -25,6 +25,7 @@ class WP_Styles extends WP_Dependencies {
var $concat_version = ''; var $concat_version = '';
var $do_concat = false; var $do_concat = false;
var $print_html = ''; var $print_html = '';
var $print_code = '';
var $default_dirs; var $default_dirs;
function __construct() { function __construct() {
@ -35,44 +36,49 @@ class WP_Styles extends WP_Dependencies {
if ( !parent::do_item($handle) ) if ( !parent::do_item($handle) )
return false; return false;
if ( null === $this->registered[$handle]->ver ) $obj = $this->registered[$handle];
if ( null === $obj->ver )
$ver = ''; $ver = '';
else else
$ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version; $ver = $obj->ver ? $obj->ver : $this->default_version;
if ( isset($this->args[$handle]) ) if ( isset($this->args[$handle]) )
$ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle]; $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
if ( $this->do_concat ) { if ( $this->do_concat ) {
if ( $this->in_default_dir($this->registered[$handle]->src) && !isset($this->registered[$handle]->extra['conditional']) && !isset($this->registered[$handle]->extra['alt']) ) { if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
$this->concat .= "$handle,"; $this->concat .= "$handle,";
$this->concat_version .= "$handle$ver"; $this->concat_version .= "$handle$ver";
if ( !empty($this->registered[$handle]->extra['data']) )
$this->print_code .= $this->registered[$handle]->extra['data'];
return true; return true;
} }
} }
if ( isset($this->registered[$handle]->args) ) if ( isset($obj->args) )
$media = esc_attr( $this->registered[$handle]->args ); $media = esc_attr( $obj->args );
else else
$media = 'all'; $media = 'all';
$href = $this->_css_href( $this->registered[$handle]->src, $ver, $handle ); $href = $this->_css_href( $obj->src, $ver, $handle );
$rel = isset($this->registered[$handle]->extra['alt']) && $this->registered[$handle]->extra['alt'] ? 'alternate stylesheet' : 'stylesheet'; $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
$title = isset($this->registered[$handle]->extra['title']) ? "title='" . esc_attr( $this->registered[$handle]->extra['title'] ) . "'" : ''; $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
$end_cond = $tag = ''; $end_cond = $tag = '';
if ( isset($this->registered[$handle]->extra['conditional']) && $this->registered[$handle]->extra['conditional'] ) { if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
$tag .= "<!--[if {$this->registered[$handle]->extra['conditional']}]>\n"; $tag .= "<!--[if {$obj->extra['conditional']}]>\n";
$end_cond = "<![endif]-->\n"; $end_cond = "<![endif]-->\n";
} }
$tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle ); $tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
if ( 'rtl' === $this->text_direction && isset($this->registered[$handle]->extra['rtl']) && $this->registered[$handle]->extra['rtl'] ) { if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
if ( is_bool( $this->registered[$handle]->extra['rtl'] ) ) { if ( is_bool( $obj->extra['rtl'] ) ) {
$suffix = isset( $this->registered[$handle]->extra['suffix'] ) ? $this->registered[$handle]->extra['suffix'] : ''; $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
$rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $this->registered[$handle]->src , $ver, "$handle-rtl" )); $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
} else { } else {
$rtl_href = $this->_css_href( $this->registered[$handle]->extra['rtl'], $ver, "$handle-rtl" ); $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
} }
$tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n", $handle ); $tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n", $handle );
@ -80,16 +86,39 @@ class WP_Styles extends WP_Dependencies {
$tag .= $end_cond; $tag .= $end_cond;
if ( $this->do_concat ) if ( $this->do_concat ) {
$this->print_html .= $tag; $this->print_html .= $tag;
else $this->print_html .= $this->print_inline_style( $handle, false );
} else {
echo $tag; echo $tag;
$this->print_inline_style( $handle );
}
// Could do something with $this->registered[$handle]->extra here to print out extra CSS rules return true;
// echo "<style type='text/css'>\n"; }
// echo "/* <![CDATA[ */\n";
// echo "/* ]]> */\n"; function add_inline_style( $handle, $data ) {
// echo "</style>\n"; if ( !$data )
return false;
if ( !empty( $this->registered[$handle]->extra['data'] ) )
$data .= "\n" . $this->registered[$handle]->extra['data'];
return $this->add_data( $handle, 'data', $data );
}
function print_inline_style( $handle, $echo = true ) {
if ( empty($this->registered[$handle]->extra['data']) )
return false;
$output = $this->registered[$handle]->extra['data'];
if ( !$echo )
return $output;
echo "<style type='text/css'>\n";
echo "$output\n";
echo "</style>\n";
return true; return true;
} }

View File

@ -2615,3 +2615,18 @@ function wp_timezone_supported() {
return true; return true;
} }
/**
* Localizes a script.
*
* Localizes only if script has already been added.
*
* @since r16
* @deprecated WP 3.3
* @see wp_add_script_data()
*/
function wp_localize_script( $handle, $object_name, $l10n ) {
_deprecated_function( __FUNCTION__, '3.3', 'wp_add_script_data()' );
return wp_add_script_data( $handle, $object_name, $l10n );
}

View File

@ -26,7 +26,7 @@ function wp_print_scripts( $handles = false ) {
global $wp_scripts; global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') ) { if ( !is_a($wp_scripts, 'WP_Scripts') ) {
if ( !$handles ) if ( !$handles )
return array(); // No need to instantiate if nothing's there. return array(); // No need to instantiate if nothing is there.
else else
$wp_scripts = new WP_Scripts(); $wp_scripts = new WP_Scripts();
} }
@ -35,7 +35,7 @@ function wp_print_scripts( $handles = false ) {
} }
/** /**
* Register new JavaScript file. * Register new Javascript file.
* *
* @since r16 * @since r16
* @param string $handle Script name * @param string $handle Script name
@ -56,19 +56,29 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_f
} }
/** /**
* Localizes a script. * Adds extra Javascript.
* *
* Localizes only if script has already been added. * Works only if the script has already been added.
* * Accepts an associative array $data and creates JS object:
* @since r16 * "$name" = {
* @see WP_Scripts::localize() * key: value,
* key: value,
* ...
* }
* The $name is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
* The $data array is JSON encoded. If called more than once for the same $handle, with the same $name,
* the object would contain all values. In that case if two or more keys are the same,
* the first value is kept and subsequent values are ignored.
*
* @since 3.3
* @see WP_Scripts::add_script_data()
*/ */
function wp_localize_script( $handle, $object_name, $l10n ) { function wp_add_script_data( $handle, $name, $data ) {
global $wp_scripts; global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') ) if ( !is_a($wp_scripts, 'WP_Scripts') )
return false; return false;
return $wp_scripts->localize( $handle, $object_name, $l10n ); return $wp_scripts->add_script_data( $handle, $name, $data );
} }
/** /**

View File

@ -25,7 +25,7 @@ function wp_print_styles( $handles = false ) {
global $wp_styles; global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') ) { if ( !is_a($wp_styles, 'WP_Styles') ) {
if ( !$handles ) if ( !$handles )
return array(); // No need to instantiate if nothing's there. return array(); // No need to instantiate if nothing is there.
else else
$wp_styles = new WP_Styles(); $wp_styles = new WP_Styles();
} }
@ -33,6 +33,23 @@ function wp_print_styles( $handles = false ) {
return $wp_styles->do_items( $handles ); return $wp_styles->do_items( $handles );
} }
/**
* Adds extra CSS.
*
* Works only if the stylesheet has already been added.
* Accepts a string $data containing the CSS.
*
* @since 3.3
* @see WP_Scripts::add_inline_style()
*/
function wp_add_inline_style( $handle, $data ) {
global $wp_styles;
if ( !is_a($wp_styles, 'WP_Styles') )
return false;
return $wp_styles->add_inline_style( $handle, $data );
}
/** /**
* Register CSS style file. * Register CSS style file.
* *

View File

@ -1,23 +0,0 @@
//Used to ensure that Entities used in L10N strings are correct
function convertEntities(o) {
var c, v;
c = function(s) {
if (/&[^;]+;/.test(s)) {
var e = document.createElement("div");
e.innerHTML = s;
return !e.firstChild ? s : e.firstChild.nodeValue;
}
return s;
}
if ( typeof o === 'string' ) {
return c(o);
} else if ( typeof o === 'object' ) {
for (v in o) {
if ( typeof o[v] === 'string' ) {
o[v] = c(o[v]);
}
}
}
return o;
}

View File

@ -1 +0,0 @@
function convertEntities(b){var d,a;d=function(c){if(/&[^;]+;/.test(c)){var f=document.createElement("div");f.innerHTML=c;return !f.firstChild?c:f.firstChild.nodeValue}return c};if(typeof b==="string"){return d(b)}else{if(typeof b==="object"){for(a in b){if(typeof b[a]==="string"){b[a]=d(b[a])}}}}return b};

View File

@ -58,17 +58,12 @@ function wp_default_scripts( &$scripts ) {
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : ''; $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
// Always ensure that we have the convertEntities function
$scripts->add( 'l10n', "/wp-includes/js/l10n$suffix.js", false, '20101110' );
$scripts->enqueue( 'l10n' );
$scripts->add( 'utils', "/wp-admin/js/utils$suffix.js", false, '20101110' ); $scripts->add( 'utils', "/wp-admin/js/utils$suffix.js", false, '20101110' );
$scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), '20110711' ); $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), '20110711' );
$scripts->add_data( 'common', 'group', 1 ); $scripts->add_data( 'common', 'group', 1 );
$scripts->localize( 'common', 'commonL10n', array( $scripts->add_script_data( 'common', 'commonL10n', array(
'warnDelete' => __("You are about to permanently delete the selected items.\n 'Cancel' to stop, 'OK' to delete."), 'warnDelete' => __("You are about to permanently delete the selected items.\n 'Cancel' to stop, 'OK' to delete.")
'l10n_print_after' => 'try{convertEntities(commonL10n);}catch(e){};'
) ); ) );
$scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1' ); $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1' );
@ -76,7 +71,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110502' ); $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110502' );
$scripts->add_data( 'quicktags', 'group', 1 ); $scripts->add_data( 'quicktags', 'group', 1 );
$scripts->localize( 'quicktags', 'quicktagsL10n', array( $scripts->add_script_data( 'quicktags', 'quicktagsL10n', array(
'quickLinks' => __('(Quick Links)'), 'quickLinks' => __('(Quick Links)'),
'wordLookup' => __('Enter a word to look up:'), 'wordLookup' => __('Enter a word to look up:'),
'dictionaryLookup' => esc_attr(__('Dictionary lookup')), 'dictionaryLookup' => esc_attr(__('Dictionary lookup')),
@ -87,8 +82,7 @@ function wp_default_scripts( &$scripts ) {
'enterImageURL' => __('Enter the URL of the image'), 'enterImageURL' => __('Enter the URL of the image'),
'enterImageDescription' => __('Enter a description of the image'), 'enterImageDescription' => __('Enter a description of the image'),
'fullscreen' => __('fullscreen'), 'fullscreen' => __('fullscreen'),
'toggleFullscreen' => esc_attr( __('Toggle fullscreen mode') ), 'toggleFullscreen' => esc_attr( __('Toggle fullscreen mode') )
'l10n_print_after' => 'try{convertEntities(quicktagsL10n);}catch(e){};'
) ); ) );
$scripts->add( 'colorpicker', "/wp-includes/js/colorpicker$suffix.js", array('prototype'), '3517m' ); $scripts->add( 'colorpicker', "/wp-includes/js/colorpicker$suffix.js", array('prototype'), '3517m' );
@ -103,10 +97,9 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'wp-ajax-response', "/wp-includes/js/wp-ajax-response$suffix.js", array('jquery'), '20091119' ); $scripts->add( 'wp-ajax-response', "/wp-includes/js/wp-ajax-response$suffix.js", array('jquery'), '20091119' );
$scripts->add_data( 'wp-ajax-response', 'group', 1 ); $scripts->add_data( 'wp-ajax-response', 'group', 1 );
$scripts->localize( 'wp-ajax-response', 'wpAjax', array( $scripts->add_script_data( 'wp-ajax-response', 'wpAjax', array(
'noPerm' => __('You do not have permission to do that.'), 'noPerm' => __('You do not have permission to do that.'),
'broken' => __('An unidentified error has occurred.'), 'broken' => __('An unidentified error has occurred.')
'l10n_print_after' => 'try{convertEntities(wpAjax);}catch(e){};'
) ); ) );
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20110524' ); $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20110524' );
@ -192,7 +185,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'thickbox', "/wp-includes/js/thickbox/thickbox.js", array('jquery'), '3.1-20110528'); $scripts->add( 'thickbox', "/wp-includes/js/thickbox/thickbox.js", array('jquery'), '3.1-20110528');
$scripts->add_data( 'thickbox', 'group', 1 ); $scripts->add_data( 'thickbox', 'group', 1 );
$scripts->localize( 'thickbox', 'thickboxL10n', array( $scripts->add_script_data( 'thickbox', 'thickboxL10n', array(
'next' => __('Next &gt;'), 'next' => __('Next &gt;'),
'prev' => __('&lt; Prev'), 'prev' => __('&lt; Prev'),
'image' => __('Image'), 'image' => __('Image'),
@ -200,8 +193,7 @@ function wp_default_scripts( &$scripts ) {
'close' => __('Close'), 'close' => __('Close'),
'noiframes' => __('This feature requires inline frames. You have iframes disabled or your browser does not support them.'), 'noiframes' => __('This feature requires inline frames. You have iframes disabled or your browser does not support them.'),
'loadingAnimation' => includes_url('js/thickbox/loadingAnimation.gif'), 'loadingAnimation' => includes_url('js/thickbox/loadingAnimation.gif'),
'closeImage' => includes_url('js/thickbox/tb-close.png'), 'closeImage' => includes_url('js/thickbox/tb-close.png')
'l10n_print_after' => 'try{convertEntities(thickboxL10n);}catch(e){};'
) ); ) );
$scripts->add( 'jcrop', "/wp-includes/js/jcrop/jquery.Jcrop$suffix.js", array('jquery'), '0.9.8-20110113'); $scripts->add( 'jcrop', "/wp-includes/js/jcrop/jquery.Jcrop$suffix.js", array('jquery'), '0.9.8-20110113');
@ -225,7 +217,7 @@ function wp_default_scripts( &$scripts ) {
if ( empty($max_upload_size) ) if ( empty($max_upload_size) )
$max_upload_size = __('not configured'); $max_upload_size = __('not configured');
// these error messages came from the sample swfupload js, they might need changing. // these error messages came from the sample swfupload js, they might need changing.
$scripts->localize( 'swfupload-handlers', 'swfuploadL10n', array( $scripts->add_script_data( 'swfupload-handlers', 'swfuploadL10n', array(
'queue_limit_exceeded' => __('You have attempted to queue too many files.'), 'queue_limit_exceeded' => __('You have attempted to queue too many files.'),
'file_exceeds_size_limit' => __('This file exceeds the maximum upload size for this site.'), 'file_exceeds_size_limit' => __('This file exceeds the maximum upload size for this site.'),
'zero_byte_file' => __('This file is empty. Please try another.'), 'zero_byte_file' => __('This file is empty. Please try another.'),
@ -242,8 +234,7 @@ function wp_default_scripts( &$scripts ) {
'dismiss' => __('Dismiss'), 'dismiss' => __('Dismiss'),
'crunching' => __('Crunching&hellip;'), 'crunching' => __('Crunching&hellip;'),
'deleted' => __('moved to the trash.'), 'deleted' => __('moved to the trash.'),
'error_uploading' => __('&#8220;%s&#8221; has failed to upload due to an error'), 'error_uploading' => __('&#8220;%s&#8221; has failed to upload due to an error')
'l10n_print_after' => 'try{convertEntities(swfuploadL10n);}catch(e){};',
) ); ) );
$scripts->add( 'comment-reply', "/wp-includes/js/comment-reply$suffix.js", false, '20090102'); $scripts->add( 'comment-reply', "/wp-includes/js/comment-reply$suffix.js", false, '20090102');
@ -255,15 +246,14 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array('jquery'), '20101027' ); $scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array('jquery'), '20101027' );
$scripts->add_data( 'password-strength-meter', 'group', 1 ); $scripts->add_data( 'password-strength-meter', 'group', 1 );
$scripts->localize( 'password-strength-meter', 'pwsL10n', array( $scripts->add_script_data( 'password-strength-meter', 'pwsL10n', array(
'empty' => __('Strength indicator'), 'empty' => __('Strength indicator'),
'short' => __('Very weak'), 'short' => __('Very weak'),
'bad' => __('Weak'), 'bad' => __('Weak'),
/* translators: password strength */ /* translators: password strength */
'good' => _x('Medium', 'password strength'), 'good' => _x('Medium', 'password strength'),
'strong' => __('Strong'), 'strong' => __('Strong'),
'mismatch' => __('Mismatch'), 'mismatch' => __('Mismatch')
'l10n_print_after' => 'try{convertEntities(pwsL10n);}catch(e){};'
) ); ) );
$scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter' ), '20110628' ); $scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter' ), '20110628' );
@ -274,13 +264,12 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'wplink', "/wp-includes/js/tinymce/plugins/wplink/js/wplink$suffix.js", array( 'jquery', 'wpdialogs' ), '20110528' ); $scripts->add( 'wplink', "/wp-includes/js/tinymce/plugins/wplink/js/wplink$suffix.js", array( 'jquery', 'wpdialogs' ), '20110528' );
$scripts->add_data( 'wplink', 'group', 1 ); $scripts->add_data( 'wplink', 'group', 1 );
$scripts->localize( 'wplink', 'wpLinkL10n', array( $scripts->add_script_data( 'wplink', 'wpLinkL10n', array(
'title' => __('Insert/edit link'), 'title' => __('Insert/edit link'),
'update' => __('Update'), 'update' => __('Update'),
'save' => __('Add Link'), 'save' => __('Add Link'),
'noTitle' => __('(no title)'), 'noTitle' => __('(no title)'),
'noMatchesFound' => __('No matches found.'), 'noMatchesFound' => __('No matches found.')
'l10n_print_after' => 'try{convertEntities(wpLinkL10n);}catch(e){};',
) ); ) );
$scripts->add( 'wpdialogs', "/wp-includes/js/tinymce/plugins/wpdialogs/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), '20110528' ); $scripts->add( 'wpdialogs', "/wp-includes/js/tinymce/plugins/wpdialogs/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), '20110528' );
@ -292,10 +281,9 @@ function wp_default_scripts( &$scripts ) {
if ( is_admin() ) { if ( is_admin() ) {
$scripts->add( 'ajaxcat', "/wp-admin/js/cat$suffix.js", array( 'wp-lists' ), '20090102' ); $scripts->add( 'ajaxcat', "/wp-admin/js/cat$suffix.js", array( 'wp-lists' ), '20090102' );
$scripts->add_data( 'ajaxcat', 'group', 1 ); $scripts->add_data( 'ajaxcat', 'group', 1 );
$scripts->localize( 'ajaxcat', 'catL10n', array( $scripts->add_script_data( 'ajaxcat', 'catL10n', array(
'add' => esc_attr(__('Add')), 'add' => esc_attr(__('Add')),
'how' => __('Separate multiple categories with commas.'), 'how' => __('Separate multiple categories with commas.')
'l10n_print_after' => 'try{convertEntities(catL10n);}catch(e){};'
) ); ) );
$scripts->add( 'admin-categories', "/wp-admin/js/categories$suffix.js", array('wp-lists'), '20091201' ); $scripts->add( 'admin-categories', "/wp-admin/js/categories$suffix.js", array('wp-lists'), '20091201' );
@ -303,10 +291,9 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array('jquery', 'wp-ajax-response'), '20110429' ); $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array('jquery', 'wp-ajax-response'), '20110429' );
$scripts->add_data( 'admin-tags', 'group', 1 ); $scripts->add_data( 'admin-tags', 'group', 1 );
$scripts->localize( 'admin-tags', 'tagsl10n', array( $scripts->add_script_data( 'admin-tags', 'tagsl10n', array(
'noPerm' => __('You do not have permission to do that.'), 'noPerm' => __('You do not have permission to do that.'),
'broken' => __('An unidentified error has occurred.'), 'broken' => __('An unidentified error has occurred.')
'l10n_print_after' => 'try{convertEntities(tagsl10n);}catch(e){};'
)); ));
$scripts->add( 'admin-custom-fields', "/wp-admin/js/custom-fields$suffix.js", array('wp-lists'), '20110429' ); $scripts->add( 'admin-custom-fields', "/wp-admin/js/custom-fields$suffix.js", array('wp-lists'), '20110429' );
@ -314,7 +301,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array('wp-lists', 'jquery-ui-resizable', 'quicktags', 'jquery-query'), '20110602' ); $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array('wp-lists', 'jquery-ui-resizable', 'quicktags', 'jquery-query'), '20110602' );
$scripts->add_data( 'admin-comments', 'group', 1 ); $scripts->add_data( 'admin-comments', 'group', 1 );
$scripts->localize( 'admin-comments', 'adminCommentsL10n', array( $scripts->add_script_data( 'admin-comments', 'adminCommentsL10n', array(
'hotkeys_highlight_first' => isset($_GET['hotkeys_highlight_first']), 'hotkeys_highlight_first' => isset($_GET['hotkeys_highlight_first']),
'hotkeys_highlight_last' => isset($_GET['hotkeys_highlight_last']), 'hotkeys_highlight_last' => isset($_GET['hotkeys_highlight_last']),
'replyApprove' => __( 'Approve and Reply' ), 'replyApprove' => __( 'Approve and Reply' ),
@ -329,7 +316,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox'), '20110524' ); $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox'), '20110524' );
$scripts->add_data( 'post', 'group', 1 ); $scripts->add_data( 'post', 'group', 1 );
$scripts->localize( 'post', 'postL10n', array( $scripts->add_script_data( 'post', 'postL10n', array(
'tagsUsed' => __('Tags used on this post:'), 'tagsUsed' => __('Tags used on this post:'),
'add' => esc_attr(__('Add')), 'add' => esc_attr(__('Add')),
'addTag' => esc_attr(__('Add new Tag')), 'addTag' => esc_attr(__('Add new Tag')),
@ -352,8 +339,7 @@ function wp_default_scripts( &$scripts ) {
'publicSticky' => __('Public, Sticky'), 'publicSticky' => __('Public, Sticky'),
'password' => __('Password Protected'), 'password' => __('Password Protected'),
'privatelyPublished' => __('Privately Published'), 'privatelyPublished' => __('Privately Published'),
'published' => __('Published'), 'published' => __('Published')
'l10n_print_after' => 'try{convertEntities(postL10n);}catch(e){};'
) ); ) );
$scripts->add( 'link', "/wp-admin/js/link$suffix.js", array('wp-lists', 'postbox'), '20110524' ); $scripts->add( 'link', "/wp-admin/js/link$suffix.js", array('wp-lists', 'postbox'), '20110524' );
@ -361,11 +347,10 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'comment', "/wp-admin/js/comment$suffix.js", array('jquery'), '20110429' ); $scripts->add( 'comment', "/wp-admin/js/comment$suffix.js", array('jquery'), '20110429' );
$scripts->add_data( 'comment', 'group', 1 ); $scripts->add_data( 'comment', 'group', 1 );
$scripts->localize( 'comment', 'commentL10n', array( $scripts->add_script_data( 'comment', 'commentL10n', array(
'cancel' => __('Cancel'), 'cancel' => __('Cancel'),
'edit' => __('Edit'), 'edit' => __('Edit'),
'submittedOn' => __('Submitted on:'), 'submittedOn' => __('Submitted on:')
'l10n_print_after' => 'try{convertEntities(commentL10n);}catch(e){};'
) ); ) );
$scripts->add( 'admin-gallery', "/wp-admin/js/gallery$suffix.js", array( 'jquery-ui-sortable' ), '20110414' ); $scripts->add( 'admin-gallery', "/wp-admin/js/gallery$suffix.js", array( 'jquery-ui-sortable' ), '20110414' );
@ -387,26 +372,23 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'inline-edit-post', "/wp-admin/js/inline-edit-post$suffix.js", array( 'jquery', 'suggest' ), '20110609' ); $scripts->add( 'inline-edit-post', "/wp-admin/js/inline-edit-post$suffix.js", array( 'jquery', 'suggest' ), '20110609' );
$scripts->add_data( 'inline-edit-post', 'group', 1 ); $scripts->add_data( 'inline-edit-post', 'group', 1 );
$scripts->localize( 'inline-edit-post', 'inlineEditL10n', array( $scripts->add_script_data( 'inline-edit-post', 'inlineEditL10n', array(
'error' => __('Error while saving the changes.'), 'error' => __('Error while saving the changes.'),
'ntdeltitle' => __('Remove From Bulk Edit'), 'ntdeltitle' => __('Remove From Bulk Edit'),
'notitle' => __('(no title)'), 'notitle' => __('(no title)')
'l10n_print_after' => 'try{convertEntities(inlineEditL10n);}catch(e){};'
) ); ) );
$scripts->add( 'inline-edit-tax', "/wp-admin/js/inline-edit-tax$suffix.js", array( 'jquery' ), '20110609' ); $scripts->add( 'inline-edit-tax', "/wp-admin/js/inline-edit-tax$suffix.js", array( 'jquery' ), '20110609' );
$scripts->add_data( 'inline-edit-tax', 'group', 1 ); $scripts->add_data( 'inline-edit-tax', 'group', 1 );
$scripts->localize( 'inline-edit-tax', 'inlineEditL10n', array( $scripts->add_script_data( 'inline-edit-tax', 'inlineEditL10n', array(
'error' => __('Error while saving the changes.'), 'error' => __('Error while saving the changes.')
'l10n_print_after' => 'try{convertEntities(inlineEditL10n);}catch(e){};'
) ); ) );
$scripts->add( 'plugin-install', "/wp-admin/js/plugin-install$suffix.js", array( 'jquery', 'thickbox' ), '20110113' ); $scripts->add( 'plugin-install', "/wp-admin/js/plugin-install$suffix.js", array( 'jquery', 'thickbox' ), '20110113' );
$scripts->add_data( 'plugin-install', 'group', 1 ); $scripts->add_data( 'plugin-install', 'group', 1 );
$scripts->localize( 'plugin-install', 'plugininstallL10n', array( $scripts->add_script_data( 'plugin-install', 'plugininstallL10n', array(
'plugin_information' => __('Plugin Information:'), 'plugin_information' => __('Plugin Information:'),
'ays' => __('Are you sure you want to install this plugin?'), 'ays' => __('Are you sure you want to install this plugin?')
'l10n_print_after' => 'try{convertEntities(plugininstallL10n);}catch(e){};'
) ); ) );
$scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' ); $scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' );
@ -427,21 +409,19 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), '20100518' ); $scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), '20100518' );
$scripts->add_data( 'set-post-thumbnail', 'group', 1 ); $scripts->add_data( 'set-post-thumbnail', 'group', 1 );
$scripts->localize( 'set-post-thumbnail', 'setPostThumbnailL10n', array( $scripts->add_script_data( 'set-post-thumbnail', 'setPostThumbnailL10n', array(
'setThumbnail' => __( 'Use as featured image' ), 'setThumbnail' => __( 'Use as featured image' ),
'saving' => __( 'Saving...' ), 'saving' => __( 'Saving...' ),
'error' => __( 'Could not set that as the thumbnail image. Try a different attachment.' ), 'error' => __( 'Could not set that as the thumbnail image. Try a different attachment.' ),
'done' => __( 'Done' ), 'done' => __( 'Done' )
'l10n_print_after' => 'try{convertEntities(setPostThumbnailL10n);}catch(e){};'
) ); ) );
// Navigation Menus // Navigation Menus
$scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", array('jquery-ui-sortable'), '20110524' ); $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", array('jquery-ui-sortable'), '20110524' );
$scripts->localize( 'nav-menu', 'navMenuL10n', array( $scripts->add_script_data( 'nav-menu', 'navMenuL10n', array(
'noResultsFound' => _x('No results found.', 'search results'), 'noResultsFound' => _x('No results found.', 'search results'),
'warnDeleteMenu' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ), 'warnDeleteMenu' => __( "You are about to permanently delete this menu. \n 'Cancel' to stop, 'OK' to delete." ),
'saveAlert' => __('The changes you made will be lost if you navigate away from this page.'), 'saveAlert' => __('The changes you made will be lost if you navigate away from this page.')
'l10n_print_after' => 'try{convertEntities(navMenuL10n);}catch(e){};'
) ); ) );
$scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), '20110511' ); $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), '20110511' );
@ -474,7 +454,7 @@ function wp_default_styles( &$styles ) {
$styles->content_url = defined('WP_CONTENT_URL')? WP_CONTENT_URL : ''; $styles->content_url = defined('WP_CONTENT_URL')? WP_CONTENT_URL : '';
$styles->default_version = get_bloginfo( 'version' ); $styles->default_version = get_bloginfo( 'version' );
$styles->text_direction = function_exists( 'is_rtl' ) && is_rtl() ? 'rtl' : 'ltr'; $styles->text_direction = function_exists( 'is_rtl' ) && is_rtl() ? 'rtl' : 'ltr';
$styles->default_dirs = array('/wp-admin/', '/wp-includes/'); $styles->default_dirs = array('/wp-admin/', '/wp-includes/css/');
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : ''; $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
@ -558,11 +538,10 @@ function wp_prototype_before_jquery( $js_array ) {
*/ */
function wp_just_in_time_script_localization() { function wp_just_in_time_script_localization() {
wp_localize_script( 'autosave', 'autosaveL10n', array( wp_add_script_data( 'autosave', 'autosaveL10n', array(
'autosaveInterval' => AUTOSAVE_INTERVAL, 'autosaveInterval' => AUTOSAVE_INTERVAL,
'savingText' => __('Saving Draft&#8230;'), 'savingText' => __('Saving Draft&#8230;'),
'saveAlert' => __('The changes you made will be lost if you navigate away from this page.'), 'saveAlert' => __('The changes you made will be lost if you navigate away from this page.')
'l10n_print_after' => 'try{convertEntities(autosaveL10n);}catch(e){};'
) ); ) );
} }
@ -635,7 +614,6 @@ function print_head_scripts() {
$wp_scripts = new WP_Scripts(); $wp_scripts = new WP_Scripts();
script_concat_settings(); script_concat_settings();
$wp_scripts->do_items( 'l10n' );
$wp_scripts->do_concat = $concatenate_scripts; $wp_scripts->do_concat = $concatenate_scripts;
$wp_scripts->do_head_items(); $wp_scripts->do_head_items();
@ -647,16 +625,13 @@ function print_head_scripts() {
} }
/** /**
* Prints the scripts that were queued for the footer on admin pages. * Prints the scripts that were queued for the footer or too late for the HTML head.
* *
* @since 2.8 * @since 2.8
*/ */
function print_footer_scripts() { function print_footer_scripts() {
global $wp_scripts, $concatenate_scripts; global $wp_scripts, $concatenate_scripts;
if ( ! did_action('wp_print_footer_scripts') )
do_action('wp_print_footer_scripts');
if ( !is_a($wp_scripts, 'WP_Scripts') ) if ( !is_a($wp_scripts, 'WP_Scripts') )
return array(); // No need to run if not instantiated. return array(); // No need to run if not instantiated.
@ -671,6 +646,9 @@ function print_footer_scripts() {
return $wp_scripts->done; return $wp_scripts->done;
} }
/**
* @internal use
*/
function _print_scripts() { function _print_scripts() {
global $wp_scripts, $compress_scripts; global $wp_scripts, $compress_scripts;
@ -681,8 +659,8 @@ function _print_scripts() {
if ( !empty($wp_scripts->concat) ) { if ( !empty($wp_scripts->concat) ) {
if ( !empty($wp_scripts->print_code) ) { if ( !empty($wp_scripts->print_code) ) {
echo "<script type='text/javascript'>\n"; echo "\n<script type='text/javascript'>\n";
echo "/* <![CDATA[ */\n"; echo "/* <![CDATA[ */\n"; // not needed in HTML 5
echo $wp_scripts->print_code; echo $wp_scripts->print_code;
echo "/* ]]> */\n"; echo "/* ]]> */\n";
echo "</script>\n"; echo "</script>\n";
@ -718,13 +696,17 @@ function wp_print_head_scripts() {
} }
/** /**
* Prints the scripts that were queued for the footer on the front end. * Hooks to print the scripts and styles in the footer.
* *
* @since 2.8 * @since 2.8
*/ */
function wp_print_footer_scripts() { function wp_print_footer_scripts() {
print_late_styles(); print_late_styles();
print_footer_scripts(); print_footer_scripts();
if ( !is_admin() )
do_action('wp_print_footer_scripts');
return true; return true;
} }
@ -740,6 +722,11 @@ function wp_enqueue_scripts() {
do_action('wp_enqueue_scripts'); do_action('wp_enqueue_scripts');
} }
/**
* Prints the styles queue in the HTML head on admin pages.
*
* @since 2.8
*/
function print_admin_styles() { function print_admin_styles() {
global $wp_styles, $concatenate_scripts, $compress_css; global $wp_styles, $concatenate_scripts, $compress_css;
@ -761,6 +748,11 @@ function print_admin_styles() {
return $wp_styles->done; return $wp_styles->done;
} }
/**
* Prints the styles that were queued too late for the HTML head.
*
* @since 3.3
*/
function print_late_styles() { function print_late_styles() {
global $wp_styles, $concatenate_scripts; global $wp_styles, $concatenate_scripts;
@ -777,6 +769,9 @@ function print_late_styles() {
return $wp_styles->done; return $wp_styles->done;
} }
/**
* @internal use
*/
function _print_styles() { function _print_styles() {
global $wp_styles, $compress_css; global $wp_styles, $compress_css;
@ -789,12 +784,23 @@ function _print_styles() {
$ver = md5("$wp_styles->concat_version{$dir}"); $ver = md5("$wp_styles->concat_version{$dir}");
$href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&load=" . trim($wp_styles->concat, ', ') . "&ver=$ver"; $href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&load=" . trim($wp_styles->concat, ', ') . "&ver=$ver";
echo "<link rel='stylesheet' href='" . esc_attr($href) . "' type='text/css' media='all' />\n"; echo "<link rel='stylesheet' href='" . esc_attr($href) . "' type='text/css' media='all' />\n";
if ( !empty($wp_styles->print_code) ) {
echo "<style type='text/css'>\n";
echo $wp_styles->print_code;
echo "\n</style>\n";
}
} }
if ( !empty($wp_styles->print_html) ) if ( !empty($wp_styles->print_html) )
echo $wp_styles->print_html; echo $wp_styles->print_html;
} }
/**
* Determine the concatenation and compression settings for scripts and styles.
*
* @since 2.8
*/
function script_concat_settings() { function script_concat_settings() {
global $concatenate_scripts, $compress_scripts, $compress_css; global $concatenate_scripts, $compress_scripts, $compress_css;