PageRenderTime 28ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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