No Description

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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. 'allow_fallback_to_jetpack_blog_token' => true,
  22. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tag/terms/slug:wordpresscom',
  23. ) );
  24. class WPCOM_JSON_API_Get_Term_Endpoint extends WPCOM_JSON_API_Endpoint {
  25. // /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
  26. function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) {
  27. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  28. if ( is_wp_error( $blog_id ) ) {
  29. return $blog_id;
  30. }
  31. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  32. $this->load_theme_functions();
  33. }
  34. $taxonomy_meta = get_taxonomy( $taxonomy );
  35. if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&
  36. ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
  37. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
  38. }
  39. $args = $this->query_args();
  40. $term = $this->get_taxonomy( $slug, $taxonomy, $args['context'] );
  41. if ( ! $term || is_wp_error( $term ) ) {
  42. return $term;
  43. }
  44. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  45. do_action( 'wpcom_json_api_objects', 'terms' );
  46. return $term;
  47. }
  48. }