Make WordPress Core

Ticket #24813: 24813.7.diff

File 24813.7.diff, 4.8 KB (added by georgestephanis, 13 years ago)
  • tests/phpunit/tests/dependencies/styles.php

     
    8686                // Cleanup
    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         * @ticket 24813
     173         */
     174        public function test_plugin_doing_inline_styles_wrong() {
     175
     176                $style  = "<style type='text/css'>\n";
     177                $style .= ".thing {\n";
     178                $style .= "\tbackground: red;\n";
     179                $style .= "}\n";
     180                $style .= "</style>";
     181
     182                $expected  = "<link rel='stylesheet' id='handle-css'  href='http://example.com?ver=1' type='text/css' media='all' />\n";
     183                $expected .= "$style\n";
     184
     185                wp_enqueue_style( 'handle', 'http://example.com', array(), 1 );
     186
     187                // Suppress _doing_it_wrong error
     188                @wp_add_inline_style( 'handle', $style );
     189
     190                $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) );
     191
     192        }
     193
    89194}
  • src/wp-includes/class.wp-styles.php

     
    8787
    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 );
     92                        }
    9193                } else {
    9294                        echo $tag;
    93                         echo $this->print_inline_style( $handle, false );
     95                        $this->print_inline_style( $handle );
    9496                }
    9597
    9698                return true;
  • src/wp-includes/functions.wp-styles.php

     
    1414 * Passing an empty array to $handles prints the queue,
    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.
    2020 *
    … …  
    7171                $wp_styles = new WP_Styles();
    7272        }
    7373
     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 ) );
     77        }
     78
    7479        return $wp_styles->add_inline_style( $handle, $data );
    7580}
    7681