| 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 | |
| 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 | */ |
| 6072 | function 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 | */ |
| 6100 | function 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 | } |