[wp-trac] [WordPress Trac] #55672: Media endpoint allows multiple media types on request, but only returns image in response

WordPress Trac noreply at wordpress.org
Wed May 4 08:40:46 UTC 2022


#55672: Media endpoint allows multiple media types on request, but only returns
image in response
--------------------------+-----------------------------
 Reporter:  grezvany13nl  |      Owner:  (none)
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  trunk
 Severity:  minor         |   Keywords:
  Focuses:  rest-api      |
--------------------------+-----------------------------
 The REST API for attachments (`/wp-json/wp/v2/media/`) allows to search
 for specific types based on `get_allowed_mime_types()`, giving a full
 range of types to be used.
 However the response of the same endpoint only returns 'image' or 'file'
 (aka everything else).

 This causes issues when requesting multiple types, since it won't be
 possible to differentiate them by type anymore (mime type is still
 correct). It's an issue I found while building a custom editor (Gutenberg)
 block which can handle multiple types (image, video, audio and pdf)


 Code which causes the issue:
 {{{
 if ( in_array( 'media_type', $fields, true ) ) {
    $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' :
 'file';
 }
 }}}
 https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes
 /rest-api/endpoints/class-wp-rest-attachments-controller.php#L756-L758


 **Workaround:**
 At the moment I use a workaround which modifies the REST response.
 {{{
 function get_real_media_type($post_id)
 {
     switch (true) {
         case wp_attachment_is('image', $post_id):
             return 'image';
         case wp_attachment_is('video', $post_id):
             return 'video';
         case wp_attachment_is('audio', $post_id):
             return 'audio';
         case wp_attachment_is('pdf', $post_id):
             return 'pdf';
         // add more types?

         // when no predefined type, return 'file'
         default:
             return 'file';
     }
 }

 add_filter('rest_prepare_attachment', function ($response, $post,
 $request) {
     if (
         is_object($response)
         && property_exists($response, 'data')
         && array_key_exists('media_type', $response->data)
     ) {
         $response->data['media_type'] =
 get_real_media_type($response->data['id']);
     }

     return $response;
 }, 10, 3);
 }}}


 **Solution:**
 Extend the response code for media files to include more different media
 types, instead of only 'image' and 'file'.
 This **shouldn't** cause any backwards compatibility issues, since 'image'
 will still work and 'file' will still be there in case it's a non-standard
 type.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55672>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list