[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-embed.php (source)

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Enable oEmbeds in BuddyPress contexts.
  15   *
  16   * Extends WP_Embed class for use with BuddyPress.
  17   *
  18   * @since 1.5.0
  19   *
  20   * @see WP_Embed
  21   */
  22  class BP_Embed extends WP_Embed {
  23  
  24      /**
  25       * Constructor
  26       *
  27       * @global WP_Embed $wp_embed
  28       */
  29  	public function __construct() {
  30          global $wp_embed;
  31  
  32          // Make sure we populate the WP_Embed handlers array.
  33          // These are providers that use a regex callback on the URL in question.
  34          // Do not confuse with oEmbed providers, which require an external ping.
  35          // Used in WP_Embed::shortcode().
  36          $this->handlers = $wp_embed->handlers;
  37  
  38          if ( bp_use_embed_in_activity() ) {
  39              add_filter( 'bp_get_activity_content_body', array( &$this, 'autoembed' ), 8 );
  40              add_filter( 'bp_get_activity_content_body', array( &$this, 'run_shortcode' ), 7 );
  41          }
  42  
  43          if ( bp_use_embed_in_activity_replies() ) {
  44              add_filter( 'bp_get_activity_content', array( &$this, 'autoembed' ), 8 );
  45              add_filter( 'bp_get_activity_content', array( &$this, 'run_shortcode' ), 7 );
  46          }
  47  
  48          if ( bp_use_embed_in_private_messages() ) {
  49              add_filter( 'bp_get_the_thread_message_content', array( &$this, 'autoembed' ), 8 );
  50              add_filter( 'bp_get_the_thread_message_content', array( &$this, 'run_shortcode' ), 7 );
  51          }
  52  
  53          /**
  54           * Filters the BuddyPress Core oEmbed setup.
  55           *
  56           * @since 1.5.0
  57           *
  58           * @param BP_Embed $this Current instance of the BP_Embed. Passed by reference.
  59           */
  60          do_action_ref_array( 'bp_core_setup_oembed', array( &$this ) );
  61      }
  62  
  63      /**
  64       * The {@link do_shortcode()} callback function.
  65       *
  66       * Attempts to convert a URL into embed HTML. Starts by checking the
  67       * URL against the regex of the registered embed handlers. Next, checks
  68       * the URL against the regex of registered {@link WP_oEmbed} providers
  69       * if oEmbed discovery is false. If none of the regex matches and it's
  70       * enabled, then the URL will be passed to {@link BP_Embed::parse_oembed()}
  71       * for oEmbed parsing.
  72       *
  73       *
  74       * @param array  $attr Shortcode attributes.
  75       * @param string $url  The URL attempting to be embeded.
  76       * @return string The embed HTML on success, otherwise the original URL.
  77       */
  78  	public function shortcode( $attr, $url = '' ) {
  79          if ( empty( $url ) )
  80              return '';
  81  
  82          $rawattr = $attr;
  83          $attr = wp_parse_args( $attr, wp_embed_defaults() );
  84  
  85          // Use kses to convert & into &amp; and we need to undo this
  86          // See https://core.trac.wordpress.org/ticket/11311.
  87          $url = str_replace( '&amp;', '&', $url );
  88  
  89          // Look for known internal handlers.
  90          ksort( $this->handlers );
  91          foreach ( $this->handlers as $priority => $handlers ) {
  92              foreach ( $handlers as $hid => $handler ) {
  93                  if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
  94                      if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) {
  95  
  96                          /**
  97                           * Filters the oEmbed handler result for the provided URL.
  98                           *
  99                           * @since 1.5.0
 100                           *
 101                           * @param string $return Handler callback for the oEmbed.
 102                           * @param string $url    URL attempting to be embedded.
 103                           * @param array  $attr   Shortcode attributes.
 104                           */
 105                          return apply_filters( 'embed_handler_html', $return, $url, $attr );
 106                      }
 107                  }
 108              }
 109          }
 110  
 111          /**
 112           * Filters the embed object ID.
 113           *
 114           * @since 1.5.0
 115           *
 116           * @param int $value Value of zero.
 117           */
 118          $id = apply_filters( 'embed_post_id', 0 );
 119  
 120          $unfiltered_html   = current_user_can( 'unfiltered_html' );
 121          $default_discovery = false;
 122  
 123          // Since 4.4, WordPress is now an oEmbed provider.
 124          if ( function_exists( 'wp_oembed_register_route' ) ) {
 125              $unfiltered_html   = true;
 126              $default_discovery = true;
 127          }
 128  
 129          /**
 130           * Filters whether or not oEmbed discovery is on.
 131           *
 132           * @since 1.5.0
 133           * @since 2.5.0 Default status of oEmbed discovery has been switched
 134           *              to true to apply changes introduced in WordPress 4.4
 135           *
 136           * @param bool $default_discovery Current status of oEmbed discovery.
 137           */
 138          $attr['discover'] = ( apply_filters( 'bp_embed_oembed_discover', $default_discovery ) && $unfiltered_html );
 139  
 140          // Set up a new WP oEmbed object to check URL with registered oEmbed providers.
 141          require_once( ABSPATH . WPINC . '/class-oembed.php' );
 142          $oembed_obj = _wp_oembed_get_object();
 143  
 144          // If oEmbed discovery is true, skip oEmbed provider check.
 145          $is_oembed_link = false;
 146          if ( !$attr['discover'] ) {
 147              foreach ( (array) $oembed_obj->providers as $provider_matchmask => $provider ) {
 148                  $regex = ( $is_regex = $provider[1] ) ? $provider_matchmask : '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $provider_matchmask ), '#' ) ) . '#i';
 149  
 150                  if ( preg_match( $regex, $url ) )
 151                      $is_oembed_link = true;
 152              }
 153  
 154              // If url doesn't match a WP oEmbed provider, stop parsing.
 155              if ( !$is_oembed_link )
 156                  return $this->maybe_make_link( $url );
 157          }
 158  
 159          return $this->parse_oembed( $id, $url, $attr, $rawattr );
 160      }
 161  
 162      /**
 163       * Base function so BP components/plugins can parse links to be embedded.
 164       *
 165       * View an example to add support in {@link bp_activity_embed()}.
 166       *
 167       *       on success.
 168       *       oEmbed failure.
 169       *
 170       * @param int    $id      ID to do the caching for.
 171       * @param string $url     The URL attempting to be embedded.
 172       * @param array  $attr    Shortcode attributes from {@link WP_Embed::shortcode()}.
 173       * @param array  $rawattr Untouched shortcode attributes from
 174       *                        {@link WP_Embed::shortcode()}.
 175       * @return string The embed HTML on success, otherwise the original URL.
 176       */
 177  	public function parse_oembed( $id, $url, $attr, $rawattr ) {
 178          $id = intval( $id );
 179  
 180          if ( $id ) {
 181              // Setup the cachekey.
 182              $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
 183  
 184              // Let components / plugins grab their cache.
 185              $cache = '';
 186  
 187              /**
 188               * Filters the cache value to be used in the oEmbed, if exists.
 189               *
 190               * @since 1.5.0
 191               *
 192               * @param string $cache    Empty initial cache value.
 193               * @param int    $id       ID that the caching is for.
 194               * @param string $cachekey Key to use for the caching in the database.
 195               * @param string $url      The URL attempting to be embedded.
 196               * @param array  $attr     Parsed shortcode attributes.
 197               * @param array  $rawattr  Unparsed shortcode attributes.
 198               */
 199              $cache = apply_filters( 'bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr );
 200  
 201              // Grab cache and return it if available.
 202              if ( !empty( $cache ) ) {
 203  
 204                  /**
 205                   * Filters the found cache for the provided URL.
 206                   *
 207                   * @since 1.5.0
 208                   *
 209                   * @param string $cache   Cached HTML markup for embed.
 210                   * @param string $url     The URL being embedded.
 211                   * @param array  $attr    Parsed shortcode attributes.
 212                   * @param array  $rawattr Unparased shortcode attributes.
 213                   */
 214                  return apply_filters( 'bp_embed_oembed_html', $cache, $url, $attr, $rawattr );
 215  
 216              // If no cache, ping the oEmbed provider and cache the result.
 217              } else {
 218                  $html = wp_oembed_get( $url, $attr );
 219                  $cache = ( $html ) ? $html : $url;
 220  
 221                  /**
 222                   * Fires if there is no existing cache and triggers cache setting.
 223                   *
 224                   * Lets components / plugins save their cache.
 225                   *
 226                   * @since 1.5.0
 227                   *
 228                   * @param string $cache    Newly cached HTML markup for embed.
 229                   * @param string $cachekey Key to use for the caching in the database.
 230                   * @param int    $id       ID to do the caching for.
 231                   */
 232                  do_action( 'bp_embed_update_cache', $cache, $cachekey, $id );
 233  
 234                  // If there was a result, return it.
 235                  if ( $html ) {
 236  
 237                      /** This filter is documented in bp-core/classes/class-bp-embed.php */
 238                      return apply_filters( 'bp_embed_oembed_html', $html, $url, $attr, $rawattr );
 239                  }
 240              }
 241          }
 242  
 243          // Still unknown.
 244          return $this->maybe_make_link( $url );
 245      }
 246  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1