-
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
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 <[email protected]>
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. |
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.
I spent some time to understand the motivation around wp_filesize(). This line jumps out:
Improves support for plugins like S3 plugins that outsource files to third party file store.
I think the way this is implemented now should continue to work correctly, but I want to check my understanding. $style['path'] is a way for styles to opt-in to this inlining. Is that correct?
Presumably, styles that aren't locally available would not add path and would not generate noise with the second _doing_it_wrong message.
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.
Approving on the assumption that my comment does not raise any new concernts.
That is correct. This is how styles can opt in to inlining. IMO, this could be made automatic by looking at the URL. But for now the
|
* trunk: Scripts: Remove non-HTML5 script support. Scripts: Remove CDATA script wrappers in WP Admin. Tests: Use `assertSame()` in some newly introduced tests. Docs: Improve description for `add_settings_section()`. Scripts: Remove default type attribute from tags. Plugins: Update plugin compatibility text to remove obsolete percentage-based number. Tests: Update fonts tests to use semantic HTML comparison. Help/About: Simplify help text on Updates screen to account for third-party updates. Tests: Rename the `admin/includesUser.php` test file to be more descriptive. Upgrade/Install: Correct and improve various docblocks relating to upgrades. Export: Fix fatal error when passing `null` to `wxr_cdata()` by casting passed value to `string`. Code Modernization: Replace `isset()` with null coalescing in `WP_Roles::get_role()`. Code Modernization: Replace some `isset()` ternary checks with null coalescing.
…nd allow inlining empty stylesheets. When a stylesheet is registered with a `path` that does not exist or which is not readable, then a `_doing_it_wrong()` is now issued. Previously, paths that did not exist were silently skipped; paths for empty styles were also needlessly skipped, since `wp_filesize()` also returns `0` for the failure case. Developed in #10653 Follow-up to [50836]. Props westonruter, jonsurrell, soyebsalar01. See #52620. Fixes #64447. git-svn-id: https://develop.svn.wordpress.org/trunk@61416 602fd350-edb4-49c9-b593-d223f7449a82
…nd allow inlining empty stylesheets. When a stylesheet is registered with a `path` that does not exist or which is not readable, then a `_doing_it_wrong()` is now issued. Previously, paths that did not exist were silently skipped; paths for empty styles were also needlessly skipped, since `wp_filesize()` also returns `0` for the failure case. Developed in WordPress/wordpress-develop#10653 Follow-up to [50836]. Props westonruter, jonsurrell, soyebsalar01. See #52620. Fixes #64447. Built from https://develop.svn.wordpress.org/trunk@61416 git-svn-id: http://core.svn.wordpress.org/trunk@60728 1a063a9b-81f0-0310-95a4-ce76da25c4cd
…nd allow inlining empty stylesheets. When a stylesheet is registered with a `path` that does not exist or which is not readable, then a `_doing_it_wrong()` is now issued. Previously, paths that did not exist were silently skipped; paths for empty styles were also needlessly skipped, since `wp_filesize()` also returns `0` for the failure case. Developed in WordPress/wordpress-develop#10653 Follow-up to [50836]. Props westonruter, jonsurrell, soyebsalar01. See #52620. Fixes #64447. Built from https://develop.svn.wordpress.org/trunk@61416 git-svn-id: https://core.svn.wordpress.org/trunk@60728 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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.