class.wpcom-json-api-get-media-endpoint.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. new WPCOM_JSON_API_Get_Media_Endpoint( array(
  3. 'description' => 'Get a single media item (by ID).',
  4. 'group' => 'media',
  5. 'stat' => 'media:1',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/media/%d',
  8. 'deprecated' => true,
  9. 'new_version' => '1.1',
  10. 'max_version' => '1',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. '$media_ID' => '(int) The ID of the media item',
  14. ),
  15. 'response_format' => array(
  16. 'id' => '(int) The ID of the media item',
  17. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  18. 'parent' => '(int) ID of the post this media is attached to',
  19. 'link' => '(string) URL to the file',
  20. 'title' => '(string) Filename',
  21. 'caption' => '(string) User-provided caption of the file',
  22. 'description' => '(string) Description of the file',
  23. 'metadata' => '(array) Array of metadata about the file, such as Exif data or sizes',
  24. ),
  25. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/934',
  26. 'example_request_data' => array(
  27. 'headers' => array(
  28. 'authorization' => 'Bearer YOUR_API_TOKEN'
  29. )
  30. )
  31. ) );
  32. class WPCOM_JSON_API_Get_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
  33. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  34. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  35. if ( is_wp_error( $blog_id ) ) {
  36. return $blog_id;
  37. }
  38. //upload_files can probably be used for other endpoints but we want contributors to be able to use media too
  39. if ( !current_user_can( 'edit_posts', $media_id ) ) {
  40. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  41. }
  42. return $this->get_media_item( $media_id );
  43. }
  44. }