[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-content/add-ons/ -> MailPress_embed.php (source)

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_embed'))
   3  {
   4  /*
   5  Plugin Name: MailPress_embed 
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/embed/
   7  Description: Mail oEmbed (equivalent of WordPress video embedding but for mails)
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_embed
  12  {
  13      public static $type = null;
  14      public static $mp_oembed = null;
  15  
  16      const meta_key = '_mp_oembed_';
  17      const usecache = true;
  18      const html_filter = 'mp_embed_oembed_html';
  19      const unknown = '{{unknown}}';
  20  
  21  	function __construct()
  22      {
  23          add_action( 'save_post',                         array(__CLASS__, 'save_post') );
  24  
  25          add_action('MailPress_build_mail_content_start',    array(__CLASS__, 'build_mail_content_start'));
  26          add_action('MailPress_build_mail_content_end',        array(__CLASS__, 'build_mail_content_end'));
  27  
  28          add_filter('mp_oembed_providers',                array(__CLASS__, 'instagram'));
  29      }
  30  
  31  	public static function instagram($providers)
  32      {
  33          $providers['#http://(www\.)?instagr\.am/.*#i'] = array( 'http://api.instagram.com/oembed', true );
  34          return $providers;
  35      }
  36  
  37  	public static function build_mail_content_start($type)
  38      {
  39          global $wp_embed, $mp_embed;
  40          remove_filter('the_content', array($wp_embed, 'autoembed'), 8 );
  41  
  42          self::$type = $type;
  43  
  44          if ('html' == $type)
  45          {
  46              $mp_embed = new MP_Embed();
  47              self::embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', array(__CLASS__, 'embed_handler_googlevideo') );
  48              do_action('MailPress_embed_register_handler');
  49          }
  50      }
  51  
  52  	public static function build_mail_content_end($type)
  53      {
  54          global $wp_embed, $mp_embed;
  55          add_filter('the_content', array($wp_embed, 'autoembed'), 8 );
  56  
  57          if ('html' == $type)
  58          {
  59              remove_filter('the_content', array($mp_embed, 'autoembed'), 8 );
  60              $mp_embed = self::$type = null;
  61          }
  62      }
  63  
  64  	public static function save_post($post_ID)
  65      {
  66          $post_metas = get_post_custom_keys($post_ID);
  67          if (empty($post_metas))    return;
  68  
  69          foreach($post_metas as $post_meta_key)
  70              if (self::meta_key == substr($post_meta_key, 0, strlen(self::meta_key)))
  71                  delete_post_meta($post_ID, $post_meta_key);
  72      }
  73  
  74  	public static function embed_register_handler( $id, $regex, $callback, $priority = 10 )
  75      {
  76          global $mp_embed;
  77          $mp_embed->register_handler( $id, $regex, $callback, $priority );
  78      }
  79  
  80  	public static function embed_unregister_handler( $id, $priority = 10 )
  81      {
  82          global $mp_embed;
  83          $mp_embed->unregister_handler( $id, $priority );
  84      }
  85  
  86  
  87  
  88  
  89  	public static function embed_handler_googlevideo( $matches, $attr, $url, $rawattr )
  90      {
  91          if (!isset($matches[2])) return $url;
  92  
  93          global $post;
  94          $post_ID = ( !empty($post->ID) ) ? $post->ID : null;
  95          if (!$post_ID) return $url;
  96  
  97          $docid = (string) $matches[2];
  98          if ('-' == $docid[0]) $docid = substr($docid, 1);
  99          $search_url = "https://ajax.googleapis.com/ajax/services/search/video?v=1.0&q={$docid}";
 100  
 101          if ( self::usecache )
 102          {
 103              $cachekey = self::meta_key . md5( $url );
 104              $html = get_post_meta( $post_ID, $cachekey, true );
 105              if ( self::unknown === $html )     return $url;        // Failures are cached
 106              if ( !empty($html) )            return apply_filters( self::html_filter, $html, $url, $attr, $post_ID );
 107          }
 108  
 109          // Use video search
 110          $json = get_object_vars(json_decode(file_get_contents($search_url)));
 111          if (isset($json['responseData']->results[0]) && 200 == $json['responseStatus'])
 112          {
 113              $r = $json['responseData']->results[0];
 114  
 115              $moves = array(    'thumbnail_url' => 'tbUrl', 'thumbnail_width' => 'tbWidth', 'thumbnail_height' => 'tbHeight', 'title' => 'title' );
 116              $data = new stdClass();
 117              foreach($moves as $k => $v) $data->{$k} = $r->{$v};
 118              $data->url = $url;
 119  
 120              $html = self::_embed_get($data);
 121          }
 122  
 123          if ( MailPress_embed::usecache )
 124              update_post_meta( $post_ID, $cachekey, ( $html ) ? $html : self::unknown );
 125  
 126          if ( $html )
 127              return apply_filters( self::html_filter, $html, $url, $attr, $post_ID );
 128  
 129          // Still unknown
 130          return $url;
 131      }
 132  
 133  
 134  
 135  
 136  	public static function _embed_get($data)
 137      {
 138          $html  = "<a target='_blank' href=\"" . esc_url($data->url) . "\"";
 139          $html .= " title=\"" . esc_html($data->title) . "\"";
 140          $html .= ">";
 141  
 142          $html .= "<img";
 143          $html .= " class='mp_{$data->provider_name}'";
 144          $html .= " width='{$data->thumbnail_width}px'";
 145          $html .= " height='{$data->thumbnail_height}px'";
 146          $html .= " src='{$data->thumbnail_url}'";
 147          $html .= " title=\"" . esc_html($data->title) . "\" alt=\"" . esc_html($data->title) . "\"";
 148          $html .= " />";
 149  
 150          $html .= "</a>";
 151  
 152          return $html;
 153      }
 154  
 155  
 156  
 157  
 158  	public static function _oembed_get($url, $args = '')
 159      {
 160          require_once ( ABSPATH . WPINC . '/class-oembed.php' );
 161          $oembed = self::_oembed_get_object();
 162          return $oembed->get_html( $url, $args );
 163      }
 164  
 165  	public static function _oembed_add_provider($format, $provider, $regex = false)
 166      {
 167          require_once ( ABSPATH . WPINC . '/class-oembed.php' );
 168          $oembed = self::_oembed_get_object();
 169          $oembed->providers[$format] = array( $provider, $regex );
 170      }
 171  
 172      public static function &_oembed_get_object() 
 173      {
 174          if ( is_null(self::$mp_oembed) ) self::$mp_oembed = new MP_oEmbed();
 175          self::$mp_oembed->providers = apply_filters('mp_oembed_providers', self::$mp_oembed->providers);
 176          return self::$mp_oembed;
 177      }
 178  }
 179  new MailPress_embed();
 180  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1