Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in the ability to get thumbnails for VideoPress via media endpoint. #6631

Merged
merged 2 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions modules/videopress/class.videopress-xmlrpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,21 @@ public function update_poster_image( $request ) {
return false;
}

// We add ssl => 1 to make sure that the videos.files.wordpress.com domain is parsed as photon.
$poster = apply_filters( 'jetpack_photon_url', $poster, array( 'ssl' => 1 ), 'https' );

$meta = wp_get_attachment_metadata( $post_id );
$meta['videopress']['poster'] = $poster;
wp_update_attachment_metadata( $post_id, $meta );

// Update the poster in the VideoPress info.
$thumbnail_id = videopress_download_poster_image( $poster, $post_id );

if ( !is_int( $thumbnail_id ) ) {
if ( ! is_int( $thumbnail_id ) ) {
return false;
}

update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
$meta = wp_get_attachment_metadata( $post_id );

$meta['videopress']['poster'] = $poster;
wp_update_attachment_metadata( $post_id, $meta );

return true;
}
Expand Down
128 changes: 124 additions & 4 deletions modules/videopress/utility-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,141 @@ function videopress_make_media_upload_path( $blog_id ) {
function video_get_info_by_blogpostid( $blog_id, $post_id ) {
$post = get_post( $post_id );

$video_info = new stdClass();
$video_info->post_id = $post_id;
$video_info->blog_id = $blog_id;
$video_info->guid = null;
$video_info->finish_date_gmt = '0000-00-00 00:00:00';

if ( is_wp_error( $post ) ) {
return false;
return $video_info;
}

if ( 'video/videopress' !== $post->post_mime_type ) {
return false;
return $video_info;
}

$video_info = new stdClass();
// Since this is a VideoPress post, lt's fill out the rest of the object.
$video_info->guid = get_post_meta( $post_id, 'videopress_guid', true );
$video_info->finish_date_gmt = '0000-00-00 00:00:00';

if ( videopress_is_finished_processing( $post_id ) ) {
$video_info->finish_date_gmt = date( 'Y-m-d H:i:s' );
}

return $video_info;
}


/**
* Check that a VideoPress video format has finished processing.
*
* This uses the info object, because that is what the WPCOM endpoint
* uses, however we don't have a complete info object in the same way
* WPCOM does, so we pull the meta information out of the post
* options instead.
*
* Note: This mimics the WPCOM function of the same name and helps the media
* API endpoint add all needed VideoPress data.
*
* @param stdClass $info
* @param string $format
* @return bool
*/
function video_format_done( $info, $format ) {

// Avoids notice when a non-videopress item is found.
if ( ! is_object( $info ) ) {
return false;
}

$post_id = $info->post_id;

if ( get_post_mime_type( $post_id ) !== 'video/videopress' ) {
return false;
}

$post = get_post( $post_id );

if ( is_wp_error( $post ) ) {
return false;
}

$meta = wp_get_attachment_metadata( $post->ID );

switch ( $format ) {
case 'fmt_hd':
return isset( $meta['videopress']['files']['hd']['mp4'] );
break;

case 'fmt_dvd':
return isset( $meta['videopress']['files']['dvd']['mp4'] );
break;

case 'fmt_std':
return isset( $meta['videopress']['files']['std']['mp4'] );
break;

case 'fmt_ogg':
return isset( $meta['videopress']['files']['std']['ogg'] );
break;
}

return false;
}

/**
* Get the image URL for the given VideoPress GUID
*
* We look up by GUID, because that is what WPCOM does and this needs to be
* parameter compatible with that.
*
* Note: This mimics the WPCOM function of the same name and helps the media
* API endpoint add all needed VideoPress data.
*
* @param string $guid
* @param string $format
* @return string
*/
function video_image_url_by_guid( $guid, $format ) {

$post = video_get_post_by_guid( $guid );

if ( is_wp_error( $post ) ) {
return null;
}

$meta = wp_get_attachment_metadata( $post->ID );

// We add ssl => 1 to make sure that the videos.files.wordpress.com domain is parsed as photon.
$poster = apply_filters( 'jetpack_photon_url', $meta['videopress']['poster'], array( 'ssl' => 1 ), 'https' );

return $poster;
}

/**
* Using a GUID, find a post.
*
* @param string $guid
* @return WP_Post
*/
function video_get_post_by_guid( $guid ) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'video/videopress',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'videopress_guid',
'value' => $guid,
'compare' => '=',
)
)
);

$query = new WP_Query( $args );

$post = $query->next_post();

return $post;
}