Changeset 13996
- Timestamp:
- 04/04/2010 12:30:22 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-oembed.php
r13297 r13996 166 166 $args = wp_parse_args( $args, wp_embed_defaults() ); 167 167 168 $provider = add_query_arg( 'format', 'json', $provider ); // JSON is easier to deal with than XML169 170 168 $provider = add_query_arg( 'maxwidth', $args['width'], $provider ); 171 169 $provider = add_query_arg( 'maxheight', $args['height'], $provider ); 172 170 $provider = add_query_arg( 'url', urlencode($url), $provider ); 173 174 if ( !$result = wp_remote_retrieve_body( wp_remote_get( $provider ) ) ) 171 172 foreach( array( 'json', 'xml' ) as $format ) { 173 $result = $this->_fetch_with_format( $provider, $format ); 174 if ( is_wp_error( $result ) && 'not-implemented' == $result->get_error_code() ) 175 continue; 176 return ( $result && ! is_wp_error( $result ) ) ? $result : false; 177 } 178 return false; 179 } 180 181 /** 182 * Fetches result from an oEmbed provider for a specific format and complete provider URL 183 * 184 * @since 3.0.0 185 * @access private 186 * @param string $provider_url_with_args URL to the provider with full arguments list (url, maxheight, etc.) 187 * @param string $format Format to use 188 * @return bool|object False on failure, otherwise the result in the form of an object. 189 */ 190 function _fetch_with_format( $provider_url_with_args, $format ) { 191 $provider_url_with_args = add_query_arg( 'format', $format, $provider_url_with_args ); 192 $response = wp_remote_get( $provider_url_with_args ); 193 if ( 501 == wp_remote_retrieve_response_code( $response ) ) 194 return new WP_Error( 'not-implemented' ); 195 if ( ! $body = wp_remote_retrieve_body( $response ) ) 175 196 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') ) { 197 $parse_method = "_parse_$format"; 198 return $this->$parse_method( $body ); 199 } 200 201 /** 202 * Parses a json response body. 203 * 204 * @since 3.0.0 205 * @access private 206 */ 207 function _parse_json( $response_body ) { 208 return ( ( $data = json_decode( trim( $response_body ) ) ) && is_object( $data ) ) ? $data : false; 209 } 210 211 /** 212 * Parses an XML response body. 213 * 214 * @since 3.0.0 215 * @access private 216 */ 217 function _parse_xml( $response_body ) { 218 if ( function_exists('simplexml_load_string') ) { 188 219 $errors = libxml_use_internal_errors( 'true' ); 189 190 $data = simplexml_load_string( $result ); 191 220 $data = simplexml_load_string( $response_body ); 192 221 libxml_use_internal_errors( $errors ); 193 194 if ( is_object($data) ) 222 if ( is_object( $data ) ) 195 223 return $data; 196 224 } 197 198 225 return false; 199 226 }
Note: See TracChangeset
for help on using the changeset viewer.