class.wpcom-json-api-render-shortcode-endpoint.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. new WPCOM_JSON_API_Render_Shortcode_Endpoint( array(
  3. 'description' => "Get a rendered shortcode for a site. Note: The current user must have publishing access.",
  4. 'group' => 'sites',
  5. 'stat' => 'shortcodes:render',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/shortcodes/render',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'query_parameters' => array(
  12. 'shortcode' => '(string) The query-string encoded shortcode string to render. Required. Only accepts one at a time.',
  13. ),
  14. 'response_format' => array(
  15. 'shortcode' => '(string) The shortcode that was passed in for rendering.',
  16. 'result' => '(html) The rendered HTML result of the shortcode.',
  17. 'scripts' => '(array) An array of JavaScript files needed to render the shortcode. Returned in the format of <code>{ "script-slug" : { "src": "http://example.com/file.js", "extra" : "" } }</code> where extra contains any neccessary extra JS for initializing the source file and src contains the script to load. Omitted if no scripts are neccessary.',
  18. 'styles' => '(array) An array of CSS files needed to render the shortcode. Returned in the format of <code>{ "style-slug" : { "src": "http://example.com/file.css", "media" : "all" } }</code>. Omitted if no styles are neccessary.',
  19. ),
  20. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/shortcodes/render?shortcode=%5Bgallery%20ids%3D%22729%2C732%2C731%2C720%22%5D',
  21. 'example_request_data' => array(
  22. 'headers' => array(
  23. 'authorization' => 'Bearer YOUR_API_TOKEN'
  24. ),
  25. )
  26. ) );
  27. class WPCOM_JSON_API_Render_Shortcode_Endpoint extends WPCOM_JSON_API_Render_Endpoint {
  28. // /sites/%s/shortcodes/render -> $blog_id
  29. function callback( $path = '', $blog_id = 0 ) {
  30. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  31. if ( is_wp_error( $blog_id ) ) {
  32. return $blog_id;
  33. }
  34. if ( ! current_user_can( 'edit_posts' ) ) {
  35. return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
  36. }
  37. $args = $this->query_args();
  38. $shortcode = trim( $args['shortcode'] );
  39. // Quick validation - shortcodes should always be enclosed in brackets []
  40. if ( ! wp_startswith( $shortcode, '[' ) || ! wp_endswith( $shortcode, ']' ) ) {
  41. return new WP_Error( 'invalid_shortcode', 'The shortcode parameter must begin and end with square brackets.', 400 );
  42. }
  43. // Make sure only one shortcode is being rendered at a time
  44. $pattern = get_shortcode_regex();
  45. preg_match_all( "/$pattern/s", $shortcode, $matches );
  46. if ( count( $matches[0] ) > 1 ) {
  47. return new WP_Error( 'invalid_shortcode', 'Only one shortcode can be rendered at a time.', 400 );
  48. }
  49. $render = $this->process_render( array( $this, 'do_shortcode' ), $shortcode );
  50. // if nothing happened, then the shortcode does not exist.
  51. if ( $shortcode == $render['result'] ) {
  52. return new WP_Error( 'invalid_shortcode', 'The requested shortcode does not exist.', 400 );
  53. }
  54. // our output for this endpoint..
  55. $return['shortcode'] = $shortcode;
  56. $return['result'] = $render['result'];
  57. $return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] );
  58. return $return;
  59. }
  60. }