| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: Gutenberg_REST_Attachments_Controller_7_1 class |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Controller which provides REST endpoint for retrieving attachments. |
| 10 |
* This overrides the core WP_REST_Attachments_Controller to provide |
| 11 |
* support for filtering by multiple media types. |
| 12 |
* |
| 13 |
* @since 7.1.0 |
| 14 |
* |
| 15 |
* @see WP_REST_Attachments_Controller |
| 16 |
*/ |
| 17 |
class Gutenberg_REST_Attachments_Controller_7_1 extends WP_REST_Attachments_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Determines the allowed query_vars for a get_items() response and |
| 21 |
* prepares for WP_Query. |
| 22 |
* |
| 23 |
* This overrides the parent method to add support for filtering by |
| 24 |
* multiple media types, which was added in WordPress 7.1. |
| 25 |
* |
| 26 |
* @since 4.7.0 |
| 27 |
* @since 7.1.0 Added orderby_mime_type filter to add custom ordering. |
| 28 |
* @since 7.1.0 Extends the `media_type` and `mime_type` request arguments to support array values. |
| 29 |
* |
| 30 |
* @param array $prepared_args Optional. Array of prepared arguments. Default empty array. |
| 31 |
* @param WP_REST_Request $request Optional. Request to prepare items for. |
| 32 |
* @return array Array of query arguments. |
| 33 |
*/ |
| 34 |
protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
| 35 |
// Store array parameters that we'll handle separately. |
| 36 |
$media_type_array = null; |
| 37 |
$mime_type_array = null; |
| 38 |
|
| 39 |
if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) { |
| 40 |
$media_type_array = $request['media_type']; |
| 41 |
unset( $request['media_type'] ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) { |
| 45 |
$mime_type_array = $request['mime_type']; |
| 46 |
unset( $request['mime_type'] ); |
| 47 |
} |
| 48 |
|
| 49 |
$query_args = parent::prepare_items_query( $prepared_args, $request ); |
| 50 |
|
| 51 |
// Restore the array parameters to the request. |
| 52 |
if ( null !== $media_type_array ) { |
| 53 |
$request['media_type'] = $media_type_array; |
| 54 |
} |
| 55 |
|
| 56 |
if ( null !== $mime_type_array ) { |
| 57 |
$request['mime_type'] = $mime_type_array; |
| 58 |
} |
| 59 |
|
| 60 |
if ( empty( $query_args['post_status'] ) ) { |
| 61 |
$query_args['post_status'] = 'inherit'; |
| 62 |
} |
| 63 |
|
| 64 |
$all_mime_types = array(); |
| 65 |
$media_types = $this->get_media_types(); |
| 66 |
|
| 67 |
if ( null !== $media_type_array ) { |
| 68 |
foreach ( $media_type_array as $type ) { |
| 69 |
if ( isset( $media_types[ $type ] ) ) { |
| 70 |
$all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( null !== $mime_type_array ) { |
| 76 |
foreach ( $mime_type_array as $mime_type ) { |
| 77 |
$parts = explode( '/', $mime_type ); |
| 78 |
if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) { |
| 79 |
$all_mime_types[] = $mime_type; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! empty( $all_mime_types ) ) { |
| 85 |
$query_args['post_mime_type'] = array_values( array_unique( $all_mime_types ) ); |
| 86 |
} |
| 87 |
|
| 88 |
// Filter query clauses to include filenames. |
| 89 |
if ( isset( $query_args['s'] ) ) { |
| 90 |
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); |
| 91 |
} |
| 92 |
|
| 93 |
return $query_args; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Retrieves the query params for collections of attachments. |
| 98 |
* |
| 99 |
* @since 4.7.0 |
| 100 |
* @since 7.1.0 Extends the `media_type` and `mime_type` request arguments to support array values. |
| 101 |
* |
| 102 |
* @return array Query parameters for the attachment collection as an array. |
| 103 |
*/ |
| 104 |
public function get_collection_params() { |
| 105 |
$params = parent::get_collection_params(); |
| 106 |
$params['status']['default'] = 'inherit'; |
| 107 |
$params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' ); |
| 108 |
$media_types = array_keys( $this->get_media_types() ); |
| 109 |
|
| 110 |
$params['media_type'] = array( |
| 111 |
'default' => null, |
| 112 |
'description' => __( 'Limit result set to attachments of a particular media type or media types.' ), |
| 113 |
'type' => 'array', |
| 114 |
'items' => array( |
| 115 |
'type' => 'string', |
| 116 |
'enum' => $media_types, |
| 117 |
), |
| 118 |
); |
| 119 |
|
| 120 |
$params['mime_type'] = array( |
| 121 |
'default' => null, |
| 122 |
'description' => __( 'Limit result set to attachments of a particular MIME type or MIME types.' ), |
| 123 |
'type' => 'array', |
| 124 |
'items' => array( |
| 125 |
'type' => 'string', |
| 126 |
), |
| 127 |
); |
| 128 |
|
| 129 |
return $params; |
| 130 |
} |
| 131 |
} |
| 132 |
|