jetpack
5 years ago
class.wpcom-json-api-add-widget-endpoint.php
8 years ago
class.wpcom-json-api-autosave-post-v1-1-endpoint.php
6 years ago
class.wpcom-json-api-bulk-delete-post-endpoint.php
8 years ago
class.wpcom-json-api-bulk-restore-post-endpoint.php
8 years ago
class.wpcom-json-api-bulk-update-comments-endpoint.php
7 years ago
class.wpcom-json-api-comment-endpoint.php
7 years ago
class.wpcom-json-api-delete-media-endpoint.php
8 years ago
class.wpcom-json-api-delete-media-v1-1-endpoint.php
8 years ago
class.wpcom-json-api-edit-media-v1-2-endpoint.php
3 years ago
class.wpcom-json-api-get-autosave-v1-1-endpoint.php
6 years ago
class.wpcom-json-api-get-comment-counts-endpoint.php
5 years ago
class.wpcom-json-api-get-comment-endpoint.php
5 years ago
class.wpcom-json-api-get-comment-history-endpoint.php
5 years ago
class.wpcom-json-api-get-comments-tree-endpoint.php
5 years ago
class.wpcom-json-api-get-comments-tree-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-get-comments-tree-v1-2-endpoint.php
5 years ago
class.wpcom-json-api-get-customcss.php
8 years ago
class.wpcom-json-api-get-media-endpoint.php
8 years ago
class.wpcom-json-api-get-media-v1-1-endpoint.php
8 years ago
class.wpcom-json-api-get-media-v1-2-endpoint.php
8 years ago
class.wpcom-json-api-get-post-counts-v1-1-endpoint.php
6 years ago
class.wpcom-json-api-get-post-endpoint.php
5 years ago
class.wpcom-json-api-get-post-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-get-site-endpoint.php
5 years ago
class.wpcom-json-api-get-site-v1-2-endpoint.php
5 years ago
class.wpcom-json-api-get-taxonomies-endpoint.php
5 years ago
class.wpcom-json-api-get-taxonomy-endpoint.php
5 years ago
class.wpcom-json-api-get-term-endpoint.php
5 years ago
class.wpcom-json-api-list-comments-endpoint.php
5 years ago
class.wpcom-json-api-list-embeds-endpoint.php
8 years ago
class.wpcom-json-api-list-media-endpoint.php
8 years ago
class.wpcom-json-api-list-media-v1-1-endpoint.php
8 years ago
class.wpcom-json-api-list-media-v1-2-endpoint.php
8 years ago
class.wpcom-json-api-list-post-type-taxonomies-endpoint.php
8 years ago
class.wpcom-json-api-list-post-types-endpoint.php
5 years ago
class.wpcom-json-api-list-posts-endpoint.php
5 years ago
class.wpcom-json-api-list-posts-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-list-posts-v1-2-endpoint.php
5 years ago
class.wpcom-json-api-list-roles-endpoint.php
5 years ago
class.wpcom-json-api-list-shortcodes-endpoint.php
8 years ago
class.wpcom-json-api-list-terms-endpoint.php
5 years ago
class.wpcom-json-api-list-users-endpoint.php
8 years ago
class.wpcom-json-api-menus-v1-1-endpoint.php
8 years ago
class.wpcom-json-api-post-endpoint.php
5 years ago
class.wpcom-json-api-post-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-render-embed-endpoint.php
6 years ago
class.wpcom-json-api-render-embed-reversal-endpoint.php
8 years ago
class.wpcom-json-api-render-endpoint.php
5 years ago
class.wpcom-json-api-render-shortcode-endpoint.php
8 years ago
class.wpcom-json-api-sharing-buttons-endpoint.php
8 years ago
class.wpcom-json-api-site-settings-endpoint.php
5 years ago
class.wpcom-json-api-site-settings-v1-2-endpoint.php
5 years ago
class.wpcom-json-api-site-settings-v1-3-endpoint.php
5 years ago
class.wpcom-json-api-site-settings-v1-4-endpoint.php
5 years ago
class.wpcom-json-api-site-user-endpoint.php
8 years ago
class.wpcom-json-api-taxonomy-endpoint.php
7 years ago
class.wpcom-json-api-update-comment-endpoint.php
5 years ago
class.wpcom-json-api-update-customcss.php
8 years ago
class.wpcom-json-api-update-media-endpoint.php
8 years ago
class.wpcom-json-api-update-media-v1-1-endpoint.php
8 years ago
class.wpcom-json-api-update-post-endpoint.php
5 years ago
class.wpcom-json-api-update-post-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-update-post-v1-2-endpoint.php
5 years ago
class.wpcom-json-api-update-site-homepage-endpoint.php
5 years ago
class.wpcom-json-api-update-site-logo-endpoint.php
5 years ago
class.wpcom-json-api-update-taxonomy-endpoint.php
8 years ago
class.wpcom-json-api-update-term-endpoint.php
7 years ago
class.wpcom-json-api-update-user-endpoint.php
5 years ago
class.wpcom-json-api-upload-media-endpoint.php
8 years ago
class.wpcom-json-api-upload-media-v1-1-endpoint.php
5 years ago
class.wpcom-json-api-list-terms-endpoint.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | new WPCOM_JSON_API_List_Terms_Endpoint( array( |
| 4 | 'description' => 'Get a list of a site\'s terms by taxonomy.', |
| 5 | 'group' => 'taxonomy', |
| 6 | 'stat' => 'terms', |
| 7 | 'method' => 'GET', |
| 8 | 'path' => '/sites/%s/taxonomies/%s/terms', |
| 9 | 'path_labels' => array( |
| 10 | '$site' => '(int|string) Site ID or domain', |
| 11 | '$taxonomy' => '(string) Taxonomy', |
| 12 | ), |
| 13 | 'query_parameters' => array( |
| 14 | 'number' => '(int=100) The number of terms to return. Limit: 1000.', |
| 15 | 'offset' => '(int=0) 0-indexed offset.', |
| 16 | 'page' => '(int) Return the Nth 1-indexed page of terms. Takes precedence over the <code>offset</code> parameter.', |
| 17 | 'search' => '(string) Limit response to include only terms whose names or slugs match the provided search query.', |
| 18 | 'order' => array( |
| 19 | 'ASC' => 'Return terms in ascending order.', |
| 20 | 'DESC' => 'Return terms in descending order.', |
| 21 | ), |
| 22 | 'order_by' => array( |
| 23 | 'name' => 'Order by the name of each tag.', |
| 24 | 'count' => 'Order by the number of posts in each tag.', |
| 25 | ), |
| 26 | ), |
| 27 | |
| 28 | 'allow_fallback_to_jetpack_blog_token' => true, |
| 29 | |
| 30 | 'response_format' => array( |
| 31 | 'found' => '(int) The number of terms returned.', |
| 32 | 'terms' => '(array) Array of tag objects.', |
| 33 | ), |
| 34 | 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tags/terms?number=5' |
| 35 | ) ); |
| 36 | |
| 37 | class WPCOM_JSON_API_List_Terms_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 38 | // /sites/%s/taxonomies/%s/terms -> $blog_id, $taxonomy |
| 39 | function callback( $path = '', $blog_id = 0, $taxonomy = 'category' ) { |
| 40 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 41 | if ( is_wp_error( $blog_id ) ) { |
| 42 | return $blog_id; |
| 43 | } |
| 44 | |
| 45 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 46 | $this->load_theme_functions(); |
| 47 | } |
| 48 | |
| 49 | $taxonomy_meta = get_taxonomy( $taxonomy ); |
| 50 | if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public && |
| 51 | ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) { |
| 52 | return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 ); |
| 53 | } |
| 54 | |
| 55 | $args = $this->query_args(); |
| 56 | $args = $this->process_args( $args ); |
| 57 | |
| 58 | $formatted_terms = $this->get_formatted_terms( $taxonomy, $args ); |
| 59 | |
| 60 | if ( ! empty( $formatted_terms ) ) { |
| 61 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
| 62 | do_action( 'wpcom_json_api_objects', 'terms', count( $formatted_terms ) ); |
| 63 | } |
| 64 | |
| 65 | return array( |
| 66 | 'found' => (int) $this->get_found( $taxonomy, $args ), |
| 67 | 'terms' => (array) $formatted_terms |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | function process_args( $args ) { |
| 72 | $args['get'] = 'all'; |
| 73 | |
| 74 | if ( $args['number'] < 1 ) { |
| 75 | $args['number'] = 100; |
| 76 | } elseif ( 1000 < $args['number'] ) { |
| 77 | return new WP_Error( 'invalid_number', 'The number parameter must be less than or equal to 1000.', 400 ); |
| 78 | } |
| 79 | |
| 80 | if ( isset( $args['page'] ) ) { |
| 81 | if ( $args['page'] < 1 ) { |
| 82 | $args['page'] = 1; |
| 83 | } |
| 84 | |
| 85 | $args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
| 86 | unset( $args['page'] ); |
| 87 | } |
| 88 | |
| 89 | if ( $args['offset'] < 0 ) { |
| 90 | $args['offset'] = 0; |
| 91 | } |
| 92 | |
| 93 | $args['orderby'] = $args['order_by']; |
| 94 | unset( $args['order_by'] ); |
| 95 | |
| 96 | unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] ); |
| 97 | return $args; |
| 98 | } |
| 99 | |
| 100 | function get_found( $taxonomy, $args ) { |
| 101 | unset( $args['offset'] ); |
| 102 | return wp_count_terms( $taxonomy, $args ); |
| 103 | } |
| 104 | |
| 105 | function get_formatted_terms( $taxonomy, $args ) { |
| 106 | $terms = get_terms( $taxonomy, $args ); |
| 107 | |
| 108 | $formatted_terms = array(); |
| 109 | foreach ( $terms as $term ) { |
| 110 | $formatted_terms[] = $this->format_taxonomy( $term, $taxonomy, 'display' ); |
| 111 | } |
| 112 | |
| 113 | return $formatted_terms; |
| 114 | } |
| 115 | } |
| 116 |