WordPress.org

Make WordPress Core

Ticket #16024: 16024.7.diff

File 16024.7.diff, 3.8 KB (added by valendesigns, 5 years ago)
  • src/wp-includes/class.wp-scripts.php

    diff --git src/wp-includes/class.wp-scripts.php src/wp-includes/class.wp-scripts.php
    index fbb8b22..a831ff9 100644
    class WP_Scripts extends WP_Dependencies { 
    9595                if ( false === $group && in_array($handle, $this->in_footer, true) )
    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 = '';
    100102                else
    101                         $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
     103                        $ver = $obj->ver ? $obj->ver : $this->default_version;
    102104
    103105                if ( isset($this->args[$handle]) )
    104106                        $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
    105107
    106                 $src = $this->registered[$handle]->src;
     108                $src = $obj->src;
     109                $cond_before = $cond_after = '';
     110                $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
     111
     112                if ( $conditional ) {
     113                        $cond_before = "<!--[if {$conditional}]>\n";
     114                        $cond_after = "\n<![endif]-->";
     115                }
    107116
    108117                if ( $this->do_concat ) {
    109118                        /**
    class WP_Scripts extends WP_Dependencies { 
    115124                         * @param string $handle Script handle.
    116125                         */
    117126                        $srce = apply_filters( 'script_loader_src', $src, $handle );
    118                         if ( $this->in_default_dir($srce) ) {
     127                        if ( $this->in_default_dir( $srce ) && ! $conditional ) {
    119128                                $this->print_code .= $this->print_extra_script( $handle, false );
    120129                                $this->concat .= "$handle,";
    121130                                $this->concat_version .= "$handle$ver";
    class WP_Scripts extends WP_Dependencies { 
    140149                if ( ! $src )
    141150                        return true;
    142151
    143                 $tag = "<script type='text/javascript' src='$src'></script>\n";
     152                $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>{$cond_after}\n";
    144153
    145154                /**
    146155                 * Filter the HTML script tag of an enqueued script.
  • src/wp-includes/functions.wp-scripts.php

    diff --git src/wp-includes/functions.wp-scripts.php src/wp-includes/functions.wp-scripts.php
    index 6eb7a44..1a77df8 100644
    function wp_script_is( $handle, $list = 'enqueued' ) { 
    258258
    259259        return (bool) $wp_scripts->query( $handle, $list );
    260260}
     261
     262
     263/**
     264 * Add metadata to a script.
     265 *
     266 * Works only if the script has already been added.
     267 *
     268 * Possible values for $key and $value:
     269 * 'conditional' string Comments for IE 6, lte IE 7, etc.
     270 *
     271 * @see WP_Dependency::add_data()
     272 *
     273 * @since 4.2.0
     274 *
     275 * @param string $handle Name of the script.
     276 * @param string $key    Name of data point for which we're storing a value.
     277 * @param mixed  $value  String containing the data to be added.
     278 * @return bool True on success, false on failure.
     279 */
     280function wp_script_add_data( $handle, $key, $value ){
     281        global $wp_scripts;
     282        return $wp_scripts->add_data( $handle, $key, $value );
     283}
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php
    index 3f4e724..783cf00 100644
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    8484                // Cleanup
    8585                $wp_scripts->base_url = $base_url_backup;
    8686        }
     87
     88        /**
     89         * Test Conditional Comments
     90         * @ticket 10624
     91         */
     92        function test_wp_script_add_data() {
     93                // Enqueue & add conditional comments
     94                wp_enqueue_script( 'twentytwelve-html5', 'example.com', array(), '1.0' );
     95                wp_script_add_data( 'twentytwelve-html5', 'conditional', 'lt IE 9' );
     96                $expected = "<!--[if lt IE 9]>\n<script type='text/javascript' src='http://example.com?ver=1.0'></script>\n<![endif]-->\n";
     97
     98                // Go!
     99                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     100
     101                // No scripts left to print
     102                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
     103        }
    87104}