]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-oembed-controller.php
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-oembed-controller.php
1 <?php
2 /**
3  * WP_oEmbed_Controller class, used to provide an oEmbed endpoint.
4  *
5  * @package WordPress
6  * @subpackage Embeds
7  * @since 4.4.0
8  */
9
10 /**
11  * oEmbed API endpoint controller.
12  *
13  * Registers the API route and delivers the response data.
14  * The output format (XML or JSON) is handled by the REST API.
15  *
16  * @since 4.4.0
17  */
18 final class WP_oEmbed_Controller {
19         /**
20          * Register the oEmbed REST API route.
21          *
22          * @since 4.4.0
23          * @access public
24          */
25         public function register_routes() {
26                 /**
27                  * Filters the maxwidth oEmbed parameter.
28                  *
29                  * @since 4.4.0
30                  *
31                  * @param int $maxwidth Maximum allowed width. Default 600.
32                  */
33                 $maxwidth = apply_filters( 'oembed_default_width', 600 );
34
35                 register_rest_route( 'oembed/1.0', '/embed', array(
36                         array(
37                                 'methods'  => WP_REST_Server::READABLE,
38                                 'callback' => array( $this, 'get_item' ),
39                                 'args'     => array(
40                                         'url'      => array(
41                                                 'required'          => true,
42                                                 'sanitize_callback' => 'esc_url_raw',
43                                         ),
44                                         'format'   => array(
45                                                 'default'           => 'json',
46                                                 'sanitize_callback' => 'wp_oembed_ensure_format',
47                                         ),
48                                         'maxwidth' => array(
49                                                 'default'           => $maxwidth,
50                                                 'sanitize_callback' => 'absint',
51                                         ),
52                                 ),
53                         ),
54                 ) );
55         }
56
57         /**
58          * Callback for the API endpoint.
59          *
60          * Returns the JSON object for the post.
61          *
62          * @since 4.4.0
63          * @access public
64          *
65          * @param WP_REST_Request $request Full data about the request.
66          * @return WP_Error|array oEmbed response data or WP_Error on failure.
67          */
68         public function get_item( $request ) {
69                 $post_id = url_to_postid( $request['url'] );
70
71                 /**
72                  * Filters the determined post ID.
73                  *
74                  * @since 4.4.0
75                  *
76                  * @param int    $post_id The post ID.
77                  * @param string $url     The requested URL.
78                  */
79                 $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
80
81                 $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
82
83                 if ( ! $data ) {
84                         return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
85                 }
86
87                 return $data;
88         }
89 }