Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: block-template.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Block template loader functions.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Adds necessary hooks to resolve '_wp-find-template' requests.
[8] Fix | Delete
*
[9] Fix | Delete
* @access private
[10] Fix | Delete
* @since 5.9.0
[11] Fix | Delete
*/
[12] Fix | Delete
function _add_template_loader_filters() {
[13] Fix | Delete
if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
[14] Fix | Delete
add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
[15] Fix | Delete
}
[16] Fix | Delete
}
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Renders a warning screen for empty block templates.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 6.8.0
[22] Fix | Delete
*
[23] Fix | Delete
* @param WP_Block_Template $block_template The block template object.
[24] Fix | Delete
* @return string The warning screen HTML.
[25] Fix | Delete
*/
[26] Fix | Delete
function wp_render_empty_block_template_warning( $block_template ) {
[27] Fix | Delete
wp_enqueue_style( 'wp-empty-template-alert' );
[28] Fix | Delete
return sprintf(
[29] Fix | Delete
/* translators: %1$s: Block template title. %2$s: Empty template warning message. %3$s: Edit template link. %4$s: Edit template button label. */
[30] Fix | Delete
'<div id="wp-empty-template-alert">
[31] Fix | Delete
<h2>%1$s</h2>
[32] Fix | Delete
<p>%2$s</p>
[33] Fix | Delete
<a href="%3$s" class="wp-element-button">
[34] Fix | Delete
%4$s
[35] Fix | Delete
</a>
[36] Fix | Delete
</div>',
[37] Fix | Delete
esc_html( $block_template->title ),
[38] Fix | Delete
__( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ),
[39] Fix | Delete
get_edit_post_link( $block_template->wp_id, 'site-editor' ),
[40] Fix | Delete
__( 'Edit template' )
[41] Fix | Delete
);
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Finds a block template with equal or higher specificity than a given PHP template file.
[46] Fix | Delete
*
[47] Fix | Delete
* Internally, this communicates the block content that needs to be used by the template canvas through a global variable.
[48] Fix | Delete
*
[49] Fix | Delete
* @since 5.8.0
[50] Fix | Delete
* @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar.
[51] Fix | Delete
*
[52] Fix | Delete
* @global string $_wp_current_template_content
[53] Fix | Delete
* @global string $_wp_current_template_id
[54] Fix | Delete
*
[55] Fix | Delete
* @param string $template Path to the template. See locate_template().
[56] Fix | Delete
* @param string $type Sanitized filename without extension.
[57] Fix | Delete
* @param string[] $templates A list of template candidates, in descending order of priority.
[58] Fix | Delete
* @return string The path to the Site Editor template canvas file, or the fallback PHP template.
[59] Fix | Delete
*/
[60] Fix | Delete
function locate_block_template( $template, $type, array $templates ) {
[61] Fix | Delete
global $_wp_current_template_content, $_wp_current_template_id;
[62] Fix | Delete
[63] Fix | Delete
if ( ! current_theme_supports( 'block-templates' ) ) {
[64] Fix | Delete
return $template;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
if ( $template ) {
[68] Fix | Delete
/*
[69] Fix | Delete
* locate_template() has found a PHP template at the path specified by $template.
[70] Fix | Delete
* That means that we have a fallback candidate if we cannot find a block template
[71] Fix | Delete
* with higher specificity.
[72] Fix | Delete
*
[73] Fix | Delete
* Thus, before looking for matching block themes, we shorten our list of candidate
[74] Fix | Delete
* templates accordingly.
[75] Fix | Delete
*/
[76] Fix | Delete
[77] Fix | Delete
// Locate the index of $template (without the theme directory path) in $templates.
[78] Fix | Delete
$relative_template_path = str_replace(
[79] Fix | Delete
array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),
[80] Fix | Delete
'',
[81] Fix | Delete
$template
[82] Fix | Delete
);
[83] Fix | Delete
$index = array_search( $relative_template_path, $templates, true );
[84] Fix | Delete
[85] Fix | Delete
// If the template hierarchy algorithm has successfully located a PHP template file,
[86] Fix | Delete
// we will only consider block templates with higher or equal specificity.
[87] Fix | Delete
$templates = array_slice( $templates, 0, $index + 1 );
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
$block_template = resolve_block_template( $type, $templates, $template );
[91] Fix | Delete
[92] Fix | Delete
if ( $block_template ) {
[93] Fix | Delete
$_wp_current_template_id = $block_template->id;
[94] Fix | Delete
[95] Fix | Delete
if ( empty( $block_template->content ) ) {
[96] Fix | Delete
if ( is_user_logged_in() ) {
[97] Fix | Delete
$_wp_current_template_content = wp_render_empty_block_template_warning( $block_template );
[98] Fix | Delete
} else {
[99] Fix | Delete
if ( $block_template->has_theme_file ) {
[100] Fix | Delete
// Show contents from theme template if user is not logged in.
[101] Fix | Delete
$theme_template = _get_block_template_file( 'wp_template', $block_template->slug );
[102] Fix | Delete
$_wp_current_template_content = file_get_contents( $theme_template['path'] );
[103] Fix | Delete
} else {
[104] Fix | Delete
$_wp_current_template_content = $block_template->content;
[105] Fix | Delete
}
[106] Fix | Delete
}
[107] Fix | Delete
} elseif ( ! empty( $block_template->content ) ) {
[108] Fix | Delete
$_wp_current_template_content = $block_template->content;
[109] Fix | Delete
}
[110] Fix | Delete
if ( isset( $_GET['_wp-find-template'] ) ) {
[111] Fix | Delete
wp_send_json_success( $block_template );
[112] Fix | Delete
}
[113] Fix | Delete
} else {
[114] Fix | Delete
if ( $template ) {
[115] Fix | Delete
return $template;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if ( 'index' === $type ) {
[119] Fix | Delete
if ( isset( $_GET['_wp-find-template'] ) ) {
[120] Fix | Delete
wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );
[121] Fix | Delete
}
[122] Fix | Delete
} else {
[123] Fix | Delete
return ''; // So that the template loader keeps looking for templates.
[124] Fix | Delete
}
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
// Add hooks for template canvas.
[128] Fix | Delete
// Add viewport meta tag.
[129] Fix | Delete
add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 );
[130] Fix | Delete
[131] Fix | Delete
// Render title tag with content, regardless of whether theme has title-tag support.
[132] Fix | Delete
remove_action( 'wp_head', '_wp_render_title_tag', 1 ); // Remove conditional title tag rendering...
[133] Fix | Delete
add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional.
[134] Fix | Delete
[135] Fix | Delete
// This file will be included instead of the theme's template file.
[136] Fix | Delete
return ABSPATH . WPINC . '/template-canvas.php';
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Returns the correct 'wp_template' to render for the request template type.
[141] Fix | Delete
*
[142] Fix | Delete
* @access private
[143] Fix | Delete
* @since 5.8.0
[144] Fix | Delete
* @since 5.9.0 Added the `$fallback_template` parameter.
[145] Fix | Delete
*
[146] Fix | Delete
* @param string $template_type The current template type.
[147] Fix | Delete
* @param string[] $template_hierarchy The current template hierarchy, ordered by priority.
[148] Fix | Delete
* @param string $fallback_template A PHP fallback template to use if no matching block template is found.
[149] Fix | Delete
* @return WP_Block_Template|null template A template object, or null if none could be found.
[150] Fix | Delete
*/
[151] Fix | Delete
function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) {
[152] Fix | Delete
if ( ! $template_type ) {
[153] Fix | Delete
return null;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
if ( empty( $template_hierarchy ) ) {
[157] Fix | Delete
$template_hierarchy = array( $template_type );
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$slugs = array_map(
[161] Fix | Delete
'_strip_template_file_suffix',
[162] Fix | Delete
$template_hierarchy
[163] Fix | Delete
);
[164] Fix | Delete
[165] Fix | Delete
// Find all potential templates 'wp_template' post matching the hierarchy.
[166] Fix | Delete
$query = array(
[167] Fix | Delete
'slug__in' => $slugs,
[168] Fix | Delete
);
[169] Fix | Delete
$templates = get_block_templates( $query );
[170] Fix | Delete
[171] Fix | Delete
// Order these templates per slug priority.
[172] Fix | Delete
// Build map of template slugs to their priority in the current hierarchy.
[173] Fix | Delete
$slug_priorities = array_flip( $slugs );
[174] Fix | Delete
[175] Fix | Delete
usort(
[176] Fix | Delete
$templates,
[177] Fix | Delete
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
[178] Fix | Delete
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
[179] Fix | Delete
}
[180] Fix | Delete
);
[181] Fix | Delete
[182] Fix | Delete
$theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR;
[183] Fix | Delete
$parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR;
[184] Fix | Delete
[185] Fix | Delete
// Is the active theme a child theme, and is the PHP fallback template part of it?
[186] Fix | Delete
if (
[187] Fix | Delete
str_starts_with( $fallback_template, $theme_base_path ) &&
[188] Fix | Delete
! str_contains( $fallback_template, $parent_theme_base_path )
[189] Fix | Delete
) {
[190] Fix | Delete
$fallback_template_slug = substr(
[191] Fix | Delete
$fallback_template,
[192] Fix | Delete
// Starting position of slug.
[193] Fix | Delete
strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ),
[194] Fix | Delete
// Remove '.php' suffix.
[195] Fix | Delete
-4
[196] Fix | Delete
);
[197] Fix | Delete
[198] Fix | Delete
// Is our candidate block template's slug identical to our PHP fallback template's?
[199] Fix | Delete
if (
[200] Fix | Delete
count( $templates ) &&
[201] Fix | Delete
$fallback_template_slug === $templates[0]->slug &&
[202] Fix | Delete
'theme' === $templates[0]->source
[203] Fix | Delete
) {
[204] Fix | Delete
// Unfortunately, we cannot trust $templates[0]->theme, since it will always
[205] Fix | Delete
// be set to the active theme's slug by _build_block_template_result_from_file(),
[206] Fix | Delete
// even if the block template is really coming from the active theme's parent.
[207] Fix | Delete
// (The reason for this is that we want it to be associated with the active theme
[208] Fix | Delete
// -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.)
[209] Fix | Delete
// Instead, we use _get_block_template_file() to locate the block template file.
[210] Fix | Delete
$template_file = _get_block_template_file( 'wp_template', $fallback_template_slug );
[211] Fix | Delete
if ( $template_file && get_template() === $template_file['theme'] ) {
[212] Fix | Delete
// The block template is part of the parent theme, so we
[213] Fix | Delete
// have to give precedence to the child theme's PHP template.
[214] Fix | Delete
array_shift( $templates );
[215] Fix | Delete
}
[216] Fix | Delete
}
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
return count( $templates ) ? $templates[0] : null;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Displays title tag with content, regardless of whether theme has title-tag support.
[224] Fix | Delete
*
[225] Fix | Delete
* @access private
[226] Fix | Delete
* @since 5.8.0
[227] Fix | Delete
*
[228] Fix | Delete
* @see _wp_render_title_tag()
[229] Fix | Delete
*/
[230] Fix | Delete
function _block_template_render_title_tag() {
[231] Fix | Delete
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* Returns the markup for the current template.
[236] Fix | Delete
*
[237] Fix | Delete
* @access private
[238] Fix | Delete
* @since 5.8.0
[239] Fix | Delete
*
[240] Fix | Delete
* @global string $_wp_current_template_id
[241] Fix | Delete
* @global string $_wp_current_template_content
[242] Fix | Delete
* @global WP_Embed $wp_embed WordPress Embed object.
[243] Fix | Delete
* @global WP_Query $wp_query WordPress Query object.
[244] Fix | Delete
*
[245] Fix | Delete
* @return string Block template markup.
[246] Fix | Delete
*/
[247] Fix | Delete
function get_the_block_template_html() {
[248] Fix | Delete
global $_wp_current_template_id, $_wp_current_template_content, $wp_embed, $wp_query;
[249] Fix | Delete
[250] Fix | Delete
if ( ! $_wp_current_template_content ) {
[251] Fix | Delete
if ( is_user_logged_in() ) {
[252] Fix | Delete
return '<h1>' . esc_html__( 'No matching template found' ) . '</h1>';
[253] Fix | Delete
}
[254] Fix | Delete
return '';
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
$content = $wp_embed->run_shortcode( $_wp_current_template_content );
[258] Fix | Delete
$content = $wp_embed->autoembed( $content );
[259] Fix | Delete
$content = shortcode_unautop( $content );
[260] Fix | Delete
$content = do_shortcode( $content );
[261] Fix | Delete
[262] Fix | Delete
/*
[263] Fix | Delete
* Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
[264] Fix | Delete
* While this technically still works since singular content templates are always for only one post, it results in
[265] Fix | Delete
* the main query loop never being entered which causes bugs in core and the plugin ecosystem.
[266] Fix | Delete
*
[267] Fix | Delete
* The workaround below ensures that the loop is started even for those singular templates. The while loop will by
[268] Fix | Delete
* definition only go through a single iteration, i.e. `do_blocks()` is only called once. Additional safeguard
[269] Fix | Delete
* checks are included to ensure the main query loop has not been tampered with and really only encompasses a
[270] Fix | Delete
* single post.
[271] Fix | Delete
*
[272] Fix | Delete
* Even if the block template contained a `core/query` and `core/post-template` block referencing the main query
[273] Fix | Delete
* loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single
[274] Fix | Delete
* post, within the actual main query loop.
[275] Fix | Delete
*
[276] Fix | Delete
* This special logic should be skipped if the current template does not come from the current theme, in which case
[277] Fix | Delete
* it has been injected by a plugin by hijacking the block template loader mechanism. In that case, entirely custom
[278] Fix | Delete
* logic may be applied which is unpredictable and therefore safer to omit this special handling on.
[279] Fix | Delete
*/
[280] Fix | Delete
if (
[281] Fix | Delete
$_wp_current_template_id &&
[282] Fix | Delete
str_starts_with( $_wp_current_template_id, get_stylesheet() . '//' ) &&
[283] Fix | Delete
is_singular() &&
[284] Fix | Delete
1 === $wp_query->post_count &&
[285] Fix | Delete
have_posts()
[286] Fix | Delete
) {
[287] Fix | Delete
while ( have_posts() ) {
[288] Fix | Delete
the_post();
[289] Fix | Delete
$content = do_blocks( $content );
[290] Fix | Delete
}
[291] Fix | Delete
} else {
[292] Fix | Delete
$content = do_blocks( $content );
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
$content = wptexturize( $content );
[296] Fix | Delete
$content = convert_smilies( $content );
[297] Fix | Delete
$content = wp_filter_content_tags( $content, 'template' );
[298] Fix | Delete
$content = str_replace( ']]>', ']]&gt;', $content );
[299] Fix | Delete
[300] Fix | Delete
// Wrap block template in .wp-site-blocks to allow for specific descendant styles
[301] Fix | Delete
// (e.g. `.wp-site-blocks > *`).
[302] Fix | Delete
return '<div class="wp-site-blocks">' . $content . '</div>';
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Renders a 'viewport' meta tag.
[307] Fix | Delete
*
[308] Fix | Delete
* This is hooked into {@see 'wp_head'} to decouple its output from the default template canvas.
[309] Fix | Delete
*
[310] Fix | Delete
* @access private
[311] Fix | Delete
* @since 5.8.0
[312] Fix | Delete
*/
[313] Fix | Delete
function _block_template_viewport_meta_tag() {
[314] Fix | Delete
echo '<meta name="viewport" content="width=device-width, initial-scale=1" />' . "\n";
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
/**
[318] Fix | Delete
* Strips .php or .html suffix from template file names.
[319] Fix | Delete
*
[320] Fix | Delete
* @access private
[321] Fix | Delete
* @since 5.8.0
[322] Fix | Delete
*
[323] Fix | Delete
* @param string $template_file Template file name.
[324] Fix | Delete
* @return string Template file name without extension.
[325] Fix | Delete
*/
[326] Fix | Delete
function _strip_template_file_suffix( $template_file ) {
[327] Fix | Delete
return preg_replace( '/\.(php|html)$/', '', $template_file );
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* Removes post details from block context when rendering a block template.
[332] Fix | Delete
*
[333] Fix | Delete
* @access private
[334] Fix | Delete
* @since 5.8.0
[335] Fix | Delete
*
[336] Fix | Delete
* @param array $context Default context.
[337] Fix | Delete
*
[338] Fix | Delete
* @return array Filtered context.
[339] Fix | Delete
*/
[340] Fix | Delete
function _block_template_render_without_post_block_context( $context ) {
[341] Fix | Delete
/*
[342] Fix | Delete
* When loading a template directly and not through a page that resolves it,
[343] Fix | Delete
* the top-level post ID and type context get set to that of the template.
[344] Fix | Delete
* Templates are just the structure of a site, and they should not be available
[345] Fix | Delete
* as post context because blocks like Post Content would recurse infinitely.
[346] Fix | Delete
*/
[347] Fix | Delete
if ( isset( $context['postType'] ) && 'wp_template' === $context['postType'] ) {
[348] Fix | Delete
unset( $context['postId'] );
[349] Fix | Delete
unset( $context['postType'] );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
return $context;
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
/**
[356] Fix | Delete
* Sets the current WP_Query to return auto-draft posts.
[357] Fix | Delete
*
[358] Fix | Delete
* The auto-draft status indicates a new post, so allow the the WP_Query instance to
[359] Fix | Delete
* return an auto-draft post for template resolution when editing a new post.
[360] Fix | Delete
*
[361] Fix | Delete
* @access private
[362] Fix | Delete
* @since 5.9.0
[363] Fix | Delete
*
[364] Fix | Delete
* @param WP_Query $wp_query Current WP_Query instance, passed by reference.
[365] Fix | Delete
*/
[366] Fix | Delete
function _resolve_template_for_new_post( $wp_query ) {
[367] Fix | Delete
if ( ! $wp_query->is_main_query() ) {
[368] Fix | Delete
return;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
[372] Fix | Delete
[373] Fix | Delete
// Pages.
[374] Fix | Delete
$page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null;
[375] Fix | Delete
[376] Fix | Delete
// Posts, including custom post types.
[377] Fix | Delete
$p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null;
[378] Fix | Delete
[379] Fix | Delete
$post_id = $page_id ? $page_id : $p;
[380] Fix | Delete
$post = get_post( $post_id );
[381] Fix | Delete
[382] Fix | Delete
if (
[383] Fix | Delete
$post &&
[384] Fix | Delete
'auto-draft' === $post->post_status &&
[385] Fix | Delete
current_user_can( 'edit_post', $post->ID )
[386] Fix | Delete
) {
[387] Fix | Delete
$wp_query->set( 'post_status', 'auto-draft' );
[388] Fix | Delete
}
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* Register a block template.
[393] Fix | Delete
*
[394] Fix | Delete
* @since 6.7.0
[395] Fix | Delete
*
[396] Fix | Delete
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
[397] Fix | Delete
* @param array|string $args {
[398] Fix | Delete
* @type string $title Optional. Title of the template as it will be shown in the Site Editor
[399] Fix | Delete
* and other UI elements.
[400] Fix | Delete
* @type string $description Optional. Description of the template as it will be shown in the Site
[401] Fix | Delete
* Editor.
[402] Fix | Delete
* @type string $content Optional. Default content of the template that will be used when the
[403] Fix | Delete
* template is rendered or edited in the editor.
[404] Fix | Delete
* @type string[] $post_types Optional. Array of post types to which the template should be available.
[405] Fix | Delete
* @type string $plugin Optional. Slug of the plugin that registers the template.
[406] Fix | Delete
* }
[407] Fix | Delete
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
[408] Fix | Delete
*/
[409] Fix | Delete
function register_block_template( $template_name, $args = array() ) {
[410] Fix | Delete
return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Unregister a block template.
[415] Fix | Delete
*
[416] Fix | Delete
* @since 6.7.0
[417] Fix | Delete
*
[418] Fix | Delete
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
[419] Fix | Delete
* @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
[420] Fix | Delete
* template doesn't exist.
[421] Fix | Delete
*/
[422] Fix | Delete
function unregister_block_template( $template_name ) {
[423] Fix | Delete
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function