class-wp-oembed-controller.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * oEmbed API endpoint controller.
  11. *
  12. * Registers the API route and delivers the response data.
  13. * The output format (XML or JSON) is handled by the REST API.
  14. *
  15. * @since 4.4.0
  16. */
  17. final class WP_oEmbed_Controller {
  18. /**
  19. * Register the oEmbed REST API route.
  20. *
  21. * @since 4.4.0
  22. */
  23. public function register_routes() {
  24. /**
  25. * Filters the maxwidth oEmbed parameter.
  26. *
  27. * @since 4.4.0
  28. *
  29. * @param int $maxwidth Maximum allowed width. Default 600.
  30. */
  31. $maxwidth = apply_filters( 'oembed_default_width', 600 );
  32. register_rest_route(
  33. 'oembed/1.0',
  34. '/embed',
  35. 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(
  57. 'oembed/1.0',
  58. '/proxy',
  59. array(
  60. array(
  61. 'methods' => WP_REST_Server::READABLE,
  62. 'callback' => array( $this, 'get_proxy_item' ),
  63. 'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
  64. 'args' => array(
  65. 'url' => array(
  66. 'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
  67. 'type' => 'string',
  68. 'required' => true,
  69. 'sanitize_callback' => 'esc_url_raw',
  70. ),
  71. 'format' => array(
  72. 'description' => __( 'The oEmbed format to use.' ),
  73. 'type' => 'string',
  74. 'default' => 'json',
  75. 'enum' => array(
  76. 'json',
  77. 'xml',
  78. ),
  79. ),
  80. 'maxwidth' => array(
  81. 'description' => __( 'The maximum width of the embed frame in pixels.' ),
  82. 'type' => 'integer',
  83. 'default' => $maxwidth,
  84. 'sanitize_callback' => 'absint',
  85. ),
  86. 'maxheight' => array(
  87. 'description' => __( 'The maximum height of the embed frame in pixels.' ),
  88. 'type' => 'integer',
  89. 'sanitize_callback' => 'absint',
  90. ),
  91. 'discover' => array(
  92. 'description' => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
  93. 'type' => 'boolean',
  94. 'default' => true,
  95. ),
  96. ),
  97. ),
  98. )
  99. );
  100. }
  101. /**
  102. * Callback for the embed API endpoint.
  103. *
  104. * Returns the JSON object for the post.
  105. *
  106. * @since 4.4.0
  107. *
  108. * @param WP_REST_Request $request Full data about the request.
  109. * @return WP_Error|array oEmbed response data or WP_Error on failure.
  110. */
  111. public function get_item( $request ) {
  112. $post_id = url_to_postid( $request['url'] );
  113. /**
  114. * Filters the determined post ID.
  115. *
  116. * @since 4.4.0
  117. *
  118. * @param int $post_id The post ID.
  119. * @param string $url The requested URL.
  120. */
  121. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
  122. $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
  123. if ( ! $data ) {
  124. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  125. }
  126. return $data;
  127. }
  128. /**
  129. * Checks if current user can make a proxy oEmbed request.
  130. *
  131. * @since 4.8.0
  132. *
  133. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  134. */
  135. public function get_proxy_item_permissions_check() {
  136. if ( ! current_user_can( 'edit_posts' ) ) {
  137. return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to make proxied oEmbed requests.' ), array( 'status' => rest_authorization_required_code() ) );
  138. }
  139. return true;
  140. }
  141. /**
  142. * Callback for the proxy API endpoint.
  143. *
  144. * Returns the JSON object for the proxied item.
  145. *
  146. * @since 4.8.0
  147. *
  148. * @see WP_oEmbed::get_html()
  149. * @param WP_REST_Request $request Full data about the request.
  150. * @return object|WP_Error oEmbed response data or WP_Error on failure.
  151. */
  152. public function get_proxy_item( $request ) {
  153. $args = $request->get_params();
  154. // Serve oEmbed data from cache if set.
  155. unset( $args['_wpnonce'] );
  156. $cache_key = 'oembed_' . md5( serialize( $args ) );
  157. $data = get_transient( $cache_key );
  158. if ( ! empty( $data ) ) {
  159. return $data;
  160. }
  161. $url = $request['url'];
  162. unset( $args['url'] );
  163. // Copy maxwidth/maxheight to width/height since WP_oEmbed::fetch() uses these arg names.
  164. if ( isset( $args['maxwidth'] ) ) {
  165. $args['width'] = $args['maxwidth'];
  166. }
  167. if ( isset( $args['maxheight'] ) ) {
  168. $args['height'] = $args['maxheight'];
  169. }
  170. // Short-circuit process for URLs belonging to the current site.
  171. $data = get_oembed_response_data_for_url( $url, $args );
  172. if ( $data ) {
  173. return $data;
  174. }
  175. $data = _wp_oembed_get_object()->get_data( $url, $args );
  176. if ( false === $data ) {
  177. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  178. }
  179. /** This filter is documented in wp-includes/class-wp-oembed.php */
  180. $data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
  181. /**
  182. * Filters the oEmbed TTL value (time to live).
  183. *
  184. * Similar to the {@see 'oembed_ttl'} filter, but for the REST API
  185. * oEmbed proxy endpoint.
  186. *
  187. * @since 4.8.0
  188. *
  189. * @param int $time Time to live (in seconds).
  190. * @param string $url The attempted embed URL.
  191. * @param array $args An array of embed request arguments.
  192. */
  193. $ttl = apply_filters( 'rest_oembed_ttl', DAY_IN_SECONDS, $url, $args );
  194. set_transient( $cache_key, $data, $ttl );
  195. return $data;
  196. }
  197. }