WordPress 3.0 beta 1 documentation kindly provided to you by Hay Kranen
| [ Index ] |
PHP Cross Reference of WordPress 3.0 beta 1 |
|
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * API for fetching the HTML to embed remote content based on a provided URL. 4 * Used internally by the {@link WP_Embed} class, but is designed to be generic. 5 * 6 * @link http://codex.wordpress.org/oEmbed oEmbed Codex Article 7 * @link http://oembed.com/ oEmbed Homepage 8 * 9 * @package WordPress 10 * @subpackage oEmbed 11 */ 12 13 /** 14 * oEmbed class. 15 * 16 * @package WordPress 17 * @subpackage oEmbed 18 * @since 2.9.0 19 */ 20 class WP_oEmbed { 21 var $providers = array(); 22 23 /** 24 * PHP4 constructor 25 */ 26 function WP_oEmbed() { 27 return $this->__construct(); 28 } 29 30 /** 31 * PHP5 constructor 32 * 33 * @uses apply_filters() Filters a list of pre-defined oEmbed providers. 34 */ 35 function __construct() { 36 // List out some popular sites that support oEmbed. 37 // The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them. 38 // Add to this list using the wp_oembed_add_provider() function (see it's PHPDoc for details). 39 $this->providers = apply_filters( 'oembed_providers', array( 40 '#http://(www\.)?youtube.com/watch.*#i' => array( 'http://www.youtube.com/oembed', true ), 41 'http://youtu.be/*' => array( 'http://www.youtube.com/oembed', false ), 42 'http://blip.tv/file/*' => array( 'http://blip.tv/oembed/', false ), 43 '#http://(www\.)?vimeo\.com/.*#i' => array( 'http://www.vimeo.com/api/oembed.{format}', true ), 44 '#http://(www\.)?dailymotion\.com/.*#i' => array( 'http://www.dailymotion.com/api/oembed', true ), 45 '#http://(www\.)?flickr\.com/.*#i' => array( 'http://www.flickr.com/services/oembed/', true ), 46 '#http://(.+)?smugmug\.com/.*#i' => array( 'http://api.smugmug.com/services/oembed/', true ), 47 '#http://(www\.)?hulu\.com/watch/.*#i' => array( 'http://www.hulu.com/api/oembed.{format}', true ), 48 '#http://(www\.)?viddler\.com/.*#i' => array( 'http://lab.viddler.com/services/oembed/', true ), 49 'http://qik.com/*' => array( 'http://qik.com/api/oembed.{format}', false ), 50 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ), 51 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ), 52 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ), 53 '#http://(www\.)?scribd\.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ), 54 'http://wordpress.tv/*' => array( 'http://wordpress.tv/oembed/', false ), 55 '#http://(answers|surveys)\.polldaddy.com/.*#i' => array( 'http://polldaddy.com/oembed/', true ), 56 '#http://(www\.)?funnyordie\.com/videos/.*#i' => array( 'http://www.funnyordie.com/oembed', true ), 57 ) ); 58 59 // Fix Scribd embeds. They contain new lines in the middle of the HTML which breaks wpautop(). 60 add_filter( 'oembed_dataparse', array(&$this, 'strip_scribd_newlines'), 10, 3 ); 61 } 62 63 /** 64 * The do-it-all function that takes a URL and attempts to return the HTML. 65 * 66 * @see WP_oEmbed::discover() 67 * @see WP_oEmbed::fetch() 68 * @see WP_oEmbed::data2html() 69 * 70 * @param string $url The URL to the content that should be attempted to be embedded. 71 * @param array $args Optional arguments. Usually passed from a shortcode. 72 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. 73 */ 74 function get_html( $url, $args = '' ) { 75 $provider = false; 76 77 if ( !isset($args['discover']) ) 78 $args['discover'] = true; 79 80 foreach ( $this->providers as $matchmask => $data ) { 81 list( $providerurl, $regex ) = $data; 82 83 // Turn the asterisk-type provider URLs into regex 84 if ( !$regex ) 85 $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i'; 86 87 if ( preg_match( $matchmask, $url ) ) { 88 $provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML 89 break; 90 } 91 } 92 93 if ( !$provider && $args['discover'] ) 94 $provider = $this->discover( $url ); 95 96 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) 97 return false; 98 99 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); 100 } 101 102 /** 103 * Attempts to find oEmbed provider discovery <link> tags at the given URL. 104 * 105 * @param string $url The URL that should be inspected for discovery <link> tags. 106 * @return bool|string False on failure, otherwise the oEmbed provider URL. 107 */ 108 function discover( $url ) { 109 $providers = array(); 110 111 // Fetch URL content 112 if ( $html = wp_remote_retrieve_body( wp_remote_get( $url ) ) ) { 113 114 // <link> types that contain oEmbed provider URLs 115 $linktypes = apply_filters( 'oembed_linktypes', array( 116 'application/json+oembed' => 'json', 117 'text/xml+oembed' => 'xml', 118 'application/xml+oembed' => 'xml', // Incorrect, but used by at least Vimeo 119 ) ); 120 121 // Strip <body> 122 $html = substr( $html, 0, stripos( $html, '</head>' ) ); 123 124 // Do a quick check 125 $tagfound = false; 126 foreach ( $linktypes as $linktype => $format ) { 127 if ( stripos($html, $linktype) ) { 128 $tagfound = true; 129 break; 130 } 131 } 132 133 if ( $tagfound && preg_match_all( '/<link([^<>]+)>/i', $html, $links ) ) { 134 foreach ( $links[1] as $link ) { 135 $atts = shortcode_parse_atts( $link ); 136 137 if ( !empty($atts['type']) && !empty($linktypes[$atts['type']]) && !empty($atts['href']) ) { 138 $providers[$linktypes[$atts['type']]] = $atts['href']; 139 140 // Stop here if it's JSON (that's all we need) 141 if ( 'json' == $linktypes[$atts['type']] ) 142 break; 143 } 144 } 145 } 146 } 147 148 // JSON is preferred to XML 149 if ( !empty($providers['json']) ) 150 return $providers['json']; 151 elseif ( !empty($providers['xml']) ) 152 return $providers['xml']; 153 else 154 return false; 155 } 156 157 /** 158 * Connects to a oEmbed provider and returns the result. 159 * 160 * @param string $provider The URL to the oEmbed provider. 161 * @param string $url The URL to the content that is desired to be embedded. 162 * @param array $args Optional arguments. Usually passed from a shortcode. 163 * @return bool|object False on failure, otherwise the result in the form of an object. 164 */ 165 function fetch( $provider, $url, $args = '' ) { 166 $args = wp_parse_args( $args, wp_embed_defaults() ); 167 168 $provider = add_query_arg( 'format', 'json', $provider ); // JSON is easier to deal with than XML 169 170 $provider = add_query_arg( 'maxwidth', $args['width'], $provider ); 171 $provider = add_query_arg( 'maxheight', $args['height'], $provider ); 172 $provider = add_query_arg( 'url', urlencode($url), $provider ); 173 174 if ( !$result = wp_remote_retrieve_body( wp_remote_get( $provider ) ) ) 175 return false; 176 177 $result = trim( $result ); 178 179 // JSON? 180 // Example content: http://vimeo.com/api/oembed.json?url=http%3A%2F%2Fvimeo.com%2F240975 181 if ( $data = json_decode($result) ) { 182 return $data; 183 } 184 185 // Must be XML. Only parse it if PHP5 is installed. (PHP4 isn't worth the trouble.) 186 // Example content: http://vimeo.com/api/oembed.xml?url=http%3A%2F%2Fvimeo.com%2F240975 187 elseif ( function_exists('simplexml_load_string') ) { 188 $errors = libxml_use_internal_errors( 'true' ); 189 190 $data = simplexml_load_string( $result ); 191 192 libxml_use_internal_errors( $errors ); 193 194 if ( is_object($data) ) 195 return $data; 196 } 197 198 return false; 199 } 200 201 /** 202 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML. 203 * 204 * @param object $data A data object result from an oEmbed provider. 205 * @param string $url The URL to the content that is desired to be embedded. 206 * @return bool|string False on error, otherwise the HTML needed to embed. 207 */ 208 function data2html( $data, $url ) { 209 if ( !is_object($data) || empty($data->type) ) 210 return false; 211 212 switch ( $data->type ) { 213 case 'photo': 214 if ( empty($data->url) || empty($data->width) || empty($data->height) ) 215 return false; 216 217 $title = ( !empty($data->title) ) ? $data->title : ''; 218 $return = '<img src="' . esc_url( $data->url ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />'; 219 break; 220 221 case 'video': 222 case 'rich': 223 $return = ( !empty($data->html) ) ? $data->html : false; 224 break; 225 226 case 'link': 227 $return = ( !empty($data->title) ) ? '<a href="' . esc_url($url) . '">' . esc_html($data->title) . '</a>' : false; 228 break; 229 230 default; 231 $return = false; 232 } 233 234 // You can use this filter to add support for custom data types or to filter the result 235 return apply_filters( 'oembed_dataparse', $return, $data, $url ); 236 } 237 238 /** 239 * Strip new lines from the HTML if it's a Scribd embed. 240 * 241 * @param string $html Existing HTML. 242 * @param object $data Data object from WP_oEmbed::data2html() 243 * @param string $url The original URL passed to oEmbed. 244 * @return string Possibly modified $html 245 */ 246 function strip_scribd_newlines( $html, $data, $url ) { 247 if ( preg_match( '#http://(www\.)?scribd.com/.*#i', $url ) ) 248 $html = str_replace( array( "\r\n", "\n" ), '', $html ); 249 250 return $html; 251 } 252 } 253 254 /** 255 * Returns the initialized {@link WP_oEmbed} object 256 * 257 * @since 2.9.0 258 * @access private 259 * 260 * @see WP_oEmbed 261 * @uses WP_oEmbed 262 * 263 * @return WP_oEmbed object. 264 */ 265 function &_wp_oembed_get_object() { 266 static $wp_oembed; 267 268 if ( is_null($wp_oembed) ) 269 $wp_oembed = new WP_oEmbed(); 270 271 return $wp_oembed; 272 } 273 274 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Apr 5 14:26:09 2010 | Cross-referenced by PHPXref 0.7 |