Sutherland: Could you incorporate this into the next stable?
Three steps copy-paste to add support for https, youtu.be URLs, private posts and mass recreation of deleted thumbs and readds support for Smart Youtube and Youtube Lyte to Video Thumbnails 1.7.6.
1. In video-thumbnails.php find from line 91 and change it from
// Check to see if thumbnail has already been found
if( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) {
to
// Check to see if thumbnail has already been found and still exists on our server as a file
if( ( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) && wp_remote_retrieve_response_code(wp_remote_head($thumbnail_meta)) === '200' ) {
2. Find from line 111 and change it from
// Checks for a standard YouTube embed
preg_match('#<object[^>]+>.+?http://www.youtube.com/[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
// More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
if(!isset($matches[1])) {
preg_match('#http://www.youtube.com/[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
// Checks for YouTube iframe
if(!isset($matches[1])) {
preg_match('#http://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
// Checks for any YouTube URL
if(!isset($matches[1])) {
preg_match('#http://w?w?w?.?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
to
// Checks for the old standard YouTube embed
preg_match('#<object[^>]+>.+?https?://www.youtube.com/[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
// More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
if(!isset($matches[1])) {
preg_match('#https?://www.youtube.com/[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
// Checks for YouTube iframe, the new standard since at least 2011
if(!isset($matches[1])) {
preg_match('#https?://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
// Checks for any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
if(!isset($matches[1])) {
preg_match('#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
// Checks for any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
if(!isset($matches[1])) {
preg_match('#(?:https?(?:a|vh?)?://)?youtu.be/([A-Za-z0-9\-_]+)#s', $markup, $matches);
}
3. Find line 336 (331 beforer the above changes) and change it from
if ( get_post_status() == 'publish') {
to
if ( get_post_status() == 'publish' || get_post_status() == 'private' ) {
Summary of changes in the second bit:
- Youtu.be support added as a separate if statement
- https? in all URLs to support https
- Added (?:a|vh?)? after https? to catch a, v or vh to support Youtube Lyte and Smart Youtube in the two catchalls
- Enclosed the protocol prefix in both of them in non-capturing parentheses to match also without it
- Changed w?w?w?.? to (?:www\.). This uses non-capturing parentheses and has the comma escaped. The previous one would have matched for example "wwyyoutube.com" (notice double w & y) which isn't desired
- Updated comments to reflect the above and tell between the old and new standard Youtube embed
The first bit checks the HTTP header to see if the thumbnail file still exists on the blog's server. Need for this arose as the scan past posts function of Video Thumbnails didn't retrieve new ones after mass deletion of the thumbs. There should be a smarter way to do this through the file system itself if someone wants to contribute it.
Tested with iframe embeds and bare youtu.be links that WordPress kindly autoembeds. Works like a charm with both. Also works with https, httpa, httpv, httpvh and their https counterparts with the bare youtu.be URL and should thus address the issues mentioned in [Plugin: Video Thumbnails] We didn't find a video thumbnail for this post.
Doesn't support Smart Youtube's httpvp or httpvhp playlists. Couldn't get any Youtube playlist embedding to work so that'll be left for later. No updates to the support of any other services than Youtube except for the private post type. Another thing that could use contributions is thinking if some other post statuses than public and private should be supported from this list: https://codex.wordpress.org/Function_Reference/get_post_status