Add support for IE conditional comments for WP_Scripts to match the functionality of WP_Styles, including unit tests. Props filosofo, aaroncampbell, ethitter, georgestephanis, valendesigns. Fixes #16024.

Built from https://develop.svn.wordpress.org/trunk@31223


git-svn-id: http://core.svn.wordpress.org/trunk@31204 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2015-01-17 01:37:22 +00:00
parent f609d9d89a
commit 9d3ea188d7
3 changed files with 53 additions and 10 deletions

View File

@ -95,15 +95,25 @@ class WP_Scripts extends WP_Dependencies {
if ( false === $group && in_array($handle, $this->in_footer, true) )
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
if ( null === $this->registered[$handle]->ver )
$obj = $this->registered[$handle];
if ( null === $obj->ver ) {
$ver = '';
else
$ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
} else {
$ver = $obj->ver ? $obj->ver : $this->default_version;
}
if ( isset($this->args[$handle]) )
$ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
$src = $this->registered[$handle]->src;
$src = $obj->src;
$cond_before = $cond_after = '';
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
if ( $conditional ) {
$cond_before = "<!--[if {$conditional}]>\n";
$cond_after = "<![endif]-->\n";
}
if ( $this->do_concat ) {
/**
@ -115,7 +125,7 @@ class WP_Scripts extends WP_Dependencies {
* @param string $handle Script handle.
*/
$srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir($srce) ) {
if ( $this->in_default_dir( $srce ) && ! $conditional ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
@ -126,13 +136,24 @@ class WP_Scripts extends WP_Dependencies {
}
}
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
if ( $has_conditional_data ) {
echo $cond_before;
}
$this->print_extra_script( $handle );
if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
if ( $has_conditional_data ) {
echo $cond_after;
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
if ( !empty($ver) )
$src = add_query_arg('ver', $ver, $src);
if ( ! empty( $ver ) )
$src = add_query_arg( 'ver', $ver, $src );
/** This filter is documented in wp-includes/class.wp-scripts.php */
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
@ -140,7 +161,7 @@ class WP_Scripts extends WP_Dependencies {
if ( ! $src )
return true;
$tag = "<script type='text/javascript' src='$src'></script>\n";
$tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
/**
* Filter the HTML script tag of an enqueued script.

View File

@ -271,3 +271,25 @@ function wp_script_is( $handle, $list = 'enqueued' ) {
return (bool) wp_scripts()->query( $handle, $list );
}
/**
* Add metadata to a script.
*
* Works only if the script has already been added.
*
* Possible values for $key and $value:
* 'conditional' string Comments for IE 6, lte IE 7, etc.
*
* @see WP_Dependency::add_data()
*
* @since 4.2.0
*
* @param string $handle Name of the script.
* @param string $key Name of data point for which we're storing a value.
* @param mixed $value String containing the data to be added.
* @return bool True on success, false on failure.
*/
function wp_script_add_data( $handle, $key, $value ){
global $wp_scripts;
return $wp_scripts->add_data( $handle, $key, $value );
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31222';
$wp_version = '4.2-alpha-31223';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.