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.


git-svn-id: https://develop.svn.wordpress.org/trunk@52062 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Robert Anderson
2021-11-08 23:09:53 +00:00
parent 81b29a2865
commit 39e33bed83
18 changed files with 1828 additions and 44 deletions

View File

@@ -156,6 +156,9 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
if ( isset( $request['area'] ) ) {
$query['area'] = $request['area'];
}
if ( isset( $request['post_type'] ) ) {
$query['post_type'] = $request['post_type'];
}
$templates = array();
foreach ( get_block_templates( $query, $this->post_type ) as $template ) {
@@ -187,7 +190,11 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
* @return WP_REST_Response|WP_Error
*/
public function get_item( $request ) {
$template = get_block_template( $request['id'], $this->post_type );
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
$template = get_block_file_template( $request['id'], $this->post_type );
} else {
$template = get_block_template( $request['id'], $this->post_type );
}
if ( ! $template ) {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
@@ -222,6 +229,11 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
}
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
wp_delete_post( $template->wp_id, true );
return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request );
}
$changes = $this->prepare_item_for_database( $request );
if ( 'custom' === $template->source ) {
@@ -395,6 +407,16 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
$changes->post_excerpt = $template->description;
}
if ( 'wp_template_part' === $this->post_type ) {
if ( isset( $request['area'] ) ) {
$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] );
} elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
$changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area );
} elseif ( ! $template->area ) {
$changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
}
}
return $changes;
}
@@ -502,16 +524,25 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
* Retrieves the query params for the posts collection.
*
* @since 5.8.0
* @since 5.9.0 Added `'area'` and `'post_type'`.
*
* @return array Collection parameters.
*/
public function get_collection_params() {
return array(
'context' => $this->get_context_param(),
'wp_id' => array(
'context' => $this->get_context_param(),
'wp_id' => array(
'description' => __( 'Limit to the specified post id.' ),
'type' => 'integer',
),
'area' => array(
'description' => __( 'Limit to the specified template part area.' ),
'type' => 'string',
),
'post_type' => array(
'description' => __( 'Post type to get the templates for.' ),
'type' => 'string',
),
);
}
@@ -519,6 +550,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
* Retrieves the block type' schema, conforming to JSON Schema.
*
* @since 5.8.0
* @since 5.9.0 Added `'area'`.
*
* @return array Item schema data.
*/
@@ -596,6 +628,14 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
),
);
if ( 'wp_template_part' === $this->post_type ) {
$schema['properties']['area'] = array(
'description' => __( 'Where the template part is intended for use (header, footer, etc.)' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
);
}
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );