WordPress.org

Make WordPress Core

Changeset 31223


Ignore:
Timestamp:
01/17/2015 01:36:55 AM (4 years ago)
Author:
azaozz
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r31189 r31223  
    9696            $this->in_footer = array_diff( $this->in_footer, (array) $handle );
    9797
    98         if ( null === $this->registered[$handle]->ver )
     98        $obj = $this->registered[$handle];
     99
     100        if ( null === $obj->ver ) {
    99101            $ver = '';
    100         else
    101             $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
     102        } else {
     103            $ver = $obj->ver ? $obj->ver : $this->default_version;
     104        }
    102105
    103106        if ( isset($this->args[$handle]) )
    104107            $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
    105108
    106         $src = $this->registered[$handle]->src;
     109        $src = $obj->src;
     110        $cond_before = $cond_after = '';
     111        $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
     112
     113        if ( $conditional ) {
     114            $cond_before = "<!--[if {$conditional}]>\n";
     115            $cond_after = "<![endif]-->\n";
     116        }
    107117
    108118        if ( $this->do_concat ) {
     
    116126             */
    117127            $srce = apply_filters( 'script_loader_src', $src, $handle );
    118             if ( $this->in_default_dir($srce) ) {
     128            if ( $this->in_default_dir( $srce ) && ! $conditional ) {
    119129                $this->print_code .= $this->print_extra_script( $handle, false );
    120130                $this->concat .= "$handle,";
     
    127137        }
    128138
     139        $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
     140
     141        if ( $has_conditional_data ) {
     142            echo $cond_before;
     143        }
     144
    129145        $this->print_extra_script( $handle );
    130         if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
     146
     147        if ( $has_conditional_data ) {
     148            echo $cond_after;
     149        }
     150
     151        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
    131152            $src = $this->base_url . $src;
    132153        }
    133154
    134         if ( !empty($ver) )
    135             $src = add_query_arg('ver', $ver, $src);
     155        if ( ! empty( $ver ) )
     156            $src = add_query_arg( 'ver', $ver, $src );
    136157
    137158        /** This filter is documented in wp-includes/class.wp-scripts.php */
     
    141162            return true;
    142163
    143         $tag = "<script type='text/javascript' src='$src'></script>\n";
     164        $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
    144165
    145166        /**
  • trunk/src/wp-includes/functions.wp-scripts.php

    r31202 r31223  
    272272    return (bool) wp_scripts()->query( $handle, $list );
    273273}
     274
     275/**
     276 * Add metadata to a script.
     277 *
     278 * Works only if the script has already been added.
     279 *
     280 * Possible values for $key and $value:
     281 * 'conditional' string Comments for IE 6, lte IE 7, etc.
     282 *
     283 * @see WP_Dependency::add_data()
     284 *
     285 * @since 4.2.0
     286 *
     287 * @param string $handle Name of the script.
     288 * @param string $key    Name of data point for which we're storing a value.
     289 * @param mixed  $value  String containing the data to be added.
     290 * @return bool True on success, false on failure.
     291 */
     292function wp_script_add_data( $handle, $key, $value ){
     293    global $wp_scripts;
     294    return $wp_scripts->add_data( $handle, $key, $value );
     295}
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r30517 r31223  
    8585        $wp_scripts->base_url = $base_url_backup;
    8686    }
     87
     88    /**
     89     * Testing add data & conditional
     90     * @ticket 16024
     91     */
     92    function test_wp_script_add_data() {
     93        // Enqueue & add data
     94        wp_enqueue_script( 'test-only-data', 'example.com', array(), null );
     95        wp_script_add_data( 'test-only-data', 'data', 'testing' );
     96        $expected = "<script type='text/javascript'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n";
     97        $expected.= "<script type='text/javascript' src='http://example.com'></script>\n";
     98
     99        // Enqueue & add conditional comments
     100        wp_enqueue_script( 'test-only-conditional', 'example.com', array(), null );
     101        wp_script_add_data( 'test-only-conditional', 'conditional', 'gt IE 7' );
     102        $expected.= "<!--[if gt IE 7]>\n<script type='text/javascript' src='http://example.com'></script>\n<![endif]-->\n";
     103
     104        // Enqueue & add data plus conditional comments for both
     105        wp_enqueue_script( 'test-conditional-with-data', 'example.com', array(), null );
     106        wp_script_add_data( 'test-conditional-with-data', 'data', 'testing' );
     107        wp_script_add_data( 'test-conditional-with-data', 'conditional', 'lt IE 9' );
     108        $expected.= "<!--[if lt IE 9]>\n<script type='text/javascript'>\n/* <![CDATA[ */\ntesting\n/* ]]> */\n</script>\n<![endif]-->\n";
     109        $expected.= "<!--[if lt IE 9]>\n<script type='text/javascript' src='http://example.com'></script>\n<![endif]-->\n";
     110
     111        // Enqueue & add an invalid key for brevity
     112        wp_enqueue_script( 'test-invalid', 'example.com', array(), null );
     113        wp_script_add_data( 'test-invalid', 'invalid', 'testing' );
     114        $expected.= "<script type='text/javascript' src='http://example.com'></script>\n";
     115
     116        // Go!
     117        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     118
     119        // No scripts left to print
     120        $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
     121    }
    87122}
Note: See TracChangeset for help on using the changeset viewer.