|
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
|
|
|
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
|
|
|
) ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Callback for the embed API endpoint. |
|
100
|
|
|
* |
|
101
|
|
|
* Returns the JSON object for the post. |
|
102
|
|
|
* |
|
103
|
|
|
* @since 4.4.0 |
|
104
|
|
|
* @access public |
|
105
|
|
|
* |
|
106
|
|
|
* @param WP_REST_Request $request Full data about the request. |
|
107
|
|
|
* @return WP_Error|array oEmbed response data or WP_Error on failure. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function get_item( $request ) { |
|
110
|
|
|
$post_id = url_to_postid( $request['url'] ); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Filters the determined post ID. |
|
114
|
|
|
* |
|
115
|
|
|
* @since 4.4.0 |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $post_id The post ID. |
|
118
|
|
|
* @param string $url The requested URL. |
|
119
|
|
|
*/ |
|
120
|
|
|
$post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] ); |
|
121
|
|
|
|
|
122
|
|
|
$data = get_oembed_response_data( $post_id, $request['maxwidth'] ); |
|
123
|
|
|
|
|
124
|
|
|
if ( ! $data ) { |
|
125
|
|
|
return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $data; |
|
129
|
|
|
} |
|
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
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.