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
This commit is contained in:
David Baumwald
2023-05-10 17:34:13 +00:00
parent 16ffd36f5d
commit c40e0c6f81
3 changed files with 152 additions and 4 deletions
+7 -4
View File
@@ -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 = '';
}
/**
+11
View File
@@ -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,
@@ -0,0 +1,134 @@
<?php
/**
* Tests the `get_edit_post_link()` function.
*
* @since 6.3.0
*
* @group link
*
* @covers ::get_edit_post_link
*/
class Tests_Link_GetEditPostLink extends WP_UnitTestCase {
/**
* The name of the theme to use for the test.
*
* @since 6.3.0
* @var string
*/
const TEST_THEME = 'block-theme';
/**
* The id of the user to use for the test.
*
* @since 6.3.0
* @var int
*/
private static $admin_id;
/**
* Creates admin user before tests run.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Create an admin user because get_edit_post_link() requires 'edit_post' capability.
self::$admin_id = $factory->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 . '&amp;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.' );
}
}