Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.45% covered (danger)
20.45%
9 / 44
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Get_Taxonomy_Endpoint
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
5.39
0.00% covered (danger)
0.00%
0 / 1
 callback
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
5.39
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Get_Taxonomy_Endpoint(
8    array(
9        'description'                          => 'Get information about a single category.',
10        'group'                                => 'taxonomy',
11        'stat'                                 => 'categories:1',
12
13        'method'                               => 'GET',
14        'path'                                 => '/sites/%s/categories/slug:%s',
15        'path_labels'                          => array(
16            '$site'     => '(int|string) Site ID or domain',
17            '$category' => '(string) The category slug',
18        ),
19
20        'allow_fallback_to_jetpack_blog_token' => true,
21
22        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/categories/slug:community',
23    )
24);
25
26new WPCOM_JSON_API_Get_Taxonomy_Endpoint(
27    array(
28        'description'                          => 'Get information about a single tag.',
29        'group'                                => 'taxonomy',
30        'stat'                                 => 'tags:1',
31
32        'method'                               => 'GET',
33        'path'                                 => '/sites/%s/tags/slug:%s',
34        'path_labels'                          => array(
35            '$site' => '(int|string) Site ID or domain',
36            '$tag'  => '(string) The tag slug',
37        ),
38
39        'allow_fallback_to_jetpack_blog_token' => true,
40
41        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/tags/slug:wordpresscom',
42    )
43);
44
45/**
46 * GET Taxonomy endpoint class.
47 *
48 * @phan-constructor-used-for-side-effects
49 */
50class WPCOM_JSON_API_Get_Taxonomy_Endpoint extends WPCOM_JSON_API_Taxonomy_Endpoint {
51    /**
52     *
53     * API callback.
54     *
55     * /sites/%s/tags/slug:%s       -> $blog_id, $tag_id
56     * /sites/%s/categories/slug:%s -> $blog_id, $tag_id
57     *
58     * @param string $path - the path.
59     * @param int    $blog_id - the blog ID.
60     * @param int    $taxonomy_id - the taxonomy ID.
61     */
62    public function callback( $path = '', $blog_id = 0, $taxonomy_id = 0 ) {
63        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
64        if ( is_wp_error( $blog_id ) ) {
65            return $blog_id;
66        }
67
68        $args = $this->query_args();
69        if ( preg_match( '#/tags/#i', $path ) ) {
70            $taxonomy_type = 'post_tag';
71        } else {
72            $taxonomy_type = 'category';
73        }
74
75        $return = $this->get_taxonomy( $taxonomy_id, $taxonomy_type, $args['context'] );
76        if ( ! $return || is_wp_error( $return ) ) {
77            return $return;
78        }
79
80        /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
81        do_action( 'wpcom_json_api_objects', 'taxonomies' );
82
83        return $return;
84    }
85}