From 80245f0791ab5cceee93ed10465c0b2cf2db55a4 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 6 Sep 2023 13:00:22 +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. Built from https://develop.svn.wordpress.org/trunk@56523 git-svn-id: http://core.svn.wordpress.org/trunk@56035 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-theme.php | 2 +- wp-includes/link-template.php | 15 ++++++++++----- wp-includes/version.php | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php index 5841ec3d9d..427e8317de 100644 --- a/wp-includes/class-wp-theme.php +++ b/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/wp-includes/link-template.php b/wp-includes/link-template.php index c0437f6de9..bdf0d1a00d 100644 --- a/wp-includes/link-template.php +++ b/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; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 29b1bb9e42..07d9c40ceb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.