Video Thumbnails - Version 1.8.2

Version Description

  • Fixes issue where some servers were unable to download thumbnails from YouTube
  • Fixes possible issue setting new thumbnail as featured image
Download this release

Release Info

Developer sutherlandboswell
Plugin Icon 128x128 Video Thumbnails
Version 1.8.2
Comparing to
See all releases

Code changes from version 1.8.1 to 1.8.2

Files changed (2) hide show
  1. readme.txt +6 -2
  2. video-thumbnails.php +38 -50
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: sutherlandboswell
3
  Donate link: http://wie.ly/u/donate
4
  Tags: Video, Thumbnails, YouTube, Vimeo, Blip.tv, Justin.tv, Dailymotion, Metacafe, Image, Featured Image, Post Thumbnail
5
  Requires at least: 3.0
6
- Tested up to: 3.3.1
7
- Stable tag: 1.8.1
8
 
9
  Video Thumbnails simplifies the process of automatically displaying video thumbnails in your WordPress template.
10
 
@@ -88,6 +88,10 @@ The Vimeo API has a rate limit, so in rare cases you may exceed this limit. Try
88
 
89
  == Changelog ==
90
 
 
 
 
 
91
  = 1.8.1 =
92
  * Plugin now scans posts added using XML-RPC which makes posting videos from iOS or other apps work smoothly
93
 
3
  Donate link: http://wie.ly/u/donate
4
  Tags: Video, Thumbnails, YouTube, Vimeo, Blip.tv, Justin.tv, Dailymotion, Metacafe, Image, Featured Image, Post Thumbnail
5
  Requires at least: 3.0
6
+ Tested up to: 3.4.2
7
+ Stable tag: 1.8.2
8
 
9
  Video Thumbnails simplifies the process of automatically displaying video thumbnails in your WordPress template.
10
 
88
 
89
  == Changelog ==
90
 
91
+ = 1.8.2 =
92
+ * Fixes issue where some servers were unable to download thumbnails from YouTube
93
+ * Fixes possible issue setting new thumbnail as featured image
94
+
95
  = 1.8.1 =
96
  * Plugin now scans posts added using XML-RPC which makes posting videos from iOS or other apps work smoothly
97
 
video-thumbnails.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://sutherlandboswell.com/projects/wordpress-video-thumbnails/
5
  Description: Automatically retrieve video thumbnails for your posts and display them in your theme. Currently supports YouTube, Vimeo, Blip.tv, Justin.tv, Dailymotion and Metacafe.
6
  Author: Sutherland Boswell
7
  Author URI: http://sutherlandboswell.com
8
- Version: 1.8.1
9
  License: GPL2
10
  */
11
  /* Copyright 2010 Sutherland Boswell (email : sutherland.boswell@gmail.com)
@@ -96,9 +96,9 @@ function getMetacafeThumbnail( $id ) {
96
  if ( $xml == false ) {
97
  return new WP_Error( 'metacafe_info_retrieval', __( 'Error retrieving video information from the URL <a href="' . $videoinfo_url . '">' . $videoinfo_url . '</a>.<br /><a href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">Libcurl error</a> ' . curl_errno( $ch ) . ': <code>' . curl_error( $ch ) . '</code>. If opening that URL in your web browser returns anything else than an error page, the problem may be related to your web server and might be something your host administrator can solve.' ) );
98
  } else {
99
- $result = $xml->xpath( "/rss/channel/item/media:thumbnail/@url" );
100
- $thumbnail = (string) $result[0]['url'];
101
- return $thumbnail;
102
  }
103
  };
104
 
@@ -215,7 +215,7 @@ function get_video_thumbnail( $post_id = null ) {
215
  if ( isset( $matches[1] ) ) {
216
  $vimeo_thumbnail = getVimeoInfo( $matches[1], $info = 'thumbnail_large' );
217
  if ( is_wp_error( $vimeo_thumbnail ) ) {
218
- return $vimeo_thumbnail;
219
  } else if ( isset( $vimeo_thumbnail ) ) {
220
  $new_thumbnail = $vimeo_thumbnail;
221
  }
@@ -232,7 +232,7 @@ function get_video_thumbnail( $post_id = null ) {
232
  if ( isset( $matches[1] ) ) {
233
  $blip_thumbnail = getBliptvInfo( $matches[1] );
234
  if ( is_wp_error( $blip_thumbnail ) ) {
235
- return $blip_thumbnail;
236
  } else if ( isset( $blip_thumbnail ) ) {
237
  $new_thumbnail = $blip_thumbnail;
238
  }
@@ -272,7 +272,7 @@ function get_video_thumbnail( $post_id = null ) {
272
  if ( isset( $matches[1] ) ) {
273
  $dailymotion_thumbnail = getDailyMotionThumbnail( $matches[1] );
274
  if ( is_wp_error( $dailymotion_thumbnail ) ) {
275
- return $dailymotion_thumbnail;
276
  } else if ( isset( $dailymotion_thumbnail ) ) {
277
  $new_thumbnail = strtok( $dailymotion_thumbnail, '?' );
278
  }
@@ -288,8 +288,8 @@ function get_video_thumbnail( $post_id = null ) {
288
  // Now if we've found a Metacafe video ID, let's set the thumbnail URL
289
  if ( isset( $matches[1] ) ) {
290
  $metacafe_thumbnail = getMetacafeThumbnail( $matches[1] );
291
- if ( is_wp_error( $metacafe_thumbnail ) ) {
292
- return $metacafe_thumbnail;
293
  } else if ( isset( $metacafe_thumbnail ) ) {
294
  $new_thumbnail = strtok( $metacafe_thumbnail, '?' );
295
  }
@@ -301,48 +301,35 @@ function get_video_thumbnail( $post_id = null ) {
301
 
302
  // Save as Attachment if enabled
303
  if ( get_option( 'video_thumbnails_save_media' ) == 1 ) {
304
- $error = '';
305
- $ch = curl_init();
306
- $timeout = 0;
307
- curl_setopt ( $ch, CURLOPT_URL, $new_thumbnail );
308
- curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
309
- curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
310
- curl_setopt( $ch, CURLOPT_FAILONERROR, true ); // Return an error for curl_error() processing if HTTP response code >= 400
311
- $image_contents = curl_exec( $ch );
312
- if ( curl_error( $ch ) != null || $image_contents == null ) {
313
- $curl_error = '';
314
- if ( curl_error( $ch ) != null ) {
315
- $curl_error = '<br /><a href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">Libcurl error</a> ' . curl_errno( $ch ) . ': <code>' . curl_error( $ch ) . '</code>';
316
- }
317
- $error = new WP_Error( 'thumbnail_retrieval', __( 'Error retrieving a thumbnail from the URL <a href="' . $new_thumbnail . '">' . $new_thumbnail . '</a>' . $curl_error . '. If opening that URL in your web browser shows an image, the problem may be related to your web server and might be something your server administrator can solve.' ) );
318
- }
319
- curl_close( $ch );
320
 
321
- if ( $error != null ) {
322
- return $error;
323
- } else {
 
 
 
324
 
325
- $upload = wp_upload_bits( basename( $new_thumbnail ), null, $image_contents );
326
 
327
- $new_thumbnail = $upload['url'];
328
 
329
- $filename = $upload['file'];
330
 
331
- $wp_filetype = wp_check_filetype( basename( $filename ), null );
332
- $attachment = array(
333
- 'post_mime_type' => $wp_filetype['type'],
334
- 'post_title' => get_the_title($post_id),
335
- 'post_content' => '',
336
- 'post_status' => 'inherit'
337
- );
338
- $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
339
- // you must first include the image.php file
340
- // for the function wp_generate_attachment_metadata() to work
341
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
342
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
343
- wp_update_attachment_metadata( $attach_id, $attach_data );
344
 
345
- }
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
  }
348
 
@@ -350,8 +337,8 @@ function get_video_thumbnail( $post_id = null ) {
350
  if ( !update_post_meta( $post_id, '_video_thumbnail', $new_thumbnail ) ) add_post_meta( $post_id, '_video_thumbnail', $new_thumbnail, true );
351
 
352
  // Set attachment as featured image if enabled
353
- if ( get_option( 'video_thumbnails_set_featured' ) == 1 && get_option( 'video_thumbnails_save_media' ) == 1 && get_post_meta( $post_id, '_thumbnail_id', true ) == '' ) {
354
- if ( !update_post_meta( $post_id, '_thumbnail_id', $attach_id ) ) add_post_meta( $post_id, '_thumbnail_id', $attach_id, true );
355
  }
356
  }
357
  return $new_thumbnail;
@@ -444,7 +431,7 @@ function video_thumbnails_callback() {
444
 
445
  if ( is_wp_error( $video_thumbnail ) ) {
446
  echo $video_thumbnail->get_error_message();
447
- } else if ( $video_thumbnail != null ) {
448
  echo '<img src="' . $video_thumbnail . '" style="max-width:100%;" />';
449
  } else {
450
  echo 'We didn\'t find a video thumbnail for this post. (be sure you have saved changes first)';
@@ -459,6 +446,7 @@ add_action( 'new_to_publish', 'save_video_thumbnail', 10, 1 );
459
  add_action( 'draft_to_publish', 'save_video_thumbnail', 10, 1 );
460
  add_action( 'pending_to_publish', 'save_video_thumbnail', 10, 1 );
461
  add_action( 'future_to_publish', 'save_video_thumbnail', 10, 1 );
 
462
 
463
  // Finds thumbnail when posting from XML-RPC
464
  // (this action passes the post ID as an argument so 'get_video_thumbnail' is used instead)
@@ -593,7 +581,7 @@ function video_thumbnails_past_callback() {
593
 
594
  if ( is_wp_error( $video_thumbnail ) ) {
595
  echo $video_thumbnail->get_error_message();
596
- } else if ( $video_thumbnail != null ) {
597
  echo '<span style="color:green">&#10004;</span> Success!';
598
  } else {
599
  echo '<span style="color:red">&#10006;</span> Couldn\'t find a video thumbnail for this post.';
@@ -618,7 +606,7 @@ function video_thumbnails_checkbox_option( $option_name, $option_description ) {
618
 
619
  function video_thumbnail_options() {
620
 
621
- if ( ! current_user_can( 'manage_options' ) ) {
622
  wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
623
  }
624
 
5
  Description: Automatically retrieve video thumbnails for your posts and display them in your theme. Currently supports YouTube, Vimeo, Blip.tv, Justin.tv, Dailymotion and Metacafe.
6
  Author: Sutherland Boswell
7
  Author URI: http://sutherlandboswell.com
8
+ Version: 1.8.2
9
  License: GPL2
10
  */
11
  /* Copyright 2010 Sutherland Boswell (email : sutherland.boswell@gmail.com)
96
  if ( $xml == false ) {
97
  return new WP_Error( 'metacafe_info_retrieval', __( 'Error retrieving video information from the URL <a href="' . $videoinfo_url . '">' . $videoinfo_url . '</a>.<br /><a href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">Libcurl error</a> ' . curl_errno( $ch ) . ': <code>' . curl_error( $ch ) . '</code>. If opening that URL in your web browser returns anything else than an error page, the problem may be related to your web server and might be something your host administrator can solve.' ) );
98
  } else {
99
+ $result = $xml->xpath( "/rss/channel/item/media:thumbnail/@url" );
100
+ $thumbnail = (string) $result[0]['url'];
101
+ return $thumbnail;
102
  }
103
  };
104
 
215
  if ( isset( $matches[1] ) ) {
216
  $vimeo_thumbnail = getVimeoInfo( $matches[1], $info = 'thumbnail_large' );
217
  if ( is_wp_error( $vimeo_thumbnail ) ) {
218
+ return $vimeo_thumbnail;
219
  } else if ( isset( $vimeo_thumbnail ) ) {
220
  $new_thumbnail = $vimeo_thumbnail;
221
  }
232
  if ( isset( $matches[1] ) ) {
233
  $blip_thumbnail = getBliptvInfo( $matches[1] );
234
  if ( is_wp_error( $blip_thumbnail ) ) {
235
+ return $blip_thumbnail;
236
  } else if ( isset( $blip_thumbnail ) ) {
237
  $new_thumbnail = $blip_thumbnail;
238
  }
272
  if ( isset( $matches[1] ) ) {
273
  $dailymotion_thumbnail = getDailyMotionThumbnail( $matches[1] );
274
  if ( is_wp_error( $dailymotion_thumbnail ) ) {
275
+ return $dailymotion_thumbnail;
276
  } else if ( isset( $dailymotion_thumbnail ) ) {
277
  $new_thumbnail = strtok( $dailymotion_thumbnail, '?' );
278
  }
288
  // Now if we've found a Metacafe video ID, let's set the thumbnail URL
289
  if ( isset( $matches[1] ) ) {
290
  $metacafe_thumbnail = getMetacafeThumbnail( $matches[1] );
291
+ if ( is_wp_error( $metacafe_thumbnail ) ) {
292
+ return $metacafe_thumbnail;
293
  } else if ( isset( $metacafe_thumbnail ) ) {
294
  $new_thumbnail = strtok( $metacafe_thumbnail, '?' );
295
  }
301
 
302
  // Save as Attachment if enabled
303
  if ( get_option( 'video_thumbnails_save_media' ) == 1 ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304
 
305
+ $response = wp_remote_get( $new_thumbnail, array( 'sslverify' => false ) );
306
+
307
+ // Check for error
308
+ if( is_wp_error( $response ) ) {
309
+ return new WP_Error( 'thumbnail_retrieval', __( 'Error retrieving a thumbnail from the URL <a href="' . $new_thumbnail . '">' . $new_thumbnail . '</a> If opening that URL in your web browser shows an image, the problem may be related to your web server and might be something your server administrator can solve. Error details: "' . $response->get_error_message() . '"' ) );
310
+ }
311
 
312
+ $image_contents = wp_remote_retrieve_body( $response );
313
 
314
+ $upload = wp_upload_bits( basename( $new_thumbnail ), null, $image_contents );
315
 
316
+ $new_thumbnail = $upload['url'];
317
 
318
+ $filename = $upload['file'];
 
 
 
 
 
 
 
 
 
 
 
 
319
 
320
+ $wp_filetype = wp_check_filetype( basename( $filename ), null );
321
+ $attachment = array(
322
+ 'post_mime_type' => $wp_filetype['type'],
323
+ 'post_title' => get_the_title($post_id),
324
+ 'post_content' => '',
325
+ 'post_status' => 'inherit'
326
+ );
327
+ $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
328
+ // you must first include the image.php file
329
+ // for the function wp_generate_attachment_metadata() to work
330
+ require_once( ABSPATH . 'wp-admin/includes/image.php' );
331
+ $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
332
+ wp_update_attachment_metadata( $attach_id, $attach_data );
333
 
334
  }
335
 
337
  if ( !update_post_meta( $post_id, '_video_thumbnail', $new_thumbnail ) ) add_post_meta( $post_id, '_video_thumbnail', $new_thumbnail, true );
338
 
339
  // Set attachment as featured image if enabled
340
+ if ( get_option( 'video_thumbnails_set_featured' ) == 1 && get_option( 'video_thumbnails_save_media' ) == 1 && !has_post_thumbnail( $post_id ) ) {
341
+ set_post_thumbnail( $post_id, $attach_id );
342
  }
343
  }
344
  return $new_thumbnail;
431
 
432
  if ( is_wp_error( $video_thumbnail ) ) {
433
  echo $video_thumbnail->get_error_message();
434
+ } else if ( $video_thumbnail != null ) {
435
  echo '<img src="' . $video_thumbnail . '" style="max-width:100%;" />';
436
  } else {
437
  echo 'We didn\'t find a video thumbnail for this post. (be sure you have saved changes first)';
446
  add_action( 'draft_to_publish', 'save_video_thumbnail', 10, 1 );
447
  add_action( 'pending_to_publish', 'save_video_thumbnail', 10, 1 );
448
  add_action( 'future_to_publish', 'save_video_thumbnail', 10, 1 );
449
+ add_action( 'private_to_publish', 'save_video_thumbnail', 10, 1 );
450
 
451
  // Finds thumbnail when posting from XML-RPC
452
  // (this action passes the post ID as an argument so 'get_video_thumbnail' is used instead)
581
 
582
  if ( is_wp_error( $video_thumbnail ) ) {
583
  echo $video_thumbnail->get_error_message();
584
+ } else if ( $video_thumbnail != null ) {
585
  echo '<span style="color:green">&#10004;</span> Success!';
586
  } else {
587
  echo '<span style="color:red">&#10006;</span> Couldn\'t find a video thumbnail for this post.';
606
 
607
  function video_thumbnail_options() {
608
 
609
+ if ( ! current_user_can( 'manage_options' ) ) {
610
  wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
611
  }
612