-
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
base: trunk
Are you sure you want to change the base?
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| if ( $path && $src ) { | ||
| $size = wp_filesize( $path ); | ||
| if ( ! $size ) { | ||
| if ( 0 === $size && ! file_exists( $path ) ) { |
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() returns 0 both when the path is invalid and when the file has a zero size. So this file_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_filesize here 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:
// Missing or unreadable files show notice
if ( ! is_readable(…) ) {
_doing_it_wrong(…);
continue;
}
// Empty files do not need to be included and are not an error.
if ( ! wp_filesize(…) {
continue;
}I think this would make it very difficult to hit a warning on the file_get_contents below, although not impossible.
I see wp_filesize() uses filesize which issues a warning if the file isn't present, but I think those will be handled by the readable check.
Warning: filesize(): stat failed for {FILE} in …
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.
It's better to inline an empty stylesheet as opposed to wastefully loading it as a blocking resource. Right?
Strictly considering the logic, I think ideally this would be:
This may conflict with the original purpose for introducing the pre_wp_filesize filter. 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 tend to use
is_readable()for checks like this.
I think file_exists() is better because this is what wp_filesize() specifically is using to return 0.
That said, I've added 8cc52a2 to use is_readable() instead of using error suppression.
src/wp-includes/script-loader.php
Outdated
|
|
||
| // Get the styles if we don't already have them. | ||
| $style['css'] = file_get_contents( $style['path'] ); | ||
| $style['css'] = @file_get_contents( $style['path'] ); |
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.
The @ error suppression is added here because in the improved test coverage, this logic path is forced by using a filter to force an invalid path to have a non-zero file size.
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 would be good to comment (in code) with justification for the error suppression.
- file_get_contents may generate
E_WARNING - it will return
falseon failure (checked below)
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.
Error suppression removed in 8cc52a2
_doing_it_wrong() when invalid path is provided to registered style_doing_it_wrong() when invalid path is provided to registered style and allow empty stylesheets to be inlined
_doing_it_wrong() when invalid path is provided to registered style and allow empty stylesheets to be inlined_doing_it_wrong() when registered style has invalid path and allow empty stylesheets to be inlined
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Tests are failing due to the GHA runner not having the built assets. |
sirreal
left a comment
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 a lot going on in this deceptively small patch. I've left some feedback for consideration.
src/wp-includes/script-loader.php
Outdated
|
|
||
| // Get the styles if we don't already have them. | ||
| $style['css'] = file_get_contents( $style['path'] ); | ||
| $style['css'] = @file_get_contents( $style['path'] ); |
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 would be good to comment (in code) with justification for the error suppression.
- file_get_contents may generate
E_WARNING - it will return
falseon failure (checked below)
| if ( $path && $src ) { | ||
| $size = wp_filesize( $path ); | ||
| if ( ! $size ) { | ||
| if ( 0 === $size && ! file_exists( $path ) ) { |
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_filesize here 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:
// Missing or unreadable files show notice
if ( ! is_readable(…) ) {
_doing_it_wrong(…);
continue;
}
// Empty files do not need to be included and are not an error.
if ( ! wp_filesize(…) {
continue;
}I think this would make it very difficult to hit a warning on the file_get_contents below, although not impossible.
I see wp_filesize() uses filesize which issues a warning if the file isn't present, but I think those will be handled by the readable check.
Warning: filesize(): stat failed for {FILE} in …
Co-authored-by: Jon Surrell <jonsurrell@git.wordpress.org>
This reverts commit 4ceb527.
|
Gemini 3 review: The changes look solid and follow WordPress core coding standards:
No issues found. Everything looks ready for commit. |
Trac ticket: https://core.trac.wordpress.org/ticket/64447
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.