Skip to content

Commit

Permalink
Backport template creation changes from core (#44299)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras authored Sep 20, 2022
1 parent 799a3e9 commit 2c29bff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public function register_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_template_fallback' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'slug' => array(
'description' => __( 'The slug of the template to get the fallback for', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'is_custom' => array(
'description' => __( ' Indicates if a template is custom or part of the template hierarchy', 'gutenberg' ),
Expand All @@ -54,16 +55,10 @@ public function register_routes() {
* @return WP_REST_Response|WP_Error
*/
public function get_template_fallback( $request ) {
if ( empty( $request['slug'] ) ) {
return new WP_Error(
'rest_invalid_param',
__( 'Invalid slug.', 'gutenberg' ),
array( 'status' => 400 )
);
}
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
return rest_ensure_response( $fallback_template );
$response = $this->prepare_item_for_response( $fallback_template, $request );
return rest_ensure_response( $response );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default function NewTemplate( { postType } ) {
template_prefix: templatePrefix,
} ),
} );
templateContent = fallbackTemplate.content;
templateContent = fallbackTemplate.content.raw;
}
const newTemplate = await saveEntityRecord(
'postType',
Expand Down
27 changes: 10 additions & 17 deletions phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,26 @@ public function test_register_routes() {
}

public function test_get_template_fallback() {
$base_path = gutenberg_dir_path() . 'test/emptytheme/block-templates/';
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
// Should match `category.html`.
// Should fallback to `category.html`.
$request->set_param( 'slug', 'category-fruits' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'category' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'category.html' );
$this->assertEquals( $expected, $data );
// Should fallback to `index.html` .
$request->set_param( 'slug', 'tag-status' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'tag' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'index.html' );
$this->assertEquals( $expected, $data );
// Should fallback to `singular.html` .
$this->assertSame( 'category', $response->get_data()['slug'], 'Should fallback to `category.html`.' );
// Should fallback to `singular.html`.
$request->set_param( 'slug', 'page-hello' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'page' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data()->content;
$expected = file_get_contents( $base_path . 'singular.html' );
$this->assertEquals( $expected, $data );
$this->assertSame( 'singular', $response->get_data()['slug'], 'Should fallback to `singular.html`.' );
// Should fallback to `index.html`.
$request->set_param( 'slug', 'tag-rigas' );
$request->set_param( 'is_custom', false );
$request->set_param( 'template_prefix', 'tag' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html`.' );
}

public function test_context_param() {
Expand Down

0 comments on commit 2c29bff

Please sign in to comment.