| 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 | if ( $desired_post_slug ) { |
| 3248 | delete_post_meta( $post_ID, '_wp_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 | |
| 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 | */ |
| 6069 | function 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 | */ |
| 6097 | function 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 | } |