← All changes
|
json-endpoints/class.wpcom-json-api-get-term-endpoint.php
+78
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,78 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | + | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit( 0 ); | |
| 5 | +} | |
| 6 | + | |
| 7 | +new WPCOM_JSON_API_Get_Term_Endpoint( | |
| 8 | + array( | |
| 9 | + 'description' => 'Get information about a single term.', | |
| 10 | + 'group' => 'taxonomy', | |
| 11 | + 'stat' => 'terms:1', | |
| 12 | + 'method' => 'GET', | |
| 13 | + 'path' => '/sites/%s/taxonomies/%s/terms/slug:%s', | |
| 14 | + 'path_labels' => array( | |
| 15 | + '$site' => '(int|string) Site ID or domain', | |
| 16 | + '$taxonomy' => '(string) Taxonomy', | |
| 17 | + '$slug' => '(string) Term slug', | |
| 18 | + ), | |
| 19 | + 'response_format' => array( | |
| 20 | + 'ID' => '(int) The term ID.', | |
| 21 | + 'name' => '(string) The name of the term.', | |
| 22 | + 'slug' => '(string) The slug of the term.', | |
| 23 | + 'description' => '(string) The description of the term.', | |
| 24 | + 'post_count' => '(int) The number of posts using this term.', | |
| 25 | + 'parent' => '(int) The parent ID for the term, if hierarchical.', | |
| 26 | + ), | |
| 27 | + | |
| 28 | + 'allow_fallback_to_jetpack_blog_token' => true, | |
| 29 | + | |
| 30 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tag/terms/slug:wordpresscom', | |
| 31 | + ) | |
| 32 | +); | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * GET Term endpoint class. | |
| 36 | + * | |
| 37 | + * @phan-constructor-used-for-side-effects | |
| 38 | + */ | |
| 39 | +class WPCOM_JSON_API_Get_Term_Endpoint extends WPCOM_JSON_API_Endpoint { | |
| 40 | + /** | |
| 41 | + * | |
| 42 | + * API callback. | |
| 43 | + * | |
| 44 | + * /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug | |
| 45 | + * | |
| 46 | + * @param string $path - the path. | |
| 47 | + * @param int $blog_id - the blog ID. | |
| 48 | + * @param string $taxonomy - the taxonomy type. | |
| 49 | + * @param int $slug - the slug. | |
| 50 | + */ | |
| 51 | + public function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) { | |
| 52 | + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); | |
| 53 | + if ( is_wp_error( $blog_id ) ) { | |
| 54 | + return $blog_id; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 58 | + $this->load_theme_functions(); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $taxonomy_meta = get_taxonomy( $taxonomy ); | |
| 62 | + if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public && | |
| 63 | + ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) { | |
| 64 | + return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + $args = $this->query_args(); | |
| 68 | + $term = $this->get_taxonomy( $slug, $taxonomy, $args['context'] ); | |
| 69 | + if ( ! $term || is_wp_error( $term ) ) { | |
| 70 | + return $term; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ | |
| 74 | + do_action( 'wpcom_json_api_objects', 'terms' ); | |
| 75 | + | |
| 76 | + return $term; | |
| 77 | + } | |
| 78 | +} | |