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 #11863. git-svn-id: https://develop.svn.wordpress.org/trunk@36607 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
9d29858b25
commit
c592a7262e
@ -3241,6 +3241,28 @@ function wp_insert_post( $postarr, $wp_error = false ) {
|
||||
*/
|
||||
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
|
||||
|
||||
/*
|
||||
* 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 );
|
||||
}
|
||||
|
||||
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
|
||||
|
||||
// Don't unslash.
|
||||
@ -6033,3 +6055,59 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
|
||||
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
78
tests/phpunit/tests/post/wpInsertPost.php
Normal file
78
tests/phpunit/tests/post/wpInsertPost.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?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 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user