Public feed #381305

Miscellany

public php v1 · immutable
Raw Edit
·published 2007-03-04 19:34 UTC
rendered paste body
<?php  /*   Plugin Name: MyTube Thumbnail   Plugin URI: http:/www.markchaney.com/category/wp-plugins/   Description: Fetches YouTube thumbnail from MyTube video post.   Author: MACscr   Version: 1.0   Author URI: http:/www.markchaney.com   */        function macscr_create_thumb($video_id)  {      $url = 'http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=rG48P7iz0eo&video_id=' . $video_id . '';            $curl_handle = curl_init();      curl_setopt($curl_handle, CURLOPT_URL, $url);      curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);      curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);      $buffer = curl_exec($curl_handle);      curl_close($curl_handle);            if (empty($buffer)) {          print "Sorry, video unavailable";      } else {          $youtube_XML = $buffer;                    $begin = strpos($youtube_XML, '<thumbnail_url>');          $end = strpos($youtube_XML, '</thumbnail_url>', $begin);                    if ($begin !== false && $end !== false) {              // numbers may be wrong, change them if you get wrong URL.              $thumbnail_url = substr($youtube_XML, $begin + 15, $end - $begin - 15);          }                    $thumbnail_path = '' . get_option('upload_path') . '/ytube_thumbs/';          $upload_path = ABSPATH . $thumbnail_path;                    $thumbnail_image = '' . $video_id . '.jpg';                    if (!copy($thumbnail_url, $upload_path . $thumbnail_image)) {              $thumb_final = '<img class="mytube_thumb" src="' . get_bloginfo('url') . '/wp-content/plugins/mytube_thumb/images/nothumb.gif" alt="Thumbnail Unavailable" />';          } else {              $thumb_final = '<img class="mytube_thumb" src="' . get_bloginfo('url') . '/' . $thumbnail_path . $thumbnail_image . '" alt="" /> ';          }      }      return $thumb_final;  }    function macscr_video_id($content)  {      $begin = strpos($content, '<!--yt_video-->');      $end = strpos($content, '<!--/yt_video-->', $begin);            if ($begin !== false && $end !== false) {          // numbers may be wrong, change them if you get wrong URL.          $video_id = substr($content, $begin + 15, $end - $begin - 15);      }            if (!$video_id) {          return false;      } else {          return macscr_get_thumbnail($video_id);      }  }    function macscr_get_thumbnail($video_id)  {      $thumbnail_path = '' . get_option('upload_path') . '/ytube_thumbs/';      $upload_path = ABSPATH . $thumbnail_path;            $thumbnail_image = '' . $video_id . '.jpg';            if (file_exists($upload_path . $thumbnail_image)) {          $thumb_final = '<img class="mytube_thumb" src="' . get_bloginfo('url') . '/' . $thumbnail_path . $thumbnail_image . '" alt="" /> ';          return $thumb_final;      } else {          return macscr_create_thumb($video_id);      }  }    function macscr_add_thumbnail($content)  {      $GLOBALS['mytube_thumb'] = macscr_video_id($content);            return $content;  }    add_filter('the_content', 'macscr_add_thumbnail', $priority = '1');?>