Skip to content

Commit 8e83aec

Browse files
General: Remove file_exists() checks after calling realpath().
`realpath()` already checks if the file exists, and returns `false` on failure. The additional `file_exists()` check is not necessary and can be removed, improving the performance. This commit simplifies the checks in two functions: * `register_block_type_from_metadata()` * `wp_json_file_decode()` Note: In both of these cases, the values are passed through `wp_normalize_path()` after `realpath()`, so if the file does not exist, the `false` value gets converted to an empty string. The updated checks work both for `false` and `''` values. Though this is a small tweak, it saves a lot of checks since one of the places we do this is when registering block styles, so it runs quite a few times on each page load. Reference: [https://www.php.net/manual/en/function.realpath.php PHP Manual: realpath()]. Follow-up to [51599], [54132], [54290], [54291]. Props aristath. Fixes #56654. git-svn-id: https://develop.svn.wordpress.org/trunk@54309 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9fb661c commit 8e83aec

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/wp-includes/blocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
442442
remove_block_asset_path_prefix( $metadata['render'] )
443443
)
444444
);
445-
if ( file_exists( $template_path ) ) {
445+
if ( $template_path ) {
446446
/**
447447
* Renders the block on the server.
448448
*

src/wp-includes/functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4528,7 +4528,8 @@ function wp_check_jsonp_callback( $callback ) {
45284528
function wp_json_file_decode( $filename, $options = array() ) {
45294529
$result = null;
45304530
$filename = wp_normalize_path( realpath( $filename ) );
4531-
if ( ! file_exists( $filename ) ) {
4531+
4532+
if ( ! $filename ) {
45324533
trigger_error(
45334534
sprintf(
45344535
/* translators: %s: Path to the JSON file. */

0 commit comments

Comments
 (0)