From 3308f3286e323ea41f1cde994377dfa1dfc8b3f6 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Sun, 7 Nov 2021 23:24:20 +0800 Subject: [PATCH 1/4] Change edit links for templates and template parts --- lib/full-site-editing/template-parts.php | 23 +++++++++++++++++++++++ lib/full-site-editing/templates.php | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 3b3dd6fa16e4df..9167b33ad39b1b 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -60,6 +60,29 @@ function gutenberg_register_template_part_post_type() { } add_action( 'init', 'gutenberg_register_template_part_post_type' ); +/** + * Get the edit link for template parts. + * + * @param string $link The original link. + * @param int $post_id The custom post id. + */ +function gutenberg_get_edit_template_part_link( $link, $post_id ) { + $template_parts = gutenberg_get_block_templates( + array( 'wp_id' => $post_id ), + 'wp_template_part' + ); + $template_part = $template_parts ? $template_parts[0] : null; + + if ( ! $template_part ) { + return $link; + } + + $id = urlencode( $template_part->id ); + + return "themes.php?page=gutenberg-edit-site&postId=$id&postType=wp_template_part"; +} +add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_part_link', 10, 2 ); + /** * Registers the 'wp_template_part_area' taxonomy. */ diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index 85cb8ce47a77aa..40bb4ecc827cdb 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -61,6 +61,29 @@ function gutenberg_register_template_post_type() { } add_action( 'init', 'gutenberg_register_template_post_type' ); +/** + * Get the edit link for templates. + * + * @param string $link The original link. + * @param int $post_id The custom post id. + */ +function gutenberg_get_edit_template_link( $link, $post_id ) { + $templates = gutenberg_get_block_templates( + array( 'wp_id' => $post_id ), + 'wp_template' + ); + $template = $templates ? $templates[0] : null; + + if ( ! $template ) { + return $link; + } + + $id = urlencode( $template->id ); + + return "themes.php?page=gutenberg-edit-site&postId=$id&postType=wp_template"; +} +add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); + /** * Registers block editor 'wp_theme' taxonomy. */ From 9626c8988262629981be1661cfd2f1b1c7d6c9a9 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Mon, 8 Nov 2021 17:19:31 +0800 Subject: [PATCH 2/4] Unify the filter --- lib/full-site-editing/template-loader.php | 25 +++++++++++++++++++++++ lib/full-site-editing/template-parts.php | 23 --------------------- lib/full-site-editing/templates.php | 23 --------------------- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index efac8b066d32a2..c01ee2fd67b54a 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 ( 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { + return $link; + } + + $template = _build_block_template_result_from_post( $post ); + + if ( ! $template ) { + return $link; + } + + $id = urlencode( $template->id ); + + return "themes.php?page=gutenberg-edit-site&postId=$id&postType=$template->type"; +} +add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); diff --git a/lib/full-site-editing/template-parts.php b/lib/full-site-editing/template-parts.php index 9167b33ad39b1b..3b3dd6fa16e4df 100644 --- a/lib/full-site-editing/template-parts.php +++ b/lib/full-site-editing/template-parts.php @@ -60,29 +60,6 @@ function gutenberg_register_template_part_post_type() { } add_action( 'init', 'gutenberg_register_template_part_post_type' ); -/** - * Get the edit link for template parts. - * - * @param string $link The original link. - * @param int $post_id The custom post id. - */ -function gutenberg_get_edit_template_part_link( $link, $post_id ) { - $template_parts = gutenberg_get_block_templates( - array( 'wp_id' => $post_id ), - 'wp_template_part' - ); - $template_part = $template_parts ? $template_parts[0] : null; - - if ( ! $template_part ) { - return $link; - } - - $id = urlencode( $template_part->id ); - - return "themes.php?page=gutenberg-edit-site&postId=$id&postType=wp_template_part"; -} -add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_part_link', 10, 2 ); - /** * Registers the 'wp_template_part_area' taxonomy. */ diff --git a/lib/full-site-editing/templates.php b/lib/full-site-editing/templates.php index 40bb4ecc827cdb..85cb8ce47a77aa 100644 --- a/lib/full-site-editing/templates.php +++ b/lib/full-site-editing/templates.php @@ -61,29 +61,6 @@ function gutenberg_register_template_post_type() { } add_action( 'init', 'gutenberg_register_template_post_type' ); -/** - * Get the edit link for templates. - * - * @param string $link The original link. - * @param int $post_id The custom post id. - */ -function gutenberg_get_edit_template_link( $link, $post_id ) { - $templates = gutenberg_get_block_templates( - array( 'wp_id' => $post_id ), - 'wp_template' - ); - $template = $templates ? $templates[0] : null; - - if ( ! $template ) { - return $link; - } - - $id = urlencode( $template->id ); - - return "themes.php?page=gutenberg-edit-site&postId=$id&postType=wp_template"; -} -add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); - /** * Registers block editor 'wp_theme' taxonomy. */ From 21656838ab186acef34363369cdaf721a62b36a0 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 8 Nov 2021 13:59:51 +0400 Subject: [PATCH 3/4] Minor adjustments --- lib/full-site-editing/template-loader.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/full-site-editing/template-loader.php b/lib/full-site-editing/template-loader.php index c01ee2fd67b54a..9f29783aa34895 100644 --- a/lib/full-site-editing/template-loader.php +++ b/lib/full-site-editing/template-loader.php @@ -276,18 +276,18 @@ function gutenberg_resolve_template_for_new_post( $wp_query ) { function gutenberg_get_edit_template_link( $link, $post_id ) { $post = get_post( $post_id ); - if ( 'wp_template' !== $post->post_type && 'wp_template_part' !== $post->post_type ) { + if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { return $link; } $template = _build_block_template_result_from_post( $post ); - if ( ! $template ) { + if ( is_wp_error( $template ) ) { return $link; } - $id = urlencode( $template->id ); + $edit_link = 'themes.php?page=gutenberg-edit-site&postId=%1$s&postType=%2$s'; - return "themes.php?page=gutenberg-edit-site&postId=$id&postType=$template->type"; + return admin_url( sprintf( $edit_link, urlencode( $template->id ), $template->type ) ); } add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); From abef19f2a66396f5483866500de8db30e264e9e9 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 8 Nov 2021 16:43:10 +0100 Subject: [PATCH 4/4] Add a unit test. --- .../template-loader-test.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 phpunit/full-site-editing/template-loader-test.php 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 ); + } +}