| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit( 0 ); |
| 5 |
} |
| 6 |
|
| 7 |
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php'; |
| 8 |
|
| 9 |
/** |
| 10 |
* List media v1_2 endpoint. |
| 11 |
*/ |
| 12 |
new WPCOM_JSON_API_List_Media_v1_2_Endpoint( |
| 13 |
array( |
| 14 |
'description' => 'Get a list of items in the media library.', |
| 15 |
'group' => 'media', |
| 16 |
'stat' => 'media', |
| 17 |
'min_version' => '1.2', |
| 18 |
'max_version' => '1.2', |
| 19 |
'method' => 'GET', |
| 20 |
'path' => '/sites/%s/media/', |
| 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 |
'page' => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.', |
| 29 |
'page_handle' => '(string) A page handle, returned from a previous API call as a <code>meta.next_page</code> property. This is the most efficient way to fetch the next page of results.', |
| 30 |
'order' => array( |
| 31 |
'DESC' => 'Return files in descending order. For dates, that means newest to oldest.', |
| 32 |
'ASC' => 'Return files in ascending order. For dates, that means oldest to newest.', |
| 33 |
), |
| 34 |
'order_by' => array( |
| 35 |
'date' => 'Order by the uploaded time of each file.', |
| 36 |
'title' => 'Order lexicographically by file titles.', |
| 37 |
'ID' => 'Order by media ID.', |
| 38 |
), |
| 39 |
'search' => '(string) Search query.', |
| 40 |
'post_ID' => '(int) Default is showing all items. The post where the media item is attached. 0 shows unattached media items.', |
| 41 |
'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).", |
| 42 |
'after' => '(ISO 8601 datetime) Return media items uploaded after the specified datetime.', |
| 43 |
'before' => '(ISO 8601 datetime) Return media items uploaded before the specified datetime.', |
| 44 |
), |
| 45 |
|
| 46 |
'response_format' => array( |
| 47 |
'media' => '(array) Array of media objects', |
| 48 |
'found' => '(int) The number of total results found', |
| 49 |
'meta' => '(object) Meta data', |
| 50 |
), |
| 51 |
|
| 52 |
'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media', |
| 53 |
'example_request_data' => array( |
| 54 |
'headers' => array( |
| 55 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 56 |
), |
| 57 |
), |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
/** |
| 62 |
* List Media v1_2 endpoint. |
| 63 |
* |
| 64 |
* @phan-constructor-used-for-side-effects |
| 65 |
*/ |
| 66 |
class WPCOM_JSON_API_List_Media_v1_2_Endpoint extends WPCOM_JSON_API_List_Media_v1_1_Endpoint { // phpcs:ignore |
| 67 |
/** |
| 68 |
* API callback. |
| 69 |
* |
| 70 |
* @param string $path - the path. |
| 71 |
* @param string $blog_id - the blog ID. |
| 72 |
*/ |
| 73 |
public function callback( $path = '', $blog_id = 0 ) { |
| 74 |
$response = parent::callback( $path, $blog_id ); |
| 75 |
|
| 76 |
if ( is_wp_error( $response ) ) { |
| 77 |
return $response; |
| 78 |
} |
| 79 |
|
| 80 |
$media_list = $response['media']; |
| 81 |
|
| 82 |
if ( ! is_countable( $media_list ) || count( $media_list ) === 0 ) { |
| 83 |
return $response; |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $media_list as $media_item ) { |
| 87 |
// expose `revision_history` object for each image. |
| 88 |
$media_item->revision_history = (object) array( |
| 89 |
'items' => (array) Jetpack_Media::get_revision_history( $media_item->ID ), |
| 90 |
'original' => (object) Jetpack_Media::get_original_media( $media_item->ID ), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
return $response; |
| 95 |
} |
| 96 |
} |
| 97 |
|