wp/wp-includes/template.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
--- a/wp/wp-includes/template.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/template.php	Fri Sep 05 18:40:08 2025 +0200
@@ -7,7 +7,7 @@
  */
 
 /**
- * Retrieve path to a template
+ * Retrieves path to a template.
  *
  * Used to quickly retrieve the path of a template without including the file
  * extension. It will also check the parent theme, if the file exists, with
@@ -104,7 +104,7 @@
 }
 
 /**
- * Retrieve path of index template in current or parent template.
+ * Retrieves path of index template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
@@ -120,7 +120,7 @@
 }
 
 /**
- * Retrieve path of 404 template in current or parent template.
+ * Retrieves path of 404 template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
@@ -136,7 +136,7 @@
 }
 
 /**
- * Retrieve path of archive template in current or parent template.
+ * Retrieves path of archive template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
@@ -152,7 +152,7 @@
 
 	$templates = array();
 
-	if ( count( $post_types ) == 1 ) {
+	if ( count( $post_types ) === 1 ) {
 		$post_type   = reset( $post_types );
 		$templates[] = "archive-{$post_type}.php";
 	}
@@ -162,7 +162,7 @@
 }
 
 /**
- * Retrieve path of post type archive template in current or parent template.
+ * Retrieves path of post type archive template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
@@ -188,7 +188,7 @@
 }
 
 /**
- * Retrieve path of author template in current or parent template.
+ * Retrieves path of author template in current or parent template.
  *
  * The hierarchy for this template looks like:
  *
@@ -226,7 +226,7 @@
 }
 
 /**
- * Retrieve path of category template in current or parent template.
+ * Retrieves path of category template in current or parent template.
  *
  * The hierarchy for this template looks like:
  *
@@ -272,7 +272,7 @@
 }
 
 /**
- * Retrieve path of tag template in current or parent template.
+ * Retrieves path of tag template in current or parent template.
  *
  * The hierarchy for this template looks like:
  *
@@ -318,7 +318,7 @@
 }
 
 /**
- * Retrieve path of custom taxonomy term template in current or parent template.
+ * Retrieves path of custom taxonomy term template in current or parent template.
  *
  * The hierarchy for this template looks like:
  *
@@ -365,7 +365,7 @@
 }
 
 /**
- * Retrieve path of date template in current or parent template.
+ * Retrieves path of date template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'date'.
@@ -381,7 +381,7 @@
 }
 
 /**
- * Retrieve path of home template in current or parent template.
+ * Retrieves path of home template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
@@ -399,7 +399,7 @@
 }
 
 /**
- * Retrieve path of front page template in current or parent template.
+ * Retrieves path of front page template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
@@ -417,7 +417,7 @@
 }
 
 /**
- * Retrieve path of Privacy Policy page template in current or parent template.
+ * Retrieves path of Privacy Policy page template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
@@ -435,7 +435,9 @@
 }
 
 /**
- * Retrieve path of page template in current or parent template.
+ * Retrieves path of page template in current or parent template.
+ *
+ * Note: For block themes, use locate_block_template() function instead.
  *
  * The hierarchy for this template looks like:
  *
@@ -468,8 +470,10 @@
 	$pagename = get_query_var( 'pagename' );
 
 	if ( ! $pagename && $id ) {
-		// If a static page is set as the front page, $pagename will not be set.
-		// Retrieve it from the queried object.
+		/*
+		 * If a static page is set as the front page, $pagename will not be set.
+		 * Retrieve it from the queried object.
+		 */
 		$post = get_queried_object();
 		if ( $post ) {
 			$pagename = $post->post_name;
@@ -496,7 +500,7 @@
 }
 
 /**
- * Retrieve path of search template in current or parent template.
+ * Retrieves path of search template in current or parent template.
  *
  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
@@ -512,7 +516,7 @@
 }
 
 /**
- * Retrieve path of single template in current or parent template. Applies to single Posts,
+ * Retrieves path of single template in current or parent template. Applies to single Posts,
  * single Attachments, and single custom post types.
  *
  * The hierarchy for this template looks like:
@@ -626,7 +630,7 @@
 }
 
 /**
- * Retrieve path of attachment template in current or parent template.
+ * Retrieves path of attachment template in current or parent template.
  *
  * The hierarchy for this template looks like:
  *
@@ -650,8 +654,6 @@
  *
  * @see get_query_template()
  *
- * @global array $posts
- *
  * @return string Full path to attachment template file.
  */
 function get_attachment_template() {
@@ -660,7 +662,7 @@
 	$templates = array();
 
 	if ( $attachment ) {
-		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
+		if ( str_contains( $attachment->post_mime_type, '/' ) ) {
 			list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
 		} else {
 			list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
@@ -678,33 +680,60 @@
 }
 
 /**
- * Retrieve the name of the highest priority template file that exists.
+ * Set up the globals used for template loading.
+ *
+ * @since 6.5.0
  *
- * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
- * so that themes which inherit from a parent theme can just overload one file.
+ * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
+ * @global string $wp_template_path   Path to current theme's template directory.
+ */
+function wp_set_template_globals() {
+	global $wp_stylesheet_path, $wp_template_path;
+
+	$wp_stylesheet_path = get_stylesheet_directory();
+	$wp_template_path   = get_template_directory();
+}
+
+/**
+ * Retrieves the name of the highest priority template file that exists.
+ *
+ * Searches in the stylesheet directory before the template directory and
+ * wp-includes/theme-compat so that themes which inherit from a parent theme
+ * can just overload one file.
  *
  * @since 2.7.0
  * @since 5.5.0 The `$args` parameter was added.
  *
+ * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
+ * @global string $wp_template_path   Path to current theme's template directory.
+ *
  * @param string|array $template_names Template file(s) to search for, in order.
  * @param bool         $load           If true the template file will be loaded if it is found.
- * @param bool         $require_once   Whether to require_once or require. Has no effect if `$load` is false.
+ * @param bool         $load_once      Whether to require_once or require. Has no effect if `$load` is false.
  *                                     Default true.
  * @param array        $args           Optional. Additional arguments passed to the template.
  *                                     Default empty array.
  * @return string The template filename if one is located.
  */
-function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
+function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
+	global $wp_stylesheet_path, $wp_template_path;
+
+	if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
+		wp_set_template_globals();
+	}
+
+	$is_child_theme = is_child_theme();
+
 	$located = '';
 	foreach ( (array) $template_names as $template_name ) {
 		if ( ! $template_name ) {
 			continue;
 		}
-		if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
-			$located = STYLESHEETPATH . '/' . $template_name;
+		if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
+			$located = $wp_stylesheet_path . '/' . $template_name;
 			break;
-		} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
-			$located = TEMPLATEPATH . '/' . $template_name;
+		} elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
+			$located = $wp_template_path . '/' . $template_name;
 			break;
 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
@@ -713,14 +742,14 @@
 	}
 
 	if ( $load && '' !== $located ) {
-		load_template( $located, $require_once, $args );
+		load_template( $located, $load_once, $args );
 	}
 
 	return $located;
 }
 
 /**
- * Require the template file with WordPress environment.
+ * Requires the template file with WordPress environment.
  *
  * The globals are set up for the template file to ensure that the WordPress
  * environment is available from within the function. The query variables are
@@ -742,11 +771,11 @@
  * @global int        $user_ID
  *
  * @param string $_template_file Path to template file.
- * @param bool   $require_once   Whether to require_once or require. Default true.
+ * @param bool   $load_once      Whether to require_once or require. Default true.
  * @param array  $args           Optional. Additional arguments passed to the template.
  *                               Default empty array.
  */
-function load_template( $_template_file, $require_once = true, $args = array() ) {
+function load_template( $_template_file, $load_once = true, $args = array() ) {
 	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
 
 	if ( is_array( $wp_query->query_vars ) ) {
@@ -766,9 +795,31 @@
 		$s = esc_attr( $s );
 	}
 
-	if ( $require_once ) {
+	/**
+	 * Fires before a template file is loaded.
+	 *
+	 * @since 6.1.0
+	 *
+	 * @param string $_template_file The full path to the template file.
+	 * @param bool   $load_once      Whether to require_once or require.
+	 * @param array  $args           Additional arguments passed to the template.
+	 */
+	do_action( 'wp_before_load_template', $_template_file, $load_once, $args );
+
+	if ( $load_once ) {
 		require_once $_template_file;
 	} else {
 		require $_template_file;
 	}
+
+	/**
+	 * Fires after a template file is loaded.
+	 *
+	 * @since 6.1.0
+	 *
+	 * @param string $_template_file The full path to the template file.
+	 * @param bool   $load_once      Whether to require_once or require.
+	 * @param array  $args           Additional arguments passed to the template.
+	 */
+	do_action( 'wp_after_load_template', $_template_file, $load_once, $args );
 }