PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-embed.php

https://gitlab.com/math4youbyusgroupillinois/WordPress
PHP | 356 lines | 140 code | 49 blank | 167 comment | 26 complexity | d68b254a70c41208a6994bff630bb772 MD5 | raw file
  1. <?php
  2. /**
  3. * API for easily embedding rich media such as videos and images into content.
  4. *
  5. * @package WordPress
  6. * @subpackage Embed
  7. * @since 2.9.0
  8. */
  9. class WP_Embed {
  10. public $handlers = array();
  11. public $post_ID;
  12. public $usecache = true;
  13. public $linkifunknown = true;
  14. /**
  15. * When an URL cannot be embedded, return false instead of returning a link
  16. * or the URL. Bypasses the 'embed_maybe_make_link' filter.
  17. */
  18. public $return_false_on_fail = false;
  19. /**
  20. * Constructor
  21. */
  22. public function __construct() {
  23. // Hack to get the [embed] shortcode to run before wpautop()
  24. add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
  25. // Shortcode placeholder for strip_shortcodes()
  26. add_shortcode( 'embed', '__return_false' );
  27. // Attempts to embed all URLs in a post
  28. add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
  29. // After a post is saved, cache oEmbed items via AJAX
  30. add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
  31. }
  32. /**
  33. * Process the [embed] shortcode.
  34. *
  35. * Since the [embed] shortcode needs to be run earlier than other shortcodes,
  36. * this function removes all existing shortcodes, registers the [embed] shortcode,
  37. * calls {@link do_shortcode()}, and then re-registers the old shortcodes.
  38. *
  39. * @uses $shortcode_tags
  40. *
  41. * @param string $content Content to parse
  42. * @return string Content with shortcode parsed
  43. */
  44. public function run_shortcode( $content ) {
  45. global $shortcode_tags;
  46. // Back up current registered shortcodes and clear them all out
  47. $orig_shortcode_tags = $shortcode_tags;
  48. remove_all_shortcodes();
  49. add_shortcode( 'embed', array( $this, 'shortcode' ) );
  50. // Do the shortcode (only the [embed] one is registered)
  51. $content = do_shortcode( $content );
  52. // Put the original shortcodes back
  53. $shortcode_tags = $orig_shortcode_tags;
  54. return $content;
  55. }
  56. /**
  57. * If a post/page was saved, then output JavaScript to make
  58. * an AJAX request that will call WP_Embed::cache_oembed().
  59. */
  60. public function maybe_run_ajax_cache() {
  61. $post = get_post();
  62. if ( ! $post || empty( $_GET['message'] ) )
  63. return;
  64. ?>
  65. <script type="text/javascript">
  66. jQuery(document).ready(function($){
  67. $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
  68. });
  69. </script>
  70. <?php
  71. }
  72. /**
  73. * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.
  74. * This function should probably also only be used for sites that do not support oEmbed.
  75. *
  76. * @param string $id An internal ID/name for the handler. Needs to be unique.
  77. * @param string $regex The regex that will be used to see if this handler should be used for a URL.
  78. * @param callback $callback The callback function that will be called if the regex is matched.
  79. * @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action.
  80. */
  81. public function register_handler( $id, $regex, $callback, $priority = 10 ) {
  82. $this->handlers[$priority][$id] = array(
  83. 'regex' => $regex,
  84. 'callback' => $callback,
  85. );
  86. }
  87. /**
  88. * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
  89. *
  90. * @param string $id The handler ID that should be removed.
  91. * @param int $priority Optional. The priority of the handler to be removed (default: 10).
  92. */
  93. public function unregister_handler( $id, $priority = 10 ) {
  94. if ( isset($this->handlers[$priority][$id]) )
  95. unset($this->handlers[$priority][$id]);
  96. }
  97. /**
  98. * The {@link do_shortcode()} callback function.
  99. *
  100. * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
  101. * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.
  102. *
  103. * @param array $attr {
  104. * Shortcode attributes. Optional.
  105. *
  106. * @type int $width Width of the embed in pixels.
  107. * @type int $height Height of the embed in pixels.
  108. * }
  109. * @param string $url The URL attempting to be embedded.
  110. * @return string|false The embed HTML on success, otherwise the original URL.
  111. * `->maybe_make_link()` can return false on failure.
  112. */
  113. public function shortcode( $attr, $url = '' ) {
  114. $post = get_post();
  115. if ( empty( $url ) && ! empty( $attr['src'] ) ) {
  116. $url = $attr['src'];
  117. }
  118. if ( empty( $url ) )
  119. return '';
  120. $rawattr = $attr;
  121. $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) );
  122. // kses converts & into &amp; and we need to undo this
  123. // See https://core.trac.wordpress.org/ticket/11311
  124. $url = str_replace( '&amp;', '&', $url );
  125. // Look for known internal handlers
  126. ksort( $this->handlers );
  127. foreach ( $this->handlers as $priority => $handlers ) {
  128. foreach ( $handlers as $id => $handler ) {
  129. if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
  130. if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
  131. /**
  132. * Filter the returned embed handler.
  133. *
  134. * @since 2.9.0
  135. *
  136. * @see WP_Embed::shortcode()
  137. *
  138. * @param mixed $return The shortcode callback function to call.
  139. * @param string $url The attempted embed URL.
  140. * @param array $attr An array of shortcode attributes.
  141. */
  142. return apply_filters( 'embed_handler_html', $return, $url, $attr );
  143. }
  144. }
  145. }
  146. $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
  147. if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
  148. $post_ID = $this->post_ID;
  149. // Unknown URL format. Let oEmbed have a go.
  150. if ( $post_ID ) {
  151. // Check for a cached result (stored in the post meta)
  152. $key_suffix = md5( $url . serialize( $attr ) );
  153. $cachekey = '_oembed_' . $key_suffix;
  154. $cachekey_time = '_oembed_time_' . $key_suffix;
  155. /**
  156. * Filter the oEmbed TTL value (time to live).
  157. *
  158. * @since 4.0.0
  159. *
  160. * @param int $time Time to live (in seconds).
  161. * @param string $url The attempted embed URL.
  162. * @param array $attr An array of shortcode attributes.
  163. * @param int $post_ID Post ID.
  164. */
  165. $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
  166. $cache = get_post_meta( $post_ID, $cachekey, true );
  167. $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
  168. if ( ! $cache_time ) {
  169. $cache_time = 0;
  170. }
  171. $cached_recently = ( time() - $cache_time ) < $ttl;
  172. if ( $this->usecache || $cached_recently ) {
  173. // Failures are cached. Serve one if we're using the cache.
  174. if ( '{{unknown}}' === $cache )
  175. return $this->maybe_make_link( $url );
  176. if ( ! empty( $cache ) ) {
  177. /**
  178. * Filter the cached oEmbed HTML.
  179. *
  180. * @since 2.9.0
  181. *
  182. * @see WP_Embed::shortcode()
  183. *
  184. * @param mixed $cache The cached HTML result, stored in post meta.
  185. * @param string $url The attempted embed URL.
  186. * @param array $attr An array of shortcode attributes.
  187. * @param int $post_ID Post ID.
  188. */
  189. return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
  190. }
  191. }
  192. /**
  193. * Filter whether to inspect the given URL for discoverable link tags.
  194. *
  195. * @since 2.9.0
  196. *
  197. * @see WP_oEmbed::discover()
  198. *
  199. * @param bool $enable Whether to enable `<link>` tag discovery. Default false.
  200. */
  201. $attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( $post_ID, 'unfiltered_html' ) );
  202. // Use oEmbed to get the HTML
  203. $html = wp_oembed_get( $url, $attr );
  204. // Maybe cache the result
  205. if ( $html ) {
  206. update_post_meta( $post_ID, $cachekey, $html );
  207. update_post_meta( $post_ID, $cachekey_time, time() );
  208. } elseif ( ! $cache ) {
  209. update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
  210. }
  211. // If there was a result, return it
  212. if ( $html ) {
  213. /** This filter is documented in wp-includes/class-wp-embed.php */
  214. return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
  215. }
  216. }
  217. // Still unknown
  218. return $this->maybe_make_link( $url );
  219. }
  220. /**
  221. * Delete all oEmbed caches. Unused by core as of 4.0.0.
  222. *
  223. * @param int $post_ID Post ID to delete the caches for.
  224. */
  225. public function delete_oembed_caches( $post_ID ) {
  226. $post_metas = get_post_custom_keys( $post_ID );
  227. if ( empty($post_metas) )
  228. return;
  229. foreach( $post_metas as $post_meta_key ) {
  230. if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
  231. delete_post_meta( $post_ID, $post_meta_key );
  232. }
  233. }
  234. /**
  235. * Triggers a caching of all oEmbed results.
  236. *
  237. * @param int $post_ID Post ID to do the caching for.
  238. */
  239. public function cache_oembed( $post_ID ) {
  240. $post = get_post( $post_ID );
  241. $post_types = get_post_types( array( 'show_ui' => true ) );
  242. /**
  243. * Filter the array of post types to cache oEmbed results for.
  244. *
  245. * @since 2.9.0
  246. *
  247. * @param array $post_types Array of post types to cache oEmbed results for. Defaults to post types with `show_ui` set to true.
  248. */
  249. if ( empty( $post->ID ) || ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ) ) ){
  250. return;
  251. }
  252. // Trigger a caching
  253. if ( ! empty( $post->post_content ) ) {
  254. $this->post_ID = $post->ID;
  255. $this->usecache = false;
  256. $content = $this->run_shortcode( $post->post_content );
  257. $this->autoembed( $content );
  258. $this->usecache = true;
  259. }
  260. }
  261. /**
  262. * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
  263. *
  264. * @uses WP_Embed::autoembed_callback()
  265. *
  266. * @param string $content The content to be searched.
  267. * @return string Potentially modified $content.
  268. */
  269. public function autoembed( $content ) {
  270. return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
  271. }
  272. /**
  273. * Callback function for {@link WP_Embed::autoembed()}.
  274. *
  275. * @param array $match A regex match array.
  276. * @return string The embed HTML on success, otherwise the original URL.
  277. */
  278. public function autoembed_callback( $match ) {
  279. $oldval = $this->linkifunknown;
  280. $this->linkifunknown = false;
  281. $return = $this->shortcode( array(), $match[2] );
  282. $this->linkifunknown = $oldval;
  283. return $match[1] . $return . $match[3];
  284. }
  285. /**
  286. * Conditionally makes a hyperlink based on an internal class variable.
  287. *
  288. * @param string $url URL to potentially be linked.
  289. * @return false|string Linked URL or the original URL. False if 'return_false_on_fail' is true.
  290. */
  291. public function maybe_make_link( $url ) {
  292. if ( $this->return_false_on_fail ) {
  293. return false;
  294. }
  295. $output = ( $this->linkifunknown ) ? '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>' : $url;
  296. /**
  297. * Filter the returned, maybe-linked embed URL.
  298. *
  299. * @since 2.9.0
  300. *
  301. * @param string $output The linked or original URL.
  302. * @param string $url The original URL.
  303. */
  304. return apply_filters( 'embed_maybe_make_link', $output, $url );
  305. }
  306. }
  307. $GLOBALS['wp_embed'] = new WP_Embed();