class.wpcom-json-api-get-term-endpoint.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. new WPCOM_JSON_API_Get_Term_Endpoint( array(
  3. 'description' => 'Get information about a single term.',
  4. 'group' => 'taxonomy',
  5. 'stat' => 'terms:1',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/taxonomies/%s/terms/slug:%s',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$taxonomy' => '(string) Taxonomy',
  11. '$slug' => '(string) Term slug',
  12. ),
  13. 'response_format' => array(
  14. 'ID' => '(int) The term ID.',
  15. 'name' => '(string) The name of the term.',
  16. 'slug' => '(string) The slug of the term.',
  17. 'description' => '(string) The description of the term.',
  18. 'post_count' => '(int) The number of posts using this term.',
  19. 'parent' => '(int) The parent ID for the term, if hierarchical.',
  20. ),
  21. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tag/terms/slug:wordpresscom'
  22. ) );
  23. class WPCOM_JSON_API_Get_Term_Endpoint extends WPCOM_JSON_API_Endpoint {
  24. // /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
  25. function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) {
  26. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  27. if ( is_wp_error( $blog_id ) ) {
  28. return $blog_id;
  29. }
  30. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  31. $this->load_theme_functions();
  32. }
  33. $taxonomy_meta = get_taxonomy( $taxonomy );
  34. if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&
  35. ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
  36. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
  37. }
  38. $args = $this->query_args();
  39. $term = $this->get_taxonomy( $slug, $taxonomy, $args['context'] );
  40. if ( ! $term || is_wp_error( $term ) ) {
  41. return $term;
  42. }
  43. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  44. do_action( 'wpcom_json_api_objects', 'terms' );
  45. return $term;
  46. }
  47. }