mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Editor: Adds template types, is_wp_suggestion, and fallback template content.
This commit improves site editor templates by: * Adds a post meta `is_wp_suggestion` to templates created from the site editor. Why? To differentiate the templates created from the post editor in the Template panel in inspector controls and the templates suggested in site editor. See [https://github.com/WordPress/gutenberg/pull/41387 Gutenberg PR 41387] for more details. * Expands the template types that can be added to the site editor to include single custom post type and specific posts templates. See [https://github.com/WordPress/gutenberg/pull/41189 Gutenberg PR 41189] for more details. * Adds fallback template content on creation in site editor: * Introduces `get_template_hierarchy()` to get the template hierarchy for a given template slug to be created. * Adds a `lookup` route to `WP_REST_Templates_Controller` to get the fallback template content. See [https://github.com/WordPress/gutenberg/pull/42520 Gutenberg PR 42520] for more details. * Fixes a typo in default category template's description within `get_default_block_template_types()`. See [https://github.com/WordPress/gutenberg/pull/42586 Gutenberg PR 42586] for more details. * Changes field checks from `in_array()` to `rest_is_field_included()` in `WP_REST_Post_Types_Controller`. * Adds an `icon` field to `WP_REST_Post_Types_Controller` Follow-up to [53129], [52331], [52275], [52062], [51962], [43087]. Props ntsekouras, spacedmonkey, mamaduka, mburridge, jameskoster, bernhard-reiter, mcsf, hellofromTonya. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54269 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -147,7 +147,7 @@ function get_default_block_template_types() {
|
||||
),
|
||||
'category' => array(
|
||||
'title' => _x( 'Category', 'Template name' ),
|
||||
'description' => __( 'Displays latest posts in single post category.' ),
|
||||
'description' => __( 'Displays latest posts from a single post category.' ),
|
||||
),
|
||||
'taxonomy' => array(
|
||||
'title' => _x( 'Taxonomy', 'Template name' ),
|
||||
@@ -555,7 +555,8 @@ function _build_block_template_result_from_post( $post ) {
|
||||
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
|
||||
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;
|
||||
|
||||
$origin = get_post_meta( $post->ID, 'origin', true );
|
||||
$origin = get_post_meta( $post->ID, 'origin', true );
|
||||
$is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true );
|
||||
|
||||
$template = new WP_Block_Template();
|
||||
$template->wp_id = $post->ID;
|
||||
@@ -570,7 +571,7 @@ function _build_block_template_result_from_post( $post ) {
|
||||
$template->title = $post->post_title;
|
||||
$template->status = $post->post_status;
|
||||
$template->has_theme_file = $has_theme_file;
|
||||
$template->is_custom = true;
|
||||
$template->is_custom = empty( $is_wp_suggestion );
|
||||
$template->author = $post->post_author;
|
||||
|
||||
if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
|
||||
@@ -679,7 +680,8 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $post_type &&
|
||||
if (
|
||||
$post_type &&
|
||||
isset( $template->post_types ) &&
|
||||
! in_array( $post_type, $template->post_types, true )
|
||||
) {
|
||||
@@ -912,9 +914,10 @@ function block_footer_area() {
|
||||
* @return Bool Whether this file is in an ignored directory.
|
||||
*/
|
||||
function wp_is_theme_directory_ignored( $path ) {
|
||||
$directories_to_ignore = array( '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
|
||||
$directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
|
||||
|
||||
foreach ( $directories_to_ignore as $directory ) {
|
||||
if ( strpos( $path, $directory ) === 0 ) {
|
||||
if ( str_starts_with( $path, $directory ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1023,3 +1026,74 @@ function wp_generate_block_templates_export_file() {
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the template hierarchy for the given template slug to be created.
|
||||
*
|
||||
*
|
||||
* Note: Always add `index` as the last fallback template.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @param string $slug The template slug to be created.
|
||||
* @param boolean $is_custom Optional. Indicates if a template is custom or
|
||||
* part of the template hierarchy. Default false.
|
||||
* @param string $template_prefix Optional. The template prefix for the created template.
|
||||
* Used to extract the main template type, e.g.
|
||||
* in `taxonomy-books` the `taxonomy` is extracted.
|
||||
* Default empty string.
|
||||
* @return string[] The template hierarchy.
|
||||
*/
|
||||
function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
|
||||
if ( 'index' === $slug ) {
|
||||
return array( 'index' );
|
||||
}
|
||||
if ( $is_custom ) {
|
||||
return array( 'page', 'singular', 'index' );
|
||||
}
|
||||
if ( 'front-page' === $slug ) {
|
||||
return array( 'front-page', 'home', 'index' );
|
||||
}
|
||||
|
||||
$template_hierarchy = array( $slug );
|
||||
|
||||
// Most default templates don't have `$template_prefix` assigned.
|
||||
if ( $template_prefix ) {
|
||||
list( $type ) = explode( '-', $template_prefix );
|
||||
// These checks are needed because the `$slug` above is always added.
|
||||
if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
|
||||
$template_hierarchy[] = $template_prefix;
|
||||
}
|
||||
if ( $slug !== $type ) {
|
||||
$template_hierarchy[] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle `archive` template.
|
||||
if (
|
||||
str_starts_with( $slug, 'author' ) ||
|
||||
str_starts_with( $slug, 'taxonomy' ) ||
|
||||
str_starts_with( $slug, 'category' ) ||
|
||||
str_starts_with( $slug, 'tag' ) ||
|
||||
'date' === $slug
|
||||
) {
|
||||
$template_hierarchy[] = 'archive';
|
||||
}
|
||||
// Handle `single` template.
|
||||
if ( 'attachment' === $slug ) {
|
||||
$template_hierarchy[] = 'single';
|
||||
}
|
||||
|
||||
// Handle `singular` template.
|
||||
if (
|
||||
str_starts_with( $slug, 'single' ) ||
|
||||
str_starts_with( $slug, 'page' ) ||
|
||||
'attachment' === $slug
|
||||
) {
|
||||
$template_hierarchy[] = 'singular';
|
||||
}
|
||||
|
||||
$template_hierarchy[] = 'index';
|
||||
|
||||
return $template_hierarchy;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user