[wp-trac] [WordPress Trac] #57629: Use `get_theme_file_path()` in `wp_theme_has_theme_json()`

WordPress Trac noreply at wordpress.org
Wed Jun 28 07:55:20 UTC 2023


#57629: Use `get_theme_file_path()` in `wp_theme_has_theme_json()`
------------------------------------+---------------------------
 Reporter:  flixos90                |       Owner:  spacedmonkey
     Type:  enhancement             |      Status:  closed
 Priority:  normal                  |   Milestone:  6.3
Component:  Themes                  |     Version:
 Severity:  normal                  |  Resolution:  fixed
 Keywords:  has-patch dev-feedback  |     Focuses:
------------------------------------+---------------------------

Comment (by spacedmonkey):

 @peterwilsoncc @joemcgill @SergeyBiryukov

 The current commit is a little wasteful. One a normal theme ( without a
 parent ), it will do two file_exists. Imagine this.

 A theme without a theme.json.
 Calls `file_exists( get_stylesheet_directory() . '/theme.json' ) `, return
 false,
 Then set `$path = get_template_directory() . '/theme.json';` and does
 another file exists.
 Calls `if ( file_exists( $path ) ) {`.

 This is two file_exists, which can be expensive.

 In my PR.

 {{{#!php
         $stylesheet_directory = get_stylesheet_directory();
         $template_directory   = get_template_directory();
         $directories          = array( $stylesheet_directory );
         if ( $stylesheet_directory !== $template_directory ) {
                 $directories[] = $template_directory;
         }

         $theme_has_support = false;
         foreach ( $directories as $directory ) {
                 /** This filter is documented in wp-includes/link-
 template.php */
                 $file_path = apply_filters( 'theme_file_path', $directory
 . '/theme.json', 'theme.json' );
                 // Does the theme have its own theme.json?
                 $theme_has_support = is_readable( $file_path );
                 if ( $theme_has_support ) {
                         break;
                 }
         }
 }}}

 This only loops around options if then is a child then, otherwise do one
 loop.

 Also a nitpic.

 This
 {{{#!php
 if ( file_exists( $path ) ) {
     $theme_has_support = true;
 } else {
     $theme_has_support = false;
 }
 }}}

 Should be this.
 {{{#!php
 $theme_has_support = file_exists( $path );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57629#comment:43>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list