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


Generated: Sat Jun 19 01:01:44 2021 Cross-referenced by PHPXref 0.7.1