Make WordPress Core

Changeset 25786


Ignore:
Timestamp:
10/15/2013 02:34:33 PM (12 years ago)
Author:
nacin
Message:

Revert [25202] and enforce that wp_add_inline_style() does not want <style> tags.

Prior to 3.7, these tags were not printed (and thus needed to be provided), but only in the admin and when concatenation was enabled. They should never be required. Strip them when we find them and issue a notice for incorrect usage.

props atimmer, georgestephanis.
fixes #24813.

Location:
trunk
Files:
3 edited

Legend:

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

    r25202 r25786  
    8888        if ( $this->do_concat ) {
    8989            $this->print_html .= $tag;
    90             $this->print_html .= $this->print_inline_style( $handle, false );
     90            if ( $inline_style = $this->print_inline_style( $handle, false ) )
     91                $this->print_html .= sprintf( "<style type='text/css'>\n%s\n</style>\n", $inline_style );
    9192        } else {
    9293            echo $tag;
    93             echo $this->print_inline_style( $handle, false );
     94            $this->print_inline_style( $handle );
    9495        }
    9596
  • trunk/src/wp-includes/functions.wp-styles.php

    r25594 r25786  
    1515 * passing an array with one string prints that style,
    1616 * and passing an array of strings prints those styles.
    17  * 
     17 *
    1818 * @see do_action() Calls 'wp_print_styles' hook.
    1919 * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
     
    7070                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
    7171        $wp_styles = new WP_Styles();
     72    }
     73
     74    if ( false !== stripos( $data, '</style>' ) ) {
     75        _doing_it_wrong( __FUNCTION__, 'Do not pass <style> tags to wp_add_inline_style()', '3.7' );
     76        $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
    7277    }
    7378
  • trunk/tests/phpunit/tests/dependencies/styles.php

    r25376 r25786  
    8787        $wp_styles->base_url = $base_url_backup;
    8888    }
     89
     90    /**
     91     * Test if inline styles work
     92     * @ticket 24813
     93     */
     94    public function test_inline_styles() {
     95
     96        $style  = ".thing {\n";
     97        $style .= "\tbackground: red;\n";
     98        $style .= "}";
     99       
     100        $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     101        $expected .= "<style type='text/css'>\n";
     102        $expected .= "$style\n";
     103        $expected .= "</style>\n";
     104
     105        wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     106        wp_add_inline_style( 'handle', $style );
     107
     108        // No styles left to print
     109        $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) );
     110    }
     111
     112    /**
     113     * Test if inline styles work with concatination
     114     * @global WP_Styles $wp_styles
     115     * @ticket 24813
     116     */
     117    public function test_inline_styles_concat() {
     118
     119        global $wp_styles;
     120
     121        $wp_styles->do_concat = true;
     122        $wp_styles->default_dirs = array( '/wp-admin/', '/wp-includes/css/' ); // Default dirs as in wp-includes/script-loader.php
     123
     124        $style  = ".thing {\n";
     125        $style .= "\tbackground: red;\n";
     126        $style .= "}";
     127
     128        $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     129        $expected .= "<style type='text/css'>\n";
     130        $expected .= "$style\n";
     131        $expected .= "</style>\n";
     132
     133        wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     134        wp_add_inline_style( 'handle', $style );
     135
     136        wp_print_styles();
     137        $this->assertEquals( $expected, $wp_styles->print_html );
     138
     139    }
     140
     141    /**
     142     * Test if multiple inline styles work
     143     * @ticket 24813
     144     */
     145    public function test_multiple_inline_styles() {
     146
     147        $style1  = ".thing1 {\n";
     148        $style1 .= "\tbackground: red;\n";
     149        $style1 .= "}";
     150       
     151        $style2  = ".thing2 {\n";
     152        $style2 .= "\tbackground: blue;\n";
     153        $style2 .= "}";
     154
     155        $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     156        $expected .= "<style type='text/css'>\n";
     157        $expected .= "$style1\n";
     158        $expected .= "$style2\n";
     159        $expected .= "</style>\n";
     160
     161        wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     162        wp_add_inline_style( 'handle', $style1 );
     163        wp_add_inline_style( 'handle', $style2 );
     164
     165        // No styles left to print
     166        $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) );
     167
     168    }
     169
     170    /**
     171     * Test if a plugin doing it the wrong way still works
     172     *
     173     * @expectedIncorrectUsage wp_add_inline_style
     174     * @ticket 24813
     175     */
     176    public function test_plugin_doing_inline_styles_wrong() {
     177
     178        $style  = "<style type='text/css'>\n";
     179        $style .= ".thing {\n";
     180        $style .= "\tbackground: red;\n";
     181        $style .= "}\n";
     182        $style .= "</style>";
     183
     184        $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     185        $expected .= "$style\n";
     186
     187        wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     188
     189        wp_add_inline_style( 'handle', $style );
     190
     191        $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) );
     192
     193    }
     194
     195    /**
     196     * Test to make sure <style> tags aren't output if there are no inline styles.
     197     * @ticket 24813
     198     */
     199    public function test_unnecessary_style_tags() {
     200
     201        $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     202
     203        wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     204
     205        $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) );
     206
     207    }
     208
    89209}
Note: See TracChangeset for help on using the changeset viewer.