Skip to content

Commit

Permalink
Make fallback behavior the default
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 21, 2021
1 parent 190b1ed commit 21da936
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,10 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
* @return Gutenberg_Block_Template|null Template.
*/
function gutenberg_get_block_template_with_fallback( $id, $template_type = 'wp_template' ) {
// Falling back to the next best template in the template hierarchy only makes sense for `wp_template`s.
if ( 'wp_template' !== $template_type ) {
return gutenberg_get_block_template( $id, $template_type );
}
/**
* Filters the block template object before the query takes place.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function register_routes() {
'resolve' => array(
'description' => __( 'Whether to return a fallback template if no template with the given ID exists', 'gutenberg' ),
'type' => 'boolean',
'default' => false,
'default' => true,
),
),
),
Expand Down Expand Up @@ -243,12 +243,7 @@ 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 ) {
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 );
}

$template = gutenberg_get_block_template_with_fallback( $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 All @@ -273,14 +268,14 @@ public function update_item( $request ) {
return $result;
}

$template = gutenberg_get_block_template( $request['id'], $this->post_type );
$template = gutenberg_get_block_template_with_fallback( $request['id'], $this->post_type );
$fields_update = $this->update_additional_fields_for_object( $template, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}

return $this->prepare_item_for_response(
gutenberg_get_block_template( $request['id'], $this->post_type ),
gutenberg_get_block_template_with_fallback( $request['id'], $this->post_type ),
$request
);
}
Expand Down Expand Up @@ -318,14 +313,14 @@ public function create_item( $request ) {
return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.', 'gutenberg' ) );
}
$id = $posts[0]->id;
$template = gutenberg_get_block_template( $id, $this->post_type );
$template = gutenberg_get_block_template_with_fallback( $id, $this->post_type );
$fields_update = $this->update_additional_fields_for_object( $template, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}

return $this->prepare_item_for_response(
gutenberg_get_block_template( $id, $this->post_type ),
gutenberg_get_block_template_with_fallback( $id, $this->post_type ),
$request
);
}
Expand Down Expand Up @@ -394,7 +389,7 @@ public function delete_item( $request ) {
* @return stdClass Changes to pass to wp_update_post.
*/
protected function prepare_item_for_database( $request ) {
$template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null;
$template = $request['id'] ? gutenberg_get_block_template_with_fallback( $request['id'], $this->post_type ) : null;
$changes = new stdClass();
if ( null === $template ) {
$changes->post_type = $this->post_type;
Expand Down
3 changes: 1 addition & 2 deletions packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export function* setTemplate( templateId, templateSlug ) {
'getEntityRecord',
'postType',
'wp_template',
templateId,
{ resolve: true } // We want the editor to display what the frontend would render.
templateId
);
pageContext.templateSlug = template?.slug;
}
Expand Down

0 comments on commit 21da936

Please sign in to comment.