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 423347a721..fdb08e78bb 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 @@ -783,6 +783,15 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { } $size_data['source_url'] = $image_src[0]; + + if ( empty( $size_data['sources'] ) || ! is_array( $size_data['sources'] ) ) { + continue; + } + + $image_url_basename = wp_basename( $image_src[0] ); + foreach ( $size_data['sources'] as $mime => &$mime_details ) { + $mime_details['source_url'] = str_replace( $image_url_basename, $mime_details['file'], $image_src[0] ); + } } $full_src = wp_get_attachment_image_src( $post->ID, 'full' ); @@ -795,6 +804,15 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 'mime_type' => $post->post_mime_type, 'source_url' => $full_src[0], ); + + if ( ! empty( $data['media_details']['sources'] ) ) { + $full_url_basename = wp_basename( $full_src[0] ); + foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) { + $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] ); + } + $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources']; + unset( $data['media_details']['sources'] ); + } } } else { $data['media_details']['sizes'] = new stdClass; diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index ec52dbcd0d..c8a6e180a3 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -2262,4 +2262,46 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control } ); } + + /** + * @ticket 55443 + */ + public function test_image_sources_to_rest_response() { + + $attachment_id = self::factory()->attachment->create_upload_object( $this->test_file ); + $metadata = wp_get_attachment_metadata( $attachment_id ); + $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(); + $mime_types = array( + 'image/jpeg', + ); + + if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { + array_push( $mime_types, 'image/webp' ); + } + + foreach ( $data['media_details']['sizes'] as $size_name => $properties ) { + if ( ! isset( $metadata['sizes'][ $size_name ]['sources'] ) ) { + continue; + } + + $this->assertArrayHasKey( 'sources', $properties ); + $this->assertIsArray( $properties['sources'] ); + + foreach ( $mime_types as $mime_type ) { + $this->assertArrayHasKey( $mime_type, $properties['sources'] ); + $this->assertArrayHasKey( 'filesize', $properties['sources'][ $mime_type ] ); + $this->assertArrayHasKey( 'file', $properties['sources'][ $mime_type ] ); + $this->assertArrayHasKey( 'source_url', $properties['sources'][ $mime_type ] ); + $this->assertNotFalse( filter_var( $properties['sources'][ $mime_type ]['source_url'], FILTER_VALIDATE_URL ) ); + } + } + $this->assertArrayNotHasKey( 'sources', $data['media_details'] ); + } }