web/wp-includes/class-oembed.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    28 	function __construct() {
    28 	function __construct() {
    29 		// List out some popular sites that support oEmbed.
    29 		// List out some popular sites that support oEmbed.
    30 		// The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them.
    30 		// The WP_Embed class disables discovery for non-unfiltered_html users, so only providers in this array will be used for them.
    31 		// Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details).
    31 		// Add to this list using the wp_oembed_add_provider() function (see its PHPDoc for details).
    32 		$this->providers = apply_filters( 'oembed_providers', array(
    32 		$this->providers = apply_filters( 'oembed_providers', array(
    33 			'#http://(www\.)?youtube.com/watch.*#i'              => array( 'http://www.youtube.com/oembed',                     true  ),
    33 			'#https?://(www\.)?youtube.com/watch.*#i'            => array( 'http://www.youtube.com/oembed',                     true  ),
    34 			'http://youtu.be/*'                                  => array( 'http://www.youtube.com/oembed',                     false ),
    34 			'http://youtu.be/*'                                  => array( 'http://www.youtube.com/oembed',                     false ),
    35 			'http://blip.tv/*'                                   => array( 'http://blip.tv/oembed/',                            false ),
    35 			'http://blip.tv/*'                                   => array( 'http://blip.tv/oembed/',                            false ),
    36 			'#http://(www\.)?vimeo\.com/.*#i'                    => array( 'http://vimeo.com/api/oembed.{format}',              true  ),
    36 			'#https?://(www\.)?vimeo\.com/.*#i'                  => array( 'http://vimeo.com/api/oembed.{format}',              true  ),
    37 			'#http://(www\.)?dailymotion\.com/.*#i'              => array( 'http://www.dailymotion.com/services/oembed',        true  ),
    37 			'#https?://(www\.)?dailymotion\.com/.*#i'            => array( 'http://www.dailymotion.com/services/oembed',        true  ),
    38 			'#http://(www\.)?flickr\.com/.*#i'                   => array( 'http://www.flickr.com/services/oembed/',            true  ),
    38 			'#https?://(www\.)?flickr\.com/.*#i'                 => array( 'http://www.flickr.com/services/oembed/',            true  ),
    39 			'#http://(.+\.)?smugmug\.com/.*#i'                   => array( 'http://api.smugmug.com/services/oembed/',           true  ),
    39 			'#https?://(.+\.)?smugmug\.com/.*#i'                 => array( 'http://api.smugmug.com/services/oembed/',           true  ),
    40 			'#http://(www\.)?hulu\.com/watch/.*#i'               => array( 'http://www.hulu.com/api/oembed.{format}',           true  ),
    40 			'#https?://(www\.)?hulu\.com/watch/.*#i'             => array( 'http://www.hulu.com/api/oembed.{format}',           true  ),
    41 			'#http://(www\.)?viddler\.com/.*#i'                  => array( 'http://lab.viddler.com/services/oembed/',           true  ),
    41 			'#https?://(www\.)?viddler\.com/.*#i'                => array( 'http://lab.viddler.com/services/oembed/',           true  ),
    42 			'http://qik.com/*'                                   => array( 'http://qik.com/api/oembed.{format}',                false ),
    42 			'http://qik.com/*'                                   => array( 'http://qik.com/api/oembed.{format}',                false ),
    43 			'http://revision3.com/*'                             => array( 'http://revision3.com/api/oembed/',                  false ),
    43 			'http://revision3.com/*'                             => array( 'http://revision3.com/api/oembed/',                  false ),
    44 			'http://i*.photobucket.com/albums/*'                 => array( 'http://photobucket.com/oembed',                     false ),
    44 			'http://i*.photobucket.com/albums/*'                 => array( 'http://photobucket.com/oembed',                     false ),
    45 			'http://gi*.photobucket.com/groups/*'                => array( 'http://photobucket.com/oembed',                     false ),
    45 			'http://gi*.photobucket.com/groups/*'                => array( 'http://photobucket.com/oembed',                     false ),
    46 			'#http://(www\.)?scribd\.com/.*#i'                   => array( 'http://www.scribd.com/services/oembed',             true  ),
    46 			'#https?://(www\.)?scribd\.com/.*#i'                 => array( 'http://www.scribd.com/services/oembed',             true  ),
    47 			'http://wordpress.tv/*'                              => array( 'http://wordpress.tv/oembed/',                       false ),
    47 			'http://wordpress.tv/*'                              => array( 'http://wordpress.tv/oembed/',                       false ),
    48 			'#http://(.+\.)?polldaddy\.com/.*#i'                 => array( 'http://polldaddy.com/oembed/',                      true  ),
    48 			'#https?://(.+\.)?polldaddy\.com/.*#i'               => array( 'http://polldaddy.com/oembed/',                      true  ),
    49 			'#http://(www\.)?funnyordie\.com/videos/.*#i'        => array( 'http://www.funnyordie.com/oembed',                  true  ),
    49 			'#https?://(www\.)?funnyordie\.com/videos/.*#i'      => array( 'http://www.funnyordie.com/oembed',                  true  ),
    50 			'#https?://(www\.)?twitter.com/.+?/status(es)?/.*#i' => array( 'http://api.twitter.com/1/statuses/oembed.{format}', true  ),
    50 			'#https?://(www\.)?twitter.com/.+?/status(es)?/.*#i' => array( 'http://api.twitter.com/1/statuses/oembed.{format}', true  ),
       
    51  			'#https?://(www\.)?soundcloud\.com/.*#i'             => array( 'http://soundcloud.com/oembed',                      true  ),
       
    52 			'#https?://(www\.)?slideshare.net/*#'                => array( 'http://www.slideshare.net/api/oembed/2',            true  ),
       
    53 			'#http://instagr(\.am|am\.com)/p/.*#i'               => array( 'http://api.instagram.com/oembed',                   true  ),
    51 		) );
    54 		) );
    52 
    55 
    53 		// Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
    56 		// Fix any embeds that contain new lines in the middle of the HTML which breaks wpautop().
    54 		add_filter( 'oembed_dataparse', array(&$this, '_strip_newlines'), 10, 3 );
    57 		add_filter( 'oembed_dataparse', array($this, '_strip_newlines'), 10, 3 );
    55 	}
    58 	}
    56 
    59 
    57 	/**
    60 	/**
    58 	 * The do-it-all function that takes a URL and attempts to return the HTML.
    61 	 * The do-it-all function that takes a URL and attempts to return the HTML.
    59 	 *
    62 	 *
    73 
    76 
    74 		foreach ( $this->providers as $matchmask => $data ) {
    77 		foreach ( $this->providers as $matchmask => $data ) {
    75 			list( $providerurl, $regex ) = $data;
    78 			list( $providerurl, $regex ) = $data;
    76 
    79 
    77 			// Turn the asterisk-type provider URLs into regex
    80 			// Turn the asterisk-type provider URLs into regex
    78 			if ( !$regex )
    81 			if ( !$regex ) {
    79 				$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
    82 				$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
       
    83 				$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
       
    84 			}
    80 
    85 
    81 			if ( preg_match( $matchmask, $url ) ) {
    86 			if ( preg_match( $matchmask, $url ) ) {
    82 				$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
    87 				$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML
    83 				break;
    88 				break;
    84 			}
    89 			}
   161 
   166 
   162 		$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
   167 		$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
   163 		$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
   168 		$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
   164 		$provider = add_query_arg( 'url', urlencode($url), $provider );
   169 		$provider = add_query_arg( 'url', urlencode($url), $provider );
   165 
   170 
       
   171 		$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );
       
   172 
   166 		foreach( array( 'json', 'xml' ) as $format ) {
   173 		foreach( array( 'json', 'xml' ) as $format ) {
   167 			$result = $this->_fetch_with_format( $provider, $format );
   174 			$result = $this->_fetch_with_format( $provider, $format );
   168 			if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
   175 			if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() )
   169 				continue;
   176 				continue;
   170 			return ( $result && ! is_wp_error( $result ) ) ? $result : false;
   177 			return ( $result && ! is_wp_error( $result ) ) ? $result : false;
   207 	 *
   214 	 *
   208 	 * @since 3.0.0
   215 	 * @since 3.0.0
   209 	 * @access private
   216 	 * @access private
   210 	 */
   217 	 */
   211 	function _parse_xml( $response_body ) {
   218 	function _parse_xml( $response_body ) {
   212 		if ( function_exists('simplexml_load_string') ) {
   219 		if ( !function_exists('simplexml_load_string') ) {
   213 			$errors = libxml_use_internal_errors( 'true' );
   220 			return false;
   214 			$data = simplexml_load_string( $response_body );
   221 		}
   215 			libxml_use_internal_errors( $errors );
   222 
   216 			if ( ! is_object( $data ) )
   223 		if ( ! class_exists( 'DOMDocument' ) )
   217 				return false;
   224 			return false;
   218 
   225 
   219 			$return = new stdClass;
   226 		$errors = libxml_use_internal_errors( true );
   220 			foreach ( $data as $key => $value )
   227 		$old_value = null;
   221 				$return->$key = (string) $value;
   228 		if ( function_exists( 'libxml_disable_entity_loader' ) ) {
   222 
   229 			$old_value = libxml_disable_entity_loader( true );
   223 			return $return;
   230 		}
   224 		}
   231 
   225 		return false;
   232 		$dom = new DOMDocument;
       
   233 		$success = $dom->loadXML( $response_body );
       
   234 
       
   235 		if ( ! is_null( $old_value ) ) {
       
   236 			libxml_disable_entity_loader( $old_value );
       
   237 		}
       
   238 		libxml_use_internal_errors( $errors );
       
   239 
       
   240 		if ( ! $success || isset( $dom->doctype ) ) {
       
   241 			return false;
       
   242 		}
       
   243 
       
   244 		$data = simplexml_import_dom( $dom );
       
   245 		if ( ! is_object( $data ) )
       
   246 			return false;
       
   247 
       
   248 		$return = new stdClass;
       
   249 		foreach ( $data as $key => $value )
       
   250 			$return->$key = (string) $value;
       
   251 		return $return;
   226 	}
   252 	}
   227 
   253 
   228 	/**
   254 	/**
   229 	 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
   255 	 * Converts a data object from {@link WP_oEmbed::fetch()} and returns the HTML.
   230 	 *
   256 	 *
   294  * @see WP_oEmbed
   320  * @see WP_oEmbed
   295  * @uses WP_oEmbed
   321  * @uses WP_oEmbed
   296  *
   322  *
   297  * @return WP_oEmbed object.
   323  * @return WP_oEmbed object.
   298  */
   324  */
   299 function &_wp_oembed_get_object() {
   325 function _wp_oembed_get_object() {
   300 	static $wp_oembed;
   326 	static $wp_oembed;
   301 
   327 
   302 	if ( is_null($wp_oembed) )
   328 	if ( is_null($wp_oembed) )
   303 		$wp_oembed = new WP_oEmbed();
   329 		$wp_oembed = new WP_oEmbed();
   304 
   330