class.wpcom-json-api-get-taxonomy-endpoint.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. new WPCOM_JSON_API_Get_Taxonomy_Endpoint( array(
  3. 'description' => 'Get information about a single category.',
  4. 'group' => 'taxonomy',
  5. 'stat' => 'categories:1',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/categories/slug:%s',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$category' => '(string) The category slug'
  11. ),
  12. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/categories/slug:community'
  13. ) );
  14. new WPCOM_JSON_API_Get_Taxonomy_Endpoint( array(
  15. 'description' => 'Get information about a single tag.',
  16. 'group' => 'taxonomy',
  17. 'stat' => 'tags:1',
  18. 'method' => 'GET',
  19. 'path' => '/sites/%s/tags/slug:%s',
  20. 'path_labels' => array(
  21. '$site' => '(int|string) Site ID or domain',
  22. '$tag' => '(string) The tag slug'
  23. ),
  24. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/tags/slug:wordpresscom'
  25. ) );
  26. class WPCOM_JSON_API_Get_Taxonomy_Endpoint extends WPCOM_JSON_API_Taxonomy_Endpoint {
  27. // /sites/%s/tags/slug:%s -> $blog_id, $tag_id
  28. // /sites/%s/categories/slug:%s -> $blog_id, $tag_id
  29. function callback( $path = '', $blog_id = 0, $taxonomy_id = 0 ) {
  30. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  31. if ( is_wp_error( $blog_id ) ) {
  32. return $blog_id;
  33. }
  34. $args = $this->query_args();
  35. if ( preg_match( '#/tags/#i', $path ) ) {
  36. $taxonomy_type = "post_tag";
  37. } else {
  38. $taxonomy_type = "category";
  39. }
  40. $return = $this->get_taxonomy( $taxonomy_id, $taxonomy_type, $args['context'] );
  41. if ( !$return || is_wp_error( $return ) ) {
  42. return $return;
  43. }
  44. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  45. do_action( 'wpcom_json_api_objects', 'taxonomies' );
  46. return $return;
  47. }
  48. }