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.
Built from https://develop.svn.wordpress.org/trunk@56523


git-svn-id: http://core.svn.wordpress.org/trunk@56035 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-06 13:00:22 +00:00
parent eb7094465d
commit 80245f0791
3 changed files with 12 additions and 7 deletions

View File

@ -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;

View File

@ -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;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56522';
$wp_version = '6.4-alpha-56523';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.