Skip to content

Commit ea0c42c

Browse files
committed
Extend media_type and mime_type parameters in the WP REST Attachments Controller to support array values for filtering. Updated tests to validate functionality and ensure compatibility with existing features. Pulling over work from #9211 Props to @himanshupathak95
1 parent e4ad4b9 commit ea0c42c

File tree

2 files changed

+228
-75
lines changed

2 files changed

+228
-75
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function register_routes() {
7070
* prepares for WP_Query.
7171
*
7272
* @since 4.7.0
73-
* @since 6.9.0 Extends the `media_type` parameter to support filtering by multiple media types.
73+
* @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values.
7474
*
7575
* @param array $prepared_args Optional. Array of prepared arguments. Default empty array.
7676
* @param WP_REST_Request $request Optional. Request to prepare items for.
@@ -83,33 +83,38 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
8383
$query_args['post_status'] = 'inherit';
8484
}
8585

86-
$media_types = $this->get_media_types();
86+
$all_mime_types = array();
87+
$media_types = $this->get_media_types();
8788

8889
if ( ! empty( $request['media_type'] ) ) {
89-
if ( is_array( $request['media_type'] ) ) {
90-
$mime_types_query = array();
90+
$media_type_input = is_array( $request['media_type'] )
91+
? $request['media_type']
92+
: explode( ',', $request['media_type'] );
9193

92-
foreach ( $request['media_type'] as $request_media_type ) {
93-
if ( isset( $media_types[ $request_media_type ] ) ) {
94-
$mime_types_query = array_merge( $mime_types_query, $media_types[ $request_media_type ] );
95-
}
96-
}
97-
98-
$query_args['post_mime_type'] = $mime_types_query;
99-
} else {
100-
if ( isset( $media_types[ $request['media_type'] ] ) ) {
101-
$query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
94+
foreach ( array_map( 'trim', $media_type_input ) as $type ) {
95+
if ( isset( $media_types[ $type ] ) ) {
96+
$all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] );
10297
}
10398
}
10499
}
105100

106101
if ( ! empty( $request['mime_type'] ) ) {
107-
$parts = explode( '/', $request['mime_type'] );
108-
if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
109-
$query_args['post_mime_type'] = $request['mime_type'];
102+
$mime_type_input = is_array( $request['mime_type'] )
103+
? $request['mime_type']
104+
: explode( ',', $request['mime_type'] );
105+
106+
foreach ( array_map( 'trim', $mime_type_input ) as $mime_type ) {
107+
$parts = explode( '/', $mime_type );
108+
if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) {
109+
$all_mime_types[] = $mime_type;
110+
}
110111
}
111112
}
112113

114+
if ( ! empty( $all_mime_types ) ) {
115+
$query_args['post_mime_type'] = array_values( array_unique( $all_mime_types ) );
116+
}
117+
113118
// Filter query clauses to include filenames.
114119
if ( isset( $query_args['s'] ) ) {
115120
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
@@ -1320,7 +1325,7 @@ public static function get_filename_from_disposition( $disposition_header ) {
13201325
* Retrieves the query params for collections of attachments.
13211326
*
13221327
* @since 4.7.0
1323-
* @since 6.9.0 Extends the `media_type` parameter to support filtering by multiple media types.
1328+
* @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values.
13241329
*
13251330
* @return array Query parameters for the attachment collection as an array.
13261331
*/
@@ -1333,25 +1338,20 @@ public function get_collection_params() {
13331338
$params['media_type'] = array(
13341339
'default' => null,
13351340
'description' => __( 'Limit result set to attachments of a particular media type or media types.' ),
1336-
'oneOf' => array(
1337-
array(
1338-
'type' => 'string',
1339-
'enum' => $media_types,
1340-
),
1341-
array(
1342-
'type' => 'array',
1343-
'items' => array(
1344-
'type' => 'string',
1345-
'enum' => $media_types,
1346-
),
1347-
),
1341+
'type' => array( 'string', 'array' ),
1342+
'items' => array(
1343+
'type' => 'string',
1344+
'enum' => $media_types,
13481345
),
13491346
);
13501347

13511348
$params['mime_type'] = array(
13521349
'default' => null,
1353-
'description' => __( 'Limit result set to attachments of a particular MIME type.' ),
1354-
'type' => 'string',
1350+
'description' => __( 'Limit result set to attachments of a particular MIME type or MIME types.' ),
1351+
'type' => array( 'string', 'array' ),
1352+
'items' => array(
1353+
'type' => 'string',
1354+
),
13551355
);
13561356

13571357
return $params;

0 commit comments

Comments
 (0)