Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 115
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Get_Taxonomies_Endpoint
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 4
210
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 process_args
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 categories
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 tags
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Get_Taxonomies_Endpoint(
8    array(
9        'description'                          => "Get a list of a site's categories.",
10        'group'                                => 'taxonomy',
11        'stat'                                 => 'categories',
12        'method'                               => 'GET',
13        'path'                                 => '/sites/%s/categories',
14        'path_labels'                          => array(
15            '$site' => '(int|string) Site ID or domain',
16        ),
17        'query_parameters'                     => array(
18            'number'   => '(int=100) The number of categories to return. Limit: 1000.',
19            'offset'   => '(int=0) 0-indexed offset.',
20            'page'     => '(int) Return the Nth 1-indexed page of categories. Takes precedence over the <code>offset</code> parameter.',
21            'search'   => '(string) Limit response to include only categories whose names or slugs match the provided search query.',
22            'order'    => array(
23                'ASC'  => 'Return categories in ascending order.',
24                'DESC' => 'Return categories in descending order.',
25            ),
26            'order_by' => array(
27                'name'  => 'Order by the name of each category.',
28                'count' => 'Order by the number of posts in each category.',
29            ),
30        ),
31        'response_format'                      => array(
32            'found'      => '(int) The number of categories returned.',
33            'categories' => '(array) Array of category objects.',
34        ),
35
36        'allow_fallback_to_jetpack_blog_token' => true,
37
38        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/categories/?number=5',
39    )
40);
41
42new WPCOM_JSON_API_Get_Taxonomies_Endpoint(
43    array(
44        'description'                          => "Get a list of a site's tags.",
45        'group'                                => 'taxonomy',
46        'stat'                                 => 'tags',
47        'method'                               => 'GET',
48        'path'                                 => '/sites/%s/tags',
49        'path_labels'                          => array(
50            '$site' => '(int|string) Site ID or domain',
51        ),
52        'query_parameters'                     => array(
53            'number'   => '(int=100) The number of tags to return. Limit: 1000.',
54            'offset'   => '(int=0) 0-indexed offset.',
55            'page'     => '(int) Return the Nth 1-indexed page of tags. Takes precedence over the <code>offset</code> parameter.',
56            'search'   => '(string) Limit response to include only tags whose names or slugs match the provided search query.',
57            'order'    => array(
58                'ASC'  => 'Return tags in ascending order.',
59                'DESC' => 'Return tags in descending order.',
60            ),
61            'order_by' => array(
62                'name'  => 'Order by the name of each tag.',
63                'count' => 'Order by the number of posts in each tag.',
64            ),
65        ),
66
67        'allow_fallback_to_jetpack_blog_token' => true,
68
69        'response_format'                      => array(
70            'found' => '(int) The number of tags returned.',
71            'tags'  => '(array) Array of tag objects.',
72        ),
73        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/tags/?number=5',
74    )
75);
76
77/**
78 * GET taxonomies endpoint class.
79 *
80 * @phan-constructor-used-for-side-effects
81 */
82class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint {
83    /**
84     *
85     * API callback.
86     * /sites/%s/tags       -> $blog_id
87     * /sites/%s/categories -> $blog_id
88     *
89     * @param string $path - the path.
90     * @param int    $blog_id - the blog ID.
91     */
92    public function callback( $path = '', $blog_id = 0 ) {
93        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
94        if ( is_wp_error( $blog_id ) ) {
95            return $blog_id;
96        }
97
98        $args = $this->query_args();
99        $args = $this->process_args( $args );
100        if ( is_wp_error( $args ) ) {
101            return $args;
102        }
103
104        if ( preg_match( '#/tags#i', $path ) ) {
105            return $this->tags( $args );
106        } else {
107            return $this->categories( $args );
108        }
109    }
110
111    /**
112     * Process args.
113     *
114     * @param array $args - the arguments.
115     */
116    public function process_args( $args ) {
117        if ( $args['number'] < 1 ) {
118            $args['number'] = 100;
119        } elseif ( 1000 < $args['number'] ) {
120            return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 );
121        }
122
123        if ( isset( $args['page'] ) ) {
124            if ( $args['page'] < 1 ) {
125                $args['page'] = 1;
126            }
127
128            $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
129            unset( $args['page'] );
130        }
131
132        if ( $args['offset'] < 0 ) {
133            $args['offset'] = 0;
134        }
135
136        $args['orderby'] = $args['order_by'];
137        unset( $args['order_by'] );
138
139        unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
140        return $args;
141    }
142
143    /**
144     * Get categories.
145     *
146     * @param array $args - the arguments.
147     */
148    public function categories( $args ) {
149        $args['get'] = 'all';
150
151        $cats = get_categories( $args );
152        unset( $args['offset'] );
153        $args['taxonomy'] = 'category';
154        $found            = wp_count_terms( $args );
155
156        $cats_obj = array();
157        foreach ( $cats as $cat ) {
158            $cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' );
159        }
160
161        return array(
162            'found'      => (int) $found,
163            'categories' => $cats_obj,
164        );
165    }
166
167    /**
168     * Get tags.
169     *
170     * @param array $args - the arguments.
171     */
172    public function tags( $args ) {
173        $args['get'] = 'all';
174
175        $tags = (array) get_tags( $args );
176        unset( $args['offset'] );
177        $args['taxonomy'] = 'post_tag';
178        $found            = wp_count_terms( $args );
179
180        $tags_obj = array();
181        foreach ( $tags as $tag ) {
182            $tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' );
183        }
184
185        return array(
186            'found' => (int) $found,
187            'tags'  => $tags_obj,
188        );
189    }
190}