<!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>[36607] trunk: Posts: Non-trashed posts should take slug priority over trashed posts.</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 { 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/36607">36607</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/36607","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ericlewis</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-21 21:44:14 +0000 (Sun, 21 Feb 2016)</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'>Posts: Non-trashed posts should take slug priority over trashed posts.

When determining a unique post slug, trashed posts are taken into account. Previously, new posts would add suffixes to their slugs (e.g. `about-2`) when a post in the trash had the desired slug (e.g. `about`). 

To avoid this behavior, when a post is trashed its slug (i.e. `post_name`) is now suffixed with `-%trashed%`. The post's pre-trash slug is stored as post meta, and if the post is restored from trash, its desired slug is reapplied. 

For existing trashed posts which don't have the `-%trashed%` suffix, the suffix is added when a post with its desired slug is created.

Props ocean90, boonebgorges, ryan, SergeyBiryukov, coffee2code, helen, williamsba1.
See <a href="https://core.trac.wordpress.org/ticket/11863">#11863</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestspostwpInsertPostphp">trunk/tests/phpunit/tests/post/wpInsertPost.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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    2016-02-21 06:14:59 UTC (rev 36606)
+++ trunk/src/wp-includes/post.php      2016-02-21 21:44:14 UTC (rev 36607)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3241,6 +3241,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /*
+        * If the post is being untrashed and it has a desired slug stored in post meta,
+        * reassign it.
+        */
+       if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
+               $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
+               if ( $desired_post_slug ) {
+                       delete_post_meta( $post_ID, '_wp_desired_post_slug' );
+                       $post_name = $desired_post_slug;
+               }
+       }
+
+       // If a trashed post has the desired slug, change it and let this post have it.
+       if ( 'trash' !== $post_status && $post_name ) {
+               wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
+       }
+
+       // When trashing an existing post, change its slug to allow non-trashed posts to use it.
+       if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
+               $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Don't unslash.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6033,3 +6055,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
</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">+
+/**
+ * If any trashed posts have a given slug, add a suffix.
+ *
+ * Store its desired (i.e. current) slug so it can try to reclaim it
+ * if the post is untrashed.
+ *
+ * For internal use.
+ *
+ * @since 4.5.0
+ *
+ * @param string $post_name    Slug.
+ * @param string $post__not_in Post ID that should be ignored.
+ */
+function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
+       $trashed_posts_with_desired_slug = get_posts( array(
+               'name' => $post_name,
+               'post_status' => 'trash',
+               'post_type' => 'any',
+               'nopaging' => true,
+               'post__not_in' => array( $post_ID )
+       ) );
+
+       if ( ! empty( $trashed_posts_with_desired_slug ) ) {
+               foreach ( $trashed_posts_with_desired_slug as $_post ) {
+                       wp_add_trashed_suffix_to_post_name_for_post( $_post );
+               }
+       }
+}
+
+/**
+ * For a given post, add a trashed suffix.
+ *
+ * Store its desired (i.e. current) slug so it can try to reclaim it
+ * if the post is untrashed.
+ *
+ * For internal use.
+ *
+ * @since 4.5.0
+ *
+ * @param WP_Post $post The post.
+ */
+function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
+       global $wpdb;
+
+       $post = get_post( $post );
+
+       if ( strpos( $post->post_name, '-%trashed%' ) ) {
+               return $post->post_name;
+       }
+       add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
+       $post_name = _truncate_post_slug( $post->post_name, 190 ) . '-%trashed%';
+       $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
+       clean_post_cache( $post->ID );
+       return $post_name;
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestspostwpInsertPostphp"></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/post/wpInsertPost.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/wpInsertPost.php                           (rev 0)
+++ trunk/tests/phpunit/tests/post/wpInsertPost.php     2016-02-21 21:44:14 UTC (rev 36607)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,78 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group post
+ */
+class Tests_WPInsertPost extends WP_UnitTestCase {
+
+       /**
+        * @ticket 11863
+        */
+       function test_trashing_a_post_should_add_trashed_suffix_to_post_name() {
+               $trashed_about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'publish'
+               ) );
+               wp_trash_post( $trashed_about_page_id );
+               $this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
+       }
+
+       /**
+        * @ticket 11863
+        */
+       function test_trashed_posts_original_post_name_should_be_reassigned_after_untrashing() {
+               $about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'publish'
+               ) );
+               wp_trash_post( $about_page_id );
+
+               wp_untrash_post( $about_page_id );
+               $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
+       }
+
+       /**
+        * @ticket 11863
+        */
+       function test_creating_a_new_post_should_add_trashed_suffix_to_post_name_of_trashed_posts_with_the_desired_slug() {
+               $trashed_about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'trash'
+               ) );
+
+               $about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'publish'
+               ) );
+
+               $this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
+               $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
+       }
+
+       /**
+       * @ticket 11863
+       */
+       function test_untrashing_a_post_with_a_stored_desired_post_name_should_get_its_post_name_suffixed_if_another_post_has_taken_the_desired_post_name() {
+               $about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'publish'
+               ) );
+               wp_trash_post( $about_page_id );
+
+               $another_about_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_title' => 'About',
+                       'post_status' => 'publish'
+               ) );
+
+               wp_untrash_post( $about_page_id );
+
+               $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name );
+               $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>