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
|
|
|
*/ |
24
|
|
|
public function register_routes() { |
25
|
|
|
/** |
26
|
|
|
* Filter the maxwidth oEmbed parameter. |
27
|
|
|
* |
28
|
|
|
* @since 4.4.0 |
29
|
|
|
* |
30
|
|
|
* @param int $maxwidth Maximum allowed width. Default 600. |
31
|
|
|
*/ |
32
|
|
|
$maxwidth = apply_filters( 'oembed_default_width', 600 ); |
33
|
|
|
|
34
|
|
|
register_rest_route( 'oembed/1.0', '/embed', array( |
35
|
|
|
array( |
36
|
|
|
'methods' => WP_REST_Server::READABLE, |
37
|
|
|
'callback' => array( $this, 'get_item' ), |
38
|
|
|
'args' => array( |
39
|
|
|
'url' => array( |
40
|
|
|
'required' => true, |
41
|
|
|
'sanitize_callback' => 'esc_url_raw', |
42
|
|
|
), |
43
|
|
|
'format' => array( |
44
|
|
|
'default' => 'json', |
45
|
|
|
'sanitize_callback' => 'wp_oembed_ensure_format', |
46
|
|
|
), |
47
|
|
|
'maxwidth' => array( |
48
|
|
|
'default' => $maxwidth, |
49
|
|
|
'sanitize_callback' => 'absint', |
50
|
|
|
), |
51
|
|
|
), |
52
|
|
|
), |
53
|
|
|
) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Callback for the API endpoint. |
58
|
|
|
* |
59
|
|
|
* Returns the JSON object for the post. |
60
|
|
|
* |
61
|
|
|
* @since 4.4.0 |
62
|
|
|
* |
63
|
|
|
* @param WP_REST_Request $request Full data about the request. |
64
|
|
|
* @return WP_Error|array oEmbed response data or WP_Error on failure. |
65
|
|
|
*/ |
66
|
|
|
public function get_item( $request ) { |
67
|
|
|
$post_id = url_to_postid( $request['url'] ); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Filter the determined post ID. |
71
|
|
|
* |
72
|
|
|
* @since 4.4.0 |
73
|
|
|
* |
74
|
|
|
* @param int $post_id The post ID. |
75
|
|
|
* @param string $url The requested URL. |
76
|
|
|
*/ |
77
|
|
|
$post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] ); |
78
|
|
|
|
79
|
|
|
$data = get_oembed_response_data( $post_id, $request['maxwidth'] ); |
80
|
|
|
|
81
|
|
|
if ( ! $data ) { |
82
|
|
|
return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $data; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|