Make WordPress Core


Ignore:
Timestamp:
09/06/2023 12:58:47 PM (15 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r56245 r56523  
    45404540    $file = ltrim( $file, '/' );
    45414541
     4542    $stylesheet_directory = get_stylesheet_directory();
     4543
    45424544    if ( empty( $file ) ) {
    45434545        $url = get_stylesheet_directory_uri();
    4544     } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
     4546    } elseif ( get_template_directory() !== $stylesheet_directory && file_exists( $stylesheet_directory . '/' . $file ) ) {
    45454547        $url = get_stylesheet_directory_uri() . '/' . $file;
    45464548    } else {
     
    46014603    $file = ltrim( $file, '/' );
    46024604
     4605    $stylesheet_directory = get_stylesheet_directory();
     4606    $template_directory   = get_template_directory();
     4607
    46034608    if ( empty( $file ) ) {
    4604         $path = get_stylesheet_directory();
    4605     } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
    4606         $path = get_stylesheet_directory() . '/' . $file;
    4607     } else {
    4608         $path = get_template_directory() . '/' . $file;
     4609        $path = $stylesheet_directory;
     4610    } elseif ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/' . $file ) ) {
     4611        $path = $stylesheet_directory . '/' . $file;
     4612    } else {
     4613        $path = $template_directory . '/' . $file;
    46094614    }
    46104615
Note: See TracChangeset for help on using the changeset viewer.