From f882625cbedf77c2170623b7fbf3c13ab633ee79 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Jun 2022 14:52:40 +0200 Subject: [PATCH 1/3] FSE: Pre-populate template with fallback upon creation --- .../wordpress-6.1/block-template-utils.php | 16 ++++++++++++++++ ...class-gutenberg-rest-templates-controller.php | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/lib/compat/wordpress-6.1/block-template-utils.php b/lib/compat/wordpress-6.1/block-template-utils.php index 2521be25c3bcdf..7554bfe4cfbcdd 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. * 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 7c73d799aa8436..63012d2de61ede 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 @@ -198,6 +198,13 @@ protected function prepare_item_for_database( $request ) { $changes->post_content = $request['content']; } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_content = $template->content; + } elseif( null === $template && empty( $template->content ) ) { + $templates = gutenberg_get_template_slugs( $template->slug ); + $fallback_template = resolve_block_template( $template->slug, $templates, '' ); + if ( ! $fallback_template ) { + $fallback_template = resolve_block_template( 'index', array(), '' ); + } + $changes->post_content = $fallback_template->content; } if ( isset( $request['title'] ) ) { $changes->post_title = $request['title']; From 558802f47af82e76c4386060cdc9cbc26aa9ddb0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Jun 2022 15:31:44 +0200 Subject: [PATCH 2/3] Make work --- .../class-gutenberg-rest-templates-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 63012d2de61ede..3ab09633dcce85 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 @@ -194,13 +194,13 @@ protected function prepare_item_for_database( $request ) { $changes->ID = $template->wp_id; $changes->post_status = 'publish'; } - if ( isset( $request['content'] ) ) { + if ( isset( $request['content'] ) && ! empty( $request['content'] ) ) { $changes->post_content = $request['content']; } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_content = $template->content; - } elseif( null === $template && empty( $template->content ) ) { - $templates = gutenberg_get_template_slugs( $template->slug ); - $fallback_template = resolve_block_template( $template->slug, $templates, '' ); + } elseif ( null === $template && empty( $request['content'] ) ) { + $fallback_templates = gutenberg_get_template_slugs( $request['slug'] ); + $fallback_template = resolve_block_template( $request['slug'], $fallback_templates, '' ); if ( ! $fallback_template ) { $fallback_template = resolve_block_template( 'index', array(), '' ); } From ff16e8552433c12c43a95b52dfbc18de6a4e0df5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 27 Jun 2022 17:37:52 +0200 Subject: [PATCH 3/3] 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 00000000000000..047914188e061a --- /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 + ); + } +}