Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/images/webp-uploads/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) {
$data = $response->get_data();
if ( ! isset( $data['media_details']['sizes'] ) || ! is_array( $data['media_details']['sizes'] ) ) {
if ( ! isset( $data['media_details'] ) || ! is_array( $data['media_details'] ) || ! isset( $data['media_details']['sizes'] ) || ! is_array( $data['media_details']['sizes'] ) ) {
return $response;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/modules/images/webp-uploads/rest-api-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,39 @@ function( $transforms ) {
$this->assertArrayNotHasKey( 'sources', $data['media_details'] );
}

/**
* Checks whether the media details information is added to the REST response object.
*
* @test
*/
public function it_should_check_media_details_in_rest_response() {
$file_location = TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg';
$attachment_id = $this->factory->attachment->create_upload_object( $file_location );

$request = new WP_REST_Request();
$request['id'] = $attachment_id;

$controller = new WP_REST_Attachments_Controller( 'attachment' );
$response = $controller->get_item( $request );

$this->assertNotWPError( $response );

$data = $response->get_data();

$this->assertArrayHasKey( 'media_details', $data );
$this->assertIsArray( $data['media_details'] );

// Delete attachment metadata to set media_details as object in response.
delete_post_meta( $attachment_id, '_wp_attachment_metadata' );

$response = $controller->get_item( $request );

$this->assertNotWPError( $response );

$data = $response->get_data();

$this->assertArrayHasKey( 'media_details', $data );
$this->assertIsNotArray( $data['media_details'] );
$this->assertIsObject( $data['media_details'] );
}
}