PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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