/json-endpoints/class.wpcom-json-api-render-shortcode-endpoint.php

  1. <?php 
  2. class WPCOM_JSON_API_Render_Shortcode_Endpoint extends WPCOM_JSON_API_Render_Endpoint { 
  3. // /sites/%s/shortcodes/render -> $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. $args = $this->query_args(); 
  13. $shortcode = trim( $args['shortcode'] ); 
  14.  
  15. // Quick validation - shortcodes should always be enclosed in brackets [] 
  16. if ( ! wp_startswith( $shortcode, '[' ) || ! wp_endswith( $shortcode, ']' ) ) { 
  17. return new WP_Error( 'invalid_shortcode', 'The shortcode parameter must begin and end with square brackets.', 400 ); 
  18.  
  19. // Make sure only one shortcode is being rendered at a time 
  20. $pattern = get_shortcode_regex(); 
  21. preg_match_all( "/$pattern/s", $shortcode, $matches ); 
  22. if ( count( $matches[0] ) > 1 ) { 
  23. return new WP_Error( 'invalid_shortcode', 'Only one shortcode can be rendered at a time.', 400 ); 
  24.  
  25. $render = $this->process_render( array( $this, 'do_shortcode' ), $shortcode ); 
  26.  
  27. // if nothing happened, then the shortcode does not exist. 
  28. if ( $shortcode == $render['result'] ) { 
  29. return new WP_Error( 'invalid_shortcode', 'The requested shortcode does not exist.', 400 ); 
  30.  
  31. // our output for this endpoint.. 
  32. $return['shortcode'] = $shortcode; 
  33. $return['result'] = $render['result']; 
  34.  
  35. $return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] ); 
  36.  
  37. return $return; 
  38.  
  39. function do_shortcode( $shortcode ) { 
  40. $result = do_shortcode( $shortcode ); 
  41. return $result; 
  42.  
.