[wp-trac] [WordPress Trac] #56908: The result of locate_block_template function might be wrong

WordPress Trac noreply at wordpress.org
Wed Oct 26 03:21:29 UTC 2022


#56908: The result of locate_block_template function might be wrong
--------------------------+-----------------------------
 Reporter:  arthur791004  |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Referring to [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/block-template.php#L66
 locate_block_template], if the `$template` found by
 [https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-
 includes/template.php#L699 locate_template] is given, this function will
 call `array_slice` to shorten the list of candidate templates. However,
 the `array_search` might not find a located template from candidate
 templates, and it leads to the return value, `$index`, becoming `false`.
 Therefore, when we run the following codes, we will always get the
 incorrect result.

 {{{#!php
 $templates = array_slice( $templates, 0, $index + 1 );
 }}}

 **Why we might not be able to find the located template from candidate
 templates?**

 The reason is the [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/template.php#L699 locate_template]
 function tries to use `STYLESHEETPATH` and `TEMPLATEPATH` to make the path
 and those two are defined by [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/default-constants.php#L392
 wp_templating_constants] but the [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/block-template.php#L66
 locate_block_template] function uses `get_stylesheet_directory` and
 `get_template_directory` to make the relative template path. As we're able
 to hook the returned value of both `get_stylesheet_directory` and
 `get_template_directory`, the result might be different from
 `STYLESHEETPATH` and `TEMPLATEPATH` respectively if the developer adds the
 hook after the [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/default-constants.php#L392
 wp_templating_constants] calls.

 **Possible Solutions**

 One way is to check the `$index` before doing `array_slice` as followed to
 ensure the `array_slice` works expected.

 {{{#!diff
 + if ( false !== $index ) {
     $templates = array_slice( $templates, 0, $index + 1 );
 + }
 }}}

 The other way is to avoid using different variables. We have to use
 "`STYLESHEETPATH` and `TEMPLATEPATH`" or "`get_stylesheet_directory` and
 `get_template_directory`" in both places

 For example

 {{{#!diff
 function locate_template( $template_names, $load = false, $require_once =
 true, $args = array() ) {
   ...

 - if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
 + if ( file_exists( get_stylesheet_directory() . '/' . $template_name ) )
 {
   ...
 }
 }}}

 What do you think?

 ---

 BTW, there is something weird in [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/block-template.php#L66
 locate_block_template]. At the end of the function, it says "**This file
 will be included instead of the theme's template file.**". However, it
 will return the template file immediately on [https://github.com/WordPress
 /wordpress-develop/blob/ec91ec56be44d8cdbc611758b9721860aae60491/src/wp-
 includes/block-template.php#L96 L96] if we have a located template. Is it
 the correct behavior?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56908>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list