[ 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          if ( file_exists( ABSPATH . WPINC . '/class-wp-oembed.php' ) ) {
 142              require_once( ABSPATH . WPINC . '/class-wp-oembed.php' );
 143          } else {
 144              // class-oembed.php is deprecated in WordPress 5.3.0.
 145              require_once( ABSPATH . WPINC . '/class-oembed.php' );
 146          }
 147  
 148          $oembed_obj = _wp_oembed_get_object();
 149  
 150          // If oEmbed discovery is true, skip oEmbed provider check.
 151          $is_oembed_link = false;
 152          if ( !$attr['discover'] ) {
 153              foreach ( (array) $oembed_obj->providers as $provider_matchmask => $provider ) {
 154                  $regex = ( $is_regex = $provider[1] ) ? $provider_matchmask : '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $provider_matchmask ), '#' ) ) . '#i';
 155  
 156                  if ( preg_match( $regex, $url ) )
 157                      $is_oembed_link = true;
 158              }
 159  
 160              // If url doesn't match a WP oEmbed provider, stop parsing.
 161              if ( !$is_oembed_link )
 162                  return $this->maybe_make_link( $url );
 163          }
 164  
 165          return $this->parse_oembed( $id, $url, $attr, $rawattr );
 166      }
 167  
 168      /**
 169       * Base function so BP components/plugins can parse links to be embedded.
 170       *
 171       * View an example to add support in {@link bp_activity_embed()}.
 172       *
 173       *       on success.
 174       *       oEmbed failure.
 175       *
 176       * @param int    $id      ID to do the caching for.
 177       * @param string $url     The URL attempting to be embedded.
 178       * @param array  $attr    Shortcode attributes from {@link WP_Embed::shortcode()}.
 179       * @param array  $rawattr Untouched shortcode attributes from
 180       *                        {@link WP_Embed::shortcode()}.
 181       * @return string The embed HTML on success, otherwise the original URL.
 182       */
 183  	public function parse_oembed( $id, $url, $attr, $rawattr ) {
 184          $id = intval( $id );
 185  
 186          if ( $id ) {
 187              // Setup the cachekey.
 188              $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
 189  
 190              // Let components / plugins grab their cache.
 191              $cache = '';
 192  
 193              /**
 194               * Filters the cache value to be used in the oEmbed, if exists.
 195               *
 196               * @since 1.5.0
 197               *
 198               * @param string $cache    Empty initial cache value.
 199               * @param int    $id       ID that the caching is for.
 200               * @param string $cachekey Key to use for the caching in the database.
 201               * @param string $url      The URL attempting to be embedded.
 202               * @param array  $attr     Parsed shortcode attributes.
 203               * @param array  $rawattr  Unparsed shortcode attributes.
 204               */
 205              $cache = apply_filters( 'bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr );
 206  
 207              // Grab cache and return it if available.
 208              if ( !empty( $cache ) ) {
 209  
 210                  /**
 211                   * Filters the found cache for the provided URL.
 212                   *
 213                   * @since 1.5.0
 214                   *
 215                   * @param string $cache   Cached HTML markup for embed.
 216                   * @param string $url     The URL being embedded.
 217                   * @param array  $attr    Parsed shortcode attributes.
 218                   * @param array  $rawattr Unparased shortcode attributes.
 219                   */
 220                  return apply_filters( 'bp_embed_oembed_html', $cache, $url, $attr, $rawattr );
 221  
 222              // If no cache, ping the oEmbed provider and cache the result.
 223              } else {
 224                  $html = wp_oembed_get( $url, $attr );
 225                  $cache = ( $html ) ? $html : $url;
 226  
 227                  /**
 228                   * Fires if there is no existing cache and triggers cache setting.
 229                   *
 230                   * Lets components / plugins save their cache.
 231                   *
 232                   * @since 1.5.0
 233                   *
 234                   * @param string $cache    Newly cached HTML markup for embed.
 235                   * @param string $cachekey Key to use for the caching in the database.
 236                   * @param int    $id       ID to do the caching for.
 237                   */
 238                  do_action( 'bp_embed_update_cache', $cache, $cachekey, $id );
 239  
 240                  // If there was a result, return it.
 241                  if ( $html ) {
 242  
 243                      /** This filter is documented in bp-core/classes/class-bp-embed.php */
 244                      return apply_filters( 'bp_embed_oembed_html', $html, $url, $attr, $rawattr );
 245                  }
 246              }
 247          }
 248  
 249          // Still unknown.
 250          return $this->maybe_make_link( $url );
 251      }
 252  }


Generated: Fri Nov 15 01:01:37 2019 Cross-referenced by PHPXref 0.7.1