WPCOM_JSON_API_Render_Embed_Reversal_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-render-embed-reversal-endpoint.php  
  1. class WPCOM_JSON_API_Render_Embed_Reversal_Endpoint extends WPCOM_JSON_API_Render_Endpoint { 
  2. // /sites/%s/embeds/reversal -> $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. $is_shortcode = $is_embed = false; 
  12.  
  13. $input = $this->input( true ); 
  14. $maybe_embed = trim( $input['maybe_embed'] ); 
  15. if ( empty( $maybe_embed ) ) { 
  16. return new WP_Error( 'empty_embed', 'Please provide an embed code to process.', 400 ); 
  17.  
  18. $ksesed_content = trim( wp_strip_all_tags( wp_kses_post( $maybe_embed ), true ) ); 
  19. if ( empty( $ksesed_content ) ) { 
  20. return new WP_Error( 'invalid_embed', 'Invalid or empty embed provided.', 400 ); 
  21.  
  22. $shortcode_pattern = get_shortcode_regex(); 
  23. $url_pattern = '/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i'; 
  24. preg_match_all( "/$shortcode_pattern/s", $ksesed_content, $shortcode_matches ); 
  25. preg_match_all( "$url_pattern", $ksesed_content, $url_matches ); 
  26.  
  27. if ( empty( $shortcode_matches[0] ) && empty( $url_matches[0] ) ) 
  28. return new WP_Error( 'invalid_embed', 'The provided embed is not supported.', 400 ); 
  29.  
  30. if ( ( count( $shortcode_matches[0] ) + count( $url_matches[0] ) ) > 1 ) { 
  31. return new WP_Error( 'invalid_embed', 'Only one embed/shortcode reversal can be rendered at a time.', 400 ); 
  32.  
  33. if ( ! empty( $shortcode_matches[0] ) ) { 
  34. $is_shortcode = true; 
  35. } elseif ( ! empty( $url_matches[0] ) ) { 
  36. $is_embed = true; 
  37.  
  38. $render = $this->process_render( array( $this, 'render_shortcode_reversal' ), array( 'shortcode_reversal' => $ksesed_content, 'is_shortcode' => $is_shortcode, 'is_embed' => $is_embed ) ); 
  39.  
  40.  
  41. // if nothing happened, then the shortcode does not exist. 
  42. global $wp_embed; 
  43. if ( empty( $render ) || empty( $render['result'] ) || $ksesed_content == $render['result'] || $wp_embed->maybe_make_link( $maybe_embed ) == $render['result'] ) { 
  44. return new WP_Error( 'invalid_embed', 'The provided embed is not supported.', 400 ); 
  45.  
  46. // our output for this endpoint.. 
  47. $return['maybe_embed'] = $maybe_embed; 
  48. $return['result'] = $ksesed_content; 
  49. $return['reversal_type'] = ( $is_embed ) ? 'embed' : 'shortcode'; 
  50. $return['render_result'] = $render['result']; 
  51.  
  52. $return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] ); 
  53.  
  54. return $return; 
  55.  
  56. function render_shortcode_reversal( $args ) { 
  57. if ( $args['is_shortcode'] ) { 
  58. return call_user_func( array( $this, 'do_shortcode' ), $args['shortcode_reversal'] ); 
  59. } else if ( $args['is_embed'] ) { 
  60. return call_user_func( array( $this, 'do_embed' ), $args['shortcode_reversal'] ); 
  61. return false; 
  62.