Make WordPress Core


Ignore:
Timestamp:
09/20/2022 09:19:10 PM (4 years ago)
Author:
hellofromTonya
Message:

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 ​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 ​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 ​Gutenberg PR 42520 for more details.

  • Fixes a typo in default category template's description within get_default_block_template_types().

See ​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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r54210 r54269  
    148148                'category'       => array(
    149149                        'title'       => _x( 'Category', 'Template name' ),
    150                         'description' => __( 'Displays latest posts in single post category.' ),
     150                        'description' => __( 'Displays latest posts from a single post category.' ),
    151151                ),
    152152                'taxonomy'       => array(
    … …  
    556556        $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;
    557557
    558         $origin = get_post_meta( $post->ID, 'origin', true );
     558        $origin           = get_post_meta( $post->ID, 'origin', true );
     559        $is_wp_suggestion = get_post_meta( $post->ID, 'is_wp_suggestion', true );
    559560
    560561        $template                 = new WP_Block_Template();
    … …  
    571572        $template->status         = $post->post_status;
    572573        $template->has_theme_file = $has_theme_file;
    573         $template->is_custom      = true;
     574        $template->is_custom      = empty( $is_wp_suggestion );
    574575        $template->author         = $post->post_author;
    575576
    … …  
    680681                }
    681682
    682                 if ( $post_type &&
     683                if (
     684                        $post_type &&
    683685                        isset( $template->post_types ) &&
    684686                        ! in_array( $post_type, $template->post_types, true )
    … …  
    913915 */
    914916function wp_is_theme_directory_ignored( $path ) {
    915         $directories_to_ignore = array( '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
     917        $directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
     918
    916919        foreach ( $directories_to_ignore as $directory ) {
    917                 if ( strpos( $path, $directory ) === 0 ) {
     920                if ( str_starts_with( $path, $directory ) ) {
    918921                        return true;
    919922                }
    … …  
    10241027        return $filename;
    10251028}
     1029
     1030/**
     1031 * Gets the template hierarchy for the given template slug to be created.
     1032 *
     1033 *
     1034 * Note: Always add `index` as the last fallback template.
     1035 *
     1036 * @since 6.1.0
     1037 *
     1038 * @param string  $slug           The template slug to be created.
     1039 * @param boolean $is_custom      Optional. Indicates if a template is custom or
     1040 *                                part of the template hierarchy. Default false.
     1041 * @param string $template_prefix Optional. The template prefix for the created template.
     1042 *                                Used to extract the main template type, e.g.
     1043 *                                in `taxonomy-books` the `taxonomy` is extracted.
     1044 *                                Default empty string.
     1045 * @return string[] The template hierarchy.
     1046 */
     1047function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
     1048        if ( 'index' === $slug ) {
     1049                return array( 'index' );
     1050        }
     1051        if ( $is_custom ) {
     1052                return array( 'page', 'singular', 'index' );
     1053        }
     1054        if ( 'front-page' === $slug ) {
     1055                return array( 'front-page', 'home', 'index' );
     1056        }
     1057
     1058        $template_hierarchy = array( $slug );
     1059
     1060        // Most default templates don't have `$template_prefix` assigned.
     1061        if ( $template_prefix ) {
     1062                list( $type ) = explode( '-', $template_prefix );
     1063                // These checks are needed because the `$slug` above is always added.
     1064                if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
     1065                        $template_hierarchy[] = $template_prefix;
     1066                }
     1067                if ( $slug !== $type ) {
     1068                        $template_hierarchy[] = $type;
     1069                }
     1070        }
     1071
     1072        // Handle `archive` template.
     1073        if (
     1074                str_starts_with( $slug, 'author' ) ||
     1075                str_starts_with( $slug, 'taxonomy' ) ||
     1076                str_starts_with( $slug, 'category' ) ||
     1077                str_starts_with( $slug, 'tag' ) ||
     1078                'date' === $slug
     1079        ) {
     1080                $template_hierarchy[] = 'archive';
     1081        }
     1082        // Handle `single` template.
     1083        if ( 'attachment' === $slug ) {
     1084                $template_hierarchy[] = 'single';
     1085        }
     1086
     1087        // Handle `singular` template.
     1088        if (
     1089                str_starts_with( $slug, 'single' ) ||
     1090                str_starts_with( $slug, 'page' ) ||
     1091                'attachment' === $slug
     1092        ) {
     1093                $template_hierarchy[] = 'singular';
     1094        }
     1095
     1096        $template_hierarchy[] = 'index';
     1097
     1098        return $template_hierarchy;
     1099};
Note: See TracChangeset for help on using the changeset viewer.