No Description

class-wp-oembed-controller.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 REST 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. 'permission_callback' => '__return_true',
  40. 'args' => array(
  41. 'url' => array(
  42. 'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
  43. 'required' => true,
  44. 'type' => 'string',
  45. 'format' => 'uri',
  46. ),
  47. 'format' => array(
  48. 'default' => 'json',
  49. 'sanitize_callback' => 'wp_oembed_ensure_format',
  50. ),
  51. 'maxwidth' => array(
  52. 'default' => $maxwidth,
  53. 'sanitize_callback' => 'absint',
  54. ),
  55. ),
  56. ),
  57. )
  58. );
  59. register_rest_route(
  60. 'oembed/1.0',
  61. '/proxy',
  62. array(
  63. array(
  64. 'methods' => WP_REST_Server::READABLE,
  65. 'callback' => array( $this, 'get_proxy_item' ),
  66. 'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
  67. 'args' => array(
  68. 'url' => array(
  69. 'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
  70. 'required' => true,
  71. 'type' => 'string',
  72. 'format' => 'uri',
  73. ),
  74. 'format' => array(
  75. 'description' => __( 'The oEmbed format to use.' ),
  76. 'type' => 'string',
  77. 'default' => 'json',
  78. 'enum' => array(
  79. 'json',
  80. 'xml',
  81. ),
  82. ),
  83. 'maxwidth' => array(
  84. 'description' => __( 'The maximum width of the embed frame in pixels.' ),
  85. 'type' => 'integer',
  86. 'default' => $maxwidth,
  87. 'sanitize_callback' => 'absint',
  88. ),
  89. 'maxheight' => array(
  90. 'description' => __( 'The maximum height of the embed frame in pixels.' ),
  91. 'type' => 'integer',
  92. 'sanitize_callback' => 'absint',
  93. ),
  94. 'discover' => array(
  95. 'description' => __( 'Whether to perform an oEmbed discovery request for unsanctioned providers.' ),
  96. 'type' => 'boolean',
  97. 'default' => true,
  98. ),
  99. ),
  100. ),
  101. )
  102. );
  103. }
  104. /**
  105. * Callback for the embed API endpoint.
  106. *
  107. * Returns the JSON object for the post.
  108. *
  109. * @since 4.4.0
  110. *
  111. * @param WP_REST_Request $request Full data about the request.
  112. * @return array|WP_Error oEmbed response data or WP_Error on failure.
  113. */
  114. public function get_item( $request ) {
  115. $post_id = url_to_postid( $request['url'] );
  116. /**
  117. * Filters the determined post ID.
  118. *
  119. * @since 4.4.0
  120. *
  121. * @param int $post_id The post ID.
  122. * @param string $url The requested URL.
  123. */
  124. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
  125. $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
  126. if ( ! $data ) {
  127. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  128. }
  129. return $data;
  130. }
  131. /**
  132. * Checks if current user can make a proxy oEmbed request.
  133. *
  134. * @since 4.8.0
  135. *
  136. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  137. */
  138. public function get_proxy_item_permissions_check() {
  139. if ( ! current_user_can( 'edit_posts' ) ) {
  140. return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to make proxied oEmbed requests.' ), array( 'status' => rest_authorization_required_code() ) );
  141. }
  142. return true;
  143. }
  144. /**
  145. * Callback for the proxy API endpoint.
  146. *
  147. * Returns the JSON object for the proxied item.
  148. *
  149. * @since 4.8.0
  150. *
  151. * @see WP_oEmbed::get_html()
  152. * @global WP_Embed $wp_embed
  153. *
  154. * @param WP_REST_Request $request Full data about the request.
  155. * @return object|WP_Error oEmbed response data or WP_Error on failure.
  156. */
  157. public function get_proxy_item( $request ) {
  158. global $wp_embed;
  159. $args = $request->get_params();
  160. // Serve oEmbed data from cache if set.
  161. unset( $args['_wpnonce'] );
  162. $cache_key = 'oembed_' . md5( serialize( $args ) );
  163. $data = get_transient( $cache_key );
  164. if ( ! empty( $data ) ) {
  165. return $data;
  166. }
  167. $url = $request['url'];
  168. unset( $args['url'] );
  169. // Copy maxwidth/maxheight to width/height since WP_oEmbed::fetch() uses these arg names.
  170. if ( isset( $args['maxwidth'] ) ) {
  171. $args['width'] = $args['maxwidth'];
  172. }
  173. if ( isset( $args['maxheight'] ) ) {
  174. $args['height'] = $args['maxheight'];
  175. }
  176. // Short-circuit process for URLs belonging to the current site.
  177. $data = get_oembed_response_data_for_url( $url, $args );
  178. if ( $data ) {
  179. return $data;
  180. }
  181. $data = _wp_oembed_get_object()->get_data( $url, $args );
  182. if ( false === $data ) {
  183. // Try using a classic embed, instead.
  184. /* @var WP_Embed $wp_embed */
  185. $html = $wp_embed->get_embed_handler_html( $args, $url );
  186. if ( $html ) {
  187. global $wp_scripts;
  188. // Check if any scripts were enqueued by the shortcode, and include them in the response.
  189. $enqueued_scripts = array();
  190. foreach ( $wp_scripts->queue as $script ) {
  191. $enqueued_scripts[] = $wp_scripts->registered[ $script ]->src;
  192. }
  193. return (object) array(
  194. 'provider_name' => __( 'Embed Handler' ),
  195. 'html' => $html,
  196. 'scripts' => $enqueued_scripts,
  197. );
  198. }
  199. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  200. }
  201. /** This filter is documented in wp-includes/class-wp-oembed.php */
  202. $data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
  203. /**
  204. * Filters the oEmbed TTL value (time to live).
  205. *
  206. * Similar to the {@see 'oembed_ttl'} filter, but for the REST API
  207. * oEmbed proxy endpoint.
  208. *
  209. * @since 4.8.0
  210. *
  211. * @param int $time Time to live (in seconds).
  212. * @param string $url The attempted embed URL.
  213. * @param array $args An array of embed request arguments.
  214. */
  215. $ttl = apply_filters( 'rest_oembed_ttl', DAY_IN_SECONDS, $url, $args );
  216. set_transient( $cache_key, $data, $ttl );
  217. return $data;
  218. }
  219. }