Ticket #23022: 23022.diff
File 23022.diff, 8.5 KB (added by , 4 years ago) |
---|
-
src/wp-admin/edit.php
134 134 break; 135 135 case 'untrash': 136 136 $untrashed = 0; 137 138 if ( isset( $_GET['doaction'] ) && ( 'undo' === $_GET['doaction'] ) ) { 139 add_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 ); 140 } 141 137 142 foreach ( (array) $post_ids as $post_id ) { 138 143 if ( ! current_user_can( 'delete_post', $post_id ) ) { 139 144 wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) ); … … 146 151 $untrashed++; 147 152 } 148 153 $sendback = add_query_arg( 'untrashed', $untrashed, $sendback ); 154 155 remove_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 ); 156 149 157 break; 150 158 case 'delete': 151 159 $deleted = 0; … … 419 427 $ids = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] ); 420 428 $messages[] = '<a href="' . esc_url( wp_nonce_url( "edit.php?post_type=$post_type&doaction=undo&action=untrash&ids=$ids", 'bulk-posts' ) ) . '">' . __( 'Undo' ) . '</a>'; 421 429 } 430 431 if ( 'untrashed' === $message && isset( $_REQUEST['ids'] ) ) { 432 $ids = explode( ',', $_REQUEST['ids'] ); 433 434 if ( 1 === count( $ids ) && current_user_can( 'edit_post', $ids[0] ) ) { 435 $messages[] = sprintf( 436 '<a href="%1$s">%2$s</a>', 437 esc_url( get_edit_post_link( $ids[0] ) ), 438 esc_html( get_post_type_object( get_post_type( $ids[0] ) )->labels->edit_item ) 439 ); 440 } 441 } 422 442 } 423 443 424 444 if ( $messages ) { -
src/wp-admin/post.php
291 291 wp_die( __( 'Error in restoring the item from Trash.' ) ); 292 292 } 293 293 294 wp_redirect( add_query_arg( 'untrashed', 1, $sendback ) ); 294 $sendback = add_query_arg( 295 array( 296 'untrashed' => 1, 297 'ids' => $post_id, 298 ), 299 $sendback 300 ); 301 wp_redirect( $sendback ); 295 302 exit; 296 303 297 304 case 'delete': -
src/wp-includes/functions.php
1193 1193 'error', 1194 1194 'hotkeys_highlight_first', 1195 1195 'hotkeys_highlight_last', 1196 'ids', 1196 1197 'locked', 1197 1198 'message', 1198 1199 'same', -
src/wp-includes/post.php
3232 3232 * Restore a post or page from the Trash. 3233 3233 * 3234 3234 * @since 2.9.0 3235 * @since 5.6.0 An untrashed post is now returned to `draft` status by default. 3235 3236 * 3236 3237 * @param int $post_id Optional. Post ID. Default is ID of the global $post. 3237 3238 * @return WP_Post|false|null Post data on success, false or null on failure. … … 3243 3244 return $post; 3244 3245 } 3245 3246 3247 $post_id = $post->ID; 3248 3246 3249 if ( 'trash' !== $post->post_status ) { 3247 3250 return false; 3248 3251 } 3249 3252 3253 $previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); 3254 3250 3255 /** 3251 3256 * Filters whether a post untrashing should take place. 3252 3257 * 3253 3258 * @since 4.9.0 3259 * @since 5.6.0 The `$previous_status` parameter was added. 3254 3260 * 3255 * @param bool|null $untrash Whether to go forward with untrashing. 3256 * @param WP_Post $post Post object. 3261 * @param bool|null $untrash Whether to go forward with untrashing. 3262 * @param WP_Post $post Post object. 3263 * @param string $previous_status The status of the post at the point where it was trashed. 3257 3264 */ 3258 3265 $check = apply_filters( 'pre_untrash_post', null, $post ); 3259 3266 if ( null !== $check ) { … … 3264 3271 * Fires before a post is restored from the Trash. 3265 3272 * 3266 3273 * @since 2.9.0 3274 * @since 5.6.0 The `$previous_status` parameter was added. 3267 3275 * 3268 * @param int $post_id Post ID. 3276 * @param int $post_id Post ID. 3277 * @param string $previous_status The status of the post at the point where it was trashed. 3269 3278 */ 3270 do_action( 'untrash_post', $post_id );3279 do_action( 'untrash_post', $post_id, $previous_status ); 3271 3280 3272 $post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); 3281 /** 3282 * Filter the status that a post gets when it is restored from the trash (untrashed). 3283 * 3284 * By default, posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status` 3285 * in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()` 3286 * function is available for this. 3287 * 3288 * Prior to WordPress 5.6, restored posts were given their original status. 3289 * 3290 * @since 5.6.0 3291 * 3292 * @param string $new_status The new status of the post being restored. 3293 * @param int $post_id The ID of the post being restored. 3294 * @param string $previous_status The status of the post at the point where it was trashed. 3295 */ 3296 $post_status = apply_filters( 'wp_untrash_post_status', 'draft', $post_id, $previous_status ); 3273 3297 3274 3298 delete_post_meta( $post_id, '_wp_trash_meta_status' ); 3275 3299 delete_post_meta( $post_id, '_wp_trash_meta_time' ); … … 3291 3315 * Fires after a post is restored from the Trash. 3292 3316 * 3293 3317 * @since 2.9.0 3318 * @since 5.6.0 The `$previous_status` parameter was added. 3294 3319 * 3295 * @param int $post_id Post ID. 3320 * @param int $post_id Post ID. 3321 * @param string $previous_status The status of the post at the point where it was trashed. 3296 3322 */ 3297 do_action( 'untrashed_post', $post_id );3323 do_action( 'untrashed_post', $post_id, $previous_status ); 3298 3324 3299 3325 return $post; 3300 3326 } … … 7495 7521 */ 7496 7522 return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); 7497 7523 } 7524 7525 /** 7526 * Filter callback which sets the status of an untrashed post to its previous status. 7527 * 7528 * This can be used as a callback on the `wp_untrash_post_status` filter. 7529 * 7530 * @since 5.6.0 7531 * 7532 * @param string $new_status The new status of the post being restored. 7533 * @param int $post_id The ID of the post being restored. 7534 * @param string $previous_status The status of the post at the point where it was trashed. 7535 * @return string The status of the post at the point where it was trashed. 7536 */ 7537 function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { 7538 return $previous_status; 7539 } -
tests/phpunit/tests/post/wpInsertPost.php
159 159 ); 160 160 161 161 wp_untrash_post( $about_page_id ); 162 wp_update_post( 163 array( 164 'ID' => $about_page_id, 165 'post_status' => 'publish', 166 ) 167 ); 162 168 163 169 $this->assertSame( 'about', get_post( $another_about_page_id )->post_name ); 164 170 $this->assertSame( 'about-2', get_post( $about_page_id )->post_name ); 165 171 } 166 172 167 173 /** 174 * @ticket 23022 175 * @dataProvider data_various_post_statuses 176 */ 177 function test_untrashing_a_post_should_always_restore_it_to_draft_status( $post_status ) { 178 $page_id = self::factory()->post->create( 179 array( 180 'post_type' => 'page', 181 'post_status' => $post_status, 182 ) 183 ); 184 185 wp_trash_post( $page_id ); 186 wp_untrash_post( $page_id ); 187 188 $this->assertSame( 'draft', get_post( $page_id )->post_status ); 189 } 190 191 /** 192 * @ticket 23022 193 * @dataProvider data_various_post_statuses 194 */ 195 function test_wp_untrash_post_status_filter_restores_post_to_correct_status( $post_status ) { 196 add_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 ); 197 198 $page_id = self::factory()->post->create( 199 array( 200 'post_type' => 'page', 201 'post_status' => $post_status, 202 ) 203 ); 204 205 wp_trash_post( $page_id ); 206 wp_untrash_post( $page_id ); 207 208 remove_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 ); 209 210 $this->assertSame( $post_status, get_post( $page_id )->post_status ); 211 } 212 213 /** 168 214 * Data for testing the ability for users to set the post slug. 169 215 * 170 216 * @return array Array of test arguments. … … 184 230 } 185 231 186 232 /** 233 * Data for testing post statuses. 234 * 235 * @return array Array of test arguments. 236 */ 237 function data_various_post_statuses() { 238 return array( 239 array( 240 'draft', 241 ), 242 array( 243 'pending', 244 ), 245 array( 246 'private', 247 ), 248 array( 249 'publish', 250 ), 251 ); 252 } 253 254 /** 187 255 * Test contributor making changes to the pending post slug. 188 256 * 189 257 * @ticket 42464