mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Media: Add information about additional MIME type sources to attachments REST endpoints.
This changeset is a follow-up to [53751] which ensures the additional `sources` information stored in attachment metadata are available under `media_details` for each image size in the REST API responses for attachments. Props mukesh27, eugenemanuilov, mitogh, flixos90, aaemnnosttv. See #55443. git-svn-id: https://develop.svn.wordpress.org/trunk@53786 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
09808ef048
commit
a9c64ad629
@ -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;
|
||||
|
||||
@ -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'] );
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user