Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

WPCOM_JSON_API_Get_Taxonomies_Endpoint::callback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
new WPCOM_JSON_API_Get_Taxonomies_Endpoint( array(
4
	'description' => "Get a list of a site's categories.",
5
	'group'       => 'taxonomy',
6
	'stat'        => 'categories',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/categories',
9
	'path_labels' => array(
10
		'$site'     => '(int|string) Site ID or domain'
11
	),
12
	'query_parameters' => array(
13
		'number'   => '(int=100) The number of categories to return. Limit: 1000.',
14
		'offset'   => '(int=0) 0-indexed offset.',
15
		'page'     => '(int) Return the Nth 1-indexed page of categories. Takes precedence over the <code>offset</code> parameter.',
16
		'search'   => '(string) Limit response to include only categories whose names or slugs match the provided search query.',
17
		'order'    => array(
18
			'ASC'  => 'Return categories in ascending order.',
19
			'DESC' => 'Return categories in descending order.',
20
		),
21
		'order_by' => array(
22
			'name'  => 'Order by the name of each category.',
23
			'count' => 'Order by the number of posts in each category.',
24
		),
25
	),
26
	'response_format' => array(
27
		'found'      => '(int) The number of categories returned.',
28
		'categories' => '(array) Array of category objects.',
29
	),
30
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/categories/?number=5'
31
) );
32
33
new WPCOM_JSON_API_Get_Taxonomies_Endpoint( array(
34
	'description' => "Get a list of a site's tags.",
35
	'group'       => 'taxonomy',
36
	'stat'        => 'tags',
37
	'method'      => 'GET',
38
	'path'        => '/sites/%s/tags',
39
	'path_labels' => array(
40
		'$site'     => '(int|string) Site ID or domain'
41
	),
42
	'query_parameters' => array(
43
		'number'   => '(int=100) The number of tags to return. Limit: 1000.',
44
		'offset'   => '(int=0) 0-indexed offset.',
45
		'page'     => '(int) Return the Nth 1-indexed page of tags. Takes precedence over the <code>offset</code> parameter.',
46
		'search'   => '(string) Limit response to include only tags whose names or slugs match the provided search query.',
47
		'order'    => array(
48
			'ASC'  => 'Return tags in ascending order.',
49
			'DESC' => 'Return tags in descending order.',
50
		),
51
		'order_by' => array(
52
			'name'  => 'Order by the name of each tag.',
53
			'count' => 'Order by the number of posts in each tag.',
54
		),
55
	),
56
	'response_format' => array(
57
		'found'    => '(int) The number of tags returned.',
58
		'tags'     => '(array) Array of tag objects.',
59
	),
60
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/tags/?number=5'
61
) );
62
63
class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint {
64
	// /sites/%s/tags       -> $blog_id
65
	// /sites/%s/categories -> $blog_id
66
	function callback( $path = '', $blog_id = 0 ) {
67
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
68
		if ( is_wp_error( $blog_id ) ) {
69
			return $blog_id;
70
		}
71
72
		$args = $this->query_args();
73
		$args = $this->process_args( $args );
74
75
		if ( preg_match( '#/tags#i', $path ) ) {
76
			return $this->tags( $args );
77
		} else {
78
			return $this->categories( $args );
79
		}
80
	}
81
82 View Code Duplication
	function process_args( $args ) {
83
		if ( $args['number'] < 1 ) {
84
			$args['number'] = 100;
85
		} elseif ( 1000 < $args['number'] ) {
86
			return new WP_Error( 'invalid_number',  'The NUMBER parameter must be less than or equal to 1000.', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_number'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
87
		}
88
89
		if ( isset( $args['page'] ) ) {
90
			if ( $args['page'] < 1 ) {
91
				$args['page'] = 1;
92
			}
93
94
			$args['offset'] = ( $args['page'] - 1 ) * $args['number'];
95
			unset( $args['page'] );
96
		}
97
98
		if ( $args['offset'] < 0 ) {
99
			$args['offset'] = 0;
100
		}
101
102
		$args['orderby'] = $args['order_by'];
103
		unset( $args['order_by'] );
104
105
		unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
106
		return $args;
107
	}
108
109 View Code Duplication
	function categories( $args ) {
110
		$args['get'] = 'all';
111
112
		$cats = get_categories( $args );
113
		unset( $args['offset'] );
114
		$found = wp_count_terms( 'category', $args );
115
116
		$cats_obj = array();
117
		foreach ( $cats as $cat ) {
118
			$cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' );
119
		}
120
121
		return array(
122
			'found'       => (int) $found,
123
			'categories'  => $cats_obj
124
		);
125
	}
126
127 View Code Duplication
	function tags( $args ) {
128
		$args['get'] = 'all';
129
130
		$tags = (array) get_tags( $args );
131
		unset( $args['offset'] );
132
		$found = wp_count_terms( 'post_tag', $args );
133
134
		$tags_obj = array();
135
		foreach ( $tags as $tag ) {
136
			$tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' );
137
		}
138
139
		return array(
140
			'found' => (int) $found,
141
			'tags'  => $tags_obj
142
		);
143
	}
144
}
145