WordPress.org

Make WordPress Core

Ticket #11863: 11863.9.diff

File 11863.9.diff, 3.0 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        /*
     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 ( $post_status !== 'trash' && $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}