From c40e0c6f816ed27ba8b75462632498a36f624181 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Wed, 10 May 2023 17:34:13 +0000 Subject: [PATCH] Revisions: Add edit link functionality for the `wp_template` and `wp_template_part` post types. In preparation for viewing revisions of templates and template parts in the editor, this set changes adds the `_edit_link` argument when registering the `wp_template` and `wp_template_part` post types. This commit also updates `get_edit_post_link` to account for the unique edit URLs for these post types. Finally, this commit also adds new unit tests for the `get_edit_post_link` function, including tests for the `post`, `wp_template`, and `wp_template_part` post types. Fixes #57709. Props andraganescu, spacedmonkey, antonvlasenko, youknowriad, ramonopoly, ironprogrammer, annezazu. git-svn-id: https://develop.svn.wordpress.org/trunk@55743 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 11 +- src/wp-includes/post.php | 11 ++ tests/phpunit/tests/link/getEditPostLink.php | 134 +++++++++++++++++++ 3 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/link/getEditPostLink.php diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index dccfc4bc8f..59bc5d2fc1 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1435,7 +1435,7 @@ function get_preview_post_link( $post = null, $query_args = array(), $preview_li * Retrieves the edit post link for post. * * Can be used within the WordPress loop or outside of it. Can be used with - * pages, posts, attachments, and revisions. + * pages, posts, attachments, revisions, global styles, templates, and template parts. * * @since 2.3.0 * @@ -1469,10 +1469,13 @@ function get_edit_post_link( $post = 0, $context = 'display' ) { return; } - if ( $post_type_object->_edit_link ) { + $link = ''; + + if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) { + $slug = urlencode( get_stylesheet() . '//' . $post->post_name ); + $link = admin_url( sprintf( $post_type_object->_edit_link, $post->post_type, $slug ) ); + } elseif ( $post_type_object->_edit_link ) { $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ); - } else { - $link = ''; } /** diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index af4823424b..77a1a49238 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -332,6 +332,14 @@ function create_initial_post_types() { ) ); + $template_edit_link = 'site-editor.php?' . build_query( + array( + 'postType' => '%s', + 'postId' => '%s', + 'canvas' => 'edit', + ) + ); + register_post_type( 'wp_template', array( @@ -358,6 +366,7 @@ function create_initial_post_types() { 'description' => __( 'Templates to include in your theme.' ), 'public' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ + '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */ 'has_archive' => false, 'show_ui' => false, 'show_in_menu' => false, @@ -418,6 +427,7 @@ function create_initial_post_types() { 'description' => __( 'Template parts to include in your templates.' ), 'public' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ + '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */ 'has_archive' => false, 'show_ui' => false, 'show_in_menu' => false, @@ -458,6 +468,7 @@ function create_initial_post_types() { 'description' => __( 'Global styles to include in themes.' ), 'public' => false, '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ + '_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */ 'show_ui' => false, 'show_in_rest' => false, 'rewrite' => false, diff --git a/tests/phpunit/tests/link/getEditPostLink.php b/tests/phpunit/tests/link/getEditPostLink.php new file mode 100644 index 0000000000..4236e7a335 --- /dev/null +++ b/tests/phpunit/tests/link/getEditPostLink.php @@ -0,0 +1,134 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Performs setup tasks for every test. + * + * @since 6.3.0 + */ + public function set_up() { + parent::set_up(); + wp_set_current_user( self::$admin_id ); + switch_theme( self::TEST_THEME ); + } + + /** + * Tests getting the edit post link for a post. + */ + public function test_get_edit_post_link() { + $post = self::factory()->post->create_and_get( + array( + 'post_type' => 'post', + 'post_title' => 'Test Post', + 'post_name' => 'test-post', + 'post_status' => 'publish', + ) + ); + $post_type_object = get_post_type_object( $post->post_type ); + $link_default_context = admin_url( sprintf( $post_type_object->_edit_link . '&action=edit', $post->ID ) ); + $link_custom_context = admin_url( sprintf( $post_type_object->_edit_link . '&action=edit', $post->ID ) ); + + $this->assertSame( $link_default_context, get_edit_post_link( $post ), 'Second argument `$context` has a default context of `"display"`.' ); + $this->assertSame( $link_custom_context, get_edit_post_link( $post, 'something-else' ), 'Pass non-default value in second argument.' ); + } + + /** + * Tests getting the edit post link for a template post type. + * + * @ticket 57709 + */ + public function test_get_edit_post_link_for_wp_template_post_type() { + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => '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( + self::TEST_THEME, + ), + ), + ) + ); + + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $post_type_object = get_post_type_object( $template_post->post_type ); + $link_default_context = admin_url( sprintf( $post_type_object->_edit_link, $template_post->post_type, get_stylesheet() . '%2F%2Fmy_template' ) ); + $link_custom_context = admin_url( sprintf( $post_type_object->_edit_link, $template_post->post_type, get_stylesheet() . '%2F%2Fmy_template' ) ); + + $this->assertSame( $link_default_context, get_edit_post_link( $template_post ), 'Second argument `$context` has a default context of `"display"`.' ); + $this->assertSame( $link_custom_context, get_edit_post_link( $template_post, 'something-else' ), 'Pass non-default value in second argument.' ); + } + + /** + * Tests getting the edit post link for a template part post type. + * + * @ticket 57709 + */ + public function test_get_edit_post_link_for_wp_template_part_post_type() { + $template_part_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template_part', + 'post_name' => 'my_template_part', + 'post_title' => 'My Template Part', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template part', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + 'wp_template_part_area' => array( + WP_TEMPLATE_PART_AREA_HEADER, + ), + ), + ) + ); + + wp_set_post_terms( $template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); + wp_set_post_terms( $template_part_post->ID, self::TEST_THEME, 'wp_theme' ); + + $post_type_object = get_post_type_object( $template_part_post->post_type ); + $link_default_context = admin_url( sprintf( $post_type_object->_edit_link, $template_part_post->post_type, get_stylesheet() . '%2F%2Fmy_template_part' ) ); + $link_custom_context = admin_url( sprintf( $post_type_object->_edit_link, $template_part_post->post_type, get_stylesheet() . '%2F%2Fmy_template_part' ) ); + + $this->assertSame( $link_default_context, get_edit_post_link( $template_part_post ), 'Second argument `$context` has a default context of `"display"`.' ); + $this->assertSame( $link_custom_context, get_edit_post_link( $template_part_post, 'something-else' ), 'Pass non-default value in second argument.' ); + } +}