Repository navigation
Conversation
… potential errors.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
Co-authored-by: Weston Ruter <[email protected]>
used is_readable not file_exists
westonruter
left a comment
There was a problem hiding this comment.
@pbearne It's still not clear how to reproduce this issue. I wonder if wrapping the code in an is_readable() will just make it harder to debug. Should there be an else statement to say the expected file is missing? Again, having a way to reproduce this would be helpful, and thus could be added as a test.
|
@westonruter what would you like in the else |
There was a problem hiding this comment.
Pull request overview
This PR adjusts WP_Theme_JSON_Resolver::read_json_file() to avoid calling wp_json_file_decode() when the passed path is not readable, preventing avoidable errors/warnings when theme.json (or similar JSON files) are missing or inaccessible.
Changes:
- Add a readability check before decoding JSON files in
WP_Theme_JSON_Resolver::read_json_file(). - Preserve existing caching behavior for successfully decoded theme.json-shaped arrays.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/wp-includes/class-wp-theme-json-resolver.php:114
is_readable()is true for directories, which would still allowwp_json_file_decode()to run. That function callsjson_decode( file_get_contents( $filename ) ...); iffile_get_contents()returnsfalse(e.g., path is a directory), PHP 8+ can throw aTypeErrorbecausejson_decode()requires a string. Consider requiring a regular file as well as readability before decoding.
if ( is_readable( $file_path ) ) {
$decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
static::$theme_json_file_cache[ $file_path ] = $decoded_file;
return static::$theme_json_file_cache[ $file_path ];
}
|
@pbearne Can you address the feedback from Copilot? Both seem like reasonable points to me. |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
… by replacing redundant checks with `is_file`.
|
@t-hamano fixed |
There was a problem hiding this comment.
Thanks for working on this. The guard stops the PHP warning, but I have a concern about where the check lives and what it hides.
How I reproduced the original warning on trunk
get_style_variations() is the only caller of read_json_file() that does not check is_readable() first. The other callers check it at lines 256, 310 and 725. So the trigger is a file in a theme's styles/ folder that exists but cannot be read:
cd wp-content/themes/twentytwentyfive/styles
echo '{"version":3,"title":"Broken"}' > broken.json
chmod 000 broken.json
# or: mkdir fake.jsonWith WP_DEBUG on, open Appearance > Editor > Styles. Trunk logs file_get_contents(...): Failed to open stream: Permission denied and then a JSON decode notice. A directory named fake.json gives errno=21 Is a directory. With this PR, nothing is logged and the variation disappears from the list.
Smaller points
- The new check repeats the
is_readable()check that three of the four callers already do. This adds extra stat calls on those paths. - Failed reads are not cached. A broken file is checked and decoded again on every call to
get_style_variations()in the same request.
Note: Claude fine this.
…andling; enhance unit tests with new test cases.
…ices in `read_json_file`
…in `read_json_file()`. Co-Authored-By: Claude <[email protected]>
…le_get_contents()` read failure. Co-Authored-By: Claude <[email protected]>
…e not readable files. Co-Authored-By: Claude <[email protected]>
…he diff focused. Co-Authored-By: Claude <[email protected]>
|
Regarding this comment: #10898 (review) I don't think the two "Smaller points" need changes in this PR:
Both checks are needed. The callers' checks stop missing files from triggering a notice, and the one in
This matches trunk and only affects broken JSON files, so it can be handled in a follow-up if needed. |
…n_file()` tests. Co-Authored-By: Claude <[email protected]>
…_file()` tests. Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
…code() for clarity Co-Authored-By: Claude <[email protected]>
class-wp-theme-json-resolver to avoid…class-wp-theme-json-resolver to avoid potential errors.
t-hamano
left a comment
There was a problem hiding this comment.
This PR looks ready to ship to me. To be safe, I'll commit it once the 7.2 branch is created with today's RC1 and trunk opens for 7.3.
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks for addressing all of my earlier feedback! The fix now lives in wp_json_file_decode(), and the "doesn't exist" notice now shows the real file path. This looks good to me.
None of the notes below should block the commit.
One more note: the PR title still names class-wp-theme-json-resolver, but the change is now in wp_json_file_decode(). That function is also used for block registration and font collections. The commit message should say that this change affects every caller, not only the theme.json resolver.
…le_decode() Co-Authored-By: Claude <[email protected]>
class-wp-theme-json-resolver to avoid potential errors.wp_json_file_decode() to avoid potential errors.
…nctions.php Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
…invalid JSON test, since PHP 8.6 appends the error location Co-Authored-By: Claude <[email protected]>
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks for working through all the earlier feedback. The code change looks good to me. I ran the tests locally: 8 pass, and the unreadable-file test skips because Docker runs PHPUnit as root, as discussed above. PHPStan reports no new errors.
I left two small, non-blocking comments.
One general request: could you fill in the PR description? It has only the Trac link right now. A short summary, testing steps, and the AI disclosure from the PR template would help the committer. It would also help to say which errors the title's "potential errors" are: a PHP warning from file_get_contents(), followed by a JSON decode notice, when a theme's styles/ folder holds a file that can't be read.
| @@ -4810,10 +4810,9 @@ function wp_check_jsonp_callback( $callback ) { | |||
| * `null` is returned if the file is not found, or its content can't be decoded. | |||
There was a problem hiding this comment.
Non-blocking: this PR adds a third case that returns null, a path that is not a readable file. Could the @return description mention it?
| * `null` is returned if the file is not found, or its content can't be decoded. | |
| * `null` is returned if the file is not found, is not a readable file, | |
| * or its content can't be decoded. |
| /** | ||
| * Tests reading a valid JSON file. | ||
| * | ||
| * @ticket 64620 |
There was a problem hiding this comment.
Non-blocking: the error-path tests moved to tests/phpunit/tests/functions.php. The three tests left in this file cover the existing caching in read_json_file(), not the change for #64620. They're useful, because nothing else tests this method directly. But the @ticket 64620 tags here and on lines 89 and 103 suggest they test the fix. Could we remove the tags?
…n description Co-Authored-By: Claude <[email protected]>
…they cover the existing caching rather than the fix Co-Authored-By: Claude <[email protected]>
… potential errors.
Trac ticket: https://core.trac.wordpress.org/ticket/64620