Skip to content

Commit f0cd966

Browse files
committed
Release 1.2.0
1 parent 4f9ed08 commit f0cd966

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed

automatic-featured-images-from-videos.php

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
/*
33
* Plugin Name: Automatic Featured Images from YouTube / Vimeo
4-
* Plugin URI: http://webdevstudios.com
4+
* Plugin URI: https://webdevstudios.com
55
* Description: If a YouTube or Vimeo video exists in the first few paragraphs of a post, automatically set the post's featured image to that video's thumbnail.
6-
* Version: 1.1.2
6+
* Version: 1.2.0
77
* Author: WebDevStudios
8-
* Author URI: http://webdevstudios.com
8+
* Author URI: https://webdevstudios.com
99
* License: GPLv2
1010
* Text Domain: automatic-featured-images-from-videos
1111
*/
@@ -35,7 +35,7 @@
3535
add_action( 'save_post', 'wds_check_if_content_contains_video', 10, 2 );
3636

3737
// Add a meta box to the post types we are checking for video on.
38-
add_action( 'add_meta_boxes', 'wds_register_display_video_metabox' );
38+
add_action( 'add_meta_boxes', 'wds_register_display_video_metabox', 10, 2 );
3939

4040
// Create an endpoint that receives the params to start bulk processing.
4141
add_action( 'wp_ajax_wds_queue_bulk_processing', 'wds_queue_bulk_processing' );
@@ -73,7 +73,7 @@ function wds_load_afi() {
7373
*/
7474
function wds_set_media_as_featured_image( $post_id, $post ) {
7575
wds_check_if_content_contains_video( $post_id, $post );
76-
_doing_it_wrong( 'wds_set_media_as_feature_image', esc_html( 'This function has been replaced with wds_check_if_content_contains_video', 'automatic-featured-images-from-videos' ), '4.6' );
76+
_doing_it_wrong( 'wds_set_media_as_feature_image', esc_html__( 'This function has been replaced with wds_check_if_content_contains_video', 'automatic-featured-images-from-videos' ), '4.6' );
7777
}
7878

7979
/**
@@ -94,22 +94,20 @@ function wds_check_if_content_contains_video( $post_id, $post ) {
9494

9595
// We need to prevent trying to assign when trashing or untrashing posts in the list screen.
9696
// get_current_screen() was not providing a unique enough value to use here.
97-
if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'trash', 'untrash' ) ) ) {
97+
if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], [ 'trash', 'untrash', 'add-menu-item' ] ) ) {
9898
return;
9999
}
100100

101101
$content = isset( $post->post_content ) ? $post->post_content : '';
102102

103-
$content_check_length = function_exists( 'the_gutenberg_project' ) ? 4000 : 800;
104-
105103
/**
106104
* Only check the first 800 characters of our post, by default.
107105
*
108106
* @since 1.0.0
109107
*
110108
* @param int $value Character limit to search.
111109
*/
112-
$content = substr( $content, 0, apply_filters( 'wds_featured_images_character_limit', $content_check_length ) );
110+
$content = substr( $content, 0, apply_filters( 'wds_featured_images_character_limit', 4000 ) );
113111

114112
// Allow developers to filter the content to allow for searching in postmeta or other places.
115113
$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 ) {
118116
$youtube_id = wds_check_for_youtube( $content );
119117
$vimeo_id = wds_check_for_vimeo( $content );
120118
$video_thumbnail_url = '';
119+
$youtube_details = [];
120+
$vimeo_details = [];
121121

122122
if ( $youtube_id ) {
123123
$youtube_details = wds_get_youtube_details( $youtube_id );
124-
$video_thumbnail_url = $youtube_details['video_thumbnail_url'];
125-
$video_url = $youtube_details['video_url'];
126-
$video_embed_url = $youtube_details['video_embed_url'];
124+
if ( ! empty( $youtube_details ) ) {
125+
$video_thumbnail_url = $youtube_details['video_thumbnail_url'];
126+
$video_url = $youtube_details['video_url'];
127+
$video_embed_url = $youtube_details['video_embed_url'];
128+
}
127129
}
128130

129131
if ( $vimeo_id ) {
130132
$vimeo_details = wds_get_vimeo_details( $vimeo_id );
131-
$video_thumbnail_url = $vimeo_details['video_thumbnail_url'];
132-
$video_url = $vimeo_details['video_url'];
133-
$video_embed_url = $vimeo_details['video_embed_url'];
133+
if ( ! empty( $vimeo_details ) ) {
134+
$video_thumbnail_url = $vimeo_details['video_thumbnail_url'];
135+
$video_url = $vimeo_details['video_url'];
136+
$video_embed_url = $vimeo_details['video_embed_url'];
137+
}
134138
}
135139

136140
if ( $post_id
@@ -223,7 +227,7 @@ function wds_set_video_thumbnail_as_featured_image( $post_id, $video_thumbnail_u
223227
*
224228
*/
225229
function wds_check_for_youtube( $content ) {
226-
if ( preg_match( '#\/\/(www\.)?(youtu|youtube|youtube-nocookie)\.(com|be)\/(watch|embed)?\/?(\?v=)?([a-zA-Z0-9\-\_]+)#', $content, $youtube_matches ) ) {
230+
if ( preg_match( '#\/\/(www\.)?(youtu|youtube|youtube-nocookie)\.(com|be)\/(?!.*user)(watch|embed)?\/?(\?v=)?([a-zA-Z0-9\-\_]+)#', $content, $youtube_matches ) ) {
227231
return $youtube_matches[6];
228232
}
229233

@@ -262,7 +266,7 @@ function wds_check_for_vimeo( $content ) {
262266
*
263267
* @return mixed
264268
*/
265-
function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filename = null, $video_id ) {
269+
function wds_ms_media_sideload_image_with_new_filename( $url, $post_id, $filename = null, $video_id = null ) {
266270

267271
if ( ! $url || ! $post_id ) {
268272
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
312316
$file_array['name'] = $url_filename;
313317
}
314318

315-
$post_data = array(
319+
$post_data = [
316320
// Just use the original filename (no extension).
317321
'post_title' => get_the_title( $post_id ),
318322
// Make sure gets tied to parent.
319323
'post_parent' => $post_id,
320-
);
324+
];
321325

322326
// Required libraries for media_handle_sideload.
323327
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
351355
* @return array Video data.
352356
*/
353357
function wds_get_youtube_details( $youtube_id ) {
354-
$video = array();
355-
$video_thumbnail_url_string = 'http://img.youtube.com/vi/%s/%s';
358+
$video = [];
359+
$video_thumbnail_url_string = 'https://img.youtube.com/vi/%s/%s';
356360

357-
$video_check = wp_remote_get( 'https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $youtube_id );
361+
$video_check = wp_remote_get( 'https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=' . $youtube_id );
358362
if ( 200 === wp_remote_retrieve_response_code( $video_check ) ) {
359363
$remote_headers = wp_remote_head(
360364
sprintf(
@@ -392,14 +396,27 @@ function wds_get_youtube_details( $youtube_id ) {
392396
* @return array Video information.
393397
*/
394398
function wds_get_vimeo_details( $vimeo_id ) {
395-
$video = array();
399+
$video = [];
396400

397401
// @todo Get remote checking matching with wds_get_youtube_details.
398-
$vimeo_data = wp_remote_get( 'http://www.vimeo.com/api/v2/video/' . intval( $vimeo_id ) . '.php' );
402+
$vimeo_data = wp_remote_get( 'https://www.vimeo.com/api/v2/video/' . intval( $vimeo_id ) . '.json' );
399403
if ( 200 === wp_remote_retrieve_response_code( $vimeo_data ) ) {
400-
$response = unserialize( $vimeo_data['body'] );
401-
$video['video_thumbnail_url'] = isset( $response[0]['thumbnail_large'] ) ? $response[0]['thumbnail_large'] : false;
402-
$video['video_url'] = $response[0]['url'];
404+
$response = json_decode( $vimeo_data['body'] );
405+
406+
$large = isset( $response[0]->thumbnail_large ) ? $response[0]->thumbnail_large : '';
407+
if ( $large ) {
408+
$larger_test = explode( '_', $large );
409+
$test_result = wp_remote_head(
410+
$larger_test[0]
411+
);
412+
if ( 200 === wp_remote_retrieve_response_code( $test_result ) ) {
413+
$large = $larger_test[0];
414+
}
415+
}
416+
417+
// For the moment, we will force jpg since WebP is still iffy.
418+
$video['video_thumbnail_url'] = isset( $large ) ? $large . '.jpg' : false;
419+
$video['video_url'] = $response[0]->url;
403420
$video['video_embed_url'] = 'https://player.vimeo.com/video/' . $vimeo_id;
404421
}
405422

@@ -471,9 +488,7 @@ function wds_get_embeddable_video_url( $post_id ) {
471488
* @author Gary Kovar
472489
* @since 1.1.0
473490
*/
474-
function wds_register_display_video_metabox() {
475-
global $post;
476-
491+
function wds_register_display_video_metabox( $post_type, $post ) {
477492
if ( get_post_meta( $post->ID, '_is_video', true ) ) {
478493
add_meta_box(
479494
'wds_display_video_urls_metabox',
@@ -507,16 +522,16 @@ function wds_video_thumbnail_meta() {
507522
* @return WP_Query WP_Query object
508523
*/
509524
function wds_automatic_featured_images_from_videos_wp_query( $post_type, $posts_per_page ) {
510-
$args = array(
525+
$args = [
511526
'post_type' => $post_type,
512-
'meta_query' => array(
513-
array(
527+
'meta_query' => [
528+
[
514529
'key' => '_is_video',
515530
'compare' => 'NOT EXISTS',
516-
),
517-
),
531+
],
532+
],
518533
'posts_per_page' => $posts_per_page,
519534
'fields' => 'ids',
520-
);
535+
];
521536
return new WP_Query( $args );
522537
}

readme.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
Contributors: bradparbs, coreymcollins, jtsternberg, webdevstudios, pluginize, binarygary
44
Donate link: http://webdevstudios.com/
55
Tags: video, youtube, vimeo, featured image
6-
Requires at least: 3.7
7-
Tested up to: 5.6.0
8-
Stable tag: 1.1.2
6+
Requires at least: 5.0
7+
Tested up to: 5.7.2
8+
Stable tag: 1.2.0
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
11+
Requires PHP: 5.6
1112

12-
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.
13+
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.
1314

1415
== Description ==
1516

16-
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.
17+
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.
1718

1819
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.
1920

@@ -39,6 +40,16 @@ In addition, after setting the video thumbnail as the featured image, an “is_v
3940

4041
== Changelog ==
4142

43+
= 1.2.0 =
44+
* Added: Support for potentially larger Vimeo images from API response.
45+
* Fixed: Various PHP notices and errors.
46+
* Updated: Minimum PHP version.
47+
* Updated: bumped up default string length to 4000 characters, for URL searching in content.
48+
* Updated: exclude user profile URLs from Youtube regex.
49+
* Updated: Switched all endpoints to make sure we're using HTTPS.
50+
* Updated: Vimeo endpoint switched to JSON responses.
51+
* Updated: Plugin description.
52+
4253
= 1.1.2 =
4354
* Fixed: Issues with Youtube HEAD request returning 40x errors.
4455

0 commit comments

Comments
 (0)