| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit( 0 ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* List media endpoint. |
| 9 |
*/ |
| 10 |
new WPCOM_JSON_API_List_Media_Endpoint( |
| 11 |
array( |
| 12 |
'description' => 'Get a list of items in the media library.', |
| 13 |
'group' => 'media', |
| 14 |
'stat' => 'media', |
| 15 |
|
| 16 |
'method' => 'GET', |
| 17 |
'path' => '/sites/%s/media/', |
| 18 |
'deprecated' => true, |
| 19 |
'new_version' => '1.1', |
| 20 |
'max_version' => '1', |
| 21 |
'path_labels' => array( |
| 22 |
'$site' => '(int|string) Site ID or domain', |
| 23 |
), |
| 24 |
|
| 25 |
'query_parameters' => array( |
| 26 |
'number' => '(int=20) The number of media items to return. Limit: 100.', |
| 27 |
'offset' => '(int=0) 0-indexed offset.', |
| 28 |
'parent_id' => '(int) Default is showing all items. The post where the media item is attached. 0 shows unattached media items.', |
| 29 |
'mime_type' => "(string) Default is empty. Filter by mime type (e.g., 'image/jpeg', 'application/pdf'). Partial searches also work (e.g. passing 'image' will search for all image files).", |
| 30 |
), |
| 31 |
|
| 32 |
'response_format' => array( |
| 33 |
'media' => '(array) Array of media', |
| 34 |
'found' => '(int) The number of total results found', |
| 35 |
), |
| 36 |
|
| 37 |
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/?number=2', |
| 38 |
'example_request_data' => array( |
| 39 |
'headers' => array( |
| 40 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 41 |
), |
| 42 |
), |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
/** |
| 47 |
* API List media endpoint class. |
| 48 |
* |
| 49 |
* @phan-constructor-used-for-side-effects |
| 50 |
*/ |
| 51 |
class WPCOM_JSON_API_List_Media_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 52 |
|
| 53 |
/** |
| 54 |
* API callback. |
| 55 |
* |
| 56 |
* @param string $path - the path. |
| 57 |
* @param string $blog_id - the blog ID. |
| 58 |
*/ |
| 59 |
public function callback( $path = '', $blog_id = 0 ) { |
| 60 |
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 61 |
if ( is_wp_error( $blog_id ) ) { |
| 62 |
return $blog_id; |
| 63 |
} |
| 64 |
|
| 65 |
// upload_files can probably be used for other endpoints but we want contributors to be able to use media too. |
| 66 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 67 |
return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); |
| 68 |
} |
| 69 |
|
| 70 |
$args = $this->query_args(); |
| 71 |
|
| 72 |
if ( $args['number'] < 1 ) { |
| 73 |
$args['number'] = 20; |
| 74 |
} elseif ( 100 < $args['number'] ) { |
| 75 |
return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); |
| 76 |
} |
| 77 |
|
| 78 |
$media = get_posts( |
| 79 |
array( |
| 80 |
'post_type' => 'attachment', |
| 81 |
'post_parent' => $args['parent_id'], |
| 82 |
'offset' => $args['offset'], |
| 83 |
'numberposts' => $args['number'], |
| 84 |
'post_mime_type' => $args['mime_type'], |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
$response = array(); |
| 89 |
foreach ( $media as $item ) { |
| 90 |
$response[] = $this->get_media_item( $item->ID ); |
| 91 |
} |
| 92 |
|
| 93 |
$_num = (array) wp_count_attachments(); |
| 94 |
$_total_media = array_sum( $_num ) - $_num['trash']; |
| 95 |
|
| 96 |
$return = array( |
| 97 |
'found' => $_total_media, |
| 98 |
'media' => $response, |
| 99 |
); |
| 100 |
|
| 101 |
return $return; |
| 102 |
} |
| 103 |
} |
| 104 |
|