Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 113
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 / 47
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 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
101        if ( preg_match( '#/tags#i', $path ) ) {
102            return $this->tags( $args );
103        } else {
104            return $this->categories( $args );
105        }
106    }
107
108    /**
109     * Process args.
110     *
111     * @param array $args - the arguments.
112     */
113    public function process_args( $args ) {
114        if ( $args['number'] < 1 ) {
115            $args['number'] = 100;
116        } elseif ( 1000 < $args['number'] ) {
117            return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 );
118        }
119
120        if ( isset( $args['page'] ) ) {
121            if ( $args['page'] < 1 ) {
122                $args['page'] = 1;
123            }
124
125            $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
126            unset( $args['page'] );
127        }
128
129        if ( $args['offset'] < 0 ) {
130            $args['offset'] = 0;
131        }
132
133        $args['orderby'] = $args['order_by'];
134        unset( $args['order_by'] );
135
136        unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
137        return $args;
138    }
139
140    /**
141     * Get categories.
142     *
143     * @param array $args - the arguments.
144     */
145    public function categories( $args ) {
146        $args['get'] = 'all';
147
148        $cats = get_categories( $args );
149        unset( $args['offset'] );
150        $args['taxonomy'] = 'category';
151        $found            = wp_count_terms( $args );
152
153        $cats_obj = array();
154        foreach ( $cats as $cat ) {
155            $cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' );
156        }
157
158        return array(
159            'found'      => (int) $found,
160            'categories' => $cats_obj,
161        );
162    }
163
164    /**
165     * Get tags.
166     *
167     * @param array $args - the arguments.
168     */
169    public function tags( $args ) {
170        $args['get'] = 'all';
171
172        $tags = (array) get_tags( $args );
173        unset( $args['offset'] );
174        $args['taxonomy'] = 'post_tag';
175        $found            = wp_count_terms( $args );
176
177        $tags_obj = array();
178        foreach ( $tags as $tag ) {
179            $tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' );
180        }
181
182        return array(
183            'found' => (int) $found,
184            'tags'  => $tags_obj,
185        );
186    }
187}