|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint { |
|
4
|
|
|
// /sites/%s/tags -> $blog_id |
|
5
|
|
|
// /sites/%s/categories -> $blog_id |
|
6
|
|
|
function callback( $path = '', $blog_id = 0 ) { |
|
7
|
|
|
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
|
|
|
|
|
|
8
|
|
|
if ( is_wp_error( $blog_id ) ) { |
|
9
|
|
|
return $blog_id; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
$args = $this->query_args(); |
|
13
|
|
|
$args = $this->process_args( $args ); |
|
14
|
|
|
|
|
15
|
|
|
if ( preg_match( '#/tags#i', $path ) ) { |
|
16
|
|
|
return $this->tags( $args ); |
|
17
|
|
|
} else { |
|
18
|
|
|
return $this->categories( $args ); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
function process_args( $args ) { |
|
23
|
|
|
if ( $args['number'] < 1 ) { |
|
24
|
|
|
$args['number'] = 100; |
|
25
|
|
|
} elseif ( 1000 < $args['number'] ) { |
|
26
|
|
|
return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ( isset( $args['page'] ) ) { |
|
30
|
|
|
if ( $args['page'] < 1 ) { |
|
31
|
|
|
$args['page'] = 1; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
|
35
|
|
|
unset( $args['page'] ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ( $args['offset'] < 0 ) { |
|
39
|
|
|
$args['offset'] = 0; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$args['orderby'] = $args['order_by']; |
|
43
|
|
|
unset( $args['order_by'] ); |
|
44
|
|
|
|
|
45
|
|
|
unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] ); |
|
46
|
|
|
return $args; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
function categories( $args ) { |
|
50
|
|
|
$args['get'] = 'all'; |
|
51
|
|
|
|
|
52
|
|
|
$cats = get_categories( $args ); |
|
53
|
|
|
unset( $args['offset'] ); |
|
54
|
|
|
$found = wp_count_terms( 'category', $args ); |
|
55
|
|
|
|
|
56
|
|
|
$cats_obj = array(); |
|
57
|
|
|
foreach ( $cats as $cat ) { |
|
58
|
|
|
$cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return array( |
|
62
|
|
|
'found' => (int) $found, |
|
63
|
|
|
'categories' => $cats_obj |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
function tags( $args ) { |
|
68
|
|
|
$args['get'] = 'all'; |
|
69
|
|
|
|
|
70
|
|
|
$tags = (array) get_tags( $args ); |
|
71
|
|
|
unset( $args['offset'] ); |
|
72
|
|
|
$found = wp_count_terms( 'post_tag', $args ); |
|
73
|
|
|
|
|
74
|
|
|
$tags_obj = array(); |
|
75
|
|
|
foreach ( $tags as $tag ) { |
|
76
|
|
|
$tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return array( |
|
80
|
|
|
'found' => (int) $found, |
|
81
|
|
|
'tags' => $tags_obj |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|