diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 939501d3fc..6442a3db20 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -597,3 +597,41 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id } add_filter( 'the_content', 'webp_uploads_update_image_references', 10 ); + +/** + * Updates the response for an attachment to include sources for additional mime types available the image. + * + * @since n.e.x.t + * + * @param WP_REST_Response $response The original response object. + * @param WP_Post $post The post object. + * @param WP_REST_Request $request The request object. + * @return WP_REST_Response A new response object for the attachment with additional sources. + */ +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'] ) ) { + return $response; + } + + $metadata = wp_get_attachment_metadata( $post->ID ); + foreach ( $data['media_details']['sizes'] as $size => $details ) { + if ( empty( $metadata['sizes'][ $size ]['sources'] ) || ! is_array( $metadata['sizes'][ $size ]['sources'] ) ) { + continue; + } + + $sources = array(); + $directory = dirname( $data['media_details']['sizes'][ $size ]['source_url'] ); + foreach ( $metadata['sizes'][ $size ]['sources'] as $mime => $mime_details ) { + $source_url = "{$directory}/{$mime_details['file']}"; + $mime_details['source_url'] = $source_url; + $sources[ $mime ] = $mime_details; + } + + $data['media_details']['sizes'][ $size ]['sources'] = $sources; + } + + return rest_ensure_response( $data ); +} + +add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 4aa3ea0f7e..4d7a5e2c9c 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -619,6 +619,50 @@ public function data_provider_not_supported_webp_images() { yield 'GIFT image' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/earth.gif' ); } + /** + * Checks whether the sources information is added to image sizes details of the REST response object. + * + * @test + */ + public function it_should_add_sources_to_rest_response() { + $file_location = TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'; + $attachment_id = $this->factory->attachment->create_upload_object( $file_location ); + $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->assertInstanceOf( 'WP_REST_Response', $response ); + + $data = $response->get_data(); + $mime_types = array( + 'image/jpeg', + '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 ) ); + } + } + } + /** * Return an error when creating an additional image source with invalid parameters *