|
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. |
|
16 * |
|
17 * This function fires twice: in wp-settings.php before plugins are loaded (for |
|
18 * backwards compatibility reasons), and again on the 'init' action. We must avoid |
|
19 * registering rewrite rules before the 'init' action. |
|
20 */ |
|
21 function create_initial_taxonomies() { |
|
22 global $wp_rewrite; |
|
23 |
|
24 if ( ! did_action( 'init' ) ) { |
|
25 $rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false ); |
|
26 } else { |
|
27 $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' ); |
|
28 $rewrite = array( |
|
29 'category' => array( |
|
30 'hierarchical' => true, |
|
31 'slug' => get_option('category_base') ? get_option('category_base') : 'category', |
|
32 'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(), |
|
33 'ep_mask' => EP_CATEGORIES, |
|
34 ), |
|
35 'post_tag' => array( |
|
36 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag', |
|
37 'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(), |
|
38 'ep_mask' => EP_TAGS, |
|
39 ), |
|
40 'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false, |
|
41 ); |
|
42 } |
|
43 |
|
44 register_taxonomy( 'category', 'post', array( |
|
45 'hierarchical' => true, |
|
46 'query_var' => 'category_name', |
|
47 'rewrite' => $rewrite['category'], |
|
48 'public' => true, |
|
49 'show_ui' => true, |
|
50 'show_admin_column' => true, |
|
51 '_builtin' => true, |
|
52 ) ); |
|
53 |
|
54 register_taxonomy( 'post_tag', 'post', array( |
|
55 'hierarchical' => false, |
|
56 'query_var' => 'tag', |
|
57 'rewrite' => $rewrite['post_tag'], |
|
58 'public' => true, |
|
59 'show_ui' => true, |
|
60 'show_admin_column' => true, |
|
61 '_builtin' => true, |
|
62 ) ); |
|
63 |
|
64 register_taxonomy( 'nav_menu', 'nav_menu_item', array( |
|
65 'public' => false, |
|
66 'hierarchical' => false, |
|
67 'labels' => array( |
|
68 'name' => __( 'Navigation Menus' ), |
|
69 'singular_name' => __( 'Navigation Menu' ), |
|
70 ), |
|
71 'query_var' => false, |
|
72 'rewrite' => false, |
|
73 'show_ui' => false, |
|
74 '_builtin' => true, |
|
75 'show_in_nav_menus' => false, |
|
76 ) ); |
|
77 |
|
78 register_taxonomy( 'link_category', 'link', array( |
|
79 'hierarchical' => false, |
|
80 'labels' => array( |
|
81 'name' => __( 'Link Categories' ), |
|
82 'singular_name' => __( 'Link Category' ), |
|
83 'search_items' => __( 'Search Link Categories' ), |
|
84 'popular_items' => null, |
|
85 'all_items' => __( 'All Link Categories' ), |
|
86 'edit_item' => __( 'Edit Link Category' ), |
|
87 'update_item' => __( 'Update Link Category' ), |
|
88 'add_new_item' => __( 'Add New Link Category' ), |
|
89 'new_item_name' => __( 'New Link Category Name' ), |
|
90 'separate_items_with_commas' => null, |
|
91 'add_or_remove_items' => null, |
|
92 'choose_from_most_used' => null, |
|
93 ), |
|
94 'capabilities' => array( |
|
95 'manage_terms' => 'manage_links', |
|
96 'edit_terms' => 'manage_links', |
|
97 'delete_terms' => 'manage_links', |
|
98 'assign_terms' => 'manage_links', |
|
99 ), |
|
100 'query_var' => false, |
|
101 'rewrite' => false, |
|
102 'public' => false, |
|
103 'show_ui' => false, |
|
104 '_builtin' => true, |
|
105 ) ); |
|
106 |
|
107 register_taxonomy( 'post_format', 'post', array( |
|
108 'public' => true, |
|
109 'hierarchical' => false, |
|
110 'labels' => array( |
|
111 'name' => _x( 'Format', 'post format' ), |
|
112 'singular_name' => _x( 'Format', 'post format' ), |
|
113 ), |
|
114 'query_var' => true, |
|
115 'rewrite' => $rewrite['post_format'], |
|
116 'show_ui' => false, |
|
117 '_builtin' => true, |
|
118 'show_in_nav_menus' => current_theme_supports( 'post-formats' ), |
|
119 ) ); |
|
120 } |
|
121 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority |
|
122 |
|
123 /** |
|
124 * Get a list of registered taxonomy objects. |
|
125 * |
|
126 * @package WordPress |
|
127 * @subpackage Taxonomy |
|
128 * @since 3.0.0 |
|
129 * @uses $wp_taxonomies |
|
130 * @see register_taxonomy |
|
131 * |
|
132 * @param array $args An array of key => value arguments to match against the taxonomy objects. |
|
133 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default. |
|
134 * @param string $operator The logical operation to perform. 'or' means only one element |
|
135 * from the array needs to match; 'and' means all elements must match. The default is 'and'. |
|
136 * @return array A list of taxonomy names or objects |
|
137 */ |
|
138 function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) { |
|
139 global $wp_taxonomies; |
|
140 |
|
141 $field = ('names' == $output) ? 'name' : false; |
|
142 |
|
143 return wp_filter_object_list($wp_taxonomies, $args, $operator, $field); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Return all of the taxonomy names that are of $object_type. |
|
148 * |
|
149 * It appears that this function can be used to find all of the names inside of |
|
150 * $wp_taxonomies global variable. |
|
151 * |
|
152 * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should |
|
153 * result in <code>Array('category', 'post_tag')</code> |
|
154 * |
|
155 * @package WordPress |
|
156 * @subpackage Taxonomy |
|
157 * @since 2.3.0 |
|
158 * |
|
159 * @uses $wp_taxonomies |
|
160 * |
|
161 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts) |
|
162 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default. |
|
163 * @return array The names of all taxonomy of $object_type. |
|
164 */ |
|
165 function get_object_taxonomies($object, $output = 'names') { |
|
166 global $wp_taxonomies; |
|
167 |
|
168 if ( is_object($object) ) { |
|
169 if ( $object->post_type == 'attachment' ) |
|
170 return get_attachment_taxonomies($object); |
|
171 $object = $object->post_type; |
|
172 } |
|
173 |
|
174 $object = (array) $object; |
|
175 |
|
176 $taxonomies = array(); |
|
177 foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { |
|
178 if ( array_intersect($object, (array) $tax_obj->object_type) ) { |
|
179 if ( 'names' == $output ) |
|
180 $taxonomies[] = $tax_name; |
|
181 else |
|
182 $taxonomies[ $tax_name ] = $tax_obj; |
|
183 } |
|
184 } |
|
185 |
|
186 return $taxonomies; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Retrieves the taxonomy object of $taxonomy. |
|
191 * |
|
192 * The get_taxonomy function will first check that the parameter string given |
|
193 * is a taxonomy object and if it is, it will return it. |
|
194 * |
|
195 * @package WordPress |
|
196 * @subpackage Taxonomy |
|
197 * @since 2.3.0 |
|
198 * |
|
199 * @uses $wp_taxonomies |
|
200 * @uses taxonomy_exists() Checks whether taxonomy exists |
|
201 * |
|
202 * @param string $taxonomy Name of taxonomy object to return |
|
203 * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist |
|
204 */ |
|
205 function get_taxonomy( $taxonomy ) { |
|
206 global $wp_taxonomies; |
|
207 |
|
208 if ( ! taxonomy_exists( $taxonomy ) ) |
|
209 return false; |
|
210 |
|
211 return $wp_taxonomies[$taxonomy]; |
|
212 } |
|
213 |
|
214 /** |
|
215 * Checks that the taxonomy name exists. |
|
216 * |
|
217 * Formerly is_taxonomy(), introduced in 2.3.0. |
|
218 * |
|
219 * @package WordPress |
|
220 * @subpackage Taxonomy |
|
221 * @since 3.0.0 |
|
222 * |
|
223 * @uses $wp_taxonomies |
|
224 * |
|
225 * @param string $taxonomy Name of taxonomy object |
|
226 * @return bool Whether the taxonomy exists. |
|
227 */ |
|
228 function taxonomy_exists( $taxonomy ) { |
|
229 global $wp_taxonomies; |
|
230 |
|
231 return isset( $wp_taxonomies[$taxonomy] ); |
|
232 } |
|
233 |
|
234 /** |
|
235 * Whether the taxonomy object is hierarchical. |
|
236 * |
|
237 * Checks to make sure that the taxonomy is an object first. Then Gets the |
|
238 * object, and finally returns the hierarchical value in the object. |
|
239 * |
|
240 * A false return value might also mean that the taxonomy does not exist. |
|
241 * |
|
242 * @package WordPress |
|
243 * @subpackage Taxonomy |
|
244 * @since 2.3.0 |
|
245 * |
|
246 * @uses taxonomy_exists() Checks whether taxonomy exists |
|
247 * @uses get_taxonomy() Used to get the taxonomy object |
|
248 * |
|
249 * @param string $taxonomy Name of taxonomy object |
|
250 * @return bool Whether the taxonomy is hierarchical |
|
251 */ |
|
252 function is_taxonomy_hierarchical($taxonomy) { |
|
253 if ( ! taxonomy_exists($taxonomy) ) |
|
254 return false; |
|
255 |
|
256 $taxonomy = get_taxonomy($taxonomy); |
|
257 return $taxonomy->hierarchical; |
|
258 } |
|
259 |
|
260 /** |
|
261 * Create or modify a taxonomy object. Do not use before init. |
|
262 * |
|
263 * A simple function for creating or modifying a taxonomy object based on the |
|
264 * parameters given. The function will accept an array (third optional |
|
265 * parameter), along with strings for the taxonomy name and another string for |
|
266 * the object type. |
|
267 * |
|
268 * Nothing is returned, so expect error maybe or use taxonomy_exists() to check |
|
269 * whether taxonomy exists. |
|
270 * |
|
271 * Optional $args contents: |
|
272 * |
|
273 * - label - Name of the taxonomy shown in the menu. Usually plural. If not set, labels['name'] will be used. |
|
274 * - labels - An array of labels for this taxonomy. |
|
275 * * By default tag labels are used for non-hierarchical types and category labels for hierarchical ones. |
|
276 * * You can see accepted values in {@link get_taxonomy_labels()}. |
|
277 * - description - A short descriptive summary of what the taxonomy is for. Defaults to blank. |
|
278 * - public - If the taxonomy should be publicly queryable; //@TODO not implemented. |
|
279 * * Defaults to true. |
|
280 * - hierarchical - Whether the taxonomy is hierarchical (e.g. category). Defaults to false. |
|
281 * - show_ui -Whether to generate a default UI for managing this taxonomy in the admin. |
|
282 * * If not set, the default is inherited from public. |
|
283 * - show_in_menu - Where to show the taxonomy in the admin menu. |
|
284 * * If true, the taxonomy is shown as a submenu of the object type menu. |
|
285 * * If false, no menu is shown. |
|
286 * * show_ui must be true. |
|
287 * * If not set, the default is inherited from show_ui. |
|
288 * - show_in_nav_menus - Makes this taxonomy available for selection in navigation menus. |
|
289 * * If not set, the default is inherited from public. |
|
290 * - show_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget. |
|
291 * * If not set, the default is inherited from show_ui. |
|
292 * - meta_box_cb - Provide a callback function for the meta box display. Defaults to |
|
293 * post_categories_meta_box for hierarchical taxonomies and post_tags_meta_box for non-hierarchical. |
|
294 * - capabilities - Array of capabilities for this taxonomy. |
|
295 * * You can see accepted values in this function. |
|
296 * - rewrite - Triggers the handling of rewrites for this taxonomy. Defaults to true, using $taxonomy as slug. |
|
297 * * To prevent rewrite, set to false. |
|
298 * * To specify rewrite rules, an array can be passed with any of these keys |
|
299 * * 'slug' => string Customize the permastruct slug. Defaults to $taxonomy key |
|
300 * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true. |
|
301 * * 'hierarchical' => bool Either hierarchical rewrite tag or not. Defaults to false. |
|
302 * * 'ep_mask' => const Assign an endpoint mask. |
|
303 * * If not specified, defaults to EP_NONE. |
|
304 * - query_var - Sets the query_var key for this taxonomy. Defaults to $taxonomy key |
|
305 * * If false, a taxonomy cannot be loaded at ?{query_var}={term_slug} |
|
306 * * If specified as a string, the query ?{query_var_string}={term_slug} will be valid. |
|
307 * - update_count_callback - Works much like a hook, in that it will be called when the count is updated. |
|
308 * * Defaults to _update_post_term_count() for taxonomies attached to post types, which then confirms |
|
309 * that the objects are published before counting them. |
|
310 * * Defaults to _update_generic_term_count() for taxonomies attached to other object types, such as links. |
|
311 * - _builtin - true if this taxonomy is a native or "built-in" taxonomy. THIS IS FOR INTERNAL USE ONLY! |
|
312 * |
|
313 * @since 2.3.0 |
|
314 * @uses $wp_taxonomies Inserts new taxonomy object into the list |
|
315 * @uses $wp Adds query vars |
|
316 * |
|
317 * @param string $taxonomy Taxonomy key, must not exceed 32 characters. |
|
318 * @param array|string $object_type Name of the object type for the taxonomy object. |
|
319 * @param array|string $args See optional args description above. |
|
320 * @return null|WP_Error WP_Error if errors, otherwise null. |
|
321 */ |
|
322 function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
|
323 global $wp_taxonomies, $wp; |
|
324 |
|
325 if ( ! is_array( $wp_taxonomies ) ) |
|
326 $wp_taxonomies = array(); |
|
327 |
|
328 $defaults = array( |
|
329 'labels' => array(), |
|
330 'description' => '', |
|
331 'public' => true, |
|
332 'hierarchical' => false, |
|
333 'show_ui' => null, |
|
334 'show_in_menu' => null, |
|
335 'show_in_nav_menus' => null, |
|
336 'show_tagcloud' => null, |
|
337 'meta_box_cb' => null, |
|
338 'capabilities' => array(), |
|
339 'rewrite' => true, |
|
340 'query_var' => $taxonomy, |
|
341 'update_count_callback' => '', |
|
342 '_builtin' => false, |
|
343 ); |
|
344 $args = wp_parse_args( $args, $defaults ); |
|
345 |
|
346 if ( strlen( $taxonomy ) > 32 ) |
|
347 return new WP_Error( 'taxonomy_too_long', __( 'Taxonomies cannot exceed 32 characters in length' ) ); |
|
348 |
|
349 if ( false !== $args['query_var'] && ! empty( $wp ) ) { |
|
350 if ( true === $args['query_var'] ) |
|
351 $args['query_var'] = $taxonomy; |
|
352 else |
|
353 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); |
|
354 $wp->add_query_var( $args['query_var'] ); |
|
355 } |
|
356 |
|
357 if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { |
|
358 $args['rewrite'] = wp_parse_args( $args['rewrite'], array( |
|
359 'with_front' => true, |
|
360 'hierarchical' => false, |
|
361 'ep_mask' => EP_NONE, |
|
362 ) ); |
|
363 |
|
364 if ( empty( $args['rewrite']['slug'] ) ) |
|
365 $args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy ); |
|
366 |
|
367 if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] ) |
|
368 $tag = '(.+?)'; |
|
369 else |
|
370 $tag = '([^/]+)'; |
|
371 |
|
372 add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" ); |
|
373 add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] ); |
|
374 } |
|
375 |
|
376 // If not set, default to the setting for public. |
|
377 if ( null === $args['show_ui'] ) |
|
378 $args['show_ui'] = $args['public']; |
|
379 |
|
380 // If not set, default to the setting for show_ui. |
|
381 if ( null === $args['show_in_menu' ] || ! $args['show_ui'] ) |
|
382 $args['show_in_menu' ] = $args['show_ui']; |
|
383 |
|
384 // If not set, default to the setting for public. |
|
385 if ( null === $args['show_in_nav_menus'] ) |
|
386 $args['show_in_nav_menus'] = $args['public']; |
|
387 |
|
388 // If not set, default to the setting for show_ui. |
|
389 if ( null === $args['show_tagcloud'] ) |
|
390 $args['show_tagcloud'] = $args['show_ui']; |
|
391 |
|
392 $default_caps = array( |
|
393 'manage_terms' => 'manage_categories', |
|
394 'edit_terms' => 'manage_categories', |
|
395 'delete_terms' => 'manage_categories', |
|
396 'assign_terms' => 'edit_posts', |
|
397 ); |
|
398 $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] ); |
|
399 unset( $args['capabilities'] ); |
|
400 |
|
401 $args['name'] = $taxonomy; |
|
402 $args['object_type'] = array_unique( (array) $object_type ); |
|
403 |
|
404 $args['labels'] = get_taxonomy_labels( (object) $args ); |
|
405 $args['label'] = $args['labels']->name; |
|
406 |
|
407 // If not set, use the default meta box |
|
408 if ( null === $args['meta_box_cb'] ) { |
|
409 if ( $args['hierarchical'] ) |
|
410 $args['meta_box_cb'] = 'post_categories_meta_box'; |
|
411 else |
|
412 $args['meta_box_cb'] = 'post_tags_meta_box'; |
|
413 } |
|
414 |
|
415 $wp_taxonomies[ $taxonomy ] = (object) $args; |
|
416 |
|
417 // register callback handling for metabox |
|
418 add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' ); |
|
419 |
|
420 do_action( 'registered_taxonomy', $taxonomy, $object_type, $args ); |
|
421 } |
|
422 |
|
423 /** |
|
424 * Builds an object with all taxonomy labels out of a taxonomy object |
|
425 * |
|
426 * Accepted keys of the label array in the taxonomy object: |
|
427 * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories |
|
428 * - singular_name - name for one object of this taxonomy. Default is Tag/Category |
|
429 * - search_items - Default is Search Tags/Search Categories |
|
430 * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags |
|
431 * - all_items - Default is All Tags/All Categories |
|
432 * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category |
|
433 * - parent_item_colon - The same as <code>parent_item</code>, but with colon <code>:</code> in the end |
|
434 * - edit_item - Default is Edit Tag/Edit Category |
|
435 * - view_item - Default is View Tag/View Category |
|
436 * - update_item - Default is Update Tag/Update Category |
|
437 * - add_new_item - Default is Add New Tag/Add New Category |
|
438 * - new_item_name - Default is New Tag Name/New Category Name |
|
439 * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box. |
|
440 * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags", used in the meta box when JavaScript is disabled. |
|
441 * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags", used in the meta box. |
|
442 * - not_found - This string isn't used on hierarchical taxonomies. Default is "No tags found", used in the meta box. |
|
443 * |
|
444 * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories). |
|
445 * |
|
446 * @since 3.0.0 |
|
447 * @param object $tax Taxonomy object |
|
448 * @return object object with all the labels as member variables |
|
449 */ |
|
450 |
|
451 function get_taxonomy_labels( $tax ) { |
|
452 $tax->labels = (array) $tax->labels; |
|
453 |
|
454 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) |
|
455 $tax->labels['separate_items_with_commas'] = $tax->helps; |
|
456 |
|
457 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) |
|
458 $tax->labels['not_found'] = $tax->no_tagcloud; |
|
459 |
|
460 $nohier_vs_hier_defaults = array( |
|
461 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ), |
|
462 'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ), |
|
463 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ), |
|
464 'popular_items' => array( __( 'Popular Tags' ), null ), |
|
465 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ), |
|
466 'parent_item' => array( null, __( 'Parent Category' ) ), |
|
467 'parent_item_colon' => array( null, __( 'Parent Category:' ) ), |
|
468 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ), |
|
469 'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ), |
|
470 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ), |
|
471 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ), |
|
472 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ), |
|
473 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ), |
|
474 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ), |
|
475 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ), |
|
476 'not_found' => array( __( 'No tags found.' ), null ), |
|
477 ); |
|
478 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
|
479 |
|
480 return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults ); |
|
481 } |
|
482 |
|
483 /** |
|
484 * Add an already registered taxonomy to an object type. |
|
485 * |
|
486 * @package WordPress |
|
487 * @subpackage Taxonomy |
|
488 * @since 3.0.0 |
|
489 * @uses $wp_taxonomies Modifies taxonomy object |
|
490 * |
|
491 * @param string $taxonomy Name of taxonomy object |
|
492 * @param string $object_type Name of the object type |
|
493 * @return bool True if successful, false if not |
|
494 */ |
|
495 function register_taxonomy_for_object_type( $taxonomy, $object_type) { |
|
496 global $wp_taxonomies; |
|
497 |
|
498 if ( !isset($wp_taxonomies[$taxonomy]) ) |
|
499 return false; |
|
500 |
|
501 if ( ! get_post_type_object($object_type) ) |
|
502 return false; |
|
503 |
|
504 if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) ) |
|
505 $wp_taxonomies[$taxonomy]->object_type[] = $object_type; |
|
506 |
|
507 return true; |
|
508 } |
|
509 |
|
510 /** |
|
511 * Remove an already registered taxonomy from an object type. |
|
512 * |
|
513 * @since 3.7.0 |
|
514 * |
|
515 * @param string $taxonomy Name of taxonomy object. |
|
516 * @param string $object_type Name of the object type. |
|
517 * @return bool True if successful, false if not. |
|
518 */ |
|
519 function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) { |
|
520 global $wp_taxonomies; |
|
521 |
|
522 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) |
|
523 return false; |
|
524 |
|
525 if ( ! get_post_type_object( $object_type ) ) |
|
526 return false; |
|
527 |
|
528 $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); |
|
529 if ( false === $key ) |
|
530 return false; |
|
531 |
|
532 unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] ); |
|
533 return true; |
|
534 } |
|
535 |
|
536 // |
|
537 // Term API |
|
538 // |
|
539 |
|
540 /** |
|
541 * Retrieve object_ids of valid taxonomy and term. |
|
542 * |
|
543 * The strings of $taxonomies must exist before this function will continue. On |
|
544 * failure of finding a valid taxonomy, it will return an WP_Error class, kind |
|
545 * of like Exceptions in PHP 5, except you can't catch them. Even so, you can |
|
546 * still test for the WP_Error class and get the error message. |
|
547 * |
|
548 * The $terms aren't checked the same as $taxonomies, but still need to exist |
|
549 * for $object_ids to be returned. |
|
550 * |
|
551 * It is possible to change the order that object_ids is returned by either |
|
552 * using PHP sort family functions or using the database by using $args with |
|
553 * either ASC or DESC array. The value should be in the key named 'order'. |
|
554 * |
|
555 * @package WordPress |
|
556 * @subpackage Taxonomy |
|
557 * @since 2.3.0 |
|
558 * |
|
559 * @uses $wpdb |
|
560 * @uses wp_parse_args() Creates an array from string $args. |
|
561 * |
|
562 * @param int|array $term_ids Term id or array of term ids of terms that will be used |
|
563 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names |
|
564 * @param array|string $args Change the order of the object_ids, either ASC or DESC |
|
565 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success |
|
566 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. |
|
567 */ |
|
568 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { |
|
569 global $wpdb; |
|
570 |
|
571 if ( ! is_array( $term_ids ) ) |
|
572 $term_ids = array( $term_ids ); |
|
573 |
|
574 if ( ! is_array( $taxonomies ) ) |
|
575 $taxonomies = array( $taxonomies ); |
|
576 |
|
577 foreach ( (array) $taxonomies as $taxonomy ) { |
|
578 if ( ! taxonomy_exists( $taxonomy ) ) |
|
579 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); |
|
580 } |
|
581 |
|
582 $defaults = array( 'order' => 'ASC' ); |
|
583 $args = wp_parse_args( $args, $defaults ); |
|
584 extract( $args, EXTR_SKIP ); |
|
585 |
|
586 $order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC'; |
|
587 |
|
588 $term_ids = array_map('intval', $term_ids ); |
|
589 |
|
590 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; |
|
591 $term_ids = "'" . implode( "', '", $term_ids ) . "'"; |
|
592 |
|
593 $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 ($term_ids) ORDER BY tr.object_id $order"); |
|
594 |
|
595 if ( ! $object_ids ) |
|
596 return array(); |
|
597 |
|
598 return $object_ids; |
|
599 } |
|
600 |
|
601 /** |
|
602 * Given a taxonomy query, generates SQL to be appended to a main query. |
|
603 * |
|
604 * @since 3.1.0 |
|
605 * |
|
606 * @see WP_Tax_Query |
|
607 * |
|
608 * @param array $tax_query A compact tax query |
|
609 * @param string $primary_table |
|
610 * @param string $primary_id_column |
|
611 * @return array |
|
612 */ |
|
613 function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { |
|
614 $tax_query_obj = new WP_Tax_Query( $tax_query ); |
|
615 return $tax_query_obj->get_sql( $primary_table, $primary_id_column ); |
|
616 } |
|
617 |
|
618 /** |
|
619 * Container class for a multiple taxonomy query. |
|
620 * |
|
621 * @since 3.1.0 |
|
622 */ |
|
623 class WP_Tax_Query { |
|
624 |
|
625 /** |
|
626 * List of taxonomy queries. A single taxonomy query is an associative array: |
|
627 * - 'taxonomy' string The taxonomy being queried |
|
628 * - 'terms' string|array The list of terms |
|
629 * - 'field' string (optional) Which term field is being used. |
|
630 * Possible values: 'term_id', 'slug' or 'name' |
|
631 * Default: 'term_id' |
|
632 * - 'operator' string (optional) |
|
633 * Possible values: 'AND', 'IN' or 'NOT IN'. |
|
634 * Default: 'IN' |
|
635 * - 'include_children' bool (optional) Whether to include child terms. |
|
636 * Default: true |
|
637 * |
|
638 * @since 3.1.0 |
|
639 * @access public |
|
640 * @var array |
|
641 */ |
|
642 public $queries = array(); |
|
643 |
|
644 /** |
|
645 * The relation between the queries. Can be one of 'AND' or 'OR'. |
|
646 * |
|
647 * @since 3.1.0 |
|
648 * @access public |
|
649 * @var string |
|
650 */ |
|
651 public $relation; |
|
652 |
|
653 /** |
|
654 * Standard response when the query should not return any rows. |
|
655 * |
|
656 * @since 3.2.0 |
|
657 * @access private |
|
658 * @var string |
|
659 */ |
|
660 private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' ); |
|
661 |
|
662 /** |
|
663 * Constructor. |
|
664 * |
|
665 * Parses a compact tax query and sets defaults. |
|
666 * |
|
667 * @since 3.1.0 |
|
668 * @access public |
|
669 * |
|
670 * @param array $tax_query A compact tax query: |
|
671 * array( |
|
672 * 'relation' => 'OR', |
|
673 * array( |
|
674 * 'taxonomy' => 'tax1', |
|
675 * 'terms' => array( 'term1', 'term2' ), |
|
676 * 'field' => 'slug', |
|
677 * ), |
|
678 * array( |
|
679 * 'taxonomy' => 'tax2', |
|
680 * 'terms' => array( 'term-a', 'term-b' ), |
|
681 * 'field' => 'slug', |
|
682 * ), |
|
683 * ) |
|
684 */ |
|
685 public function __construct( $tax_query ) { |
|
686 if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) { |
|
687 $this->relation = 'OR'; |
|
688 } else { |
|
689 $this->relation = 'AND'; |
|
690 } |
|
691 |
|
692 $defaults = array( |
|
693 'taxonomy' => '', |
|
694 'terms' => array(), |
|
695 'include_children' => true, |
|
696 'field' => 'term_id', |
|
697 'operator' => 'IN', |
|
698 ); |
|
699 |
|
700 foreach ( $tax_query as $query ) { |
|
701 if ( ! is_array( $query ) ) |
|
702 continue; |
|
703 |
|
704 $query = array_merge( $defaults, $query ); |
|
705 |
|
706 $query['terms'] = (array) $query['terms']; |
|
707 |
|
708 $this->queries[] = $query; |
|
709 } |
|
710 } |
|
711 |
|
712 /** |
|
713 * Generates SQL clauses to be appended to a main query. |
|
714 * |
|
715 * @since 3.1.0 |
|
716 * @access public |
|
717 * |
|
718 * @param string $primary_table |
|
719 * @param string $primary_id_column |
|
720 * @return array |
|
721 */ |
|
722 public function get_sql( $primary_table, $primary_id_column ) { |
|
723 global $wpdb; |
|
724 |
|
725 $join = ''; |
|
726 $where = array(); |
|
727 $i = 0; |
|
728 $count = count( $this->queries ); |
|
729 |
|
730 foreach ( $this->queries as $index => $query ) { |
|
731 $this->clean_query( $query ); |
|
732 |
|
733 if ( is_wp_error( $query ) ) |
|
734 return self::$no_results; |
|
735 |
|
736 extract( $query ); |
|
737 |
|
738 if ( 'IN' == $operator ) { |
|
739 |
|
740 if ( empty( $terms ) ) { |
|
741 if ( 'OR' == $this->relation ) { |
|
742 if ( ( $index + 1 === $count ) && empty( $where ) ) |
|
743 return self::$no_results; |
|
744 continue; |
|
745 } else { |
|
746 return self::$no_results; |
|
747 } |
|
748 } |
|
749 |
|
750 $terms = implode( ',', $terms ); |
|
751 |
|
752 $alias = $i ? 'tt' . $i : $wpdb->term_relationships; |
|
753 |
|
754 $join .= " INNER JOIN $wpdb->term_relationships"; |
|
755 $join .= $i ? " AS $alias" : ''; |
|
756 $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)"; |
|
757 |
|
758 $where[] = "$alias.term_taxonomy_id $operator ($terms)"; |
|
759 } elseif ( 'NOT IN' == $operator ) { |
|
760 |
|
761 if ( empty( $terms ) ) |
|
762 continue; |
|
763 |
|
764 $terms = implode( ',', $terms ); |
|
765 |
|
766 $where[] = "$primary_table.$primary_id_column NOT IN ( |
|
767 SELECT object_id |
|
768 FROM $wpdb->term_relationships |
|
769 WHERE term_taxonomy_id IN ($terms) |
|
770 )"; |
|
771 } elseif ( 'AND' == $operator ) { |
|
772 |
|
773 if ( empty( $terms ) ) |
|
774 continue; |
|
775 |
|
776 $num_terms = count( $terms ); |
|
777 |
|
778 $terms = implode( ',', $terms ); |
|
779 |
|
780 $where[] = "( |
|
781 SELECT COUNT(1) |
|
782 FROM $wpdb->term_relationships |
|
783 WHERE term_taxonomy_id IN ($terms) |
|
784 AND object_id = $primary_table.$primary_id_column |
|
785 ) = $num_terms"; |
|
786 } |
|
787 |
|
788 $i++; |
|
789 } |
|
790 |
|
791 if ( ! empty( $where ) ) |
|
792 $where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )'; |
|
793 else |
|
794 $where = ''; |
|
795 |
|
796 return compact( 'join', 'where' ); |
|
797 } |
|
798 |
|
799 /** |
|
800 * Validates a single query. |
|
801 * |
|
802 * @since 3.2.0 |
|
803 * @access private |
|
804 * |
|
805 * @param array &$query The single query |
|
806 */ |
|
807 private function clean_query( &$query ) { |
|
808 if ( ! taxonomy_exists( $query['taxonomy'] ) ) { |
|
809 $query = new WP_Error( 'Invalid taxonomy' ); |
|
810 return; |
|
811 } |
|
812 |
|
813 $query['terms'] = array_unique( (array) $query['terms'] ); |
|
814 |
|
815 if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) { |
|
816 $this->transform_query( $query, 'term_id' ); |
|
817 |
|
818 if ( is_wp_error( $query ) ) |
|
819 return; |
|
820 |
|
821 $children = array(); |
|
822 foreach ( $query['terms'] as $term ) { |
|
823 $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) ); |
|
824 $children[] = $term; |
|
825 } |
|
826 $query['terms'] = $children; |
|
827 } |
|
828 |
|
829 $this->transform_query( $query, 'term_taxonomy_id' ); |
|
830 } |
|
831 |
|
832 /** |
|
833 * Transforms a single query, from one field to another. |
|
834 * |
|
835 * @since 3.2.0 |
|
836 * |
|
837 * @param array &$query The single query |
|
838 * @param string $resulting_field The resulting field |
|
839 */ |
|
840 public function transform_query( &$query, $resulting_field ) { |
|
841 global $wpdb; |
|
842 |
|
843 if ( empty( $query['terms'] ) ) |
|
844 return; |
|
845 |
|
846 if ( $query['field'] == $resulting_field ) |
|
847 return; |
|
848 |
|
849 $resulting_field = sanitize_key( $resulting_field ); |
|
850 |
|
851 switch ( $query['field'] ) { |
|
852 case 'slug': |
|
853 case 'name': |
|
854 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'"; |
|
855 $terms = $wpdb->get_col( " |
|
856 SELECT $wpdb->term_taxonomy.$resulting_field |
|
857 FROM $wpdb->term_taxonomy |
|
858 INNER JOIN $wpdb->terms USING (term_id) |
|
859 WHERE taxonomy = '{$query['taxonomy']}' |
|
860 AND $wpdb->terms.{$query['field']} IN ($terms) |
|
861 " ); |
|
862 break; |
|
863 case 'term_taxonomy_id': |
|
864 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); |
|
865 $terms = $wpdb->get_col( " |
|
866 SELECT $resulting_field |
|
867 FROM $wpdb->term_taxonomy |
|
868 WHERE term_taxonomy_id IN ($terms) |
|
869 " ); |
|
870 break; |
|
871 default: |
|
872 $terms = implode( ',', array_map( 'intval', $query['terms'] ) ); |
|
873 $terms = $wpdb->get_col( " |
|
874 SELECT $resulting_field |
|
875 FROM $wpdb->term_taxonomy |
|
876 WHERE taxonomy = '{$query['taxonomy']}' |
|
877 AND term_id IN ($terms) |
|
878 " ); |
|
879 } |
|
880 |
|
881 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) { |
|
882 $query = new WP_Error( 'Inexistent terms' ); |
|
883 return; |
|
884 } |
|
885 |
|
886 $query['terms'] = $terms; |
|
887 $query['field'] = $resulting_field; |
|
888 } |
|
889 } |
|
890 |
|
891 /** |
|
892 * Get all Term data from database by Term ID. |
|
893 * |
|
894 * The usage of the get_term function is to apply filters to a term object. It |
|
895 * is possible to get a term object from the database before applying the |
|
896 * filters. |
|
897 * |
|
898 * $term ID must be part of $taxonomy, to get from the database. Failure, might |
|
899 * be able to be captured by the hooks. Failure would be the same value as $wpdb |
|
900 * returns for the get_row method. |
|
901 * |
|
902 * There are two hooks, one is specifically for each term, named 'get_term', and |
|
903 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the |
|
904 * term object, and the taxonomy name as parameters. Both hooks are expected to |
|
905 * return a Term object. |
|
906 * |
|
907 * 'get_term' hook - Takes two parameters the term Object and the taxonomy name. |
|
908 * Must return term object. Used in get_term() as a catch-all filter for every |
|
909 * $term. |
|
910 * |
|
911 * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy |
|
912 * name. Must return term object. $taxonomy will be the taxonomy name, so for |
|
913 * example, if 'category', it would be 'get_category' as the filter name. Useful |
|
914 * for custom taxonomies or plugging into default taxonomies. |
|
915 * |
|
916 * @package WordPress |
|
917 * @subpackage Taxonomy |
|
918 * @since 2.3.0 |
|
919 * |
|
920 * @uses $wpdb |
|
921 * @uses sanitize_term() Cleanses the term based on $filter context before returning. |
|
922 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
|
923 * |
|
924 * @param int|object $term If integer, will get from database. If object will apply filters and return $term. |
|
925 * @param string $taxonomy Taxonomy name that $term is part of. |
|
926 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N |
|
927 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. |
|
928 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not |
|
929 * exist then WP_Error will be returned. |
|
930 */ |
|
931 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { |
|
932 global $wpdb; |
|
933 $null = null; |
|
934 |
|
935 if ( empty($term) ) { |
|
936 $error = new WP_Error('invalid_term', __('Empty Term')); |
|
937 return $error; |
|
938 } |
|
939 |
|
940 if ( ! taxonomy_exists($taxonomy) ) { |
|
941 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
942 return $error; |
|
943 } |
|
944 |
|
945 if ( is_object($term) && empty($term->filter) ) { |
|
946 wp_cache_add($term->term_id, $term, $taxonomy); |
|
947 $_term = $term; |
|
948 } else { |
|
949 if ( is_object($term) ) |
|
950 $term = $term->term_id; |
|
951 if ( !$term = (int) $term ) |
|
952 return $null; |
|
953 if ( ! $_term = wp_cache_get($term, $taxonomy) ) { |
|
954 $_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 = %d LIMIT 1", $taxonomy, $term) ); |
|
955 if ( ! $_term ) |
|
956 return $null; |
|
957 wp_cache_add($term, $_term, $taxonomy); |
|
958 } |
|
959 } |
|
960 |
|
961 $_term = apply_filters('get_term', $_term, $taxonomy); |
|
962 $_term = apply_filters("get_$taxonomy", $_term, $taxonomy); |
|
963 $_term = sanitize_term($_term, $taxonomy, $filter); |
|
964 |
|
965 if ( $output == OBJECT ) { |
|
966 return $_term; |
|
967 } elseif ( $output == ARRAY_A ) { |
|
968 $__term = get_object_vars($_term); |
|
969 return $__term; |
|
970 } elseif ( $output == ARRAY_N ) { |
|
971 $__term = array_values(get_object_vars($_term)); |
|
972 return $__term; |
|
973 } else { |
|
974 return $_term; |
|
975 } |
|
976 } |
|
977 |
|
978 /** |
|
979 * Get all Term data from database by Term field and data. |
|
980 * |
|
981 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if |
|
982 * required. |
|
983 * |
|
984 * The default $field is 'id', therefore it is possible to also use null for |
|
985 * field, but not recommended that you do so. |
|
986 * |
|
987 * If $value does not exist, the return value will be false. If $taxonomy exists |
|
988 * and $field and $value combinations exist, the Term will be returned. |
|
989 * |
|
990 * @package WordPress |
|
991 * @subpackage Taxonomy |
|
992 * @since 2.3.0 |
|
993 * |
|
994 * @uses $wpdb |
|
995 * @uses sanitize_term() Cleanses the term based on $filter context before returning. |
|
996 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
|
997 * |
|
998 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' |
|
999 * @param string|int $value Search for this term value |
|
1000 * @param string $taxonomy Taxonomy Name |
|
1001 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N |
|
1002 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. |
|
1003 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. |
|
1004 */ |
|
1005 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { |
|
1006 global $wpdb; |
|
1007 |
|
1008 if ( ! taxonomy_exists($taxonomy) ) |
|
1009 return false; |
|
1010 |
|
1011 if ( 'slug' == $field ) { |
|
1012 $field = 't.slug'; |
|
1013 $value = sanitize_title($value); |
|
1014 if ( empty($value) ) |
|
1015 return false; |
|
1016 } else if ( 'name' == $field ) { |
|
1017 // Assume already escaped |
|
1018 $value = wp_unslash($value); |
|
1019 $field = 't.name'; |
|
1020 } else if ( 'term_taxonomy_id' == $field ) { |
|
1021 $value = (int) $value; |
|
1022 $field = 'tt.term_taxonomy_id'; |
|
1023 } else { |
|
1024 $term = get_term( (int) $value, $taxonomy, $output, $filter); |
|
1025 if ( is_wp_error( $term ) ) |
|
1026 $term = false; |
|
1027 return $term; |
|
1028 } |
|
1029 |
|
1030 $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) ); |
|
1031 if ( !$term ) |
|
1032 return false; |
|
1033 |
|
1034 wp_cache_add($term->term_id, $term, $taxonomy); |
|
1035 |
|
1036 $term = apply_filters('get_term', $term, $taxonomy); |
|
1037 $term = apply_filters("get_$taxonomy", $term, $taxonomy); |
|
1038 $term = sanitize_term($term, $taxonomy, $filter); |
|
1039 |
|
1040 if ( $output == OBJECT ) { |
|
1041 return $term; |
|
1042 } elseif ( $output == ARRAY_A ) { |
|
1043 return get_object_vars($term); |
|
1044 } elseif ( $output == ARRAY_N ) { |
|
1045 return array_values(get_object_vars($term)); |
|
1046 } else { |
|
1047 return $term; |
|
1048 } |
|
1049 } |
|
1050 |
|
1051 /** |
|
1052 * Merge all term children into a single array of their IDs. |
|
1053 * |
|
1054 * This recursive function will merge all of the children of $term into the same |
|
1055 * array of term IDs. Only useful for taxonomies which are hierarchical. |
|
1056 * |
|
1057 * Will return an empty array if $term does not exist in $taxonomy. |
|
1058 * |
|
1059 * @package WordPress |
|
1060 * @subpackage Taxonomy |
|
1061 * @since 2.3.0 |
|
1062 * |
|
1063 * @uses $wpdb |
|
1064 * @uses _get_term_hierarchy() |
|
1065 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term |
|
1066 * |
|
1067 * @param string $term_id ID of Term to get children |
|
1068 * @param string $taxonomy Taxonomy Name |
|
1069 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist |
|
1070 */ |
|
1071 function get_term_children( $term_id, $taxonomy ) { |
|
1072 if ( ! taxonomy_exists($taxonomy) ) |
|
1073 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
1074 |
|
1075 $term_id = intval( $term_id ); |
|
1076 |
|
1077 $terms = _get_term_hierarchy($taxonomy); |
|
1078 |
|
1079 if ( ! isset($terms[$term_id]) ) |
|
1080 return array(); |
|
1081 |
|
1082 $children = $terms[$term_id]; |
|
1083 |
|
1084 foreach ( (array) $terms[$term_id] as $child ) { |
|
1085 if ( isset($terms[$child]) ) |
|
1086 $children = array_merge($children, get_term_children($child, $taxonomy)); |
|
1087 } |
|
1088 |
|
1089 return $children; |
|
1090 } |
|
1091 |
|
1092 /** |
|
1093 * Get sanitized Term field. |
|
1094 * |
|
1095 * Does checks for $term, based on the $taxonomy. The function is for contextual |
|
1096 * reasons and for simplicity of usage. See sanitize_term_field() for more |
|
1097 * information. |
|
1098 * |
|
1099 * @package WordPress |
|
1100 * @subpackage Taxonomy |
|
1101 * @since 2.3.0 |
|
1102 * |
|
1103 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success. |
|
1104 * |
|
1105 * @param string $field Term field to fetch |
|
1106 * @param int $term Term ID |
|
1107 * @param string $taxonomy Taxonomy Name |
|
1108 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options. |
|
1109 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term. |
|
1110 */ |
|
1111 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { |
|
1112 $term = (int) $term; |
|
1113 $term = get_term( $term, $taxonomy ); |
|
1114 if ( is_wp_error($term) ) |
|
1115 return $term; |
|
1116 |
|
1117 if ( !is_object($term) ) |
|
1118 return ''; |
|
1119 |
|
1120 if ( !isset($term->$field) ) |
|
1121 return ''; |
|
1122 |
|
1123 return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); |
|
1124 } |
|
1125 |
|
1126 /** |
|
1127 * Sanitizes Term for editing. |
|
1128 * |
|
1129 * Return value is sanitize_term() and usage is for sanitizing the term for |
|
1130 * editing. Function is for contextual and simplicity. |
|
1131 * |
|
1132 * @package WordPress |
|
1133 * @subpackage Taxonomy |
|
1134 * @since 2.3.0 |
|
1135 * |
|
1136 * @uses sanitize_term() Passes the return value on success |
|
1137 * |
|
1138 * @param int|object $id Term ID or Object |
|
1139 * @param string $taxonomy Taxonomy Name |
|
1140 * @return mixed|null|WP_Error Will return empty string if $term is not an object. |
|
1141 */ |
|
1142 function get_term_to_edit( $id, $taxonomy ) { |
|
1143 $term = get_term( $id, $taxonomy ); |
|
1144 |
|
1145 if ( is_wp_error($term) ) |
|
1146 return $term; |
|
1147 |
|
1148 if ( !is_object($term) ) |
|
1149 return ''; |
|
1150 |
|
1151 return sanitize_term($term, $taxonomy, 'edit'); |
|
1152 } |
|
1153 |
|
1154 /** |
|
1155 * Retrieve the terms in a given taxonomy or list of taxonomies. |
|
1156 * |
|
1157 * You can fully inject any customizations to the query before it is sent, as |
|
1158 * well as control the output with a filter. |
|
1159 * |
|
1160 * The 'get_terms' filter will be called when the cache has the term and will |
|
1161 * pass the found term along with the array of $taxonomies and array of $args. |
|
1162 * This filter is also called before the array of terms is passed and will pass |
|
1163 * the array of terms, along with the $taxonomies and $args. |
|
1164 * |
|
1165 * The 'list_terms_exclusions' filter passes the compiled exclusions along with |
|
1166 * the $args. |
|
1167 * |
|
1168 * The 'get_terms_orderby' filter passes the ORDER BY clause for the query |
|
1169 * along with the $args array. |
|
1170 * |
|
1171 * The 'get_terms_fields' filter passes the fields for the SELECT query |
|
1172 * along with the $args array. |
|
1173 * |
|
1174 * The list of arguments that $args can contain, which will overwrite the defaults: |
|
1175 * |
|
1176 * orderby - Default is 'name'. Can be name, count, term_group, slug or nothing |
|
1177 * (will use term_id), Passing a custom value other than these will cause it to |
|
1178 * order based on the custom value. |
|
1179 * |
|
1180 * order - Default is ASC. Can use DESC. |
|
1181 * |
|
1182 * hide_empty - Default is true. Will not return empty terms, which means |
|
1183 * terms whose count is 0 according to the given taxonomy. |
|
1184 * |
|
1185 * exclude - Default is an empty array. An array, comma- or space-delimited string |
|
1186 * of term ids to exclude from the return array. If 'include' is non-empty, |
|
1187 * 'exclude' is ignored. |
|
1188 * |
|
1189 * exclude_tree - Default is an empty array. An array, comma- or space-delimited |
|
1190 * string of term ids to exclude from the return array, along with all of their |
|
1191 * descendant terms according to the primary taxonomy. If 'include' is non-empty, |
|
1192 * 'exclude_tree' is ignored. |
|
1193 * |
|
1194 * include - Default is an empty array. An array, comma- or space-delimited string |
|
1195 * of term ids to include in the return array. |
|
1196 * |
|
1197 * number - The maximum number of terms to return. Default is to return them all. |
|
1198 * |
|
1199 * offset - The number by which to offset the terms query. |
|
1200 * |
|
1201 * fields - Default is 'all', which returns an array of term objects. |
|
1202 * If 'fields' is 'ids' or 'names', returns an array of |
|
1203 * integers or strings, respectively. |
|
1204 * |
|
1205 * slug - Returns terms whose "slug" matches this value. Default is empty string. |
|
1206 * |
|
1207 * hierarchical - Whether to include terms that have non-empty descendants |
|
1208 * (even if 'hide_empty' is set to true). |
|
1209 * |
|
1210 * search - Returned terms' names will contain the value of 'search', |
|
1211 * case-insensitive. Default is an empty string. |
|
1212 * |
|
1213 * name__like - Returned terms' names will contain the value of 'name__like', |
|
1214 * case-insensitive. Default is empty string. |
|
1215 * |
|
1216 * description__like - Returned terms' descriptions will contain the value of |
|
1217 * 'description__like', case-insensitive. Default is empty string. |
|
1218 * |
|
1219 * The argument 'pad_counts', if set to true will include the quantity of a term's |
|
1220 * children in the quantity of each term's "count" object variable. |
|
1221 * |
|
1222 * The 'get' argument, if set to 'all' instead of its default empty string, |
|
1223 * returns terms regardless of ancestry or whether the terms are empty. |
|
1224 * |
|
1225 * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default |
|
1226 * is 0. If set to a non-zero value, all returned terms will be descendants |
|
1227 * of that term according to the given taxonomy. Hence 'child_of' is set to 0 |
|
1228 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies |
|
1229 * make term ancestry ambiguous. |
|
1230 * |
|
1231 * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is |
|
1232 * the empty string '', which has a different meaning from the integer 0. |
|
1233 * If set to an integer value, all returned terms will have as an immediate |
|
1234 * ancestor the term whose ID is specified by that integer according to the given taxonomy. |
|
1235 * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent' |
|
1236 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc. |
|
1237 * |
|
1238 * The 'cache_domain' argument enables a unique cache key to be produced when this query is stored |
|
1239 * in object cache. For instance, if you are using one of this function's filters to modify the |
|
1240 * query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite |
|
1241 * the cache for similar queries. Default value is 'core'. |
|
1242 * |
|
1243 * @package WordPress |
|
1244 * @subpackage Taxonomy |
|
1245 * @since 2.3.0 |
|
1246 * |
|
1247 * @uses $wpdb |
|
1248 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings. |
|
1249 * |
|
1250 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names |
|
1251 * @param string|array $args The values of what to search for when returning terms |
|
1252 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. |
|
1253 */ |
|
1254 function get_terms($taxonomies, $args = '') { |
|
1255 global $wpdb; |
|
1256 $empty_array = array(); |
|
1257 |
|
1258 $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies ); |
|
1259 if ( ! is_array( $taxonomies ) ) |
|
1260 $taxonomies = array( $taxonomies ); |
|
1261 |
|
1262 foreach ( $taxonomies as $taxonomy ) { |
|
1263 if ( ! taxonomy_exists($taxonomy) ) { |
|
1264 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
1265 return $error; |
|
1266 } |
|
1267 } |
|
1268 |
|
1269 $defaults = array('orderby' => 'name', 'order' => 'ASC', |
|
1270 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), |
|
1271 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', |
|
1272 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', |
|
1273 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); |
|
1274 $args = wp_parse_args( $args, $defaults ); |
|
1275 $args['number'] = absint( $args['number'] ); |
|
1276 $args['offset'] = absint( $args['offset'] ); |
|
1277 if ( !$single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) || |
|
1278 '' !== $args['parent'] ) { |
|
1279 $args['child_of'] = 0; |
|
1280 $args['hierarchical'] = false; |
|
1281 $args['pad_counts'] = false; |
|
1282 } |
|
1283 |
|
1284 if ( 'all' == $args['get'] ) { |
|
1285 $args['child_of'] = 0; |
|
1286 $args['hide_empty'] = 0; |
|
1287 $args['hierarchical'] = false; |
|
1288 $args['pad_counts'] = false; |
|
1289 } |
|
1290 |
|
1291 $args = apply_filters( 'get_terms_args', $args, $taxonomies ); |
|
1292 |
|
1293 extract($args, EXTR_SKIP); |
|
1294 |
|
1295 if ( $child_of ) { |
|
1296 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); |
|
1297 if ( ! isset( $hierarchy[ $child_of ] ) ) |
|
1298 return $empty_array; |
|
1299 } |
|
1300 |
|
1301 if ( $parent ) { |
|
1302 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); |
|
1303 if ( ! isset( $hierarchy[ $parent ] ) ) |
|
1304 return $empty_array; |
|
1305 } |
|
1306 |
|
1307 // $args can be whatever, only use the args defined in defaults to compute the key |
|
1308 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; |
|
1309 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key ); |
|
1310 $last_changed = wp_cache_get( 'last_changed', 'terms' ); |
|
1311 if ( ! $last_changed ) { |
|
1312 $last_changed = microtime(); |
|
1313 wp_cache_set( 'last_changed', $last_changed, 'terms' ); |
|
1314 } |
|
1315 $cache_key = "get_terms:$key:$last_changed"; |
|
1316 $cache = wp_cache_get( $cache_key, 'terms' ); |
|
1317 if ( false !== $cache ) { |
|
1318 $cache = apply_filters('get_terms', $cache, $taxonomies, $args); |
|
1319 return $cache; |
|
1320 } |
|
1321 |
|
1322 $_orderby = strtolower($orderby); |
|
1323 if ( 'count' == $_orderby ) |
|
1324 $orderby = 'tt.count'; |
|
1325 else if ( 'name' == $_orderby ) |
|
1326 $orderby = 't.name'; |
|
1327 else if ( 'slug' == $_orderby ) |
|
1328 $orderby = 't.slug'; |
|
1329 else if ( 'term_group' == $_orderby ) |
|
1330 $orderby = 't.term_group'; |
|
1331 else if ( 'none' == $_orderby ) |
|
1332 $orderby = ''; |
|
1333 elseif ( empty($_orderby) || 'id' == $_orderby ) |
|
1334 $orderby = 't.term_id'; |
|
1335 else |
|
1336 $orderby = 't.name'; |
|
1337 |
|
1338 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies ); |
|
1339 |
|
1340 if ( !empty($orderby) ) |
|
1341 $orderby = "ORDER BY $orderby"; |
|
1342 else |
|
1343 $order = ''; |
|
1344 |
|
1345 $order = strtoupper( $order ); |
|
1346 if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) ) |
|
1347 $order = 'ASC'; |
|
1348 |
|
1349 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; |
|
1350 $inclusions = ''; |
|
1351 if ( ! empty( $include ) ) { |
|
1352 $exclude = ''; |
|
1353 $exclude_tree = ''; |
|
1354 $inclusions = implode( ',', wp_parse_id_list( $include ) ); |
|
1355 } |
|
1356 |
|
1357 if ( ! empty( $inclusions ) ) { |
|
1358 $inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )'; |
|
1359 $where .= $inclusions; |
|
1360 } |
|
1361 |
|
1362 $exclusions = ''; |
|
1363 if ( ! empty( $exclude_tree ) ) { |
|
1364 $exclude_tree = wp_parse_id_list( $exclude_tree ); |
|
1365 $excluded_children = $exclude_tree; |
|
1366 foreach ( $exclude_tree as $extrunk ) { |
|
1367 $excluded_children = array_merge( |
|
1368 $excluded_children, |
|
1369 (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) ) |
|
1370 ); |
|
1371 } |
|
1372 $exclusions = implode( ',', array_map( 'intval', $excluded_children ) ); |
|
1373 } |
|
1374 |
|
1375 if ( ! empty( $exclude ) ) { |
|
1376 $exterms = wp_parse_id_list( $exclude ); |
|
1377 if ( empty( $exclusions ) ) |
|
1378 $exclusions = implode( ',', $exterms ); |
|
1379 else |
|
1380 $exclusions .= ', ' . implode( ',', $exterms ); |
|
1381 } |
|
1382 |
|
1383 if ( ! empty( $exclusions ) ) |
|
1384 $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')'; |
|
1385 |
|
1386 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); |
|
1387 |
|
1388 if ( ! empty( $exclusions ) ) |
|
1389 $where .= $exclusions; |
|
1390 |
|
1391 if ( !empty($slug) ) { |
|
1392 $slug = sanitize_title($slug); |
|
1393 $where .= " AND t.slug = '$slug'"; |
|
1394 } |
|
1395 |
|
1396 if ( !empty($name__like) ) { |
|
1397 $name__like = like_escape( $name__like ); |
|
1398 $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' ); |
|
1399 } |
|
1400 |
|
1401 if ( ! empty( $description__like ) ) { |
|
1402 $description__like = like_escape( $description__like ); |
|
1403 $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' ); |
|
1404 } |
|
1405 |
|
1406 if ( '' !== $parent ) { |
|
1407 $parent = (int) $parent; |
|
1408 $where .= " AND tt.parent = '$parent'"; |
|
1409 } |
|
1410 |
|
1411 if ( 'count' == $fields ) |
|
1412 $hierarchical = false; |
|
1413 |
|
1414 if ( $hide_empty && !$hierarchical ) |
|
1415 $where .= ' AND tt.count > 0'; |
|
1416 |
|
1417 // don't limit the query results when we have to descend the family tree |
|
1418 if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { |
|
1419 if ( $offset ) |
|
1420 $limits = 'LIMIT ' . $offset . ',' . $number; |
|
1421 else |
|
1422 $limits = 'LIMIT ' . $number; |
|
1423 } else { |
|
1424 $limits = ''; |
|
1425 } |
|
1426 |
|
1427 if ( ! empty( $search ) ) { |
|
1428 $search = like_escape( $search ); |
|
1429 $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' ); |
|
1430 } |
|
1431 |
|
1432 $selects = array(); |
|
1433 switch ( $fields ) { |
|
1434 case 'all': |
|
1435 $selects = array( 't.*', 'tt.*' ); |
|
1436 break; |
|
1437 case 'ids': |
|
1438 case 'id=>parent': |
|
1439 $selects = array( 't.term_id', 'tt.parent', 'tt.count' ); |
|
1440 break; |
|
1441 case 'names': |
|
1442 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' ); |
|
1443 break; |
|
1444 case 'count': |
|
1445 $orderby = ''; |
|
1446 $order = ''; |
|
1447 $selects = array( 'COUNT(*)' ); |
|
1448 break; |
|
1449 case 'id=>name': |
|
1450 $selects = array( 't.term_id', 't.name' ); |
|
1451 break; |
|
1452 case 'id=>slug': |
|
1453 $selects = array( 't.term_id', 't.slug' ); |
|
1454 break; |
|
1455 } |
|
1456 |
|
1457 $_fields = $fields; |
|
1458 |
|
1459 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); |
|
1460 |
|
1461 $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; |
|
1462 |
|
1463 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); |
|
1464 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); |
|
1465 foreach ( $pieces as $piece ) |
|
1466 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; |
|
1467 |
|
1468 $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; |
|
1469 |
|
1470 $fields = $_fields; |
|
1471 |
|
1472 if ( 'count' == $fields ) { |
|
1473 $term_count = $wpdb->get_var($query); |
|
1474 return $term_count; |
|
1475 } |
|
1476 |
|
1477 $terms = $wpdb->get_results($query); |
|
1478 if ( 'all' == $fields ) { |
|
1479 update_term_cache($terms); |
|
1480 } |
|
1481 |
|
1482 if ( empty($terms) ) { |
|
1483 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); |
|
1484 $terms = apply_filters('get_terms', array(), $taxonomies, $args); |
|
1485 return $terms; |
|
1486 } |
|
1487 |
|
1488 if ( $child_of ) { |
|
1489 $children = _get_term_hierarchy( reset( $taxonomies ) ); |
|
1490 if ( ! empty( $children ) ) |
|
1491 $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) ); |
|
1492 } |
|
1493 |
|
1494 // Update term counts to include children. |
|
1495 if ( $pad_counts && 'all' == $fields ) |
|
1496 _pad_term_counts( $terms, reset( $taxonomies ) ); |
|
1497 |
|
1498 // Make sure we show empty categories that have children. |
|
1499 if ( $hierarchical && $hide_empty && is_array( $terms ) ) { |
|
1500 foreach ( $terms as $k => $term ) { |
|
1501 if ( ! $term->count ) { |
|
1502 $children = _get_term_children( $term->term_id, $terms, reset( $taxonomies ) ); |
|
1503 if ( is_array( $children ) ) |
|
1504 foreach ( $children as $child ) |
|
1505 if ( $child->count ) |
|
1506 continue 2; |
|
1507 |
|
1508 // It really is empty |
|
1509 unset($terms[$k]); |
|
1510 } |
|
1511 } |
|
1512 } |
|
1513 reset( $terms ); |
|
1514 |
|
1515 $_terms = array(); |
|
1516 if ( 'id=>parent' == $fields ) { |
|
1517 while ( $term = array_shift( $terms ) ) |
|
1518 $_terms[$term->term_id] = $term->parent; |
|
1519 } elseif ( 'ids' == $fields ) { |
|
1520 while ( $term = array_shift( $terms ) ) |
|
1521 $_terms[] = $term->term_id; |
|
1522 } elseif ( 'names' == $fields ) { |
|
1523 while ( $term = array_shift( $terms ) ) |
|
1524 $_terms[] = $term->name; |
|
1525 } elseif ( 'id=>name' == $fields ) { |
|
1526 while ( $term = array_shift( $terms ) ) |
|
1527 $_terms[$term->term_id] = $term->name; |
|
1528 } elseif ( 'id=>slug' == $fields ) { |
|
1529 while ( $term = array_shift( $terms ) ) |
|
1530 $_terms[$term->term_id] = $term->slug; |
|
1531 } |
|
1532 |
|
1533 if ( ! empty( $_terms ) ) |
|
1534 $terms = $_terms; |
|
1535 |
|
1536 if ( $number && is_array( $terms ) && count( $terms ) > $number ) |
|
1537 $terms = array_slice( $terms, $offset, $number ); |
|
1538 |
|
1539 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); |
|
1540 |
|
1541 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); |
|
1542 return $terms; |
|
1543 } |
|
1544 |
|
1545 /** |
|
1546 * Check if Term exists. |
|
1547 * |
|
1548 * Formerly is_term(), introduced in 2.3.0. |
|
1549 * |
|
1550 * @package WordPress |
|
1551 * @subpackage Taxonomy |
|
1552 * @since 3.0.0 |
|
1553 * |
|
1554 * @uses $wpdb |
|
1555 * |
|
1556 * @param int|string $term The term to check |
|
1557 * @param string $taxonomy The taxonomy name to use |
|
1558 * @param int $parent ID of parent term under which to confine the exists search. |
|
1559 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified |
|
1560 * and the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists. |
|
1561 */ |
|
1562 function term_exists($term, $taxonomy = '', $parent = 0) { |
|
1563 global $wpdb; |
|
1564 |
|
1565 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; |
|
1566 $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 "; |
|
1567 |
|
1568 if ( is_int($term) ) { |
|
1569 if ( 0 == $term ) |
|
1570 return 0; |
|
1571 $where = 't.term_id = %d'; |
|
1572 if ( !empty($taxonomy) ) |
|
1573 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); |
|
1574 else |
|
1575 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); |
|
1576 } |
|
1577 |
|
1578 $term = trim( wp_unslash( $term ) ); |
|
1579 |
|
1580 if ( '' === $slug = sanitize_title($term) ) |
|
1581 return 0; |
|
1582 |
|
1583 $where = 't.slug = %s'; |
|
1584 $else_where = 't.name = %s'; |
|
1585 $where_fields = array($slug); |
|
1586 $else_where_fields = array($term); |
|
1587 if ( !empty($taxonomy) ) { |
|
1588 $parent = (int) $parent; |
|
1589 if ( $parent > 0 ) { |
|
1590 $where_fields[] = $parent; |
|
1591 $else_where_fields[] = $parent; |
|
1592 $where .= ' AND tt.parent = %d'; |
|
1593 $else_where .= ' AND tt.parent = %d'; |
|
1594 } |
|
1595 |
|
1596 $where_fields[] = $taxonomy; |
|
1597 $else_where_fields[] = $taxonomy; |
|
1598 |
|
1599 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) ) |
|
1600 return $result; |
|
1601 |
|
1602 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); |
|
1603 } |
|
1604 |
|
1605 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) ) |
|
1606 return $result; |
|
1607 |
|
1608 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) ); |
|
1609 } |
|
1610 |
|
1611 /** |
|
1612 * Check if a term is an ancestor of another term. |
|
1613 * |
|
1614 * You can use either an id or the term object for both parameters. |
|
1615 * |
|
1616 * @since 3.4.0 |
|
1617 * |
|
1618 * @param int|object $term1 ID or object to check if this is the parent term. |
|
1619 * @param int|object $term2 The child term. |
|
1620 * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to. |
|
1621 * @return bool Whether $term2 is child of $term1 |
|
1622 */ |
|
1623 function term_is_ancestor_of( $term1, $term2, $taxonomy ) { |
|
1624 if ( ! isset( $term1->term_id ) ) |
|
1625 $term1 = get_term( $term1, $taxonomy ); |
|
1626 if ( ! isset( $term2->parent ) ) |
|
1627 $term2 = get_term( $term2, $taxonomy ); |
|
1628 |
|
1629 if ( empty( $term1->term_id ) || empty( $term2->parent ) ) |
|
1630 return false; |
|
1631 if ( $term2->parent == $term1->term_id ) |
|
1632 return true; |
|
1633 |
|
1634 return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); |
|
1635 } |
|
1636 |
|
1637 /** |
|
1638 * Sanitize Term all fields. |
|
1639 * |
|
1640 * Relies on sanitize_term_field() to sanitize the term. The difference is that |
|
1641 * this function will sanitize <strong>all</strong> fields. The context is based |
|
1642 * on sanitize_term_field(). |
|
1643 * |
|
1644 * The $term is expected to be either an array or an object. |
|
1645 * |
|
1646 * @package WordPress |
|
1647 * @subpackage Taxonomy |
|
1648 * @since 2.3.0 |
|
1649 * |
|
1650 * @uses sanitize_term_field Used to sanitize all fields in a term |
|
1651 * |
|
1652 * @param array|object $term The term to check |
|
1653 * @param string $taxonomy The taxonomy name to use |
|
1654 * @param string $context Default is 'display'. |
|
1655 * @return array|object Term with all fields sanitized |
|
1656 */ |
|
1657 function sanitize_term($term, $taxonomy, $context = 'display') { |
|
1658 |
|
1659 if ( 'raw' == $context ) |
|
1660 return $term; |
|
1661 |
|
1662 $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group'); |
|
1663 |
|
1664 $do_object = false; |
|
1665 if ( is_object($term) ) |
|
1666 $do_object = true; |
|
1667 |
|
1668 $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0); |
|
1669 |
|
1670 foreach ( (array) $fields as $field ) { |
|
1671 if ( $do_object ) { |
|
1672 if ( isset($term->$field) ) |
|
1673 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); |
|
1674 } else { |
|
1675 if ( isset($term[$field]) ) |
|
1676 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); |
|
1677 } |
|
1678 } |
|
1679 |
|
1680 if ( $do_object ) |
|
1681 $term->filter = $context; |
|
1682 else |
|
1683 $term['filter'] = $context; |
|
1684 |
|
1685 return $term; |
|
1686 } |
|
1687 |
|
1688 /** |
|
1689 * Cleanse the field value in the term based on the context. |
|
1690 * |
|
1691 * Passing a term field value through the function should be assumed to have |
|
1692 * cleansed the value for whatever context the term field is going to be used. |
|
1693 * |
|
1694 * If no context or an unsupported context is given, then default filters will |
|
1695 * be applied. |
|
1696 * |
|
1697 * There are enough filters for each context to support a custom filtering |
|
1698 * without creating your own filter function. Simply create a function that |
|
1699 * hooks into the filter you need. |
|
1700 * |
|
1701 * @package WordPress |
|
1702 * @subpackage Taxonomy |
|
1703 * @since 2.3.0 |
|
1704 * |
|
1705 * @uses $wpdb |
|
1706 * |
|
1707 * @param string $field Term field to sanitize |
|
1708 * @param string $value Search for this term value |
|
1709 * @param int $term_id Term ID |
|
1710 * @param string $taxonomy Taxonomy Name |
|
1711 * @param string $context Either edit, db, display, attribute, or js. |
|
1712 * @return mixed sanitized field |
|
1713 */ |
|
1714 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { |
|
1715 if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) { |
|
1716 $value = (int) $value; |
|
1717 if ( $value < 0 ) |
|
1718 $value = 0; |
|
1719 } |
|
1720 |
|
1721 if ( 'raw' == $context ) |
|
1722 return $value; |
|
1723 |
|
1724 if ( 'edit' == $context ) { |
|
1725 $value = apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy); |
|
1726 $value = apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id); |
|
1727 if ( 'description' == $field ) |
|
1728 $value = esc_html($value); // textarea_escaped |
|
1729 else |
|
1730 $value = esc_attr($value); |
|
1731 } else if ( 'db' == $context ) { |
|
1732 $value = apply_filters("pre_term_{$field}", $value, $taxonomy); |
|
1733 $value = apply_filters("pre_{$taxonomy}_{$field}", $value); |
|
1734 // Back compat filters |
|
1735 if ( 'slug' == $field ) |
|
1736 $value = apply_filters('pre_category_nicename', $value); |
|
1737 |
|
1738 } else if ( 'rss' == $context ) { |
|
1739 $value = apply_filters("term_{$field}_rss", $value, $taxonomy); |
|
1740 $value = apply_filters("{$taxonomy}_{$field}_rss", $value); |
|
1741 } else { |
|
1742 // Use display filters by default. |
|
1743 $value = apply_filters("term_{$field}", $value, $term_id, $taxonomy, $context); |
|
1744 $value = apply_filters("{$taxonomy}_{$field}", $value, $term_id, $context); |
|
1745 } |
|
1746 |
|
1747 if ( 'attribute' == $context ) |
|
1748 $value = esc_attr($value); |
|
1749 else if ( 'js' == $context ) |
|
1750 $value = esc_js($value); |
|
1751 |
|
1752 return $value; |
|
1753 } |
|
1754 |
|
1755 /** |
|
1756 * Count how many terms are in Taxonomy. |
|
1757 * |
|
1758 * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). |
|
1759 * |
|
1760 * @package WordPress |
|
1761 * @subpackage Taxonomy |
|
1762 * @since 2.3.0 |
|
1763 * |
|
1764 * @uses get_terms() |
|
1765 * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array. |
|
1766 * |
|
1767 * @param string $taxonomy Taxonomy name |
|
1768 * @param array|string $args Overwrite defaults. See get_terms() |
|
1769 * @return int|WP_Error How many terms are in $taxonomy. WP_Error if $taxonomy does not exist. |
|
1770 */ |
|
1771 function wp_count_terms( $taxonomy, $args = array() ) { |
|
1772 $defaults = array('hide_empty' => false); |
|
1773 $args = wp_parse_args($args, $defaults); |
|
1774 |
|
1775 // backwards compatibility |
|
1776 if ( isset($args['ignore_empty']) ) { |
|
1777 $args['hide_empty'] = $args['ignore_empty']; |
|
1778 unset($args['ignore_empty']); |
|
1779 } |
|
1780 |
|
1781 $args['fields'] = 'count'; |
|
1782 |
|
1783 return get_terms($taxonomy, $args); |
|
1784 } |
|
1785 |
|
1786 /** |
|
1787 * Will unlink the object from the taxonomy or taxonomies. |
|
1788 * |
|
1789 * Will remove all relationships between the object and any terms in |
|
1790 * a particular taxonomy or taxonomies. Does not remove the term or |
|
1791 * taxonomy itself. |
|
1792 * |
|
1793 * @package WordPress |
|
1794 * @subpackage Taxonomy |
|
1795 * @since 2.3.0 |
|
1796 * @uses wp_remove_object_terms() |
|
1797 * |
|
1798 * @param int $object_id The term Object Id that refers to the term |
|
1799 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. |
|
1800 */ |
|
1801 function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|
1802 $object_id = (int) $object_id; |
|
1803 |
|
1804 if ( !is_array($taxonomies) ) |
|
1805 $taxonomies = array($taxonomies); |
|
1806 |
|
1807 foreach ( (array) $taxonomies as $taxonomy ) { |
|
1808 $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
|
1809 $term_ids = array_map( 'intval', $term_ids ); |
|
1810 wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); |
|
1811 } |
|
1812 } |
|
1813 |
|
1814 /** |
|
1815 * Removes a term from the database. |
|
1816 * |
|
1817 * If the term is a parent of other terms, then the children will be updated to |
|
1818 * that term's parent. |
|
1819 * |
|
1820 * The $args 'default' will only override the terms found, if there is only one |
|
1821 * term found. Any other and the found terms are used. |
|
1822 * |
|
1823 * The $args 'force_default' will force the term supplied as default to be |
|
1824 * assigned even if the object was not going to be termless |
|
1825 * @package WordPress |
|
1826 * @subpackage Taxonomy |
|
1827 * @since 2.3.0 |
|
1828 * |
|
1829 * @uses $wpdb |
|
1830 * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action |
|
1831 * hooks, passing term ID, term taxonomy ID, and deleted term object. 'delete_term' |
|
1832 * also gets taxonomy as the third parameter. |
|
1833 * |
|
1834 * @param int $term Term ID |
|
1835 * @param string $taxonomy Taxonomy Name |
|
1836 * @param array|string $args Optional. Change 'default' term id and override found term ids. |
|
1837 * @return bool|WP_Error Returns false if not term; true if completes delete action. |
|
1838 */ |
|
1839 function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|
1840 global $wpdb; |
|
1841 |
|
1842 $term = (int) $term; |
|
1843 |
|
1844 if ( ! $ids = term_exists($term, $taxonomy) ) |
|
1845 return false; |
|
1846 if ( is_wp_error( $ids ) ) |
|
1847 return $ids; |
|
1848 |
|
1849 $tt_id = $ids['term_taxonomy_id']; |
|
1850 |
|
1851 $defaults = array(); |
|
1852 |
|
1853 if ( 'category' == $taxonomy ) { |
|
1854 $defaults['default'] = get_option( 'default_category' ); |
|
1855 if ( $defaults['default'] == $term ) |
|
1856 return 0; // Don't delete the default category |
|
1857 } |
|
1858 |
|
1859 $args = wp_parse_args($args, $defaults); |
|
1860 extract($args, EXTR_SKIP); |
|
1861 |
|
1862 if ( isset( $default ) ) { |
|
1863 $default = (int) $default; |
|
1864 if ( ! term_exists($default, $taxonomy) ) |
|
1865 unset($default); |
|
1866 } |
|
1867 |
|
1868 // Update children to point to new parent |
|
1869 if ( is_taxonomy_hierarchical($taxonomy) ) { |
|
1870 $term_obj = get_term($term, $taxonomy); |
|
1871 if ( is_wp_error( $term_obj ) ) |
|
1872 return $term_obj; |
|
1873 $parent = $term_obj->parent; |
|
1874 |
|
1875 $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); |
|
1876 do_action( 'edit_term_taxonomies', $edit_tt_ids ); |
|
1877 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) ); |
|
1878 do_action( 'edited_term_taxonomies', $edit_tt_ids ); |
|
1879 } |
|
1880 |
|
1881 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); |
|
1882 |
|
1883 foreach ( (array) $objects as $object ) { |
|
1884 $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none')); |
|
1885 if ( 1 == count($terms) && isset($default) ) { |
|
1886 $terms = array($default); |
|
1887 } else { |
|
1888 $terms = array_diff($terms, array($term)); |
|
1889 if (isset($default) && isset($force_default) && $force_default) |
|
1890 $terms = array_merge($terms, array($default)); |
|
1891 } |
|
1892 $terms = array_map('intval', $terms); |
|
1893 wp_set_object_terms($object, $terms, $taxonomy); |
|
1894 } |
|
1895 |
|
1896 // Clean the relationship caches for all object types using this term |
|
1897 $tax_object = get_taxonomy( $taxonomy ); |
|
1898 foreach ( $tax_object->object_type as $object_type ) |
|
1899 clean_object_term_cache( $objects, $object_type ); |
|
1900 |
|
1901 // Get the object before deletion so we can pass to actions below |
|
1902 $deleted_term = get_term( $term, $taxonomy ); |
|
1903 |
|
1904 do_action( 'delete_term_taxonomy', $tt_id ); |
|
1905 $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); |
|
1906 do_action( 'deleted_term_taxonomy', $tt_id ); |
|
1907 |
|
1908 // Delete the term if no taxonomies use it. |
|
1909 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) ) |
|
1910 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); |
|
1911 |
|
1912 clean_term_cache($term, $taxonomy); |
|
1913 |
|
1914 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term ); |
|
1915 do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term ); |
|
1916 |
|
1917 return true; |
|
1918 } |
|
1919 |
|
1920 /** |
|
1921 * Deletes one existing category. |
|
1922 * |
|
1923 * @since 2.0.0 |
|
1924 * @uses wp_delete_term() |
|
1925 * |
|
1926 * @param int $cat_ID |
|
1927 * @return mixed Returns true if completes delete action; false if term doesn't exist; |
|
1928 * Zero on attempted deletion of default Category; WP_Error object is also a possibility. |
|
1929 */ |
|
1930 function wp_delete_category( $cat_ID ) { |
|
1931 return wp_delete_term( $cat_ID, 'category' ); |
|
1932 } |
|
1933 |
|
1934 /** |
|
1935 * Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
|
1936 * |
|
1937 * The following information has to do the $args parameter and for what can be |
|
1938 * contained in the string or array of that parameter, if it exists. |
|
1939 * |
|
1940 * The first argument is called, 'orderby' and has the default value of 'name'. |
|
1941 * The other value that is supported is 'count'. |
|
1942 * |
|
1943 * The second argument is called, 'order' and has the default value of 'ASC'. |
|
1944 * The only other value that will be acceptable is 'DESC'. |
|
1945 * |
|
1946 * The final argument supported is called, 'fields' and has the default value of |
|
1947 * 'all'. There are multiple other options that can be used instead. Supported |
|
1948 * values are as follows: 'all', 'ids', 'names', and finally |
|
1949 * 'all_with_object_id'. |
|
1950 * |
|
1951 * The fields argument also decides what will be returned. If 'all' or |
|
1952 * 'all_with_object_id' is chosen or the default kept intact, then all matching |
|
1953 * terms objects will be returned. If either 'ids' or 'names' is used, then an |
|
1954 * array of all matching term ids or term names will be returned respectively. |
|
1955 * |
|
1956 * @package WordPress |
|
1957 * @subpackage Taxonomy |
|
1958 * @since 2.3.0 |
|
1959 * @uses $wpdb |
|
1960 * |
|
1961 * @param int|array $object_ids The ID(s) of the object(s) to retrieve. |
|
1962 * @param string|array $taxonomies The taxonomies to retrieve terms from. |
|
1963 * @param array|string $args Change what is returned |
|
1964 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist. |
|
1965 */ |
|
1966 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { |
|
1967 global $wpdb; |
|
1968 |
|
1969 if ( empty( $object_ids ) || empty( $taxonomies ) ) |
|
1970 return array(); |
|
1971 |
|
1972 if ( !is_array($taxonomies) ) |
|
1973 $taxonomies = array($taxonomies); |
|
1974 |
|
1975 foreach ( (array) $taxonomies as $taxonomy ) { |
|
1976 if ( ! taxonomy_exists($taxonomy) ) |
|
1977 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
1978 } |
|
1979 |
|
1980 if ( !is_array($object_ids) ) |
|
1981 $object_ids = array($object_ids); |
|
1982 $object_ids = array_map('intval', $object_ids); |
|
1983 |
|
1984 $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); |
|
1985 $args = wp_parse_args( $args, $defaults ); |
|
1986 |
|
1987 $terms = array(); |
|
1988 if ( count($taxonomies) > 1 ) { |
|
1989 foreach ( $taxonomies as $index => $taxonomy ) { |
|
1990 $t = get_taxonomy($taxonomy); |
|
1991 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { |
|
1992 unset($taxonomies[$index]); |
|
1993 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); |
|
1994 } |
|
1995 } |
|
1996 } else { |
|
1997 $t = get_taxonomy($taxonomies[0]); |
|
1998 if ( isset($t->args) && is_array($t->args) ) |
|
1999 $args = array_merge($args, $t->args); |
|
2000 } |
|
2001 |
|
2002 extract($args, EXTR_SKIP); |
|
2003 |
|
2004 if ( 'count' == $orderby ) |
|
2005 $orderby = 'tt.count'; |
|
2006 else if ( 'name' == $orderby ) |
|
2007 $orderby = 't.name'; |
|
2008 else if ( 'slug' == $orderby ) |
|
2009 $orderby = 't.slug'; |
|
2010 else if ( 'term_group' == $orderby ) |
|
2011 $orderby = 't.term_group'; |
|
2012 else if ( 'term_order' == $orderby ) |
|
2013 $orderby = 'tr.term_order'; |
|
2014 else if ( 'none' == $orderby ) { |
|
2015 $orderby = ''; |
|
2016 $order = ''; |
|
2017 } else { |
|
2018 $orderby = 't.term_id'; |
|
2019 } |
|
2020 |
|
2021 // tt_ids queries can only be none or tr.term_taxonomy_id |
|
2022 if ( ('tt_ids' == $fields) && !empty($orderby) ) |
|
2023 $orderby = 'tr.term_taxonomy_id'; |
|
2024 |
|
2025 if ( !empty($orderby) ) |
|
2026 $orderby = "ORDER BY $orderby"; |
|
2027 |
|
2028 $order = strtoupper( $order ); |
|
2029 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) |
|
2030 $order = 'ASC'; |
|
2031 |
|
2032 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|
2033 $object_ids = implode(', ', $object_ids); |
|
2034 |
|
2035 $select_this = ''; |
|
2036 if ( 'all' == $fields ) |
|
2037 $select_this = 't.*, tt.*'; |
|
2038 else if ( 'ids' == $fields ) |
|
2039 $select_this = 't.term_id'; |
|
2040 else if ( 'names' == $fields ) |
|
2041 $select_this = 't.name'; |
|
2042 else if ( 'slugs' == $fields ) |
|
2043 $select_this = 't.slug'; |
|
2044 else if ( 'all_with_object_id' == $fields ) |
|
2045 $select_this = 't.*, tt.*, tr.object_id'; |
|
2046 |
|
2047 $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"; |
|
2048 |
|
2049 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { |
|
2050 $terms = array_merge($terms, $wpdb->get_results($query)); |
|
2051 update_term_cache($terms); |
|
2052 } else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { |
|
2053 $terms = array_merge($terms, $wpdb->get_col($query)); |
|
2054 } else if ( 'tt_ids' == $fields ) { |
|
2055 $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"); |
|
2056 } |
|
2057 |
|
2058 if ( ! $terms ) |
|
2059 $terms = array(); |
|
2060 |
|
2061 return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args); |
|
2062 } |
|
2063 |
|
2064 /** |
|
2065 * Add a new term to the database. |
|
2066 * |
|
2067 * A non-existent term is inserted in the following sequence: |
|
2068 * 1. The term is added to the term table, then related to the taxonomy. |
|
2069 * 2. If everything is correct, several actions are fired. |
|
2070 * 3. The 'term_id_filter' is evaluated. |
|
2071 * 4. The term cache is cleaned. |
|
2072 * 5. Several more actions are fired. |
|
2073 * 6. An array is returned containing the term_id and term_taxonomy_id. |
|
2074 * |
|
2075 * If the 'slug' argument is not empty, then it is checked to see if the term |
|
2076 * is invalid. If it is not a valid, existing term, it is added and the term_id |
|
2077 * is given. |
|
2078 * |
|
2079 * If the taxonomy is hierarchical, and the 'parent' argument is not empty, |
|
2080 * the term is inserted and the term_id will be given. |
|
2081 |
|
2082 * Error handling: |
|
2083 * If $taxonomy does not exist or $term is empty, |
|
2084 * a WP_Error object will be returned. |
|
2085 * |
|
2086 * If the term already exists on the same hierarchical level, |
|
2087 * or the term slug and name are not unique, a WP_Error object will be returned. |
|
2088 * |
|
2089 * @global wpdb $wpdb The WordPress database object. |
|
2090 |
|
2091 * @since 2.3.0 |
|
2092 * |
|
2093 * @param string $term The term to add or update. |
|
2094 * @param string $taxonomy The taxonomy to which to add the term |
|
2095 * @param array|string $args { |
|
2096 * Arguments to change values of the inserted term. |
|
2097 * |
|
2098 * @type string 'alias_of' Slug of the term to make this term an alias of. |
|
2099 * Default empty string. Accepts a term slug. |
|
2100 * @type string 'description' The term description. |
|
2101 * Default empty string. |
|
2102 * @type int 'parent' The id of the parent term. |
|
2103 * Default 0. |
|
2104 * @type string 'slug' The term slug to use. |
|
2105 * Default empty string. |
|
2106 * } |
|
2107 * @return array|WP_Error An array containing the term_id and term_taxonomy_id, WP_Error otherwise. |
|
2108 */ |
|
2109 function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|
2110 global $wpdb; |
|
2111 |
|
2112 if ( ! taxonomy_exists($taxonomy) ) |
|
2113 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
2114 |
|
2115 $term = apply_filters( 'pre_insert_term', $term, $taxonomy ); |
|
2116 if ( is_wp_error( $term ) ) |
|
2117 return $term; |
|
2118 |
|
2119 if ( is_int($term) && 0 == $term ) |
|
2120 return new WP_Error('invalid_term_id', __('Invalid term ID')); |
|
2121 |
|
2122 if ( '' == trim($term) ) |
|
2123 return new WP_Error('empty_term_name', __('A name is required for this term')); |
|
2124 |
|
2125 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|
2126 $args = wp_parse_args($args, $defaults); |
|
2127 $args['name'] = $term; |
|
2128 $args['taxonomy'] = $taxonomy; |
|
2129 $args = sanitize_term($args, $taxonomy, 'db'); |
|
2130 extract($args, EXTR_SKIP); |
|
2131 |
|
2132 // expected_slashed ($name) |
|
2133 $name = wp_unslash($name); |
|
2134 $description = wp_unslash($description); |
|
2135 |
|
2136 if ( empty($slug) ) |
|
2137 $slug = sanitize_title($name); |
|
2138 |
|
2139 $term_group = 0; |
|
2140 if ( $alias_of ) { |
|
2141 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); |
|
2142 if ( $alias->term_group ) { |
|
2143 // The alias we want is already in a group, so let's use that one. |
|
2144 $term_group = $alias->term_group; |
|
2145 } else { |
|
2146 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|
2147 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; |
|
2148 do_action( 'edit_terms', $alias->term_id, $taxonomy ); |
|
2149 $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); |
|
2150 do_action( 'edited_terms', $alias->term_id, $taxonomy ); |
|
2151 } |
|
2152 } |
|
2153 |
|
2154 if ( $term_id = term_exists($slug) ) { |
|
2155 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A ); |
|
2156 // We've got an existing term in the same taxonomy, which matches the name of the new term: |
|
2157 if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) { |
|
2158 // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level. |
|
2159 $siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) ); |
|
2160 if ( in_array($name, $siblings) ) { |
|
2161 return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']); |
|
2162 } else { |
|
2163 $slug = wp_unique_term_slug($slug, (object) $args); |
|
2164 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) |
|
2165 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); |
|
2166 $term_id = (int) $wpdb->insert_id; |
|
2167 } |
|
2168 } elseif ( $existing_term['name'] != $name ) { |
|
2169 // We've got an existing term, with a different name, Create the new term. |
|
2170 $slug = wp_unique_term_slug($slug, (object) $args); |
|
2171 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) |
|
2172 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); |
|
2173 $term_id = (int) $wpdb->insert_id; |
|
2174 } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) { |
|
2175 // Same name, same slug. |
|
2176 return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']); |
|
2177 } |
|
2178 } else { |
|
2179 // This term does not exist at all in the database, Create it. |
|
2180 $slug = wp_unique_term_slug($slug, (object) $args); |
|
2181 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) |
|
2182 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); |
|
2183 $term_id = (int) $wpdb->insert_id; |
|
2184 } |
|
2185 |
|
2186 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. |
|
2187 if ( empty($slug) ) { |
|
2188 $slug = sanitize_title($slug, $term_id); |
|
2189 do_action( 'edit_terms', $term_id, $taxonomy ); |
|
2190 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
2191 do_action( 'edited_terms', $term_id, $taxonomy ); |
|
2192 } |
|
2193 |
|
2194 $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 ) ); |
|
2195 |
|
2196 if ( !empty($tt_id) ) |
|
2197 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
2198 |
|
2199 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); |
|
2200 $tt_id = (int) $wpdb->insert_id; |
|
2201 |
|
2202 do_action("create_term", $term_id, $tt_id, $taxonomy); |
|
2203 do_action("create_$taxonomy", $term_id, $tt_id); |
|
2204 |
|
2205 $term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|
2206 |
|
2207 clean_term_cache($term_id, $taxonomy); |
|
2208 |
|
2209 do_action("created_term", $term_id, $tt_id, $taxonomy); |
|
2210 do_action("created_$taxonomy", $term_id, $tt_id); |
|
2211 |
|
2212 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
2213 } |
|
2214 |
|
2215 /** |
|
2216 * Create Term and Taxonomy Relationships. |
|
2217 * |
|
2218 * Relates an object (post, link etc) to a term and taxonomy type. Creates the |
|
2219 * term and taxonomy relationship if it doesn't already exist. Creates a term if |
|
2220 * it doesn't exist (using the slug). |
|
2221 * |
|
2222 * A relationship means that the term is grouped in or belongs to the taxonomy. |
|
2223 * A term has no meaning until it is given context by defining which taxonomy it |
|
2224 * exists under. |
|
2225 * |
|
2226 * @package WordPress |
|
2227 * @subpackage Taxonomy |
|
2228 * @since 2.3.0 |
|
2229 * @uses wp_remove_object_terms() |
|
2230 * |
|
2231 * @param int $object_id The object to relate to. |
|
2232 * @param array|int|string $terms The slug or id of the term, will replace all existing |
|
2233 * related terms in this taxonomy. |
|
2234 * @param array|string $taxonomy The context in which to relate the term to the object. |
|
2235 * @param bool $append If false will delete difference of terms. |
|
2236 * @return array|WP_Error Affected Term IDs |
|
2237 */ |
|
2238 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { |
|
2239 global $wpdb; |
|
2240 |
|
2241 $object_id = (int) $object_id; |
|
2242 |
|
2243 if ( ! taxonomy_exists($taxonomy) ) |
|
2244 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
2245 |
|
2246 if ( !is_array($terms) ) |
|
2247 $terms = array($terms); |
|
2248 |
|
2249 if ( ! $append ) |
|
2250 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); |
|
2251 else |
|
2252 $old_tt_ids = array(); |
|
2253 |
|
2254 $tt_ids = array(); |
|
2255 $term_ids = array(); |
|
2256 $new_tt_ids = array(); |
|
2257 |
|
2258 foreach ( (array) $terms as $term) { |
|
2259 if ( !strlen(trim($term)) ) |
|
2260 continue; |
|
2261 |
|
2262 if ( !$term_info = term_exists($term, $taxonomy) ) { |
|
2263 // Skip if a non-existent term ID is passed. |
|
2264 if ( is_int($term) ) |
|
2265 continue; |
|
2266 $term_info = wp_insert_term($term, $taxonomy); |
|
2267 } |
|
2268 if ( is_wp_error($term_info) ) |
|
2269 return $term_info; |
|
2270 $term_ids[] = $term_info['term_id']; |
|
2271 $tt_id = $term_info['term_taxonomy_id']; |
|
2272 $tt_ids[] = $tt_id; |
|
2273 |
|
2274 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 ) ) ) |
|
2275 continue; |
|
2276 do_action( 'add_term_relationship', $object_id, $tt_id ); |
|
2277 $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) ); |
|
2278 do_action( 'added_term_relationship', $object_id, $tt_id ); |
|
2279 $new_tt_ids[] = $tt_id; |
|
2280 } |
|
2281 |
|
2282 if ( $new_tt_ids ) |
|
2283 wp_update_term_count( $new_tt_ids, $taxonomy ); |
|
2284 |
|
2285 if ( ! $append ) { |
|
2286 $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids ); |
|
2287 |
|
2288 if ( $delete_tt_ids ) { |
|
2289 $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; |
|
2290 $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) ); |
|
2291 $delete_term_ids = array_map( 'intval', $delete_term_ids ); |
|
2292 |
|
2293 $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); |
|
2294 if ( is_wp_error( $remove ) ) { |
|
2295 return $remove; |
|
2296 } |
|
2297 } |
|
2298 } |
|
2299 |
|
2300 $t = get_taxonomy($taxonomy); |
|
2301 if ( ! $append && isset($t->sort) && $t->sort ) { |
|
2302 $values = array(); |
|
2303 $term_order = 0; |
|
2304 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); |
|
2305 foreach ( $tt_ids as $tt_id ) |
|
2306 if ( in_array($tt_id, $final_tt_ids) ) |
|
2307 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order); |
|
2308 if ( $values ) |
|
2309 if ( false === $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)" ) ) |
|
2310 return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database' ), $wpdb->last_error ); |
|
2311 } |
|
2312 |
|
2313 wp_cache_delete( $object_id, $taxonomy . '_relationships' ); |
|
2314 |
|
2315 do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); |
|
2316 return $tt_ids; |
|
2317 } |
|
2318 |
|
2319 /** |
|
2320 * Add term(s) associated with a given object. |
|
2321 * |
|
2322 * @package WordPress |
|
2323 * @subpackage Taxonomy |
|
2324 * @since 3.6 |
|
2325 * @uses wp_set_object_terms() |
|
2326 * |
|
2327 * @param int $object_id The ID of the object to which the terms will be added. |
|
2328 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to add. |
|
2329 * @param array|string $taxonomy Taxonomy name. |
|
2330 * @return array|WP_Error Affected Term IDs |
|
2331 */ |
|
2332 function wp_add_object_terms( $object_id, $terms, $taxonomy ) { |
|
2333 return wp_set_object_terms( $object_id, $terms, $taxonomy, true ); |
|
2334 } |
|
2335 |
|
2336 /** |
|
2337 * Remove term(s) associated with a given object. |
|
2338 * |
|
2339 * @package WordPress |
|
2340 * @subpackage Taxonomy |
|
2341 * @since 3.6 |
|
2342 * @uses $wpdb |
|
2343 * |
|
2344 * @uses apply_filters() Calls 'delete_term_relationships' hook with object_id and tt_ids as parameters. |
|
2345 * @uses apply_filters() Calls 'deleted_term_relationships' hook with object_id and tt_ids as parameters. |
|
2346 * |
|
2347 * @param int $object_id The ID of the object from which the terms will be removed. |
|
2348 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove. |
|
2349 * @param array|string $taxonomy Taxonomy name. |
|
2350 * @return bool|WP_Error True on success, false or WP_Error on failure. |
|
2351 */ |
|
2352 function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { |
|
2353 global $wpdb; |
|
2354 |
|
2355 $object_id = (int) $object_id; |
|
2356 |
|
2357 if ( ! taxonomy_exists( $taxonomy ) ) { |
|
2358 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); |
|
2359 } |
|
2360 |
|
2361 if ( ! is_array( $terms ) ) { |
|
2362 $terms = array( $terms ); |
|
2363 } |
|
2364 |
|
2365 $tt_ids = array(); |
|
2366 |
|
2367 foreach ( (array) $terms as $term ) { |
|
2368 if ( ! strlen( trim( $term ) ) ) { |
|
2369 continue; |
|
2370 } |
|
2371 |
|
2372 if ( ! $term_info = term_exists( $term, $taxonomy ) ) { |
|
2373 // Skip if a non-existent term ID is passed. |
|
2374 if ( is_int( $term ) ) { |
|
2375 continue; |
|
2376 } |
|
2377 } |
|
2378 |
|
2379 if ( is_wp_error( $term_info ) ) { |
|
2380 return $term_info; |
|
2381 } |
|
2382 |
|
2383 $tt_ids[] = $term_info['term_taxonomy_id']; |
|
2384 } |
|
2385 |
|
2386 if ( $tt_ids ) { |
|
2387 $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; |
|
2388 do_action( 'delete_term_relationships', $object_id, $tt_ids ); |
|
2389 $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); |
|
2390 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); |
|
2391 wp_update_term_count( $tt_ids, $taxonomy ); |
|
2392 |
|
2393 return (bool) $deleted; |
|
2394 } |
|
2395 |
|
2396 return false; |
|
2397 } |
|
2398 |
|
2399 /** |
|
2400 * Will make slug unique, if it isn't already. |
|
2401 * |
|
2402 * The $slug has to be unique global to every taxonomy, meaning that one |
|
2403 * taxonomy term can't have a matching slug with another taxonomy term. Each |
|
2404 * slug has to be globally unique for every taxonomy. |
|
2405 * |
|
2406 * The way this works is that if the taxonomy that the term belongs to is |
|
2407 * hierarchical and has a parent, it will append that parent to the $slug. |
|
2408 * |
|
2409 * If that still doesn't return an unique slug, then it try to append a number |
|
2410 * until it finds a number that is truly unique. |
|
2411 * |
|
2412 * The only purpose for $term is for appending a parent, if one exists. |
|
2413 * |
|
2414 * @package WordPress |
|
2415 * @subpackage Taxonomy |
|
2416 * @since 2.3.0 |
|
2417 * @uses $wpdb |
|
2418 * |
|
2419 * @param string $slug The string that will be tried for a unique slug |
|
2420 * @param object $term The term object that the $slug will belong too |
|
2421 * @return string Will return a true unique slug. |
|
2422 */ |
|
2423 function wp_unique_term_slug($slug, $term) { |
|
2424 global $wpdb; |
|
2425 |
|
2426 if ( ! term_exists( $slug ) ) |
|
2427 return $slug; |
|
2428 |
|
2429 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique |
|
2430 // by incorporating parent slugs. |
|
2431 if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) { |
|
2432 $the_parent = $term->parent; |
|
2433 while ( ! empty($the_parent) ) { |
|
2434 $parent_term = get_term($the_parent, $term->taxonomy); |
|
2435 if ( is_wp_error($parent_term) || empty($parent_term) ) |
|
2436 break; |
|
2437 $slug .= '-' . $parent_term->slug; |
|
2438 if ( ! term_exists( $slug ) ) |
|
2439 return $slug; |
|
2440 |
|
2441 if ( empty($parent_term->parent) ) |
|
2442 break; |
|
2443 $the_parent = $parent_term->parent; |
|
2444 } |
|
2445 } |
|
2446 |
|
2447 // If we didn't get a unique slug, try appending a number to make it unique. |
|
2448 if ( ! empty( $term->term_id ) ) |
|
2449 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); |
|
2450 else |
|
2451 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
|
2452 |
|
2453 if ( $wpdb->get_var( $query ) ) { |
|
2454 $num = 2; |
|
2455 do { |
|
2456 $alt_slug = $slug . "-$num"; |
|
2457 $num++; |
|
2458 $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
|
2459 } while ( $slug_check ); |
|
2460 $slug = $alt_slug; |
|
2461 } |
|
2462 |
|
2463 return $slug; |
|
2464 } |
|
2465 |
|
2466 /** |
|
2467 * Update term based on arguments provided. |
|
2468 * |
|
2469 * The $args will indiscriminately override all values with the same field name. |
|
2470 * Care must be taken to not override important information need to update or |
|
2471 * update will fail (or perhaps create a new term, neither would be acceptable). |
|
2472 * |
|
2473 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not |
|
2474 * defined in $args already. |
|
2475 * |
|
2476 * 'alias_of' will create a term group, if it doesn't already exist, and update |
|
2477 * it for the $term. |
|
2478 * |
|
2479 * If the 'slug' argument in $args is missing, then the 'name' in $args will be |
|
2480 * used. It should also be noted that if you set 'slug' and it isn't unique then |
|
2481 * a WP_Error will be passed back. If you don't pass any slug, then a unique one |
|
2482 * will be created for you. |
|
2483 * |
|
2484 * For what can be overrode in $args, check the term scheme can contain and stay |
|
2485 * away from the term keys. |
|
2486 * |
|
2487 * @package WordPress |
|
2488 * @subpackage Taxonomy |
|
2489 * @since 2.3.0 |
|
2490 * |
|
2491 * @uses $wpdb |
|
2492 * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice. |
|
2493 * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term |
|
2494 * id and taxonomy id. |
|
2495 * |
|
2496 * @param int $term_id The ID of the term |
|
2497 * @param string $taxonomy The context in which to relate the term to the object. |
|
2498 * @param array|string $args Overwrite term field values |
|
2499 * @return array|WP_Error Returns Term ID and Taxonomy Term ID |
|
2500 */ |
|
2501 function wp_update_term( $term_id, $taxonomy, $args = array() ) { |
|
2502 global $wpdb; |
|
2503 |
|
2504 if ( ! taxonomy_exists($taxonomy) ) |
|
2505 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|
2506 |
|
2507 $term_id = (int) $term_id; |
|
2508 |
|
2509 // First, get all of the original args |
|
2510 $term = get_term ($term_id, $taxonomy, ARRAY_A); |
|
2511 |
|
2512 if ( is_wp_error( $term ) ) |
|
2513 return $term; |
|
2514 |
|
2515 // Escape data pulled from DB. |
|
2516 $term = wp_slash($term); |
|
2517 |
|
2518 // Merge old and new args with new args overwriting old ones. |
|
2519 $args = array_merge($term, $args); |
|
2520 |
|
2521 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|
2522 $args = wp_parse_args($args, $defaults); |
|
2523 $args = sanitize_term($args, $taxonomy, 'db'); |
|
2524 extract($args, EXTR_SKIP); |
|
2525 |
|
2526 // expected_slashed ($name) |
|
2527 $name = wp_unslash($name); |
|
2528 $description = wp_unslash($description); |
|
2529 |
|
2530 if ( '' == trim($name) ) |
|
2531 return new WP_Error('empty_term_name', __('A name is required for this term')); |
|
2532 |
|
2533 $empty_slug = false; |
|
2534 if ( empty($slug) ) { |
|
2535 $empty_slug = true; |
|
2536 $slug = sanitize_title($name); |
|
2537 } |
|
2538 |
|
2539 if ( $alias_of ) { |
|
2540 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); |
|
2541 if ( $alias->term_group ) { |
|
2542 // The alias we want is already in a group, so let's use that one. |
|
2543 $term_group = $alias->term_group; |
|
2544 } else { |
|
2545 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|
2546 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; |
|
2547 do_action( 'edit_terms', $alias->term_id, $taxonomy ); |
|
2548 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); |
|
2549 do_action( 'edited_terms', $alias->term_id, $taxonomy ); |
|
2550 } |
|
2551 } |
|
2552 |
|
2553 // Check $parent to see if it will cause a hierarchy loop |
|
2554 $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args ); |
|
2555 |
|
2556 // Check for duplicate slug |
|
2557 $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); |
|
2558 if ( $id && ($id != $term_id) ) { |
|
2559 // If an empty slug was passed or the parent changed, reset the slug to something unique. |
|
2560 // Otherwise, bail. |
|
2561 if ( $empty_slug || ( $parent != $term['parent']) ) |
|
2562 $slug = wp_unique_term_slug($slug, (object) $args); |
|
2563 else |
|
2564 return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); |
|
2565 } |
|
2566 do_action( 'edit_terms', $term_id, $taxonomy ); |
|
2567 $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); |
|
2568 if ( empty($slug) ) { |
|
2569 $slug = sanitize_title($name, $term_id); |
|
2570 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
2571 } |
|
2572 do_action( 'edited_terms', $term_id, $taxonomy ); |
|
2573 |
|
2574 $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) ); |
|
2575 do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); |
|
2576 $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); |
|
2577 do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); |
|
2578 |
|
2579 do_action("edit_term", $term_id, $tt_id, $taxonomy); |
|
2580 do_action("edit_$taxonomy", $term_id, $tt_id); |
|
2581 |
|
2582 $term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|
2583 |
|
2584 clean_term_cache($term_id, $taxonomy); |
|
2585 |
|
2586 do_action("edited_term", $term_id, $tt_id, $taxonomy); |
|
2587 do_action("edited_$taxonomy", $term_id, $tt_id); |
|
2588 |
|
2589 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
2590 } |
|
2591 |
|
2592 /** |
|
2593 * Enable or disable term counting. |
|
2594 * |
|
2595 * @since 2.5.0 |
|
2596 * |
|
2597 * @param bool $defer Optional. Enable if true, disable if false. |
|
2598 * @return bool Whether term counting is enabled or disabled. |
|
2599 */ |
|
2600 function wp_defer_term_counting($defer=null) { |
|
2601 static $_defer = false; |
|
2602 |
|
2603 if ( is_bool($defer) ) { |
|
2604 $_defer = $defer; |
|
2605 // flush any deferred counts |
|
2606 if ( !$defer ) |
|
2607 wp_update_term_count( null, null, true ); |
|
2608 } |
|
2609 |
|
2610 return $_defer; |
|
2611 } |
|
2612 |
|
2613 /** |
|
2614 * Updates the amount of terms in taxonomy. |
|
2615 * |
|
2616 * If there is a taxonomy callback applied, then it will be called for updating |
|
2617 * the count. |
|
2618 * |
|
2619 * The default action is to count what the amount of terms have the relationship |
|
2620 * of term ID. Once that is done, then update the database. |
|
2621 * |
|
2622 * @package WordPress |
|
2623 * @subpackage Taxonomy |
|
2624 * @since 2.3.0 |
|
2625 * @uses $wpdb |
|
2626 * |
|
2627 * @param int|array $terms The term_taxonomy_id of the terms |
|
2628 * @param string $taxonomy The context of the term. |
|
2629 * @return bool If no terms will return false, and if successful will return true. |
|
2630 */ |
|
2631 function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) { |
|
2632 static $_deferred = array(); |
|
2633 |
|
2634 if ( $do_deferred ) { |
|
2635 foreach ( (array) array_keys($_deferred) as $tax ) { |
|
2636 wp_update_term_count_now( $_deferred[$tax], $tax ); |
|
2637 unset( $_deferred[$tax] ); |
|
2638 } |
|
2639 } |
|
2640 |
|
2641 if ( empty($terms) ) |
|
2642 return false; |
|
2643 |
|
2644 if ( !is_array($terms) ) |
|
2645 $terms = array($terms); |
|
2646 |
|
2647 if ( wp_defer_term_counting() ) { |
|
2648 if ( !isset($_deferred[$taxonomy]) ) |
|
2649 $_deferred[$taxonomy] = array(); |
|
2650 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); |
|
2651 return true; |
|
2652 } |
|
2653 |
|
2654 return wp_update_term_count_now( $terms, $taxonomy ); |
|
2655 } |
|
2656 |
|
2657 /** |
|
2658 * Perform term count update immediately. |
|
2659 * |
|
2660 * @since 2.5.0 |
|
2661 * |
|
2662 * @param array $terms The term_taxonomy_id of terms to update. |
|
2663 * @param string $taxonomy The context of the term. |
|
2664 * @return bool Always true when complete. |
|
2665 */ |
|
2666 function wp_update_term_count_now( $terms, $taxonomy ) { |
|
2667 global $wpdb; |
|
2668 |
|
2669 $terms = array_map('intval', $terms); |
|
2670 |
|
2671 $taxonomy = get_taxonomy($taxonomy); |
|
2672 if ( !empty($taxonomy->update_count_callback) ) { |
|
2673 call_user_func($taxonomy->update_count_callback, $terms, $taxonomy); |
|
2674 } else { |
|
2675 $object_types = (array) $taxonomy->object_type; |
|
2676 foreach ( $object_types as &$object_type ) { |
|
2677 if ( 0 === strpos( $object_type, 'attachment:' ) ) |
|
2678 list( $object_type ) = explode( ':', $object_type ); |
|
2679 } |
|
2680 |
|
2681 if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) { |
|
2682 // Only post types are attached to this taxonomy |
|
2683 _update_post_term_count( $terms, $taxonomy ); |
|
2684 } else { |
|
2685 // Default count updater |
|
2686 _update_generic_term_count( $terms, $taxonomy ); |
|
2687 } |
|
2688 } |
|
2689 |
|
2690 clean_term_cache($terms, '', false); |
|
2691 |
|
2692 return true; |
|
2693 } |
|
2694 |
|
2695 // |
|
2696 // Cache |
|
2697 // |
|
2698 |
|
2699 /** |
|
2700 * Removes the taxonomy relationship to terms from the cache. |
|
2701 * |
|
2702 * Will remove the entire taxonomy relationship containing term $object_id. The |
|
2703 * term IDs have to exist within the taxonomy $object_type for the deletion to |
|
2704 * take place. |
|
2705 * |
|
2706 * @package WordPress |
|
2707 * @subpackage Taxonomy |
|
2708 * @since 2.3.0 |
|
2709 * |
|
2710 * @see get_object_taxonomies() for more on $object_type |
|
2711 * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion. |
|
2712 * Passes, function params in same order. |
|
2713 * |
|
2714 * @param int|array $object_ids Single or list of term object ID(s) |
|
2715 * @param array|string $object_type The taxonomy object type |
|
2716 */ |
|
2717 function clean_object_term_cache($object_ids, $object_type) { |
|
2718 if ( !is_array($object_ids) ) |
|
2719 $object_ids = array($object_ids); |
|
2720 |
|
2721 $taxonomies = get_object_taxonomies( $object_type ); |
|
2722 |
|
2723 foreach ( $object_ids as $id ) |
|
2724 foreach ( $taxonomies as $taxonomy ) |
|
2725 wp_cache_delete($id, "{$taxonomy}_relationships"); |
|
2726 |
|
2727 do_action('clean_object_term_cache', $object_ids, $object_type); |
|
2728 } |
|
2729 |
|
2730 /** |
|
2731 * Will remove all of the term ids from the cache. |
|
2732 * |
|
2733 * @package WordPress |
|
2734 * @subpackage Taxonomy |
|
2735 * @since 2.3.0 |
|
2736 * @uses $wpdb |
|
2737 * |
|
2738 * @param int|array $ids Single or list of Term IDs |
|
2739 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context. |
|
2740 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true. |
|
2741 */ |
|
2742 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { |
|
2743 global $wpdb; |
|
2744 static $cleaned = array(); |
|
2745 |
|
2746 if ( !is_array($ids) ) |
|
2747 $ids = array($ids); |
|
2748 |
|
2749 $taxonomies = array(); |
|
2750 // If no taxonomy, assume tt_ids. |
|
2751 if ( empty($taxonomy) ) { |
|
2752 $tt_ids = array_map('intval', $ids); |
|
2753 $tt_ids = implode(', ', $tt_ids); |
|
2754 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); |
|
2755 $ids = array(); |
|
2756 foreach ( (array) $terms as $term ) { |
|
2757 $taxonomies[] = $term->taxonomy; |
|
2758 $ids[] = $term->term_id; |
|
2759 wp_cache_delete($term->term_id, $term->taxonomy); |
|
2760 } |
|
2761 $taxonomies = array_unique($taxonomies); |
|
2762 } else { |
|
2763 $taxonomies = array($taxonomy); |
|
2764 foreach ( $taxonomies as $taxonomy ) { |
|
2765 foreach ( $ids as $id ) { |
|
2766 wp_cache_delete($id, $taxonomy); |
|
2767 } |
|
2768 } |
|
2769 } |
|
2770 |
|
2771 foreach ( $taxonomies as $taxonomy ) { |
|
2772 if ( isset($cleaned[$taxonomy]) ) |
|
2773 continue; |
|
2774 $cleaned[$taxonomy] = true; |
|
2775 |
|
2776 if ( $clean_taxonomy ) { |
|
2777 wp_cache_delete('all_ids', $taxonomy); |
|
2778 wp_cache_delete('get', $taxonomy); |
|
2779 delete_option("{$taxonomy}_children"); |
|
2780 // Regenerate {$taxonomy}_children |
|
2781 _get_term_hierarchy($taxonomy); |
|
2782 } |
|
2783 |
|
2784 do_action('clean_term_cache', $ids, $taxonomy); |
|
2785 } |
|
2786 |
|
2787 wp_cache_set( 'last_changed', microtime(), 'terms' ); |
|
2788 } |
|
2789 |
|
2790 /** |
|
2791 * Retrieves the taxonomy relationship to the term object id. |
|
2792 * |
|
2793 * @package WordPress |
|
2794 * @subpackage Taxonomy |
|
2795 * @since 2.3.0 |
|
2796 * |
|
2797 * @uses wp_cache_get() Retrieves taxonomy relationship from cache |
|
2798 * |
|
2799 * @param int|array $id Term object ID |
|
2800 * @param string $taxonomy Taxonomy Name |
|
2801 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id. |
|
2802 */ |
|
2803 function get_object_term_cache($id, $taxonomy) { |
|
2804 $cache = wp_cache_get($id, "{$taxonomy}_relationships"); |
|
2805 return $cache; |
|
2806 } |
|
2807 |
|
2808 /** |
|
2809 * Updates the cache for Term ID(s). |
|
2810 * |
|
2811 * Will only update the cache for terms not already cached. |
|
2812 * |
|
2813 * The $object_ids expects that the ids be separated by commas, if it is a |
|
2814 * string. |
|
2815 * |
|
2816 * It should be noted that update_object_term_cache() is very time extensive. It |
|
2817 * is advised that the function is not called very often or at least not for a |
|
2818 * lot of terms that exist in a lot of taxonomies. The amount of time increases |
|
2819 * for each term and it also increases for each taxonomy the term belongs to. |
|
2820 * |
|
2821 * @package WordPress |
|
2822 * @subpackage Taxonomy |
|
2823 * @since 2.3.0 |
|
2824 * @uses wp_get_object_terms() Used to get terms from the database to update |
|
2825 * |
|
2826 * @param string|array $object_ids Single or list of term object ID(s) |
|
2827 * @param array|string $object_type The taxonomy object type |
|
2828 * @return null|bool Null value is given with empty $object_ids. False if |
|
2829 */ |
|
2830 function update_object_term_cache($object_ids, $object_type) { |
|
2831 if ( empty($object_ids) ) |
|
2832 return; |
|
2833 |
|
2834 if ( !is_array($object_ids) ) |
|
2835 $object_ids = explode(',', $object_ids); |
|
2836 |
|
2837 $object_ids = array_map('intval', $object_ids); |
|
2838 |
|
2839 $taxonomies = get_object_taxonomies($object_type); |
|
2840 |
|
2841 $ids = array(); |
|
2842 foreach ( (array) $object_ids as $id ) { |
|
2843 foreach ( $taxonomies as $taxonomy ) { |
|
2844 if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) { |
|
2845 $ids[] = $id; |
|
2846 break; |
|
2847 } |
|
2848 } |
|
2849 } |
|
2850 |
|
2851 if ( empty( $ids ) ) |
|
2852 return false; |
|
2853 |
|
2854 $terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id')); |
|
2855 |
|
2856 $object_terms = array(); |
|
2857 foreach ( (array) $terms as $term ) |
|
2858 $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; |
|
2859 |
|
2860 foreach ( $ids as $id ) { |
|
2861 foreach ( $taxonomies as $taxonomy ) { |
|
2862 if ( ! isset($object_terms[$id][$taxonomy]) ) { |
|
2863 if ( !isset($object_terms[$id]) ) |
|
2864 $object_terms[$id] = array(); |
|
2865 $object_terms[$id][$taxonomy] = array(); |
|
2866 } |
|
2867 } |
|
2868 } |
|
2869 |
|
2870 foreach ( $object_terms as $id => $value ) { |
|
2871 foreach ( $value as $taxonomy => $terms ) { |
|
2872 wp_cache_add( $id, $terms, "{$taxonomy}_relationships" ); |
|
2873 } |
|
2874 } |
|
2875 } |
|
2876 |
|
2877 /** |
|
2878 * Updates Terms to Taxonomy in cache. |
|
2879 * |
|
2880 * @package WordPress |
|
2881 * @subpackage Taxonomy |
|
2882 * @since 2.3.0 |
|
2883 * |
|
2884 * @param array $terms List of Term objects to change |
|
2885 * @param string $taxonomy Optional. Update Term to this taxonomy in cache |
|
2886 */ |
|
2887 function update_term_cache($terms, $taxonomy = '') { |
|
2888 foreach ( (array) $terms as $term ) { |
|
2889 $term_taxonomy = $taxonomy; |
|
2890 if ( empty($term_taxonomy) ) |
|
2891 $term_taxonomy = $term->taxonomy; |
|
2892 |
|
2893 wp_cache_add($term->term_id, $term, $term_taxonomy); |
|
2894 } |
|
2895 } |
|
2896 |
|
2897 // |
|
2898 // Private |
|
2899 // |
|
2900 |
|
2901 /** |
|
2902 * Retrieves children of taxonomy as Term IDs. |
|
2903 * |
|
2904 * @package WordPress |
|
2905 * @subpackage Taxonomy |
|
2906 * @access private |
|
2907 * @since 2.3.0 |
|
2908 * |
|
2909 * @uses update_option() Stores all of the children in "$taxonomy_children" |
|
2910 * option. That is the name of the taxonomy, immediately followed by '_children'. |
|
2911 * |
|
2912 * @param string $taxonomy Taxonomy Name |
|
2913 * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs. |
|
2914 */ |
|
2915 function _get_term_hierarchy($taxonomy) { |
|
2916 if ( !is_taxonomy_hierarchical($taxonomy) ) |
|
2917 return array(); |
|
2918 $children = get_option("{$taxonomy}_children"); |
|
2919 |
|
2920 if ( is_array($children) ) |
|
2921 return $children; |
|
2922 $children = array(); |
|
2923 $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent')); |
|
2924 foreach ( $terms as $term_id => $parent ) { |
|
2925 if ( $parent > 0 ) |
|
2926 $children[$parent][] = $term_id; |
|
2927 } |
|
2928 update_option("{$taxonomy}_children", $children); |
|
2929 |
|
2930 return $children; |
|
2931 } |
|
2932 |
|
2933 /** |
|
2934 * Get the subset of $terms that are descendants of $term_id. |
|
2935 * |
|
2936 * If $terms is an array of objects, then _get_term_children returns an array of objects. |
|
2937 * If $terms is an array of IDs, then _get_term_children returns an array of IDs. |
|
2938 * |
|
2939 * @package WordPress |
|
2940 * @subpackage Taxonomy |
|
2941 * @access private |
|
2942 * @since 2.3.0 |
|
2943 * |
|
2944 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id. |
|
2945 * @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. |
|
2946 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. |
|
2947 * @return array The subset of $terms that are descendants of $term_id. |
|
2948 */ |
|
2949 function _get_term_children($term_id, $terms, $taxonomy) { |
|
2950 $empty_array = array(); |
|
2951 if ( empty($terms) ) |
|
2952 return $empty_array; |
|
2953 |
|
2954 $term_list = array(); |
|
2955 $has_children = _get_term_hierarchy($taxonomy); |
|
2956 |
|
2957 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) |
|
2958 return $empty_array; |
|
2959 |
|
2960 foreach ( (array) $terms as $term ) { |
|
2961 $use_id = false; |
|
2962 if ( !is_object($term) ) { |
|
2963 $term = get_term($term, $taxonomy); |
|
2964 if ( is_wp_error( $term ) ) |
|
2965 return $term; |
|
2966 $use_id = true; |
|
2967 } |
|
2968 |
|
2969 if ( $term->term_id == $term_id ) |
|
2970 continue; |
|
2971 |
|
2972 if ( $term->parent == $term_id ) { |
|
2973 if ( $use_id ) |
|
2974 $term_list[] = $term->term_id; |
|
2975 else |
|
2976 $term_list[] = $term; |
|
2977 |
|
2978 if ( !isset($has_children[$term->term_id]) ) |
|
2979 continue; |
|
2980 |
|
2981 if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) |
|
2982 $term_list = array_merge($term_list, $children); |
|
2983 } |
|
2984 } |
|
2985 |
|
2986 return $term_list; |
|
2987 } |
|
2988 |
|
2989 /** |
|
2990 * Add count of children to parent count. |
|
2991 * |
|
2992 * Recalculates term counts by including items from child terms. Assumes all |
|
2993 * relevant children are already in the $terms argument. |
|
2994 * |
|
2995 * @package WordPress |
|
2996 * @subpackage Taxonomy |
|
2997 * @access private |
|
2998 * @since 2.3.0 |
|
2999 * @uses $wpdb |
|
3000 * |
|
3001 * @param array $terms List of Term IDs |
|
3002 * @param string $taxonomy Term Context |
|
3003 * @return null Will break from function if conditions are not met. |
|
3004 */ |
|
3005 function _pad_term_counts(&$terms, $taxonomy) { |
|
3006 global $wpdb; |
|
3007 |
|
3008 // This function only works for hierarchical taxonomies like post categories. |
|
3009 if ( !is_taxonomy_hierarchical( $taxonomy ) ) |
|
3010 return; |
|
3011 |
|
3012 $term_hier = _get_term_hierarchy($taxonomy); |
|
3013 |
|
3014 if ( empty($term_hier) ) |
|
3015 return; |
|
3016 |
|
3017 $term_items = array(); |
|
3018 |
|
3019 foreach ( (array) $terms as $key => $term ) { |
|
3020 $terms_by_id[$term->term_id] = & $terms[$key]; |
|
3021 $term_ids[$term->term_taxonomy_id] = $term->term_id; |
|
3022 } |
|
3023 |
|
3024 // Get the object and term ids and stick them in a lookup table |
|
3025 $tax_obj = get_taxonomy($taxonomy); |
|
3026 $object_types = esc_sql($tax_obj->object_type); |
|
3027 $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 (" . implode(',', array_keys($term_ids)) . ") AND post_type IN ('" . implode("', '", $object_types) . "') AND post_status = 'publish'"); |
|
3028 foreach ( $results as $row ) { |
|
3029 $id = $term_ids[$row->term_taxonomy_id]; |
|
3030 $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1; |
|
3031 } |
|
3032 |
|
3033 // Touch every ancestor's lookup row for each post in each term |
|
3034 foreach ( $term_ids as $term_id ) { |
|
3035 $child = $term_id; |
|
3036 while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) { |
|
3037 if ( !empty( $term_items[$term_id] ) ) |
|
3038 foreach ( $term_items[$term_id] as $item_id => $touches ) { |
|
3039 $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1; |
|
3040 } |
|
3041 $child = $parent; |
|
3042 } |
|
3043 } |
|
3044 |
|
3045 // Transfer the touched cells |
|
3046 foreach ( (array) $term_items as $id => $items ) |
|
3047 if ( isset($terms_by_id[$id]) ) |
|
3048 $terms_by_id[$id]->count = count($items); |
|
3049 } |
|
3050 |
|
3051 // |
|
3052 // Default callbacks |
|
3053 // |
|
3054 |
|
3055 /** |
|
3056 * Will update term count based on object types of the current taxonomy. |
|
3057 * |
|
3058 * Private function for the default callback for post_tag and category |
|
3059 * taxonomies. |
|
3060 * |
|
3061 * @package WordPress |
|
3062 * @subpackage Taxonomy |
|
3063 * @access private |
|
3064 * @since 2.3.0 |
|
3065 * @uses $wpdb |
|
3066 * |
|
3067 * @param array $terms List of Term taxonomy IDs |
|
3068 * @param object $taxonomy Current taxonomy object of terms |
|
3069 */ |
|
3070 function _update_post_term_count( $terms, $taxonomy ) { |
|
3071 global $wpdb; |
|
3072 |
|
3073 $object_types = (array) $taxonomy->object_type; |
|
3074 |
|
3075 foreach ( $object_types as &$object_type ) |
|
3076 list( $object_type ) = explode( ':', $object_type ); |
|
3077 |
|
3078 $object_types = array_unique( $object_types ); |
|
3079 |
|
3080 if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) { |
|
3081 unset( $object_types[ $check_attachments ] ); |
|
3082 $check_attachments = true; |
|
3083 } |
|
3084 |
|
3085 if ( $object_types ) |
|
3086 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); |
|
3087 |
|
3088 foreach ( (array) $terms as $term ) { |
|
3089 $count = 0; |
|
3090 |
|
3091 // Attachments can be 'inherit' status, we need to base count off the parent's status if so |
|
3092 if ( $check_attachments ) |
|
3093 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); |
|
3094 |
|
3095 if ( $object_types ) |
|
3096 $count += (int) $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 IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); |
|
3097 |
|
3098 do_action( 'edit_term_taxonomy', $term, $taxonomy ); |
|
3099 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|
3100 do_action( 'edited_term_taxonomy', $term, $taxonomy ); |
|
3101 } |
|
3102 } |
|
3103 |
|
3104 /** |
|
3105 * Will update term count based on number of objects. |
|
3106 * |
|
3107 * Default callback for the link_category taxonomy. |
|
3108 * |
|
3109 * @package WordPress |
|
3110 * @subpackage Taxonomy |
|
3111 * @since 3.3.0 |
|
3112 * @uses $wpdb |
|
3113 * |
|
3114 * @param array $terms List of Term taxonomy IDs |
|
3115 * @param object $taxonomy Current taxonomy object of terms |
|
3116 */ |
|
3117 function _update_generic_term_count( $terms, $taxonomy ) { |
|
3118 global $wpdb; |
|
3119 |
|
3120 foreach ( (array) $terms as $term ) { |
|
3121 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|
3122 |
|
3123 do_action( 'edit_term_taxonomy', $term, $taxonomy ); |
|
3124 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|
3125 do_action( 'edited_term_taxonomy', $term, $taxonomy ); |
|
3126 } |
|
3127 } |
|
3128 |
|
3129 /** |
|
3130 * Generates a permalink for a taxonomy term archive. |
|
3131 * |
|
3132 * @since 2.5.0 |
|
3133 * |
|
3134 * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters. |
|
3135 * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters. |
|
3136 * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID. |
|
3137 * |
|
3138 * @param object|int|string $term |
|
3139 * @param string $taxonomy (optional if $term is object) |
|
3140 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist. |
|
3141 */ |
|
3142 function get_term_link( $term, $taxonomy = '') { |
|
3143 global $wp_rewrite; |
|
3144 |
|
3145 if ( !is_object($term) ) { |
|
3146 if ( is_int($term) ) { |
|
3147 $term = get_term($term, $taxonomy); |
|
3148 } else { |
|
3149 $term = get_term_by('slug', $term, $taxonomy); |
|
3150 } |
|
3151 } |
|
3152 |
|
3153 if ( !is_object($term) ) |
|
3154 $term = new WP_Error('invalid_term', __('Empty Term')); |
|
3155 |
|
3156 if ( is_wp_error( $term ) ) |
|
3157 return $term; |
|
3158 |
|
3159 $taxonomy = $term->taxonomy; |
|
3160 |
|
3161 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); |
|
3162 |
|
3163 $slug = $term->slug; |
|
3164 $t = get_taxonomy($taxonomy); |
|
3165 |
|
3166 if ( empty($termlink) ) { |
|
3167 if ( 'category' == $taxonomy ) |
|
3168 $termlink = '?cat=' . $term->term_id; |
|
3169 elseif ( $t->query_var ) |
|
3170 $termlink = "?$t->query_var=$slug"; |
|
3171 else |
|
3172 $termlink = "?taxonomy=$taxonomy&term=$slug"; |
|
3173 $termlink = home_url($termlink); |
|
3174 } else { |
|
3175 if ( $t->rewrite['hierarchical'] ) { |
|
3176 $hierarchical_slugs = array(); |
|
3177 $ancestors = get_ancestors($term->term_id, $taxonomy); |
|
3178 foreach ( (array)$ancestors as $ancestor ) { |
|
3179 $ancestor_term = get_term($ancestor, $taxonomy); |
|
3180 $hierarchical_slugs[] = $ancestor_term->slug; |
|
3181 } |
|
3182 $hierarchical_slugs = array_reverse($hierarchical_slugs); |
|
3183 $hierarchical_slugs[] = $slug; |
|
3184 $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink); |
|
3185 } else { |
|
3186 $termlink = str_replace("%$taxonomy%", $slug, $termlink); |
|
3187 } |
|
3188 $termlink = home_url( user_trailingslashit($termlink, 'category') ); |
|
3189 } |
|
3190 // Back Compat filters. |
|
3191 if ( 'post_tag' == $taxonomy ) |
|
3192 $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); |
|
3193 elseif ( 'category' == $taxonomy ) |
|
3194 $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); |
|
3195 |
|
3196 return apply_filters('term_link', $termlink, $term, $taxonomy); |
|
3197 } |
|
3198 |
|
3199 /** |
|
3200 * Display the taxonomies of a post with available options. |
|
3201 * |
|
3202 * This function can be used within the loop to display the taxonomies for a |
|
3203 * post without specifying the Post ID. You can also use it outside the Loop to |
|
3204 * display the taxonomies for a specific post. |
|
3205 * |
|
3206 * The available defaults are: |
|
3207 * 'post' : default is 0. The post ID to get taxonomies of. |
|
3208 * 'before' : default is empty string. Display before taxonomies list. |
|
3209 * 'sep' : default is empty string. Separate every taxonomy with value in this. |
|
3210 * 'after' : default is empty string. Display this after the taxonomies list. |
|
3211 * 'template' : The template to use for displaying the taxonomy terms. |
|
3212 * |
|
3213 * @since 2.5.0 |
|
3214 * @uses get_the_taxonomies() |
|
3215 * |
|
3216 * @param array $args Override the defaults. |
|
3217 */ |
|
3218 function the_taxonomies($args = array()) { |
|
3219 $defaults = array( |
|
3220 'post' => 0, |
|
3221 'before' => '', |
|
3222 'sep' => ' ', |
|
3223 'after' => '', |
|
3224 'template' => '%s: %l.' |
|
3225 ); |
|
3226 |
|
3227 $r = wp_parse_args( $args, $defaults ); |
|
3228 extract( $r, EXTR_SKIP ); |
|
3229 |
|
3230 echo $before . join($sep, get_the_taxonomies($post, $r)) . $after; |
|
3231 } |
|
3232 |
|
3233 /** |
|
3234 * Retrieve all taxonomies associated with a post. |
|
3235 * |
|
3236 * This function can be used within the loop. It will also return an array of |
|
3237 * the taxonomies with links to the taxonomy and name. |
|
3238 * |
|
3239 * @since 2.5.0 |
|
3240 * |
|
3241 * @param int $post Optional. Post ID or will use Global Post ID (in loop). |
|
3242 * @param array $args Override the defaults. |
|
3243 * @return array |
|
3244 */ |
|
3245 function get_the_taxonomies($post = 0, $args = array() ) { |
|
3246 $post = get_post( $post ); |
|
3247 |
|
3248 $args = wp_parse_args( $args, array( |
|
3249 'template' => '%s: %l.', |
|
3250 ) ); |
|
3251 extract( $args, EXTR_SKIP ); |
|
3252 |
|
3253 $taxonomies = array(); |
|
3254 |
|
3255 if ( !$post ) |
|
3256 return $taxonomies; |
|
3257 |
|
3258 foreach ( get_object_taxonomies($post) as $taxonomy ) { |
|
3259 $t = (array) get_taxonomy($taxonomy); |
|
3260 if ( empty($t['label']) ) |
|
3261 $t['label'] = $taxonomy; |
|
3262 if ( empty($t['args']) ) |
|
3263 $t['args'] = array(); |
|
3264 if ( empty($t['template']) ) |
|
3265 $t['template'] = $template; |
|
3266 |
|
3267 $terms = get_object_term_cache($post->ID, $taxonomy); |
|
3268 if ( false === $terms ) |
|
3269 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); |
|
3270 |
|
3271 $links = array(); |
|
3272 |
|
3273 foreach ( $terms as $term ) |
|
3274 $links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>"; |
|
3275 |
|
3276 if ( $links ) |
|
3277 $taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms); |
|
3278 } |
|
3279 return $taxonomies; |
|
3280 } |
|
3281 |
|
3282 /** |
|
3283 * Retrieve all taxonomies of a post with just the names. |
|
3284 * |
|
3285 * @since 2.5.0 |
|
3286 * @uses get_object_taxonomies() |
|
3287 * |
|
3288 * @param int $post Optional. Post ID |
|
3289 * @return array |
|
3290 */ |
|
3291 function get_post_taxonomies($post = 0) { |
|
3292 $post = get_post( $post ); |
|
3293 |
|
3294 return get_object_taxonomies($post); |
|
3295 } |
|
3296 |
|
3297 /** |
|
3298 * Determine if the given object is associated with any of the given terms. |
|
3299 * |
|
3300 * The given terms are checked against the object's terms' term_ids, names and slugs. |
|
3301 * Terms given as integers will only be checked against the object's terms' term_ids. |
|
3302 * If no terms are given, determines if object is associated with any terms in the given taxonomy. |
|
3303 * |
|
3304 * @since 2.7.0 |
|
3305 * @uses get_object_term_cache() |
|
3306 * @uses wp_get_object_terms() |
|
3307 * |
|
3308 * @param int $object_id ID of the object (post ID, link ID, ...) |
|
3309 * @param string $taxonomy Single taxonomy name |
|
3310 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said |
|
3311 * @return bool|WP_Error. WP_Error on input error. |
|
3312 */ |
|
3313 function is_object_in_term( $object_id, $taxonomy, $terms = null ) { |
|
3314 if ( !$object_id = (int) $object_id ) |
|
3315 return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) ); |
|
3316 |
|
3317 $object_terms = get_object_term_cache( $object_id, $taxonomy ); |
|
3318 if ( false === $object_terms ) |
|
3319 $object_terms = wp_get_object_terms( $object_id, $taxonomy ); |
|
3320 |
|
3321 if ( is_wp_error( $object_terms ) ) |
|
3322 return $object_terms; |
|
3323 if ( empty( $object_terms ) ) |
|
3324 return false; |
|
3325 if ( empty( $terms ) ) |
|
3326 return ( !empty( $object_terms ) ); |
|
3327 |
|
3328 $terms = (array) $terms; |
|
3329 |
|
3330 if ( $ints = array_filter( $terms, 'is_int' ) ) |
|
3331 $strs = array_diff( $terms, $ints ); |
|
3332 else |
|
3333 $strs =& $terms; |
|
3334 |
|
3335 foreach ( $object_terms as $object_term ) { |
|
3336 if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id |
|
3337 if ( $strs ) { |
|
3338 if ( in_array( $object_term->term_id, $strs ) ) return true; |
|
3339 if ( in_array( $object_term->name, $strs ) ) return true; |
|
3340 if ( in_array( $object_term->slug, $strs ) ) return true; |
|
3341 } |
|
3342 } |
|
3343 |
|
3344 return false; |
|
3345 } |
|
3346 |
|
3347 /** |
|
3348 * Determine if the given object type is associated with the given taxonomy. |
|
3349 * |
|
3350 * @since 3.0.0 |
|
3351 * @uses get_object_taxonomies() |
|
3352 * |
|
3353 * @param string $object_type Object type string |
|
3354 * @param string $taxonomy Single taxonomy name |
|
3355 * @return bool True if object is associated with the taxonomy, otherwise false. |
|
3356 */ |
|
3357 function is_object_in_taxonomy($object_type, $taxonomy) { |
|
3358 $taxonomies = get_object_taxonomies($object_type); |
|
3359 |
|
3360 if ( empty($taxonomies) ) |
|
3361 return false; |
|
3362 |
|
3363 if ( in_array($taxonomy, $taxonomies) ) |
|
3364 return true; |
|
3365 |
|
3366 return false; |
|
3367 } |
|
3368 |
|
3369 /** |
|
3370 * Get an array of ancestor IDs for a given object. |
|
3371 * |
|
3372 * @param int $object_id The ID of the object |
|
3373 * @param string $object_type The type of object for which we'll be retrieving ancestors. |
|
3374 * @return array of ancestors from lowest to highest in the hierarchy. |
|
3375 */ |
|
3376 function get_ancestors($object_id = 0, $object_type = '') { |
|
3377 $object_id = (int) $object_id; |
|
3378 |
|
3379 $ancestors = array(); |
|
3380 |
|
3381 if ( empty( $object_id ) ) { |
|
3382 return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); |
|
3383 } |
|
3384 |
|
3385 if ( is_taxonomy_hierarchical( $object_type ) ) { |
|
3386 $term = get_term($object_id, $object_type); |
|
3387 while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { |
|
3388 $ancestors[] = (int) $term->parent; |
|
3389 $term = get_term($term->parent, $object_type); |
|
3390 } |
|
3391 } elseif ( post_type_exists( $object_type ) ) { |
|
3392 $ancestors = get_post_ancestors($object_id); |
|
3393 } |
|
3394 |
|
3395 return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); |
|
3396 } |
|
3397 |
|
3398 /** |
|
3399 * Returns the term's parent's term_ID |
|
3400 * |
|
3401 * @since 3.1.0 |
|
3402 * |
|
3403 * @param int $term_id |
|
3404 * @param string $taxonomy |
|
3405 * |
|
3406 * @return int|bool false on error |
|
3407 */ |
|
3408 function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) { |
|
3409 $term = get_term( $term_id, $taxonomy ); |
|
3410 if ( !$term || is_wp_error( $term ) ) |
|
3411 return false; |
|
3412 return (int) $term->parent; |
|
3413 } |
|
3414 |
|
3415 /** |
|
3416 * Checks the given subset of the term hierarchy for hierarchy loops. |
|
3417 * Prevents loops from forming and breaks those that it finds. |
|
3418 * |
|
3419 * Attached to the wp_update_term_parent filter. |
|
3420 * |
|
3421 * @since 3.1.0 |
|
3422 * @uses wp_find_hierarchy_loop() |
|
3423 * |
|
3424 * @param int $parent term_id of the parent for the term we're checking. |
|
3425 * @param int $term_id The term we're checking. |
|
3426 * @param string $taxonomy The taxonomy of the term we're checking. |
|
3427 * |
|
3428 * @return int The new parent for the term. |
|
3429 */ |
|
3430 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { |
|
3431 // Nothing fancy here - bail |
|
3432 if ( !$parent ) |
|
3433 return 0; |
|
3434 |
|
3435 // Can't be its own parent |
|
3436 if ( $parent == $term_id ) |
|
3437 return 0; |
|
3438 |
|
3439 // Now look for larger loops |
|
3440 |
|
3441 if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) |
|
3442 return $parent; // No loop |
|
3443 |
|
3444 // Setting $parent to the given value causes a loop |
|
3445 if ( isset( $loop[$term_id] ) ) |
|
3446 return 0; |
|
3447 |
|
3448 // There's a loop, but it doesn't contain $term_id. Break the loop. |
|
3449 foreach ( array_keys( $loop ) as $loop_member ) |
|
3450 wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); |
|
3451 |
|
3452 return $parent; |
|
3453 } |