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