From a691ce65141708d2fdfc827b043567861f126a01 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 8 Jul 2025 14:46:31 +1000 Subject: [PATCH 1/2] Try adding attachment trashable status to REST API --- .../class-wp-rest-attachments-controller.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 589e0e2ccec17..c8be778e51340 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -941,6 +941,10 @@ public function prepare_item_for_response( $item, $request ) { $data['missing_image_sizes'] = array_keys( wp_get_missing_image_subsizes( $post->ID ) ); } + if ( in_array( 'trashable', $fields, true ) ) { + $data['trashable'] = ( defined( 'MEDIA_TRASH' ) && MEDIA_TRASH ); + } + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->filter_response_by_context( $data, $context ); @@ -1083,6 +1087,13 @@ public function get_item_schema() { 'readonly' => true, ); + $schema['properties']['trashable'] = array( + 'description' => __( 'Whether the attachment can be moved to the trash.' ), + 'type' => 'boolean', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ); + unset( $schema['properties']['password'] ); $this->schema = $schema; From 28096a6c72eda4bff8b4fbbf94dc40f865015967 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 8 Jul 2025 15:27:37 +1000 Subject: [PATCH 2/2] Update schema test --- tests/phpunit/tests/rest-api/rest-attachments-controller.php | 3 ++- tests/qunit/fixtures/wp-api-generated.js | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index d065238b8beb7..7d84072f5cd23 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -1670,7 +1670,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 29, $properties ); + $this->assertCount( 30, $properties ); $this->assertArrayHasKey( 'author', $properties ); $this->assertArrayHasKey( 'alt_text', $properties ); $this->assertArrayHasKey( 'caption', $properties ); @@ -1704,6 +1704,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'rendered', $properties['title']['properties'] ); $this->assertArrayHasKey( 'type', $properties ); $this->assertArrayHasKey( 'missing_image_sizes', $properties ); + $this->assertArrayHasKey( 'trashable', $properties ); $this->assertArrayHasKey( 'featured_media', $properties ); $this->assertArrayHasKey( 'class_list', $properties ); } diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 6626758a8a9dc..eb9763c71b904 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -13079,6 +13079,7 @@ mockedApiResponse.MediaCollection = [ "media_details": {}, "post": null, "source_url": "http://example.org/wp-content/uploads//tmp/canola.jpg", + "trashable": false, "_links": { "self": [ { @@ -13156,7 +13157,8 @@ mockedApiResponse.MediaModel = { "mime_type": "image/jpeg", "media_details": {}, "post": null, - "source_url": "http://example.org/wp-content/uploads//tmp/canola.jpg" + "source_url": "http://example.org/wp-content/uploads//tmp/canola.jpg", + "trashable": false }; mockedApiResponse.TypesCollection = {