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


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1