-
Notifications
You must be signed in to change notification settings - Fork 3.2k
REST API: Support multiple media types and MIME types when listing media #9211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1faea18
f242f5e
ff91633
5fe24dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,17 +82,31 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul | |
| $query_args['post_status'] = 'inherit'; | ||
| } | ||
|
|
||
| $media_types = $this->get_media_types(); | ||
| $all_mime_types = array(); | ||
| $media_types = $this->get_media_types(); | ||
|
|
||
| if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) { | ||
| $query_args['post_mime_type'] = $media_types[ $request['media_type'] ]; | ||
| if ( ! empty( $request['media_type'] ) ) { | ||
| $media_type_input = is_array( $request['media_type'] ) | ||
| ? $request['media_type'] | ||
| : explode( ',', $request['media_type'] ); | ||
|
|
||
| foreach ( array_map( 'trim', $media_type_input ) as $type ) { | ||
| if ( isset( $media_types[ $type ] ) ) { | ||
| $all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( ! empty( $request['mime_type'] ) ) { | ||
| $parts = explode( '/', $request['mime_type'] ); | ||
| if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) { | ||
| $query_args['post_mime_type'] = $request['mime_type']; | ||
| } | ||
| $mime_type_input = is_array( $request['mime_type'] ) | ||
| ? $request['mime_type'] | ||
| : explode( ',', $request['mime_type'] ); | ||
|
|
||
| $all_mime_types = array_merge( $all_mime_types, array_map( 'trim', $mime_type_input ) ); | ||
|
Comment on lines
+101
to
+105
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need the former check to see if the mime type prefix part is available in |
||
| } | ||
|
|
||
| if ( ! empty( $all_mime_types ) ) { | ||
| $query_args['post_mime_type'] = array_values( array_unique( $all_mime_types ) ); | ||
| } | ||
|
|
||
| // Filter query clauses to include filenames. | ||
|
|
@@ -1285,19 +1299,24 @@ public function get_collection_params() { | |
| $params = parent::get_collection_params(); | ||
| $params['status']['default'] = 'inherit'; | ||
| $params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' ); | ||
| $media_types = $this->get_media_types(); | ||
|
|
||
| $params['media_type'] = array( | ||
| 'default' => null, | ||
| 'description' => __( 'Limit result set to attachments of a particular media type.' ), | ||
| 'type' => 'string', | ||
| 'enum' => array_keys( $media_types ), | ||
| 'description' => __( 'Limit result set to attachments of particular media types.' ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, this method's docblock needs an annotation: |
||
| 'type' => array( 'string', 'array' ), | ||
| 'items' => array( | ||
| 'type' => 'string', | ||
| 'enum' => array_keys( $this->get_media_types() ), | ||
| ), | ||
| ); | ||
|
|
||
| $params['mime_type'] = array( | ||
| 'default' => null, | ||
| 'description' => __( 'Limit result set to attachments of a particular MIME type.' ), | ||
| 'type' => 'string', | ||
| 'description' => __( 'Limit result set to attachments of particular MIME types.' ), | ||
| 'type' => array( 'string', 'array' ), | ||
| 'items' => array( | ||
| 'type' => 'string', | ||
| ), | ||
| ); | ||
|
|
||
| return $params; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a
@since 6.9.0annotation in the doc block. 🤞🏻 this makes it to 6.9!E.g.,
@since 6.9.0 Extends the media_type and mime_type request arguments to support array values.