PageRenderTime 55ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 498 lines | 241 code | 66 blank | 191 comment | 35 complexity | 651a4cba7f2365d353d5b7eafd5bc695 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. * @var bool
  23. */
  24. public $return_false_on_fail = false;
  25. /**
  26. * Constructor
  27. */
  28. public function __construct() {
  29. // Hack to get the [embed] shortcode to run before wpautop().
  30. add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
  31. add_filter( 'widget_text_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. add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
  37. // After a post is saved, cache oEmbed items via Ajax.
  38. add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
  39. add_action( 'edit_page_form', array( $this, 'maybe_run_ajax_cache' ) );
  40. }
  41. /**
  42. * Process the [embed] shortcode.
  43. *
  44. * Since the [embed] shortcode needs to be run earlier than other shortcodes,
  45. * this function removes all existing shortcodes, registers the [embed] shortcode,
  46. * calls do_shortcode(), and then re-registers the old shortcodes.
  47. *
  48. * @global array $shortcode_tags
  49. *
  50. * @param string $content Content to parse
  51. * @return string Content with shortcode parsed
  52. */
  53. public function run_shortcode( $content ) {
  54. global $shortcode_tags;
  55. // Back up current registered shortcodes and clear them all out.
  56. $orig_shortcode_tags = $shortcode_tags;
  57. remove_all_shortcodes();
  58. add_shortcode( 'embed', array( $this, 'shortcode' ) );
  59. // Do the shortcode (only the [embed] one is registered).
  60. $content = do_shortcode( $content, true );
  61. // Put the original shortcodes back.
  62. $shortcode_tags = $orig_shortcode_tags;
  63. return $content;
  64. }
  65. /**
  66. * If a post/page was saved, then output JavaScript to make
  67. * an Ajax request that will call WP_Embed::cache_oembed().
  68. */
  69. public function maybe_run_ajax_cache() {
  70. $post = get_post();
  71. if ( ! $post || empty( $_GET['message'] ) ) {
  72. return;
  73. }
  74. ?>
  75. <script type="text/javascript">
  76. jQuery(document).ready(function($){
  77. $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
  78. });
  79. </script>
  80. <?php
  81. }
  82. /**
  83. * Registers an embed handler.
  84. *
  85. * Do not use this function directly, use wp_embed_register_handler() instead.
  86. *
  87. * This function should probably also only be used for sites that do not support oEmbed.
  88. *
  89. * @param string $id An internal ID/name for the handler. Needs to be unique.
  90. * @param string $regex The regex that will be used to see if this handler should be used for a URL.
  91. * @param callable $callback The callback function that will be called if the regex is matched.
  92. * @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.
  93. */
  94. public function register_handler( $id, $regex, $callback, $priority = 10 ) {
  95. $this->handlers[ $priority ][ $id ] = array(
  96. 'regex' => $regex,
  97. 'callback' => $callback,
  98. );
  99. }
  100. /**
  101. * Unregisters a previously-registered embed handler.
  102. *
  103. * Do not use this function directly, use wp_embed_unregister_handler() instead.
  104. *
  105. * @param string $id The handler ID that should be removed.
  106. * @param int $priority Optional. The priority of the handler to be removed (default: 10).
  107. */
  108. public function unregister_handler( $id, $priority = 10 ) {
  109. unset( $this->handlers[ $priority ][ $id ] );
  110. }
  111. /**
  112. * The do_shortcode() callback function.
  113. *
  114. * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of
  115. * the registered embed handlers. If none of the regex matches and it's enabled, then the URL
  116. * will be given to the WP_oEmbed class.
  117. *
  118. * @param array $attr {
  119. * Shortcode attributes. Optional.
  120. *
  121. * @type int $width Width of the embed in pixels.
  122. * @type int $height Height of the embed in pixels.
  123. * }
  124. * @param string $url The URL attempting to be embedded.
  125. * @return string|false The embed HTML on success, otherwise the original URL.
  126. * `->maybe_make_link()` can return false on failure.
  127. */
  128. public function shortcode( $attr, $url = '' ) {
  129. $post = get_post();
  130. if ( empty( $url ) && ! empty( $attr['src'] ) ) {
  131. $url = $attr['src'];
  132. }
  133. $this->last_url = $url;
  134. if ( empty( $url ) ) {
  135. $this->last_attr = $attr;
  136. return '';
  137. }
  138. $rawattr = $attr;
  139. $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) );
  140. $this->last_attr = $attr;
  141. // KSES converts & into &amp; and we need to undo this.
  142. // See https://core.trac.wordpress.org/ticket/11311
  143. $url = str_replace( '&amp;', '&', $url );
  144. // Look for known internal handlers.
  145. ksort( $this->handlers );
  146. foreach ( $this->handlers as $priority => $handlers ) {
  147. foreach ( $handlers as $id => $handler ) {
  148. if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
  149. $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr );
  150. if ( false !== $return ) {
  151. /**
  152. * Filters the returned embed HTML.
  153. *
  154. * @since 2.9.0
  155. *
  156. * @see WP_Embed::shortcode()
  157. *
  158. * @param string|false $return The HTML result of the shortcode, or false on failure.
  159. * @param string $url The embed URL.
  160. * @param array $attr An array of shortcode attributes.
  161. */
  162. return apply_filters( 'embed_handler_html', $return, $url, $attr );
  163. }
  164. }
  165. }
  166. }
  167. $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
  168. // Potentially set by WP_Embed::cache_oembed().
  169. if ( ! empty( $this->post_ID ) ) {
  170. $post_ID = $this->post_ID;
  171. }
  172. // Check for a cached result (stored as custom post or in the post meta).
  173. $key_suffix = md5( $url . serialize( $attr ) );
  174. $cachekey = '_oembed_' . $key_suffix;
  175. $cachekey_time = '_oembed_time_' . $key_suffix;
  176. /**
  177. * Filters the oEmbed TTL value (time to live).
  178. *
  179. * @since 4.0.0
  180. *
  181. * @param int $time Time to live (in seconds).
  182. * @param string $url The attempted embed URL.
  183. * @param array $attr An array of shortcode attributes.
  184. * @param int $post_ID Post ID.
  185. */
  186. $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );
  187. $cache = '';
  188. $cache_time = 0;
  189. $cached_post_id = $this->find_oembed_post_id( $key_suffix );
  190. if ( $post_ID ) {
  191. $cache = get_post_meta( $post_ID, $cachekey, true );
  192. $cache_time = get_post_meta( $post_ID, $cachekey_time, true );
  193. if ( ! $cache_time ) {
  194. $cache_time = 0;
  195. }
  196. } elseif ( $cached_post_id ) {
  197. $cached_post = get_post( $cached_post_id );
  198. $cache = $cached_post->post_content;
  199. $cache_time = strtotime( $cached_post->post_modified_gmt );
  200. }
  201. $cached_recently = ( time() - $cache_time ) < $ttl;
  202. if ( $this->usecache || $cached_recently ) {
  203. // Failures are cached. Serve one if we're using the cache.
  204. if ( '{{unknown}}' === $cache ) {
  205. return $this->maybe_make_link( $url );
  206. }
  207. if ( ! empty( $cache ) ) {
  208. /**
  209. * Filters the cached oEmbed HTML.
  210. *
  211. * @since 2.9.0
  212. *
  213. * @see WP_Embed::shortcode()
  214. *
  215. * @param string|false $cache The cached HTML result, stored in post meta.
  216. * @param string $url The attempted embed URL.
  217. * @param array $attr An array of shortcode attributes.
  218. * @param int $post_ID Post ID.
  219. */
  220. return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
  221. }
  222. }
  223. /**
  224. * Filters whether to inspect the given URL for discoverable link tags.
  225. *
  226. * @since 2.9.0
  227. * @since 4.4.0 The default value changed to true.
  228. *
  229. * @see WP_oEmbed::discover()
  230. *
  231. * @param bool $enable Whether to enable `<link>` tag discovery. Default true.
  232. */
  233. $attr['discover'] = apply_filters( 'embed_oembed_discover', true );
  234. // Use oEmbed to get the HTML.
  235. $html = wp_oembed_get( $url, $attr );
  236. if ( $post_ID ) {
  237. if ( $html ) {
  238. update_post_meta( $post_ID, $cachekey, $html );
  239. update_post_meta( $post_ID, $cachekey_time, time() );
  240. } elseif ( ! $cache ) {
  241. update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
  242. }
  243. } else {
  244. $has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
  245. if ( $has_kses ) {
  246. // Prevent KSES from corrupting JSON in post_content.
  247. kses_remove_filters();
  248. }
  249. $insert_post_args = array(
  250. 'post_name' => $key_suffix,
  251. 'post_status' => 'publish',
  252. 'post_type' => 'oembed_cache',
  253. );
  254. if ( $html ) {
  255. if ( $cached_post_id ) {
  256. wp_update_post(
  257. wp_slash(
  258. array(
  259. 'ID' => $cached_post_id,
  260. 'post_content' => $html,
  261. )
  262. )
  263. );
  264. } else {
  265. wp_insert_post(
  266. wp_slash(
  267. array_merge(
  268. $insert_post_args,
  269. array(
  270. 'post_content' => $html,
  271. )
  272. )
  273. )
  274. );
  275. }
  276. } elseif ( ! $cache ) {
  277. wp_insert_post(
  278. wp_slash(
  279. array_merge(
  280. $insert_post_args,
  281. array(
  282. 'post_content' => '{{unknown}}',
  283. )
  284. )
  285. )
  286. );
  287. }
  288. if ( $has_kses ) {
  289. kses_init_filters();
  290. }
  291. }
  292. // If there was a result, return it.
  293. if ( $html ) {
  294. /** This filter is documented in wp-includes/class-wp-embed.php */
  295. return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
  296. }
  297. // Still unknown.
  298. return $this->maybe_make_link( $url );
  299. }
  300. /**
  301. * Delete all oEmbed caches. Unused by core as of 4.0.0.
  302. *
  303. * @param int $post_ID Post ID to delete the caches for.
  304. */
  305. public function delete_oembed_caches( $post_ID ) {
  306. $post_metas = get_post_custom_keys( $post_ID );
  307. if ( empty( $post_metas ) ) {
  308. return;
  309. }
  310. foreach ( $post_metas as $post_meta_key ) {
  311. if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) {
  312. delete_post_meta( $post_ID, $post_meta_key );
  313. }
  314. }
  315. }
  316. /**
  317. * Triggers a caching of all oEmbed results.
  318. *
  319. * @param int $post_ID Post ID to do the caching for.
  320. */
  321. public function cache_oembed( $post_ID ) {
  322. $post = get_post( $post_ID );
  323. $post_types = get_post_types( array( 'show_ui' => true ) );
  324. /**
  325. * Filters the array of post types to cache oEmbed results for.
  326. *
  327. * @since 2.9.0
  328. *
  329. * @param string[] $post_types Array of post type names to cache oEmbed results for. Defaults to post types with `show_ui` set to true.
  330. */
  331. if ( empty( $post->ID ) || ! in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', $post_types ) ) ) {
  332. return;
  333. }
  334. // Trigger a caching.
  335. if ( ! empty( $post->post_content ) ) {
  336. $this->post_ID = $post->ID;
  337. $this->usecache = false;
  338. $content = $this->run_shortcode( $post->post_content );
  339. $this->autoembed( $content );
  340. $this->usecache = true;
  341. }
  342. }
  343. /**
  344. * Passes any unlinked URLs that are on their own line to WP_Embed::shortcode() for potential embedding.
  345. *
  346. * @see WP_Embed::autoembed_callback()
  347. *
  348. * @param string $content The content to be searched.
  349. * @return string Potentially modified $content.
  350. */
  351. public function autoembed( $content ) {
  352. // Replace line breaks from all HTML elements with placeholders.
  353. $content = wp_replace_in_html_tags( $content, array( "\n" => '<!-- wp-line-break -->' ) );
  354. if ( preg_match( '#(^|\s|>)https?://#i', $content ) ) {
  355. // Find URLs on their own line.
  356. $content = preg_replace_callback( '|^(\s*)(https?://[^\s<>"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
  357. // Find URLs in their own paragraph.
  358. $content = preg_replace_callback( '|(<p(?: [^>]*)?>\s*)(https?://[^\s<>"]+)(\s*<\/p>)|i', array( $this, 'autoembed_callback' ), $content );
  359. }
  360. // Put the line breaks back.
  361. return str_replace( '<!-- wp-line-break -->', "\n", $content );
  362. }
  363. /**
  364. * Callback function for WP_Embed::autoembed().
  365. *
  366. * @param array $match A regex match array.
  367. * @return string The embed HTML on success, otherwise the original URL.
  368. */
  369. public function autoembed_callback( $match ) {
  370. $oldval = $this->linkifunknown;
  371. $this->linkifunknown = false;
  372. $return = $this->shortcode( array(), $match[2] );
  373. $this->linkifunknown = $oldval;
  374. return $match[1] . $return . $match[3];
  375. }
  376. /**
  377. * Conditionally makes a hyperlink based on an internal class variable.
  378. *
  379. * @param string $url URL to potentially be linked.
  380. * @return string|false Linked URL or the original URL. False if 'return_false_on_fail' is true.
  381. */
  382. public function maybe_make_link( $url ) {
  383. if ( $this->return_false_on_fail ) {
  384. return false;
  385. }
  386. $output = ( $this->linkifunknown ) ? '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>' : $url;
  387. /**
  388. * Filters the returned, maybe-linked embed URL.
  389. *
  390. * @since 2.9.0
  391. *
  392. * @param string $output The linked or original URL.
  393. * @param string $url The original URL.
  394. */
  395. return apply_filters( 'embed_maybe_make_link', $output, $url );
  396. }
  397. /**
  398. * Find the oEmbed cache post ID for a given cache key.
  399. *
  400. * @since 4.9.0
  401. *
  402. * @param string $cache_key oEmbed cache key.
  403. * @return int|null Post ID on success, null on failure.
  404. */
  405. public function find_oembed_post_id( $cache_key ) {
  406. $cache_group = 'oembed_cache_post';
  407. $oembed_post_id = wp_cache_get( $cache_key, $cache_group );
  408. if ( $oembed_post_id && 'oembed_cache' === get_post_type( $oembed_post_id ) ) {
  409. return $oembed_post_id;
  410. }
  411. $oembed_post_query = new WP_Query(
  412. array(
  413. 'post_type' => 'oembed_cache',
  414. 'post_status' => 'publish',
  415. 'name' => $cache_key,
  416. 'posts_per_page' => 1,
  417. 'no_found_rows' => true,
  418. 'cache_results' => true,
  419. 'update_post_meta_cache' => false,
  420. 'update_post_term_cache' => false,
  421. 'lazy_load_term_meta' => false,
  422. )
  423. );
  424. if ( ! empty( $oembed_post_query->posts ) ) {
  425. // Note: 'fields' => 'ids' is not being used in order to cache the post object as it will be needed.
  426. $oembed_post_id = $oembed_post_query->posts[0]->ID;
  427. wp_cache_set( $cache_key, $oembed_post_id, $cache_group );
  428. return $oembed_post_id;
  429. }
  430. return null;
  431. }
  432. }