Make WordPress Core


Ignore:
Timestamp:
11/08/2021 11:09:53 PM (3 years ago)
Author:
noisysocks
Message:

Editor: Add block theme infrastructure

Adds the required infrastructure to render block-based themes. This is sourced
from the Gutenberg plugin.

Fixes #54335.
Props bernhard-reiter, youknowriad, ntsekouras, hellofromtonya.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r51962 r52062  
    157157            $query['area'] = $request['area'];
    158158        }
     159        if ( isset( $request['post_type'] ) ) {
     160            $query['post_type'] = $request['post_type'];
     161        }
    159162
    160163        $templates = array();
     
    188191     */
    189192    public function get_item( $request ) {
    190         $template = get_block_template( $request['id'], $this->post_type );
     193        if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
     194            $template = get_block_file_template( $request['id'], $this->post_type );
     195        } else {
     196            $template = get_block_template( $request['id'], $this->post_type );
     197        }
    191198
    192199        if ( ! $template ) {
     
    221228        if ( ! $template ) {
    222229            return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
     230        }
     231
     232        if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
     233            wp_delete_post( $template->wp_id, true );
     234            return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request );
    223235        }
    224236
     
    396408        }
    397409
     410        if ( 'wp_template_part' === $this->post_type ) {
     411            if ( isset( $request['area'] ) ) {
     412                $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] );
     413            } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
     414                $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
     415            } elseif ( ! $template->area ) {
     416                $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
     417            }
     418        }
     419
    398420        return $changes;
    399421    }
     
    503525     *
    504526     * @since 5.8.0
     527     * @since 5.9.0 Added `'area'` and `'post_type'`.
    505528     *
    506529     * @return array Collection parameters.
     
    508531    public function get_collection_params() {
    509532        return array(
    510             'context' => $this->get_context_param(),
    511             'wp_id'   => array(
     533            'context'   => $this->get_context_param(),
     534            'wp_id'     => array(
    512535                'description' => __( 'Limit to the specified post id.' ),
    513536                'type'        => 'integer',
    514537            ),
     538            'area'      => array(
     539                'description' => __( 'Limit to the specified template part area.' ),
     540                'type'        => 'string',
     541            ),
     542            'post_type' => array(
     543                'description' => __( 'Post type to get the templates for.' ),
     544                'type'        => 'string',
     545            ),
    515546        );
    516547    }
     
    520551     *
    521552     * @since 5.8.0
     553     * @since 5.9.0 Added `'area'`.
    522554     *
    523555     * @return array Item schema data.
     
    597629        );
    598630
     631        if ( 'wp_template_part' === $this->post_type ) {
     632            $schema['properties']['area'] = array(
     633                'description' => __( 'Where the template part is intended for use (header, footer, etc.)' ),
     634                'type'        => 'string',
     635                'context'     => array( 'embed', 'view', 'edit' ),
     636            );
     637        }
     638
    599639        $this->schema = $schema;
    600640
Note: See TracChangeset for help on using the changeset viewer.