PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dkrzos/phc
PHP | 277 lines | 118 code | 41 blank | 118 comment | 22 complexity | f428fabdea1ec980ba2f72b03b2222fa MD5 | raw file
Possible License(s): GPL-2.0
  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 Shortcode attributes.
  117. * @param string $url The URL attempting to be embedded.
  118. * @return string The embed HTML on success, otherwise the original URL.
  119. */
  120. function shortcode( $attr, $url = '' ) {
  121. $post = get_post();
  122. if ( empty( $url ) )
  123. return '';
  124. $rawattr = $attr;
  125. $attr = wp_parse_args( $attr, wp_embed_defaults() );
  126. // kses converts & into &amp; and we need to undo this
  127. // See http://core.trac.wordpress.org/ticket/11311
  128. $url = str_replace( '&amp;', '&', $url );
  129. // Look for known internal handlers
  130. ksort( $this->handlers );
  131. foreach ( $this->handlers as $priority => $handlers ) {
  132. foreach ( $handlers as $id => $handler ) {
  133. if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
  134. if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
  135. return apply_filters( 'embed_handler_html', $return, $url, $attr );
  136. }
  137. }
  138. }
  139. $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
  140. if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
  141. $post_ID = $this->post_ID;
  142. // Unknown URL format. Let oEmbed have a go.
  143. if ( $post_ID ) {
  144. // Check for a cached result (stored in the post meta)
  145. $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
  146. if ( $this->usecache ) {
  147. $cache = get_post_meta( $post_ID, $cachekey, true );
  148. // Failures are cached
  149. if ( '{{unknown}}' === $cache )
  150. return $this->maybe_make_link( $url );
  151. if ( ! empty( $cache ) )
  152. return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
  153. }
  154. // Use oEmbed to get the HTML
  155. $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) );
  156. $html = wp_oembed_get( $url, $attr );
  157. // Cache the result
  158. $cache = ( $html ) ? $html : '{{unknown}}';
  159. update_post_meta( $post_ID, $cachekey, $cache );
  160. // If there was a result, return it
  161. if ( $html )
  162. return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
  163. }
  164. // Still unknown
  165. return $this->maybe_make_link( $url );
  166. }
  167. /**
  168. * Delete all oEmbed caches.
  169. *
  170. * @param int $post_ID Post ID to delete the caches for.
  171. */
  172. function delete_oembed_caches( $post_ID ) {
  173. $post_metas = get_post_custom_keys( $post_ID );
  174. if ( empty($post_metas) )
  175. return;
  176. foreach( $post_metas as $post_meta_key ) {
  177. if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
  178. delete_post_meta( $post_ID, $post_meta_key );
  179. }
  180. }
  181. /**
  182. * Triggers a caching of all oEmbed results.
  183. *
  184. * @param int $post_ID Post ID to do the caching for.
  185. */
  186. function cache_oembed( $post_ID ) {
  187. $post = get_post( $post_ID );
  188. if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
  189. return;
  190. // Trigger a caching
  191. if ( !empty($post->post_content) ) {
  192. $this->post_ID = $post->ID;
  193. $this->usecache = false;
  194. $content = $this->run_shortcode( $post->post_content );
  195. $this->autoembed( $content );
  196. $this->usecache = true;
  197. }
  198. }
  199. /**
  200. * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
  201. *
  202. * @uses WP_Embed::autoembed_callback()
  203. *
  204. * @param string $content The content to be searched.
  205. * @return string Potentially modified $content.
  206. */
  207. function autoembed( $content ) {
  208. return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
  209. }
  210. /**
  211. * Callback function for {@link WP_Embed::autoembed()}.
  212. *
  213. * @uses WP_Embed::shortcode()
  214. *
  215. * @param array $match A regex match array.
  216. * @return string The embed HTML on success, otherwise the original URL.
  217. */
  218. function autoembed_callback( $match ) {
  219. $oldval = $this->linkifunknown;
  220. $this->linkifunknown = false;
  221. $return = $this->shortcode( array(), $match[1] );
  222. $this->linkifunknown = $oldval;
  223. return "\n$return\n";
  224. }
  225. /**
  226. * Conditionally makes a hyperlink based on an internal class variable.
  227. *
  228. * @param string $url URL to potentially be linked.
  229. * @return string Linked URL or the original URL.
  230. */
  231. function maybe_make_link( $url ) {
  232. $output = ( $this->linkifunknown ) ? '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>' : $url;
  233. return apply_filters( 'embed_maybe_make_link', $output, $url );
  234. }
  235. }
  236. $GLOBALS['wp_embed'] = new WP_Embed();