Completed
Push — update/dialogue-focus-on-conte... ( 9f1745...fa862f )
by
unknown
80:03 queued 71:18
created

class.wpcom-json-api-list-terms-endpoint.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_List_Terms_Endpoint( array(
4
	'description' => 'Get a list of a site\'s terms by taxonomy.',
5
	'group'       => 'taxonomy',
6
	'stat'        => 'terms',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/taxonomies/%s/terms',
9
	'path_labels' => array(
10
		'$site'     => '(int|string) Site ID or domain',
11
		'$taxonomy' => '(string) Taxonomy',
12
	),
13
	'query_parameters' => array(
14
		'number'   => '(int=100) The number of terms to return. Limit: 1000.',
15
		'offset'   => '(int=0) 0-indexed offset.',
16
		'page'     => '(int) Return the Nth 1-indexed page of terms. Takes precedence over the <code>offset</code> parameter.',
17
		'search'   => '(string) Limit response to include only terms whose names or slugs match the provided search query.',
18
		'order'    => array(
19
			'ASC'  => 'Return terms in ascending order.',
20
			'DESC' => 'Return terms in descending order.',
21
		),
22
		'order_by' => array(
23
			'name'  => 'Order by the name of each tag.',
24
			'count' => 'Order by the number of posts in each tag.',
25
		),
26
	),
27
28
	'allow_fallback_to_jetpack_blog_token' => true,
29
30
	'response_format' => array(
31
		'found' => '(int) The number of terms returned.',
32
		'terms' => '(array) Array of tag objects.',
33
	),
34
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tags/terms?number=5'
35
) );
36
37
class WPCOM_JSON_API_List_Terms_Endpoint extends WPCOM_JSON_API_Endpoint {
38
	// /sites/%s/taxonomies/%s/terms -> $blog_id, $taxonomy
39
	function callback( $path = '', $blog_id = 0, $taxonomy = 'category' ) {
40
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
41
		if ( is_wp_error( $blog_id ) ) {
42
			return $blog_id;
43
		}
44
45
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
46
			$this->load_theme_functions();
47
		}
48
49
		$taxonomy_meta = get_taxonomy( $taxonomy );
50 View Code Duplication
		if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&
51
				! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
52
			return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_taxonomy'.

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...
53
		}
54
55
		$args = $this->query_args();
56
		$args = $this->process_args( $args );
57
58
		$formatted_terms = $this->get_formatted_terms( $taxonomy, $args );
59
60
		if ( ! empty( $formatted_terms ) ) {
61
			/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
62
			do_action( 'wpcom_json_api_objects', 'terms', count( $formatted_terms ) );
63
		}
64
65
		return array(
66
			'found' => (int) $this->get_found( $taxonomy, $args ),
67
			'terms' => (array) $formatted_terms
68
		);
69
	}
70
71 View Code Duplication
	function process_args( $args ) {
72
		$args['get'] = 'all';
73
74
		if ( $args['number'] < 1 ) {
75
			$args['number'] = 100;
76
		} elseif ( 1000 < $args['number'] ) {
77
			return new WP_Error( 'invalid_number', 'The number parameter must be less than or equal to 1000.', 400 );
0 ignored issues
show
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...
78
		}
79
80
		if ( isset( $args['page'] ) ) {
81
			if ( $args['page'] < 1 ) {
82
				$args['page'] = 1;
83
			}
84
85
			$args['offset'] = ( $args['page'] - 1 ) * $args['number'];
86
			unset( $args['page'] );
87
		}
88
89
		if ( $args['offset'] < 0 ) {
90
			$args['offset'] = 0;
91
		}
92
93
		$args['orderby'] = $args['order_by'];
94
		unset( $args['order_by'] );
95
96
		unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
97
		return $args;
98
	}
99
100
	function get_found( $taxonomy, $args ) {
101
		unset( $args['offset'] );
102
		return wp_count_terms( $taxonomy, $args );
103
	}
104
105
	function get_formatted_terms( $taxonomy, $args ) {
106
		$terms = get_terms( $taxonomy, $args );
107
108
		$formatted_terms = array();
109
		foreach ( $terms as $term ) {
110
			$formatted_terms[] = $this->format_taxonomy( $term, $taxonomy, 'display' );
111
		}
112
113
		return $formatted_terms;
114
	}
115
}
116