Make WordPress Core


Ignore:
Timestamp:
05/11/2017 06:18:00 PM (9 years ago)
Author:
jnylen0
Message:

REST API: Add endpoint for proxying requests to external oEmbed providers.

This endpoint is a prerequisite for the media widgets work (see https://github.com/xwp/wp-core-media-widgets).

Also use the new endpoint in the media modal instead of the parse-embed AJAX action.

Props westonruter, timmydcrawford, swissspidy, jnylen0.
Fixes #40450.

File:
1 edited

Legend:

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

    r37568 r40628  
    5353                        ),
    5454                ) );
     55
     56                register_rest_route( 'oembed/1.0', '/proxy', array(
     57                        array(
     58                                'methods'  => WP_REST_Server::READABLE,
     59                                'callback' => array( $this, 'get_proxy_item' ),
     60                                'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
     61                                'args'     => array(
     62                                        'url'      => array(
     63                                                'description'       => __( 'The URL of the resource for which to fetch oEmbed data.' ),
     64                                                'type'              => 'string',
     65                                                'required'          => true,
     66                                                'sanitize_callback' => 'esc_url_raw',
     67                                        ),
     68                                        'format'   => array(
     69                                                'description'       => __( 'The oEmbed format to use.' ),
     70                                                'type'              => 'string',
     71                                                'default'           => 'json',
     72                                                'enum'              => array(
     73                                                        'json',
     74                                                        'xml',
     75                                                ),
     76                                        ),
     77                                        'maxwidth' => array(
     78                                                'description'       => __( 'The maximum width of the embed frame in pixels.' ),
     79                                                'type'              => 'integer',
     80                                                'default'           => $maxwidth,
     81                                                'sanitize_callback' => 'absint',
     82                                        ),
     83                                        'maxheight' => array(
     84                                                'description'       => __( 'The maximum height of the embed frame in pixels.' ),
     85                                                'type'              => 'integer',
     86                                                'sanitize_callback' => 'absint',
     87                                        ),
     88                                        'discover' => array(
     89                                                'description'       => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
     90                                                'type'              => 'boolean',
     91                                                'default'           => true,
     92                                        ),
     93                                ),
     94                        ),
     95                ) );
    5596        }
    5697
    5798        /**
    58          * Callback for the API endpoint.
     99         * Callback for the embed API endpoint.
    59100         *
    60101         * Returns the JSON object for the post.
     
    87128                return $data;
    88129        }
     130
     131        /**
     132         * Checks if current user can make a proxy oEmbed request.
     133         *
     134         * @since 4.8.0
     135         * @access public
     136         *
     137         * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
     138         */
     139        public function get_proxy_item_permissions_check() {
     140                if ( ! current_user_can( 'edit_posts' ) ) {
     141                        return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to make proxied oEmbed requests.' ), array( 'status' => rest_authorization_required_code() ) );
     142                }
     143                return true;
     144        }
     145
     146        /**
     147         * Callback for the proxy API endpoint.
     148         *
     149         * Returns the JSON object for the proxied item.
     150         *
     151         * @since 4.8.0
     152         * @access public
     153         *
     154         * @see WP_oEmbed::get_html()
     155         * @param WP_REST_Request $request Full data about the request.
     156         * @return WP_Error|array oEmbed response data or WP_Error on failure.
     157         */
     158        public function get_proxy_item( $request ) {
     159                $args = $request->get_params();
     160
     161                // Serve oEmbed data from cache if set.
     162                $cache_key = 'oembed_' . md5( serialize( $args ) );
     163                $data = get_transient( $cache_key );
     164                if ( ! empty( $data ) ) {
     165                        return $data;
     166                }
     167
     168                $url = $request['url'];
     169                unset( $args['url'] );
     170
     171                $data = _wp_oembed_get_object()->get_data( $url, $args );
     172
     173                if ( false === $data ) {
     174                        return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
     175                }
     176
     177                /**
     178                 * Filters the oEmbed TTL value (time to live).
     179                 *
     180                 * Similar to the {@see 'oembed_ttl'} filter, but for the REST API
     181                 * oEmbed proxy endpoint.
     182                 *
     183                 * @since 4.8.0
     184                 *
     185                 * @param int    $time    Time to live (in seconds).
     186                 * @param string $url     The attempted embed URL.
     187                 * @param array  $args    An array of embed request arguments.
     188                 */
     189                $ttl = apply_filters( 'rest_oembed_ttl', DAY_IN_SECONDS, $url, $args );
     190
     191                set_transient( $cache_key, $data, $ttl );
     192
     193                return $data;
     194        }
    89195}
Note: See TracChangeset for help on using the changeset viewer.