diff --git a/wp-admin/includes/theme.php b/wp-admin/includes/theme.php index 6bb450ee1b..ea73d8650d 100644 --- a/wp-admin/includes/theme.php +++ b/wp-admin/includes/theme.php @@ -1166,11 +1166,14 @@ function resume_theme( $theme, $redirect = '' ) { * creating a fatal error. */ if ( ! empty( $redirect ) ) { + $stylesheet_path = get_stylesheet_directory(); + $template_path = get_template_directory(); + $functions_path = ''; - if ( str_contains( STYLESHEETPATH, $extension ) ) { - $functions_path = STYLESHEETPATH . '/functions.php'; - } elseif ( str_contains( TEMPLATEPATH, $extension ) ) { - $functions_path = TEMPLATEPATH . '/functions.php'; + if ( str_contains( $stylesheet_path, $extension ) ) { + $functions_path = $stylesheet_path . '/functions.php'; + } elseif ( str_contains( $template_path, $extension ) ) { + $functions_path = $template_path . '/functions.php'; } if ( ! empty( $functions_path ) ) { diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 03f450dc9b..a80dc60e41 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -1381,7 +1381,7 @@ function wp_comment_form_unfiltered_html_nonce() { * and the post ID respectively. * * The `$file` path is passed through a filter hook called {@see 'comments_template'}, - * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path + * which includes the template directory and $file combined. Tries the $filtered path * first and if it fails it will require the default comment template from the * default theme. If either does not exist, then the WordPress process will be * halted. It is advised for that reason, that the default theme is not deleted. @@ -1600,7 +1600,10 @@ function comments_template( $file = '/comments.php', $separate_comments = false define( 'COMMENTS_TEMPLATE', true ); } - $theme_template = STYLESHEETPATH . $file; + $stylesheet_path = get_stylesheet_directory(); + $template_path = get_template_directory(); + + $theme_template = $stylesheet_path . $file; /** * Filters the path to the theme template file used for the comments template. @@ -1613,8 +1616,8 @@ function comments_template( $file = '/comments.php', $separate_comments = false if ( file_exists( $include ) ) { require $include; - } elseif ( file_exists( TEMPLATEPATH . $file ) ) { - require TEMPLATEPATH . $file; + } elseif ( file_exists( $template_path . $file ) ) { + require $template_path . $file; } else { // Backward compat code will be removed in a future release. require ABSPATH . WPINC . '/theme-compat/comments.php'; } diff --git a/wp-includes/default-constants.php b/wp-includes/default-constants.php index 6e64e789a0..0c0ee771eb 100644 --- a/wp-includes/default-constants.php +++ b/wp-includes/default-constants.php @@ -407,6 +407,8 @@ function wp_templating_constants() { * Filesystem path to the current active template directory. * * @since 1.5.0 + * @deprecated 6.4.0 Use get_template_directory() instead. + * @see get_template_directory() */ define( 'TEMPLATEPATH', get_template_directory() ); @@ -414,6 +416,8 @@ function wp_templating_constants() { * Filesystem path to the current active template stylesheet directory. * * @since 2.1.0 + * @deprecated 6.4.0 Use get_stylesheet_directory() instead. + * @see get_stylesheet_directory() */ define( 'STYLESHEETPATH', get_stylesheet_directory() ); diff --git a/wp-includes/load.php b/wp-includes/load.php index 8f18a3a6e9..77e17b3f8b 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -1049,11 +1049,14 @@ function wp_get_active_and_valid_themes() { return $themes; } - if ( TEMPLATEPATH !== STYLESHEETPATH ) { - $themes[] = STYLESHEETPATH; + $stylesheet_path = get_stylesheet_directory(); + $template_path = get_template_directory(); + + if ( $template_path !== $stylesheet_path ) { + $themes[] = $stylesheet_path; } - $themes[] = TEMPLATEPATH; + $themes[] = $template_path; /* * Remove themes from the list of active themes when we're on an endpoint diff --git a/wp-includes/template.php b/wp-includes/template.php index afc2ca6edf..989d630f7f 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -684,8 +684,9 @@ function get_attachment_template() { /** * Retrieves the name of the highest priority template file that exists. * - * 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. + * 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. @@ -699,16 +700,20 @@ function get_attachment_template() { * @return string The template filename if one is located. */ function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) { + $stylesheet_path = get_stylesheet_directory(); + $template_path = get_template_directory(); + $is_child_theme = $stylesheet_path !== $template_path; + $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( $stylesheet_path . '/' . $template_name ) ) { + $located = $stylesheet_path . '/' . $template_name; break; - } elseif ( is_child_theme() && file_exists( TEMPLATEPATH . '/' . $template_name ) ) { - $located = TEMPLATEPATH . '/' . $template_name; + } elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) { + $located = $template_path . '/' . $template_name; break; } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) { $located = ABSPATH . WPINC . '/theme-compat/' . $template_name; diff --git a/wp-includes/theme.php b/wp-includes/theme.php index 5ff31953b1..6be2ad700a 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -157,7 +157,7 @@ function wp_clean_themes_cache( $clear_update_cache = true ) { * @return bool True if a child theme is in use, false otherwise. */ function is_child_theme() { - return ( TEMPLATEPATH !== STYLESHEETPATH ); + return get_template_directory() !== get_stylesheet_directory(); } /** @@ -187,24 +187,40 @@ function get_stylesheet() { * Retrieves stylesheet directory path for the active theme. * * @since 1.5.0 + * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. + * + * @global string $wp_stylesheet_path Current theme stylesheet directory path. * * @return string Path to active theme's stylesheet directory. */ function get_stylesheet_directory() { - $stylesheet = get_stylesheet(); - $theme_root = get_theme_root( $stylesheet ); - $stylesheet_dir = "$theme_root/$stylesheet"; + global $wp_stylesheet_path; - /** - * Filters the stylesheet directory path for the active theme. - * - * @since 1.5.0 - * - * @param string $stylesheet_dir Absolute path to the active theme. - * @param string $stylesheet Directory name of the active theme. - * @param string $theme_root Absolute path to themes directory. - */ - return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); + if ( null === $wp_stylesheet_path ) { + $stylesheet = get_stylesheet(); + $theme_root = get_theme_root( $stylesheet ); + $stylesheet_dir = "$theme_root/$stylesheet"; + + /** + * Filters the stylesheet directory path for the active theme. + * + * @since 1.5.0 + * + * @param string $stylesheet_dir Absolute path to the active theme. + * @param string $stylesheet Directory name of the active theme. + * @param string $theme_root Absolute path to themes directory. + */ + $stylesheet_dir = apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); + + // If there are filter callbacks, force the logic to execute on every call. + if ( has_filter( 'stylesheet' ) || has_filter( 'theme_root' ) || has_filter( 'stylesheet_directory' ) ) { + return $stylesheet_dir; + } + + $wp_stylesheet_path = $stylesheet_dir; + } + + return $wp_stylesheet_path; } /** @@ -321,24 +337,40 @@ function get_template() { * Retrieves template directory path for the active theme. * * @since 1.5.0 + * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. + * + * @global string $wp_template_path Current theme template directory path. * * @return string Path to active theme's template directory. */ function get_template_directory() { - $template = get_template(); - $theme_root = get_theme_root( $template ); - $template_dir = "$theme_root/$template"; + global $wp_template_path; - /** - * Filters the active theme directory path. - * - * @since 1.5.0 - * - * @param string $template_dir The path of the active theme directory. - * @param string $template Directory name of the active theme. - * @param string $theme_root Absolute path to the themes directory. - */ - return apply_filters( 'template_directory', $template_dir, $template, $theme_root ); + if ( null === $wp_template_path ) { + $template = get_template(); + $theme_root = get_theme_root( $template ); + $template_dir = "$theme_root/$template"; + + /** + * Filters the active theme directory path. + * + * @since 1.5.0 + * + * @param string $template_dir The path of the active theme directory. + * @param string $template Directory name of the active theme. + * @param string $theme_root Absolute path to the themes directory. + */ + $template_dir = apply_filters( 'template_directory', $template_dir, $template, $theme_root ); + + // If there are filter callbacks, force the logic to execute on every call. + if ( has_filter( 'template' ) || has_filter( 'theme_root' ) || has_filter( 'template_directory' ) ) { + return $template_dir; + } + + $wp_template_path = $template_dir; + } + + return $wp_template_path; } /** @@ -744,11 +776,13 @@ function locale_stylesheet() { * @global WP_Customize_Manager $wp_customize * @global array $sidebars_widgets * @global array $wp_registered_sidebars + * @global string $wp_stylesheet_path + * @global string $wp_template_path * * @param string $stylesheet Stylesheet name. */ function switch_theme( $stylesheet ) { - global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars; + global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars, $wp_stylesheet_path, $wp_template_path; $requirements = validate_theme_requirements( $stylesheet ); if ( is_wp_error( $requirements ) ) { @@ -832,6 +866,13 @@ function switch_theme( $stylesheet ) { update_option( 'theme_switched', $old_theme->get_stylesheet() ); + /* + * Reset globals to force refresh the next time these directories are + * accessed via `get_stylesheet_directory()` / `get_template_directory()`. + */ + $wp_stylesheet_path = null; + $wp_template_path = null; + /** * Fires after the theme is switched. * diff --git a/wp-includes/version.php b/wp-includes/version.php index a677de9de8..3c8d88a187 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56634'; +$wp_version = '6.4-alpha-56635'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.