BP_Embed

Enable oEmbeds in BuddyPress contexts.

Defined (1)

The class is defined in the following location(s).

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