]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-embed.php
Wordpress 3.5.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-embed.php
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         /**
16          * Constructor
17          */
18         function __construct() {
19                 // Hack to get the [embed] shortcode to run before wpautop()
20                 add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
21
22                 // Shortcode placeholder for strip_shortcodes()
23                 add_shortcode( 'embed', '__return_false' );
24
25                 // Attempts to embed all URLs in a post
26                 add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
27
28                 // When a post is saved, invalidate the oEmbed cache
29                 add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) );
30
31                 // After a post is saved, cache oEmbed items via AJAX
32                 add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );
33         }
34
35         /**
36          * Process the [embed] shortcode.
37          *
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.
41          *
42          * @uses $shortcode_tags
43          * @uses remove_all_shortcodes()
44          * @uses add_shortcode()
45          * @uses do_shortcode()
46          *
47          * @param string $content Content to parse
48          * @return string Content with shortcode parsed
49          */
50         function run_shortcode( $content ) {
51                 global $shortcode_tags;
52
53                 // Back up current registered shortcodes and clear them all out
54                 $orig_shortcode_tags = $shortcode_tags;
55                 remove_all_shortcodes();
56
57                 add_shortcode( 'embed', array( $this, 'shortcode' ) );
58
59                 // Do the shortcode (only the [embed] one is registered)
60                 $content = do_shortcode( $content );
61
62                 // Put the original shortcodes back
63                 $shortcode_tags = $orig_shortcode_tags;
64
65                 return $content;
66         }
67
68         /**
69          * If a post/page was saved, then output JavaScript to make
70          * an AJAX request that will call WP_Embed::cache_oembed().
71          */
72         function maybe_run_ajax_cache() {
73                 $post = get_post();
74
75                 if ( ! $post || empty($_GET['message']) || 1 != $_GET['message'] )
76                         return;
77
78 ?>
79 <script type="text/javascript">
80 /* <![CDATA[ */
81         jQuery(document).ready(function($){
82                 $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
83         });
84 /* ]]> */
85 </script>
86 <?php
87         }
88
89         /**
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.
92          *
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.
97          */
98         function register_handler( $id, $regex, $callback, $priority = 10 ) {
99                 $this->handlers[$priority][$id] = array(
100                         'regex'    => $regex,
101                         'callback' => $callback,
102                 );
103         }
104
105         /**
106          * Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
107          *
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).
110          */
111         function unregister_handler( $id, $priority = 10 ) {
112                 if ( isset($this->handlers[$priority][$id]) )
113                         unset($this->handlers[$priority][$id]);
114         }
115
116         /**
117          * The {@link do_shortcode()} callback function.
118          *
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.
121          *
122          * @uses wp_oembed_get()
123          * @uses wp_parse_args()
124          * @uses wp_embed_defaults()
125          * @uses WP_Embed::maybe_make_link()
126          * @uses get_option()
127          * @uses author_can()
128          * @uses wp_cache_get()
129          * @uses wp_cache_set()
130          * @uses get_post_meta()
131          * @uses update_post_meta()
132          *
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.
136          */
137         function shortcode( $attr, $url = '' ) {
138                 $post = get_post();
139
140                 if ( empty( $url ) )
141                         return '';
142
143                 $rawattr = $attr;
144                 $attr = wp_parse_args( $attr, wp_embed_defaults() );
145
146                 // kses converts & into &amp; and we need to undo this
147                 // See http://core.trac.wordpress.org/ticket/11311
148                 $url = str_replace( '&amp;', '&', $url );
149
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 );
157                                 }
158                         }
159                 }
160
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;
164
165                 // Unknown URL format. Let oEmbed have a go.
166                 if ( $post_ID ) {
167
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 );
172
173                                 // Failures are cached
174                                 if ( '{{unknown}}' === $cache )
175                                         return $this->maybe_make_link( $url );
176
177                                 if ( ! empty( $cache ) )
178                                         return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
179                         }
180
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 );
184
185                         // Cache the result
186                         $cache = ( $html ) ? $html : '{{unknown}}';
187                         update_post_meta( $post_ID, $cachekey, $cache );
188
189                         // If there was a result, return it
190                         if ( $html )
191                                 return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
192                 }
193
194                 // Still unknown
195                 return $this->maybe_make_link( $url );
196         }
197
198         /**
199          * Delete all oEmbed caches.
200          *
201          * @param int $post_ID Post ID to delete the caches for.
202          */
203         function delete_oembed_caches( $post_ID ) {
204                 $post_metas = get_post_custom_keys( $post_ID );
205                 if ( empty($post_metas) )
206                         return;
207
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 );
211                 }
212         }
213
214         /**
215          * Triggers a caching of all oEmbed results.
216          *
217          * @param int $post_ID Post ID to do the caching for.
218          */
219         function cache_oembed( $post_ID ) {
220                 $post = get_post( $post_ID );
221
222                 if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
223                         return;
224
225                 // Trigger a caching
226                 if ( !empty($post->post_content) ) {
227                         $this->post_ID = $post->ID;
228                         $this->usecache = false;
229
230                         $content = $this->run_shortcode( $post->post_content );
231                         $this->autoembed( $content );
232
233                         $this->usecache = true;
234                 }
235         }
236
237         /**
238          * Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
239          *
240          * @uses WP_Embed::autoembed_callback()
241          *
242          * @param string $content The content to be searched.
243          * @return string Potentially modified $content.
244          */
245         function autoembed( $content ) {
246                 return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
247         }
248
249         /**
250          * Callback function for {@link WP_Embed::autoembed()}.
251          *
252          * @uses WP_Embed::shortcode()
253          *
254          * @param array $match A regex match array.
255          * @return string The embed HTML on success, otherwise the original URL.
256          */
257         function autoembed_callback( $match ) {
258                 $oldval = $this->linkifunknown;
259                 $this->linkifunknown = false;
260                 $return = $this->shortcode( array(), $match[1] );
261                 $this->linkifunknown = $oldval;
262
263                 return "\n$return\n";
264         }
265
266         /**
267          * Conditionally makes a hyperlink based on an internal class variable.
268          *
269          * @param string $url URL to potentially be linked.
270          * @return string Linked URL or the original URL.
271          */
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 );
275         }
276 }
277 $GLOBALS['wp_embed'] = new WP_Embed();