/json-endpoints/class.wpcom-json-api-render-embed-reversal-endpoint.php

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