diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index efac8b066d32a2..9f29783aa34895 100644 --- a/lib/full-site-editing/template-loader.php +++ b/lib/full-site-editing/template-loader.php @@ -266,3 +266,28 @@ function gutenberg_resolve_template_for_new_post( $wp_query ) { $wp_query->set( 'post_status', 'auto-draft' ); } } + +/** + * Redirect the edit links for templates to the site editor. + * + * @param string $link The original link. + * @param int $post_id The custom post id. + */ +function gutenberg_get_edit_template_link( $link, $post_id ) { + $post = get_post( $post_id ); + + if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { + return $link; + } + + $template = _build_block_template_result_from_post( $post ); + + if ( is_wp_error( $template ) ) { + return $link; + } + + $edit_link = 'themes.php?page=gutenberg-edit-site&postId=%1$s&postType=%2$s'; + + return admin_url( sprintf( $edit_link, urlencode( $template->id ), $template->type ) ); +} +add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); diff --git a/phpunit/full-site-editing/template-loader-test.php b/phpunit/full-site-editing/template-loader-test.php new file mode 100644 index 00000000000000..e0935f2f829f8c --- /dev/null +++ b/phpunit/full-site-editing/template-loader-test.php @@ -0,0 +1,44 @@ + 'wp_template', + 'post_name' => 'my_template', + 'post_title' => 'My Template', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template.', + 'tax_input' => array( + 'wp_theme' => array( + $stylesheet, + ), + ), + ); + $post = self::factory()->post->create_and_get( $args ); + + wp_set_post_terms( $post->ID, get_stylesheet(), 'wp_theme' ); + $default_url = 'https://some.link'; + $url = gutenberg_get_edit_template_link( $default_url, $post->ID ); + $this->assertIsString( $url ); + $this->assertNotSame( $default_url, $url ); + $this->assertStringContainsString( 'themes.php', $url ); + $this->assertStringContainsString( 'postId', $url ); + $this->assertStringContainsString( $stylesheet, $url ); + wp_delete_post( $post->ID ); + } + + public function test_gutenberg_get_edit_template_link_returns_default_link_for_non_template_posts() { + $post = self::factory()->post->create_and_get(); + $default_url = 'https://some.link'; + $url = gutenberg_get_edit_template_link( $default_url, $post->ID ); + $this->assertSame( $default_url, $url ); + wp_delete_post( $post->ID ); + } +}