class.wpcom-json-api-list-shortcodes-endpoint.php
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | /** |
| 4 | * List shortcodes endpoint. |
| 5 | */ |
| 6 | new WPCOM_JSON_API_List_Shortcodes_Endpoint( |
| 7 | array( |
| 8 | 'description' => 'Get a list of shortcodes available on a site. Note: The current user must have publishing access.', |
| 9 | 'group' => 'sites', |
| 10 | 'stat' => 'shortcodes', |
| 11 | 'method' => 'GET', |
| 12 | 'path' => '/sites/%s/shortcodes', |
| 13 | 'path_labels' => array( |
| 14 | '$site' => '(int|string) Site ID or domain', |
| 15 | ), |
| 16 | 'response_format' => array( |
| 17 | 'shortcodes' => '(array) A list of supported shortcodes by their handle.', |
| 18 | ), |
| 19 | 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/shortcodes', |
| 20 | 'example_request_data' => array( |
| 21 | 'headers' => array( |
| 22 | 'authorization' => 'Bearer YOUR_API_TOKEN', |
| 23 | ), |
| 24 | ), |
| 25 | ) |
| 26 | ); |
| 27 | |
| 28 | /** |
| 29 | * List shortcodes endpoint class |
| 30 | * |
| 31 | * /sites/%s/shortcodes -> $blog_id |
| 32 | */ |
| 33 | class WPCOM_JSON_API_List_Shortcodes_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 34 | /** |
| 35 | * API callback. |
| 36 | * |
| 37 | * @param string $path - the path. |
| 38 | * @param string $blog_id - the blog ID. |
| 39 | */ |
| 40 | public function callback( $path = '', $blog_id = 0 ) { |
| 41 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 42 | if ( is_wp_error( $blog_id ) ) { |
| 43 | return $blog_id; |
| 44 | } |
| 45 | |
| 46 | // permissions check. |
| 47 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 48 | return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 ); |
| 49 | } |
| 50 | |
| 51 | // list em. |
| 52 | global $shortcode_tags; |
| 53 | $output = array( 'shortcodes' => array() ); |
| 54 | |
| 55 | foreach ( $shortcode_tags as $tag => $class ) { |
| 56 | if ( '__return_false' === $class ) { |
| 57 | continue; |
| 58 | } |
| 59 | $output['shortcodes'][] = $tag; |
| 60 | } |
| 61 | |
| 62 | return $output; |
| 63 | } |
| 64 | } |
| 65 |