2021-05-25 10:20:57 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Block template loader functions.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
2021-12-03 14:39:00 -05:00
|
|
|
/**
|
|
|
|
* Adds necessary filters to use 'wp_template' posts instead of theme template files.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.9.0
|
|
|
|
*/
|
|
|
|
function _add_template_loader_filters() {
|
|
|
|
if ( ! current_theme_supports( 'block-templates' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template_types = array_keys( get_default_block_template_types() );
|
|
|
|
foreach ( $template_types as $template_type ) {
|
|
|
|
// Skip 'embed' for now because it is not a regular template type.
|
|
|
|
if ( 'embed' === $template_type ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add_filter( str_replace( '-', '', $template_type ) . '_template', 'locate_block_template', 20, 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request to resolve a template.
|
|
|
|
if ( isset( $_GET['_wp-find-template'] ) ) {
|
2023-02-14 10:57:21 -05:00
|
|
|
add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
|
2021-12-03 14:39:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:20:57 -04:00
|
|
|
/**
|
2022-07-20 09:21:08 -04:00
|
|
|
* Finds a block template with equal or higher specificity than a given PHP template file.
|
2021-05-25 10:20:57 -04:00
|
|
|
*
|
|
|
|
* Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-15 06:21:57 -04:00
|
|
|
* @global string $_wp_current_template_content
|
|
|
|
*
|
2021-07-01 17:02:57 -04:00
|
|
|
* @param string $template Path to the template. See locate_template().
|
|
|
|
* @param string $type Sanitized filename without extension.
|
|
|
|
* @param string[] $templates A list of template candidates, in descending order of priority.
|
2022-11-10 04:40:16 -05:00
|
|
|
* @return string The path to the Site Editor template canvas file, or the fallback PHP template.
|
2021-05-25 10:20:57 -04:00
|
|
|
*/
|
|
|
|
function locate_block_template( $template, $type, array $templates ) {
|
|
|
|
global $_wp_current_template_content;
|
|
|
|
|
Query: Check if the theme supports `block-templates` before calling `locate_block_template()` in `get_query_template()`.
This change improves performance for classic themes by removing an unnecessary query and fixes an issue where a classic theme would show "Empty template: Index" on the frontend when an empty `(block-)templates/index.html` file exists.
Props johnbillion, ianatkins, Mamaduka, costdev, manfcarlo, dolphingg, audrasjb, madeinua, kapilpaul, rafiahmedd, SergeyBiryukov.
Fixes #54844.
Built from https://develop.svn.wordpress.org/trunk@52697
git-svn-id: http://core.svn.wordpress.org/trunk@52286 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-02-09 08:22:03 -05:00
|
|
|
if ( ! current_theme_supports( 'block-templates' ) ) {
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:20:57 -04:00
|
|
|
if ( $template ) {
|
2021-06-16 05:42:56 -04:00
|
|
|
/*
|
|
|
|
* locate_template() has found a PHP template at the path specified by $template.
|
|
|
|
* That means that we have a fallback candidate if we cannot find a block template
|
|
|
|
* with higher specificity.
|
|
|
|
*
|
|
|
|
* Thus, before looking for matching block themes, we shorten our list of candidate
|
|
|
|
* templates accordingly.
|
|
|
|
*/
|
2021-05-25 10:20:57 -04:00
|
|
|
|
|
|
|
// Locate the index of $template (without the theme directory path) in $templates.
|
|
|
|
$relative_template_path = str_replace(
|
|
|
|
array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),
|
|
|
|
'',
|
|
|
|
$template
|
|
|
|
);
|
|
|
|
$index = array_search( $relative_template_path, $templates, true );
|
|
|
|
|
2022-01-18 15:29:06 -05:00
|
|
|
// If the template hierarchy algorithm has successfully located a PHP template file,
|
2021-05-25 10:20:57 -04:00
|
|
|
// we will only consider block templates with higher or equal specificity.
|
|
|
|
$templates = array_slice( $templates, 0, $index + 1 );
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:36:59 -05:00
|
|
|
$block_template = resolve_block_template( $type, $templates, $template );
|
2021-05-25 10:20:57 -04:00
|
|
|
|
|
|
|
if ( $block_template ) {
|
|
|
|
if ( empty( $block_template->content ) && is_user_logged_in() ) {
|
|
|
|
$_wp_current_template_content =
|
|
|
|
sprintf(
|
|
|
|
/* translators: %s: Template title */
|
|
|
|
__( 'Empty template: %s' ),
|
|
|
|
$block_template->title
|
|
|
|
);
|
|
|
|
} elseif ( ! empty( $block_template->content ) ) {
|
|
|
|
$_wp_current_template_content = $block_template->content;
|
|
|
|
}
|
|
|
|
if ( isset( $_GET['_wp-find-template'] ) ) {
|
|
|
|
wp_send_json_success( $block_template );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( $template ) {
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'index' === $type ) {
|
|
|
|
if ( isset( $_GET['_wp-find-template'] ) ) {
|
|
|
|
wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ''; // So that the template loader keeps looking for templates.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add hooks for template canvas.
|
|
|
|
// Add viewport meta tag.
|
|
|
|
add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 );
|
|
|
|
|
|
|
|
// Render title tag with content, regardless of whether theme has title-tag support.
|
|
|
|
remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
|
|
|
|
add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional.
|
|
|
|
|
|
|
|
// This file will be included instead of the theme's template file.
|
|
|
|
return ABSPATH . WPINC . '/template-canvas.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-20 09:21:08 -04:00
|
|
|
* Returns the correct 'wp_template' to render for the request template type.
|
2021-05-25 10:20:57 -04:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
2021-12-02 18:36:59 -05:00
|
|
|
* @since 5.9.0 Added the `$fallback_template` parameter.
|
2021-05-25 10:20:57 -04:00
|
|
|
*
|
|
|
|
* @param string $template_type The current template type.
|
2021-06-23 15:05:57 -04:00
|
|
|
* @param string[] $template_hierarchy The current template hierarchy, ordered by priority.
|
2021-12-02 18:36:59 -05:00
|
|
|
* @param string $fallback_template A PHP fallback template to use if no matching block template is found.
|
2021-05-25 10:20:57 -04:00
|
|
|
* @return WP_Block_Template|null template A template object, or null if none could be found.
|
|
|
|
*/
|
2021-12-02 18:36:59 -05:00
|
|
|
function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) {
|
2021-05-25 10:20:57 -04:00
|
|
|
if ( ! $template_type ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $template_hierarchy ) ) {
|
|
|
|
$template_hierarchy = array( $template_type );
|
|
|
|
}
|
|
|
|
|
|
|
|
$slugs = array_map(
|
|
|
|
'_strip_template_file_suffix',
|
|
|
|
$template_hierarchy
|
|
|
|
);
|
|
|
|
|
|
|
|
// Find all potential templates 'wp_template' post matching the hierarchy.
|
|
|
|
$query = array(
|
|
|
|
'slug__in' => $slugs,
|
|
|
|
);
|
|
|
|
$templates = get_block_templates( $query );
|
|
|
|
|
|
|
|
// Order these templates per slug priority.
|
|
|
|
// Build map of template slugs to their priority in the current hierarchy.
|
|
|
|
$slug_priorities = array_flip( $slugs );
|
|
|
|
|
|
|
|
usort(
|
|
|
|
$templates,
|
2021-08-26 08:59:02 -04:00
|
|
|
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
|
2021-05-25 10:20:57 -04:00
|
|
|
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-12-02 18:36:59 -05:00
|
|
|
$theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR;
|
|
|
|
$parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR;
|
|
|
|
|
2022-01-20 18:53:05 -05:00
|
|
|
// Is the active theme a child theme, and is the PHP fallback template part of it?
|
2021-12-02 18:36:59 -05:00
|
|
|
if (
|
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 11:45:22 -04:00
|
|
|
str_starts_with( $fallback_template, $theme_base_path ) &&
|
2021-12-02 18:36:59 -05:00
|
|
|
strpos( $fallback_template, $parent_theme_base_path ) === false
|
|
|
|
) {
|
|
|
|
$fallback_template_slug = substr(
|
|
|
|
$fallback_template,
|
|
|
|
// Starting position of slug.
|
|
|
|
strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ),
|
|
|
|
// Remove '.php' suffix.
|
|
|
|
-4
|
|
|
|
);
|
|
|
|
|
|
|
|
// Is our candidate block template's slug identical to our PHP fallback template's?
|
|
|
|
if (
|
|
|
|
count( $templates ) &&
|
|
|
|
$fallback_template_slug === $templates[0]->slug &&
|
|
|
|
'theme' === $templates[0]->source
|
|
|
|
) {
|
|
|
|
// Unfortunately, we cannot trust $templates[0]->theme, since it will always
|
2022-01-20 18:53:05 -05:00
|
|
|
// be set to the active theme's slug by _build_block_template_result_from_file(),
|
|
|
|
// even if the block template is really coming from the active theme's parent.
|
|
|
|
// (The reason for this is that we want it to be associated with the active theme
|
2021-12-02 18:36:59 -05:00
|
|
|
// -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.)
|
|
|
|
// Instead, we use _get_block_template_file() to locate the block template file.
|
|
|
|
$template_file = _get_block_template_file( 'wp_template', $fallback_template_slug );
|
|
|
|
if ( $template_file && get_template() === $template_file['theme'] ) {
|
|
|
|
// The block template is part of the parent theme, so we
|
|
|
|
// have to give precedence to the child theme's PHP template.
|
|
|
|
array_shift( $templates );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 10:20:57 -04:00
|
|
|
return count( $templates ) ? $templates[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays title tag with content, regardless of whether theme has title-tag support.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
|
|
|
* @see _wp_render_title_tag()
|
|
|
|
*/
|
|
|
|
function _block_template_render_title_tag() {
|
|
|
|
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the markup for the current template.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-15 06:21:57 -04:00
|
|
|
* @global string $_wp_current_template_content
|
|
|
|
* @global WP_Embed $wp_embed
|
|
|
|
*
|
|
|
|
* @return string Block template markup.
|
2021-05-25 10:20:57 -04:00
|
|
|
*/
|
|
|
|
function get_the_block_template_html() {
|
|
|
|
global $_wp_current_template_content;
|
|
|
|
global $wp_embed;
|
|
|
|
|
|
|
|
if ( ! $_wp_current_template_content ) {
|
|
|
|
if ( is_user_logged_in() ) {
|
|
|
|
return '<h1>' . esc_html__( 'No matching template found' ) . '</h1>';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $wp_embed->run_shortcode( $_wp_current_template_content );
|
|
|
|
$content = $wp_embed->autoembed( $content );
|
|
|
|
$content = do_blocks( $content );
|
|
|
|
$content = wptexturize( $content );
|
2022-01-17 22:39:26 -05:00
|
|
|
$content = convert_smilies( $content );
|
|
|
|
$content = shortcode_unautop( $content );
|
Media: Enhance logic to determine LCP image in block themes and avoid lazy-loading it.
[52065] originally introduced the logic to guess the LCP image based on certain heuristics and to not lazy-load that image. However, with the introduction of block themes, that logic was not functioning correctly, resulting in all featured images to be lazy-loaded, regardless of whether it was the LCP image or not.
Together with an update to the `core/post-featured-image` block included in [55079], this changeset fixes the logic to correctly handle featured images in block themes as well.
Additionally, in combination with an update to the `core/template-part` block from [55246], this changeset includes an enhancement which uses the benefits of block template parts to avoid lazy-loading images in the `header` block template part, making the lazy-loading heuristics even more accurate for sites using a block theme.
Props flixos90, adamsilverstein, mamaduka, antonvlasenko, shahidul95, reduanmasud, costdev, mukesh27, ironprogrammer, manfcarlo, robinwpdeveloper, spacedmonkey.
Fixes #56930.
Built from https://develop.svn.wordpress.org/trunk@55318
git-svn-id: http://core.svn.wordpress.org/trunk@54851 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-13 13:34:17 -05:00
|
|
|
$content = wp_filter_content_tags( $content, 'template' );
|
2022-09-26 18:43:10 -04:00
|
|
|
$content = do_shortcode( $content );
|
2021-05-25 10:20:57 -04:00
|
|
|
$content = str_replace( ']]>', ']]>', $content );
|
|
|
|
|
|
|
|
// Wrap block template in .wp-site-blocks to allow for specific descendant styles
|
|
|
|
// (e.g. `.wp-site-blocks > *`).
|
|
|
|
return '<div class="wp-site-blocks">' . $content . '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a 'viewport' meta tag.
|
|
|
|
*
|
2021-06-23 15:05:57 -04:00
|
|
|
* This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
|
|
|
|
*
|
2021-05-25 10:20:57 -04:00
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
function _block_template_viewport_meta_tag() {
|
|
|
|
echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips .php or .html suffix from template file names.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
|
|
|
* @param string $template_file Template file name.
|
|
|
|
* @return string Template file name without extension.
|
|
|
|
*/
|
|
|
|
function _strip_template_file_suffix( $template_file ) {
|
|
|
|
return preg_replace( '/\.(php|html)$/', '', $template_file );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes post details from block context when rendering a block template.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
|
|
|
* @param array $context Default context.
|
|
|
|
*
|
|
|
|
* @return array Filtered context.
|
|
|
|
*/
|
|
|
|
function _block_template_render_without_post_block_context( $context ) {
|
|
|
|
/*
|
2021-06-16 05:42:56 -04:00
|
|
|
* When loading a template directly and not through a page that resolves it,
|
|
|
|
* the top-level post ID and type context get set to that of the template.
|
|
|
|
* Templates are just the structure of a site, and they should not be available
|
|
|
|
* as post context because blocks like Post Content would recurse infinitely.
|
2021-05-25 10:20:57 -04:00
|
|
|
*/
|
|
|
|
if ( isset( $context['postType'] ) && 'wp_template' === $context['postType'] ) {
|
|
|
|
unset( $context['postId'] );
|
|
|
|
unset( $context['postType'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
2021-12-03 14:39:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current WP_Query to return auto-draft posts.
|
|
|
|
*
|
|
|
|
* The auto-draft status indicates a new post, so allow the the WP_Query instance to
|
|
|
|
* return an auto-draft post for template resolution when editing a new post.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.9.0
|
|
|
|
*
|
|
|
|
* @param WP_Query $wp_query Current WP_Query instance, passed by reference.
|
|
|
|
*/
|
|
|
|
function _resolve_template_for_new_post( $wp_query ) {
|
2022-06-30 11:48:15 -04:00
|
|
|
if ( ! $wp_query->is_main_query() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 14:39:00 -05:00
|
|
|
remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
|
|
|
|
|
|
|
|
// Pages.
|
|
|
|
$page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null;
|
|
|
|
|
|
|
|
// Posts, including custom post types.
|
|
|
|
$p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null;
|
|
|
|
|
|
|
|
$post_id = $page_id ? $page_id : $p;
|
|
|
|
$post = get_post( $post_id );
|
|
|
|
|
|
|
|
if (
|
|
|
|
$post &&
|
|
|
|
'auto-draft' === $post->post_status &&
|
|
|
|
current_user_can( 'edit_post', $post->ID )
|
|
|
|
) {
|
|
|
|
$wp_query->set( 'post_status', 'auto-draft' );
|
|
|
|
}
|
|
|
|
}
|