Skip to content

Commit

Permalink
json-endpoint: add media endpoint definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox committed Feb 28, 2017
1 parent b29b0b8 commit 01c4bb6
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 13 deletions.
121 changes: 121 additions & 0 deletions json-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
require_once( $json_endpoints_dir . 'class.wpcom-json-api-site-settings-v1-2-endpoint.php' );
require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-site-v1-2-endpoint.php' );

// Media
require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-media-v1-2-endpoint.php' );
require_once( $json_endpoints_dir . 'class.wpcom-json-api-edit-media-v1-2-endpoint.php' );

// Jetpack Only Endpoints
$json_jetpack_endpoints_dir = dirname( __FILE__ ) . '/json-endpoints/jetpack/';

Expand Down Expand Up @@ -1296,6 +1300,52 @@
)
) );

new WPCOM_JSON_API_Get_Media_v1_2_Endpoint( array(
'description' => 'Get a single media item (by ID).',
'group' => 'media',
'stat' => 'media:1',
'min_version' => '1.2',
'max_version' => '1.2',
'method' => 'GET',
'path' => '/sites/%s/media/%d',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
'$media_ID' => '(int) The ID of the media item',
),
'response_format' => array(
'ID' => '(int) The ID of the media item',
'date' => '(ISO 8601 datetime) The date the media was uploaded',
'post_ID' => '(int) ID of the post this media is attached to',
'author_ID' => '(int) ID of the user who uploaded the media',
'URL' => '(string) URL to the file',
'guid' => '(string) Unique identifier',
'file' => '(string) Filename',
'extension' => '(string) File extension',
'mime_type' => '(string) File MIME type',
'title' => '(string) Filename',
'caption' => '(string) User-provided caption of the file',
'description' => '(string) Description of the file',
'alt' => '(string) Alternative text for image files.',
'thumbnails' => '(object) Media item thumbnail URL options',
'height' => '(int) (Image & video only) Height of the media item',
'width' => '(int) (Image & video only) Width of the media item',
'length' => '(int) (Video & audio only) Duration of the media item, in seconds',
'exif' => '(array) (Image & audio only) Exif (meta) information about the media item',
'videopress_guid' => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
'videopress_processing_done' => '(bool) (Video only) If the video is uploaded on a blog with VideoPress, this will return the status of processing on the video.',
'revision_history' => '(array) An array of snapshots of the previous images of this Media.' .
'Each item has useful data such as `URL`, `date, `extension`, `width` and `height`,' .
'`mime_type` and the `thumbnails` array.'
),

'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media/934',
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
)
)
) );

new WPCOM_JSON_API_Upload_Media_Endpoint( array(
'description' => 'Upload a new media item.',
'group' => 'media',
Expand Down Expand Up @@ -1464,6 +1514,77 @@
)
) );

new WPCOM_JSON_API_Edit_Media_v1_2_Endpoint( array(
'description' => 'Edit a media item.',
'group' => 'media',
'stat' => 'media:1:POST',
'min_version' => '1',
'max_version' => '1.2',
'method' => 'POST',
'path' => '/sites/%s/media/%d/edit',
'path_labels' => array(
'$site' => '(int|string) Site ID or domain',
'$media_ID' => '(int) The ID of the media item',
),

'request_format' => array(
'parent_id' => '(int) ID of the post this media is attached to',
'title' => '(string) The file name.',
'caption' => '(string) File caption.',
'description' => '(HTML) Description of the file.',
'alt' => "(string) Alternative text for image files.",
'artist' => "(string) Audio Only. Artist metadata for the audio track.",
'album' => "(string) Audio Only. Album metadata for the audio track.",
'media' => "(object) An object file to attach to the post. To upload media, " .
"the entire request should be multipart/form-data encoded. " .
"Multiple media items will be displayed in a gallery. Accepts " .
"jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. " .
"Audio and Video may also be available. See <code>allowed_file_types</code> " .
"in the options response of the site endpoint. " .
"<br /><br /><strong>Example</strong>:<br />" .
"<code>curl \<br />--form 'title=Image' \<br />--form 'media=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
'attrs' => "(object) An Object of attributes (`title`, `description` and `caption`) " .
"are supported to assign to the media uploaded via the `media` or `media_url`",
'media_url' => "(string) An URL of the image to attach to a post.",
),

'response_format' => array(
'ID' => '(int) The ID of the media item',
'date' => '(ISO 8601 datetime) The date the media was uploaded',
'post_ID' => '(int) ID of the post this media is attached to',
'author_ID' => '(int) ID of the user who uploaded the media',
'URL' => '(string) URL to the file',
'guid' => '(string) Unique identifier',
'file' => '(string) File name',
'extension' => '(string) File extension',
'mime_type' => '(string) File mime type',
'title' => '(string) File name',
'caption' => '(string) User provided caption of the file',
'description' => '(string) Description of the file',
'alt' => '(string) Alternative text for image files.',
'thumbnails' => '(object) Media item thumbnail URL options',
'height' => '(int) (Image & video only) Height of the media item',
'width' => '(int) (Image & video only) Width of the media item',
'length' => '(int) (Video & audio only) Duration of the media item, in seconds',
'exif' => '(array) (Image & audio only) Exif (meta) information about the media item',
'videopress_guid' => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
'videopress_processing_done' => '(bool) (Video only) If the video is uploaded on a blog with VideoPress, this will return the status of processing on the video.',
'revision_history' => '(object) An object with `items` and `original` keys. ' .
'`original` is an object with data about the original image. ' .
'`items` is an array of snapshots of the previous images of this Media. ' .
'Each item has the `URL`, `file, `extension`, `date`, and `mime_type` fields.'
),

'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media/446',
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
),
'body' => array(
'title' => 'Updated Title'
)
)
) );

new WPCOM_JSON_API_Delete_Media_Endpoint( array(
'description' => 'Delete a piece of media.',
Expand Down
17 changes: 4 additions & 13 deletions json-endpoints/class.wpcom-json-api-edit-media-v1-2-endpoint.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/*
* WARNING: This file is distributed verbatim in Jetpack.
* There should be nothing WordPress.com specific in this file.
*
* @hide-in-jetpack
*/

jetpack_require_lib( 'class.media' );

Expand All @@ -15,7 +9,7 @@ class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Medi
*
* @param {Number} $media_id - post media ID
* @param {Object} $attrs - `attrs` parameter sent from the client in the request body
* @return
* @return bool|WP_Error `WP_Error` on failure. `true` on success.
*/
private function update_by_attrs_parameter( $media_id, $attrs ) {
$insert = array();
Expand All @@ -37,7 +31,7 @@ private function update_by_attrs_parameter( $media_id, $attrs ) {
$insert['ID'] = $media_id;
$update_action = wp_update_post( (object) $insert );
if ( is_wp_error( $update_action ) ) {
return $update_action;
$update_action;
}
}

Expand All @@ -60,14 +54,11 @@ private function update_by_attrs_parameter( $media_id, $attrs ) {
// Before updating metadata, ensure that the item is audio
$item = $this->get_media_item_v1_1( $media_id );
if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
$update_action = wp_update_attachment_metadata( $media_id, $id3_meta );
if ( is_wp_error( $update_action ) ) {
return $update_action;
}
wp_update_attachment_metadata( $media_id, $id3_meta );
}
}

return $post_update_action;
return $update_action;
}

/**
Expand Down

0 comments on commit 01c4bb6

Please sign in to comment.