Make WordPress Core

Changeset 36607


Ignore:
Timestamp:
02/21/2016 09:44:14 PM (9 years ago)
Author:
ericlewis
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r36569 r36607  
    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
     
    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}
Note: See TracChangeset for help on using the changeset viewer.