You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

90 lines
2.0 KiB

  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. }
  54. /**
  55. * Callback for the API endpoint.
  56. *
  57. * Returns the JSON object for the post.
  58. *
  59. * @since 4.4.0
  60. * @access public
  61. *
  62. * @param WP_REST_Request $request Full data about the request.
  63. * @return WP_Error|array oEmbed response data or WP_Error on failure.
  64. */
  65. public function get_item( $request ) {
  66. $post_id = url_to_postid( $request['url'] );
  67. /**
  68. * Filters the determined post ID.
  69. *
  70. * @since 4.4.0
  71. *
  72. * @param int $post_id The post ID.
  73. * @param string $url The requested URL.
  74. */
  75. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
  76. $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
  77. if ( ! $data ) {
  78. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
  79. }
  80. return $data;
  81. }
  82. }