WordPress.org

Make WordPress Core

Ticket #11863: 11863.7.diff

File 11863.7.diff, 4.4 KB (added by ericlewis, 4 years ago)
  • src/wp-includes/post.php

     
    32413241         */
    32423242        $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
    32433243
     3244        // If the post is being untrashed and its slug was changed previously.
     3245        if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
     3246                $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
     3247                delete_post_meta( $post_ID, '_wp_desired_post_slug' );
     3248                if ( $desired_post_slug ) {
     3249                        $post_name = $desired_post_slug;
     3250                }
     3251        }
     3252
     3253        // If a trashed post has the desired slug, change it and let this post have it.
     3254        if ( $post_status != 'trash' && $post_name ) {
     3255                wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
     3256        }
     3257
     3258        // When trashing a post, change its slug to allow non-trashed posts to use it.
     3259        if ( 'trash' === $post_status ) {
     3260                $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
     3261        }
     3262
    32443263        $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    32453264
    32463265        // Don't unslash.
     
    60336052                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    60346053        }
    60356054}
     6055
     6056/**
     6057 * If any trashed posts have a given slug, add a suffix.
     6058 *
     6059 * Store its desired (i.e. current) slug so it can try to reclaim it
     6060 * if the post is untrashed.
     6061 *
     6062 * For internal use.
     6063 *
     6064 * @since 4.5.0
     6065 *
     6066 * @param string $post_name    Slug.
     6067 * @param string $post__not_in Post ID that should be ignored.
     6068 */
     6069function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
     6070        $trashed_posts_with_desired_slug = get_posts( array(
     6071                'name' => $post_name,
     6072                'post_status' => 'trash',
     6073                'post_type' => 'any',
     6074                'nopaging' => true,
     6075                'post__not_in' => array( $post_ID )
     6076        ) );
     6077
     6078        if ( ! empty( $trashed_posts_with_desired_slug ) ) {
     6079                foreach ( $trashed_posts_with_desired_slug as $_post ) {
     6080                        wp_add_trashed_suffix_to_post_name_for_post( $_post );
     6081                }
     6082        }
     6083}
     6084
     6085/**
     6086 * For a given post, add a trashed suffix.
     6087 *
     6088 * Store its desired (i.e. current) slug so it can try to reclaim it
     6089 * if the post is untrashed.
     6090 *
     6091 * For internal use.
     6092 *
     6093 * @since 4.5.0
     6094 *
     6095 * @param WP_Post $post The post.
     6096 */
     6097function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
     6098        global $wpdb;
     6099
     6100        $post = get_post( $post );
     6101
     6102        if ( strpos( $post->post_name, '-%trashed%' ) ) {
     6103                return $post->post_name;
     6104        }
     6105        add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
     6106        $post_name = _truncate_post_slug( $post->post_name, 189 ) . '-%trashed%';
     6107        $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
     6108        clean_post_cache( $post->ID );
     6109        return $post_name;
     6110}
  • tests/phpunit/tests/post/wpInsertPost.php

     
     1<?php
     2
     3/**
     4 * @group post
     5 */
     6class Tests_WPInsertPost extends WP_UnitTestCase {
     7
     8        /**
     9         * @ticket 11863
     10         */
     11        function test_trashed_post_slugs_should_move_for_non_trashed_posts() {
     12                $trashed_about_page_id = self::factory()->post->create( array(
     13                        'post_type' => 'page',
     14                        'post_title' => 'About',
     15                        'post_status' => 'publish'
     16                ) );
     17                wp_trash_post( $trashed_about_page_id );
     18                $about_page_id = self::factory()->post->create( array(
     19                        'post_type' => 'page',
     20                        'post_title' => 'About',
     21                        'post_status' => 'publish'
     22                ) );
     23                $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
     24                $this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
     25        }
     26
     27        /**
     28         * @ticket 11863
     29         */
     30        function test_trashed_post_slugs_that_were_moved_should_be_reassigned_after_untrashing() {
     31                $about_page_id = self::factory()->post->create( array(
     32                        'post_type' => 'page',
     33                        'post_title' => 'About',
     34                        'post_status' => 'publish'
     35                ) );
     36                wp_trash_post( $about_page_id );
     37
     38                $another_about_page_id = self::factory()->post->create( array(
     39                        'post_type' => 'page',
     40                        'post_title' => 'About',
     41                        'post_status' => 'publish'
     42                ) );
     43                wp_trash_post( $another_about_page_id );
     44
     45                wp_untrash_post( $about_page_id );
     46                $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
     47        }
     48}