Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 3rd, 2012  |  syntax: PHP  |  size: 28.48 KB  |  views: 142  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  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.8-working
  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>.<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.' ) );
  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( 'bliptv_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
  63. function getJustintvInfo( $id ) {
  64.         $xml = simplexml_load_file( "http://api.justin.tv/api/clip/show/$id.xml" );
  65.         return (string) $xml->clip->image_url_large;
  66. }
  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>.<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.' ) );
  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( '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.  
  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
  118.         else {
  119.  
  120.                 // Gets the post's content
  121.                 $post_array = get_post( $post_id );
  122.                 $markup = $post_array->post_content;
  123.                 $markup = apply_filters( 'the_content', $markup );
  124.                 $new_thumbnail = null;
  125.  
  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\-_]+)#', $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\-_]+)#', $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\-_]+)#', $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\-_]+)#', $markup, $matches );
  154.                 }
  155.  
  156.                 // Checks for YouTube Lyte
  157.                 if ( !isset( $matches[1] ) && function_exists( 'lyte_parse' ) ) {
  158.                         preg_match( '#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', $markup, $matches );
  159.                 }
  160.  
  161.                 // If we've found a YouTube video ID, create the thumbnail URL
  162.                 if ( isset( $matches[1] ) ) {
  163.                         $youtube_thumbnail = 'http://img.youtube.com/vi/' . $matches[1] . '/0.jpg';
  164.  
  165.                         // Check to make sure it's an actual thumbnail
  166.                         if ( ! function_exists( 'curl_init' ) ) {
  167.                                 $new_thumbnail = $youtube_thumbnail;
  168.                         } else {
  169.                                 $ch = curl_init( $youtube_thumbnail );
  170.                                 curl_setopt( $ch, CURLOPT_NOBODY, true );
  171.                                 curl_exec( $ch );
  172.                                 $retcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
  173.                                 // $retcode > 400 -> not found, $retcode = 200, found.
  174.                                 curl_close( $ch );
  175.                                 if ( $retcode == 200 ) {
  176.                                         $new_thumbnail = $youtube_thumbnail;
  177.                                 }
  178.                         }
  179.                 }
  180.  
  181.                 // Vimeo
  182.                 if ( $new_thumbnail == null ) {
  183.  
  184.                         // Standard embed code
  185.                         preg_match( '#<object[^>]+>.+?http://vimeo\.com/moogaloop.swf\?clip_id=([A-Za-z0-9\-_]+)&.+?</object>#s', $markup, $matches );
  186.  
  187.                         // Find Vimeo embedded with iframe code
  188.                         if ( !isset( $matches[1] ) ) {
  189.                                 preg_match( '#http://player\.vimeo\.com/video/([0-9]+)#', $markup, $matches );
  190.                         }
  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\-_]+)]#', $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\-_]+)#', $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\-_]+)"[^>]*]#', $markup, $matches );
  205.                         }
  206.                         if ( !isset( $matches[1] ) ) {
  207.                                 preg_match( '#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#', $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.                         }
  219.                 }
  220.  
  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]+)#', $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.  
  238.                 // Justin.tv
  239.                 if ( $new_thumbnail == null ) {
  240.  
  241.                         // Justin.tv archive ID
  242.                         preg_match( '#archive_id=([0-9]+)#', $markup, $matches );
  243.  
  244.                         // Now if we've found a Justin.tv archive ID, let's set the thumbnail URL
  245.                         if ( isset( $matches[1] ) ) {
  246.                                 $justin_thumbnail = getJustintvInfo( $matches[1] );
  247.                                 $new_thumbnail = $justin_thumbnail;
  248.                         }
  249.                 }
  250.  
  251.                 // Dailymotion
  252.                 if ( $new_thumbnail == null ) {
  253.  
  254.                         // Dailymotion flash
  255.                         preg_match( '#<object[^>]+>.+?http://www\.dailymotion\.com/swf/video/([A-Za-z0-9]+).+?</object>#s', $markup, $matches );
  256.  
  257.                         // Dailymotion url
  258.                         if ( !isset( $matches[1] ) ) {
  259.                                 preg_match( '#(?:https?://)?(?:www\.)?dailymotion\.com/video/([A-Za-z0-9]+)#', $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]+)#', $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.  
  278.                 // Metacafe
  279.                 if ( $new_thumbnail == null ) {
  280.  
  281.                         // Find ID from Metacafe embed url
  282.                         preg_match( '#http://www\.metacafe\.com/fplayer/([A-Za-z0-9\-_]+)/#', $markup, $matches );
  283.  
  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.  
  295.                 // Return the new thumbnail variable and update meta if one is found
  296.                 if ( $new_thumbnail != null ) {
  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 = '<br /><a href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">Libcurl error</a> ' . curl_errno( $ch ) . ': <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
  346.                         if ( !update_post_meta( $post_id, '_video_thumbnail', $new_thumbnail ) ) add_post_meta( $post_id, '_video_thumbnail', $new_thumbnail, true );
  347.  
  348.                         // Set attachment as featured image if enabled
  349.                         if ( get_option( 'video_thumbnails_set_featured' ) == 1 && get_option( 'video_thumbnails_save_media' ) == 1 && get_post_meta( $post_id, '_thumbnail_id', true ) == '' ) {
  350.                                 if ( !update_post_meta( $post_id, '_thumbnail_id', $attach_id ) ) add_post_meta( $post_id, '_thumbnail_id', $attach_id, true );
  351.                         }
  352.                 }
  353.                 return $new_thumbnail;
  354.  
  355.         }
  356. };
  357.  
  358. // Echo thumbnail
  359. function video_thumbnail( $post_id = null ) {
  360.         if ( ( $video_thumbnail = get_video_thumbnail( $post_id ) ) == null ) { echo plugins_url() . '/video-thumbnails/default.jpg'; }
  361.         else { echo $video_thumbnail; }
  362. };
  363.  
  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 ) ) {
  371.                 foreach ( $video_thumbnails_post_types as $type ) {
  372.                         add_meta_box( 'video_thumbnail', 'Video Thumbnail', 'video_thumbnail_admin', $type, 'side', 'low' );
  373.                 }
  374.         }
  375. }
  376.  
  377. function video_thumbnail_admin() {
  378.         global $post;
  379.         $custom = get_post_custom( $post->ID );
  380.         $video_thumbnail = $custom["_video_thumbnail"][0];
  381.  
  382.         if ( isset( $video_thumbnail ) && $video_thumbnail != '' ) {
  383.                 echo '<p id="video-thumbnails-preview"><img src="' . $video_thumbnail . '" style="max-width:100%;" /></p>';     }
  384.  
  385.         if ( get_post_status() == 'publish' || get_post_status() == 'private' ) {
  386.                 if ( isset( $video_thumbnail ) && $video_thumbnail != '' ) {
  387.                         echo '<p><a href="#" id="video-thumbnails-reset" onclick="video_thumbnails_reset(\'' . $post->ID . '\' );return false;">Reset Video Thumbnail</a></p>';
  388.                 } else {
  389.                         echo '<p id="video-thumbnails-preview">We didn\'t find a video thumbnail for this post.</p>';
  390.                         echo '<p><a href="#" id="video-thumbnails-reset" onclick="video_thumbnails_reset(\'' . $post->ID . '\' );return false;">Search Again</a></p>';
  391.                 }
  392.         } else {
  393.                 if ( isset( $video_thumbnail ) && $video_thumbnail != '' ) {
  394.                         echo '<p><a href="#" id="video-thumbnails-reset" onclick="video_thumbnails_reset(\'' . $post->ID . '\' );return false;">Reset Video Thumbnail</a></p>';
  395.                 } else {
  396.                         echo '<p>A video thumbnail will be found for this post when it is published.</p>';
  397.                 }
  398.         }
  399. }
  400.  
  401. // AJAX Searching
  402.  
  403. if ( in_array( basename( $_SERVER['PHP_SELF'] ), apply_filters( 'video_thumbnails_editor_pages', array( 'post-new.php', 'page-new.php', 'post.php', 'page.php' ) ) ) ) {
  404.         add_action( 'admin_head', 'video_thumbnails_ajax' );
  405. }
  406.  
  407. function video_thumbnails_ajax() {
  408. ?>
  409.  
  410. <!-- Video Thumbnails Researching Ajax -->
  411. <script type="text/javascript">
  412. function video_thumbnails_reset(id) {
  413.  
  414.         var data = {
  415.                 action: 'video_thumbnails',
  416.                 post_id: id
  417.         };
  418.  
  419.         document.getElementById('video-thumbnails-preview').innerHTML='Working... <img src="<?php echo home_url( 'wp-admin/images/loading.gif' ); ?>"/>';
  420.  
  421.         // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  422.         jQuery.post(ajaxurl, data, function(response){
  423.                 document.getElementById('video-thumbnails-preview').innerHTML=response;
  424.         });
  425. };
  426. </script>
  427. <?php
  428. }
  429.  
  430. add_action( 'wp_ajax_video_thumbnails', 'video_thumbnails_callback' );
  431.  
  432. function video_thumbnails_callback() {
  433.         global $wpdb; // this is how you get access to the database
  434.  
  435.         $post_id = $_POST['post_id'];
  436.  
  437.         delete_post_meta( $post_id, '_video_thumbnail' );
  438.  
  439.         $video_thumbnail = get_video_thumbnail( $post_id );
  440.  
  441.         if ( is_wp_error( $video_thumbnail ) ) {
  442.                 echo $video_thumbnail->get_error_message();
  443.         } else  if ( $video_thumbnail != null ) {
  444.                 echo '<img src="' . $video_thumbnail . '" style="max-width:100%;" />';
  445.         } else {
  446.                 echo 'We didn\'t find a video thumbnail for this post. (be sure you have saved changes first)';
  447.         }
  448.  
  449.         die();
  450. }
  451.  
  452. // Find video thumbnail when saving a post, but not on autosave
  453.  
  454. add_action( 'new_to_publish', 'save_video_thumbnail', 10, 1 );
  455. add_action( 'draft_to_publish', 'save_video_thumbnail', 10, 1 );
  456. add_action( 'pending_to_publish', 'save_video_thumbnail', 10, 1 );
  457. add_action( 'future_to_publish', 'save_video_thumbnail', 10, 1 );
  458.  
  459. function save_video_thumbnail( $post ) {
  460.         $post_type = get_post_type( $post->ID );
  461.         $video_thumbnails_post_types = get_option( 'video_thumbnails_post_types' );
  462.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  463.                 return null;
  464.         } else {
  465.                 // Check that Video Thumbnails are enabled for current post type
  466.                 if ( in_array( $post_type, (array) $video_thumbnails_post_types ) || $post_type == $video_thumbnails_post_types ) {
  467.                         get_video_thumbnail( $post->ID );
  468.                 } else {
  469.                         return null;
  470.                 }
  471.         }
  472. }
  473.  
  474. // Set Default Options
  475.  
  476. register_activation_hook( __FILE__, 'video_thumbnails_activate' );
  477. register_deactivation_hook( __FILE__, 'video_thumbnails_deactivate' );
  478.  
  479. function video_thumbnails_activate() {
  480.         add_option( 'video_thumbnails_save_media', '1' );
  481.         add_option( 'video_thumbnails_set_featured', '1' );
  482.         add_option( 'video_thumbnails_post_types', array( 'post' ) );
  483. }
  484.  
  485. function video_thumbnails_deactivate() {
  486.         delete_option( 'video_thumbnails_save_media' );
  487.         delete_option( 'video_thumbnails_set_featured' );
  488.         delete_option( 'video_thumbnails_post_types' );
  489. }
  490.  
  491. // Check for cURL
  492.  
  493. register_activation_hook( __FILE__, 'video_thumbnails_curl_check' );
  494.  
  495. function video_thumbnails_curl_check() {
  496.         if ( ! function_exists( 'curl_init' ) ) {
  497.                 deactivate_plugins( basename( __FILE__ ) ); // Deactivate ourself
  498.                 wp_die( 'Sorry, but this plugin requires <a href="http://curl.haxx.se/libcurl/">libcurl</a> to be activated on your server.' );
  499.         }
  500. }
  501.  
  502. // AJAX for Past Posts
  503.  
  504. if ( isset ( $_GET['page'] ) && ( $_GET['page'] == 'video-thumbnail-options' ) ) {
  505.         add_action( 'admin_head', 'video_thumbnails_past_ajax' );
  506. }
  507.  
  508. function video_thumbnails_past_ajax() {
  509. ?>
  510.  
  511. <!-- Video Thumbnails Past Post Ajax -->
  512. <script type="text/javascript">
  513. function video_thumbnails_past(id) {
  514.  
  515.         var data = {
  516.                 action: 'video_thumbnails_past',
  517.                 post_id: id
  518.         };
  519.  
  520.         // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  521.         jQuery.post(ajaxurl, data, function(response){
  522.  
  523.                 document.getElementById(id+'_result').innerHTML = response;
  524.  
  525.         });
  526.  
  527. };
  528.  
  529. <?php
  530. $video_thumbnails_post_types = get_option( 'video_thumbnails_post_types' );
  531. $posts = get_posts( array(
  532.         'showposts' => -1,
  533.         'post_type' => $video_thumbnails_post_types
  534. ) );
  535.  
  536. if ( $posts ) {
  537.         foreach ( $posts as $post ) {
  538.                 $post_ids[] = $post->ID;
  539.         }
  540.         $ids = implode( ', ', $post_ids );
  541. }
  542. ?>
  543.  
  544. var scanComplete = false;
  545.  
  546. function scan_video_thumbnails(){
  547.  
  548.         if(scanComplete==false){
  549.                 scanComplete = true;
  550.                 var ids = new Array(<?php echo $ids; ?>);
  551.                 for (var i = 0; i < ids.length; i++){
  552.                         var container = document.getElementById('video-thumbnails-past');
  553.                         var new_element = document.createElement('li');
  554.                         new_element.setAttribute('id',ids[i]+'_result');
  555.                         new_element.innerHTML = 'Waiting...';
  556.                         container.insertBefore(new_element, container.firstChild);
  557.                 }
  558.                 for (var i = 0; i < ids.length; i++){
  559.                         document.getElementById(ids[i]+'_result').innerHTML = '<span style="color:yellow">&#8226;</span> Working...';
  560.                         video_thumbnails_past(ids[i]);
  561.                 }
  562.         } else {
  563.                 alert('Scan has already been run, please reload the page before trying again.')
  564.         }
  565.  
  566. }
  567. </script>
  568.  
  569. <?php
  570. }
  571.  
  572. add_action( 'wp_ajax_video_thumbnails_past', 'video_thumbnails_past_callback' );
  573.  
  574. function video_thumbnails_past_callback() {
  575.         global $wpdb; // this is how you get access to the database
  576.  
  577.         $post_id = $_POST['post_id'];
  578.  
  579.         echo get_the_title( $post_id ) . ' - ';
  580.  
  581.         $video_thumbnail = get_video_thumbnail( $post_id );
  582.  
  583.         if ( is_wp_error( $video_thumbnail ) ) {
  584.                 echo $video_thumbnail->get_error_message();
  585.         } else  if ( $video_thumbnail != null ) {
  586.                 echo '<span style="color:green">&#10004;</span> Success!';
  587.         } else {
  588.                 echo '<span style="color:red">&#10006;</span> Couldn\'t find a video thumbnail for this post.';
  589.         }
  590.  
  591.         die();
  592. }
  593.  
  594. // Administration
  595.  
  596. add_action( 'admin_menu', 'video_thumbnails_menu' );
  597.  
  598. function video_thumbnails_menu() {
  599.         add_options_page( 'Video Thumbnail Options', 'Video Thumbnails', 'manage_options', 'video-thumbnail-options', 'video_thumbnail_options' );
  600. }
  601.  
  602. function video_thumbnails_checkbox_option( $option_name, $option_description ) { ?>
  603.         <fieldset><legend class="screen-reader-text"><span><?php echo $option_description; ?></span></legend>
  604.         <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>
  605.         </fieldset> <?php
  606. }
  607.  
  608. function video_thumbnail_options() {
  609.  
  610.         if ( ! current_user_can( 'manage_options' ) )  {
  611.                 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  612.         }
  613.  
  614. ?>
  615.  
  616. <div class="wrap">
  617.  
  618.         <div id="icon-options-general" class="icon32"></div><h2>Video Thumbnails Options</h2>
  619.  
  620.         <p>Say thanks by donating, any amount is appreciated!<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHRwYJKoZIhvcNAQcEoIIHODCCBzQCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYB1rPWk/Rr89ydxDsoXWyYIlAwIORRiWzcLHSBBVBMY69PHCO6WVTK2lXYmjZbDrvrHmN/jrM5r3Q008oX19NujzZ4d1VV+dWZxPU+vROuLToOFkk3ivjcvlT825HfdZRoiY/eTwWfBH93YQ+3kAAdc2s3FRxVyC4cUdrtbkBmYpDELMAkGBSsOAwIaBQAwgcQGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIkO3IVfkE9PGAgaA9fgOdXrQSpdGgo8ZgjiOxDGlEHoRL51gvB6AZdhNCubfLbqolJjYfTPEMg6Z0dfrq3hVSF2+nLV7BRcmXAtxY5NkH7vu1Kv0Bsb5kDOWb8h4AfnwElD1xyaykvYAr7CRNqHcizYRXZHKE7elWY0w6xRV/bfE7w6E4ZjKvFowHFp9E7/3mcZDrqxbZVU5hqs5gsV2YJj8fNBzG1bbdTucXoIIDhzCCA4MwggLsoAMCAQICAQAwDQYJKoZIhvcNAQEFBQAwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMB4XDTA0MDIxMzEwMTMxNVoXDTM1MDIxMzEwMTMxNVowgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBR07d/ETMS1ycjtkpkvjXZe9k+6CieLuLsPumsJ7QC1odNz3sJiCbs2wC0nLE0uLGaEtXynIgRqIddYCHx88pb5HTXv4SZeuv0Rqq4+axW9PLAAATU8w04qqjaSXgbGLP3NmohqM6bV9kZZwZLR/klDaQGo1u9uDb9lr4Yn+rBQIDAQABo4HuMIHrMB0GA1UdDgQWBBSWn3y7xm8XvVk/UtcKG+wQ1mSUazCBuwYDVR0jBIGzMIGwgBSWn3y7xm8XvVk/UtcKG+wQ1mSUa6GBlKSBkTCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb22CAQAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCBXzpWmoBa5e9fo6ujionW1hUhPkOBakTr3YCDjbYfvJEiv/2P+IobhOGJr85+XHhN0v4gUkEDI8r2/rNk1m0GA8HKddvTjyGw/XqXa+LSTlDYkqI8OwR8GEYj4efEtcRpRYBxV8KxAW93YDWzFGvruKnnLbDAF6VR5w/cCMn5hzGCAZowggGWAgEBMIGUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTExMDA3MDUzMjM1WjAjBgkqhkiG9w0BCQQxFgQUHXhTYmeIfU7OyslesSVlGviqHbIwDQYJKoZIhvcNAQEBBQAEgYDAU3s+ej0si2FdN0uZeXhR+GGCDOMSYbkRswu7K3TRDXoD9D9c67VjQ+GfqP95cA9s40aT73goH+AxPbiQhG64OaHZZGJeSmwiGiCo4rBoVPxNUDONMPWaYfp6vm3Mt41gbxUswUEDNnzps4waBsFRJvuFjbbeQVYg7wbVfQC99Q==-----END PKCS7-----"><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></form></p>
  621.  
  622.         <form method="post" action="options.php">
  623.         <?php wp_nonce_field( 'update-options' ); ?>
  624.  
  625.         <table class="form-table">
  626.  
  627.         <tr valign="top">
  628.         <th scope="row">Save Thumbnail to Media</th>
  629.         <td><?php video_thumbnails_checkbox_option( 'video_thumbnails_save_media', 'Save local copies of thumbnails using the media library' ); ?></td>
  630.         </tr>
  631.  
  632.         <tr valign="top">
  633.         <th scope="row">Set as Featured Image</th>
  634.         <td><?php video_thumbnails_checkbox_option( 'video_thumbnails_set_featured', 'Automatically set thumbnail as featured image ("Save Thumbnail to Media" must be enabled)' ); ?></td>
  635.         </tr>
  636.  
  637.         <tr valign="top">
  638.         <th scope="row">Post Types</th>
  639.         <td>
  640.         <?php $video_thumbnails_post_types = get_option( 'video_thumbnails_post_types' ); ?>
  641.                 <?php foreach ( get_post_types() as $type ) : if ( $type == 'attachment' OR $type == 'revision' OR $type == 'nav_menu_item' ) continue; ?>
  642.                 <label for="video_thumbnails_post_types_<?php echo $type; ?>">
  643.                         <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 ) ); ?> />
  644.                         <?php echo $type; ?>
  645.                 </label>
  646.                 <br />
  647.                 <?php endforeach; ?>
  648.         </td>
  649.         </tr>
  650.  
  651.         </table>
  652.  
  653.         <p class="submit">
  654.         <input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ) ?>" />
  655.         </p>
  656.  
  657.         <h3>How to use</h3>
  658.  
  659.         <p>For themes that use featured images, simply leave the two settings above enabled.</p>
  660.  
  661.         <p>For more detailed instructions, check out the page for <a href="http://wordpress.org/extend/plugins/video-thumbnails/">Video Thumbnails on the official plugin directory</a>.</p>
  662.  
  663.         <input type="hidden" name="action" value="update" />
  664.         <input type="hidden" name="page_options" value="video_thumbnails_save_media,video_thumbnails_set_featured,video_thumbnails_post_types" />
  665.  
  666.         </form>
  667.  
  668.         <h3>Scan All Posts</h3>
  669.  
  670.         <p>Scan all of your past posts for video thumbnails. Be sure to save any settings before running the scan.</p>
  671.  
  672.         <p><input type="submit" class="button-primary" onclick="scan_video_thumbnails();" value="Scan Past Posts" /></p>
  673.  
  674.         <ol id="video-thumbnails-past">
  675.         </ol>
  676.  
  677. </div>
  678.  
  679. <?php
  680.  
  681. }
  682.  
  683. ?>