Make WordPress Core


Ignore:
Timestamp:
10/29/2015 10:50:13 PM (9 years ago)
Author:
pento
Message:

Embeds: Who put this REST API infrastructure in my WordPress?

Well, while it's here, we probably should make use of it. The oEmbed endpoint now uses the REST API infrastructure, instead of providing its own.

Props swissspidy.

Fixes #34207.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/embed-functions.php

    r35432 r35436  
    329329
    330330/**
    331  * Parses an oEmbed API query.
    332  *
    333  * @since 4.4.0
    334  *
    335  * @see WP_oEmbed_Controller::parse_query()
    336  *
    337  * @param WP_Query $wp_query The current WP_Query instance.
    338  */
    339 function wp_oembed_parse_query( $wp_query ) {
     331 * Registers the oEmbed REST API route.
     332 *
     333 * @since 4.4.0
     334 */
     335function wp_oembed_register_route() {
    340336    $controller = new WP_oEmbed_Controller();
    341     $controller->parse_query( $wp_query );
     337    $controller->register_routes();
    342338}
    343339
     
    422418 */
    423419function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) {
    424     $url = add_query_arg( array( 'oembed' => 'true' ), home_url( '/' ) );
     420    $url = rest_url( 'oembed/1.0/embed' );
    425421
    426422    if ( 'json' === $format ) {
     
    546542    ) );
    547543
    548     if ( $width < $min_max_width['min'] ) {
    549         $width = $min_max_width['min'];
    550     } elseif ( $width > $min_max_width['max'] ) {
    551         $width = $min_max_width['max'];
    552     }
    553 
    554     $height = ceil( $width / 16 * 9 );
    555 
    556     if ( 200 > $height ) {
    557         $height = 200;
    558     }
     544    $width  = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
     545    $height = max( ceil( $width / 16 * 9 ), 200 );
    559546
    560547    $data = array(
     
    645632
    646633    return $format;
     634}
     635
     636/**
     637 * Hooks into the REST API output to print XML instead of JSON.
     638 *
     639 * This is only done for the oEmbed API endpoint,
     640 * which supports both formats.
     641 *
     642 * @access private
     643 * @since 4.4.0
     644 *
     645 * @param bool                      $served  Whether the request has already been served.
     646 * @param WP_HTTP_ResponseInterface $result  Result to send to the client. Usually a WP_REST_Response.
     647 * @param WP_REST_Request           $request Request used to generate the response.
     648 * @param WP_REST_Server            $server  Server instance.
     649 * @return true
     650 */
     651function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) {
     652    $params = $request->get_params();
     653
     654    if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) {
     655        return $served;
     656    }
     657
     658    if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) {
     659        return $served;
     660    }
     661
     662    // Embed links inside the request.
     663    $data = $server->response_to_data( $result, false );
     664
     665    if ( 404 === $result->get_status() ) {
     666        $data = $data[0];
     667    }
     668
     669    if ( ! class_exists( 'SimpleXMLElement' ) ) {
     670        status_header( 501 );
     671        die( get_status_header_desc( 501 ) );
     672    }
     673
     674    $result = _oembed_create_xml( $data );
     675
     676    // Bail if there's no XML.
     677    if ( ! $result ) {
     678        status_header( 501 );
     679        return get_status_header_desc( 501 );
     680    }
     681
     682    if ( ! headers_sent() ) {
     683        $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) );
     684    }
     685
     686    echo $result;
     687
     688    return true;
    647689}
    648690
Note: See TracChangeset for help on using the changeset viewer.