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