diff --git a/src/wp-includes/class.wp-styles.php b/src/wp-includes/class.wp-styles.php index 4a54b71676..927a7e7307 100644 --- a/src/wp-includes/class.wp-styles.php +++ b/src/wp-includes/class.wp-styles.php @@ -87,10 +87,11 @@ class WP_Styles extends WP_Dependencies { if ( $this->do_concat ) { $this->print_html .= $tag; - $this->print_html .= $this->print_inline_style( $handle, false ); + if ( $inline_style = $this->print_inline_style( $handle, false ) ) + $this->print_html .= sprintf( "\n", $inline_style ); } else { echo $tag; - echo $this->print_inline_style( $handle, false ); + $this->print_inline_style( $handle ); } return true; diff --git a/src/wp-includes/functions.wp-styles.php b/src/wp-includes/functions.wp-styles.php index c3569927fb..d97667cf88 100644 --- a/src/wp-includes/functions.wp-styles.php +++ b/src/wp-includes/functions.wp-styles.php @@ -14,7 +14,7 @@ * Passing an empty array to $handles prints the queue, * passing an array with one string prints that style, * and passing an array of strings prints those styles. - * + * * @see do_action() Calls 'wp_print_styles' hook. * @global WP_Styles $wp_styles The WP_Styles object for printing styles. * @@ -71,6 +71,11 @@ function wp_add_inline_style( $handle, $data ) { $wp_styles = new WP_Styles(); } + if ( false !== stripos( $data, '' ) ) { + _doing_it_wrong( __FUNCTION__, 'Do not pass #is', '$1', $data ) ); + } + return $wp_styles->add_inline_style( $handle, $data ); } diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 693130a897..a467718346 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -86,4 +86,124 @@ class Tests_Dependencies_Styles extends WP_UnitTestCase { // Cleanup $wp_styles->base_url = $base_url_backup; } + + /** + * Test if inline styles work + * @ticket 24813 + */ + public function test_inline_styles() { + + $style = ".thing {\n"; + $style .= "\tbackground: red;\n"; + $style .= "}"; + + $expected = "\n"; + $expected .= "\n"; + + wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); + wp_add_inline_style( 'handle', $style ); + + // No styles left to print + $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) ); + } + + /** + * Test if inline styles work with concatination + * @global WP_Styles $wp_styles + * @ticket 24813 + */ + public function test_inline_styles_concat() { + + global $wp_styles; + + $wp_styles->do_concat = true; + $wp_styles->default_dirs = array( '/wp-admin/', '/wp-includes/css/' ); // Default dirs as in wp-includes/script-loader.php + + $style = ".thing {\n"; + $style .= "\tbackground: red;\n"; + $style .= "}"; + + $expected = "\n"; + $expected .= "\n"; + + wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); + wp_add_inline_style( 'handle', $style ); + + wp_print_styles(); + $this->assertEquals( $expected, $wp_styles->print_html ); + + } + + /** + * Test if multiple inline styles work + * @ticket 24813 + */ + public function test_multiple_inline_styles() { + + $style1 = ".thing1 {\n"; + $style1 .= "\tbackground: red;\n"; + $style1 .= "}"; + + $style2 = ".thing2 {\n"; + $style2 .= "\tbackground: blue;\n"; + $style2 .= "}"; + + $expected = "\n"; + $expected .= "\n"; + + wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); + wp_add_inline_style( 'handle', $style1 ); + wp_add_inline_style( 'handle', $style2 ); + + // No styles left to print + $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) ); + + } + + /** + * Test if a plugin doing it the wrong way still works + * + * @expectedIncorrectUsage wp_add_inline_style + * @ticket 24813 + */ + public function test_plugin_doing_inline_styles_wrong() { + + $style = ""; + + $expected = "\n"; + $expected .= "$style\n"; + + wp_enqueue_style( 'handle', 'http://example.com', array(), 1 ); + + wp_add_inline_style( 'handle', $style ); + + $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) ); + + } + + /** + * Test to make sure