Skip to content

Commit 295efcc

Browse files
committed
Ensure stylesheets of zero size can be inlined
1 parent 9c7273c commit 295efcc

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/wp-includes/script-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3081,7 +3081,7 @@ function wp_maybe_inline_styles() {
30813081
$path = $wp_styles->get_data( $handle, 'path' );
30823082
if ( $path && $src ) {
30833083
$size = wp_filesize( $path );
3084-
if ( 0 === $size ) {
3084+
if ( 0 === $size && ! file_exists( $path ) ) {
30853085
_doing_it_wrong(
30863086
__FUNCTION__,
30873087
sprintf(

tests/phpunit/tests/dependencies/styles.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,13 @@ public function test_wp_maybe_inline_styles_bad_path_at_file_size_check() {
794794
* @expectedIncorrectUsage wp_maybe_inline_styles
795795
*/
796796
public function test_wp_maybe_inline_styles_bad_path_with_file_size_provided() {
797+
$inc_path = '/css/ancient-themes.css'; // Does not exist.
798+
797799
// This ensures the initial file size check is bypassed.
798800
add_filter(
799801
'pre_wp_filesize',
800-
static function ( $size, $path ) {
801-
if ( str_contains( $path, 'ancient-themes.css' ) ) {
802+
static function ( $size, $path ) use ( $inc_path ) {
803+
if ( str_contains( $path, $inc_path ) ) {
802804
$size = 1000;
803805
}
804806
return $size;
@@ -807,9 +809,8 @@ static function ( $size, $path ) {
807809
2
808810
);
809811

810-
$handle = 'test-handle';
811-
$inc_path = '/css/ancient-themes.css'; // Does not exist.
812-
$url = '/' . WPINC . $inc_path;
812+
$handle = 'test-handle';
813+
$url = '/' . WPINC . $inc_path;
813814
wp_register_style( $handle, $url );
814815
wp_style_add_data( $handle, 'path', ABSPATH . WPINC . $inc_path );
815816
wp_enqueue_style( $handle );
@@ -819,6 +820,37 @@ static function ( $size, $path ) {
819820
$this->assertSame( $GLOBALS['wp_styles']->registered[ $handle ]->src, $url );
820821
}
821822

823+
/**
824+
* @ticket 64447
825+
*
826+
* @covers ::wp_maybe_inline_styles
827+
*/
828+
public function test_wp_maybe_inline_styles_good_path_with_zero_file_size_provided() {
829+
$inc_path = '/css/classic-themes.css';
830+
831+
// This simulates the file having a zero size.
832+
add_filter(
833+
'pre_wp_filesize',
834+
static function ( $size, $path ) use ( $inc_path ) {
835+
if ( str_contains( $path, $inc_path ) ) {
836+
$size = 0;
837+
}
838+
return $size;
839+
},
840+
10,
841+
2
842+
);
843+
844+
$handle = 'test-handle';
845+
wp_register_style( $handle, '/' . WPINC . $inc_path );
846+
wp_style_add_data( $handle, 'path', ABSPATH . WPINC . $inc_path );
847+
wp_enqueue_style( $handle );
848+
849+
wp_maybe_inline_styles();
850+
851+
$this->assertFalse( $GLOBALS['wp_styles']->registered[ $handle ]->src );
852+
}
853+
822854
/**
823855
* @ticket 63887
824856
*/

0 commit comments

Comments
 (0)