-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Issue _doing_it_wrong() when registered style has invalid path and allow empty stylesheets to be inlined
#10653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
9828bac
9c7273c
295efcc
8cb36ca
ce80e7a
8cc52a2
16f8e46
23debdb
15d5bd8
4ceb527
5d1ba9f
a7660a9
1f8a2f8
f4694aa
d3fbff0
5c5912a
46afeb7
7152b33
7e0f735
4b94b3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3081,7 +3081,18 @@ function wp_maybe_inline_styles() { | |
| $path = $wp_styles->get_data( $handle, 'path' ); | ||
| if ( $path && $src ) { | ||
| $size = wp_filesize( $path ); | ||
| if ( ! $size ) { | ||
| if ( 0 === $size && ! file_exists( $path ) ) { | ||
| _doing_it_wrong( | ||
| __FUNCTION__, | ||
| sprintf( | ||
| /* translators: 1: 'path', 2: filesystem path, 3: style handle */ | ||
| __( 'Unable to read the "%1$s" key with value "%2$s" for stylesheet "%3$s".' ), | ||
| 'path', | ||
| esc_html( $path ), | ||
| esc_html( $handle ) | ||
| ), | ||
| '7.0.0' | ||
| ); | ||
| continue; | ||
| } | ||
| $styles[] = array( | ||
|
|
@@ -3119,7 +3130,21 @@ static function ( $a, $b ) { | |
| } | ||
|
|
||
| // Get the styles if we don't already have them. | ||
| $style['css'] = file_get_contents( $style['path'] ); | ||
| $style['css'] = @file_get_contents( $style['path'] ); | ||
|
||
| if ( false === $style['css'] ) { | ||
| _doing_it_wrong( | ||
| __FUNCTION__, | ||
| sprintf( | ||
| /* translators: 1: 'path', 2: filesystem path, 3: style handle */ | ||
| __( 'Unable to read the "%1$s" key with value "%2$s" for stylesheet "%3$s".' ), | ||
| 'path', | ||
| esc_html( $style['path'] ), | ||
| esc_html( $style['handle'] ) | ||
| ), | ||
| '7.0.0' | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| /* | ||
| * Check if the style contains relative URLs that need to be modified. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
wp_filesize()returns0both when the path is invalid and when the file has a zero size. So thisfile_exists()check here ensures that an empty stylesheet can be inlined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no reason to inline an empty stylesheet, is there? I suspect the falsy check here is appropriate.
This is interesting, is
wp_filesizehere intended to be an optimization where filters can be used to avoid hitting the filesystem?I tend to use
is_readable()for checks like this.Strictly considering the logic, I think ideally this would be:
I think this would make it very difficult to hit a warning on the
file_get_contentsbelow, although not impossible.I see
wp_filesize()usesfilesizewhich issues a warning if the file isn't present, but I think those will be handled by the readable check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to inline an empty stylesheet as opposed to wastefully loading it as a blocking resource. Right?
This may conflict with the original purpose for introducing the
pre_wp_filesizefilter. If the intention is to bypass the filesystem, then adding the separate! is_readable()check first would conflict with that. I see this was introduced in r52837.I think
file_exists()is better because this is whatwp_filesize()specifically is using to return0.That said, I've added 8cc52a2 to use
is_readable()instead of using error suppression.