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