diff --git src/wp-includes/post.php src/wp-includes/post.php
index 306e567bb9..0867e04c60 100644
|
|
|
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = fa
|
| 5470 | 5470 | * for media is disabled, item is already in the trash, or $force_delete is true. |
| 5471 | 5471 | * |
| 5472 | 5472 | * @since 2.0.0 |
| | 5473 | * @since 5.3.0 Return `false` if `wp_delete_attachment_files()` failed. |
| 5473 | 5474 | * |
| 5474 | 5475 | * @global wpdb $wpdb WordPress database abstraction object. |
| 5475 | 5476 | * |
| … |
… |
function wp_delete_attachment( $post_id, $force_delete = false ) {
|
| 5545 | 5546 | } |
| 5546 | 5547 | /** This action is documented in wp-includes/post.php */ |
| 5547 | 5548 | do_action( 'deleted_post', $post_id ); |
| 5548 | | |
| 5549 | | wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
| | 5549 | |
| | 5550 | $delete_file = wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
| | 5551 | if ( ! $delete_file ) { |
| | 5552 | return false; |
| | 5553 | } |
| 5550 | 5554 | |
| 5551 | 5555 | clean_post_cache( $post ); |
| 5552 | 5556 | |
diff --git tests/phpunit/tests/post/attachments.php tests/phpunit/tests/post/attachments.php
index 70255d5bec..8b422e3456 100644
|
|
|
class Tests_Post_Attachments extends WP_UnitTestCase {
|
| 529 | 529 | |
| 530 | 530 | $this->assertContains( 'images/media/video.png', $icon ); |
| 531 | 531 | } |
| | 532 | |
| | 533 | /** |
| | 534 | * @ticket 47868 |
| | 535 | */ |
| | 536 | function test_wp_delete_attachment_return_false_if_file_not_deleted() { |
| | 537 | $filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' ); |
| | 538 | $contents = file_get_contents( $filename ); |
| | 539 | |
| | 540 | $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); |
| | 541 | $this->assertTrue( empty( $upload['error'] ) ); |
| | 542 | |
| | 543 | $attachment_id = $this->_make_attachment( $upload ); |
| | 544 | |
| | 545 | // Corrupt the data by removing `_wp_attached_file` and `_wp_attachment_metadata` metas |
| | 546 | // of the newly created attachment post. |
| | 547 | delete_post_meta( $attachment_id, '_wp_attached_file' ); |
| | 548 | delete_post_meta( $attachment_id, '_wp_attachment_metadata' ); |
| | 549 | |
| | 550 | $delete_attachment = wp_delete_attachment( $attachment_id ); |
| | 551 | |
| | 552 | // Since the metas are corrupted, the file wouldn't be deleted. |
| | 553 | $this->assertTrue( file_exists( $filename ) ); |
| | 554 | |
| | 555 | // And since no file was deleted, `wp_delete_attachment()` should return false. |
| | 556 | $this->assertFalse( $delete_attachment ); |
| | 557 | } |
| | 558 | |
| 532 | 559 | } |
diff --git tests/phpunit/tests/rest-api/rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php
index 556cc6c8be..80a10cfe6a 100644
|
|
|
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
| 1222 | 1222 | |
| 1223 | 1223 | public function test_delete_item() { |
| 1224 | 1224 | wp_set_current_user( self::$editor_id ); |
| 1225 | | $attachment_id = $this->factory->attachment->create_object( |
| 1226 | | $this->test_file, |
| 1227 | | 0, |
| 1228 | | array( |
| 1229 | | 'post_mime_type' => 'image/jpeg', |
| 1230 | | 'post_excerpt' => 'A sample caption', |
| 1231 | | ) |
| 1232 | | ); |
| | 1225 | |
| | 1226 | $contents = file_get_contents( $this->test_file ); |
| | 1227 | $upload = wp_upload_bits( wp_basename( $this->test_file ), null, $contents ); |
| | 1228 | |
| | 1229 | $this->assertTrue( empty( $upload['error'] ) ); |
| | 1230 | |
| | 1231 | $attachment_id = $this->_make_attachment( $upload ); |
| | 1232 | |
| 1233 | 1233 | $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); |
| 1234 | 1234 | $request['force'] = true; |
| 1235 | 1235 | $response = rest_get_server()->dispatch( $request ); |