diff --git a/automatic-featured-images-from-videos.php b/automatic-featured-images-from-videos.php index ab719d2..47fdf4f 100644 --- a/automatic-featured-images-from-videos.php +++ b/automatic-featured-images-from-videos.php @@ -1,11 +1,11 @@ post_content ) ? $post->post_content : ''; - $content_check_length = function_exists( 'the_gutenberg_project' ) ? 4000 : 800; - /** * Only check the first 800 characters of our post, by default. * @@ -109,7 +107,7 @@ function wds_check_if_content_contains_video( $post_id, $post ) { * * @param int $value Character limit to search. */ - $content = substr( $content, 0, apply_filters( 'wds_featured_images_character_limit', $content_check_length ) ); + $content = substr( $content, 0, apply_filters( 'wds_featured_images_character_limit', 4000 ) ); // Allow developers to filter the content to allow for searching in postmeta or other places. $content = apply_filters( 'wds_featured_images_from_video_filter_content', $content, $post_id ); @@ -118,19 +116,25 @@ function wds_check_if_content_contains_video( $post_id, $post ) { $youtube_id = wds_check_for_youtube( $content ); $vimeo_id = wds_check_for_vimeo( $content ); $video_thumbnail_url = ''; + $youtube_details = []; + $vimeo_details = []; if ( $youtube_id ) { $youtube_details = wds_get_youtube_details( $youtube_id ); - $video_thumbnail_url = $youtube_details['video_thumbnail_url']; - $video_url = $youtube_details['video_url']; - $video_embed_url = $youtube_details['video_embed_url']; + if ( ! empty( $youtube_details ) ) { + $video_thumbnail_url = $youtube_details['video_thumbnail_url']; + $video_url = $youtube_details['video_url']; + $video_embed_url = $youtube_details['video_embed_url']; + } } if ( $vimeo_id ) { $vimeo_details = wds_get_vimeo_details( $vimeo_id ); - $video_thumbnail_url = $vimeo_details['video_thumbnail_url']; - $video_url = $vimeo_details['video_url']; - $video_embed_url = $vimeo_details['video_embed_url']; + if ( ! empty( $vimeo_details ) ) { + $video_thumbnail_url = $vimeo_details['video_thumbnail_url']; + $video_url = $vimeo_details['video_url']; + $video_embed_url = $vimeo_details['video_embed_url']; + } } if ( $post_id @@ -223,7 +227,7 @@ function wds_set_video_thumbnail_as_featured_image( $post_id, $video_thumbnail_u * */ function wds_check_for_youtube( $content ) { - if ( preg_match( '#\/\/(www\.)?(youtu|youtube|youtube-nocookie)\.(com|be)\/(watch|embed)?\/?(\?v=)?([a-zA-Z0-9\-\_]+)#', $content, $youtube_matches ) ) { + if ( preg_match( '#\/\/(www\.)?(youtu|youtube|youtube-nocookie)\.(com|be)\/(?!.*user)(watch|embed)?\/?(\?v=)?([a-zA-Z0-9\-\_]+)#', $content, $youtube_matches ) ) { return $youtube_matches[6]; } @@ -262,7 +266,7 @@ function wds_check_for_vimeo( $content ) { * * @return mixed */ -function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filename = null, $video_id ) { +function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filename = null, $video_id = null ) { if ( ! $url || ! $post_id ) { return new WP_Error( 'missing', esc_html__( 'Need a valid URL and post ID...', 'automatic-featured-images-from-videos' ) ); @@ -312,12 +316,12 @@ function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filenam $file_array['name'] = $url_filename; } - $post_data = array( + $post_data = [ // Just use the original filename (no extension). 'post_title' => get_the_title( $post_id ), // Make sure gets tied to parent. 'post_parent' => $post_id, - ); + ]; // Required libraries for media_handle_sideload. require_once( ABSPATH . 'wp-admin/includes/file.php' ); @@ -351,10 +355,10 @@ function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filenam * @return array Video data. */ function wds_get_youtube_details( $youtube_id ) { - $video = array(); - $video_thumbnail_url_string = 'http://img.youtube.com/vi/%s/%s'; + $video = []; + $video_thumbnail_url_string = 'https://img.youtube.com/vi/%s/%s'; - $video_check = wp_remote_get( 'https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $youtube_id ); + $video_check = wp_remote_get( 'https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=' . $youtube_id ); if ( 200 === wp_remote_retrieve_response_code( $video_check ) ) { $remote_headers = wp_remote_head( sprintf( @@ -392,14 +396,27 @@ function wds_get_youtube_details( $youtube_id ) { * @return array Video information. */ function wds_get_vimeo_details( $vimeo_id ) { - $video = array(); + $video = []; // @todo Get remote checking matching with wds_get_youtube_details. - $vimeo_data = wp_remote_get( 'http://www.vimeo.com/api/v2/video/' . intval( $vimeo_id ) . '.php' ); + $vimeo_data = wp_remote_get( 'https://www.vimeo.com/api/v2/video/' . intval( $vimeo_id ) . '.json' ); if ( 200 === wp_remote_retrieve_response_code( $vimeo_data ) ) { - $response = unserialize( $vimeo_data['body'] ); - $video['video_thumbnail_url'] = isset( $response[0]['thumbnail_large'] ) ? $response[0]['thumbnail_large'] : false; - $video['video_url'] = $response[0]['url']; + $response = json_decode( $vimeo_data['body'] ); + + $large = isset( $response[0]->thumbnail_large ) ? $response[0]->thumbnail_large : ''; + if ( $large ) { + $larger_test = explode( '_', $large ); + $test_result = wp_remote_head( + $larger_test[0] + ); + if ( 200 === wp_remote_retrieve_response_code( $test_result ) ) { + $large = $larger_test[0]; + } + } + + // For the moment, we will force jpg since WebP is still iffy. + $video['video_thumbnail_url'] = isset( $large ) ? $large . '.jpg' : false; + $video['video_url'] = $response[0]->url; $video['video_embed_url'] = 'https://player.vimeo.com/video/' . $vimeo_id; } @@ -471,9 +488,7 @@ function wds_get_embeddable_video_url( $post_id ) { * @author Gary Kovar * @since 1.1.0 */ -function wds_register_display_video_metabox() { - global $post; - +function wds_register_display_video_metabox( $post_type, $post ) { if ( get_post_meta( $post->ID, '_is_video', true ) ) { add_meta_box( 'wds_display_video_urls_metabox', @@ -507,16 +522,16 @@ function wds_video_thumbnail_meta() { * @return WP_Query WP_Query object */ function wds_automatic_featured_images_from_videos_wp_query( $post_type, $posts_per_page ) { - $args = array( + $args = [ 'post_type' => $post_type, - 'meta_query' => array( - array( + 'meta_query' => [ + [ 'key' => '_is_video', 'compare' => 'NOT EXISTS', - ), - ), + ], + ], 'posts_per_page' => $posts_per_page, 'fields' => 'ids', - ); + ]; return new WP_Query( $args ); } diff --git a/readme.txt b/readme.txt index b06497f..0a2f1fa 100644 --- a/readme.txt +++ b/readme.txt @@ -3,17 +3,18 @@ Contributors: bradparbs, coreymcollins, jtsternberg, webdevstudios, pluginize, binarygary Donate link: http://webdevstudios.com/ Tags: video, youtube, vimeo, featured image -Requires at least: 3.7 -Tested up to: 5.6.0 -Stable tag: 1.1.2 +Requires at least: 5.0 +Tested up to: 5.7.2 +Stable tag: 1.2.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html +Requires PHP: 5.6 -If a YouTube or Vimeo video exists in the first portion of a post, automatically set the post's featured image to that video's thumbnail. +If a YouTube or Vimeo video embed exists near the start of a post, we'll automatically set the post's featured image to a thumbnail of the video. == Description == -When placing a YouTube or Vimeo video within the first 800 characters of a post, the thumbnail of that video will automatically be sideloaded and set as the featured image for the post as long as the post does not already have a featured image set. +When placing a YouTube or Vimeo video within the first 4000 characters of a post, the thumbnail of that video will automatically be uploaded and set as the featured image for the post as long as the post does not already have a set featured image. In addition, after setting the video thumbnail as the featured image, an “is_video” post meta field is updated to allow for the use of conditional statements within your loop. @@ -39,6 +40,16 @@ In addition, after setting the video thumbnail as the featured image, an “is_v == Changelog == += 1.2.0 = +* Added: Support for potentially larger Vimeo images from API response. +* Fixed: Various PHP notices and errors. +* Updated: Minimum PHP version. +* Updated: bumped up default string length to 4000 characters, for URL searching in content. +* Updated: exclude user profile URLs from Youtube regex. +* Updated: Switched all endpoints to make sure we're using HTTPS. +* Updated: Vimeo endpoint switched to JSON responses. +* Updated: Plugin description. + = 1.1.2 = * Fixed: Issues with Youtube HEAD request returning 40x errors.