Video Thumbnails - Version 1.7.7

Version Description

  • Better cURL error handling
  • Better regex matching
  • Bug fixes
  • Thanks to Daedalon for many of these changes
Download this release

Release Info

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

Code changes from version 1.7.6 to 1.7.7

Files changed (2) hide show
  1. readme.txt +8 -2
  2. video-thumbnails.php +166 -103
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: sutherlandboswell
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=sutherland%2eboswell%40gmail%2ecom&lc=US&item_name=Sutherland%20Boswell&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted
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.1.2
7
- Stable tag: 1.7.6
8
 
9
  Video Thumbnails simplifies the process of automatically displaying video thumbnails in your WordPress template.
10
 
@@ -88,6 +88,12 @@ The Vimeo API has a rate limit, so in rare cases you may exceed this limit. Try
88
 
89
  == Changelog ==
90
 
 
 
 
 
 
 
91
  = 1.7.6 =
92
  * Fixed plugin link
93
  * Added donate button
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=sutherland%2eboswell%40gmail%2ecom&lc=US&item_name=Sutherland%20Boswell&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted
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
7
+ Stable tag: 1.7.7
8
 
9
  Video Thumbnails simplifies the process of automatically displaying video thumbnails in your WordPress template.
10
 
88
 
89
  == Changelog ==
90
 
91
+ = 1.7.7 =
92
+ * Better cURL error handling
93
+ * Better regex matching
94
+ * Bug fixes
95
+ * Thanks to [Daedalon](http://wordpress.org/support/profile/daedalon) for many of these changes
96
+
97
  = 1.7.6 =
98
  * Fixed plugin link
99
  * Added donate button
video-thumbnails.php CHANGED
@@ -2,51 +2,61 @@
2
  /*
3
  Plugin Name: Video Thumbnails
4
  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, and Justin.tv.
6
  Author: Sutherland Boswell
7
  Author URI: http://sutherlandboswell.com
8
- Version: 1.7.6
9
  License: GPL2
10
  */
11
  /* Copyright 2010 Sutherland Boswell (email : sutherland.boswell@gmail.com)
12
 
13
- This program is free software; you can redistribute it and/or modify
14
- it under the terms of the GNU General Public License, version 2, as
15
- published by the Free Software Foundation.
16
 
17
- This program is distributed in the hope that it will be useful,
18
- but WITHOUT ANY WARRANTY; without even the implied warranty of
19
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
- GNU General Public License for more details.
21
 
22
- You should have received a copy of the GNU General Public License
23
- along with this program; if not, write to the Free Software
24
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
27
  // Get Vimeo Thumbnail
28
  function getVimeoInfo($id, $info = 'thumbnail_large') {
29
- if (!function_exists('curl_init')) {
30
- return null;
31
- } else {
32
  $ch = curl_init();
33
- curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php");
 
34
  curl_setopt($ch, CURLOPT_HEADER, 0);
35
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
36
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 
37
  $output = unserialize(curl_exec($ch));
38
  $output = $output[0][$info];
 
 
 
39
  curl_close($ch);
40
  return $output;
41
- }
42
  };
43
 
44
  // Blip.tv Functions
45
  function getBliptvInfo($id) {
46
- $xml = simplexml_load_file("http://blip.tv/players/episode/$id?skin=rss");
47
- $result = $xml->xpath("/rss/channel/item/media:thumbnail/@url");
48
- $thumbnail = (string) $result[0]['url'];
49
- return $thumbnail;
 
 
 
 
 
50
  }
51
 
52
  // Justin.tv Functions
@@ -57,39 +67,51 @@ function getJustintvInfo($id) {
57
 
58
  // Get DailyMotion Thumbnail
59
  function getDailyMotionThumbnail($id) {
60
- if (!function_exists('curl_init')) {
61
- return null;
62
- } else {
63
  $ch = curl_init();
64
- curl_setopt($ch, CURLOPT_URL, "https://api.dailymotion.com/video/$id?fields=thumbnail_url");
 
65
  curl_setopt($ch, CURLOPT_HEADER, 0);
66
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
67
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
68
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 
69
  $output = curl_exec($ch);
70
- curl_close($ch);
71
  $output = json_decode($output);
72
  $output = $output->thumbnail_url;
 
 
 
 
73
  return $output;
74
- }
75
  };
76
 
77
  // Metacafe
78
  function getMetacafeThumbnail($id) {
79
- $xml = simplexml_load_file("http://www.metacafe.com/api/item/$id/");
80
- $result = $xml->xpath("/rss/channel/item/media:thumbnail/@url");
81
- $thumbnail = (string) $result[0]['url'];
82
- return $thumbnail;
 
 
 
 
 
83
  };
84
 
 
85
  // The Main Event
 
86
  function get_video_thumbnail($post_id=null) {
87
 
88
  // Get the post ID if none is provided
89
  if($post_id==null OR $post_id=='') $post_id = get_the_ID();
90
 
91
- // Check to see if thumbnail has already been found
92
- if( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) {
93
  return $thumbnail_meta;
94
  }
95
  // If the thumbnail isn't stored in custom meta, fetch a thumbnail
@@ -104,26 +126,31 @@ function get_video_thumbnail($post_id=null) {
104
  // Simple Video Embedder Compatibility
105
  if(function_exists('p75HasVideo')) {
106
  if ( p75HasVideo($post_id) ) {
107
- $markup = p75GetVideo($post_id);
108
  }
109
  }
110
 
111
- // Checks for a standard YouTube embed
112
- preg_match('#<object[^>]+>.+?http://www.youtube.com/[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
113
 
114
  // More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
115
  if(!isset($matches[1])) {
116
- preg_match('#http://www.youtube.com/[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
117
  }
118
 
119
- // Checks for YouTube iframe
120
  if(!isset($matches[1])) {
121
- preg_match('#http://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s', $markup, $matches);
122
  }
123
 
124
- // Checks for any YouTube URL
 
 
 
 
 
125
  if(!isset($matches[1])) {
126
- preg_match('#http://w?w?w?.?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);
127
  }
128
 
129
  // Checks for YouTube Lyte
@@ -164,26 +191,28 @@ function get_video_thumbnail($post_id=null) {
164
 
165
  // If we still haven't found anything, check for Vimeo embedded with JR_embed
166
  if(!isset($matches[1])) {
167
- preg_match('#\[vimeo id=([A-Za-z0-9\-_]+)]#s', $markup, $matches);
168
- }
169
 
170
  // If we still haven't found anything, check for Vimeo URL
171
  if(!isset($matches[1])) {
172
- preg_match('#http://w?w?w?.?vimeo.com/([A-Za-z0-9\-_]+)#s', $markup, $matches);
173
- }
174
 
175
  // If we still haven't found anything, check for Vimeo shortcode
176
  if(!isset($matches[1])) {
177
- preg_match('#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches);
178
- }
179
  if(!isset($matches[1])) {
180
- preg_match('#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches);
181
- }
182
 
183
  // Now if we've found a Vimeo ID, let's set the thumbnail URL
184
  if(isset($matches[1])) {
185
  $vimeo_thumbnail = getVimeoInfo($matches[1], $info = 'thumbnail_large');
186
- if(isset($vimeo_thumbnail)) {
 
 
187
  $new_thumbnail = $vimeo_thumbnail;
188
  }
189
  }
@@ -192,13 +221,17 @@ function get_video_thumbnail($post_id=null) {
192
  // Blip.tv
193
  if($new_thumbnail==null) {
194
 
195
- // Blip.tv file URL
196
  preg_match('#http://blip.tv/play/([A-Za-z0-9]+)#s', $markup, $matches);
197
 
198
- // Now if we've found a Blip.tv file URL, let's set the thumbnail URL
199
  if(isset($matches[1])) {
200
  $blip_thumbnail = getBliptvInfo($matches[1]);
201
- $new_thumbnail = $blip_thumbnail;
 
 
 
 
202
  }
203
  }
204
 
@@ -223,18 +256,22 @@ function get_video_thumbnail($post_id=null) {
223
 
224
  // Dailymotion url
225
  if(!isset($matches[1])) {
226
- preg_match('#http://www.dailymotion.com/video/([A-Za-z0-9]+)#s', $markup, $matches);
227
  }
228
 
229
  // Dailymotion iframe
230
  if(!isset($matches[1])) {
231
- preg_match('#http://www.dailymotion.com/embed/video/([A-Za-z0-9]+)#s', $markup, $matches);
232
  }
233
 
234
  // Now if we've found a Dailymotion video ID, let's set the thumbnail URL
235
  if(isset($matches[1])) {
236
  $dailymotion_thumbnail = getDailyMotionThumbnail($matches[1]);
 
 
 
237
  $new_thumbnail = strtok($dailymotion_thumbnail, '?');
 
238
  }
239
  }
240
 
@@ -247,7 +284,11 @@ function get_video_thumbnail($post_id=null) {
247
  // Now if we've found a Metacafe video ID, let's set the thumbnail URL
248
  if(isset($matches[1])) {
249
  $metacafe_thumbnail = getMetacafeThumbnail($matches[1]);
250
- $new_thumbnail = strtok($metacafe_thumbnail, '?');
 
 
 
 
251
  }
252
  }
253
 
@@ -256,35 +297,49 @@ function get_video_thumbnail($post_id=null) {
256
 
257
  // Save as Attachment if enabled
258
  if(get_option('video_thumbnails_save_media')==1) {
259
-
260
  $ch = curl_init();
261
  $timeout = 0;
262
  curl_setopt ($ch, CURLOPT_URL, $new_thumbnail);
263
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
264
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
265
- $image_contents = curl_exec($ch);
266
- curl_close($ch);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
- $upload = wp_upload_bits(basename($new_thumbnail), null, $image_contents);
269
-
270
- $new_thumbnail = $upload['url'];
271
-
272
- $filename = $upload['file'];
273
-
274
- $wp_filetype = wp_check_filetype(basename($filename), null );
275
- $attachment = array(
276
- 'post_mime_type' => $wp_filetype['type'],
277
- 'post_title' => get_the_title($post_id),
278
- 'post_content' => '',
279
- 'post_status' => 'inherit'
280
- );
281
- $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
282
- // you must first include the image.php file
283
- // for the function wp_generate_attachment_metadata() to work
284
- require_once(ABSPATH . "wp-admin" . '/includes/image.php');
285
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
286
- wp_update_attachment_metadata( $attach_id, $attach_data );
287
-
288
  }
289
 
290
  // Add hidden custom field with thumbnail URL
@@ -309,7 +364,7 @@ function video_thumbnail($post_id=null) {
309
  // Create Meta Fields on Edit Page
310
 
311
  add_action("admin_init", "video_thumbnail_admin_init");
312
-
313
  function video_thumbnail_admin_init(){
314
  $video_thumbnails_post_types = get_option('video_thumbnails_post_types');
315
  if(is_array($video_thumbnails_post_types)) {
@@ -328,7 +383,7 @@ function video_thumbnail_admin(){
328
  echo '<p id="video-thumbnails-preview"><img src="' . $video_thumbnail . '" width="100%" /></p>';
329
  }
330
 
331
- if ( get_post_status() == 'publish') {
332
  if(isset($video_thumbnail) && $video_thumbnail!='') {
333
  echo '<p><a href="#" id="video-thumbnails-reset" onclick="video_thumbnails_reset(\'' . $post->ID . '\');return false;">Reset Video Thumbnail</a></p>';
334
  } else {
@@ -382,11 +437,15 @@ function video_thumbnails_callback() {
382
 
383
  delete_post_meta($post_id, '_video_thumbnail');
384
 
385
- if ( ($video_thumbnail=get_video_thumbnail($post_id)) != null ) {
386
- echo '<img src="' . $video_thumbnail . '" width="100%" />';
387
- } else {
388
- echo 'We didn\'t find a video thumbnail for this post. (be sure you have saved changes first)';
389
- }
 
 
 
 
390
 
391
  die();
392
  }
@@ -475,7 +534,7 @@ $posts = get_posts(array('showposts'=>-1,'post_type'=>$video_thumbnails_post_typ
475
 
476
  if ($posts) {
477
  foreach($posts as $post) {
478
- $post_ids[] = $post->ID;
479
  }
480
  $ids = implode(',',$post_ids);
481
  }
@@ -518,7 +577,11 @@ function video_thumbnails_past_callback() {
518
 
519
  echo get_the_title($post_id) . ' - ';
520
 
521
- if ( ($video_thumbnail=get_video_thumbnail($post_id)) != null ) {
 
 
 
 
522
  echo '<span style="color:green">&#10004;</span> Success!';
523
  } else {
524
  echo '<span style="color:red">&#10006;</span> Couldn\'t find a video thumbnail for this post.';
@@ -527,28 +590,28 @@ function video_thumbnails_past_callback() {
527
  die();
528
  }
529
 
530
- // Aministration
531
 
532
  add_action('admin_menu', 'video_thumbnails_menu');
533
 
534
  function video_thumbnails_menu() {
535
 
536
- add_options_page('Video Thumbnail Options', 'Video Thumbnails', 'manage_options', 'video-thumbnail-options', 'video_thumbnail_options');
537
 
538
  }
539
 
540
- function video_thumbnail_options() {
541
-
542
- if (!current_user_can('manage_options')) {
543
- wp_die( __('You do not have sufficient permissions to access this page.') );
544
- }
545
-
546
  function video_thumbnails_checkbox_option($option_name, $option_description) { ?>
547
  <fieldset><legend class="screen-reader-text"><span><?php echo $option_description; ?></span></legend>
548
  <label for="<?php echo $option_name; ?>"><input name="<?php echo $option_name; ?>" type="checkbox" id="<?php echo $option_name; ?>" value="1" <?php if(get_option($option_name)==1) echo "checked='checked'"; ?>/> <?php echo $option_description; ?></label>
549
  </fieldset> <?php
550
  }
551
 
 
 
 
 
 
 
552
  ?>
553
 
554
  <div class="wrap">
@@ -576,13 +639,13 @@ function video_thumbnails_checkbox_option($option_name, $option_description) { ?
576
  <th scope="row">Post Types</th>
577
  <td>
578
  <?php $video_thumbnails_post_types = get_option('video_thumbnails_post_types'); ?>
579
- <?php foreach(get_post_types() as $type): if($type == 'attachment' OR $type == 'revision' OR $type == 'nav_menu_item') continue; ?>
580
- <label for="video_thumbnails_post_types_<?php echo $type; ?>">
581
- <input id="video_thumbnails_post_types_<?php echo $type; ?>" name="video_thumbnails_post_types[]" type="checkbox" value="<?php echo $type; ?>" <?php if(is_array($video_thumbnails_post_types)) checked(in_array($type, $video_thumbnails_post_types)); ?> />
582
- <?php echo $type; ?>
583
- </label>
584
- <br />
585
- <?php endforeach; ?>
586
  </td>
587
  </tr>
588
 
2
  /*
3
  Plugin Name: Video Thumbnails
4
  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.7.7
9
  License: GPL2
10
  */
11
  /* Copyright 2010 Sutherland Boswell (email : sutherland.boswell@gmail.com)
12
 
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License, version 2, as
15
+ published by the Free Software Foundation.
16
 
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
 
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program; if not, write to the Free Software
24
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
27
  // Get Vimeo Thumbnail
28
  function getVimeoInfo($id, $info = 'thumbnail_large') {
29
+ if (!function_exists('curl_init')) {
30
+ return null;
31
+ } else {
32
  $ch = curl_init();
33
+ $videoinfo_url = "http://vimeo.com/api/v2/video/$id.php";
34
+ curl_setopt($ch, CURLOPT_URL, $videoinfo_url);
35
  curl_setopt($ch, CURLOPT_HEADER, 0);
36
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
37
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
38
+ curl_setopt($ch, CURLOPT_FAILONERROR, true); // Return an error for curl_error() processing if HTTP response code >= 400
39
  $output = unserialize(curl_exec($ch));
40
  $output = $output[0][$info];
41
+ if (curl_error($ch) != null) {
42
+ $output = new WP_Error('vimeo_info_retrieval', __("Error retrieving video information from the URL <a href=\"" . $videoinfo_url . "\">" . $videoinfo_url . "</a>: <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."));
43
+ }
44
  curl_close($ch);
45
  return $output;
46
+ }
47
  };
48
 
49
  // Blip.tv Functions
50
  function getBliptvInfo($id) {
51
+ $videoinfo_url = "http://blip.tv/players/episode/$id?skin=rss";
52
+ $xml = simplexml_load_file($videoinfo_url);
53
+ if ($xml == false) {
54
+ return new WP_Error('dailymotion_info_retrieval', __("Error retrieving video information from the URL <a href=\"" . $videoinfo_url . "\">" . $videoinfo_url . "</a>. 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."));
55
+ } else {
56
+ $result = $xml->xpath("/rss/channel/item/media:thumbnail/@url");
57
+ $thumbnail = (string) $result[0]['url'];
58
+ return $thumbnail;
59
+ }
60
  }
61
 
62
  // Justin.tv Functions
67
 
68
  // Get DailyMotion Thumbnail
69
  function getDailyMotionThumbnail($id) {
70
+ if (!function_exists('curl_init')) {
71
+ return null;
72
+ } else {
73
  $ch = curl_init();
74
+ $videoinfo_url = "https://api.dailymotion.com/video/$id?fields=thumbnail_url";
75
+ curl_setopt($ch, CURLOPT_URL, $videoinfo_url);
76
  curl_setopt($ch, CURLOPT_HEADER, 0);
77
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
79
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
80
+ curl_setopt($ch, CURLOPT_FAILONERROR, true); // Return an error for curl_error() processing if HTTP response code >= 400
81
  $output = curl_exec($ch);
 
82
  $output = json_decode($output);
83
  $output = $output->thumbnail_url;
84
+ if (curl_error($ch) != null) {
85
+ $output = new WP_Error('dailymotion_info_retrieval', __("Error retrieving video information from the URL <a href=\"" . $videoinfo_url . "\">" . $videoinfo_url . "</a>: <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."));
86
+ }
87
+ curl_close($ch); // Moved here to allow curl_error() operation above. Was previously below curl_exec() call.
88
  return $output;
89
+ }
90
  };
91
 
92
  // Metacafe
93
  function getMetacafeThumbnail($id) {
94
+ $videoinfo_url = "http://www.metacafe.com/api/item/$id/";
95
+ $xml = simplexml_load_file($videoinfo_url);
96
+ if ($xml == false) {
97
+ return new WP_Error('dailymotion_info_retrieval', __("Error retrieving video information from the URL <a href=\"" . $videoinfo_url . "\">" . $videoinfo_url . "</a>. 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
 
105
+ //
106
  // The Main Event
107
+ //
108
  function get_video_thumbnail($post_id=null) {
109
 
110
  // Get the post ID if none is provided
111
  if($post_id==null OR $post_id=='') $post_id = get_the_ID();
112
 
113
+ // Check to see if thumbnail has already been found and still exists as a file
114
+ if( ( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) && wp_remote_retrieve_response_code(wp_remote_head($thumbnail_meta)) === '200' ) {
115
  return $thumbnail_meta;
116
  }
117
  // If the thumbnail isn't stored in custom meta, fetch a thumbnail
126
  // Simple Video Embedder Compatibility
127
  if(function_exists('p75HasVideo')) {
128
  if ( p75HasVideo($post_id) ) {
129
+ $markup = p75GetVideo($post_id);
130
  }
131
  }
132
 
133
+ // Checks for the old standard YouTube embed
134
+ preg_match('#<object[^>]+>.+?https?://www.youtube.com/[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
135
 
136
  // More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
137
  if(!isset($matches[1])) {
138
+ preg_match('#https?://www.youtube.com/[ve]/([A-Za-z0-9\-_]+)#s', $markup, $matches);
139
  }
140
 
141
+ // Checks for YouTube iframe, the new standard since at least 2011
142
  if(!isset($matches[1])) {
143
+ preg_match('#https?://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s', $markup, $matches);
144
  }
145
 
146
+ // Checks for any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
147
+ if(!isset($matches[1])) {
148
+ preg_match('#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches);
149
+ }
150
+
151
+ // Checks for any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
152
  if(!isset($matches[1])) {
153
+ preg_match('#(?:https?(?:a|vh?)?://)?youtu.be/([A-Za-z0-9\-_]+)#s', $markup, $matches);
154
  }
155
 
156
  // Checks for YouTube Lyte
191
 
192
  // If we still haven't found anything, check for Vimeo embedded with JR_embed
193
  if(!isset($matches[1])) {
194
+ preg_match('#\[vimeo id=([A-Za-z0-9\-_]+)]#s', $markup, $matches);
195
+ }
196
 
197
  // If we still haven't found anything, check for Vimeo URL
198
  if(!isset($matches[1])) {
199
+ preg_match('#(?:http://)?(?:www\.)?vimeo.com/([A-Za-z0-9\-_]+)#s', $markup, $matches);
200
+ }
201
 
202
  // If we still haven't found anything, check for Vimeo shortcode
203
  if(!isset($matches[1])) {
204
+ preg_match('#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches);
205
+ }
206
  if(!isset($matches[1])) {
207
+ preg_match('#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches);
208
+ }
209
 
210
  // Now if we've found a Vimeo ID, let's set the thumbnail URL
211
  if(isset($matches[1])) {
212
  $vimeo_thumbnail = getVimeoInfo($matches[1], $info = 'thumbnail_large');
213
+ if (is_wp_error($vimeo_thumbnail)) {
214
+ return $vimeo_thumbnail;
215
+ } else if (isset($vimeo_thumbnail)) {
216
  $new_thumbnail = $vimeo_thumbnail;
217
  }
218
  }
221
  // Blip.tv
222
  if($new_thumbnail==null) {
223
 
224
+ // Blip.tv embed URL
225
  preg_match('#http://blip.tv/play/([A-Za-z0-9]+)#s', $markup, $matches);
226
 
227
+ // Now if we've found a Blip.tv embed URL, let's set the thumbnail URL
228
  if(isset($matches[1])) {
229
  $blip_thumbnail = getBliptvInfo($matches[1]);
230
+ if (is_wp_error($blip_thumbnail)) {
231
+ return $blip_thumbnail;
232
+ } else if (isset($blip_thumbnail)) {
233
+ $new_thumbnail = $blip_thumbnail;
234
+ }
235
  }
236
  }
237
 
256
 
257
  // Dailymotion url
258
  if(!isset($matches[1])) {
259
+ preg_match('#(?:https?://)?(?:www\.)?dailymotion.com/video/([A-Za-z0-9]+)#s', $markup, $matches);
260
  }
261
 
262
  // Dailymotion iframe
263
  if(!isset($matches[1])) {
264
+ preg_match('#https?://www.dailymotion.com/embed/video/([A-Za-z0-9]+)#s', $markup, $matches);
265
  }
266
 
267
  // Now if we've found a Dailymotion video ID, let's set the thumbnail URL
268
  if(isset($matches[1])) {
269
  $dailymotion_thumbnail = getDailyMotionThumbnail($matches[1]);
270
+ if (is_wp_error($dailymotion_thumbnail)) {
271
+ return $dailymotion_thumbnail;
272
+ } else if (isset($dailymotion_thumbnail)) {
273
  $new_thumbnail = strtok($dailymotion_thumbnail, '?');
274
+ }
275
  }
276
  }
277
 
284
  // Now if we've found a Metacafe video ID, let's set the thumbnail URL
285
  if(isset($matches[1])) {
286
  $metacafe_thumbnail = getMetacafeThumbnail($matches[1]);
287
+ if (is_wp_error($metacafe_thumbnail)) {
288
+ return $metacafe_thumbnail;
289
+ } else if (isset($metacafe_thumbnail)) {
290
+ $new_thumbnail = strtok($metacafe_thumbnail, '?');
291
+ }
292
  }
293
  }
294
 
297
 
298
  // Save as Attachment if enabled
299
  if(get_option('video_thumbnails_save_media')==1) {
300
+ $error = '';
301
  $ch = curl_init();
302
  $timeout = 0;
303
  curl_setopt ($ch, CURLOPT_URL, $new_thumbnail);
304
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
305
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
306
+ curl_setopt($ch, CURLOPT_FAILONERROR, true); // Return an error for curl_error() processing if HTTP response code >= 400
307
+ $image_contents = curl_exec($ch);
308
+ if (curl_error($ch) != null || $image_contents == null) {
309
+ $curl_error = '';
310
+ if (curl_error($ch) != null) {
311
+ $curl_error = ": <code>" . curl_error($ch) . "</code>";
312
+ }
313
+ $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."));
314
+ }
315
+ curl_close($ch);
316
+
317
+ if ($error != null) {
318
+ return $error;
319
+ } else {
320
+
321
+ $upload = wp_upload_bits(basename($new_thumbnail), null, $image_contents);
322
+
323
+ $new_thumbnail = $upload['url'];
324
+
325
+ $filename = $upload['file'];
326
+
327
+ $wp_filetype = wp_check_filetype(basename($filename), null );
328
+ $attachment = array(
329
+ 'post_mime_type' => $wp_filetype['type'],
330
+ 'post_title' => get_the_title($post_id),
331
+ 'post_content' => '',
332
+ 'post_status' => 'inherit'
333
+ );
334
+ $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
335
+ // you must first include the image.php file
336
+ // for the function wp_generate_attachment_metadata() to work
337
+ require_once(ABSPATH . "wp-admin" . '/includes/image.php');
338
+ $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
339
+ wp_update_attachment_metadata( $attach_id, $attach_data );
340
+
341
+ }
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
  }
344
 
345
  // Add hidden custom field with thumbnail URL
364
  // Create Meta Fields on Edit Page
365
 
366
  add_action("admin_init", "video_thumbnail_admin_init");
367
+
368
  function video_thumbnail_admin_init(){
369
  $video_thumbnails_post_types = get_option('video_thumbnails_post_types');
370
  if(is_array($video_thumbnails_post_types)) {
383
  echo '<p id="video-thumbnails-preview"><img src="' . $video_thumbnail . '" width="100%" /></p>';
384
  }
385
 
386
+ if ( get_post_status() == 'publish' || get_post_status() == 'private' ) {
387
  if(isset($video_thumbnail) && $video_thumbnail!='') {
388
  echo '<p><a href="#" id="video-thumbnails-reset" onclick="video_thumbnails_reset(\'' . $post->ID . '\');return false;">Reset Video Thumbnail</a></p>';
389
  } else {
437
 
438
  delete_post_meta($post_id, '_video_thumbnail');
439
 
440
+ $video_thumbnail = get_video_thumbnail($post_id);
441
+
442
+ if ( is_wp_error($video_thumbnail) ) {
443
+ echo $video_thumbnail->get_error_message();
444
+ } else if ( $video_thumbnail != null ){
445
+ echo '<img src="' . $video_thumbnail . '" width="100%" />';
446
+ } else {
447
+ echo 'We didn\'t find a video thumbnail for this post. (be sure you have saved changes first)';
448
+ }
449
 
450
  die();
451
  }
534
 
535
  if ($posts) {
536
  foreach($posts as $post) {
537
+ $post_ids[] = $post->ID;
538
  }
539
  $ids = implode(',',$post_ids);
540
  }
577
 
578
  echo get_the_title($post_id) . ' - ';
579
 
580
+ $video_thumbnail = get_video_thumbnail($post_id);
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.';
590
  die();
591
  }
592
 
593
+ // Administration
594
 
595
  add_action('admin_menu', 'video_thumbnails_menu');
596
 
597
  function video_thumbnails_menu() {
598
 
599
+ add_options_page('Video Thumbnail Options', 'Video Thumbnails', 'manage_options', 'video-thumbnail-options', 'video_thumbnail_options');
600
 
601
  }
602
 
 
 
 
 
 
 
603
  function video_thumbnails_checkbox_option($option_name, $option_description) { ?>
604
  <fieldset><legend class="screen-reader-text"><span><?php echo $option_description; ?></span></legend>
605
  <label for="<?php echo $option_name; ?>"><input name="<?php echo $option_name; ?>" type="checkbox" id="<?php echo $option_name; ?>" value="1" <?php if(get_option($option_name)==1) echo "checked='checked'"; ?>/> <?php echo $option_description; ?></label>
606
  </fieldset> <?php
607
  }
608
 
609
+ function video_thumbnail_options() {
610
+
611
+ if (!current_user_can('manage_options')) {
612
+ wp_die( __('You do not have sufficient permissions to access this page.') );
613
+ }
614
+
615
  ?>
616
 
617
  <div class="wrap">
639
  <th scope="row">Post Types</th>
640
  <td>
641
  <?php $video_thumbnails_post_types = get_option('video_thumbnails_post_types'); ?>
642
+ <?php foreach(get_post_types() as $type): if($type == 'attachment' OR $type == 'revision' OR $type == 'nav_menu_item') continue; ?>
643
+ <label for="video_thumbnails_post_types_<?php echo $type; ?>">
644
+ <input id="video_thumbnails_post_types_<?php echo $type; ?>" name="video_thumbnails_post_types[]" type="checkbox" value="<?php echo $type; ?>" <?php if(is_array($video_thumbnails_post_types)) checked(in_array($type, $video_thumbnails_post_types)); ?> />
645
+ <?php echo $type; ?>
646
+ </label>
647
+ <br />
648
+ <?php endforeach; ?>
649
  </td>
650
  </tr>
651