class-wp-oembed-controller.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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( 'oembed/1.0', '/embed', array(
  33. array(
  34. 'methods' => WP_REST_Server::READABLE,
  35. 'callback' => array( $this, 'get_item' ),
  36. 'args' => array(
  37. 'url' => array(
  38. 'required' => true,
  39. 'sanitize_callback' => 'esc_url_raw',
  40. ),
  41. 'format' => array(
  42. 'default' => 'json',
  43. 'sanitize_callback' => 'wp_oembed_ensure_format',
  44. ),
  45. 'maxwidth' => array(
  46. 'default' => $maxwidth,
  47. 'sanitize_callback' => 'absint',
  48. ),
  49. ),
  50. ),
  51. ) );
  52. register_rest_route( 'oembed/1.0', '/proxy', array(
  53. array(
  54. 'methods' => WP_REST_Server::READABLE,
  55. 'callback' => array( $this, 'get_proxy_item' ),
  56. 'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
  57. 'args' => array(
  58. 'url' => array(
  59. 'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
  60. 'type' => 'string',
  61. 'required' => true,
  62. 'sanitize_callback' => 'esc_url_raw',
  63. ),
  64. 'format' => array(
  65. 'description' => __( 'The oEmbed format to use.' ),
  66. 'type' => 'string',
  67. 'default' => 'json',
  68. 'enum' => array(
  69. 'json',
  70. 'xml',
  71. ),
  72. ),
  73. 'maxwidth' => array(
  74. 'description' => __( 'The maximum width of the embed frame in pixels.' ),
  75. 'type' => 'integer',
  76. 'default' => $maxwidth,
  77. 'sanitize_callback' => 'absint',
  78. ),
  79. 'maxheight' => array(
  80. 'description' => __( 'The maximum height of the embed frame in pixels.' ),
  81. 'type' => 'integer',
  82. 'sanitize_callback' => 'absint',
  83. ),
  84. 'discover' => array(
  85. 'description' => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
  86. 'type' => 'boolean',
  87. 'default' => true,
  88. ),
  89. ),
  90. ),
  91. ) );
  92. }
  93. /**
  94. * Callback for the embed API endpoint.
  95. *
  96. * Returns the JSON object for the post.
  97. *
  98. * @since 4.4.0
  99. *
  100. * @param WP_REST_Request $request Full data about the request.
  101. * @return WP_Error|array oEmbed response data or WP_Error on failure.
  102. */
  103. public function get_item( $request ) {
  104. $post_id = url_to_postid( $request['url'] );
  105. /**
  106. * Filters the determined post ID.
  107. *
  108. * @since 4.4.0
  109. *
  110. * @param int $post_id The post ID.
  111. * @param string $url The requested URL.
  112. */
  113. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
  114. $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
  115. if ( ! $data ) {
  116. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  117. }
  118. return $data;
  119. }
  120. /**
  121. * Checks if current user can make a proxy oEmbed request.
  122. *
  123. * @since 4.8.0
  124. *
  125. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  126. */
  127. public function get_proxy_item_permissions_check() {
  128. if ( ! current_user_can( 'edit_posts' ) ) {
  129. return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to make proxied oEmbed requests.' ), array( 'status' => rest_authorization_required_code() ) );
  130. }
  131. return true;
  132. }
  133. /**
  134. * Callback for the proxy API endpoint.
  135. *
  136. * Returns the JSON object for the proxied item.
  137. *
  138. * @since 4.8.0
  139. *
  140. * @see WP_oEmbed::get_html()
  141. * @param WP_REST_Request $request Full data about the request.
  142. * @return object|WP_Error oEmbed response data or WP_Error on failure.
  143. */
  144. public function get_proxy_item( $request ) {
  145. $args = $request->get_params();
  146. // Serve oEmbed data from cache if set.
  147. unset( $args['_wpnonce'] );
  148. $cache_key = 'oembed_' . md5( serialize( $args ) );
  149. $data = get_transient( $cache_key );
  150. if ( ! empty( $data ) ) {
  151. return $data;
  152. }
  153. $url = $request['url'];
  154. unset( $args['url'] );
  155. // Copy maxwidth/maxheight to width/height since WP_oEmbed::fetch() uses these arg names.
  156. if ( isset( $args['maxwidth'] ) ) {
  157. $args['width'] = $args['maxwidth'];
  158. }
  159. if ( isset( $args['maxheight'] ) ) {
  160. $args['height'] = $args['maxheight'];
  161. }
  162. $data = _wp_oembed_get_object()->get_data( $url, $args );
  163. if ( false === $data ) {
  164. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  165. }
  166. /**
  167. * Filters the oEmbed TTL value (time to live).
  168. *
  169. * Similar to the {@see 'oembed_ttl'} filter, but for the REST API
  170. * oEmbed proxy endpoint.
  171. *
  172. * @since 4.8.0
  173. *
  174. * @param int $time Time to live (in seconds).
  175. * @param string $url The attempted embed URL.
  176. * @param array $args An array of embed request arguments.
  177. */
  178. $ttl = apply_filters( 'rest_oembed_ttl', DAY_IN_SECONDS, $url, $args );
  179. set_transient( $cache_key, $data, $ttl );
  180. return $data;
  181. }
  182. }