2021-05-25 16:20:57 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Block template loader functions.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
2021-12-03 20:39:00 +01:00
|
|
|
/**
|
Themes: Block template is located twice in `get_query_template()`.
When the function `_template_loader_filters` was ported to core from gutenberg, it retained the filter to load block templates. However, the function `locate_block_template` is called manually in `get_query_template`, so this filter is not needed. Calling `locate_block_template` twice, results in performance issue, as `locate_block_template` is a expensive function to run, as it does database and file lookups.
Props dlh, mukesh27, flixos90, SergeyBiryukov, bernhard-reiter, spacedmonkey.
Fixes #58299.
Built from https://develop.svn.wordpress.org/trunk@56060
git-svn-id: http://core.svn.wordpress.org/trunk@55572 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-27 12:15:28 +02:00
|
|
|
* Adds necessary hooks to resolve '_wp-find-template' requests.
|
2021-12-03 20:39:00 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.9.0
|
|
|
|
*/
|
|
|
|
function _add_template_loader_filters() {
|
Themes: Block template is located twice in `get_query_template()`.
When the function `_template_loader_filters` was ported to core from gutenberg, it retained the filter to load block templates. However, the function `locate_block_template` is called manually in `get_query_template`, so this filter is not needed. Calling `locate_block_template` twice, results in performance issue, as `locate_block_template` is a expensive function to run, as it does database and file lookups.
Props dlh, mukesh27, flixos90, SergeyBiryukov, bernhard-reiter, spacedmonkey.
Fixes #58299.
Built from https://develop.svn.wordpress.org/trunk@56060
git-svn-id: http://core.svn.wordpress.org/trunk@55572 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-27 12:15:28 +02:00
|
|
|
if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
|
2023-02-14 16:57:21 +01:00
|
|
|
add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
|
2021-12-03 20:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:20:57 +02:00
|
|
|
/**
|
2022-07-20 15:21:08 +02:00
|
|
|
* Finds a block template with equal or higher specificity than a given PHP template file.
|
2021-05-25 16:20:57 +02:00
|
|
|
*
|
|
|
|
* Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
Toolbar: Make the 'Edit site' link open the editor with the current template.
Adds a global, `$_wp_current_template_id`, to allow the 'Edit site' link in the admin bar to be aware of the current template and pass it to the Site Editor, so it can load the appropriate one directly.
See https://github.com/WordPress/gutenberg/issues/37850 for further discussion.
Props Mamaduka, poena, abitofmind, audrasjb, mikinc860, alexstine, annezazu, beckej, jameskoster, bacoords, clubkert, paaljoachim, get_dave, priethor, skorasaurus.
Fixes #58746.
Built from https://develop.svn.wordpress.org/trunk@56209
git-svn-id: http://core.svn.wordpress.org/trunk@55721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-11 13:33:27 +02:00
|
|
|
* @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar.
|
2021-05-25 16:20:57 +02:00
|
|
|
*
|
2021-06-15 12:21:57 +02:00
|
|
|
* @global string $_wp_current_template_content
|
Toolbar: Make the 'Edit site' link open the editor with the current template.
Adds a global, `$_wp_current_template_id`, to allow the 'Edit site' link in the admin bar to be aware of the current template and pass it to the Site Editor, so it can load the appropriate one directly.
See https://github.com/WordPress/gutenberg/issues/37850 for further discussion.
Props Mamaduka, poena, abitofmind, audrasjb, mikinc860, alexstine, annezazu, beckej, jameskoster, bacoords, clubkert, paaljoachim, get_dave, priethor, skorasaurus.
Fixes #58746.
Built from https://develop.svn.wordpress.org/trunk@56209
git-svn-id: http://core.svn.wordpress.org/trunk@55721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-11 13:33:27 +02:00
|
|
|
* @global string $_wp_current_template_id
|
2021-06-15 12:21:57 +02:00
|
|
|
*
|
2021-07-01 23:02:57 +02: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 10:40:16 +01:00
|
|
|
* @return string The path to the Site Editor template canvas file, or the fallback PHP template.
|
2021-05-25 16:20:57 +02:00
|
|
|
*/
|
|
|
|
function locate_block_template( $template, $type, array $templates ) {
|
Toolbar: Make the 'Edit site' link open the editor with the current template.
Adds a global, `$_wp_current_template_id`, to allow the 'Edit site' link in the admin bar to be aware of the current template and pass it to the Site Editor, so it can load the appropriate one directly.
See https://github.com/WordPress/gutenberg/issues/37850 for further discussion.
Props Mamaduka, poena, abitofmind, audrasjb, mikinc860, alexstine, annezazu, beckej, jameskoster, bacoords, clubkert, paaljoachim, get_dave, priethor, skorasaurus.
Fixes #58746.
Built from https://develop.svn.wordpress.org/trunk@56209
git-svn-id: http://core.svn.wordpress.org/trunk@55721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-11 13:33:27 +02:00
|
|
|
global $_wp_current_template_content, $_wp_current_template_id;
|
2021-05-25 16:20:57 +02:00
|
|
|
|
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 14:22:03 +01:00
|
|
|
if ( ! current_theme_supports( 'block-templates' ) ) {
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:20:57 +02:00
|
|
|
if ( $template ) {
|
2021-06-16 11:42:56 +02: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 16:20:57 +02: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 21:29:06 +01:00
|
|
|
// If the template hierarchy algorithm has successfully located a PHP template file,
|
2021-05-25 16:20:57 +02:00
|
|
|
// we will only consider block templates with higher or equal specificity.
|
|
|
|
$templates = array_slice( $templates, 0, $index + 1 );
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:36:59 +01:00
|
|
|
$block_template = resolve_block_template( $type, $templates, $template );
|
2021-05-25 16:20:57 +02:00
|
|
|
|
|
|
|
if ( $block_template ) {
|
Toolbar: Make the 'Edit site' link open the editor with the current template.
Adds a global, `$_wp_current_template_id`, to allow the 'Edit site' link in the admin bar to be aware of the current template and pass it to the Site Editor, so it can load the appropriate one directly.
See https://github.com/WordPress/gutenberg/issues/37850 for further discussion.
Props Mamaduka, poena, abitofmind, audrasjb, mikinc860, alexstine, annezazu, beckej, jameskoster, bacoords, clubkert, paaljoachim, get_dave, priethor, skorasaurus.
Fixes #58746.
Built from https://develop.svn.wordpress.org/trunk@56209
git-svn-id: http://core.svn.wordpress.org/trunk@55721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-07-11 13:33:27 +02:00
|
|
|
$_wp_current_template_id = $block_template->id;
|
|
|
|
|
2021-05-25 16:20:57 +02:00
|
|
|
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 15:21:08 +02:00
|
|
|
* Returns the correct 'wp_template' to render for the request template type.
|
2021-05-25 16:20:57 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @since 5.8.0
|
2021-12-03 00:36:59 +01:00
|
|
|
* @since 5.9.0 Added the `$fallback_template` parameter.
|
2021-05-25 16:20:57 +02:00
|
|
|
*
|
|
|
|
* @param string $template_type The current template type.
|
2021-06-23 21:05:57 +02:00
|
|
|
* @param string[] $template_hierarchy The current template hierarchy, ordered by priority.
|
2021-12-03 00:36:59 +01:00
|
|
|
* @param string $fallback_template A PHP fallback template to use if no matching block template is found.
|
2021-05-25 16:20:57 +02:00
|
|
|
* @return WP_Block_Template|null template A template object, or null if none could be found.
|
|
|
|
*/
|
2021-12-03 00:36:59 +01:00
|
|
|
function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) {
|
2021-05-25 16:20:57 +02: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 14:59:02 +02:00
|
|
|
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
|
2021-05-25 16:20:57 +02:00
|
|
|
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-12-03 00:36:59 +01:00
|
|
|
$theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR;
|
|
|
|
$parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR;
|
|
|
|
|
2022-01-21 00:53:05 +01:00
|
|
|
// Is the active theme a child theme, and is the PHP fallback template part of it?
|
2021-12-03 00:36:59 +01: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 17:45:22 +02:00
|
|
|
str_starts_with( $fallback_template, $theme_base_path ) &&
|
Code Modernization: Replace usage of `strpos()` with `str_contains()`.
`str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle).
WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987].
Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.
Built from https://develop.svn.wordpress.org/trunk@55988
git-svn-id: http://core.svn.wordpress.org/trunk@55500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 16:36:26 +02:00
|
|
|
! str_contains( $fallback_template, $parent_theme_base_path )
|
2021-12-03 00:36:59 +01:00
|
|
|
) {
|
|
|
|
$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-21 00:53:05 +01: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-03 00:36:59 +01: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 16:20:57 +02: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
|
|
|
|
*
|
2023-10-27 20:18:22 +02:00
|
|
|
* @global string $_wp_current_template_id
|
2021-06-15 12:21:57 +02:00
|
|
|
* @global string $_wp_current_template_content
|
2024-03-06 06:05:12 +01:00
|
|
|
* @global WP_Embed $wp_embed WordPress Embed object.
|
|
|
|
* @global WP_Query $wp_query WordPress Query object.
|
2021-06-15 12:21:57 +02:00
|
|
|
*
|
|
|
|
* @return string Block template markup.
|
2021-05-25 16:20:57 +02:00
|
|
|
*/
|
|
|
|
function get_the_block_template_html() {
|
2023-10-27 20:18:22 +02:00
|
|
|
global $_wp_current_template_id, $_wp_current_template_content, $wp_embed, $wp_query;
|
2021-05-25 16:20:57 +02:00
|
|
|
|
|
|
|
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 );
|
Editor: Restore shortcode support for block templates.
Follow up to [55761].
Props youknowriad, Clorith, desrosj, antpb, Otto42, johnbillion, jorbin, rmccue, timothyblynjacobs, mikeschroder, peterwilsoncc, audrasjb, matveb, noisysocks, xknown, ehtis, isabel_brison, talldanwp, gziolo, chriscct7, davidbaumwald.
Fixes #58333.
Built from https://develop.svn.wordpress.org/trunk@55828
git-svn-id: http://core.svn.wordpress.org/trunk@55340 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-20 05:58:47 +02:00
|
|
|
$content = shortcode_unautop( $content );
|
|
|
|
$content = do_shortcode( $content );
|
Editor: Ensure main query loop is entered for singular content in block themes.
Block themes currently lack the means to trigger the main query loop for singular content, since they cannot reasonably use the `core/query` and `core/post-template` blocks which are intended only for displaying a list of posts. So far, the missing main query loop on singular block templates has been worked around by enforcing the loop in certain `core/post-*` blocks, which however causes other bugs.
This changeset ensures that the main query loop is still started for singular block theme templates, by wrapping the entire template into the loop, which will by definition only have a single cycle as it only encompasses a single post. This is currently the most reliable solution, since even if there were blocks to properly trigger the main query loop on singular content, it would be unrealistic to expect all existing block themes to update their templates accordingly. It may be revisited in the future.
Props gziolo, youknowriad, joemcgill, costdev, mukesh27, flixos90.
Fixes #58154.
See #59225, #58027.
Built from https://develop.svn.wordpress.org/trunk@56507
git-svn-id: http://core.svn.wordpress.org/trunk@56019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-01 19:32:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
|
|
|
|
* While this technically still works since singular content templates are always for only one post, it results in
|
|
|
|
* the main query loop never being entered which causes bugs in core and the plugin ecosystem.
|
|
|
|
*
|
|
|
|
* The workaround below ensures that the loop is started even for those singular templates. The while loop will by
|
|
|
|
* definition only go through a single iteration, i.e. `do_blocks()` is only called once. Additional safeguard
|
|
|
|
* checks are included to ensure the main query loop has not been tampered with and really only encompasses a
|
|
|
|
* single post.
|
|
|
|
*
|
|
|
|
* Even if the block template contained a `core/query` and `core/post-template` block referencing the main query
|
|
|
|
* loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single
|
|
|
|
* post, within the actual main query loop.
|
2023-10-27 20:18:22 +02:00
|
|
|
*
|
|
|
|
* This special logic should be skipped if the current template does not come from the current theme, in which case
|
|
|
|
* it has been injected by a plugin by hijacking the block template loader mechanism. In that case, entirely custom
|
|
|
|
* logic may be applied which is unpredictable and therefore safer to omit this special handling on.
|
Editor: Ensure main query loop is entered for singular content in block themes.
Block themes currently lack the means to trigger the main query loop for singular content, since they cannot reasonably use the `core/query` and `core/post-template` blocks which are intended only for displaying a list of posts. So far, the missing main query loop on singular block templates has been worked around by enforcing the loop in certain `core/post-*` blocks, which however causes other bugs.
This changeset ensures that the main query loop is still started for singular block theme templates, by wrapping the entire template into the loop, which will by definition only have a single cycle as it only encompasses a single post. This is currently the most reliable solution, since even if there were blocks to properly trigger the main query loop on singular content, it would be unrealistic to expect all existing block themes to update their templates accordingly. It may be revisited in the future.
Props gziolo, youknowriad, joemcgill, costdev, mukesh27, flixos90.
Fixes #58154.
See #59225, #58027.
Built from https://develop.svn.wordpress.org/trunk@56507
git-svn-id: http://core.svn.wordpress.org/trunk@56019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-01 19:32:16 +02:00
|
|
|
*/
|
2023-10-27 20:18:22 +02:00
|
|
|
if (
|
|
|
|
$_wp_current_template_id &&
|
|
|
|
str_starts_with( $_wp_current_template_id, get_stylesheet() . '//' ) &&
|
|
|
|
is_singular() &&
|
|
|
|
1 === $wp_query->post_count &&
|
|
|
|
have_posts()
|
|
|
|
) {
|
Editor: Ensure main query loop is entered for singular content in block themes.
Block themes currently lack the means to trigger the main query loop for singular content, since they cannot reasonably use the `core/query` and `core/post-template` blocks which are intended only for displaying a list of posts. So far, the missing main query loop on singular block templates has been worked around by enforcing the loop in certain `core/post-*` blocks, which however causes other bugs.
This changeset ensures that the main query loop is still started for singular block theme templates, by wrapping the entire template into the loop, which will by definition only have a single cycle as it only encompasses a single post. This is currently the most reliable solution, since even if there were blocks to properly trigger the main query loop on singular content, it would be unrealistic to expect all existing block themes to update their templates accordingly. It may be revisited in the future.
Props gziolo, youknowriad, joemcgill, costdev, mukesh27, flixos90.
Fixes #58154.
See #59225, #58027.
Built from https://develop.svn.wordpress.org/trunk@56507
git-svn-id: http://core.svn.wordpress.org/trunk@56019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-01 19:32:16 +02:00
|
|
|
while ( have_posts() ) {
|
|
|
|
the_post();
|
|
|
|
$content = do_blocks( $content );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$content = do_blocks( $content );
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:20:57 +02:00
|
|
|
$content = wptexturize( $content );
|
2022-01-18 04:39:26 +01:00
|
|
|
$content = convert_smilies( $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 19:34:17 +01:00
|
|
|
$content = wp_filter_content_tags( $content, 'template' );
|
2021-05-25 16:20:57 +02: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 21:05:57 +02:00
|
|
|
* This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
|
|
|
|
*
|
2021-05-25 16:20:57 +02: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 11:42:56 +02: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 16:20:57 +02:00
|
|
|
*/
|
|
|
|
if ( isset( $context['postType'] ) && 'wp_template' === $context['postType'] ) {
|
|
|
|
unset( $context['postId'] );
|
|
|
|
unset( $context['postType'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
2021-12-03 20:39:00 +01: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 17:48:15 +02:00
|
|
|
if ( ! $wp_query->is_main_query() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:39:00 +01: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' );
|
|
|
|
}
|
|
|
|
}
|
2024-09-20 04:07:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a block template.
|
|
|
|
*
|
|
|
|
* @since 6.7.0
|
|
|
|
*
|
|
|
|
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
|
|
|
|
* @param array|string $args {
|
|
|
|
* @type string $title Optional. Title of the template as it will be shown in the Site Editor
|
|
|
|
* and other UI elements.
|
|
|
|
* @type string $description Optional. Description of the template as it will be shown in the Site
|
|
|
|
* Editor.
|
|
|
|
* @type string $content Optional. Default content of the template that will be used when the
|
|
|
|
* template is rendered or edited in the editor.
|
|
|
|
* @type string[] $post_types Optional. Array of post types to which the template should be available.
|
|
|
|
* @type string $plugin Optional. Slug of the plugin that registers the template.
|
|
|
|
* }
|
|
|
|
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
|
|
|
|
*/
|
2024-10-10 00:11:16 +02:00
|
|
|
function register_block_template( $template_name, $args = array() ) {
|
2024-09-20 04:07:12 +02:00
|
|
|
return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister a block template.
|
|
|
|
*
|
|
|
|
* @since 6.7.0
|
|
|
|
*
|
|
|
|
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
|
|
|
|
* @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
|
|
|
|
* template doesn't exist.
|
|
|
|
*/
|
2024-10-10 00:11:16 +02:00
|
|
|
function unregister_block_template( $template_name ) {
|
2024-09-20 04:07:12 +02:00
|
|
|
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
|
|
|
|
}
|