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