From bd594f72a83e0512d60aec5436ab653ed5966678 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 6 Sep 2023 12:58:47 +0000 Subject: [PATCH] Themes: Remove unnecessary check if file exists in the theme functions. Previously, several functions and methods in themes api were designed to check for the existence of files in a child theme before falling back to the parent theme. However, these checks did not consider whether the current theme was a child theme or not, resulting in unnecessary file existence checks for non-child themes. Check to see if stylesheet directory matches the template directory before doing the file exists. This optimization helps reduce unnecessary file system access, as file existence checks can be resource-intensive in PHP. The following functions and methods have been updated as part of this enhancement: - `WP_Theme::get_file_path` - `get_theme_file_path` - `get_theme_file_uri` Props spacedmonkey, flixos90, sabernhardt, 10upsimon, mukesh27. Fixes #59279. git-svn-id: https://develop.svn.wordpress.org/trunk@56523 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 2 +- src/wp-includes/link-template.php | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 5841ec3d9d..427e8317de 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1556,7 +1556,7 @@ final class WP_Theme implements ArrayAccess { if ( empty( $file ) ) { $path = $stylesheet_directory; - } elseif ( file_exists( $stylesheet_directory . '/' . $file ) ) { + } elseif ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { $path = $stylesheet_directory . '/' . $file; } else { $path = $template_directory . '/' . $file; diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index c0437f6de9..bdf0d1a00d 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -4539,9 +4539,11 @@ function get_avatar_data( $id_or_email, $args = null ) { function get_theme_file_uri( $file = '' ) { $file = ltrim( $file, '/' ); + $stylesheet_directory = get_stylesheet_directory(); + if ( empty( $file ) ) { $url = get_stylesheet_directory_uri(); - } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { + } elseif ( get_template_directory() !== $stylesheet_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { $url = get_stylesheet_directory_uri() . '/' . $file; } else { $url = get_template_directory_uri() . '/' . $file; @@ -4600,12 +4602,15 @@ function get_parent_theme_file_uri( $file = '' ) { function get_theme_file_path( $file = '' ) { $file = ltrim( $file, '/' ); + $stylesheet_directory = get_stylesheet_directory(); + $template_directory = get_template_directory(); + if ( empty( $file ) ) { - $path = get_stylesheet_directory(); - } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { - $path = get_stylesheet_directory() . '/' . $file; + $path = $stylesheet_directory; + } elseif ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/' . $file ) ) { + $path = $stylesheet_directory . '/' . $file; } else { - $path = get_template_directory() . '/' . $file; + $path = $template_directory . '/' . $file; } /**