/bp-core/classes/class-bp-embed.php

  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. if ( bp_use_embed_in_activity_replies() ) { 
  43. add_filter( 'bp_get_activity_content', array( &$this, 'autoembed' ), 8 ); 
  44. add_filter( 'bp_get_activity_content', array( &$this, 'run_shortcode' ), 7 ); 
  45.  
  46. if ( bp_use_embed_in_forum_posts() ) { 
  47. add_filter( 'bp_get_the_topic_post_content', array( &$this, 'autoembed' ), 8 ); 
  48. add_filter( 'bp_get_the_topic_post_content', array( &$this, 'run_shortcode' ), 7 ); 
  49.  
  50. if ( bp_use_embed_in_private_messages() ) { 
  51. add_filter( 'bp_get_the_thread_message_content', array( &$this, 'autoembed' ), 8 ); 
  52. add_filter( 'bp_get_the_thread_message_content', array( &$this, 'run_shortcode' ), 7 ); 
  53.  
  54. /** 
  55. * Filters the BuddyPress Core oEmbed setup. 
  56. * 
  57. * @since 1.5.0 
  58. * 
  59. * @param BP_Embed $this Current instance of the BP_Embed. Passed by reference. 
  60. */ 
  61. do_action_ref_array( 'bp_core_setup_oembed', array( &$this ) ); 
  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 & and we need to undo this 
  86. // See https://core.trac.wordpress.org/ticket/11311. 
  87. $url = str_replace( '&', '&', $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. * Filters the embed object ID. 
  109. * 
  110. * @since 1.5.0 
  111. * 
  112. * @param int $value Value of zero. 
  113. */ 
  114. $id = apply_filters( 'embed_post_id', 0 ); 
  115.  
  116. $unfiltered_html = current_user_can( 'unfiltered_html' ); 
  117. $default_discovery = false; 
  118.  
  119. // Since 4.4, WordPress is now an oEmbed provider. 
  120. if ( function_exists( 'wp_oembed_register_route' ) ) { 
  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. require_once( ABSPATH . WPINC . '/class-oembed.php' ); 
  137. $oembed_obj = _wp_oembed_get_object(); 
  138.  
  139. // If oEmbed discovery is true, skip oEmbed provider check. 
  140. $is_oembed_link = false; 
  141. if ( !$attr['discover'] ) { 
  142. foreach ( (array) $oembed_obj->providers as $provider_matchmask => $provider ) { 
  143. $regex = ( $is_regex = $provider[1] ) ? $provider_matchmask : '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $provider_matchmask ), '#' ) ) . '#i'; 
  144.  
  145. if ( preg_match( $regex, $url ) ) 
  146. $is_oembed_link = true; 
  147.  
  148. // If url doesn't match a WP oEmbed provider, stop parsing. 
  149. if ( !$is_oembed_link ) 
  150. return $this->maybe_make_link( $url ); 
  151.  
  152. return $this->parse_oembed( $id, $url, $attr, $rawattr ); 
  153.  
  154. /** 
  155. * Base function so BP components/plugins can parse links to be embedded. 
  156. * 
  157. * View an example to add support in {@link bp_activity_embed()}. 
  158. * 
  159. * on success. 
  160. * oEmbed failure. 
  161. * 
  162. * @param int $id ID to do the caching for. 
  163. * @param string $url The URL attempting to be embedded. 
  164. * @param array $attr Shortcode attributes from {@link WP_Embed::shortcode()}. 
  165. * @param array $rawattr Untouched shortcode attributes from 
  166. * {@link WP_Embed::shortcode()}. 
  167. * @return string The embed HTML on success, otherwise the original URL. 
  168. */ 
  169. public function parse_oembed( $id, $url, $attr, $rawattr ) { 
  170. $id = intval( $id ); 
  171.  
  172. if ( $id ) { 
  173. // Setup the cachekey. 
  174. $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); 
  175.  
  176. // Let components / plugins grab their cache. 
  177. $cache = ''; 
  178.  
  179. /** 
  180. * Filters the cache value to be used in the oEmbed, if exists. 
  181. * 
  182. * @since 1.5.0 
  183. * 
  184. * @param string $cache Empty initial cache value. 
  185. * @param int $id ID that the caching is for. 
  186. * @param string $cachekey Key to use for the caching in the database. 
  187. * @param string $url The URL attempting to be embedded. 
  188. * @param array $attr Parsed shortcode attributes. 
  189. * @param array $rawattr Unparsed shortcode attributes. 
  190. */ 
  191. $cache = apply_filters( 'bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr ); 
  192.  
  193. // Grab cache and return it if available. 
  194. if ( !empty( $cache ) ) { 
  195.  
  196. /** 
  197. * Filters the found cache for the provided URL. 
  198. * 
  199. * @since 1.5.0 
  200. * 
  201. * @param string $cache Cached HTML markup for embed. 
  202. * @param string $url The URL being embedded. 
  203. * @param array $attr Parsed shortcode attributes. 
  204. * @param array $rawattr Unparased shortcode attributes. 
  205. */ 
  206. return apply_filters( 'bp_embed_oembed_html', $cache, $url, $attr, $rawattr ); 
  207.  
  208. // If no cache, ping the oEmbed provider and cache the result. 
  209. } else { 
  210. $html = wp_oembed_get( $url, $attr ); 
  211. $cache = ( $html ) ? $html : $url; 
  212.  
  213. /** 
  214. * Fires if there is no existing cache and triggers cache setting. 
  215. * 
  216. * Lets components / plugins save their cache. 
  217. * 
  218. * @since 1.5.0 
  219. * 
  220. * @param string $cache Newly cached HTML markup for embed. 
  221. * @param string $cachekey Key to use for the caching in the database. 
  222. * @param int $id ID to do the caching for. 
  223. */ 
  224. do_action( 'bp_embed_update_cache', $cache, $cachekey, $id ); 
  225.  
  226. // If there was a result, return it. 
  227. if ( $html ) { 
  228.  
  229. /** This filter is documented in bp-core/classes/class-bp-embed.php */ 
  230. return apply_filters( 'bp_embed_oembed_html', $html, $url, $attr, $rawattr ); 
  231.  
  232. // Still unknown. 
  233. return $this->maybe_make_link( $url ); 
.