Make WordPress Core

Changeset 13996


Ignore:
Timestamp:
04/04/2010 12:30:22 PM (14 years ago)
Author:
nacin
Message:

Request XML from an oEmbed provider if the provider returns '501 Not Implemented' for JSON. props nbachiyski, fixes #11964.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-oembed.php

    r13297 r13996  
    166166        $args = wp_parse_args( $args, wp_embed_defaults() );
    167167
    168         $provider = add_query_arg( 'format', 'json', $provider ); // JSON is easier to deal with than XML
    169 
    170168        $provider = add_query_arg( 'maxwidth', $args['width'], $provider );
    171169        $provider = add_query_arg( 'maxheight', $args['height'], $provider );
    172170        $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 ) )
    175196            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') ) {
    188219            $errors = libxml_use_internal_errors( 'true' );
    189 
    190             $data = simplexml_load_string( $result );
    191 
     220            $data = simplexml_load_string( $response_body );
    192221            libxml_use_internal_errors( $errors );
    193 
    194             if ( is_object($data) )
     222            if ( is_object( $data ) )
    195223                return $data;
    196224        }
    197 
    198225        return false;
    199226    }
Note: See TracChangeset for help on using the changeset viewer.