locate_block_template()
最后更新于:2021-11-27 14:21:26
locate_block_template( string$template, string$type, array$templates)Find a block template with equal or higher specificity than a given PHP template file.
参数
- $template
-
(string) (Required) Path to the template. See locate_template().
- $type
-
(string) (Required) Sanitized filename without extension.
- $templates
-
(array) (Required) A list of template candidates, in descending order of priority.
响应
(string) The path to the Full Site Editing template canvas file, or the fallback PHP template.
源文件
文件: gc-includes/block-template.php
function locate_block_template( $template, $type, array $templates ) {
global $_gc_current_template_content;
if ( $template ) {
/*
* 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.
*/
// 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 );
// If the template hiearchy algorithm has successfully located a PHP template file,
// we will only consider block templates with higher or equal specificity.
$templates = array_slice( $templates, 0, $index + 1 );
}
$block_template = resolve_block_template( $type, $templates );
if ( $block_template ) {
if ( empty( $block_template->content ) && is_user_logged_in() ) {
$_gc_current_template_content =
sprintf(
/* translators: %s: Template title */
__( 'Empty template: %s' ),
$block_template->title
);
} elseif ( ! empty( $block_template->content ) ) {
$_gc_current_template_content = $block_template->content;
}
if ( isset( $_GET['_gc-find-template'] ) ) {
gc_send_json_success( $block_template );
}
} else {
if ( $template ) {
return $template;
}
if ( 'index' === $type ) {
if ( isset( $_GET['_gc-find-template'] ) ) {
gc_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 viegcort meta tag.
add_action( 'gc_head', '_block_template_viegcort_meta_tag', 0 );
// Render title tag with content, regardless of whether theme has title-tag support.
remove_action( 'gc_head', '_gc_render_title_tag', 1 ); // Remove conditional title tag rendering...
add_action( 'gc_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 . GCINC . '/template-canvas.php';
}