Make WordPress Core

Ticket #11863: 11863.12.diff

File 11863.12.diff, 5.5 KB (added by ericlewis, 9 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        /*
     3245         * If the post is being untrashed and it has a desired slug stored in post meta,
     3246         * reassign it.
     3247         */
     3248        if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
     3249                $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
     3250                if ( $desired_post_slug ) {
     3251                        delete_post_meta( $post_ID, '_wp_desired_post_slug' );
     3252                        $post_name = $desired_post_slug;
     3253                }
     3254        }
     3255
     3256        // If a trashed post has the desired slug, change it and let this post have it.
     3257        if ( 'trash' !== $post_status && $post_name ) {
     3258                wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
     3259        }
     3260
     3261        // When trashing an existing post, change its slug to allow non-trashed posts to use it.
     3262        if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
     3263                $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
     3264        }
     3265
    32443266        $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    32453267
    32463268        // Don't unslash.
     
    60336055                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    60346056        }
    60356057}
     6058
     6059/**
     6060 * If any trashed posts have a given slug, add a suffix.
     6061 *
     6062 * Store its desired (i.e. current) slug so it can try to reclaim it
     6063 * if the post is untrashed.
     6064 *
     6065 * For internal use.
     6066 *
     6067 * @since 4.5.0
     6068 *
     6069 * @param string $post_name    Slug.
     6070 * @param string $post__not_in Post ID that should be ignored.
     6071 */
     6072function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
     6073        $trashed_posts_with_desired_slug = get_posts( array(
     6074                'name' => $post_name,
     6075                'post_status' => 'trash',
     6076                'post_type' => 'any',
     6077                'nopaging' => true,
     6078                'post__not_in' => array( $post_ID )
     6079        ) );
     6080
     6081        if ( ! empty( $trashed_posts_with_desired_slug ) ) {
     6082                foreach ( $trashed_posts_with_desired_slug as $_post ) {
     6083                        wp_add_trashed_suffix_to_post_name_for_post( $_post );
     6084                }
     6085        }
     6086}
     6087
     6088/**
     6089 * For a given post, add a trashed suffix.
     6090 *
     6091 * Store its desired (i.e. current) slug so it can try to reclaim it
     6092 * if the post is untrashed.
     6093 *
     6094 * For internal use.
     6095 *
     6096 * @since 4.5.0
     6097 *
     6098 * @param WP_Post $post The post.
     6099 */
     6100function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
     6101        global $wpdb;
     6102
     6103        $post = get_post( $post );
     6104
     6105        if ( strpos( $post->post_name, '-%trashed%' ) ) {
     6106                return $post->post_name;
     6107        }
     6108        add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
     6109        $post_name = _truncate_post_slug( $post->post_name, 190 ) . '-%trashed%';
     6110        $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
     6111        clean_post_cache( $post->ID );
     6112        return $post_name;
     6113}
  • 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_trashing_a_post_should_add_trashed_suffix_to_post_name() {
     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                $this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
     19        }
     20
     21        /**
     22         * @ticket 11863
     23         */
     24        function test_trashed_posts_original_post_name_should_be_reassigned_after_untrashing() {
     25                $about_page_id = self::factory()->post->create( array(
     26                        'post_type' => 'page',
     27                        'post_title' => 'About',
     28                        'post_status' => 'publish'
     29                ) );
     30                wp_trash_post( $about_page_id );
     31
     32                wp_untrash_post( $about_page_id );
     33                $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
     34        }
     35
     36        /**
     37         * @ticket 11863
     38         */
     39        function test_creating_a_new_post_should_add_trashed_suffix_to_post_name_of_trashed_posts_with_the_desired_slug() {
     40                $trashed_about_page_id = self::factory()->post->create( array(
     41                        'post_type' => 'page',
     42                        'post_title' => 'About',
     43                        'post_status' => 'trash'
     44                ) );
     45
     46                $about_page_id = self::factory()->post->create( array(
     47                        'post_type' => 'page',
     48                        'post_title' => 'About',
     49                        'post_status' => 'publish'
     50                ) );
     51
     52                $this->assertEquals( 'about-%trashed%', get_post( $trashed_about_page_id )->post_name );
     53                $this->assertEquals( 'about', get_post( $about_page_id )->post_name );
     54        }
     55
     56        /**
     57        * @ticket 11863
     58        */
     59        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() {
     60                $about_page_id = self::factory()->post->create( array(
     61                        'post_type' => 'page',
     62                        'post_title' => 'About',
     63                        'post_status' => 'publish'
     64                ) );
     65                wp_trash_post( $about_page_id );
     66
     67                $another_about_page_id = self::factory()->post->create( array(
     68                        'post_type' => 'page',
     69                        'post_title' => 'About',
     70                        'post_status' => 'publish'
     71                ) );
     72
     73                wp_untrash_post( $about_page_id );
     74
     75                $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name );
     76                $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name );
     77        }
     78}