3 * WP_oEmbed_Controller class, used to provide an oEmbed endpoint.
11 * oEmbed API endpoint controller.
13 * Registers the API route and delivers the response data.
14 * The output format (XML or JSON) is handled by the REST API.
18 final class WP_oEmbed_Controller {
20 * Register the oEmbed REST API route.
25 public function register_routes() {
27 * Filters the maxwidth oEmbed parameter.
31 * @param int $maxwidth Maximum allowed width. Default 600.
33 $maxwidth = apply_filters( 'oembed_default_width', 600 );
35 register_rest_route( 'oembed/1.0', '/embed', array(
37 'methods' => WP_REST_Server::READABLE,
38 'callback' => array( $this, 'get_item' ),
42 'sanitize_callback' => 'esc_url_raw',
46 'sanitize_callback' => 'wp_oembed_ensure_format',
49 'default' => $maxwidth,
50 'sanitize_callback' => 'absint',
58 * Callback for the API endpoint.
60 * Returns the JSON object for the post.
65 * @param WP_REST_Request $request Full data about the request.
66 * @return WP_Error|array oEmbed response data or WP_Error on failure.
68 public function get_item( $request ) {
69 $post_id = url_to_postid( $request['url'] );
72 * Filters the determined post ID.
76 * @param int $post_id The post ID.
77 * @param string $url The requested URL.
79 $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
81 $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
84 return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );