[ Index ]

MailPress 7.1

[ 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          $rawattr = $attr;
  46          $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) );
  47  
  48          // kses converts & into &amp; and we need to undo this
  49          // See http://core.trac.wordpress.org/ticket/11311
  50          $url = str_replace( '&amp;', '&', $url );
  51  
  52          // Look for known internal handlers
  53          ksort( $this->handlers );
  54          foreach ( $this->handlers as $priority => $handlers ) 
  55          {
  56              foreach ( $handlers as $id => $handler ) 
  57              {
  58                  if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) 
  59                  {
  60                      if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
  61                          return apply_filters( 'mp_embed_handler_html', $return, $url, $attr );
  62                  }
  63              }
  64          }
  65  
  66          $post = get_post();
  67          $post_ID = ( !empty( $post->ID ) ) ? $post->ID : null;
  68          if ( !$post_ID ) $post_ID = -1;
  69  
  70          // Unknown URL format. Let oEmbed have a go.
  71          if ( MailPress_embed::usecache )
  72          {
  73              $cachekey = MailPress_embed::meta_key . md5( $url . serialize( $attr ) );
  74              $html = get_post_meta( $post_ID, $cachekey, true );
  75              if ( MailPress_embed::unknown === $html )     return $url;        // Failures are cached
  76              if ( !empty( $html ) ) return apply_filters( MailPress_embed::html_filter, $html, $url, $attr, $post_ID );
  77          }
  78  
  79          $attr['discover'] = ( apply_filters( 'mp_embed_oembed_discover', false ) );
  80  
  81          // Use oEmbed to get the 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: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1