From 19c9a972d94d8f76271f8b91dee62f1a1a1a4098 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jun 2022 14:49:42 +0200 Subject: [PATCH 01/11] FSE: Fall back to next best template in hierarchy when querying through REST API --- .../wordpress-6.1/block-template-utils.php | 39 ++++-------- ...ss-gutenberg-rest-templates-controller.php | 17 +++++ .../add-new-template/new-template.js | 62 +++++-------------- 3 files changed, 44 insertions(+), 74 deletions(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 2521be25c3bcd..aa9a80b107377 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -195,33 +195,20 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { if ( count( $parts ) < 2 ) { return null; } - list( $theme, $slug ) = $parts; - $wp_query_args = array( - 'post_name__in' => array( $slug ), - 'post_type' => $template_type, - 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), - 'posts_per_page' => 1, - 'no_found_rows' => true, - 'tax_query' => array( - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => $theme, - ), - ), - ); - $template_query = new WP_Query( $wp_query_args ); - $posts = $template_query->posts; - - if ( count( $posts ) > 0 ) { - $template = gutenberg_build_block_template_result_from_post( $posts[0] ); - - if ( ! is_wp_error( $template ) ) { - return $template; - } + list( , $slug ) = $parts; + $block_template = resolve_block_template( $slug, array(), '' ); + if ( ! $block_template ) { + $block_template = resolve_block_template( 'index', array(), '' ); + } + // This might give us a fallback template with a different ID, + // so we have to override it to make sure it's correct. + $block_template->id = $id; + $block_template->slug = $slug; + $default_template_types = get_default_block_template_types(); + if ( array_key_exists( $slug, $default_template_types ) ) { + $block_template->title = $default_template_types[ $slug ]['title']; + $block_template->description = $default_template_types[ $slug ]['description']; } - - $block_template = get_block_file_template( $id, $template_type ); /** * Filters the queried block template object after it's been fetched. diff --git a/lib/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php b/lib/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php index 7c73d799aa843..6de5879608594 100644 --- a/lib/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php +++ b/lib/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php @@ -95,6 +95,23 @@ public function create_item( $request ) { ); } + /** + * Deletes a single template. + * + * @since 5.8.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function delete_item( $request ) { + $template = 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). + return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) ); + } + + return parent::delete_item( $request ); + } + /** * Updates a single template. * diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js index f8b2f9ec26d31..324a5cbde5dd3 100644 --- a/packages/edit-site/src/components/add-new-template/new-template.js +++ b/packages/edit-site/src/components/add-new-template/new-template.js @@ -13,7 +13,7 @@ import { NavigableMenu, } from '@wordpress/components'; import { useState } from '@wordpress/element'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { store as editorStore } from '@wordpress/editor'; import { @@ -32,7 +32,6 @@ import { tag, } from '@wordpress/icons'; import { __, sprintf } from '@wordpress/i18n'; -import { store as noticesStore } from '@wordpress/notices'; /** * Internal dependencies @@ -40,7 +39,6 @@ import { store as noticesStore } from '@wordpress/notices'; import AddCustomTemplateModal from './add-custom-template-modal'; import { usePostTypes, usePostTypesEntitiesInfo } from './utils'; import { useHistory } from '../routes'; -import { store as editSiteStore } from '../../store'; const DEFAULT_TEMPLATE_SLUGS = [ 'front-page', @@ -76,64 +74,32 @@ const TEMPLATE_ICONS = { export default function NewTemplate( { postType } ) { const history = useHistory(); const postTypes = usePostTypes(); - const [ showCustomTemplateModal, setShowCustomTemplateModal ] = - useState( false ); + const [ showCustomTemplateModal, setShowCustomTemplateModal ] = useState( + false + ); const [ entityForSuggestions, setEntityForSuggestions ] = useState( {} ); - const { existingTemplates, defaultTemplateTypes } = useSelect( + const { existingTemplates, defaultTemplateTypes, theme } = useSelect( ( select ) => ( { existingTemplates: select( coreStore ).getEntityRecords( 'postType', 'wp_template', { per_page: -1 } ), - defaultTemplateTypes: - select( editorStore ).__experimentalGetDefaultTemplateTypes(), + defaultTemplateTypes: select( + editorStore + ).__experimentalGetDefaultTemplateTypes(), + theme: select( coreStore ).getCurrentTheme(), } ), [] ); const postTypesEntitiesInfo = usePostTypesEntitiesInfo( existingTemplates ); - const { saveEntityRecord } = useDispatch( coreStore ); - const { createErrorNotice } = useDispatch( noticesStore ); - const { setTemplate } = useDispatch( editSiteStore ); async function createTemplate( template ) { - try { - const { title, description, slug } = template; - const newTemplate = await saveEntityRecord( - 'postType', - 'wp_template', - { - description, - // Slugs need to be strings, so this is for template `404` - slug: slug.toString(), - status: 'publish', - title, - // This adds a post meta field in template that is part of `is_custom` value calculation. - is_wp_suggestion: true, - }, - { throwOnError: true } - ); - - // Set template before navigating away to avoid initial stale value. - setTemplate( newTemplate.id, newTemplate.slug ); - - // Navigate to the created template editor. - history.push( { - postId: newTemplate.id, - postType: newTemplate.type, - } ); - - // TODO: Add a success notice? - } catch ( error ) { - const errorMessage = - error.message && error.code !== 'unknown_error' - ? error.message - : __( 'An error occurred while creating the template.' ); - - createErrorNotice( errorMessage, { - type: 'snackbar', - } ); - } + const { slug } = template; + history.push( { + postId: theme.stylesheet + '//' + slug.toString(), + postType: 'wp_template', + } ); } const existingTemplateSlugs = ( existingTemplates || [] ).map( ( { slug } ) => slug From e5ce7f33e3bcf6bfae7c03588f7d95f042c8a1cf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 21 Jun 2022 21:37:43 +0200 Subject: [PATCH 02/11] Correctly fall back to more generic templates for the same type --- .../wordpress-6.1/block-template-utils.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index aa9a80b107377..0597aa493c981 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -165,6 +165,22 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t return apply_filters( 'get_block_templates', $query_result, $query, $template_type ); } +function gutenberg_get_template_slugs( $template ) { + $limit = 2; + if ( strpos( $template, 'single-' ) === 0 || strpos( $template, 'taxonomy-' ) === 0 ) { + // E.g. single-post-mypost or taxonomy-recipes-vegetarian. + $limit = 3; + } + $parts = explode( '-', $template, $limit ); + $type = array_shift( $parts ); + $slugs = array( $type ); + + foreach( $parts as $part ) { + array_unshift( $slugs, $slugs[0] . '-' . $part ); + } + return $slugs; +} + /** * Retrieves a single unified template object using its id. * @@ -196,7 +212,9 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { return null; } list( , $slug ) = $parts; - $block_template = resolve_block_template( $slug, array(), '' ); + + $templates = gutenberg_get_template_slugs( $slug ); + $block_template = resolve_block_template( $slug, $templates, '' ); if ( ! $block_template ) { $block_template = resolve_block_template( 'index', array(), '' ); } From d414e0e67b91eb76add5ae04ab5debbd8caf2e89 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 22 Jun 2022 15:11:52 +0200 Subject: [PATCH 03/11] prettier --- .../src/components/add-new-template/new-template.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js index 324a5cbde5dd3..640c6f18a74cd 100644 --- a/packages/edit-site/src/components/add-new-template/new-template.js +++ b/packages/edit-site/src/components/add-new-template/new-template.js @@ -74,9 +74,8 @@ const TEMPLATE_ICONS = { export default function NewTemplate( { postType } ) { const history = useHistory(); const postTypes = usePostTypes(); - const [ showCustomTemplateModal, setShowCustomTemplateModal ] = useState( - false - ); + const [ showCustomTemplateModal, setShowCustomTemplateModal ] = + useState( false ); const [ entityForSuggestions, setEntityForSuggestions ] = useState( {} ); const { existingTemplates, defaultTemplateTypes, theme } = useSelect( ( select ) => ( { @@ -85,9 +84,8 @@ export default function NewTemplate( { postType } ) { 'wp_template', { per_page: -1 } ), - defaultTemplateTypes: select( - editorStore - ).__experimentalGetDefaultTemplateTypes(), + defaultTemplateTypes: + select( editorStore ).__experimentalGetDefaultTemplateTypes(), theme: select( coreStore ).getCurrentTheme(), } ), [] From c9ca559fc3d3701a81b3742e8084d55e63d859cb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 22 Jun 2022 15:15:36 +0200 Subject: [PATCH 04/11] phpcbf --- lib/compat/wordpress-6.1/block-template-utils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 0597aa493c981..275bbbf18f276 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -172,10 +172,10 @@ function gutenberg_get_template_slugs( $template ) { $limit = 3; } $parts = explode( '-', $template, $limit ); - $type = array_shift( $parts ); + $type = array_shift( $parts ); $slugs = array( $type ); - foreach( $parts as $part ) { + foreach ( $parts as $part ) { array_unshift( $slugs, $slugs[0] . '-' . $part ); } return $slugs; @@ -213,7 +213,7 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { } list( , $slug ) = $parts; - $templates = gutenberg_get_template_slugs( $slug ); + $templates = gutenberg_get_template_slugs( $slug ); $block_template = resolve_block_template( $slug, $templates, '' ); if ( ! $block_template ) { $block_template = resolve_block_template( 'index', array(), '' ); From 62953df1a2f7a4e23bbfb1fed5594f8133af39a4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 22 Jun 2022 17:44:34 +0200 Subject: [PATCH 05/11] Fix title and description for single templates --- .../wordpress-6.1/block-template-utils.php | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 275bbbf18f276..3a18f5930a5d8 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -223,7 +223,42 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { $block_template->id = $id; $block_template->slug = $slug; $default_template_types = get_default_block_template_types(); - if ( array_key_exists( $slug, $default_template_types ) ) { + + $slug_parts = explode( '-', $slug, 3 ); + if ( count( $slug_parts ) > 1 ) { + if ( 'single' === $slug_parts [0] ) { + // Get CPT labels + $post_type = get_post_type_object( $slug_parts[1] ); + $labels = $post_type->labels; + + if ( count( $slug_parts ) > 2 ) { + // Now we look for the CPT with slug as defined in $slug_parts[2] + $post = get_page_by_path( $slug_parts[2], OBJECT, $slug_parts[1] ); + $block_template->title = sprintf( + // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress" + __( '%1$s: %2$s' ), + $labels->singular_name, + $post->post_title + ); + $block_template->description = sprintf( + // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress" + __( 'Template for %1$s' ), + $block_template->title + ); + } else { + $block_template->title = sprintf( + // translators: %s: Name of the post type e.g: "Post". + __( 'Single item: %s' ), + $labels->singular_name + ); + $block_template->description = sprintf( + // translators: %s: Name of the post type e.g: "Post". + __( 'Displays a single item: %s.' ), + $labels->singular_name + ); + } + } + } elseif ( array_key_exists( $slug, $default_template_types ) ) { $block_template->title = $default_template_types[ $slug ]['title']; $block_template->description = $default_template_types[ $slug ]['description']; } From cdc73958cbe126a2995f6530478f2e89d81ab78e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 17:37:52 +0200 Subject: [PATCH 06/11] Add basic coverage for gutenberg_get_template_slugs --- .../block-template-utils-test.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 phpunit/compat/wordpress-6.1/block-template-utils-test.php diff --git a/phpunit/compat/wordpress-6.1/block-template-utils-test.php b/phpunit/compat/wordpress-6.1/block-template-utils-test.php new file mode 100644 index 0000000000000..047914188e061 --- /dev/null +++ b/phpunit/compat/wordpress-6.1/block-template-utils-test.php @@ -0,0 +1,26 @@ +assertSame( + array( + 'single-post-hello-world', + // Should *not* fall back to `single-post-hello`! + 'single-post', + 'single', + ), + $templates + ); + } +} From d1ee1f7a09ba572ef2ea299826c4d81b3696f59a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 18:43:30 +0200 Subject: [PATCH 07/11] Encapsulate in gutenberg_get_block_template_type_for_slug --- .../wordpress-6.1/block-template-utils.php | 97 +++++++++++-------- 1 file changed, 56 insertions(+), 41 deletions(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 3a18f5930a5d8..0c97ae0e59a5f 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -181,6 +181,57 @@ function gutenberg_get_template_slugs( $template ) { return $slugs; } +function gutenberg_get_block_template_type_for_slug( $slug ) { + $slug_parts = explode( '-', $slug, 3 ); + + if ( count( $slug_parts ) > 1 ) { + if ( 'single' === $slug_parts [0] ) { + // Get CPT labels + $post_type = get_post_type_object( $slug_parts[1] ); + $labels = $post_type->labels; + + if ( count( $slug_parts ) > 2 ) { + // Now we look for the CPT with slug as defined in $slug_parts[2] + $post = get_page_by_path( $slug_parts[2], OBJECT, $slug_parts[1] ); + $title = sprintf( + // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress" + __( '%1$s: %2$s' ), + $labels->singular_name, + $post->post_title + ); + $description = sprintf( + // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress" + __( 'Template for %1$s' ), + $title + ); + } else { + $title = sprintf( + // translators: %s: Name of the post type e.g: "Post". + __( 'Single item: %s' ), + $labels->singular_name + ); + $description = sprintf( + // translators: %s: Name of the post type e.g: "Post". + __( 'Displays a single item: %s.' ), + $labels->singular_name + ); + } + } + } else { + $default_template_types = get_default_block_template_types(); + + if ( array_key_exists( $slug, $default_template_types ) ) { + $title = $default_template_types[ $slug ]['title']; + $description = $default_template_types[ $slug ]['description']; + } + } + + return array( + 'title' => $title, + 'description' => $description, + ); +} + /** * Retrieves a single unified template object using its id. * @@ -220,48 +271,12 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { } // This might give us a fallback template with a different ID, // so we have to override it to make sure it's correct. - $block_template->id = $id; - $block_template->slug = $slug; - $default_template_types = get_default_block_template_types(); + $block_template->id = $id; + $block_template->slug = $slug; - $slug_parts = explode( '-', $slug, 3 ); - if ( count( $slug_parts ) > 1 ) { - if ( 'single' === $slug_parts [0] ) { - // Get CPT labels - $post_type = get_post_type_object( $slug_parts[1] ); - $labels = $post_type->labels; - - if ( count( $slug_parts ) > 2 ) { - // Now we look for the CPT with slug as defined in $slug_parts[2] - $post = get_page_by_path( $slug_parts[2], OBJECT, $slug_parts[1] ); - $block_template->title = sprintf( - // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress" - __( '%1$s: %2$s' ), - $labels->singular_name, - $post->post_title - ); - $block_template->description = sprintf( - // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress" - __( 'Template for %1$s' ), - $block_template->title - ); - } else { - $block_template->title = sprintf( - // translators: %s: Name of the post type e.g: "Post". - __( 'Single item: %s' ), - $labels->singular_name - ); - $block_template->description = sprintf( - // translators: %s: Name of the post type e.g: "Post". - __( 'Displays a single item: %s.' ), - $labels->singular_name - ); - } - } - } elseif ( array_key_exists( $slug, $default_template_types ) ) { - $block_template->title = $default_template_types[ $slug ]['title']; - $block_template->description = $default_template_types[ $slug ]['description']; - } + $template_type_info = gutenberg_get_block_template_type_for_slug(); + $block_template->title = $template_type_info['title']; + $block_template->description = $template_type_info['description']; /** * Filters the queried block template object after it's been fetched. From e0ff8b53b4321c11af59c1a4f62ad2d07e48cf6f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 19:54:03 +0200 Subject: [PATCH 08/11] Add basic test coverage for gutenberg_get_block_template_type_for_slug --- .../block-template-utils-test.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/phpunit/compat/wordpress-6.1/block-template-utils-test.php b/phpunit/compat/wordpress-6.1/block-template-utils-test.php index 047914188e061..f11fcae0ba4e4 100644 --- a/phpunit/compat/wordpress-6.1/block-template-utils-test.php +++ b/phpunit/compat/wordpress-6.1/block-template-utils-test.php @@ -11,6 +11,23 @@ * @group block-templates */ class Tests_Block_Template_Utils_6_1 extends WP_UnitTestCase { + private static $post; + + public static function wpSetUpBeforeClass() { + $args = array( + 'post_type' => 'post', + 'post_name' => 'hello-world', + 'post_title' => 'Hello World!', + 'post_content' => 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!', + 'post_excerpt' => 'Welcome to WordPress.', + ); + self::$post = self::factory()->post->create_and_get( $args ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post->ID ); + } + public function test_get_template_slugs() { $templates = gutenberg_get_template_slugs( 'single-post-hello-world' ); $this->assertSame( @@ -23,4 +40,16 @@ public function test_get_template_slugs() { $templates ); } + + public function test_gutenberg_get_block_template_type_for_slug() { + $template_slug = 'single-' . self::$post->post_type . '-' . self::$post->post_name; + $template_info = gutenberg_get_block_template_type_for_slug( $template_slug ); + $this->assertSame( + array( + 'title' => 'Post: Hello World!', + 'description' => 'Template for Post: Hello World!', + ), + $template_info + ); + } } From f090473e444df8369aad21566f32e4d79bfa23f7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 19:56:13 +0200 Subject: [PATCH 09/11] Cover generic templates --- .../wordpress-6.1/block-template-utils-test.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/phpunit/compat/wordpress-6.1/block-template-utils-test.php b/phpunit/compat/wordpress-6.1/block-template-utils-test.php index f11fcae0ba4e4..1a77a01f36552 100644 --- a/phpunit/compat/wordpress-6.1/block-template-utils-test.php +++ b/phpunit/compat/wordpress-6.1/block-template-utils-test.php @@ -41,7 +41,19 @@ public function test_get_template_slugs() { ); } - public function test_gutenberg_get_block_template_type_for_slug() { + public function test_gutenberg_get_block_template_type_for_slug_generic() { + $template_slug = 'single-' . self::$post->post_type; + $template_info = gutenberg_get_block_template_type_for_slug( $template_slug ); + $this->assertSame( + array( + 'title' => 'Single item: Post', + 'description' => 'Displays a single item: Post.', + ), + $template_info + ); + } + + public function test_gutenberg_get_block_template_type_for_slug_individual() { $template_slug = 'single-' . self::$post->post_type . '-' . self::$post->post_name; $template_info = gutenberg_get_block_template_type_for_slug( $template_slug ); $this->assertSame( From c062ef5520a6bb02e7f9f8ac25534845d3d57e81 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 21:29:54 +0200 Subject: [PATCH 10/11] Forgot argument --- lib/compat/wordpress-6.1/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 0c97ae0e59a5f..e63e45a4db447 100644 --- a/lib/compat/wordpress-6.1/block-template-utils.php +++ b/lib/compat/wordpress-6.1/block-template-utils.php @@ -274,7 +274,7 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) { $block_template->id = $id; $block_template->slug = $slug; - $template_type_info = gutenberg_get_block_template_type_for_slug(); + $template_type_info = gutenberg_get_block_template_type_for_slug( $slug ); $block_template->title = $template_type_info['title']; $block_template->description = $template_type_info['description']; From 0d92f7f8c3f4e4bf235dbbaeb2593ccede64934a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 22:19:31 +0200 Subject: [PATCH 11/11] Restore setTemplate call --- .../src/components/add-new-template/new-template.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js index 640c6f18a74cd..bb2c8a1a6890e 100644 --- a/packages/edit-site/src/components/add-new-template/new-template.js +++ b/packages/edit-site/src/components/add-new-template/new-template.js @@ -13,7 +13,7 @@ import { NavigableMenu, } from '@wordpress/components'; import { useState } from '@wordpress/element'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { store as editorStore } from '@wordpress/editor'; import { @@ -39,6 +39,7 @@ import { __, sprintf } from '@wordpress/i18n'; import AddCustomTemplateModal from './add-custom-template-modal'; import { usePostTypes, usePostTypesEntitiesInfo } from './utils'; import { useHistory } from '../routes'; +import { store as editSiteStore } from '../../store'; const DEFAULT_TEMPLATE_SLUGS = [ 'front-page', @@ -92,10 +93,17 @@ export default function NewTemplate( { postType } ) { ); const postTypesEntitiesInfo = usePostTypesEntitiesInfo( existingTemplates ); + const { setTemplate } = useDispatch( editSiteStore ); + async function createTemplate( template ) { const { slug } = template; + const templateId = theme.stylesheet + '//' + slug.toString(); + + // Set template before navigating away to avoid initial stale value. + setTemplate( templateId, slug ); + history.push( { - postId: theme.stylesheet + '//' + slug.toString(), + postId: templateId, postType: 'wp_template', } ); }