Changes from branches/3.0/wp-includes/taxonomy.php at r15426 to trunk/wp-includes/taxonomy.php at r17443
- File:
-
- 1 edited
-
trunk/wp-includes/taxonomy.php (modified) (42 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/taxonomy.php
r15426 r17443 19 19 'hierarchical' => true, 20 20 'update_count_callback' => '_update_post_term_count', 21 'query_var' => false, 22 'rewrite' => false, 23 'public' => true, 24 'show_ui' => true, 25 '_builtin' => true, 26 ) ) ; 27 28 register_taxonomy( 'post_tag', 'post', array( 29 'hierarchical' => false, 30 'update_count_callback' => '_update_post_term_count', 31 'query_var' => false, 32 'rewrite' => false, 21 'query_var' => 'category_name', 22 'rewrite' => did_action( 'init' ) ? array( 23 'hierarchical' => true, 24 'slug' => get_option('category_base') ? get_option('category_base') : 'category', 25 'with_front' => false) : false, 33 26 'public' => true, 34 27 'show_ui' => true, … … 36 29 ) ); 37 30 31 register_taxonomy( 'post_tag', 'post', array( 32 'hierarchical' => false, 33 'update_count_callback' => '_update_post_term_count', 34 'query_var' => 'tag', 35 'rewrite' => did_action( 'init' ) ? array( 36 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag', 37 'with_front' => false) : false, 38 'public' => true, 39 'show_ui' => true, 40 '_builtin' => true, 41 ) ); 42 38 43 register_taxonomy( 'nav_menu', 'nav_menu_item', array( 44 'public' => false, 39 45 'hierarchical' => false, 40 46 'labels' => array( … … 47 53 '_builtin' => true, 48 54 'show_in_nav_menus' => false, 49 ) ) ;55 ) ); 50 56 51 57 register_taxonomy( 'link_category', 'link', array( 52 58 'hierarchical' => false, 53 59 'labels' => array( 54 'name' => __( 'Categories' ), 55 'singular_name' => __( 'Category' ), 56 'update_item' => __( 'Update Category' ), 60 'name' => __( 'Link Categories' ), 61 'singular_name' => __( 'Link Category' ), 62 'search_items' => __( 'Search Link Categories' ), 63 'popular_items' => null, 64 'all_items' => __( 'All Link Categories' ), 65 'edit_item' => __( 'Edit Link Category' ), 66 'update_item' => __( 'Update Link Category' ), 67 'add_new_item' => __( 'Add New Link Category' ), 68 'new_item_name' => __( 'New Link Category Name' ), 69 'separate_items_with_commas' => null, 70 'add_or_remove_items' => null, 71 'choose_from_most_used' => null, 57 72 ), 58 73 'query_var' => false, … … 61 76 'show_ui' => false, 62 77 '_builtin' => true, 63 ) ) ; 78 ) ); 79 80 $rewrite = false; 81 if ( did_action( 'init' ) ) { 82 $rewrite = apply_filters( 'post_format_rewrite_base', 'type' ); 83 $rewrite = $rewrite ? array( 'slug' => $rewrite ) : false; 84 } 85 86 register_taxonomy( 'post_format', 'post', array( 87 'public' => true, 88 'hierarchical' => false, 89 'labels' => array( 90 'name' => _x( 'Format', 'post format' ), 91 'singular_name' => _x( 'Format', 'post format' ), 92 ), 93 'query_var' => true, 94 'rewrite' => $rewrite, 95 'show_ui' => false, 96 '_builtin' => true, 97 'show_in_nav_menus' => false, 98 ) ); 64 99 } 65 100 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority … … 286 321 'slug' => sanitize_title_with_dashes($taxonomy), 287 322 'with_front' => true, 323 'hierarchical' => false 288 324 )); 289 $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term="); 290 $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']); 325 326 if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] ) 327 $tag = '(.+?)'; 328 else 329 $tag = '([^/]+)'; 330 331 $wp_rewrite->add_rewrite_tag("%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term="); 332 $wp_rewrite->add_permastruct($taxonomy, "{$wp_rewrite->root}{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']); 291 333 } 292 334 … … 319 361 320 362 // register callback handling for metabox 321 add_filter('wp_ajax_add-' .$taxonomy, '_wp_ajax_add_hierarchical_term');363 add_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term'); 322 364 } 323 365 … … 368 410 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ), 369 411 ); 412 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; 370 413 371 414 return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults ); … … 461 504 462 505 return $object_ids; 506 } 507 508 /** 509 * Given a taxonomy query, generates SQL to be appended to a main query. 510 * 511 * @since 3.1.0 512 * 513 * @see WP_Tax_Query 514 * 515 * @param array $tax_query A compact tax query 516 * @param string $primary_table 517 * @param string $primary_id_column 518 * @return array 519 */ 520 function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { 521 $tax_query_obj = new WP_Tax_Query( $tax_query ); 522 return $tax_query_obj->get_sql( $primary_table, $primary_id_column ); 523 } 524 525 /** 526 * Container class for a multiple taxonomy query. 527 * 528 * @since 3.1.0 529 */ 530 class WP_Tax_Query { 531 532 /** 533 * List of taxonomy queries. A single taxonomy query is an associative array: 534 * - 'taxonomy' string The taxonomy being queried 535 * - 'terms' string|array The list of terms 536 * - 'field' string (optional) Which term field is being used. 537 * Possible values: 'term_id', 'slug' or 'name' 538 * Default: 'term_id' 539 * - 'operator' string (optional) 540 * Possible values: 'IN' and 'NOT IN'. 541 * Default: 'IN' 542 * - 'include_children' bool (optional) Whether to include child terms. 543 * Default: true 544 * 545 * @since 3.1.0 546 * @access public 547 * @var array 548 */ 549 var $queries = array(); 550 551 /** 552 * The relation between the queries. Can be one of 'AND' or 'OR'. 553 * 554 * @since 3.1.0 555 * @access public 556 * @var string 557 */ 558 var $relation; 559 560 /** 561 * PHP4 type constructor. 562 * 563 * Parses a compact tax query and sets defaults. 564 * 565 * @since 3.1.0 566 * @access public 567 * 568 * @param array $tax_query A compact tax query: 569 * array( 570 * 'relation' => 'OR', 571 * array( 572 * 'taxonomy' => 'tax1', 573 * 'terms' => array( 'term1', 'term2' ), 574 * 'field' => 'slug', 575 * ), 576 * array( 577 * 'taxonomy' => 'tax2', 578 * 'terms' => array( 'term-a', 'term-b' ), 579 * 'field' => 'slug', 580 * ), 581 * ) 582 * 583 * @return WP_Tax_Query 584 */ 585 function WP_Tax_Query( $tax_query ) { 586 if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) { 587 $this->relation = 'OR'; 588 } else { 589 $this->relation = 'AND'; 590 } 591 592 $defaults = array( 593 'taxonomy' => '', 594 'terms' => array(), 595 'include_children' => true, 596 'field' => 'term_id', 597 'operator' => 'IN', 598 ); 599 600 foreach ( $tax_query as $query ) { 601 if ( ! is_array( $query ) ) 602 continue; 603 604 $query = array_merge( $defaults, $query ); 605 606 $query['terms'] = (array) $query['terms']; 607 608 $this->queries[] = $query; 609 } 610 } 611 612 /** 613 * Generates SQL clauses to be appended to a main query. 614 * 615 * @since 3.1.0 616 * @access public 617 * 618 * @param string $primary_table 619 * @param string $primary_id_column 620 * @return array 621 */ 622 function get_sql( $primary_table, $primary_id_column ) { 623 global $wpdb; 624 625 $join = ''; 626 $where = array(); 627 $i = 0; 628 629 foreach ( $this->queries as $query ) { 630 extract( $query ); 631 632 if ( ! taxonomy_exists( $taxonomy ) ) 633 return array( 'join' => '', 'where' => ' AND 0 = 1'); 634 635 $terms = array_unique( (array) $terms ); 636 637 if ( empty( $terms ) ) 638 continue; 639 640 if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) { 641 $this->_transform_terms( $terms, $taxonomy, $field, 'term_id' ); 642 643 $children = array(); 644 foreach ( $terms as $term ) { 645 $children = array_merge( $children, get_term_children( $term, $taxonomy ) ); 646 $children[] = $term; 647 } 648 $terms = $children; 649 650 $this->_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' ); 651 } 652 else { 653 $this->_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' ); 654 } 655 656 if ( 'IN' == $operator ) { 657 658 if ( empty( $terms ) ) { 659 if ( 'OR' == $this->relation ) 660 continue; 661 else 662 return array( 'join' => '', 'where' => ' AND 0 = 1' ); 663 } 664 665 $terms = implode( ',', $terms ); 666 667 $alias = $i ? 'tt' . $i : $wpdb->term_relationships; 668 669 $join .= " INNER JOIN $wpdb->term_relationships"; 670 $join .= $i ? " AS $alias" : ''; 671 $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)"; 672 673 $where[] = "$alias.term_taxonomy_id $operator ($terms)"; 674 } elseif ( 'NOT IN' == $operator ) { 675 676 if ( empty( $terms ) ) 677 continue; 678 679 $terms = implode( ',', $terms ); 680 681 $where[] = "$primary_table.$primary_id_column NOT IN ( 682 SELECT object_id 683 FROM $wpdb->term_relationships 684 WHERE term_taxonomy_id IN ($terms) 685 )"; 686 } elseif ( 'AND' == $operator ) { 687 688 if ( empty( $terms ) ) 689 continue; 690 691 $num_terms = count( $terms ); 692 693 $terms = implode( ',', $terms ); 694 695 $where[] = "$primary_table.$primary_id_column IN ( 696 SELECT object_id 697 FROM $wpdb->term_relationships 698 WHERE term_taxonomy_id IN ($terms) 699 GROUP BY object_id HAVING COUNT(object_id) = $num_terms 700 )"; 701 } 702 703 $i++; 704 } 705 706 if ( !empty( $where ) ) 707 $where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )'; 708 else 709 $where = ''; 710 711 return compact( 'join', 'where' ); 712 } 713 714 /** 715 * Transforms a list of terms, from one field to another. 716 * 717 * @since 3.1.0 718 * @access private 719 * 720 * @param array &$terms The list of terms 721 * @param string $taxonomy The taxonomy of the terms 722 * @param string $field The initial field 723 * @param string $resulting_field The resulting field 724 */ 725 function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) { 726 global $wpdb; 727 728 if ( empty( $terms ) ) 729 return; 730 731 if ( $field == $resulting_field ) 732 return; 733 734 $resulting_field = esc_sql( $resulting_field ); 735 736 switch ( $field ) { 737 case 'slug': 738 case 'name': 739 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'"; 740 $terms = $wpdb->get_col( " 741 SELECT $wpdb->term_taxonomy.$resulting_field 742 FROM $wpdb->term_taxonomy 743 INNER JOIN $wpdb->terms USING (term_id) 744 WHERE taxonomy = '$taxonomy' 745 AND $wpdb->terms.$field IN ($terms) 746 " ); 747 break; 748 749 default: 750 $terms = implode( ',', array_map( 'intval', $terms ) ); 751 $terms = $wpdb->get_col( " 752 SELECT $resulting_field 753 FROM $wpdb->term_taxonomy 754 WHERE taxonomy = '$taxonomy' 755 AND term_id IN ($terms) 756 " ); 757 } 758 } 463 759 } 464 760 … … 632 928 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term 633 929 * 634 * @param string $term ID of Term to get children930 * @param string $term_id ID of Term to get children 635 931 * @param string $taxonomy Taxonomy Name 636 932 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist … … 807 1103 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings. 808 1104 * 809 * @param string|array Taxonomy name or list of Taxonomy names1105 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names 810 1106 * @param string|array $args The values of what to search for when returning terms 811 1107 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. … … 821 1117 } 822 1118 823 foreach ( (array)$taxonomies as $taxonomy ) {1119 foreach ( $taxonomies as $taxonomy ) { 824 1120 if ( ! taxonomy_exists($taxonomy) ) { 825 1121 $error = & new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); … … 827 1123 } 828 1124 } 829 830 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";831 1125 832 1126 $defaults = array('orderby' => 'name', 'order' => 'ASC', … … 851 1145 $args['pad_counts'] = false; 852 1146 } 1147 1148 $args = apply_filters( 'get_terms_args', $args, $taxonomies ); 1149 853 1150 extract($args, EXTR_SKIP); 854 1151 … … 901 1198 $order = ''; 902 1199 903 $where = '';1200 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; 904 1201 $inclusions = ''; 905 1202 if ( !empty($include) ) { … … 923 1220 $excluded_trunks = wp_parse_id_list($exclude_tree); 924 1221 foreach ( $excluded_trunks as $extrunk ) { 925 $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids' ));1222 $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0)); 926 1223 $excluded_children[] = $extrunk; 927 1224 foreach( $excluded_children as $exterm ) { … … 955 1252 956 1253 if ( !empty($name__like) ) 957 $where .= " AND t.name LIKE ' {$name__like}%'";1254 $where .= " AND t.name LIKE '" . like_escape( $name__like ) . "%'"; 958 1255 959 1256 if ( '' !== $parent ) { … … 968 1265 if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) { 969 1266 if ( $offset ) 970 $limit = 'LIMIT ' . $offset . ',' . $number;1267 $limits = 'LIMIT ' . $offset . ',' . $number; 971 1268 else 972 $limit = 'LIMIT ' . $number;1269 $limits = 'LIMIT ' . $number; 973 1270 } else { 974 $limit = '';1271 $limits = ''; 975 1272 } 976 1273 … … 982 1279 $selects = array(); 983 1280 switch ( $fields ) { 984 case 'all':985 $selects = array('t.*', 'tt.*');986 break;987 case 'ids':1281 case 'all': 1282 $selects = array('t.*', 'tt.*'); 1283 break; 1284 case 'ids': 988 1285 case 'id=>parent': 989 $selects = array('t.term_id', 'tt.parent', 'tt.count');990 break;991 case 'names':992 $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');993 break;994 case 'count':1286 $selects = array('t.term_id', 'tt.parent', 'tt.count'); 1287 break; 1288 case 'names': 1289 $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); 1290 break; 1291 case 'count': 995 1292 $orderby = ''; 996 1293 $order = ''; 997 $selects = array('COUNT(*)'); 998 } 999 $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); 1000 1001 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where $orderby $order $limit"; 1294 $selects = array('COUNT(*)'); 1295 } 1296 1297 $_fields = $fields; 1298 1299 $fields = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); 1300 1301 $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 1302 1303 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); 1304 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); 1305 foreach ( $pieces as $piece ) 1306 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 1307 1308 $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; 1309 1310 $fields = $_fields; 1002 1311 1003 1312 if ( 'count' == $fields ) { … … 1012 1321 1013 1322 if ( empty($terms) ) { 1014 wp_cache_add( $cache_key, array(), 'terms' );1323 wp_cache_add( $cache_key, array(), 'terms', 86400 ); // one day 1015 1324 $terms = apply_filters('get_terms', array(), $taxonomies, $args); 1016 1325 return $terms; … … 1063 1372 } 1064 1373 1065 wp_cache_add( $cache_key, $terms, 'terms' );1374 wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day 1066 1375 1067 1376 $terms = apply_filters('get_terms', $terms, $taxonomies, $args); … … 1224 1533 1225 1534 if ( 'edit' == $context ) { 1226 $value = apply_filters("edit_term_ $field", $value, $term_id, $taxonomy);1227 $value = apply_filters("edit_ ${taxonomy}_$field", $value, $term_id);1535 $value = apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy); 1536 $value = apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id); 1228 1537 if ( 'description' == $field ) 1229 $value = format_to_edit($value);1538 $value = esc_html($value); // textarea_escaped 1230 1539 else 1231 1540 $value = esc_attr($value); 1232 1541 } else if ( 'db' == $context ) { 1233 $value = apply_filters("pre_term_ $field", $value, $taxonomy);1234 $value = apply_filters("pre_ ${taxonomy}_$field", $value);1542 $value = apply_filters("pre_term_{$field}", $value, $taxonomy); 1543 $value = apply_filters("pre_{$taxonomy}_{$field}", $value); 1235 1544 // Back compat filters 1236 1545 if ( 'slug' == $field ) … … 1238 1547 1239 1548 } else if ( 'rss' == $context ) { 1240 $value = apply_filters("term_ ${field}_rss", $value, $taxonomy);1241 $value = apply_filters(" ${taxonomy}_${field}_rss", $value);1549 $value = apply_filters("term_{$field}_rss", $value, $taxonomy); 1550 $value = apply_filters("{$taxonomy}_{$field}_rss", $value); 1242 1551 } else { 1243 1552 // Use display filters by default. 1244 $value = apply_filters("term_ $field", $value, $term_id, $taxonomy, $context);1245 $value = apply_filters(" ${taxonomy}_$field", $value, $term_id, $context);1553 $value = apply_filters("term_{$field}", $value, $term_id, $taxonomy, $context); 1554 $value = apply_filters("{$taxonomy}_{$field}", $value, $term_id, $context); 1246 1555 } 1247 1556 … … 1286 1595 1287 1596 /** 1288 * Will unlink the term from the taxonomy.1289 * 1290 * Will remove the term's relationship to the taxonomy, not the term or taxonomy1291 * itself. The term and taxonomy will still exist. Will require the term's1292 * object ID to perform the operation.1597 * Will unlink the object from the taxonomy or taxonomies. 1598 * 1599 * Will remove all relationships between the object and any terms in 1600 * a particular taxonomy or taxonomies. Does not remove the term or 1601 * taxonomy itself. 1293 1602 * 1294 1603 * @package WordPress … … 1298 1607 * 1299 1608 * @param int $object_id The term Object Id that refers to the term 1300 * @param string|array $taxonom yList of Taxonomy Names or single Taxonomy name.1609 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. 1301 1610 */ 1302 1611 function wp_delete_object_term_relationships( $object_id, $taxonomies ) { … … 1356 1665 1357 1666 $defaults = array(); 1667 1668 if ( 'category' == $taxonomy ) { 1669 $defaults['default'] = get_option( 'default_category' ); 1670 if ( $defaults['default'] == $term ) 1671 return 0; // Don't delete the default category 1672 } 1673 1358 1674 $args = wp_parse_args($args, $defaults); 1359 1675 extract($args, EXTR_SKIP); 1360 1676 1361 if ( isset( $default) ) {1677 if ( isset( $default ) ) { 1362 1678 $default = (int) $default; 1363 1679 if ( ! term_exists($default, $taxonomy) ) … … 1393 1709 } 1394 1710 1711 // Clean the relationship caches for all object types using this term 1712 $tax_object = get_taxonomy( $taxonomy ); 1713 foreach ( $tax_object->object_type as $object_type ) 1714 clean_object_term_cache( $objects, $object_type ); 1715 1395 1716 do_action( 'delete_term_taxonomy', $tt_id ); 1396 1717 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) ); … … 1407 1728 1408 1729 return true; 1730 } 1731 1732 /** 1733 * Deletes one existing category. 1734 * 1735 * @since 2.0.0 1736 * @uses wp_delete_term() 1737 * 1738 * @param int $cat_ID 1739 * @return mixed Returns true if completes delete action; false if term doesnt exist; 1740 * Zero on attempted deletion of default Category; WP_Error object is also a possibility. 1741 */ 1742 function wp_delete_category( $cat_ID ) { 1743 return wp_delete_term( $cat_ID, 'category' ); 1409 1744 } 1410 1745 … … 1436 1771 * @uses $wpdb 1437 1772 * 1438 * @param int|array $object_id The idof the object(s) to retrieve.1773 * @param int|array $object_ids The ID(s) of the object(s) to retrieve. 1439 1774 * @param string|array $taxonomies The taxonomies to retrieve terms from. 1440 1775 * @param array|string $args Change what is returned … … 1706 2041 * 1707 2042 * @param int $object_id The object to relate to. 1708 * @param array|int|string $term The slug or id of the term, will replace all existing2043 * @param array|int|string $terms The slug or id of the term, will replace all existing 1709 2044 * related terms in this taxonomy. 1710 2045 * @param array|string $taxonomy The context in which to relate the term to the object. … … 1937 2272 } 1938 2273 2274 // Check $parent to see if it will cause a hierarchy loop 2275 $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args ); 2276 1939 2277 // Check for duplicate slug 1940 2278 $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); … … 2472 2810 * @since 2.5.0 2473 2811 * 2812 * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters. 2813 * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters. 2814 * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID. 2815 * 2474 2816 * @param object|int|string $term 2475 * @param string $taxonomy 2476 * @return string HTML link to taxonomy term archive2477 */ 2478 function get_term_link( $term, $taxonomy ) {2817 * @param string $taxonomy (optional if $term is object) 2818 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist. 2819 */ 2820 function get_term_link( $term, $taxonomy = '') { 2479 2821 global $wp_rewrite; 2480 2822 … … 2493 2835 return $term; 2494 2836 2495 // use legacy functions for core taxonomies until they are fully plugged in 2496 if ( $taxonomy == 'category' ) 2497 return get_category_link((int) $term->term_id); 2498 if ( $taxonomy == 'post_tag' ) 2499 return get_tag_link((int) $term->term_id); 2837 $taxonomy = $term->taxonomy; 2500 2838 2501 2839 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); 2502 2840 2503 2841 $slug = $term->slug; 2842 $t = get_taxonomy($taxonomy); 2504 2843 2505 2844 if ( empty($termlink) ) { 2506 $t = get_taxonomy($taxonomy);2507 2845 if ( $t->query_var ) 2508 2846 $termlink = "?$t->query_var=$slug"; … … 2511 2849 $termlink = home_url($termlink); 2512 2850 } else { 2513 $termlink = str_replace("%$taxonomy%", $slug, $termlink); 2851 if ( $t->rewrite['hierarchical'] ) { 2852 $hierarchical_slugs = array(); 2853 $ancestors = get_ancestors($term->term_id, $taxonomy); 2854 foreach ( (array)$ancestors as $ancestor ) { 2855 $ancestor_term = get_term($ancestor, $taxonomy); 2856 $hierarchical_slugs[] = $ancestor_term->slug; 2857 } 2858 $hierarchical_slugs = array_reverse($hierarchical_slugs); 2859 $hierarchical_slugs[] = $slug; 2860 $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink); 2861 } else { 2862 $termlink = str_replace("%$taxonomy%", $slug, $termlink); 2863 } 2514 2864 $termlink = home_url( user_trailingslashit($termlink, 'category') ); 2515 2865 } 2866 // Back Compat filters. 2867 if ( 'post_tag' == $taxonomy ) 2868 $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); 2869 elseif ( 'category' == $taxonomy ) 2870 $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); 2871 2516 2872 return apply_filters('term_link', $termlink, $term, $taxonomy); 2517 2873 } … … 2529 2885 * 'sep' : default is empty string. Separate every taxonomy with value in this. 2530 2886 * 'after' : default is empty string. Display this after the taxonomies list. 2887 * 'template' : The template to use for displaying the taxonomy terms. 2531 2888 * 2532 2889 * @since 2.5.0 … … 2541 2898 'sep' => ' ', 2542 2899 'after' => '', 2900 'template' => '%s: %l.' 2543 2901 ); 2544 2902 … … 2546 2904 extract( $r, EXTR_SKIP ); 2547 2905 2548 echo $before . join($sep, get_the_taxonomies($post )) . $after;2906 echo $before . join($sep, get_the_taxonomies($post, $r)) . $after; 2549 2907 } 2550 2908 … … 2558 2916 * 2559 2917 * @param int $post Optional. Post ID or will use Global Post ID (in loop). 2918 * @param array $args Override the defaults. 2560 2919 * @return array 2561 2920 */ 2562 function get_the_taxonomies($post = 0 ) {2921 function get_the_taxonomies($post = 0, $args = array() ) { 2563 2922 if ( is_int($post) ) 2564 2923 $post =& get_post($post); … … 2566 2925 $post =& $GLOBALS['post']; 2567 2926 2927 $args = wp_parse_args( $args, array( 2928 'template' => '%s: %l.', 2929 ) ); 2930 extract( $args, EXTR_SKIP ); 2931 2568 2932 $taxonomies = array(); 2569 2933 2570 2934 if ( !$post ) 2571 2935 return $taxonomies; 2572 2573 $template = apply_filters('taxonomy_template', '%s: %l.');2574 2936 2575 2937 foreach ( get_object_taxonomies($post) as $taxonomy ) { … … 2589 2951 2590 2952 foreach ( $terms as $term ) 2591 $links[] = "<a href='" . esc_attr( get_term_link($term, $taxonomy)) . "'>$term->name</a>";2953 $links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>"; 2592 2954 2593 2955 if ( $links ) … … 2623 2985 * @uses wp_get_object_terms() 2624 2986 * 2625 * @param int $object_id .ID of the object (post ID, link ID, ...)2626 * @param string $taxonomy .Single taxonomy name2987 * @param int $object_id ID of the object (post ID, link ID, ...) 2988 * @param string $taxonomy Single taxonomy name 2627 2989 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said 2628 2990 * @return bool|WP_Error. WP_Error on input error. … … 2669 3031 * 2670 3032 * @param string $object_type Object type string 2671 * @param string $taxonomy .Single taxonomy name3033 * @param string $taxonomy Single taxonomy name 2672 3034 * @return bool True if object is associated with the taxonomy, otherwise false. 2673 3035 */ … … 2683 3045 return false; 2684 3046 } 3047 3048 /** 3049 * Get an array of ancestor IDs for a given object. 3050 * 3051 * @param int $object_id The ID of the object 3052 * @param string $object_type The type of object for which we'll be retrieving ancestors. 3053 * @return array of ancestors from lowest to highest in the hierarchy. 3054 */ 3055 function get_ancestors($object_id = 0, $object_type = '') { 3056 $object_id = (int) $object_id; 3057 3058 $ancestors = array(); 3059 3060 if ( empty( $object_id ) ) { 3061 return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); 3062 } 3063 3064 if ( is_taxonomy_hierarchical( $object_type ) ) { 3065 $term = get_term($object_id, $object_type); 3066 while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { 3067 $ancestors[] = (int) $term->parent; 3068 $term = get_term($term->parent, $object_type); 3069 } 3070 } elseif ( null !== get_post_type_object( $object_type ) ) { 3071 $object = get_post($object_id); 3072 if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) ) 3073 $ancestors = $object->ancestors; 3074 else { 3075 while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) { 3076 $ancestors[] = (int) $object->post_parent; 3077 $object = get_post($object->post_parent); 3078 } 3079 } 3080 } 3081 3082 return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); 3083 } 3084 3085 /** 3086 * Returns the term's parent's term_ID 3087 * 3088 * @since 3.1.0 3089 * 3090 * @param int $term_id 3091 * @param string $taxonomy 3092 * 3093 * @return int|bool false on error 3094 */ 3095 function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) { 3096 $term = get_term( $term_id, $taxonomy ); 3097 if ( !$term || is_wp_error( $term ) ) 3098 return false; 3099 return (int) $term->parent; 3100 } 3101 3102 /** 3103 * Checks the given subset of the term hierarchy for hierarchy loops. 3104 * Prevents loops from forming and breaks those that it finds. 3105 * 3106 * Attached to the wp_update_term_parent filter. 3107 * 3108 * @since 3.1.0 3109 * @uses wp_find_hierarchy_loop() 3110 * 3111 * @param int $parent term_id of the parent for the term we're checking. 3112 * @param int $term_id The term we're checking. 3113 * @param string $taxonomy The taxonomy of the term we're checking. 3114 * 3115 * @return int The new parent for the term. 3116 */ 3117 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { 3118 // Nothing fancy here - bail 3119 if ( !$parent ) 3120 return 0; 3121 3122 // Can't be its own parent 3123 if ( $parent == $term_id ) 3124 return 0; 3125 3126 // Now look for larger loops 3127 3128 if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) 3129 return $parent; // No loop 3130 3131 // Setting $parent to the given value causes a loop 3132 if ( isset( $loop[$term_id] ) ) 3133 return 0; 3134 3135 // There's a loop, but it doesn't contain $term_id. Break the loop. 3136 foreach ( array_keys( $loop ) as $loop_member ) 3137 wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); 3138 3139 return $parent; 3140 }
Note: See TracChangeset
for help on using the changeset viewer.