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