class.wpcom-json-api-list-shortcodes-endpoint.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. new WPCOM_JSON_API_List_Shortcodes_Endpoint( array(
  3. 'description' => "Get a list of shortcodes available on a site. Note: The current user must have publishing access.",
  4. 'group' => 'sites',
  5. 'stat' => 'shortcodes',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/shortcodes',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. ),
  11. 'response_format' => array(
  12. 'shortcodes' => '(array) A list of supported shortcodes by their handle.',
  13. ),
  14. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/shortcodes',
  15. 'example_request_data' => array(
  16. 'headers' => array(
  17. 'authorization' => 'Bearer YOUR_API_TOKEN'
  18. ),
  19. )
  20. ) );
  21. class WPCOM_JSON_API_List_Shortcodes_Endpoint extends WPCOM_JSON_API_Endpoint {
  22. // /sites/%s/shortcodes -> $blog_id
  23. function callback( $path = '', $blog_id = 0 ) {
  24. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  25. if ( is_wp_error( $blog_id ) ) {
  26. return $blog_id;
  27. }
  28. // permissions check
  29. if ( ! current_user_can( 'edit_posts' ) ) {
  30. return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
  31. }
  32. // list em
  33. global $shortcode_tags;
  34. $output = array( 'shortcodes' => array() );
  35. foreach ( $shortcode_tags as $tag => $class ) {
  36. if ( '__return_false' == $class )
  37. continue;
  38. $output['shortcodes'][] = $tag;
  39. }
  40. return $output;
  41. }
  42. }