[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/class/ -> MP_Embed.class.php (source)

   1  <?php
   2  class MP_Embed 
   3  {
   4      var $handlers = array();
   5  
   6      /**
   7       * Constructor
   8       */
   9  	function __construct() 
  10      {
  11          // Attempts to embed all URLs in a post in a mail
  12          if ( !get_option('embed_autourls') ) return;
  13  
  14          add_filter( 'the_content', array($this, 'autoembed'), 8 );
  15      }
  16  
  17  	function register_handler( $id, $regex, $callback, $priority = 10 ) 
  18      {
  19          $this->handlers[$priority][$id] = array(
  20              'regex'    => $regex,
  21              'callback' => $callback,
  22          );
  23      }
  24  
  25  	function unregister_handler( $id, $priority = 10 ) 
  26      {
  27          if ( isset($this->handlers[$priority][$id]) ) unset($this->handlers[$priority][$id]);
  28      }
  29  
  30  	function autoembed( $content ) 
  31      {
  32          return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array($this, 'autoembed_callback'), $content );
  33      }
  34  
  35  	function autoembed_callback( $match ) 
  36      {
  37          $return = $this->shortcode( array(), $match[1] );
  38          return "\n$return\n";
  39      }
  40  
  41  	function shortcode( $attr, $url = '' ) 
  42      {
  43          if ( empty($url) ) return '';
  44  
  45          global $post;
  46          $post_ID = ( !empty($post->ID) ) ? $post->ID : null;
  47  //        if (!$post_ID) return $url;
  48          if (!$post_ID) $post_ID = -1;
  49  
  50          $rawattr = $attr;
  51          $attr = wp_parse_args( $attr, wp_embed_defaults() );
  52  
  53          // kses converts & into &amp; and we need to undo this
  54          // See http://core.trac.wordpress.org/ticket/11311
  55          $url = str_replace( '&amp;', '&', $url );
  56  
  57          // Look for known internal handlers
  58          ksort( $this->handlers );
  59          foreach ( $this->handlers as $priority => $handlers ) 
  60          {
  61              foreach ( $handlers as $id => $handler ) 
  62              {
  63                  if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) 
  64                  {
  65                      if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
  66                          return apply_filters( 'mp_embed_handler_html', $return, $url, $attr );
  67                  }
  68              }
  69          }
  70  
  71          // Unknown URL format. Let oEmbed have a go.
  72          if ( MailPress_embed::usecache )
  73          {
  74              $cachekey = MailPress_embed::meta_key . md5( $url . serialize( $attr ) );
  75              $html = get_post_meta( $post_ID, $cachekey, true );
  76              if ( MailPress_embed::unknown === $html )     return $url;        // Failures are cached
  77              if ( !empty($html) )                    return apply_filters( MailPress_embed::html_filter, $html, $url, $attr, $post_ID );
  78          }
  79  
  80          // Use oEmbed to get the HTML
  81          $attr['discover'] = ( apply_filters('mp_embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) );
  82          $html = MailPress_Embed::_oembed_get( $url, $attr );
  83  
  84          if ( MailPress_embed::usecache )
  85              update_post_meta( $post_ID, $cachekey, ( $html ) ? $html : MailPress_embed::unknown );
  86  
  87          if ( $html )
  88              return apply_filters( MailPress_embed::html_filter, $html, $url, $attr, $post_ID );
  89  
  90          // Still unknown
  91          return $url;
  92      }
  93  }


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