<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55743] trunk: Revisions: Add edit link functionality for the `wp_template` and `wp_template_part` post types.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55743">55743</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55743","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>davidbaumwald</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-05-10 17:34:13 +0000 (Wed, 10 May 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/57709">#57709</a>.
Props andraganescu, spacedmonkey, antonvlasenko, youknowriad, ramonopoly, ironprogrammer, annezazu.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeslinktemplatephp">trunk/src/wp-includes/link-template.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestslinkgetEditPostLinkphp">trunk/tests/phpunit/tests/link/getEditPostLink.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeslinktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/link-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/link-template.php 2023-05-10 14:00:01 UTC (rev 55742)
+++ trunk/src/wp-includes/link-template.php 2023-05-10 17:34:13 UTC (rev 55743)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1435,7 +1435,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the edit post link for post.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Can be used within the WordPress loop or outside of it. Can be used with
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * pages, posts, attachments, and revisions.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * pages, posts, attachments, revisions, global styles, templates, and template parts.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1469,10 +1469,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $post_type_object->_edit_link ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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 ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
- $link = '';
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2023-05-10 14:00:01 UTC (rev 55742)
+++ trunk/src/wp-includes/post.php 2023-05-10 17:34:13 UTC (rev 55743)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -332,6 +332,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $template_edit_link = 'site-editor.php?' . build_query(
+ array(
+ 'postType' => '%s',
+ 'postId' => '%s',
+ 'canvas' => 'edit',
+ )
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> register_post_type(
</span><span class="cx" style="display: block; padding: 0 10px"> 'wp_template',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -358,6 +366,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Templates to include in your theme.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'public' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
</ins><span class="cx" style="display: block; padding: 0 10px"> 'has_archive' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_ui' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_in_menu' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -418,6 +427,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Template parts to include in your templates.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'public' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '_edit_link' => $template_edit_link, /* internal use only. don't use this when registering your own post type. */
</ins><span class="cx" style="display: block; padding: 0 10px"> 'has_archive' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_ui' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_in_menu' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -458,6 +468,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Global styles to include in themes.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'public' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
</ins><span class="cx" style="display: block; padding: 0 10px"> 'show_ui' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'show_in_rest' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'rewrite' => false,
</span></span></pre></div>
<a id="trunktestsphpunittestslinkgetEditPostLinkphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/link/getEditPostLink.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/link/getEditPostLink.php (rev 0)
+++ trunk/tests/phpunit/tests/link/getEditPostLink.php 2023-05-10 17:34:13 UTC (rev 55743)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,134 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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 . '&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.' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/link/getEditPostLink.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>