Add a file existence check in class-wp-theme-json-resolver to avoid… - #10898
Add a file existence check in class-wp-theme-json-resolver to avoid…#10898pbearne wants to merge 6 commits into
class-wp-theme-json-resolver to avoid…#10898Conversation
… potential errors.
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @pbearne@git.wordpress.org. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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. |
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 <westonruter@gmail.com>
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.
| if ( is_array( $decoded_file ) ) { | ||
| static::$theme_json_file_cache[ $file_path ] = $decoded_file; | ||
| return static::$theme_json_file_cache[ $file_path ]; | ||
| if ( is_readable( $file_path ) ) { |
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 ];
}
| * @package WordPress | ||
| * @subpackage Theme | ||
| * | ||
| * @group theme |
| // Test unreadable file. | ||
| if ( function_exists( 'posix_getpwuid' ) && 'root' !== posix_getpwuid( posix_geteuid() )['name'] ) { | ||
| $unreadable_file = DIR_TESTDATA . '/unreadable.json'; | ||
| touch( $unreadable_file ); | ||
| chmod( $unreadable_file, 0000 ); | ||
| $result = @$read_json_file->invoke( null, $unreadable_file ); | ||
| $this->assertSame( array(), $result ); | ||
| unlink( $unreadable_file ); | ||
| } |
|
@pbearne Can you address the feedback from Copilot? Both seem like reasonable points to me. |
… potential errors.
Trac ticket: https://core.trac.wordpress.org/ticket/64620