Make WordPress Core


Ignore:
Timestamp:
10/09/2025 05:21:12 AM (11 months ago)
Author:
ramonopoly
Message:

Attachments REST API endpoint: add support for filtering attachments by multiple media types

This patch enhances the REST API media endpoint to allow filtering by multiple values for the media_type and mime_type parameters. String, comma-separated values and array are supported.

Props abcd95, ramonopoly, andrewserong, mukesh27, adamsilverstein, timothyblynjacobs, swissspidy.

Fixes #63668.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r60916 r60917  
    7171         *
    7272         * @since 4.7.0
     73         * @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values.
    7374         *
    7475         * @param array           $prepared_args Optional. Array of prepared arguments. Default empty array.
     
    8384                }
    8485
    85                 $media_types = $this->get_media_types();
    86 
    87                 if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) {
    88                         $query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
    89                 }
    90 
    91                 if ( ! empty( $request['mime_type'] ) ) {
    92                         $parts = explode( '/', $request['mime_type'] );
    93                         if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
    94                                 $query_args['post_mime_type'] = $request['mime_type'];
    95                         }
     86                $all_mime_types = array();
     87                $media_types    = $this->get_media_types();
     88
     89                if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) {
     90                        foreach ( $request['media_type'] as $type ) {
     91                                if ( isset( $media_types[ $type ] ) ) {
     92                                        $all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] );
     93                                }
     94                        }
     95                }
     96
     97                if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) {
     98                        foreach ( $request['mime_type'] as $mime_type ) {
     99                                $parts = explode( '/', $mime_type );
     100                                if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) {
     101                                        $all_mime_types[] = $mime_type;
     102                                }
     103                        }
     104                }
     105
     106                if ( ! empty( $all_mime_types ) ) {
     107                        $query_args['post_mime_type'] = array_values( array_unique( $all_mime_types ) );
    96108                }
    97109
     
    13431355         *
    13441356         * @since 4.7.0
     1357         * @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values.
    13451358         *
    13461359         * @return array Query parameters for the attachment collection as an array.
     
    13501363                $params['status']['default']       = 'inherit';
    13511364                $params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' );
    1352                 $media_types                       = $this->get_media_types();
     1365                $media_types                       = array_keys( $this->get_media_types() );
    13531366
    13541367                $params['media_type'] = array(
    13551368                        'default'     => null,
    1356                         'description' => __( 'Limit result set to attachments of a particular media type.' ),
    1357                         'type'        => 'string',
    1358                         'enum'        => array_keys( $media_types ),
     1369                        'description' => __( 'Limit result set to attachments of a particular media type or media types.' ),
     1370                        'type'        => 'array',
     1371                        'items'       => array(
     1372                                'type' => 'string',
     1373                                'enum' => $media_types,
     1374                        ),
    13591375                );
    13601376
    13611377                $params['mime_type'] = array(
    13621378                        'default'     => null,
    1363                         'description' => __( 'Limit result set to attachments of a particular MIME type.' ),
    1364                         'type'        => 'string',
     1379                        'description' => __( 'Limit result set to attachments of a particular MIME type or MIME types.' ),
     1380                        'type'        => 'array',
     1381                        'items'       => array(
     1382                                'type' => 'string',
     1383                        ),
    13651384                );
    13661385
Note: See TracChangeset for help on using the changeset viewer.