136
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Taxonomy API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Taxonomy |
|
7 |
* @since 2.3.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
// |
|
11 |
// Taxonomy Registration |
|
12 |
// |
|
13 |
|
|
14 |
/** |
|
15 |
* Creates the initial taxonomies when 'init' action is fired. |
|
16 |
*/ |
|
17 |
function create_initial_taxonomies() { |
|
18 |
register_taxonomy( 'category', 'post', array('hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'label' => __('Categories'), 'query_var' => false, 'rewrite' => false) ) ; |
|
19 |
register_taxonomy( 'post_tag', 'post', array('hierarchical' => false, 'update_count_callback' => '_update_post_term_count', 'label' => __('Post Tags'), 'query_var' => false, 'rewrite' => false) ) ; |
|
20 |
register_taxonomy( 'link_category', 'link', array('hierarchical' => false, 'label' => __('Categories'), 'query_var' => false, 'rewrite' => false) ) ; |
|
21 |
} |
|
22 |
add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority |
|
23 |
|
|
24 |
/** |
|
25 |
* Return all of the taxonomy names that are of $object_type. |
|
26 |
* |
|
27 |
* It appears that this function can be used to find all of the names inside of |
|
28 |
* $wp_taxonomies global variable. |
|
29 |
* |
|
30 |
* <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should |
|
31 |
* result in <code>Array('category', 'post_tag')</code> |
|
32 |
* |
|
33 |
* @package WordPress |
|
34 |
* @subpackage Taxonomy |
|
35 |
* @since 2.3.0 |
|
36 |
* |
|
37 |
* @uses $wp_taxonomies |
|
38 |
* |
|
39 |
* @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts) |
|
40 |
* @return array The names of all taxonomy of $object_type. |
|
41 |
*/ |
|
42 |
function get_object_taxonomies($object) { |
|
43 |
global $wp_taxonomies; |
|
44 |
|
|
45 |
if ( is_object($object) ) { |
|
46 |
if ( $object->post_type == 'attachment' ) |
|
47 |
return get_attachment_taxonomies($object); |
|
48 |
$object = $object->post_type; |
|
49 |
} |
|
50 |
|
|
51 |
$object = (array) $object; |
|
52 |
|
|
53 |
$taxonomies = array(); |
|
54 |
foreach ( (array) $wp_taxonomies as $taxonomy ) { |
|
55 |
if ( array_intersect($object, (array) $taxonomy->object_type) ) |
|
56 |
$taxonomies[] = $taxonomy->name; |
|
57 |
} |
|
58 |
|
|
59 |
return $taxonomies; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* Retrieves the taxonomy object of $taxonomy. |
|
64 |
* |
|
65 |
* The get_taxonomy function will first check that the parameter string given |
|
66 |
* is a taxonomy object and if it is, it will return it. |
|
67 |
* |
|
68 |
* @package WordPress |
|
69 |
* @subpackage Taxonomy |
|
70 |
* @since 2.3.0 |
|
71 |
* |
|
72 |
* @uses $wp_taxonomies |
|
73 |
* @uses is_taxonomy() Checks whether taxonomy exists |
|
74 |
* |
|
75 |
* @param string $taxonomy Name of taxonomy object to return |
|
76 |
* @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist |
|
77 |
*/ |
|
78 |
function get_taxonomy( $taxonomy ) { |
|
79 |
global $wp_taxonomies; |
|
80 |
|
|
81 |
if ( ! is_taxonomy($taxonomy) ) |
|
82 |
return false; |
|
83 |
|
|
84 |
return $wp_taxonomies[$taxonomy]; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Checks that the taxonomy name exists. |
|
89 |
* |
|
90 |
* @package WordPress |
|
91 |
* @subpackage Taxonomy |
|
92 |
* @since 2.3.0 |
|
93 |
* |
|
94 |
* @uses $wp_taxonomies |
|
95 |
* |
|
96 |
* @param string $taxonomy Name of taxonomy object |
|
97 |
* @return bool Whether the taxonomy exists or not. |
|
98 |
*/ |
|
99 |
function is_taxonomy( $taxonomy ) { |
|
100 |
global $wp_taxonomies; |
|
101 |
|
|
102 |
return isset($wp_taxonomies[$taxonomy]); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Whether the taxonomy object is hierarchical. |
|
107 |
* |
|
108 |
* Checks to make sure that the taxonomy is an object first. Then Gets the |
|
109 |
* object, and finally returns the hierarchical value in the object. |
|
110 |
* |
|
111 |
* A false return value might also mean that the taxonomy does not exist. |
|
112 |
* |
|
113 |
* @package WordPress |
|
114 |
* @subpackage Taxonomy |
|
115 |
* @since 2.3.0 |
|
116 |
* |
|
117 |
* @uses is_taxonomy() Checks whether taxonomy exists |
|
118 |
* @uses get_taxonomy() Used to get the taxonomy object |
|
119 |
* |
|
120 |
* @param string $taxonomy Name of taxonomy object |
|
121 |
* @return bool Whether the taxonomy is hierarchical |
|
122 |
*/ |
|
123 |
function is_taxonomy_hierarchical($taxonomy) { |
|
124 |
if ( ! is_taxonomy($taxonomy) ) |
|
125 |
return false; |
|
126 |
|
|
127 |
$taxonomy = get_taxonomy($taxonomy); |
|
128 |
return $taxonomy->hierarchical; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Create or modify a taxonomy object. Do not use before init. |
|
133 |
* |
|
134 |
* A simple function for creating or modifying a taxonomy object based on the |
|
135 |
* parameters given. The function will accept an array (third optional |
|
136 |
* parameter), along with strings for the taxonomy name and another string for |
|
137 |
* the object type. |
|
138 |
* |
|
139 |
* Nothing is returned, so expect error maybe or use is_taxonomy() to check |
|
140 |
* whether taxonomy exists. |
|
141 |
* |
|
142 |
* Optional $args contents: |
|
143 |
* |
|
144 |
* hierarachical - has some defined purpose at other parts of the API and is a |
|
145 |
* boolean value. |
|
146 |
* |
|
147 |
* update_count_callback - works much like a hook, in that it will be called |
|
148 |
* when the count is updated. |
|
149 |
* |
|
150 |
* rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize |
|
151 |
* permastruct; default will use $taxonomy as slug. |
|
152 |
* |
|
153 |
* query_var - false to prevent queries, or string to customize query var |
|
154 |
* (?$query_var=$term); default will use $taxonomy as query var. |
|
155 |
* |
|
156 |
* @package WordPress |
|
157 |
* @subpackage Taxonomy |
|
158 |
* @since 2.3.0 |
|
159 |
* @uses $wp_taxonomies Inserts new taxonomy object into the list |
|
160 |
* @uses $wp_rewrite Adds rewrite tags and permastructs |
|
161 |
* @uses $wp Adds query vars |
|
162 |
* |
|
163 |
* @param string $taxonomy Name of taxonomy object |
|
164 |
* @param array|string $object_type Name of the object type for the taxonomy object. |
|
165 |
* @param array|string $args See above description for the two keys values. |
|
166 |
*/ |
|
167 |
function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
|
168 |
global $wp_taxonomies, $wp_rewrite, $wp; |
|
169 |
|
|
170 |
if (!is_array($wp_taxonomies)) |
|
171 |
$wp_taxonomies = array(); |
|
172 |
|
|
173 |
$defaults = array('hierarchical' => false, 'update_count_callback' => '', 'rewrite' => true, 'query_var' => true); |
|
174 |
$args = wp_parse_args($args, $defaults); |
|
175 |
|
|
176 |
if ( false !== $args['query_var'] && !empty($wp) ) { |
|
177 |
if ( true === $args['query_var'] ) |
|
178 |
$args['query_var'] = $taxonomy; |
|
179 |
$args['query_var'] = sanitize_title_with_dashes($args['query_var']); |
|
180 |
$wp->add_query_var($args['query_var']); |
|
181 |
} |
|
182 |
|
|
183 |
if ( false !== $args['rewrite'] && !empty($wp_rewrite) ) { |
|
184 |
if ( !is_array($args['rewrite']) ) |
|
185 |
$args['rewrite'] = array(); |
|
186 |
if ( !isset($args['rewrite']['slug']) ) |
|
187 |
$args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy); |
|
188 |
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term"); |
|
189 |
$wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%"); |
|
190 |
} |
|
191 |
|
|
192 |
$args['name'] = $taxonomy; |
|
193 |
$args['object_type'] = $object_type; |
|
194 |
$wp_taxonomies[$taxonomy] = (object) $args; |
|
195 |
} |
|
196 |
|
|
197 |
// |
|
198 |
// Term API |
|
199 |
// |
|
200 |
|
|
201 |
/** |
|
202 |
* Retrieve object_ids of valid taxonomy and term. |
|
203 |
* |
|
204 |
* The strings of $taxonomies must exist before this function will continue. On |
|
205 |
* failure of finding a valid taxonomy, it will return an WP_Error class, kind |
|
206 |
* of like Exceptions in PHP 5, except you can't catch them. Even so, you can |
|
207 |
* still test for the WP_Error class and get the error message. |
|
208 |
* |
|
209 |
* The $terms aren't checked the same as $taxonomies, but still need to exist |
|
210 |
* for $object_ids to be returned. |
|
211 |
* |
|
212 |
* It is possible to change the order that object_ids is returned by either |
|
213 |
* using PHP sort family functions or using the database by using $args with |
|
214 |
* either ASC or DESC array. The value should be in the key named 'order'. |
|
215 |
* |
|
216 |
* @package WordPress |
|
217 |
* @subpackage Taxonomy |
|
218 |
* @since 2.3.0 |
|
219 |
* |
|
220 |
* @uses $wpdb |
|
221 |
* @uses wp_parse_args() Creates an array from string $args. |
|
222 |
* |
|
223 |
* @param string|array $terms String of term or array of string values of terms that will be used |
|
224 |
* @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names |
|
225 |
* @param array|string $args Change the order of the object_ids, either ASC or DESC |
|
226 |
* @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success |
|
227 |
* the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. |
|
228 |
*/ |
|
229 |
function get_objects_in_term( $terms, $taxonomies, $args = array() ) { |
|
230 |
global $wpdb; |
|
231 |
|
|
232 |
if ( !is_array( $terms) ) |
|
233 |
$terms = array($terms); |
|
234 |
|
|
235 |
if ( !is_array($taxonomies) ) |
|
236 |
$taxonomies = array($taxonomies); |
|
237 |
|
|
238 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
239 |
if ( ! is_taxonomy($taxonomy) ) |
|
240 |
return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
241 |
} |
|
242 |
|
|
243 |
$defaults = array('order' => 'ASC'); |
|
244 |
$args = wp_parse_args( $args, $defaults ); |
|
245 |
extract($args, EXTR_SKIP); |
|
246 |
|
|
247 |
$order = ( 'desc' == strtolower($order) ) ? 'DESC' : 'ASC'; |
|
248 |
|
|
249 |
$terms = array_map('intval', $terms); |
|
250 |
|
|
251 |
$taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|
252 |
$terms = "'" . implode("', '", $terms) . "'"; |
|
253 |
|
|
254 |
$object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($terms) ORDER BY tr.object_id $order"); |
|
255 |
|
|
256 |
if ( ! $object_ids ) |
|
257 |
return array(); |
|
258 |
|
|
259 |
return $object_ids; |
|
260 |
} |
|
261 |
|
|
262 |
/** |
|
263 |
* Get all Term data from database by Term ID. |
|
264 |
* |
|
265 |
* The usage of the get_term function is to apply filters to a term object. It |
|
266 |
* is possible to get a term object from the database before applying the |
|
267 |
* filters. |
|
268 |
* |
|
269 |
* $term ID must be part of $taxonomy, to get from the database. Failure, might |
|
270 |
* be able to be captured by the hooks. Failure would be the same value as $wpdb |
|
271 |
* returns for the get_row method. |
|
272 |
* |
|
273 |
* There are two hooks, one is specifically for each term, named 'get_term', and |
|
274 |
* the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the |
|
275 |
* term object, and the taxonomy name as parameters. Both hooks are expected to |
|
276 |
* return a Term object. |
|
277 |
* |
|
278 |
* 'get_term' hook - Takes two parameters the term Object and the taxonomy name. |
|
279 |
* Must return term object. Used in get_term() as a catch-all filter for every |
|
280 |
* $term. |
|
281 |
* |
|
282 |
* 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy |
|
283 |
* name. Must return term object. $taxonomy will be the taxonomy name, so for |
|
284 |
* example, if 'category', it would be 'get_category' as the filter name. Useful |
|
285 |
* for custom taxonomies or plugging into default taxonomies. |
|
286 |
* |
|
287 |
* @package WordPress |
|
288 |
* @subpackage Taxonomy |
|
289 |
* @since 2.3.0 |
|
290 |
* |
|
291 |
* @uses $wpdb |
|
292 |
* @uses sanitize_term() Cleanses the term based on $filter context before returning. |
|
293 |
* @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
|
294 |
* |
|
295 |
* @param int|object $term If integer, will get from database. If object will apply filters and return $term. |
|
296 |
* @param string $taxonomy Taxonomy name that $term is part of. |
|
297 |
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N |
|
298 |
* @param string $filter Optional, default is raw or no WordPress defined filter will applied. |
|
299 |
* @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not |
|
300 |
* exist then WP_Error will be returned. |
|
301 |
*/ |
|
302 |
function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { |
|
303 |
global $wpdb; |
|
304 |
$null = null; |
|
305 |
|
|
306 |
if ( empty($term) ) { |
|
307 |
$error = new WP_Error('invalid_term', __('Empty Term')); |
|
308 |
return $error; |
|
309 |
} |
|
310 |
|
|
311 |
if ( ! is_taxonomy($taxonomy) ) { |
|
312 |
$error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
313 |
return $error; |
|
314 |
} |
|
315 |
|
|
316 |
if ( is_object($term) && empty($term->filter) ) { |
|
317 |
wp_cache_add($term->term_id, $term, $taxonomy); |
|
318 |
$_term = $term; |
|
319 |
} else { |
|
320 |
if ( is_object($term) ) |
|
321 |
$term = $term->term_id; |
|
322 |
$term = (int) $term; |
|
323 |
if ( ! $_term = wp_cache_get($term, $taxonomy) ) { |
|
324 |
$_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %s LIMIT 1", $taxonomy, $term) ); |
|
325 |
if ( ! $_term ) |
|
326 |
return $null; |
|
327 |
wp_cache_add($term, $_term, $taxonomy); |
|
328 |
} |
|
329 |
} |
|
330 |
|
|
331 |
$_term = apply_filters('get_term', $_term, $taxonomy); |
|
332 |
$_term = apply_filters("get_$taxonomy", $_term, $taxonomy); |
|
333 |
$_term = sanitize_term($_term, $taxonomy, $filter); |
|
334 |
|
|
335 |
if ( $output == OBJECT ) { |
|
336 |
return $_term; |
|
337 |
} elseif ( $output == ARRAY_A ) { |
|
338 |
$__term = get_object_vars($_term); |
|
339 |
return $__term; |
|
340 |
} elseif ( $output == ARRAY_N ) { |
|
341 |
$__term = array_values(get_object_vars($_term)); |
|
342 |
return $__term; |
|
343 |
} else { |
|
344 |
return $_term; |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
/** |
|
349 |
* Get all Term data from database by Term field and data. |
|
350 |
* |
|
351 |
* Warning: $value is not escaped for 'name' $field. You must do it yourself, if |
|
352 |
* required. |
|
353 |
* |
|
354 |
* The default $field is 'id', therefore it is possible to also use null for |
|
355 |
* field, but not recommended that you do so. |
|
356 |
* |
|
357 |
* If $value does not exist, the return value will be false. If $taxonomy exists |
|
358 |
* and $field and $value combinations exist, the Term will be returned. |
|
359 |
* |
|
360 |
* @package WordPress |
|
361 |
* @subpackage Taxonomy |
|
362 |
* @since 2.3.0 |
|
363 |
* |
|
364 |
* @uses $wpdb |
|
365 |
* @uses sanitize_term() Cleanses the term based on $filter context before returning. |
|
366 |
* @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
|
367 |
* |
|
368 |
* @param string $field Either 'slug', 'name', or 'id' |
|
369 |
* @param string|int $value Search for this term value |
|
370 |
* @param string $taxonomy Taxonomy Name |
|
371 |
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N |
|
372 |
* @param string $filter Optional, default is raw or no WordPress defined filter will applied. |
|
373 |
* @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. |
|
374 |
*/ |
|
375 |
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { |
|
376 |
global $wpdb; |
|
377 |
|
|
378 |
if ( ! is_taxonomy($taxonomy) ) |
|
379 |
return false; |
|
380 |
|
|
381 |
if ( 'slug' == $field ) { |
|
382 |
$field = 't.slug'; |
|
383 |
$value = sanitize_title($value); |
|
384 |
if ( empty($value) ) |
|
385 |
return false; |
|
386 |
} else if ( 'name' == $field ) { |
|
387 |
// Assume already escaped |
|
388 |
$value = stripslashes($value); |
|
389 |
$field = 't.name'; |
|
390 |
} else { |
|
391 |
$field = 't.term_id'; |
|
392 |
$value = (int) $value; |
|
393 |
} |
|
394 |
|
|
395 |
$term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) ); |
|
396 |
if ( !$term ) |
|
397 |
return false; |
|
398 |
|
|
399 |
wp_cache_add($term->term_id, $term, $taxonomy); |
|
400 |
|
|
401 |
$term = sanitize_term($term, $taxonomy, $filter); |
|
402 |
|
|
403 |
if ( $output == OBJECT ) { |
|
404 |
return $term; |
|
405 |
} elseif ( $output == ARRAY_A ) { |
|
406 |
return get_object_vars($term); |
|
407 |
} elseif ( $output == ARRAY_N ) { |
|
408 |
return array_values(get_object_vars($term)); |
|
409 |
} else { |
|
410 |
return $term; |
|
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
/** |
|
415 |
* Merge all term children into a single array of their IDs. |
|
416 |
* |
|
417 |
* This recursive function will merge all of the children of $term into the same |
|
418 |
* array of term IDs. Only useful for taxonomies which are hierarchical. |
|
419 |
* |
|
420 |
* Will return an empty array if $term does not exist in $taxonomy. |
|
421 |
* |
|
422 |
* @package WordPress |
|
423 |
* @subpackage Taxonomy |
|
424 |
* @since 2.3.0 |
|
425 |
* |
|
426 |
* @uses $wpdb |
|
427 |
* @uses _get_term_hierarchy() |
|
428 |
* @uses get_term_children() Used to get the children of both $taxonomy and the parent $term |
|
429 |
* |
|
430 |
* @param string $term ID of Term to get children |
|
431 |
* @param string $taxonomy Taxonomy Name |
|
432 |
* @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist |
|
433 |
*/ |
|
434 |
function get_term_children( $term_id, $taxonomy ) { |
|
435 |
if ( ! is_taxonomy($taxonomy) ) |
|
436 |
return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
437 |
|
|
438 |
$term_id = intval( $term_id ); |
|
439 |
|
|
440 |
$terms = _get_term_hierarchy($taxonomy); |
|
441 |
|
|
442 |
if ( ! isset($terms[$term_id]) ) |
|
443 |
return array(); |
|
444 |
|
|
445 |
$children = $terms[$term_id]; |
|
446 |
|
|
447 |
foreach ( (array) $terms[$term_id] as $child ) { |
|
448 |
if ( isset($terms[$child]) ) |
|
449 |
$children = array_merge($children, get_term_children($child, $taxonomy)); |
|
450 |
} |
|
451 |
|
|
452 |
return $children; |
|
453 |
} |
|
454 |
|
|
455 |
/** |
|
456 |
* Get sanitized Term field. |
|
457 |
* |
|
458 |
* Does checks for $term, based on the $taxonomy. The function is for contextual |
|
459 |
* reasons and for simplicity of usage. See sanitize_term_field() for more |
|
460 |
* information. |
|
461 |
* |
|
462 |
* @package WordPress |
|
463 |
* @subpackage Taxonomy |
|
464 |
* @since 2.3.0 |
|
465 |
* |
|
466 |
* @uses sanitize_term_field() Passes the return value in sanitize_term_field on success. |
|
467 |
* |
|
468 |
* @param string $field Term field to fetch |
|
469 |
* @param int $term Term ID |
|
470 |
* @param string $taxonomy Taxonomy Name |
|
471 |
* @param string $context Optional, default is display. Look at sanitize_term_field() for available options. |
|
472 |
* @return mixed Will return an empty string if $term is not an object or if $field is not set in $term. |
|
473 |
*/ |
|
474 |
function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { |
|
475 |
$term = (int) $term; |
|
476 |
$term = get_term( $term, $taxonomy ); |
|
477 |
if ( is_wp_error($term) ) |
|
478 |
return $term; |
|
479 |
|
|
480 |
if ( !is_object($term) ) |
|
481 |
return ''; |
|
482 |
|
|
483 |
if ( !isset($term->$field) ) |
|
484 |
return ''; |
|
485 |
|
|
486 |
return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); |
|
487 |
} |
|
488 |
|
|
489 |
/** |
|
490 |
* Sanitizes Term for editing. |
|
491 |
* |
|
492 |
* Return value is sanitize_term() and usage is for sanitizing the term for |
|
493 |
* editing. Function is for contextual and simplicity. |
|
494 |
* |
|
495 |
* @package WordPress |
|
496 |
* @subpackage Taxonomy |
|
497 |
* @since 2.3.0 |
|
498 |
* |
|
499 |
* @uses sanitize_term() Passes the return value on success |
|
500 |
* |
|
501 |
* @param int|object $id Term ID or Object |
|
502 |
* @param string $taxonomy Taxonomy Name |
|
503 |
* @return mixed|null|WP_Error Will return empty string if $term is not an object. |
|
504 |
*/ |
|
505 |
function get_term_to_edit( $id, $taxonomy ) { |
|
506 |
$term = get_term( $id, $taxonomy ); |
|
507 |
|
|
508 |
if ( is_wp_error($term) ) |
|
509 |
return $term; |
|
510 |
|
|
511 |
if ( !is_object($term) ) |
|
512 |
return ''; |
|
513 |
|
|
514 |
return sanitize_term($term, $taxonomy, 'edit'); |
|
515 |
} |
|
516 |
|
|
517 |
/** |
|
518 |
* Retrieve the terms in a given taxonomy or list of taxonomies. |
|
519 |
* |
|
520 |
* You can fully inject any customizations to the query before it is sent, as |
|
521 |
* well as control the output with a filter. |
|
522 |
* |
|
523 |
* The 'get_terms' filter will be called when the cache has the term and will |
|
524 |
* pass the found term along with the array of $taxonomies and array of $args. |
|
525 |
* This filter is also called before the array of terms is passed and will pass |
|
526 |
* the array of terms, along with the $taxonomies and $args. |
|
527 |
* |
|
528 |
* The 'list_terms_exclusions' filter passes the compiled exclusions along with |
|
529 |
* the $args. |
|
530 |
* |
|
531 |
* The 'get_terms_orderby' filter passes the ORDER BY clause for the query |
|
532 |
* along with the $args array. |
|
533 |
|
|
534 |
* The 'get_terms_fields' filter passes the fields for the SELECT query |
|
535 |
* along with the $args array. |
|
536 |
* |
|
537 |
* The list of arguments that $args can contain, which will overwrite the defaults: |
|
538 |
* |
|
539 |
* orderby - Default is 'name'. Can be name, count, term_group, slug or nothing |
|
540 |
* (will use term_id), Passing a custom value other than these will cause it to |
|
541 |
* order based on the custom value. |
|
542 |
* |
|
543 |
* order - Default is ASC. Can use DESC. |
|
544 |
* |
|
545 |
* hide_empty - Default is true. Will not return empty terms, which means |
|
546 |
* terms whose count is 0 according to the given taxonomy. |
|
547 |
* |
|
548 |
* exclude - Default is an empty string. A comma- or space-delimited string |
|
549 |
* of term ids to exclude from the return array. If 'include' is non-empty, |
|
550 |
* 'exclude' is ignored. |
|
551 |
* |
|
552 |
* exclude_tree - A comma- or space-delimited string of term ids to exclude |
|
553 |
* from the return array, along with all of their descendant terms according to |
|
554 |
* the primary taxonomy. If 'include' is non-empty, 'exclude_tree' is ignored. |
|
555 |
* |
|
556 |
* include - Default is an empty string. A comma- or space-delimited string |
|
557 |
* of term ids to include in the return array. |
|
558 |
* |
|
559 |
* number - The maximum number of terms to return. Default is empty. |
|
560 |
* |
|
561 |
* offset - The number by which to offset the terms query. |
|
562 |
* |
|
563 |
* fields - Default is 'all', which returns an array of term objects. |
|
564 |
* If 'fields' is 'ids' or 'names', returns an array of |
|
565 |
* integers or strings, respectively. |
|
566 |
* |
|
567 |
* slug - Returns terms whose "slug" matches this value. Default is empty string. |
|
568 |
* |
|
569 |
* hierarchical - Whether to include terms that have non-empty descendants |
|
570 |
* (even if 'hide_empty' is set to true). |
|
571 |
* |
|
572 |
* search - Returned terms' names will contain the value of 'search', |
|
573 |
* case-insensitive. Default is an empty string. |
|
574 |
* |
|
575 |
* name__like - Returned terms' names will begin with the value of 'name__like', |
|
576 |
* case-insensitive. Default is empty string. |
|
577 |
* |
|
578 |
* The argument 'pad_counts', if set to true will include the quantity of a term's |
|
579 |
* children in the quantity of each term's "count" object variable. |
|
580 |
* |
|
581 |
* The 'get' argument, if set to 'all' instead of its default empty string, |
|
582 |
* returns terms regardless of ancestry or whether the terms are empty. |
|
583 |
* |
|
584 |
* The 'child_of' argument, when used, should be set to the integer of a term ID. Its default |
|
585 |
* is 0. If set to a non-zero value, all returned terms will be descendants |
|
586 |
* of that term according to the given taxonomy. Hence 'child_of' is set to 0 |
|
587 |
* if more than one taxonomy is passed in $taxonomies, because multiple taxonomies |
|
588 |
* make term ancestry ambiguous. |
|
589 |
* |
|
590 |
* The 'parent' argument, when used, should be set to the integer of a term ID. Its default is |
|
591 |
* the empty string '', which has a different meaning from the integer 0. |
|
592 |
* If set to an integer value, all returned terms will have as an immediate |
|
593 |
* ancestor the term whose ID is specified by that integer according to the given taxonomy. |
|
594 |
* The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent' |
|
595 |
* of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc. |
|
596 |
* |
|
597 |
* @package WordPress |
|
598 |
* @subpackage Taxonomy |
|
599 |
* @since 2.3.0 |
|
600 |
* |
|
601 |
* @uses $wpdb |
|
602 |
* @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings. |
|
603 |
* |
|
604 |
* @param string|array Taxonomy name or list of Taxonomy names |
|
605 |
* @param string|array $args The values of what to search for when returning terms |
|
606 |
* @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. |
|
607 |
*/ |
|
608 |
function &get_terms($taxonomies, $args = '') { |
|
609 |
global $wpdb; |
|
610 |
$empty_array = array(); |
|
611 |
|
|
612 |
$single_taxonomy = false; |
|
613 |
if ( !is_array($taxonomies) ) { |
|
614 |
$single_taxonomy = true; |
|
615 |
$taxonomies = array($taxonomies); |
|
616 |
} |
|
617 |
|
|
618 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
619 |
if ( ! is_taxonomy($taxonomy) ) { |
|
620 |
$error = & new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
621 |
return $error; |
|
622 |
} |
|
623 |
} |
|
624 |
|
|
625 |
$in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|
626 |
|
|
627 |
$defaults = array('orderby' => 'name', 'order' => 'ASC', |
|
628 |
'hide_empty' => true, 'exclude' => '', 'exclude_tree' => '', 'include' => '', |
|
629 |
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', |
|
630 |
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', |
|
631 |
'pad_counts' => false, 'offset' => '', 'search' => ''); |
|
632 |
$args = wp_parse_args( $args, $defaults ); |
|
633 |
$args['number'] = absint( $args['number'] ); |
|
634 |
$args['offset'] = absint( $args['offset'] ); |
|
635 |
if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) || |
|
636 |
'' !== $args['parent'] ) { |
|
637 |
$args['child_of'] = 0; |
|
638 |
$args['hierarchical'] = false; |
|
639 |
$args['pad_counts'] = false; |
|
640 |
} |
|
641 |
|
|
642 |
if ( 'all' == $args['get'] ) { |
|
643 |
$args['child_of'] = 0; |
|
644 |
$args['hide_empty'] = 0; |
|
645 |
$args['hierarchical'] = false; |
|
646 |
$args['pad_counts'] = false; |
|
647 |
} |
|
648 |
extract($args, EXTR_SKIP); |
|
649 |
|
|
650 |
if ( $child_of ) { |
|
651 |
$hierarchy = _get_term_hierarchy($taxonomies[0]); |
|
652 |
if ( !isset($hierarchy[$child_of]) ) |
|
653 |
return $empty_array; |
|
654 |
} |
|
655 |
|
|
656 |
if ( $parent ) { |
|
657 |
$hierarchy = _get_term_hierarchy($taxonomies[0]); |
|
658 |
if ( !isset($hierarchy[$parent]) ) |
|
659 |
return $empty_array; |
|
660 |
} |
|
661 |
|
|
662 |
// $args can be whatever, only use the args defined in defaults to compute the key |
|
663 |
$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; |
|
664 |
$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key ); |
|
665 |
$last_changed = wp_cache_get('last_changed', 'terms'); |
|
666 |
if ( !$last_changed ) { |
|
667 |
$last_changed = time(); |
|
668 |
wp_cache_set('last_changed', $last_changed, 'terms'); |
|
669 |
} |
|
670 |
$cache_key = "get_terms:$key:$last_changed"; |
|
671 |
$cache = wp_cache_get( $cache_key, 'terms' ); |
|
672 |
if ( false !== $cache ) { |
|
673 |
$cache = apply_filters('get_terms', $cache, $taxonomies, $args); |
|
674 |
return $cache; |
|
675 |
} |
|
676 |
|
|
677 |
$_orderby = strtolower($orderby); |
|
678 |
if ( 'count' == $_orderby ) |
|
679 |
$orderby = 'tt.count'; |
|
680 |
else if ( 'name' == $_orderby ) |
|
681 |
$orderby = 't.name'; |
|
682 |
else if ( 'slug' == $_orderby ) |
|
683 |
$orderby = 't.slug'; |
|
684 |
else if ( 'term_group' == $_orderby ) |
|
685 |
$orderby = 't.term_group'; |
|
686 |
elseif ( empty($_orderby) || 'id' == $_orderby ) |
|
687 |
$orderby = 't.term_id'; |
|
688 |
|
|
689 |
$orderby = apply_filters( 'get_terms_orderby', $orderby, $args ); |
|
690 |
|
|
691 |
$where = ''; |
|
692 |
$inclusions = ''; |
|
693 |
if ( !empty($include) ) { |
|
694 |
$exclude = ''; |
|
695 |
$exclude_tree = ''; |
|
696 |
$interms = preg_split('/[\s,]+/',$include); |
|
697 |
if ( count($interms) ) { |
|
698 |
foreach ( (array) $interms as $interm ) { |
|
699 |
if (empty($inclusions)) |
|
700 |
$inclusions = ' AND ( t.term_id = ' . intval($interm) . ' '; |
|
701 |
else |
|
702 |
$inclusions .= ' OR t.term_id = ' . intval($interm) . ' '; |
|
703 |
} |
|
704 |
} |
|
705 |
} |
|
706 |
|
|
707 |
if ( !empty($inclusions) ) |
|
708 |
$inclusions .= ')'; |
|
709 |
$where .= $inclusions; |
|
710 |
|
|
711 |
$exclusions = ''; |
|
712 |
if ( ! empty( $exclude_tree ) ) { |
|
713 |
$excluded_trunks = preg_split('/[\s,]+/',$exclude_tree); |
|
714 |
foreach( (array) $excluded_trunks as $extrunk ) { |
|
715 |
$excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids')); |
|
716 |
$excluded_children[] = $extrunk; |
|
717 |
foreach( (array) $excluded_children as $exterm ) { |
|
718 |
if ( empty($exclusions) ) |
|
719 |
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' '; |
|
720 |
else |
|
721 |
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' '; |
|
722 |
|
|
723 |
} |
|
724 |
} |
|
725 |
} |
|
726 |
if ( !empty($exclude) ) { |
|
727 |
$exterms = preg_split('/[\s,]+/',$exclude); |
|
728 |
if ( count($exterms) ) { |
|
729 |
foreach ( (array) $exterms as $exterm ) { |
|
730 |
if ( empty($exclusions) ) |
|
731 |
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' '; |
|
732 |
else |
|
733 |
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' '; |
|
734 |
} |
|
735 |
} |
|
736 |
} |
|
737 |
|
|
738 |
if ( !empty($exclusions) ) |
|
739 |
$exclusions .= ')'; |
|
740 |
$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args ); |
|
741 |
$where .= $exclusions; |
|
742 |
|
|
743 |
if ( !empty($slug) ) { |
|
744 |
$slug = sanitize_title($slug); |
|
745 |
$where .= " AND t.slug = '$slug'"; |
|
746 |
} |
|
747 |
|
|
748 |
if ( !empty($name__like) ) |
|
749 |
$where .= " AND t.name LIKE '{$name__like}%'"; |
|
750 |
|
|
751 |
if ( '' !== $parent ) { |
|
752 |
$parent = (int) $parent; |
|
753 |
$where .= " AND tt.parent = '$parent'"; |
|
754 |
} |
|
755 |
|
|
756 |
if ( $hide_empty && !$hierarchical ) |
|
757 |
$where .= ' AND tt.count > 0'; |
|
758 |
|
|
759 |
// don't limit the query results when we have to descend the family tree |
|
760 |
if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) { |
|
761 |
if( $offset ) |
|
762 |
$limit = 'LIMIT ' . $offset . ',' . $number; |
|
763 |
else |
|
764 |
$limit = 'LIMIT ' . $number; |
|
765 |
|
|
766 |
} else |
|
767 |
$limit = ''; |
|
768 |
|
|
769 |
if ( !empty($search) ) { |
|
770 |
$search = like_escape($search); |
|
771 |
$where .= " AND (t.name LIKE '%$search%')"; |
|
772 |
} |
|
773 |
|
|
774 |
$selects = array(); |
|
775 |
if ( 'all' == $fields ) |
|
776 |
$selects = array('t.*', 'tt.*'); |
|
777 |
else if ( 'ids' == $fields ) |
|
778 |
$selects = array('t.term_id', 'tt.parent', 'tt.count'); |
|
779 |
else if ( 'names' == $fields ) |
|
780 |
$selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); |
|
781 |
$select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); |
|
782 |
|
|
783 |
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $limit"; |
|
784 |
|
|
785 |
$terms = $wpdb->get_results($query); |
|
786 |
if ( 'all' == $fields ) { |
|
787 |
update_term_cache($terms); |
|
788 |
} |
|
789 |
|
|
790 |
if ( empty($terms) ) { |
|
791 |
wp_cache_add( $cache_key, array(), 'terms' ); |
|
792 |
$terms = apply_filters('get_terms', array(), $taxonomies, $args); |
|
793 |
return $terms; |
|
794 |
} |
|
795 |
|
|
796 |
if ( $child_of ) { |
|
797 |
$children = _get_term_hierarchy($taxonomies[0]); |
|
798 |
if ( ! empty($children) ) |
|
799 |
$terms = & _get_term_children($child_of, $terms, $taxonomies[0]); |
|
800 |
} |
|
801 |
|
|
802 |
// Update term counts to include children. |
|
803 |
if ( $pad_counts && 'all' == $fields ) |
|
804 |
_pad_term_counts($terms, $taxonomies[0]); |
|
805 |
|
|
806 |
// Make sure we show empty categories that have children. |
|
807 |
if ( $hierarchical && $hide_empty && is_array($terms) ) { |
|
808 |
foreach ( $terms as $k => $term ) { |
|
809 |
if ( ! $term->count ) { |
|
810 |
$children = _get_term_children($term->term_id, $terms, $taxonomies[0]); |
|
811 |
if( is_array($children) ) |
|
812 |
foreach ( $children as $child ) |
|
813 |
if ( $child->count ) |
|
814 |
continue 2; |
|
815 |
|
|
816 |
// It really is empty |
|
817 |
unset($terms[$k]); |
|
818 |
} |
|
819 |
} |
|
820 |
} |
|
821 |
reset ( $terms ); |
|
822 |
|
|
823 |
$_terms = array(); |
|
824 |
if ( 'ids' == $fields ) { |
|
825 |
while ( $term = array_shift($terms) ) |
|
826 |
$_terms[] = $term->term_id; |
|
827 |
$terms = $_terms; |
|
828 |
} elseif ( 'names' == $fields ) { |
|
829 |
while ( $term = array_shift($terms) ) |
|
830 |
$_terms[] = $term->name; |
|
831 |
$terms = $_terms; |
|
832 |
} |
|
833 |
|
|
834 |
if ( 0 < $number && intval(@count($terms)) > $number ) { |
|
835 |
$terms = array_slice($terms, $offset, $number); |
|
836 |
} |
|
837 |
|
|
838 |
wp_cache_add( $cache_key, $terms, 'terms' ); |
|
839 |
|
|
840 |
$terms = apply_filters('get_terms', $terms, $taxonomies, $args); |
|
841 |
return $terms; |
|
842 |
} |
|
843 |
|
|
844 |
/** |
|
845 |
* Check if Term exists. |
|
846 |
* |
|
847 |
* Returns the index of a defined term, or 0 (false) if the term doesn't exist. |
|
848 |
* |
|
849 |
* @package WordPress |
|
850 |
* @subpackage Taxonomy |
|
851 |
* @since 2.3.0 |
|
852 |
* |
|
853 |
* @uses $wpdb |
|
854 |
* |
|
855 |
* @param int|string $term The term to check |
|
856 |
* @param string $taxonomy The taxonomy name to use |
|
857 |
* @param int $parent ID of parent term under which to confine the exists search. |
|
858 |
* @return mixed Get the term id or Term Object, if exists. |
|
859 |
*/ |
|
860 |
function is_term($term, $taxonomy = '', $parent = 0) { |
|
861 |
global $wpdb; |
|
862 |
|
|
863 |
$select = "SELECT term_id FROM $wpdb->terms as t WHERE "; |
|
864 |
$tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE "; |
|
865 |
|
|
866 |
if ( is_int($term) ) { |
|
867 |
if ( 0 == $term ) |
|
868 |
return 0; |
|
869 |
$where = 't.term_id = %d'; |
|
870 |
if ( !empty($taxonomy) ) |
|
871 |
return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); |
|
872 |
else |
|
873 |
return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); |
|
874 |
} |
|
875 |
|
|
876 |
$term = trim( stripslashes( $term ) ); |
|
877 |
|
|
878 |
if ( '' === $slug = sanitize_title($term) ) |
|
879 |
return 0; |
|
880 |
|
|
881 |
$where = 't.slug = %s'; |
|
882 |
$else_where = 't.name = %s'; |
|
883 |
$where_fields = array($slug); |
|
884 |
$else_where_fields = array($term); |
|
885 |
if ( !empty($taxonomy) ) { |
|
886 |
$parent = (int) $parent; |
|
887 |
if ( $parent > 0 ) { |
|
888 |
$where_fields[] = $parent; |
|
889 |
$else_where_fields[] = $parent; |
|
890 |
$where .= ' AND tt.parent = %d'; |
|
891 |
$else_where .= ' AND tt.parent = %d'; |
|
892 |
} |
|
893 |
|
|
894 |
$where_fields[] = $taxonomy; |
|
895 |
$else_where_fields[] = $taxonomy; |
|
896 |
|
|
897 |
if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields), ARRAY_A) ) |
|
898 |
return $result; |
|
899 |
|
|
900 |
return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields), ARRAY_A); |
|
901 |
} |
|
902 |
|
|
903 |
if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) ) |
|
904 |
return $result; |
|
905 |
|
|
906 |
return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) ); |
|
907 |
} |
|
908 |
|
|
909 |
/** |
|
910 |
* Sanitize Term all fields. |
|
911 |
* |
|
912 |
* Relys on sanitize_term_field() to sanitize the term. The difference is that |
|
913 |
* this function will sanitize <strong>all</strong> fields. The context is based |
|
914 |
* on sanitize_term_field(). |
|
915 |
* |
|
916 |
* The $term is expected to be either an array or an object. |
|
917 |
* |
|
918 |
* @package WordPress |
|
919 |
* @subpackage Taxonomy |
|
920 |
* @since 2.3.0 |
|
921 |
* |
|
922 |
* @uses sanitize_term_field Used to sanitize all fields in a term |
|
923 |
* |
|
924 |
* @param array|object $term The term to check |
|
925 |
* @param string $taxonomy The taxonomy name to use |
|
926 |
* @param string $context Default is 'display'. |
|
927 |
* @return array|object Term with all fields sanitized |
|
928 |
*/ |
|
929 |
function sanitize_term($term, $taxonomy, $context = 'display') { |
|
930 |
|
|
931 |
if ( 'raw' == $context ) |
|
932 |
return $term; |
|
933 |
|
|
934 |
$fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group'); |
|
935 |
|
|
936 |
$do_object = false; |
|
937 |
if ( is_object($term) ) |
|
938 |
$do_object = true; |
|
939 |
|
|
940 |
$term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0); |
|
941 |
|
|
942 |
foreach ( (array) $fields as $field ) { |
|
943 |
if ( $do_object ) { |
|
944 |
if ( isset($term->$field) ) |
|
945 |
$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); |
|
946 |
} else { |
|
947 |
if ( isset($term[$field]) ) |
|
948 |
$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); |
|
949 |
} |
|
950 |
} |
|
951 |
|
|
952 |
if ( $do_object ) |
|
953 |
$term->filter = $context; |
|
954 |
else |
|
955 |
$term['filter'] = $context; |
|
956 |
|
|
957 |
return $term; |
|
958 |
} |
|
959 |
|
|
960 |
/** |
|
961 |
* Cleanse the field value in the term based on the context. |
|
962 |
* |
|
963 |
* Passing a term field value through the function should be assumed to have |
|
964 |
* cleansed the value for whatever context the term field is going to be used. |
|
965 |
* |
|
966 |
* If no context or an unsupported context is given, then default filters will |
|
967 |
* be applied. |
|
968 |
* |
|
969 |
* There are enough filters for each context to support a custom filtering |
|
970 |
* without creating your own filter function. Simply create a function that |
|
971 |
* hooks into the filter you need. |
|
972 |
* |
|
973 |
* @package WordPress |
|
974 |
* @subpackage Taxonomy |
|
975 |
* @since 2.3.0 |
|
976 |
* |
|
977 |
* @uses $wpdb |
|
978 |
* |
|
979 |
* @param string $field Term field to sanitize |
|
980 |
* @param string $value Search for this term value |
|
981 |
* @param int $term_id Term ID |
|
982 |
* @param string $taxonomy Taxonomy Name |
|
983 |
* @param string $context Either edit, db, display, attribute, or js. |
|
984 |
* @return mixed sanitized field |
|
985 |
*/ |
|
986 |
function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { |
|
987 |
if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) { |
|
988 |
$value = (int) $value; |
|
989 |
if ( $value < 0 ) |
|
990 |
$value = 0; |
|
991 |
} |
|
992 |
|
|
993 |
if ( 'raw' == $context ) |
|
994 |
return $value; |
|
995 |
|
|
996 |
if ( 'edit' == $context ) { |
|
997 |
$value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy); |
|
998 |
$value = apply_filters("edit_${taxonomy}_$field", $value, $term_id); |
|
999 |
if ( 'description' == $field ) |
|
1000 |
$value = format_to_edit($value); |
|
1001 |
else |
|
1002 |
$value = esc_attr($value); |
|
1003 |
} else if ( 'db' == $context ) { |
|
1004 |
$value = apply_filters("pre_term_$field", $value, $taxonomy); |
|
1005 |
$value = apply_filters("pre_${taxonomy}_$field", $value); |
|
1006 |
// Back compat filters |
|
1007 |
if ( 'slug' == $field ) |
|
1008 |
$value = apply_filters('pre_category_nicename', $value); |
|
1009 |
|
|
1010 |
} else if ( 'rss' == $context ) { |
|
1011 |
$value = apply_filters("term_${field}_rss", $value, $taxonomy); |
|
1012 |
$value = apply_filters("${taxonomy}_${field}_rss", $value); |
|
1013 |
} else { |
|
1014 |
// Use display filters by default. |
|
1015 |
$value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context); |
|
1016 |
$value = apply_filters("${taxonomy}_$field", $value, $term_id, $context); |
|
1017 |
} |
|
1018 |
|
|
1019 |
if ( 'attribute' == $context ) |
|
1020 |
$value = esc_attr($value); |
|
1021 |
else if ( 'js' == $context ) |
|
1022 |
$value = esc_js($value); |
|
1023 |
|
|
1024 |
return $value; |
|
1025 |
} |
|
1026 |
|
|
1027 |
/** |
|
1028 |
* Count how many terms are in Taxonomy. |
|
1029 |
* |
|
1030 |
* Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code> |
|
1031 |
* or <code>array('ignore_empty' => true);</code>. |
|
1032 |
* |
|
1033 |
* @package WordPress |
|
1034 |
* @subpackage Taxonomy |
|
1035 |
* @since 2.3.0 |
|
1036 |
* |
|
1037 |
* @uses $wpdb |
|
1038 |
* @uses wp_parse_args() Turns strings into arrays and merges defaults into an array. |
|
1039 |
* |
|
1040 |
* @param string $taxonomy Taxonomy name |
|
1041 |
* @param array|string $args Overwrite defaults |
|
1042 |
* @return int How many terms are in $taxonomy |
|
1043 |
*/ |
|
1044 |
function wp_count_terms( $taxonomy, $args = array() ) { |
|
1045 |
global $wpdb; |
|
1046 |
|
|
1047 |
$defaults = array('ignore_empty' => false); |
|
1048 |
$args = wp_parse_args($args, $defaults); |
|
1049 |
extract($args, EXTR_SKIP); |
|
1050 |
|
|
1051 |
$where = ''; |
|
1052 |
if ( $ignore_empty ) |
|
1053 |
$where = 'AND count > 0'; |
|
1054 |
|
|
1055 |
return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) ); |
|
1056 |
} |
|
1057 |
|
|
1058 |
/** |
|
1059 |
* Will unlink the term from the taxonomy. |
|
1060 |
* |
|
1061 |
* Will remove the term's relationship to the taxonomy, not the term or taxonomy |
|
1062 |
* itself. The term and taxonomy will still exist. Will require the term's |
|
1063 |
* object ID to perform the operation. |
|
1064 |
* |
|
1065 |
* @package WordPress |
|
1066 |
* @subpackage Taxonomy |
|
1067 |
* @since 2.3.0 |
|
1068 |
* @uses $wpdb |
|
1069 |
* |
|
1070 |
* @param int $object_id The term Object Id that refers to the term |
|
1071 |
* @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name. |
|
1072 |
*/ |
|
1073 |
function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|
1074 |
global $wpdb; |
|
1075 |
|
|
1076 |
$object_id = (int) $object_id; |
|
1077 |
|
|
1078 |
if ( !is_array($taxonomies) ) |
|
1079 |
$taxonomies = array($taxonomies); |
|
1080 |
|
|
1081 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
1082 |
$tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|
1083 |
$in_tt_ids = "'" . implode("', '", $tt_ids) . "'"; |
|
1084 |
do_action( 'delete_term_relationships', $object_id, $tt_ids ); |
|
1085 |
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) ); |
|
1086 |
do_action( 'deleted_term_relationships', $object_id, $tt_ids ); |
|
1087 |
wp_update_term_count($tt_ids, $taxonomy); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
|
|
1091 |
/** |
|
1092 |
* Removes a term from the database. |
|
1093 |
* |
|
1094 |
* If the term is a parent of other terms, then the children will be updated to |
|
1095 |
* that term's parent. |
|
1096 |
* |
|
1097 |
* The $args 'default' will only override the terms found, if there is only one |
|
1098 |
* term found. Any other and the found terms are used. |
|
1099 |
* |
|
1100 |
* The $args 'force_default' will force the term supplied as default to be |
|
1101 |
* assigned even if the object was not going to be termless |
|
1102 |
* @package WordPress |
|
1103 |
* @subpackage Taxonomy |
|
1104 |
* @since 2.3.0 |
|
1105 |
* |
|
1106 |
* @uses $wpdb |
|
1107 |
* @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action |
|
1108 |
* hooks, passing term object, term id. 'delete_term' gets an additional |
|
1109 |
* parameter with the $taxonomy parameter. |
|
1110 |
* |
|
1111 |
* @param int $term Term ID |
|
1112 |
* @param string $taxonomy Taxonomy Name |
|
1113 |
* @param array|string $args Optional. Change 'default' term id and override found term ids. |
|
1114 |
* @return bool|WP_Error Returns false if not term; true if completes delete action. |
|
1115 |
*/ |
|
1116 |
function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|
1117 |
global $wpdb; |
|
1118 |
|
|
1119 |
$term = (int) $term; |
|
1120 |
|
|
1121 |
if ( ! $ids = is_term($term, $taxonomy) ) |
|
1122 |
return false; |
|
1123 |
if ( is_wp_error( $ids ) ) |
|
1124 |
return $ids; |
|
1125 |
|
|
1126 |
$tt_id = $ids['term_taxonomy_id']; |
|
1127 |
|
|
1128 |
$defaults = array(); |
|
1129 |
$args = wp_parse_args($args, $defaults); |
|
1130 |
extract($args, EXTR_SKIP); |
|
1131 |
|
|
1132 |
if ( isset($default) ) { |
|
1133 |
$default = (int) $default; |
|
1134 |
if ( ! is_term($default, $taxonomy) ) |
|
1135 |
unset($default); |
|
1136 |
} |
|
1137 |
|
|
1138 |
// Update children to point to new parent |
|
1139 |
if ( is_taxonomy_hierarchical($taxonomy) ) { |
|
1140 |
$term_obj = get_term($term, $taxonomy); |
|
1141 |
if ( is_wp_error( $term_obj ) ) |
|
1142 |
return $term_obj; |
|
1143 |
$parent = $term_obj->parent; |
|
1144 |
|
|
1145 |
$edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); |
|
1146 |
do_action( 'edit_term_taxonomies', $edit_tt_ids ); |
|
1147 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); |
|
1148 |
do_action( 'edited_term_taxonomies', $edit_tt_ids ); |
|
1149 |
} |
|
1150 |
|
|
1151 |
$objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); |
|
1152 |
|
|
1153 |
foreach ( (array) $objects as $object ) { |
|
1154 |
$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none')); |
|
1155 |
if ( 1 == count($terms) && isset($default) ) { |
|
1156 |
$terms = array($default); |
|
1157 |
} else { |
|
1158 |
$terms = array_diff($terms, array($term)); |
|
1159 |
if (isset($default) && isset($force_default) && $force_default) |
|
1160 |
$terms = array_merge($terms, array($default)); |
|
1161 |
} |
|
1162 |
$terms = array_map('intval', $terms); |
|
1163 |
wp_set_object_terms($object, $terms, $taxonomy); |
|
1164 |
} |
|
1165 |
|
|
1166 |
do_action( 'delete_term_taxonomy', $tt_id ); |
|
1167 |
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) ); |
|
1168 |
do_action( 'deleted_term_taxonomy', $tt_id ); |
|
1169 |
|
|
1170 |
// Delete the term if no taxonomies use it. |
|
1171 |
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) ) |
|
1172 |
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) ); |
|
1173 |
|
|
1174 |
clean_term_cache($term, $taxonomy); |
|
1175 |
|
|
1176 |
do_action('delete_term', $term, $tt_id, $taxonomy); |
|
1177 |
do_action("delete_$taxonomy", $term, $tt_id); |
|
1178 |
|
|
1179 |
return true; |
|
1180 |
} |
|
1181 |
|
|
1182 |
/** |
|
1183 |
* Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
|
1184 |
* |
|
1185 |
* The following information has to do the $args parameter and for what can be |
|
1186 |
* contained in the string or array of that parameter, if it exists. |
|
1187 |
* |
|
1188 |
* The first argument is called, 'orderby' and has the default value of 'name'. |
|
1189 |
* The other value that is supported is 'count'. |
|
1190 |
* |
|
1191 |
* The second argument is called, 'order' and has the default value of 'ASC'. |
|
1192 |
* The only other value that will be acceptable is 'DESC'. |
|
1193 |
* |
|
1194 |
* The final argument supported is called, 'fields' and has the default value of |
|
1195 |
* 'all'. There are multiple other options that can be used instead. Supported |
|
1196 |
* values are as follows: 'all', 'ids', 'names', and finally |
|
1197 |
* 'all_with_object_id'. |
|
1198 |
* |
|
1199 |
* The fields argument also decides what will be returned. If 'all' or |
|
1200 |
* 'all_with_object_id' is choosen or the default kept intact, then all matching |
|
1201 |
* terms objects will be returned. If either 'ids' or 'names' is used, then an |
|
1202 |
* array of all matching term ids or term names will be returned respectively. |
|
1203 |
* |
|
1204 |
* @package WordPress |
|
1205 |
* @subpackage Taxonomy |
|
1206 |
* @since 2.3.0 |
|
1207 |
* @uses $wpdb |
|
1208 |
* |
|
1209 |
* @param int|array $object_id The id of the object(s) to retrieve. |
|
1210 |
* @param string|array $taxonomies The taxonomies to retrieve terms from. |
|
1211 |
* @param array|string $args Change what is returned |
|
1212 |
* @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist. |
|
1213 |
*/ |
|
1214 |
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { |
|
1215 |
global $wpdb; |
|
1216 |
|
|
1217 |
if ( !is_array($taxonomies) ) |
|
1218 |
$taxonomies = array($taxonomies); |
|
1219 |
|
|
1220 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
1221 |
if ( ! is_taxonomy($taxonomy) ) |
|
1222 |
return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
1223 |
} |
|
1224 |
|
|
1225 |
if ( !is_array($object_ids) ) |
|
1226 |
$object_ids = array($object_ids); |
|
1227 |
$object_ids = array_map('intval', $object_ids); |
|
1228 |
|
|
1229 |
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); |
|
1230 |
$args = wp_parse_args( $args, $defaults ); |
|
1231 |
|
|
1232 |
$terms = array(); |
|
1233 |
if ( count($taxonomies) > 1 ) { |
|
1234 |
foreach ( $taxonomies as $index => $taxonomy ) { |
|
1235 |
$t = get_taxonomy($taxonomy); |
|
1236 |
if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { |
|
1237 |
unset($taxonomies[$index]); |
|
1238 |
$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); |
|
1239 |
} |
|
1240 |
} |
|
1241 |
} else { |
|
1242 |
$t = get_taxonomy($taxonomies[0]); |
|
1243 |
if ( isset($t->args) && is_array($t->args) ) |
|
1244 |
$args = array_merge($args, $t->args); |
|
1245 |
} |
|
1246 |
|
|
1247 |
extract($args, EXTR_SKIP); |
|
1248 |
|
|
1249 |
if ( 'count' == $orderby ) |
|
1250 |
$orderby = 'tt.count'; |
|
1251 |
else if ( 'name' == $orderby ) |
|
1252 |
$orderby = 't.name'; |
|
1253 |
else if ( 'slug' == $orderby ) |
|
1254 |
$orderby = 't.slug'; |
|
1255 |
else if ( 'term_group' == $orderby ) |
|
1256 |
$orderby = 't.term_group'; |
|
1257 |
else if ( 'term_order' == $orderby ) |
|
1258 |
$orderby = 'tr.term_order'; |
|
1259 |
else if ( 'none' == $orderby ) { |
|
1260 |
$orderby = ''; |
|
1261 |
$order = ''; |
|
1262 |
} else { |
|
1263 |
$orderby = 't.term_id'; |
|
1264 |
} |
|
1265 |
|
|
1266 |
// tt_ids queries can only be none or tr.term_taxonomy_id |
|
1267 |
if ( ('tt_ids' == $fields) && !empty($orderby) ) |
|
1268 |
$orderby = 'tr.term_taxonomy_id'; |
|
1269 |
|
|
1270 |
if ( !empty($orderby) ) |
|
1271 |
$orderby = "ORDER BY $orderby"; |
|
1272 |
|
|
1273 |
$taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|
1274 |
$object_ids = implode(', ', $object_ids); |
|
1275 |
|
|
1276 |
$select_this = ''; |
|
1277 |
if ( 'all' == $fields ) |
|
1278 |
$select_this = 't.*, tt.*'; |
|
1279 |
else if ( 'ids' == $fields ) |
|
1280 |
$select_this = 't.term_id'; |
|
1281 |
else if ( 'names' == $fields ) |
|
1282 |
$select_this = 't.name'; |
|
1283 |
else if ( 'all_with_object_id' == $fields ) |
|
1284 |
$select_this = 't.*, tt.*, tr.object_id'; |
|
1285 |
|
|
1286 |
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order"; |
|
1287 |
|
|
1288 |
if ( 'all' == $fields || 'all_with_object_id' == $fields ) { |
|
1289 |
$terms = array_merge($terms, $wpdb->get_results($query)); |
|
1290 |
update_term_cache($terms); |
|
1291 |
} else if ( 'ids' == $fields || 'names' == $fields ) { |
|
1292 |
$terms = array_merge($terms, $wpdb->get_col($query)); |
|
1293 |
} else if ( 'tt_ids' == $fields ) { |
|
1294 |
$terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order"); |
|
1295 |
} |
|
1296 |
|
|
1297 |
if ( ! $terms ) |
|
1298 |
$terms = array(); |
|
1299 |
|
|
1300 |
return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args); |
|
1301 |
} |
|
1302 |
|
|
1303 |
/** |
|
1304 |
* Adds a new term to the database. Optionally marks it as an alias of an existing term. |
|
1305 |
* |
|
1306 |
* Error handling is assigned for the nonexistance of the $taxonomy and $term |
|
1307 |
* parameters before inserting. If both the term id and taxonomy exist |
|
1308 |
* previously, then an array will be returned that contains the term id and the |
|
1309 |
* contents of what is returned. The keys of the array are 'term_id' and |
|
1310 |
* 'term_taxonomy_id' containing numeric values. |
|
1311 |
* |
|
1312 |
* It is assumed that the term does not yet exist or the above will apply. The |
|
1313 |
* term will be first added to the term table and then related to the taxonomy |
|
1314 |
* if everything is well. If everything is correct, then several actions will be |
|
1315 |
* run prior to a filter and then several actions will be run after the filter |
|
1316 |
* is run. |
|
1317 |
* |
|
1318 |
* The arguments decide how the term is handled based on the $args parameter. |
|
1319 |
* The following is a list of the available overrides and the defaults. |
|
1320 |
* |
|
1321 |
* 'alias_of'. There is no default, but if added, expected is the slug that the |
|
1322 |
* term will be an alias of. Expected to be a string. |
|
1323 |
* |
|
1324 |
* 'description'. There is no default. If exists, will be added to the database |
|
1325 |
* along with the term. Expected to be a string. |
|
1326 |
* |
|
1327 |
* 'parent'. Expected to be numeric and default is 0 (zero). Will assign value |
|
1328 |
* of 'parent' to the term. |
|
1329 |
* |
|
1330 |
* 'slug'. Expected to be a string. There is no default. |
|
1331 |
* |
|
1332 |
* If 'slug' argument exists then the slug will be checked to see if it is not |
|
1333 |
* a valid term. If that check succeeds (it is not a valid term), then it is |
|
1334 |
* added and the term id is given. If it fails, then a check is made to whether |
|
1335 |
* the taxonomy is hierarchical and the parent argument is not empty. If the |
|
1336 |
* second check succeeds, the term will be inserted and the term id will be |
|
1337 |
* given. |
|
1338 |
* |
|
1339 |
* @package WordPress |
|
1340 |
* @subpackage Taxonomy |
|
1341 |
* @since 2.3.0 |
|
1342 |
* @uses $wpdb |
|
1343 |
* |
|
1344 |
* @uses do_action() Calls 'create_term' hook with the term id and taxonomy id as parameters. |
|
1345 |
* @uses do_action() Calls 'create_$taxonomy' hook with term id and taxonomy id as parameters. |
|
1346 |
* @uses apply_filters() Calls 'term_id_filter' hook with term id and taxonomy id as parameters. |
|
1347 |
* @uses do_action() Calls 'created_term' hook with the term id and taxonomy id as parameters. |
|
1348 |
* @uses do_action() Calls 'created_$taxonomy' hook with term id and taxonomy id as parameters. |
|
1349 |
* |
|
1350 |
* @param int|string $term The term to add or update. |
|
1351 |
* @param string $taxonomy The taxonomy to which to add the term |
|
1352 |
* @param array|string $args Change the values of the inserted term |
|
1353 |
* @return array|WP_Error The Term ID and Term Taxonomy ID |
|
1354 |
*/ |
|
1355 |
function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|
1356 |
global $wpdb; |
|
1357 |
|
|
1358 |
if ( ! is_taxonomy($taxonomy) ) |
|
1359 |
return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
1360 |
|
|
1361 |
if ( is_int($term) && 0 == $term ) |
|
1362 |
return new WP_Error('invalid_term_id', __('Invalid term ID')); |
|
1363 |
|
|
1364 |
if ( '' == trim($term) ) |
|
1365 |
return new WP_Error('empty_term_name', __('A name is required for this term')); |
|
1366 |
|
|
1367 |
$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|
1368 |
$args = wp_parse_args($args, $defaults); |
|
1369 |
$args['name'] = $term; |
|
1370 |
$args['taxonomy'] = $taxonomy; |
|
1371 |
$args = sanitize_term($args, $taxonomy, 'db'); |
|
1372 |
extract($args, EXTR_SKIP); |
|
1373 |
|
|
1374 |
// expected_slashed ($name) |
|
1375 |
$name = stripslashes($name); |
|
1376 |
$description = stripslashes($description); |
|
1377 |
|
|
1378 |
if ( empty($slug) ) |
|
1379 |
$slug = sanitize_title($name); |
|
1380 |
|
|
1381 |
$term_group = 0; |
|
1382 |
if ( $alias_of ) { |
|
1383 |
$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); |
|
1384 |
if ( $alias->term_group ) { |
|
1385 |
// The alias we want is already in a group, so let's use that one. |
|
1386 |
$term_group = $alias->term_group; |
|
1387 |
} else { |
|
1388 |
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|
1389 |
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; |
|
1390 |
do_action( 'edit_terms', $alias->term_id ); |
|
1391 |
$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); |
|
1392 |
do_action( 'edited_terms', $alias->term_id ); |
|
1393 |
} |
|
1394 |
} |
|
1395 |
|
|
1396 |
if ( ! $term_id = is_term($slug) ) { |
|
1397 |
if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) |
|
1398 |
return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); |
|
1399 |
$term_id = (int) $wpdb->insert_id; |
|
1400 |
} else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) { |
|
1401 |
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique |
|
1402 |
// by incorporating parent slugs. |
|
1403 |
$slug = wp_unique_term_slug($slug, (object) $args); |
|
1404 |
if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) |
|
1405 |
return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); |
|
1406 |
$term_id = (int) $wpdb->insert_id; |
|
1407 |
} |
|
1408 |
|
|
1409 |
if ( empty($slug) ) { |
|
1410 |
$slug = sanitize_title($slug, $term_id); |
|
1411 |
do_action( 'edit_terms', $term_id ); |
|
1412 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
1413 |
do_action( 'edited_terms', $term_id ); |
|
1414 |
} |
|
1415 |
|
|
1416 |
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) ); |
|
1417 |
|
|
1418 |
if ( !empty($tt_id) ) |
|
1419 |
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
1420 |
|
|
1421 |
$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); |
|
1422 |
$tt_id = (int) $wpdb->insert_id; |
|
1423 |
|
|
1424 |
do_action("create_term", $term_id, $tt_id, $taxonomy); |
|
1425 |
do_action("create_$taxonomy", $term_id, $tt_id); |
|
1426 |
|
|
1427 |
$term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|
1428 |
|
|
1429 |
clean_term_cache($term_id, $taxonomy); |
|
1430 |
|
|
1431 |
do_action("created_term", $term_id, $tt_id, $taxonomy); |
|
1432 |
do_action("created_$taxonomy", $term_id, $tt_id); |
|
1433 |
|
|
1434 |
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
1435 |
} |
|
1436 |
|
|
1437 |
/** |
|
1438 |
* Create Term and Taxonomy Relationships. |
|
1439 |
* |
|
1440 |
* Relates an object (post, link etc) to a term and taxonomy type. Creates the |
|
1441 |
* term and taxonomy relationship if it doesn't already exist. Creates a term if |
|
1442 |
* it doesn't exist (using the slug). |
|
1443 |
* |
|
1444 |
* A relationship means that the term is grouped in or belongs to the taxonomy. |
|
1445 |
* A term has no meaning until it is given context by defining which taxonomy it |
|
1446 |
* exists under. |
|
1447 |
* |
|
1448 |
* @package WordPress |
|
1449 |
* @subpackage Taxonomy |
|
1450 |
* @since 2.3.0 |
|
1451 |
* @uses $wpdb |
|
1452 |
* |
|
1453 |
* @param int $object_id The object to relate to. |
|
1454 |
* @param array|int|string $term The slug or id of the term, will replace all existing |
|
1455 |
* related terms in this taxonomy. |
|
1456 |
* @param array|string $taxonomy The context in which to relate the term to the object. |
|
1457 |
* @param bool $append If false will delete difference of terms. |
|
1458 |
* @return array|WP_Error Affected Term IDs |
|
1459 |
*/ |
|
1460 |
function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { |
|
1461 |
global $wpdb; |
|
1462 |
|
|
1463 |
$object_id = (int) $object_id; |
|
1464 |
|
|
1465 |
if ( ! is_taxonomy($taxonomy) ) |
|
1466 |
return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|
1467 |
|
|
1468 |
if ( !is_array($terms) ) |
|
1469 |
$terms = array($terms); |
|
1470 |
|
|
1471 |
if ( ! $append ) |
|
1472 |
$old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); |
|
1473 |
|
|
1474 |
$tt_ids = array(); |
|
1475 |
$term_ids = array(); |
|
1476 |
|
|
1477 |
foreach ( (array) $terms as $term) { |
|
1478 |
if ( !strlen(trim($term)) ) |
|
1479 |
continue; |
|
1480 |
|
|
1481 |
if ( !$term_info = is_term($term, $taxonomy) ) |
|
1482 |
$term_info = wp_insert_term($term, $taxonomy); |
|
1483 |
if ( is_wp_error($term_info) ) |
|
1484 |
return $term_info; |
|
1485 |
$term_ids[] = $term_info['term_id']; |
|
1486 |
$tt_id = $term_info['term_taxonomy_id']; |
|
1487 |
$tt_ids[] = $tt_id; |
|
1488 |
|
|
1489 |
if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) |
|
1490 |
continue; |
|
1491 |
do_action( 'add_term_relationship', $object_id, $tt_id ); |
|
1492 |
$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) ); |
|
1493 |
do_action( 'added_term_relationship', $object_id, $tt_id ); |
|
1494 |
} |
|
1495 |
|
|
1496 |
wp_update_term_count($tt_ids, $taxonomy); |
|
1497 |
|
|
1498 |
if ( ! $append ) { |
|
1499 |
$delete_terms = array_diff($old_tt_ids, $tt_ids); |
|
1500 |
if ( $delete_terms ) { |
|
1501 |
$in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; |
|
1502 |
do_action( 'delete_term_relationships', $object_id, $delete_terms ); |
|
1503 |
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) ); |
|
1504 |
do_action( 'deleted_term_relationships', $object_id, $delete_terms ); |
|
1505 |
wp_update_term_count($delete_terms, $taxonomy); |
|
1506 |
} |
|
1507 |
} |
|
1508 |
|
|
1509 |
$t = get_taxonomy($taxonomy); |
|
1510 |
if ( ! $append && isset($t->sort) && $t->sort ) { |
|
1511 |
$values = array(); |
|
1512 |
$term_order = 0; |
|
1513 |
$final_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|
1514 |
foreach ( $tt_ids as $tt_id ) |
|
1515 |
if ( in_array($tt_id, $final_tt_ids) ) |
|
1516 |
$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order); |
|
1517 |
if ( $values ) |
|
1518 |
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); |
|
1519 |
} |
|
1520 |
|
|
1521 |
do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); |
|
1522 |
return $tt_ids; |
|
1523 |
} |
|
1524 |
|
|
1525 |
/** |
|
1526 |
* Will make slug unique, if it isn't already. |
|
1527 |
* |
|
1528 |
* The $slug has to be unique global to every taxonomy, meaning that one |
|
1529 |
* taxonomy term can't have a matching slug with another taxonomy term. Each |
|
1530 |
* slug has to be globally unique for every taxonomy. |
|
1531 |
* |
|
1532 |
* The way this works is that if the taxonomy that the term belongs to is |
|
1533 |
* heirarchical and has a parent, it will append that parent to the $slug. |
|
1534 |
* |
|
1535 |
* If that still doesn't return an unique slug, then it try to append a number |
|
1536 |
* until it finds a number that is truely unique. |
|
1537 |
* |
|
1538 |
* The only purpose for $term is for appending a parent, if one exists. |
|
1539 |
* |
|
1540 |
* @package WordPress |
|
1541 |
* @subpackage Taxonomy |
|
1542 |
* @since 2.3.0 |
|
1543 |
* @uses $wpdb |
|
1544 |
* |
|
1545 |
* @param string $slug The string that will be tried for a unique slug |
|
1546 |
* @param object $term The term object that the $slug will belong too |
|
1547 |
* @return string Will return a true unique slug. |
|
1548 |
*/ |
|
1549 |
function wp_unique_term_slug($slug, $term) { |
|
1550 |
global $wpdb; |
|
1551 |
|
|
1552 |
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique |
|
1553 |
// by incorporating parent slugs. |
|
1554 |
if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) { |
|
1555 |
$the_parent = $term->parent; |
|
1556 |
while ( ! empty($the_parent) ) { |
|
1557 |
$parent_term = get_term($the_parent, $term->taxonomy); |
|
1558 |
if ( is_wp_error($parent_term) || empty($parent_term) ) |
|
1559 |
break; |
|
1560 |
$slug .= '-' . $parent_term->slug; |
|
1561 |
if ( empty($parent_term->parent) ) |
|
1562 |
break; |
|
1563 |
$the_parent = $parent_term->parent; |
|
1564 |
} |
|
1565 |
} |
|
1566 |
|
|
1567 |
// If we didn't get a unique slug, try appending a number to make it unique. |
|
1568 |
if ( !empty($args['term_id']) ) |
|
1569 |
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] ); |
|
1570 |
else |
|
1571 |
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
|
1572 |
|
|
1573 |
if ( $wpdb->get_var( $query ) ) { |
|
1574 |
$num = 2; |
|
1575 |
do { |
|
1576 |
$alt_slug = $slug . "-$num"; |
|
1577 |
$num++; |
|
1578 |
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
|
1579 |
} while ( $slug_check ); |
|
1580 |
$slug = $alt_slug; |
|
1581 |
} |
|
1582 |
|
|
1583 |
return $slug; |
|
1584 |
} |
|
1585 |
|
|
1586 |
/** |
|
1587 |
* Update term based on arguments provided. |
|
1588 |
* |
|
1589 |
* The $args will indiscriminately override all values with the same field name. |
|
1590 |
* Care must be taken to not override important information need to update or |
|
1591 |
* update will fail (or perhaps create a new term, neither would be acceptable). |
|
1592 |
* |
|
1593 |
* Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not |
|
1594 |
* defined in $args already. |
|
1595 |
* |
|
1596 |
* 'alias_of' will create a term group, if it doesn't already exist, and update |
|
1597 |
* it for the $term. |
|
1598 |
* |
|
1599 |
* If the 'slug' argument in $args is missing, then the 'name' in $args will be |
|
1600 |
* used. It should also be noted that if you set 'slug' and it isn't unique then |
|
1601 |
* a WP_Error will be passed back. If you don't pass any slug, then a unique one |
|
1602 |
* will be created for you. |
|
1603 |
* |
|
1604 |
* For what can be overrode in $args, check the term scheme can contain and stay |
|
1605 |
* away from the term keys. |
|
1606 |
* |
|
1607 |
* @package WordPress |
|
1608 |
* @subpackage Taxonomy |
|
1609 |
* @since 2.3.0 |
|
1610 |
* |
|
1611 |
* @uses $wpdb |
|
1612 |
* @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice. |
|
1613 |
* @uses apply_filters() Will call the 'term_id_filter' filter and pass the term |
|
1614 |
* id and taxonomy id. |
|
1615 |
* |
|
1616 |
* @param int $term_id The ID of the term |
|
1617 |
* @param string $taxonomy The context in which to relate the term to the object. |
|
1618 |
* @param array|string $args Overwrite term field values |
|
1619 |
* @return array|WP_Error Returns Term ID and Taxonomy Term ID |
|
1620 |
*/ |
|
1621 |
function wp_update_term( $term_id, $taxonomy, $args = array() ) { |
|
1622 |
global $wpdb; |
|
1623 |
|
|
1624 |
if ( ! is_taxonomy($taxonomy) ) |
|
1625 |
return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
1626 |
|
|
1627 |
$term_id = (int) $term_id; |
|
1628 |
|
|
1629 |
// First, get all of the original args |
|
1630 |
$term = get_term ($term_id, $taxonomy, ARRAY_A); |
|
1631 |
|
|
1632 |
if ( is_wp_error( $term ) ) |
|
1633 |
return $term; |
|
1634 |
|
|
1635 |
// Escape data pulled from DB. |
|
1636 |
$term = add_magic_quotes($term); |
|
1637 |
|
|
1638 |
// Merge old and new args with new args overwriting old ones. |
|
1639 |
$args = array_merge($term, $args); |
|
1640 |
|
|
1641 |
$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|
1642 |
$args = wp_parse_args($args, $defaults); |
|
1643 |
$args = sanitize_term($args, $taxonomy, 'db'); |
|
1644 |
extract($args, EXTR_SKIP); |
|
1645 |
|
|
1646 |
// expected_slashed ($name) |
|
1647 |
$name = stripslashes($name); |
|
1648 |
$description = stripslashes($description); |
|
1649 |
|
|
1650 |
if ( '' == trim($name) ) |
|
1651 |
return new WP_Error('empty_term_name', __('A name is required for this term')); |
|
1652 |
|
|
1653 |
$empty_slug = false; |
|
1654 |
if ( empty($slug) ) { |
|
1655 |
$empty_slug = true; |
|
1656 |
$slug = sanitize_title($name); |
|
1657 |
} |
|
1658 |
|
|
1659 |
if ( $alias_of ) { |
|
1660 |
$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); |
|
1661 |
if ( $alias->term_group ) { |
|
1662 |
// The alias we want is already in a group, so let's use that one. |
|
1663 |
$term_group = $alias->term_group; |
|
1664 |
} else { |
|
1665 |
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|
1666 |
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; |
|
1667 |
do_action( 'edit_terms', $alias->term_id ); |
|
1668 |
$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); |
|
1669 |
do_action( 'edited_terms', $alias->term_id ); |
|
1670 |
} |
|
1671 |
} |
|
1672 |
|
|
1673 |
// Check for duplicate slug |
|
1674 |
$id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); |
|
1675 |
if ( $id && ($id != $term_id) ) { |
|
1676 |
// If an empty slug was passed or the parent changed, reset the slug to something unique. |
|
1677 |
// Otherwise, bail. |
|
1678 |
if ( $empty_slug || ( $parent != $term->parent) ) |
|
1679 |
$slug = wp_unique_term_slug($slug, (object) $args); |
|
1680 |
else |
|
1681 |
return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); |
|
1682 |
} |
|
1683 |
do_action( 'edit_terms', $term_id ); |
|
1684 |
$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); |
|
1685 |
if ( empty($slug) ) { |
|
1686 |
$slug = sanitize_title($name, $term_id); |
|
1687 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
1688 |
} |
|
1689 |
do_action( 'edited_terms', $term_id ); |
|
1690 |
|
|
1691 |
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); |
|
1692 |
do_action( 'edit_term_taxonomy', $tt_id ); |
|
1693 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); |
|
1694 |
do_action( 'edited_term_taxonomy', $tt_id ); |
|
1695 |
|
|
1696 |
do_action("edit_term", $term_id, $tt_id, $taxonomy); |
|
1697 |
do_action("edit_$taxonomy", $term_id, $tt_id); |
|
1698 |
|
|
1699 |
$term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|
1700 |
|
|
1701 |
clean_term_cache($term_id, $taxonomy); |
|
1702 |
|
|
1703 |
do_action("edited_term", $term_id, $tt_id, $taxonomy); |
|
1704 |
do_action("edited_$taxonomy", $term_id, $tt_id); |
|
1705 |
|
|
1706 |
return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
1707 |
} |
|
1708 |
|
|
1709 |
/** |
|
1710 |
* Enable or disable term counting. |
|
1711 |
* |
|
1712 |
* @since 2.5.0 |
|
1713 |
* |
|
1714 |
* @param bool $defer Optional. Enable if true, disable if false. |
|
1715 |
* @return bool Whether term counting is enabled or disabled. |
|
1716 |
*/ |
|
1717 |
function wp_defer_term_counting($defer=null) { |
|
1718 |
static $_defer = false; |
|
1719 |
|
|
1720 |
if ( is_bool($defer) ) { |
|
1721 |
$_defer = $defer; |
|
1722 |
// flush any deferred counts |
|
1723 |
if ( !$defer ) |
|
1724 |
wp_update_term_count( null, null, true ); |
|
1725 |
} |
|
1726 |
|
|
1727 |
return $_defer; |
|
1728 |
} |
|
1729 |
|
|
1730 |
/** |
|
1731 |
* Updates the amount of terms in taxonomy. |
|
1732 |
* |
|
1733 |
* If there is a taxonomy callback applyed, then it will be called for updating |
|
1734 |
* the count. |
|
1735 |
* |
|
1736 |
* The default action is to count what the amount of terms have the relationship |
|
1737 |
* of term ID. Once that is done, then update the database. |
|
1738 |
* |
|
1739 |
* @package WordPress |
|
1740 |
* @subpackage Taxonomy |
|
1741 |
* @since 2.3.0 |
|
1742 |
* @uses $wpdb |
|
1743 |
* |
|
1744 |
* @param int|array $terms The term_taxonomy_id of the terms |
|
1745 |
* @param string $taxonomy The context of the term. |
|
1746 |
* @return bool If no terms will return false, and if successful will return true. |
|
1747 |
*/ |
|
1748 |
function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) { |
|
1749 |
static $_deferred = array(); |
|
1750 |
|
|
1751 |
if ( $do_deferred ) { |
|
1752 |
foreach ( (array) array_keys($_deferred) as $tax ) { |
|
1753 |
wp_update_term_count_now( $_deferred[$tax], $tax ); |
|
1754 |
unset( $_deferred[$tax] ); |
|
1755 |
} |
|
1756 |
} |
|
1757 |
|
|
1758 |
if ( empty($terms) ) |
|
1759 |
return false; |
|
1760 |
|
|
1761 |
if ( !is_array($terms) ) |
|
1762 |
$terms = array($terms); |
|
1763 |
|
|
1764 |
if ( wp_defer_term_counting() ) { |
|
1765 |
if ( !isset($_deferred[$taxonomy]) ) |
|
1766 |
$_deferred[$taxonomy] = array(); |
|
1767 |
$_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); |
|
1768 |
return true; |
|
1769 |
} |
|
1770 |
|
|
1771 |
return wp_update_term_count_now( $terms, $taxonomy ); |
|
1772 |
} |
|
1773 |
|
|
1774 |
/** |
|
1775 |
* Perform term count update immediately. |
|
1776 |
* |
|
1777 |
* @since 2.5.0 |
|
1778 |
* |
|
1779 |
* @param array $terms The term_taxonomy_id of terms to update. |
|
1780 |
* @param string $taxonomy The context of the term. |
|
1781 |
* @return bool Always true when complete. |
|
1782 |
*/ |
|
1783 |
function wp_update_term_count_now( $terms, $taxonomy ) { |
|
1784 |
global $wpdb; |
|
1785 |
|
|
1786 |
$terms = array_map('intval', $terms); |
|
1787 |
|
|
1788 |
$taxonomy = get_taxonomy($taxonomy); |
|
1789 |
if ( !empty($taxonomy->update_count_callback) ) { |
|
1790 |
call_user_func($taxonomy->update_count_callback, $terms); |
|
1791 |
} else { |
|
1792 |
// Default count updater |
|
1793 |
foreach ( (array) $terms as $term) { |
|
1794 |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) ); |
|
1795 |
do_action( 'edit_term_taxonomy', $term ); |
|
1796 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|
1797 |
do_action( 'edited_term_taxonomy', $term ); |
|
1798 |
} |
|
1799 |
|
|
1800 |
} |
|
1801 |
|
|
1802 |
clean_term_cache($terms); |
|
1803 |
|
|
1804 |
return true; |
|
1805 |
} |
|
1806 |
|
|
1807 |
// |
|
1808 |
// Cache |
|
1809 |
// |
|
1810 |
|
|
1811 |
|
|
1812 |
/** |
|
1813 |
* Removes the taxonomy relationship to terms from the cache. |
|
1814 |
* |
|
1815 |
* Will remove the entire taxonomy relationship containing term $object_id. The |
|
1816 |
* term IDs have to exist within the taxonomy $object_type for the deletion to |
|
1817 |
* take place. |
|
1818 |
* |
|
1819 |
* @package WordPress |
|
1820 |
* @subpackage Taxonomy |
|
1821 |
* @since 2.3.0 |
|
1822 |
* |
|
1823 |
* @see get_object_taxonomies() for more on $object_type |
|
1824 |
* @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion. |
|
1825 |
* Passes, function params in same order. |
|
1826 |
* |
|
1827 |
* @param int|array $object_ids Single or list of term object ID(s) |
|
1828 |
* @param array|string $object_type The taxonomy object type |
|
1829 |
*/ |
|
1830 |
function clean_object_term_cache($object_ids, $object_type) { |
|
1831 |
if ( !is_array($object_ids) ) |
|
1832 |
$object_ids = array($object_ids); |
|
1833 |
|
|
1834 |
foreach ( $object_ids as $id ) |
|
1835 |
foreach ( get_object_taxonomies($object_type) as $taxonomy ) |
|
1836 |
wp_cache_delete($id, "{$taxonomy}_relationships"); |
|
1837 |
|
|
1838 |
do_action('clean_object_term_cache', $object_ids, $object_type); |
|
1839 |
} |
|
1840 |
|
|
1841 |
|
|
1842 |
/** |
|
1843 |
* Will remove all of the term ids from the cache. |
|
1844 |
* |
|
1845 |
* @package WordPress |
|
1846 |
* @subpackage Taxonomy |
|
1847 |
* @since 2.3.0 |
|
1848 |
* @uses $wpdb |
|
1849 |
* |
|
1850 |
* @param int|array $ids Single or list of Term IDs |
|
1851 |
* @param string $taxonomy Can be empty and will assume tt_ids, else will use for context. |
|
1852 |
*/ |
|
1853 |
function clean_term_cache($ids, $taxonomy = '') { |
|
1854 |
global $wpdb; |
|
1855 |
static $cleaned = array(); |
|
1856 |
|
|
1857 |
if ( !is_array($ids) ) |
|
1858 |
$ids = array($ids); |
|
1859 |
|
|
1860 |
$taxonomies = array(); |
|
1861 |
// If no taxonomy, assume tt_ids. |
|
1862 |
if ( empty($taxonomy) ) { |
|
1863 |
$tt_ids = implode(', ', $ids); |
|
1864 |
$terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); |
|
1865 |
foreach ( (array) $terms as $term ) { |
|
1866 |
$taxonomies[] = $term->taxonomy; |
|
1867 |
wp_cache_delete($term->term_id, $term->taxonomy); |
|
1868 |
} |
|
1869 |
$taxonomies = array_unique($taxonomies); |
|
1870 |
} else { |
|
1871 |
foreach ( $ids as $id ) { |
|
1872 |
wp_cache_delete($id, $taxonomy); |
|
1873 |
} |
|
1874 |
$taxonomies = array($taxonomy); |
|
1875 |
} |
|
1876 |
|
|
1877 |
foreach ( $taxonomies as $taxonomy ) { |
|
1878 |
if ( isset($cleaned[$taxonomy]) ) |
|
1879 |
continue; |
|
1880 |
$cleaned[$taxonomy] = true; |
|
1881 |
wp_cache_delete('all_ids', $taxonomy); |
|
1882 |
wp_cache_delete('get', $taxonomy); |
|
1883 |
delete_option("{$taxonomy}_children"); |
|
1884 |
} |
|
1885 |
|
|
1886 |
wp_cache_set('last_changed', time(), 'terms'); |
|
1887 |
|
|
1888 |
do_action('clean_term_cache', $ids, $taxonomy); |
|
1889 |
} |
|
1890 |
|
|
1891 |
|
|
1892 |
/** |
|
1893 |
* Retrieves the taxonomy relationship to the term object id. |
|
1894 |
* |
|
1895 |
* @package WordPress |
|
1896 |
* @subpackage Taxonomy |
|
1897 |
* @since 2.3.0 |
|
1898 |
* |
|
1899 |
* @uses wp_cache_get() Retrieves taxonomy relationship from cache |
|
1900 |
* |
|
1901 |
* @param int|array $id Term object ID |
|
1902 |
* @param string $taxonomy Taxonomy Name |
|
1903 |
* @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. |
|
1904 |
*/ |
|
1905 |
function &get_object_term_cache($id, $taxonomy) { |
|
1906 |
$cache = wp_cache_get($id, "{$taxonomy}_relationships"); |
|
1907 |
return $cache; |
|
1908 |
} |
|
1909 |
|
|
1910 |
|
|
1911 |
/** |
|
1912 |
* Updates the cache for Term ID(s). |
|
1913 |
* |
|
1914 |
* Will only update the cache for terms not already cached. |
|
1915 |
* |
|
1916 |
* The $object_ids expects that the ids be separated by commas, if it is a |
|
1917 |
* string. |
|
1918 |
* |
|
1919 |
* It should be noted that update_object_term_cache() is very time extensive. It |
|
1920 |
* is advised that the function is not called very often or at least not for a |
|
1921 |
* lot of terms that exist in a lot of taxonomies. The amount of time increases |
|
1922 |
* for each term and it also increases for each taxonomy the term belongs to. |
|
1923 |
* |
|
1924 |
* @package WordPress |
|
1925 |
* @subpackage Taxonomy |
|
1926 |
* @since 2.3.0 |
|
1927 |
* @uses wp_get_object_terms() Used to get terms from the database to update |
|
1928 |
* |
|
1929 |
* @param string|array $object_ids Single or list of term object ID(s) |
|
1930 |
* @param array|string $object_type The taxonomy object type |
|
1931 |
* @return null|bool Null value is given with empty $object_ids. False if |
|
1932 |
*/ |
|
1933 |
function update_object_term_cache($object_ids, $object_type) { |
|
1934 |
if ( empty($object_ids) ) |
|
1935 |
return; |
|
1936 |
|
|
1937 |
if ( !is_array($object_ids) ) |
|
1938 |
$object_ids = explode(',', $object_ids); |
|
1939 |
|
|
1940 |
$object_ids = array_map('intval', $object_ids); |
|
1941 |
|
|
1942 |
$taxonomies = get_object_taxonomies($object_type); |
|
1943 |
|
|
1944 |
$ids = array(); |
|
1945 |
foreach ( (array) $object_ids as $id ) { |
|
1946 |
foreach ( $taxonomies as $taxonomy ) { |
|
1947 |
if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) { |
|
1948 |
$ids[] = $id; |
|
1949 |
break; |
|
1950 |
} |
|
1951 |
} |
|
1952 |
} |
|
1953 |
|
|
1954 |
if ( empty( $ids ) ) |
|
1955 |
return false; |
|
1956 |
|
|
1957 |
$terms = wp_get_object_terms($ids, $taxonomies, 'fields=all_with_object_id'); |
|
1958 |
|
|
1959 |
$object_terms = array(); |
|
1960 |
foreach ( (array) $terms as $term ) |
|
1961 |
$object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; |
|
1962 |
|
|
1963 |
foreach ( $ids as $id ) { |
|
1964 |
foreach ( $taxonomies as $taxonomy ) { |
|
1965 |
if ( ! isset($object_terms[$id][$taxonomy]) ) { |
|
1966 |
if ( !isset($object_terms[$id]) ) |
|
1967 |
$object_terms[$id] = array(); |
|
1968 |
$object_terms[$id][$taxonomy] = array(); |
|
1969 |
} |
|
1970 |
} |
|
1971 |
} |
|
1972 |
|
|
1973 |
foreach ( $object_terms as $id => $value ) { |
|
1974 |
foreach ( $value as $taxonomy => $terms ) { |
|
1975 |
wp_cache_set($id, $terms, "{$taxonomy}_relationships"); |
|
1976 |
} |
|
1977 |
} |
|
1978 |
} |
|
1979 |
|
|
1980 |
|
|
1981 |
/** |
|
1982 |
* Updates Terms to Taxonomy in cache. |
|
1983 |
* |
|
1984 |
* @package WordPress |
|
1985 |
* @subpackage Taxonomy |
|
1986 |
* @since 2.3.0 |
|
1987 |
* |
|
1988 |
* @param array $terms List of Term objects to change |
|
1989 |
* @param string $taxonomy Optional. Update Term to this taxonomy in cache |
|
1990 |
*/ |
|
1991 |
function update_term_cache($terms, $taxonomy = '') { |
|
1992 |
foreach ( (array) $terms as $term ) { |
|
1993 |
$term_taxonomy = $taxonomy; |
|
1994 |
if ( empty($term_taxonomy) ) |
|
1995 |
$term_taxonomy = $term->taxonomy; |
|
1996 |
|
|
1997 |
wp_cache_add($term->term_id, $term, $term_taxonomy); |
|
1998 |
} |
|
1999 |
} |
|
2000 |
|
|
2001 |
// |
|
2002 |
// Private |
|
2003 |
// |
|
2004 |
|
|
2005 |
|
|
2006 |
/** |
|
2007 |
* Retrieves children of taxonomy as Term IDs. |
|
2008 |
* |
|
2009 |
* @package WordPress |
|
2010 |
* @subpackage Taxonomy |
|
2011 |
* @access private |
|
2012 |
* @since 2.3.0 |
|
2013 |
* |
|
2014 |
* @uses update_option() Stores all of the children in "$taxonomy_children" |
|
2015 |
* option. That is the name of the taxonomy, immediately followed by '_children'. |
|
2016 |
* |
|
2017 |
* @param string $taxonomy Taxonomy Name |
|
2018 |
* @return array Empty if $taxonomy isn't hierarachical or returns children as Term IDs. |
|
2019 |
*/ |
|
2020 |
function _get_term_hierarchy($taxonomy) { |
|
2021 |
if ( !is_taxonomy_hierarchical($taxonomy) ) |
|
2022 |
return array(); |
|
2023 |
$children = get_option("{$taxonomy}_children"); |
|
2024 |
if ( is_array($children) ) |
|
2025 |
return $children; |
|
2026 |
|
|
2027 |
$children = array(); |
|
2028 |
$terms = get_terms($taxonomy, 'get=all'); |
|
2029 |
foreach ( $terms as $term ) { |
|
2030 |
if ( $term->parent > 0 ) |
|
2031 |
$children[$term->parent][] = $term->term_id; |
|
2032 |
} |
|
2033 |
update_option("{$taxonomy}_children", $children); |
|
2034 |
|
|
2035 |
return $children; |
|
2036 |
} |
|
2037 |
|
|
2038 |
|
|
2039 |
/** |
|
2040 |
* Get the subset of $terms that are descendants of $term_id. |
|
2041 |
* |
|
2042 |
* If $terms is an array of objects, then _get_term_children returns an array of objects. |
|
2043 |
* If $terms is an array of IDs, then _get_term_children returns an array of IDs. |
|
2044 |
* |
|
2045 |
* @package WordPress |
|
2046 |
* @subpackage Taxonomy |
|
2047 |
* @access private |
|
2048 |
* @since 2.3.0 |
|
2049 |
* |
|
2050 |
* @param int $term_id The ancestor term: all returned terms should be descendants of $term_id. |
|
2051 |
* @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen. |
|
2052 |
* @param string $taxonomy The taxonomy which determines the hierarchy of the terms. |
|
2053 |
* @return array The subset of $terms that are descendants of $term_id. |
|
2054 |
*/ |
|
2055 |
function &_get_term_children($term_id, $terms, $taxonomy) { |
|
2056 |
$empty_array = array(); |
|
2057 |
if ( empty($terms) ) |
|
2058 |
return $empty_array; |
|
2059 |
|
|
2060 |
$term_list = array(); |
|
2061 |
$has_children = _get_term_hierarchy($taxonomy); |
|
2062 |
|
|
2063 |
if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) |
|
2064 |
return $empty_array; |
|
2065 |
|
|
2066 |
foreach ( (array) $terms as $term ) { |
|
2067 |
$use_id = false; |
|
2068 |
if ( !is_object($term) ) { |
|
2069 |
$term = get_term($term, $taxonomy); |
|
2070 |
if ( is_wp_error( $term ) ) |
|
2071 |
return $term; |
|
2072 |
$use_id = true; |
|
2073 |
} |
|
2074 |
|
|
2075 |
if ( $term->term_id == $term_id ) |
|
2076 |
continue; |
|
2077 |
|
|
2078 |
if ( $term->parent == $term_id ) { |
|
2079 |
if ( $use_id ) |
|
2080 |
$term_list[] = $term->term_id; |
|
2081 |
else |
|
2082 |
$term_list[] = $term; |
|
2083 |
|
|
2084 |
if ( !isset($has_children[$term->term_id]) ) |
|
2085 |
continue; |
|
2086 |
|
|
2087 |
if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) |
|
2088 |
$term_list = array_merge($term_list, $children); |
|
2089 |
} |
|
2090 |
} |
|
2091 |
|
|
2092 |
return $term_list; |
|
2093 |
} |
|
2094 |
|
|
2095 |
|
|
2096 |
/** |
|
2097 |
* Add count of children to parent count. |
|
2098 |
* |
|
2099 |
* Recalculates term counts by including items from child terms. Assumes all |
|
2100 |
* relevant children are already in the $terms argument. |
|
2101 |
* |
|
2102 |
* @package WordPress |
|
2103 |
* @subpackage Taxonomy |
|
2104 |
* @access private |
|
2105 |
* @since 2.3.0 |
|
2106 |
* @uses $wpdb |
|
2107 |
* |
|
2108 |
* @param array $terms List of Term IDs |
|
2109 |
* @param string $taxonomy Term Context |
|
2110 |
* @return null Will break from function if conditions are not met. |
|
2111 |
*/ |
|
2112 |
function _pad_term_counts(&$terms, $taxonomy) { |
|
2113 |
global $wpdb; |
|
2114 |
|
|
2115 |
// This function only works for hierarchical taxonomies like post categories. |
|
2116 |
if ( !is_taxonomy_hierarchical( $taxonomy ) ) |
|
2117 |
return; |
|
2118 |
|
|
2119 |
$term_hier = _get_term_hierarchy($taxonomy); |
|
2120 |
|
|
2121 |
if ( empty($term_hier) ) |
|
2122 |
return; |
|
2123 |
|
|
2124 |
$term_items = array(); |
|
2125 |
|
|
2126 |
foreach ( (array) $terms as $key => $term ) { |
|
2127 |
$terms_by_id[$term->term_id] = & $terms[$key]; |
|
2128 |
$term_ids[$term->term_taxonomy_id] = $term->term_id; |
|
2129 |
} |
|
2130 |
|
|
2131 |
// Get the object and term ids and stick them in a lookup table |
|
2132 |
$results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") AND post_type = 'post' AND post_status = 'publish'"); |
|
2133 |
foreach ( $results as $row ) { |
|
2134 |
$id = $term_ids[$row->term_taxonomy_id]; |
|
2135 |
$term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1; |
|
2136 |
} |
|
2137 |
|
|
2138 |
// Touch every ancestor's lookup row for each post in each term |
|
2139 |
foreach ( $term_ids as $term_id ) { |
|
2140 |
$child = $term_id; |
|
2141 |
while ( $parent = $terms_by_id[$child]->parent ) { |
|
2142 |
if ( !empty($term_items[$term_id]) ) |
|
2143 |
foreach ( $term_items[$term_id] as $item_id => $touches ) { |
|
2144 |
$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1; |
|
2145 |
} |
|
2146 |
$child = $parent; |
|
2147 |
} |
|
2148 |
} |
|
2149 |
|
|
2150 |
// Transfer the touched cells |
|
2151 |
foreach ( (array) $term_items as $id => $items ) |
|
2152 |
if ( isset($terms_by_id[$id]) ) |
|
2153 |
$terms_by_id[$id]->count = count($items); |
|
2154 |
} |
|
2155 |
|
|
2156 |
// |
|
2157 |
// Default callbacks |
|
2158 |
// |
|
2159 |
|
|
2160 |
/** |
|
2161 |
* Will update term count based on posts. |
|
2162 |
* |
|
2163 |
* Private function for the default callback for post_tag and category |
|
2164 |
* taxonomies. |
|
2165 |
* |
|
2166 |
* @package WordPress |
|
2167 |
* @subpackage Taxonomy |
|
2168 |
* @access private |
|
2169 |
* @since 2.3.0 |
|
2170 |
* @uses $wpdb |
|
2171 |
* |
|
2172 |
* @param array $terms List of Term taxonomy IDs |
|
2173 |
*/ |
|
2174 |
function _update_post_term_count( $terms ) { |
|
2175 |
global $wpdb; |
|
2176 |
|
|
2177 |
foreach ( (array) $terms as $term ) { |
|
2178 |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term ) ); |
|
2179 |
do_action( 'edit_term_taxonomy', $term ); |
|
2180 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|
2181 |
do_action( 'edited_term_taxonomy', $term ); |
|
2182 |
} |
|
2183 |
} |
|
2184 |
|
|
2185 |
|
|
2186 |
/** |
|
2187 |
* Generates a permalink for a taxonomy term archive. |
|
2188 |
* |
|
2189 |
* @since 2.5.0 |
|
2190 |
* |
|
2191 |
* @param object|int|string $term |
|
2192 |
* @param string $taxonomy |
|
2193 |
* @return string HTML link to taxonomy term archive |
|
2194 |
*/ |
|
2195 |
function get_term_link( $term, $taxonomy ) { |
|
2196 |
global $wp_rewrite; |
|
2197 |
|
|
2198 |
if ( !is_object($term) ) { |
|
2199 |
if ( is_int($term) ) { |
|
2200 |
$term = &get_term($term, $taxonomy); |
|
2201 |
} else { |
|
2202 |
$term = &get_term_by('slug', $term, $taxonomy); |
|
2203 |
} |
|
2204 |
} |
|
2205 |
if ( is_wp_error( $term ) ) |
|
2206 |
return $term; |
|
2207 |
|
|
2208 |
// use legacy functions for core taxonomies until they are fully plugged in |
|
2209 |
if ( $taxonomy == 'category' ) |
|
2210 |
return get_category_link((int) $term->term_id); |
|
2211 |
if ( $taxonomy == 'post_tag' ) |
|
2212 |
return get_tag_link((int) $term->term_id); |
|
2213 |
|
|
2214 |
$termlink = $wp_rewrite->get_extra_permastruct($taxonomy); |
|
2215 |
|
|
2216 |
$slug = $term->slug; |
|
2217 |
|
|
2218 |
if ( empty($termlink) ) { |
|
2219 |
$file = trailingslashit( get_option('home') ); |
|
2220 |
$t = get_taxonomy($taxonomy); |
|
2221 |
if ( $t->query_var ) |
|
2222 |
$termlink = "$file?$t->query_var=$slug"; |
|
2223 |
else |
|
2224 |
$termlink = "$file?taxonomy=$taxonomy&term=$slug"; |
|
2225 |
} else { |
|
2226 |
$termlink = str_replace("%$taxonomy%", $slug, $termlink); |
|
2227 |
$termlink = get_option('home') . user_trailingslashit($termlink, 'category'); |
|
2228 |
} |
|
2229 |
return apply_filters('term_link', $termlink, $term, $taxonomy); |
|
2230 |
} |
|
2231 |
|
|
2232 |
/** |
|
2233 |
* Display the taxonomies of a post with available options. |
|
2234 |
* |
|
2235 |
* This function can be used within the loop to display the taxonomies for a |
|
2236 |
* post without specifying the Post ID. You can also use it outside the Loop to |
|
2237 |
* display the taxonomies for a specific post. |
|
2238 |
* |
|
2239 |
* The available defaults are: |
|
2240 |
* 'post' : default is 0. The post ID to get taxonomies of. |
|
2241 |
* 'before' : default is empty string. Display before taxonomies list. |
|
2242 |
* 'sep' : default is empty string. Separate every taxonomy with value in this. |
|
2243 |
* 'after' : default is empty string. Display this after the taxonomies list. |
|
2244 |
* |
|
2245 |
* @since 2.5.0 |
|
2246 |
* @uses get_the_taxonomies() |
|
2247 |
* |
|
2248 |
* @param array $args Override the defaults. |
|
2249 |
*/ |
|
2250 |
function the_taxonomies($args = array()) { |
|
2251 |
$defaults = array( |
|
2252 |
'post' => 0, |
|
2253 |
'before' => '', |
|
2254 |
'sep' => ' ', |
|
2255 |
'after' => '', |
|
2256 |
); |
|
2257 |
|
|
2258 |
$r = wp_parse_args( $args, $defaults ); |
|
2259 |
extract( $r, EXTR_SKIP ); |
|
2260 |
|
|
2261 |
echo $before . join($sep, get_the_taxonomies($post)) . $after; |
|
2262 |
} |
|
2263 |
|
|
2264 |
/** |
|
2265 |
* Retrieve all taxonomies associated with a post. |
|
2266 |
* |
|
2267 |
* This function can be used within the loop. It will also return an array of |
|
2268 |
* the taxonomies with links to the taxonomy and name. |
|
2269 |
* |
|
2270 |
* @since 2.5.0 |
|
2271 |
* |
|
2272 |
* @param int $post Optional. Post ID or will use Global Post ID (in loop). |
|
2273 |
* @return array |
|
2274 |
*/ |
|
2275 |
function get_the_taxonomies($post = 0) { |
|
2276 |
if ( is_int($post) ) |
|
2277 |
$post =& get_post($post); |
|
2278 |
elseif ( !is_object($post) ) |
|
2279 |
$post =& $GLOBALS['post']; |
|
2280 |
|
|
2281 |
$taxonomies = array(); |
|
2282 |
|
|
2283 |
if ( !$post ) |
|
2284 |
return $taxonomies; |
|
2285 |
|
|
2286 |
$template = apply_filters('taxonomy_template', '%s: %l.'); |
|
2287 |
|
|
2288 |
foreach ( get_object_taxonomies($post) as $taxonomy ) { |
|
2289 |
$t = (array) get_taxonomy($taxonomy); |
|
2290 |
if ( empty($t['label']) ) |
|
2291 |
$t['label'] = $taxonomy; |
|
2292 |
if ( empty($t['args']) ) |
|
2293 |
$t['args'] = array(); |
|
2294 |
if ( empty($t['template']) ) |
|
2295 |
$t['template'] = $template; |
|
2296 |
|
|
2297 |
$terms = get_object_term_cache($post->ID, $taxonomy); |
|
2298 |
if ( empty($terms) ) |
|
2299 |
$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); |
|
2300 |
|
|
2301 |
$links = array(); |
|
2302 |
|
|
2303 |
foreach ( $terms as $term ) |
|
2304 |
$links[] = "<a href='" . esc_attr(get_term_link($term, $taxonomy)) . "'>$term->name</a>"; |
|
2305 |
|
|
2306 |
if ( $links ) |
|
2307 |
$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms); |
|
2308 |
} |
|
2309 |
return $taxonomies; |
|
2310 |
} |
|
2311 |
|
|
2312 |
/** |
|
2313 |
* Retrieve all taxonomies of a post with just the names. |
|
2314 |
* |
|
2315 |
* @since 2.5.0 |
|
2316 |
* @uses get_object_taxonomies() |
|
2317 |
* |
|
2318 |
* @param int $post Optional. Post ID |
|
2319 |
* @return array |
|
2320 |
*/ |
|
2321 |
function get_post_taxonomies($post = 0) { |
|
2322 |
$post =& get_post($post); |
|
2323 |
|
|
2324 |
return get_object_taxonomies($post); |
|
2325 |
} |
|
2326 |
|
|
2327 |
/** |
|
2328 |
* Determine if the given object is associated with any of the given terms. |
|
2329 |
* |
|
2330 |
* The given terms are checked against the object's terms' term_ids, names and slugs. |
|
2331 |
* Terms given as integers will only be checked against the object's terms' term_ids. |
|
2332 |
* If no terms are given, determines if object is associated with any terms in the given taxonomy. |
|
2333 |
* |
|
2334 |
* @since 2.7.0 |
|
2335 |
* @uses get_object_term_cache() |
|
2336 |
* @uses wp_get_object_terms() |
|
2337 |
* |
|
2338 |
* @param int $object_id. ID of the object (post ID, link ID, ...) |
|
2339 |
* @param string $taxonomy. Single taxonomy name |
|
2340 |
* @param int|string|array $terms Optional. Term term_id, name, slug or array of said |
|
2341 |
* @return bool|WP_Error. WP_Error on input error. |
|
2342 |
*/ |
|
2343 |
function is_object_in_term( $object_id, $taxonomy, $terms = null ) { |
|
2344 |
if ( !$object_id = (int) $object_id ) |
|
2345 |
return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) ); |
|
2346 |
|
|
2347 |
$object_terms = get_object_term_cache( $object_id, $taxonomy ); |
|
2348 |
if ( empty( $object_terms ) ) |
|
2349 |
$object_terms = wp_get_object_terms( $object_id, $taxonomy ); |
|
2350 |
|
|
2351 |
if ( is_wp_error( $object_terms ) ) |
|
2352 |
return $object_terms; |
|
2353 |
if ( empty( $object_terms ) ) |
|
2354 |
return false; |
|
2355 |
if ( empty( $terms ) ) |
|
2356 |
return ( !empty( $object_terms ) ); |
|
2357 |
|
|
2358 |
$terms = (array) $terms; |
|
2359 |
|
|
2360 |
if ( $ints = array_filter( $terms, 'is_int' ) ) |
|
2361 |
$strs = array_diff( $terms, $ints ); |
|
2362 |
else |
|
2363 |
$strs =& $terms; |
|
2364 |
|
|
2365 |
foreach ( $object_terms as $object_term ) { |
|
2366 |
if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id |
|
2367 |
if ( $strs ) { |
|
2368 |
if ( in_array( $object_term->term_id, $strs ) ) return true; |
|
2369 |
if ( in_array( $object_term->name, $strs ) ) return true; |
|
2370 |
if ( in_array( $object_term->slug, $strs ) ) return true; |
|
2371 |
} |
|
2372 |
} |
|
2373 |
|
|
2374 |
return false; |
|
2375 |
} |
|
2376 |
|
|
2377 |
?> |