3 * API for easily embedding rich media such as videos and images into content.
10 var $handlers = array();
13 var $linkifunknown = true;
18 function __construct() {
19 // Hack to get the [embed] shortcode to run before wpautop()
20 add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
22 // Shortcode placeholder for strip_shortcodes()
23 add_shortcode( 'embed', '__return_false' );
25 // Attempts to embed all URLs in a post
26 add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
28 // When a post is saved, invalidate the oEmbed cache
29 add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) );
31 // After a post is saved, cache oEmbed items via AJAX
32 add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
36 * Process the [embed] shortcode.
38 * Since the [embed] shortcode needs to be run earlier than other shortcodes,
39 * this function removes all existing shortcodes, registers the [embed] shortcode,
40 * calls {@link do_shortcode()}, and then re-registers the old shortcodes.
42 * @uses $shortcode_tags
43 * @uses remove_all_shortcodes()
44 * @uses add_shortcode()
45 * @uses do_shortcode()
47 * @param string $content Content to parse
48 * @return string Content with shortcode parsed
50 function run_shortcode( $content ) {
51 global $shortcode_tags;
53 // Back up current registered shortcodes and clear them all out
54 $orig_shortcode_tags = $shortcode_tags;
55 remove_all_shortcodes();
57 add_shortcode( 'embed', array( $this, 'shortcode' ) );
59 // Do the shortcode (only the [embed] one is registered)
60 $content = do_shortcode( $content );
62 // Put the original shortcodes back
63 $shortcode_tags = $orig_shortcode_tags;
69 * If a post/page was saved, then output JavaScript to make
70 * an AJAX request that will call WP_Embed::cache_oembed().
72 function maybe_run_ajax_cache() {
75 if ( ! $post || empty($_GET['message']) || 1 != $_GET['message'] )
79 <script type="text/javascript">
81 jQuery(document).ready(function($){
82 $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
90 * Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.
91 * This function should probably also only be used for sites that do not support oEmbed.
93 * @param string $id An internal ID/name for the handler. Needs to be unique.
94 * @param string $regex The regex that will be used to see if this handler should be used for a URL.
95 * @param callback $callback The callback function that will be called if the regex is matched.
96 * @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.
98 function register_handler( $id, $regex, $callback, $priority = 10 ) {
99 $this->handlers[$priority][$id] = array(
101 'callback' => $callback,
106 * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
108 * @param string $id The handler ID that should be removed.
109 * @param int $priority Optional. The priority of the handler to be removed (default: 10).
111 function unregister_handler( $id, $priority = 10 ) {
112 if ( isset($this->handlers[$priority][$id]) )
113 unset($this->handlers[$priority][$id]);
117 * The {@link do_shortcode()} callback function.
119 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
120 * If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.
122 * @uses wp_oembed_get()
123 * @uses wp_parse_args()
124 * @uses wp_embed_defaults()
125 * @uses WP_Embed::maybe_make_link()
128 * @uses wp_cache_get()
129 * @uses wp_cache_set()
130 * @uses get_post_meta()
131 * @uses update_post_meta()
133 * @param array $attr Shortcode attributes.
134 * @param string $url The URL attempting to be embedded.
135 * @return string The embed HTML on success, otherwise the original URL.
137 function shortcode( $attr, $url = '' ) {
144 $attr = wp_parse_args( $attr, wp_embed_defaults() );
146 // kses converts & into & and we need to undo this
147 // See http://core.trac.wordpress.org/ticket/11311
148 $url = str_replace( '&', '&', $url );
150 // Look for known internal handlers
151 ksort( $this->handlers );
152 foreach ( $this->handlers as $priority => $handlers ) {
153 foreach ( $handlers as $id => $handler ) {
154 if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
155 if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
156 return apply_filters( 'embed_handler_html', $return, $url, $attr );
161 $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
162 if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
163 $post_ID = $this->post_ID;
165 // Unknown URL format. Let oEmbed have a go.
168 // Check for a cached result (stored in the post meta)
169 $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
170 if ( $this->usecache ) {
171 $cache = get_post_meta( $post_ID, $cachekey, true );
173 // Failures are cached
174 if ( '{{unknown}}' === $cache )
175 return $this->maybe_make_link( $url );
177 if ( ! empty( $cache ) )
178 return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
181 // Use oEmbed to get the HTML
182 $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) );
183 $html = wp_oembed_get( $url, $attr );
186 $cache = ( $html ) ? $html : '{{unknown}}';
187 update_post_meta( $post_ID, $cachekey, $cache );
189 // If there was a result, return it
191 return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
195 return $this->maybe_make_link( $url );
199 * Delete all oEmbed caches.
201 * @param int $post_ID Post ID to delete the caches for.
203 function delete_oembed_caches( $post_ID ) {
204 $post_metas = get_post_custom_keys( $post_ID );
205 if ( empty($post_metas) )
208 foreach( $post_metas as $post_meta_key ) {
209 if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
210 delete_post_meta( $post_ID, $post_meta_key );
215 * Triggers a caching of all oEmbed results.
217 * @param int $post_ID Post ID to do the caching for.
219 function cache_oembed( $post_ID ) {
220 $post = get_post( $post_ID );
222 if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
226 if ( !empty($post->post_content) ) {
227 $this->post_ID = $post->ID;
228 $this->usecache = false;
230 $content = $this->run_shortcode( $post->post_content );
231 $this->autoembed( $content );
233 $this->usecache = true;
238 * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
240 * @uses WP_Embed::autoembed_callback()
242 * @param string $content The content to be searched.
243 * @return string Potentially modified $content.
245 function autoembed( $content ) {
246 return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
250 * Callback function for {@link WP_Embed::autoembed()}.
252 * @uses WP_Embed::shortcode()
254 * @param array $match A regex match array.
255 * @return string The embed HTML on success, otherwise the original URL.
257 function autoembed_callback( $match ) {
258 $oldval = $this->linkifunknown;
259 $this->linkifunknown = false;
260 $return = $this->shortcode( array(), $match[1] );
261 $this->linkifunknown = $oldval;
263 return "\n$return\n";
267 * Conditionally makes a hyperlink based on an internal class variable.
269 * @param string $url URL to potentially be linked.
270 * @return string Linked URL or the original URL.
272 function maybe_make_link( $url ) {
273 $output = ( $this->linkifunknown ) ? '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>' : $url;
274 return apply_filters( 'embed_maybe_make_link', $output, $url );
277 $GLOBALS['wp_embed'] = new WP_Embed();