class-wp-oembed-controller.php 5.5 KB

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