class.wpcom-json-api-update-media-endpoint.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. new WPCOM_JSON_API_Update_Media_Endpoint( array(
  3. 'description' => 'Edit basic information about a media item.',
  4. 'group' => 'media',
  5. 'stat' => 'media:1:POST',
  6. 'method' => 'POST',
  7. 'path' => '/sites/%s/media/%d',
  8. 'deprecated' => true,
  9. 'max_version' => '1',
  10. 'new_version' => '1.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. 'request_format' => array(
  16. 'title' => '(string) The file name.',
  17. 'caption' => '(string) File caption.',
  18. 'description' => '(HTML) Description of the file.',
  19. ),
  20. 'response_format' => array(
  21. 'id' => '(int) The ID of the media item',
  22. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  23. 'parent' => '(int) ID of the post this media is attached to',
  24. 'link' => '(string) URL to the file',
  25. 'title' => '(string) File name',
  26. 'caption' => '(string) User provided caption of the file',
  27. 'description' => '(string) Description of the file',
  28. 'metadata' => '(array) Array of metadata about the file, such as Exif data or sizes',
  29. ),
  30. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/446',
  31. 'example_request_data' => array(
  32. 'headers' => array(
  33. 'authorization' => 'Bearer YOUR_API_TOKEN'
  34. ),
  35. 'body' => array(
  36. 'title' => 'Updated Title'
  37. )
  38. )
  39. ) );
  40. class WPCOM_JSON_API_Update_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
  41. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  42. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  43. if ( is_wp_error( $blog_id ) ) {
  44. return $blog_id;
  45. }
  46. if ( !current_user_can( 'upload_files', $media_id ) ) {
  47. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  48. }
  49. $item = $this->get_media_item( $media_id );
  50. if ( is_wp_error( $item ) ) {
  51. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  52. }
  53. $input = $this->input( true );
  54. $insert = array();
  55. if ( !empty( $input['title'] ) ) {
  56. $insert['post_title'] = $input['title'];
  57. }
  58. if ( !empty( $input['caption'] ) )
  59. $insert['post_excerpt'] = $input['caption'];
  60. if ( !empty( $input['description'] ) )
  61. $insert['post_content'] = $input['description'];
  62. $insert['ID'] = $media_id;
  63. wp_update_post( (object) $insert );
  64. $item = $this->get_media_item( $media_id );
  65. return $item;
  66. }
  67. }