Make WordPress Core


Ignore:
Timestamp:
09/20/2022 09:19:10 PM (15 months 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/rest-api/endpoints/class-wp-rest-templates-controller.php

    r54121 r54269  
    4343     *
    4444     * @since 5.8.0
     45     * @since 6.1.0 Endpoint for fallback template content.
    4546     */
    4647    public function register_routes() {
     
    6364                ),
    6465                'schema' => array( $this, 'get_public_item_schema' ),
     66            )
     67        );
     68
     69        // Get fallback template content.
     70        register_rest_route(
     71            $this->namespace,
     72            '/' . $this->rest_base . '/lookup',
     73            array(
     74                array(
     75                    'methods'             => WP_REST_Server::READABLE,
     76                    'callback'            => array( $this, 'get_template_fallback' ),
     77                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
     78                    'args'                => array(
     79                        'slug'            => array(
     80                            'description' => __( 'The slug of the template to get the fallback for' ),
     81                            'type'        => 'string',
     82                            'required'    => true,
     83                        ),
     84                        'is_custom'       => array(
     85                            'description' => __( ' Indicates if a template is custom or part of the template hierarchy' ),
     86                            'type'        => 'boolean',
     87                        ),
     88                        'template_prefix' => array(
     89                            'description' => __( 'The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`' ),
     90                            'type'        => 'string',
     91                        ),
     92                    ),
     93                ),
    6594            )
    6695        );
     
    116145            )
    117146        );
     147    }
     148
     149    /**
     150     * Returns the fallback template for the given slug.
     151     *
     152     * @since 6.1.0
     153     *
     154     * @param WP_REST_Request $request The request instance.
     155     * @return WP_REST_Response|WP_Error
     156     */
     157    public function get_template_fallback( $request ) {
     158        $hierarchy         = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
     159        $fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
     160        $response          = $this->prepare_item_for_response( $fallback_template, $request );
     161        return rest_ensure_response( $response );
    118162    }
    119163
     
    526570        }
    527571
     572        if ( 'wp_template' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) {
     573            $changes->meta_input     = wp_parse_args(
     574                array(
     575                    'is_wp_suggestion' => $request['is_wp_suggestion'],
     576                ),
     577                $changes->meta_input = array()
     578            );
     579        }
     580
    528581        if ( 'wp_template_part' === $this->post_type ) {
    529582            if ( isset( $request['area'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.