Skip to content

Commit

Permalink
Introduce resolve flag, use in site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 21, 2021
1 parent aaabafc commit 190b1ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function register_routes() {
'type' => 'string',
'sanitize_callback' => array( $this, '_sanitize_template_id' ),
),
'resolve' => array(
'description' => __( 'Whether to return a fallback template if no template with the given ID exists', 'gutenberg' ),
'type' => 'boolean',
'default' => false,
),
),
),
array(
Expand Down Expand Up @@ -208,6 +213,8 @@ public function get_item_permissions_check( $request ) {
public function get_item( $request ) {
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
$template = get_block_file_template( $request['id'], $this->post_type );
} elseif ( isset( $request['resolve'] ) && true === $request['resolve'] ) {
$template = gutenberg_get_block_template_with_fallback( $request['id'], $this->post_type );
} else {
$template = gutenberg_get_block_template( $request['id'], $this->post_type );
}
Expand Down Expand Up @@ -236,7 +243,12 @@ public function update_item_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item( $request ) {
$template = gutenberg_get_block_template( $request['id'], $this->post_type );
if ( isset( $request['resolve'] ) && true === $request['resolve'] ) {
$template = gutenberg_get_block_template_with_fallback( $request['id'], $this->post_type );
} else {
$template = gutenberg_get_block_template( $request['id'], $this->post_type );
}

if ( ! $template ) {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) );
}
Expand Down Expand Up @@ -336,7 +348,7 @@ public function delete_item_permissions_check( $request ) {
*/
public function delete_item( $request ) {
$template = gutenberg_get_block_template( $request['id'], $this->post_type );
if ( ! $template || $template->id !== $request['id'] ) { // Make sure there is a template for this ID (and not just a fallback one).
if ( ! $template ) {
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.', 'gutenberg' ), array( 'status' => 404 ) );
}
if ( 'custom' !== $template->source ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function* setTemplate( templateId, templateSlug ) {
'getEntityRecord',
'postType',
'wp_template',
templateId
templateId,
{ resolve: true } // We want the editor to display what the frontend would render.
);
pageContext.templateSlug = template?.slug;
}
Expand Down

0 comments on commit 190b1ed

Please sign in to comment.