This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
/
class-wp-json-taxonomies.php
executable file
·212 lines (187 loc) · 6.08 KB
/
class-wp-json-taxonomies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
class WP_JSON_Taxonomies {
/**
* Register the taxonomy-related routes
*
* @param array $routes Existing routes
* @return array Modified routes
*/
public function registerRoutes( $routes ) {
$tax_routes = array(
'/posts/types/(?P<type>\w+)/taxonomies' => array(
array( array( $this, 'getTaxonomies' ), WP_JSON_Server::READABLE ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)' => array(
array( array( $this, 'getTaxonomy' ), WP_JSON_Server::READABLE ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms' => array(
array( array( $this, 'getTerms' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
),
'/posts/types/(?P<type>\w+)/taxonomies/(?P<taxonomy>\w+)/terms/(?P<term>\w+)' => array(
array( array( $this, 'getTerm' ), WP_JSON_Server::READABLE ),
array( '__return_null', WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( '__return_null', WP_JSON_Server::DELETABLE ),
),
);
return array_merge( $routes, $tax_routes );
}
/**
* Get taxonomies
*
* @param string $type Post type to get taxonomies for
* @return array Taxonomy data
*/
public function getTaxonomies( $type ) {
$taxonomies = get_object_taxonomies( $type, 'objects' );
$data = array();
foreach ($taxonomies as $tax_type => $value) {
$tax = $this->prepare_taxonomy( $value, $type, true );
if ( is_wp_error( $tax ) )
continue;
$data[ $tax_type ] = $tax;
}
return $data;
}
/**
* Get taxonomies
*
* @param string $type Post type to get taxonomies for
* @return array Taxonomy data
*/
public function getTaxonomy( $type, $taxonomy ) {
$tax = get_taxonomy( $taxonomy );
if ( empty( $tax ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
return $this->prepare_taxonomy( $tax, $type );
}
/**
* Prepare a taxonomy for serialization
*
* @param stdClass $taxonomy Taxonomy data
* @param boolean $_in_collection Are we in a collection?
* @return array Taxonomy data
*/
protected function prepare_taxonomy( $taxonomy, $type, $_in_collection = false ) {
if ( $taxonomy->public === false )
return new WP_Error( 'json_cannot_read_taxonomy', __( 'Cannot view taxonomy' ), array( 'status' => 403 ) );
$base_url = '/posts/types/' . $type . '/taxonomies/' . $taxonomy->name;
$data = array(
'name' => $taxonomy->label,
'slug' => $taxonomy->name,
'labels' => $taxonomy->labels,
'types' => array(),
'show_cloud' => $taxonomy->show_tagcloud,
'hierarchical' => $taxonomy->hierarchical,
'meta' => array(
'links' => array(
'archives' => json_url( $base_url . '/terms' )
)
),
);
if ( $_in_collection ) {
$data['meta']['links']['self'] = json_url( $base_url );
}
else {
$data['meta']['links']['collection'] = json_url( $base_url );
}
return apply_filters( 'json_prepare_taxonomy', $data );
}
/**
* Add taxonomy data to post type data
*
* @param array $data Type data
* @param array $post Internal type data
* @return array Filtered data
*/
public function add_taxonomy_data( $data, $type ) {
$data['taxonomies'] = $this->getTaxonomies( $type->name );
return $data;
}
/**
* Get terms for a post type
*
* @param string $type Post type
* @param string $taxonomy Taxonomy slug
* @return array Term collection
*/
public function getTerms( $type, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
$args = array(
'hide_empty' => false,
);
$terms = get_terms( $taxonomy, $args );
if ( is_wp_error( $terms ) )
return $terms;
$data = array();
foreach ($terms as $term) {
$data[ $term->slug ] = $this->prepare_term( $term, $type );
}
return $data;
}
/**
* Get term for a post type
*
* @param string $type Post type
* @param string $taxonomy Taxonomy slug
* @param string $term Term slug
* @param string $context Context (view/view-parent)
* @return array Term entity
*/
public function getTerm( $type, $taxonomy, $term, $context = 'view' ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'json_taxonomy_invalid_id', __( 'Invalid taxonomy ID.' ), array( 'status' => 404 ) );
$data = get_term( $term, $taxonomy );
if ( empty( $data ) )
return new WP_Error( 'json_taxonomy_invalid_term', __( 'Invalid term ID.' ), array( 'status' => 404 ) );
return $this->prepare_term( $data, $type, $context );
}
/**
* Add term data to post data
*
* @param array $data Post data
* @param array $post Internal post data
* @param string $context Post context
* @return array Filtered data
*/
public function add_term_data( $data, $post, $context ) {
$post_type_taxonomies = get_object_taxonomies( $post['post_type'] );
$terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
$data['terms'] = array();
foreach ( $terms as $term ) {
$data['terms'][ $term->taxonomy ][] = $this->prepare_term( $term, $post['post_type'] );
}
return $data;
}
/**
* Prepare term data for serialization
*
* @param array|object $term The unprepared term data
* @return array The prepared term data
*/
protected function prepare_term( $term, $type, $context = 'view' ) {
$base_url = '/posts/types/' . $type . '/taxonomies/' . $term->taxonomy . '/terms';
$data = array(
'ID' => (int) $term->term_taxonomy_id,
'name' => $term->name,
'slug' => $term->slug,
'parent' => (int) $term->parent,
'count' => (int) $term->count,
'link' => get_term_link( $term, $term->taxonomy ),
'meta' => array(
'links' => array(
'collection' => json_url( $base_url ),
'self' => json_url( $base_url . '/' . $term->term_id ),
),
),
);
if ( ! empty( $data['parent'] ) && $context === 'view' ) {
$data['parent'] = $this->getTerm( $type, $term->taxonomy, $data['parent'], 'view-parent' );
}
elseif ( empty( $data['parent'] ) ) {
$data['parent'] = null;
}
return apply_filters( 'json_prepare_term', $data, $term );
}
}