This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terms.php
176 lines (145 loc) · 6.44 KB
/
terms.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
<?php
/*
Controller name: Terms
Controller description: Interface to get_terms (with optional taxonomy images)
*/
class JSON_API_Terms_Controller {
public function get_terms() {
global $json_api;
// See:
// http://codex.wordpress.org/Function_Reference/get_terms
// The only real difference here is we add rand as an option for orderby... not entirely random though.
// We also check to make sure offset doesn't exceed total terms for the taxonomy.
//
// Asking for images to be returned requires the taxonomy images plugin:
// http://wordpress.org/plugins/taxonomy-images/
$taxonomy = $json_api->query->taxonomy; // The taxonomy to request
$term_args = $json_api->query->term_args; // The args for get_terms()
// Taxonomy images
$return_images = $json_api->query->return_images == 'true' ? true : false;
$having_images = $json_api->query->having_images == 'true' ? true : false;
$cache_images = $json_api->query->cache_images == 'true' ? true : false;
$image_size = $json_api->query->image_size;
// Make sure a taxonomy is supplied otherwise we can't fetch any terms
if ( !$taxonomy ) {
$json_api->error("taxonomy is required to retrieve the terms from.");
return null;
}
// Do we need to return images? This will require the taxonomy images plugin!
if ( $return_images == true ) {
// Pull in wp-admin's plugin.php include to use is_plugin_active()
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Check if the taxonomy-images plugin is installed and active
if ( !is_plugin_active('taxonomy-images/taxonomy-images.php') ) {
$json_api->error("return_images requires the taxonomy images plugin available from: http://wordpress.org/plugins/taxonomy-images/");
return null;
}
// Image size is required if we are to return image urls instead of ids
if ( !$image_size ) {
$json_api->error("return_images requires image_size to define a thumbnail size.");
return null;
}
}
// We shouldn't let offset exceed the total terms
$term_count = wp_count_terms($taxonomy);
$term_max_offset;
if ( array_key_exists('number', $term_args) ) {
$term_max_offset = $term_count - $term_args['number'];
}
else {
$term_max_offset = $term_count - 1;
}
if ( array_key_exists('offset', $term_args) && $term_args['offset'] > $term_max_offset ) {
$json_api->error("term_args[offset] cannot exceed the total number of terms for the taxonomy.");
return null;
}
// If we want random terms we need to pick a random offset... order by id seems as good as anything for our needs.
if ( array_key_exists('orderby', $term_args) && $term_args['orderby'] == 'rand' ) {
$term_args['orderby'] = 'id';
$term_args['offset'] = rand(0, $term_max_offset);
}
// Fetch the terms, either with get_terms, or with the filter from the taxonomy images plugin.
$terms;
if ( $return_images == true ) {
$terms = apply_filters(
'taxonomy-images-get-terms',
'',
array(
'taxonomy' => $taxonomy,
'having_images' => $having_images,
'cache_images' => $cache_images,
'term_args' => $term_args
)
);
// Fetch the src and other details for images
$index = 0;
foreach($terms as $term) {
$image = wp_get_attachment_image_src( $term->image_id, $image_size );
$terms[ $index ]->image_url = $image[0];
$terms[ $index ]->image_width = $image[1];
$terms[ $index ]->image_height = $image[2];
$terms[ $index ]->image_resized = $image[3];
$index++;
}
}
else {
$terms = get_terms($taxonomy, $term_args);
}
if ($terms) {
return array(
'count' => count($terms),
'terms' => $terms
);
}
else {
$json_api->error("No terms matching your request were found.");
return null;
}
}
public function get_term() {
global $json_api;
// See:
// http://codex.wordpress.org/Function_Reference/get_term
// TODO: Should probably add support for taxonomy images
$term_id = $json_api->query->term_id; // The term's id
$taxonomy = $json_api->query->taxonomy; // The taxonomy
// term_id and taxonomy are required
if ( !$term_id || !$taxonomy ) {
$json_api->error("The term_id and taxonomy are required to retrieve the term details.");
return null;
}
$term = get_term($term_id, $taxonomy);
if ($term) {
return array('term' => $term);
}
else {
$json_api->error("No term matching your request was found.");
return null;
}
}
public function get_the_terms() {
global $json_api;
// See:
// http://codex.wordpress.org/Function_Reference/get_the_terms
// TODO: Should probably add support for taxonomy images
$post_id = $json_api->query->post_id; // The post's id to retrieve the attached terms from
$taxonomy = $json_api->query->taxonomy; // The taxonomy
// term_id and taxonomy are required
if ( !$post_id || !$taxonomy ) {
$json_api->error("The post_id and taxonomy are required to retrieve the term details.");
return null;
}
$terms = get_the_terms($post_id, $taxonomy);
if ($terms) {
return array(
'count' => count($terms),
'terms' => $terms
);
}
else {
$json_api->error("No terms matching the post was found.");
return null;
}
}
}
?>