class.wpcom-json-api-list-media-v1-2-endpoint.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. jetpack_require_lib( 'class.media' );
  3. new WPCOM_JSON_API_List_Media_v1_2_Endpoint( array(
  4. 'description' => 'Get a list of items in the media library.',
  5. 'group' => 'media',
  6. 'stat' => 'media',
  7. 'min_version' => '1.2',
  8. 'max_version' => '1.2',
  9. 'method' => 'GET',
  10. 'path' => '/sites/%s/media/',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. ),
  14. 'query_parameters' => array(
  15. 'number' => '(int=20) The number of media items to return. Limit: 100.',
  16. 'offset' => '(int=0) 0-indexed offset.',
  17. 'page' => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.',
  18. '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.',
  19. 'order' => array(
  20. 'DESC' => 'Return files in descending order. For dates, that means newest to oldest.',
  21. 'ASC' => 'Return files in ascending order. For dates, that means oldest to newest.',
  22. ),
  23. 'order_by' => array(
  24. 'date' => 'Order by the uploaded time of each file.',
  25. 'title' => "Order lexicographically by file titles.",
  26. 'ID' => 'Order by media ID.',
  27. ),
  28. 'search' => '(string) Search query.',
  29. 'post_ID' => '(int) Default is showing all items. The post where the media item is attached. 0 shows unattached media items.',
  30. '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).",
  31. 'after' => '(ISO 8601 datetime) Return media items uploaded after the specified datetime.',
  32. 'before' => '(ISO 8601 datetime) Return media items uploaded before the specified datetime.',
  33. ),
  34. 'response_format' => array(
  35. 'media' => '(array) Array of media objects',
  36. 'found' => '(int) The number of total results found',
  37. 'meta' => '(object) Meta data',
  38. ),
  39. 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media',
  40. 'example_request_data' => array(
  41. 'headers' => array(
  42. 'authorization' => 'Bearer YOUR_API_TOKEN'
  43. )
  44. )
  45. ) );
  46. class WPCOM_JSON_API_List_Media_v1_2_Endpoint extends WPCOM_JSON_API_List_Media_v1_1_Endpoint {
  47. function callback( $path = '', $blog_id = 0 ) {
  48. $response = parent::callback( $path, $blog_id );
  49. if ( is_wp_error( $response ) ) {
  50. return $response;
  51. }
  52. $media_list = $response['media'];
  53. if ( count( $media_list ) < 1 ) {
  54. return $response;
  55. }
  56. foreach ( $media_list as $index => $media_item ) {
  57. // expose `revision_history` object for each image
  58. $media_item->revision_history = (object) array(
  59. 'items' => (array) Jetpack_Media::get_revision_history( $media_item->ID ),
  60. 'original' => (object) Jetpack_Media::get_original_media( $media_item->ID )
  61. );
  62. }
  63. return $response;
  64. }
  65. }