WPCOM_JSON_API_Render_Embed_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Render Embed Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-render-embed-endpoint.php  
  1. class WPCOM_JSON_API_Render_Embed_Endpoint extends WPCOM_JSON_API_Render_Endpoint { 
  2. // /sites/%s/embeds/render -> $blog_id 
  3. function callback( $path = '', $blog_id = 0 ) { 
  4. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  5. if ( is_wp_error( $blog_id ) ) { 
  6. return $blog_id; 
  7.  
  8. if ( ! current_user_can( 'edit_posts' ) ) { 
  9. return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 ); 
  10.  
  11. $args = $this->query_args(); 
  12. $embed_url = trim( $args['embed_url'] ); 
  13.  
  14. // quick validation 
  15. if ( ! preg_match_all( '|^\s*(https?://[^\s"]+)\s*$|im', $embed_url, $matches ) ) { 
  16. return new WP_Error( 'invalid_embed_url', 'The embed_url parameter must be a valid URL.', 400 ); 
  17.  
  18. if ( count( $matches[1] ) > 1 ) { 
  19. return new WP_Error( 'invalid_embed', 'Only one embed can be rendered at a time.', 400 ); 
  20.  
  21. $embed_url = array_shift( $matches[1] ); 
  22. $parts = parse_url( $embed_url ); 
  23. if ( ! $parts ) { 
  24. return new WP_Error( 'invalid_embed_url', 'The embed_url parameter must be a valid URL.', 400 ); 
  25.  
  26. // in order for oEmbed to fire in the `$wp_embed->shortcode` method, we need to set a post as the current post 
  27. $_posts = get_posts( array( 'posts_per_page' => 1, 'suppress_filters' => false ) ); 
  28. if ( ! empty( $_posts ) ) { 
  29. global $post; 
  30. $post = array_shift( $_posts ); 
  31.  
  32. global $wp_embed; 
  33. $render = $this->process_render( array( $this, 'do_embed' ), $embed_url ); 
  34.  
  35. // if nothing happened, then the shortcode does not exist. 
  36. $is_an_embed = ( $embed_url != $render['result'] && $wp_embed->maybe_make_link( $embed_url ) != $render['result'] ); 
  37. if ( ! $is_an_embed ) { 
  38. return new WP_Error( 'invalid_embed', 'The requested URL is not an embed.', 400 ); 
  39.  
  40. // our output for this endpoint.. 
  41. $return['embed_url'] = $embed_url; 
  42. $return['result'] = $render['result']; 
  43.  
  44. $return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] ); 
  45.  
  46. return $return; 
  47.  
  48. function do_embed( $embed_url ) { 
  49. global $wp_embed; 
  50. return $wp_embed->shortcode( array(), $embed_url );