WP_oEmbed_Controller

OEmbed API endpoint controller.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-oembed-controller.php  
  1. final class WP_oEmbed_Controller { 
  2. /** 
  3. * Register the oEmbed REST API route. 
  4. * @since 4.4.0 
  5. * @access public 
  6. */ 
  7. public function register_routes() { 
  8. /** 
  9. * Filters the maxwidth oEmbed parameter. 
  10. * @since 4.4.0 
  11. * @param int $maxwidth Maximum allowed width. Default 600. 
  12. */ 
  13. $maxwidth = apply_filters( 'oembed_default_width', 600 ); 
  14.  
  15. register_rest_route( 'oembed/1.0', '/embed', array( 
  16. array( 
  17. 'methods' => WP_REST_Server::READABLE,  
  18. 'callback' => array( $this, 'get_item' ),  
  19. 'args' => array( 
  20. 'url' => array( 
  21. 'required' => true,  
  22. 'sanitize_callback' => 'esc_url_raw',  
  23. ),  
  24. 'format' => array( 
  25. 'default' => 'json',  
  26. 'sanitize_callback' => 'wp_oembed_ensure_format',  
  27. ),  
  28. 'maxwidth' => array( 
  29. 'default' => $maxwidth,  
  30. 'sanitize_callback' => 'absint',  
  31. ),  
  32. ),  
  33. ),  
  34. ) ); 
  35.  
  36. /** 
  37. * Callback for the API endpoint. 
  38. * Returns the JSON object for the post. 
  39. * @since 4.4.0 
  40. * @access public 
  41. * @param WP_REST_Request $request Full data about the request. 
  42. * @return WP_Error|array oEmbed response data or WP_Error on failure. 
  43. */ 
  44. public function get_item( $request ) { 
  45. $post_id = url_to_postid( $request['url'] ); 
  46.  
  47. /** 
  48. * Filters the determined post ID. 
  49. * @since 4.4.0 
  50. * @param int $post_id The post ID. 
  51. * @param string $url The requested URL. 
  52. */ 
  53. $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] ); 
  54.  
  55. $data = get_oembed_response_data( $post_id, $request['maxwidth'] ); 
  56.  
  57. if ( ! $data ) { 
  58. return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); 
  59.  
  60. return $data;