Ticket #15242: 15242.2.diff
| File 15242.2.diff, 48.4 KB (added by , 16 years ago) |
|---|
-
wp-includes/taxonomy.php
21 21 'query_var' => 'category_name', 22 22 'rewrite' => array( 23 23 'hierarchical' => true, 24 'slug' => get_option('category_base') ? get_option('category_base') : 'category', 25 'with_front' => false), 24 'slug' => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category', 25 'with_front' => false 26 ), 26 27 'public' => true, 27 28 'show_ui' => true, 28 29 '_builtin' => true, … … 33 34 'update_count_callback' => '_update_post_term_count', 34 35 'query_var' => 'tag', 35 36 'rewrite' => array( 36 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag' , 37 'with_front' => false), 37 'slug' => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag', 38 'with_front' => false 39 ), 38 40 'public' => true, 39 41 'show_ui' => true, 40 42 '_builtin' => true, … … 52 54 'show_ui' => false, 53 55 '_builtin' => true, 54 56 'show_in_nav_menus' => false, 55 ) ) ;57 ) ); 56 58 57 59 register_taxonomy( 'link_category', 'link', array( 58 60 'hierarchical' => false, … … 75 77 'public' => false, 76 78 'show_ui' => false, 77 79 '_builtin' => true, 78 ) ) ;80 ) ); 79 81 80 register_taxonomy( 'post_format', array( 'post', 'page', 'attachment'), array(82 register_taxonomy( 'post_format', array( 'post', 'page', 'attachment' ), array( 81 83 'public' => false, 82 84 'hierarchical' => false, 83 85 'labels' => array( … … 89 91 'show_ui' => false, 90 92 '_builtin' => true, 91 93 'show_in_nav_menus' => false, 92 ) ) ;94 ) ); 93 95 } 94 96 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority 95 97 … … 111 113 function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) { 112 114 global $wp_taxonomies; 113 115 114 $field = ( 'names' == $output) ? 'name' : false;116 $field = ( 'names' == $output ) ? 'name' : false; 115 117 116 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field);118 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field ); 117 119 } 118 120 119 121 … … 136 138 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default. 137 139 * @return array The names of all taxonomy of $object_type. 138 140 */ 139 function get_object_taxonomies( $object, $output = 'names') {141 function get_object_taxonomies( $object, $output = 'names' ) { 140 142 global $wp_taxonomies; 141 143 142 if ( is_object( $object) ) {144 if ( is_object( $object ) ) { 143 145 if ( $object->post_type == 'attachment' ) 144 return get_attachment_taxonomies( $object);146 return get_attachment_taxonomies( $object ); 145 147 $object = $object->post_type; 146 148 } 147 149 … … 149 151 150 152 $taxonomies = array(); 151 153 foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { 152 if ( array_intersect( $object, (array) $tax_obj->object_type) ) {154 if ( array_intersect( $object, (array) $tax_obj->object_type ) ) { 153 155 if ( 'names' == $output ) 154 156 $taxonomies[] = $tax_name; 155 157 else … … 223 225 * @param string $taxonomy Name of taxonomy object 224 226 * @return bool Whether the taxonomy is hierarchical 225 227 */ 226 function is_taxonomy_hierarchical( $taxonomy) {227 if ( ! taxonomy_exists( $taxonomy) )228 function is_taxonomy_hierarchical( $taxonomy ) { 229 if ( ! taxonomy_exists( $taxonomy ) ) 228 230 return false; 229 231 230 $taxonomy = get_taxonomy( $taxonomy);232 $taxonomy = get_taxonomy( $taxonomy ); 231 233 return $taxonomy->hierarchical; 232 234 } 233 235 … … 286 288 function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 287 289 global $wp_taxonomies, $wp_rewrite, $wp; 288 290 289 if ( ! is_array( $wp_taxonomies) )291 if ( ! is_array( $wp_taxonomies ) ) 290 292 $wp_taxonomies = array(); 291 293 292 294 $defaults = array( 'hierarchical' => false, … … 301 303 'capabilities' => array(), 302 304 'show_in_nav_menus' => null, 303 305 ); 304 $args = wp_parse_args( $args, $defaults);306 $args = wp_parse_args( $args, $defaults ); 305 307 306 if ( false !== $args['query_var'] && !empty( $wp) ) {308 if ( false !== $args['query_var'] && !empty( $wp ) ) { 307 309 if ( true === $args['query_var'] ) 308 310 $args['query_var'] = $taxonomy; 309 $args['query_var'] = sanitize_title_with_dashes( $args['query_var']);310 $wp->add_query_var( $args['query_var']);311 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); 312 $wp->add_query_var( $args['query_var'] ); 311 313 } 312 314 313 if ( false !== $args['rewrite'] && '' != get_option( 'permalink_structure') && !empty($wp_rewrite) ) {314 $args['rewrite'] = wp_parse_args( $args['rewrite'], array(315 'slug' => sanitize_title_with_dashes( $taxonomy),315 if ( false !== $args['rewrite'] && '' != get_option( 'permalink_structure' ) && !empty( $wp_rewrite ) ) { 316 $args['rewrite'] = wp_parse_args( $args['rewrite'], array( 317 'slug' => sanitize_title_with_dashes( $taxonomy ), 316 318 'with_front' => true, 317 319 'hierarchical' => false 318 ) );320 ) ); 319 321 320 322 if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] ) 321 323 $tag = '(.+?)'; 322 324 else 323 325 $tag = '([^/]+)'; 324 326 325 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");326 $wp_rewrite->add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);327 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" ); 328 $wp_rewrite->add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front'] ); 327 329 } 328 330 329 if ( is_null( $args['show_ui']) )331 if ( is_null( $args['show_ui'] ) ) 330 332 $args['show_ui'] = $args['public']; 331 333 332 334 // Whether to show this type in nav-menus.php. Defaults to the setting for public. 333 335 if ( null === $args['show_in_nav_menus'] ) 334 336 $args['show_in_nav_menus'] = $args['public']; 335 337 336 if ( is_null( $args['show_tagcloud']) )338 if ( is_null( $args['show_tagcloud'] ) ) 337 339 $args['show_tagcloud'] = $args['show_ui']; 338 340 339 341 $default_caps = array( … … 354 356 $wp_taxonomies[$taxonomy] = (object) $args; 355 357 356 358 // register callback handling for metabox 357 add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term');359 add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' ); 358 360 } 359 361 360 362 /** … … 419 421 * @param array|string $object_type Name of the object type 420 422 * @return bool True if successful, false if not 421 423 */ 422 function register_taxonomy_for_object_type( $taxonomy, $object_type ) {424 function register_taxonomy_for_object_type( $taxonomy, $object_type ) { 423 425 global $wp_taxonomies; 424 426 425 if ( !isset( $wp_taxonomies[$taxonomy]) )427 if ( !isset( $wp_taxonomies[$taxonomy] ) ) 426 428 return false; 427 429 428 if ( ! get_post_type_object( $object_type) )430 if ( ! get_post_type_object( $object_type ) ) 429 431 return false; 430 432 431 433 $wp_taxonomies[$taxonomy]->object_type[] = $object_type; … … 536 538 foreach ( $terms as $i => $term ) { 537 539 $terms[$i] = sanitize_title_for_query( $term ); 538 540 } 539 $terms = array_filter( $terms);541 $terms = array_filter( $terms ); 540 542 541 543 $terms = "'" . implode( "','", $terms ) . "'"; 542 544 $sql = " … … 593 595 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not 594 596 * exist then WP_Error will be returned. 595 597 */ 596 function &get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw') {598 function &get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) { 597 599 global $wpdb; 598 600 $null = null; 599 601 600 if ( empty( $term) ) {601 $error = new WP_Error( 'invalid_term', __('Empty Term'));602 if ( empty( $term ) ) { 603 $error = new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 602 604 return $error; 603 605 } 604 606 605 if ( ! taxonomy_exists( $taxonomy) ) {606 $error = new WP_Error( 'invalid_taxonomy', __('Invalid Taxonomy'));607 if ( ! taxonomy_exists( $taxonomy ) ) { 608 $error = new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 607 609 return $error; 608 610 } 609 611 610 if ( is_object( $term) && empty($term->filter) ) {611 wp_cache_add( $term->term_id, $term, $taxonomy);612 if ( is_object( $term ) && empty( $term->filter ) ) { 613 wp_cache_add( $term->term_id, $term, $taxonomy ); 612 614 $_term = $term; 613 615 } else { 614 if ( is_object( $term) )616 if ( is_object( $term ) ) 615 617 $term = $term->term_id; 616 618 $term = (int) $term; 617 if ( ! $_term = wp_cache_get( $term, $taxonomy) ) {618 $_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 ) );619 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) { 620 $_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 ) ); 619 621 if ( ! $_term ) 620 622 return $null; 621 wp_cache_add( $term, $_term, $taxonomy);623 wp_cache_add( $term, $_term, $taxonomy ); 622 624 } 623 625 } 624 626 625 $_term = apply_filters( 'get_term', $_term, $taxonomy);626 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy);627 $_term = sanitize_term( $_term, $taxonomy, $filter);627 $_term = apply_filters( 'get_term', $_term, $taxonomy ); 628 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 629 $_term = sanitize_term( $_term, $taxonomy, $filter ); 628 630 629 631 if ( $output == OBJECT ) { 630 632 return $_term; 631 633 } elseif ( $output == ARRAY_A ) { 632 $__term = get_object_vars( $_term);634 $__term = get_object_vars( $_term ); 633 635 return $__term; 634 636 } elseif ( $output == ARRAY_N ) { 635 $__term = array_values( get_object_vars($_term));637 $__term = array_values( get_object_vars( $_term ) ); 636 638 return $__term; 637 639 } else { 638 640 return $_term; … … 666 668 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 667 669 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. 668 670 */ 669 function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {671 function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) { 670 672 global $wpdb; 671 673 672 if ( ! taxonomy_exists( $taxonomy) )674 if ( ! taxonomy_exists( $taxonomy ) ) 673 675 return false; 674 676 675 677 if ( 'slug' == $field ) { 676 678 $field = 't.slug'; 677 $value = sanitize_title( $value);678 if ( empty( $value) )679 $value = sanitize_title( $value ); 680 if ( empty( $value ) ) 679 681 return false; 680 682 } else if ( 'name' == $field ) { 681 683 // Assume already escaped 682 $value = stripslashes( $value);684 $value = stripslashes( $value ); 683 685 $field = 't.name'; 684 686 } else { 685 return get_term( (int) $value, $taxonomy, $output, $filter );687 return get_term( (int) $value, $taxonomy, $output, $filter ); 686 688 } 687 689 688 $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 ) );690 $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 ) ); 689 691 if ( !$term ) 690 692 return false; 691 693 692 wp_cache_add( $term->term_id, $term, $taxonomy);694 wp_cache_add( $term->term_id, $term, $taxonomy ); 693 695 694 $term = apply_filters( 'get_term', $term, $taxonomy);695 $term = apply_filters( "get_$taxonomy", $term, $taxonomy);696 $term = sanitize_term( $term, $taxonomy, $filter);696 $term = apply_filters( 'get_term', $term, $taxonomy ); 697 $term = apply_filters( "get_$taxonomy", $term, $taxonomy ); 698 $term = sanitize_term( $term, $taxonomy, $filter ); 697 699 698 700 if ( $output == OBJECT ) { 699 701 return $term; 700 702 } elseif ( $output == ARRAY_A ) { 701 return get_object_vars( $term);703 return get_object_vars( $term ); 702 704 } elseif ( $output == ARRAY_N ) { 703 return array_values( get_object_vars($term));705 return array_values( get_object_vars( $term ) ); 704 706 } else { 705 707 return $term; 706 708 } … … 727 729 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist 728 730 */ 729 731 function get_term_children( $term_id, $taxonomy ) { 730 if ( ! taxonomy_exists( $taxonomy) )731 return new WP_Error( 'invalid_taxonomy', __('Invalid Taxonomy'));732 if ( ! taxonomy_exists( $taxonomy ) ) 733 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 732 734 733 735 $term_id = intval( $term_id ); 734 736 735 $terms = _get_term_hierarchy( $taxonomy);737 $terms = _get_term_hierarchy( $taxonomy ); 736 738 737 if ( ! isset( $terms[$term_id]) )739 if ( ! isset( $terms[$term_id] ) ) 738 740 return array(); 739 741 740 742 $children = $terms[$term_id]; 741 743 742 744 foreach ( (array) $terms[$term_id] as $child ) { 743 if ( isset( $terms[$child]) )744 $children = array_merge( $children, get_term_children($child, $taxonomy));745 if ( isset( $terms[$child] ) ) 746 $children = array_merge( $children, get_term_children( $child, $taxonomy ) ); 745 747 } 746 748 747 749 return $children; … … 769 771 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { 770 772 $term = (int) $term; 771 773 $term = get_term( $term, $taxonomy ); 772 if ( is_wp_error( $term) )774 if ( is_wp_error( $term ) ) 773 775 return $term; 774 776 775 if ( !is_object( $term) )777 if ( !is_object( $term ) ) 776 778 return ''; 777 779 778 if ( !isset( $term->$field) )780 if ( !isset( $term->$field ) ) 779 781 return ''; 780 782 781 return sanitize_term_field( $field, $term->$field, $term->term_id, $taxonomy, $context);783 return sanitize_term_field( $field, $term->$field, $term->term_id, $taxonomy, $context ); 782 784 } 783 785 784 786 /** … … 800 802 function get_term_to_edit( $id, $taxonomy ) { 801 803 $term = get_term( $id, $taxonomy ); 802 804 803 if ( is_wp_error( $term) )805 if ( is_wp_error( $term ) ) 804 806 return $term; 805 807 806 if ( !is_object( $term) )808 if ( !is_object( $term ) ) 807 809 return ''; 808 810 809 return sanitize_term( $term, $taxonomy, 'edit');811 return sanitize_term( $term, $taxonomy, 'edit' ); 810 812 } 811 813 812 814 /** … … 901 903 * @param string|array $args The values of what to search for when returning terms 902 904 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist. 903 905 */ 904 function &get_terms( $taxonomies, $args = '') {906 function &get_terms( $taxonomies, $args = '' ) { 905 907 global $wpdb; 906 908 $empty_array = array(); 907 909 908 910 $single_taxonomy = false; 909 if ( !is_array( $taxonomies) ) {911 if ( !is_array( $taxonomies ) ) { 910 912 $single_taxonomy = true; 911 $taxonomies = array( $taxonomies);913 $taxonomies = array( $taxonomies ); 912 914 } 913 915 914 916 foreach ( (array) $taxonomies as $taxonomy ) { 915 if ( ! taxonomy_exists( $taxonomy) ) {916 $error = & new WP_Error( 'invalid_taxonomy', __('Invalid Taxonomy'));917 if ( ! taxonomy_exists( $taxonomy ) ) { 918 $error = & new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 917 919 return $error; 918 920 } 919 921 } 920 922 921 $in_taxonomies = "'" . implode( "', '", $taxonomies) . "'";923 $in_taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; 922 924 923 $defaults = array( 'orderby' => 'name', 'order' => 'ASC',925 $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 924 926 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 925 927 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 926 928 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 927 'pad_counts' => false, 'offset' => '', 'search' => ''); 929 'pad_counts' => false, 'offset' => '', 'search' => '' 930 ); 928 931 $args = wp_parse_args( $args, $defaults ); 929 932 $args['number'] = absint( $args['number'] ); 930 933 $args['offset'] = absint( $args['offset'] ); 931 if ( !$single_taxonomy || !is_taxonomy_hierarchical( $taxonomies[0]) ||934 if ( !$single_taxonomy || !is_taxonomy_hierarchical( $taxonomies[0] ) || 932 935 '' !== $args['parent'] ) { 933 936 $args['child_of'] = 0; 934 937 $args['hierarchical'] = false; … … 941 944 $args['hierarchical'] = false; 942 945 $args['pad_counts'] = false; 943 946 } 944 extract( $args, EXTR_SKIP);947 extract( $args, EXTR_SKIP ); 945 948 946 949 if ( $child_of ) { 947 $hierarchy = _get_term_hierarchy( $taxonomies[0]);948 if ( !isset( $hierarchy[$child_of]) )950 $hierarchy = _get_term_hierarchy( $taxonomies[0] ); 951 if ( !isset( $hierarchy[$child_of] ) ) 949 952 return $empty_array; 950 953 } 951 954 952 955 if ( $parent ) { 953 $hierarchy = _get_term_hierarchy( $taxonomies[0]);954 if ( !isset( $hierarchy[$parent]) )956 $hierarchy = _get_term_hierarchy( $taxonomies[0] ); 957 if ( !isset( $hierarchy[$parent] ) ) 955 958 return $empty_array; 956 959 } 957 960 958 961 // $args can be whatever, only use the args defined in defaults to compute the key 959 $filter_key = ( has_filter( 'list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';960 $key = md5( serialize( compact( array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );961 $last_changed = wp_cache_get( 'last_changed', 'terms');962 $filter_key = ( has_filter( 'list_terms_exclusions' ) ) ? serialize( $GLOBALS['wp_filter']['list_terms_exclusions'] ) : ''; 963 $key = md5( serialize( compact( array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key ); 964 $last_changed = wp_cache_get( 'last_changed', 'terms' ); 962 965 if ( !$last_changed ) { 963 966 $last_changed = time(); 964 wp_cache_set( 'last_changed', $last_changed, 'terms');967 wp_cache_set( 'last_changed', $last_changed, 'terms' ); 965 968 } 966 969 $cache_key = "get_terms:$key:$last_changed"; 967 970 $cache = wp_cache_get( $cache_key, 'terms' ); 968 971 if ( false !== $cache ) { 969 $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args);972 $cache = apply_filters( 'get_terms', $cache, $taxonomies, $args ); 970 973 return $cache; 971 974 } 972 975 973 $_orderby = strtolower( $orderby);976 $_orderby = strtolower( $orderby ); 974 977 if ( 'count' == $_orderby ) 975 978 $orderby = 'tt.count'; 976 979 else if ( 'name' == $_orderby ) … … 981 984 $orderby = 't.term_group'; 982 985 else if ( 'none' == $_orderby ) 983 986 $orderby = ''; 984 elseif ( empty( $_orderby) || 'id' == $_orderby )987 elseif ( empty( $_orderby ) || 'id' == $_orderby ) 985 988 $orderby = 't.term_id'; 986 989 987 990 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args ); 988 991 989 if ( !empty( $orderby) )992 if ( !empty( $orderby ) ) 990 993 $orderby = "ORDER BY $orderby"; 991 994 else 992 995 $order = ''; 993 996 994 997 $where = ''; 995 998 $inclusions = ''; 996 if ( !empty( $include) ) {999 if ( !empty( $include ) ) { 997 1000 $exclude = ''; 998 1001 $exclude_tree = ''; 999 $interms = wp_parse_id_list( $include);1002 $interms = wp_parse_id_list( $include ); 1000 1003 foreach ( $interms as $interm ) { 1001 if ( empty( $inclusions) )1002 $inclusions = ' AND ( t.term_id = ' . intval( $interm) . ' ';1004 if ( empty( $inclusions ) ) 1005 $inclusions = ' AND ( t.term_id = ' . intval( $interm ) . ' '; 1003 1006 else 1004 $inclusions .= ' OR t.term_id = ' . intval( $interm) . ' ';1007 $inclusions .= ' OR t.term_id = ' . intval( $interm ) . ' '; 1005 1008 } 1006 1009 } 1007 1010 1008 if ( !empty( $inclusions) )1011 if ( !empty( $inclusions ) ) 1009 1012 $inclusions .= ')'; 1010 1013 $where .= $inclusions; 1011 1014 1012 1015 $exclusions = ''; 1013 1016 if ( !empty( $exclude_tree ) ) { 1014 $excluded_trunks = wp_parse_id_list( $exclude_tree);1017 $excluded_trunks = wp_parse_id_list( $exclude_tree ); 1015 1018 foreach ( $excluded_trunks as $extrunk ) { 1016 $excluded_children = (array) get_terms( $taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0));1019 $excluded_children = (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) ); 1017 1020 $excluded_children[] = $extrunk; 1018 1021 foreach( $excluded_children as $exterm ) { 1019 if ( empty( $exclusions) )1020 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm) . ' ';1022 if ( empty( $exclusions ) ) 1023 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm ) . ' '; 1021 1024 else 1022 $exclusions .= ' AND t.term_id <> ' . intval( $exterm) . ' ';1025 $exclusions .= ' AND t.term_id <> ' . intval( $exterm ) . ' '; 1023 1026 } 1024 1027 } 1025 1028 } 1026 1029 1027 if ( !empty( $exclude) ) {1028 $exterms = wp_parse_id_list( $exclude);1030 if ( !empty( $exclude ) ) { 1031 $exterms = wp_parse_id_list( $exclude ); 1029 1032 foreach ( $exterms as $exterm ) { 1030 if ( empty( $exclusions) )1031 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm) . ' ';1033 if ( empty( $exclusions ) ) 1034 $exclusions = ' AND ( t.term_id <> ' . intval( $exterm ) . ' '; 1032 1035 else 1033 $exclusions .= ' AND t.term_id <> ' . intval( $exterm) . ' ';1036 $exclusions .= ' AND t.term_id <> ' . intval( $exterm ) . ' '; 1034 1037 } 1035 1038 } 1036 1039 1037 if ( !empty( $exclusions) )1040 if ( !empty( $exclusions ) ) 1038 1041 $exclusions .= ')'; 1039 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );1042 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args ); 1040 1043 $where .= $exclusions; 1041 1044 1042 if ( !empty( $slug) ) {1043 $slug = sanitize_title( $slug);1045 if ( !empty( $slug ) ) { 1046 $slug = sanitize_title( $slug ); 1044 1047 $where .= " AND t.slug = '$slug'"; 1045 1048 } 1046 1049 1047 if ( !empty( $name__like) )1050 if ( !empty( $name__like ) ) 1048 1051 $where .= " AND t.name LIKE '{$name__like}%'"; 1049 1052 1050 1053 if ( '' !== $parent ) { … … 1056 1059 $where .= ' AND tt.count > 0'; 1057 1060 1058 1061 // don't limit the query results when we have to descend the family tree 1059 if ( ! empty( $number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {1062 if ( ! empty( $number ) && ! $hierarchical && empty( $child_of ) && '' === $parent ) { 1060 1063 if ( $offset ) 1061 1064 $limit = 'LIMIT ' . $offset . ',' . $number; 1062 1065 else … … 1065 1068 $limit = ''; 1066 1069 } 1067 1070 1068 if ( !empty( $search) ) {1069 $search = like_escape( $search);1071 if ( !empty( $search ) ) { 1072 $search = like_escape( $search ); 1070 1073 $where .= " AND (t.name LIKE '%$search%')"; 1071 1074 } 1072 1075 1073 1076 $selects = array(); 1074 1077 switch ( $fields ) { 1075 1078 case 'all': 1076 $selects = array( 't.*', 'tt.*');1079 $selects = array( 't.*', 'tt.*' ); 1077 1080 break; 1078 1081 case 'ids': 1079 1082 case 'id=>parent': 1080 $selects = array( 't.term_id', 'tt.parent', 'tt.count');1083 $selects = array( 't.term_id', 'tt.parent', 'tt.count' ); 1081 1084 break; 1082 1085 case 'names': 1083 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name');1086 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' ); 1084 1087 break; 1085 1088 case 'count': 1086 1089 $orderby = ''; 1087 1090 $order = ''; 1088 $selects = array( 'COUNT(*)');1091 $selects = array( 'COUNT(*)' ); 1089 1092 } 1090 $select_this = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args ));1093 $select_this = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args ) ); 1091 1094 1092 1095 $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"; 1093 1096 1094 1097 if ( 'count' == $fields ) { 1095 $term_count = $wpdb->get_var( $query);1098 $term_count = $wpdb->get_var( $query ); 1096 1099 return $term_count; 1097 1100 } 1098 1101 1099 $terms = $wpdb->get_results( $query);1102 $terms = $wpdb->get_results( $query ); 1100 1103 if ( 'all' == $fields ) { 1101 update_term_cache( $terms);1104 update_term_cache( $terms ); 1102 1105 } 1103 1106 1104 if ( empty( $terms) ) {1107 if ( empty( $terms ) ) { 1105 1108 wp_cache_add( $cache_key, array(), 'terms', 86400 ); // one day 1106 $terms = apply_filters( 'get_terms', array(), $taxonomies, $args);1109 $terms = apply_filters( 'get_terms', array(), $taxonomies, $args ); 1107 1110 return $terms; 1108 1111 } 1109 1112 1110 1113 if ( $child_of ) { 1111 $children = _get_term_hierarchy( $taxonomies[0]);1112 if ( ! empty( $children) )1113 $terms = & _get_term_children( $child_of, $terms, $taxonomies[0]);1114 $children = _get_term_hierarchy( $taxonomies[0] ); 1115 if ( ! empty( $children ) ) 1116 $terms = & _get_term_children( $child_of, $terms, $taxonomies[0] ); 1114 1117 } 1115 1118 1116 1119 // Update term counts to include children. 1117 1120 if ( $pad_counts && 'all' == $fields ) 1118 _pad_term_counts( $terms, $taxonomies[0]);1121 _pad_term_counts( $terms, $taxonomies[0] ); 1119 1122 1120 1123 // Make sure we show empty categories that have children. 1121 if ( $hierarchical && $hide_empty && is_array( $terms) ) {1124 if ( $hierarchical && $hide_empty && is_array( $terms ) ) { 1122 1125 foreach ( $terms as $k => $term ) { 1123 1126 if ( ! $term->count ) { 1124 $children = _get_term_children( $term->term_id, $terms, $taxonomies[0]);1125 if ( is_array( $children) )1127 $children = _get_term_children( $term->term_id, $terms, $taxonomies[0] ); 1128 if ( is_array( $children ) ) 1126 1129 foreach ( $children as $child ) 1127 1130 if ( $child->count ) 1128 1131 continue 2; 1129 1132 1130 1133 // It really is empty 1131 unset( $terms[$k]);1134 unset( $terms[$k] ); 1132 1135 } 1133 1136 } 1134 1137 } … … 1136 1139 1137 1140 $_terms = array(); 1138 1141 if ( 'id=>parent' == $fields ) { 1139 while ( $term = array_shift( $terms) )1142 while ( $term = array_shift( $terms ) ) 1140 1143 $_terms[$term->term_id] = $term->parent; 1141 1144 $terms = $_terms; 1142 1145 } elseif ( 'ids' == $fields ) { 1143 while ( $term = array_shift( $terms) )1146 while ( $term = array_shift( $terms ) ) 1144 1147 $_terms[] = $term->term_id; 1145 1148 $terms = $_terms; 1146 1149 } elseif ( 'names' == $fields ) { 1147 while ( $term = array_shift( $terms) )1150 while ( $term = array_shift( $terms ) ) 1148 1151 $_terms[] = $term->name; 1149 1152 $terms = $_terms; 1150 1153 } 1151 1154 1152 if ( 0 < $number && intval( @count($terms)) > $number ) {1153 $terms = array_slice( $terms, $offset, $number);1155 if ( 0 < $number && intval( @count( $terms ) ) > $number ) { 1156 $terms = array_slice( $terms, $offset, $number ); 1154 1157 } 1155 1158 1156 1159 wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day 1157 1160 1158 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args);1161 $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args ); 1159 1162 return $terms; 1160 1163 } 1161 1164 … … 1177 1180 * @param int $parent ID of parent term under which to confine the exists search. 1178 1181 * @return mixed Get the term id or Term Object, if exists. 1179 1182 */ 1180 function term_exists( $term, $taxonomy = '', $parent = 0) {1183 function term_exists( $term, $taxonomy = '', $parent = 0 ) { 1181 1184 global $wpdb; 1182 1185 1183 1186 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1184 1187 $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 "; 1185 1188 1186 if ( is_int( $term) ) {1189 if ( is_int( $term ) ) { 1187 1190 if ( 0 == $term ) 1188 1191 return 0; 1189 1192 $where = 't.term_id = %d'; 1190 if ( !empty( $taxonomy) )1193 if ( !empty( $taxonomy ) ) 1191 1194 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); 1192 1195 else 1193 1196 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); … … 1195 1198 1196 1199 $term = trim( stripslashes( $term ) ); 1197 1200 1198 if ( '' === $slug = sanitize_title( $term) )1201 if ( '' === $slug = sanitize_title( $term ) ) 1199 1202 return 0; 1200 1203 1201 1204 $where = 't.slug = %s'; 1202 1205 $else_where = 't.name = %s'; 1203 $where_fields = array( $slug);1204 $else_where_fields = array( $term);1205 if ( !empty( $taxonomy) ) {1206 $where_fields = array( $slug ); 1207 $else_where_fields = array( $term ); 1208 if ( !empty( $taxonomy ) ) { 1206 1209 $parent = (int) $parent; 1207 1210 if ( $parent > 0 ) { 1208 1211 $where_fields[] = $parent; … … 1214 1217 $where_fields[] = $taxonomy; 1215 1218 $else_where_fields[] = $taxonomy; 1216 1219 1217 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) )1220 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 ) ) 1218 1221 return $result; 1219 1222 1220 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);1223 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 ); 1221 1224 } 1222 1225 1223 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )1226 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields ) ) ) 1224 1227 return $result; 1225 1228 1226 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );1229 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields ) ); 1227 1230 } 1228 1231 1229 1232 /** … … 1246 1249 * @param string $context Default is 'display'. 1247 1250 * @return array|object Term with all fields sanitized 1248 1251 */ 1249 function sanitize_term( $term, $taxonomy, $context = 'display') {1252 function sanitize_term( $term, $taxonomy, $context = 'display' ) { 1250 1253 1251 1254 if ( 'raw' == $context ) 1252 1255 return $term; 1253 1256 1254 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');1257 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group' ); 1255 1258 1256 1259 $do_object = false; 1257 if ( is_object( $term) )1260 if ( is_object( $term ) ) 1258 1261 $do_object = true; 1259 1262 1260 $term_id = $do_object ? $term->term_id : ( isset($term['term_id']) ? $term['term_id'] : 0);1263 $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 ); 1261 1264 1262 1265 foreach ( (array) $fields as $field ) { 1263 1266 if ( $do_object ) { 1264 if ( isset( $term->$field) )1265 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context);1267 if ( isset( $term->$field ) ) 1268 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); 1266 1269 } else { 1267 if ( isset( $term[$field]) )1268 $term[$field] = sanitize_term_field( $field, $term[$field], $term_id, $taxonomy, $context);1270 if ( isset( $term[$field] ) ) 1271 $term[$field] = sanitize_term_field( $field, $term[$field], $term_id, $taxonomy, $context ); 1269 1272 } 1270 1273 } 1271 1274 … … 1303 1306 * @param string $context Either edit, db, display, attribute, or js. 1304 1307 * @return mixed sanitized field 1305 1308 */ 1306 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context) {1309 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { 1307 1310 if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) { 1308 1311 $value = (int) $value; 1309 1312 if ( $value < 0 ) … … 1314 1317 return $value; 1315 1318 1316 1319 if ( 'edit' == $context ) { 1317 $value = apply_filters( "edit_term_$field", $value, $term_id, $taxonomy);1318 $value = apply_filters( "edit_${taxonomy}_$field", $value, $term_id);1320 $value = apply_filters( "edit_term_$field", $value, $term_id, $taxonomy ); 1321 $value = apply_filters( "edit_${taxonomy}_$field", $value, $term_id ); 1319 1322 if ( 'description' == $field ) 1320 $value = format_to_edit( $value);1323 $value = format_to_edit( $value ); 1321 1324 else 1322 $value = esc_attr( $value);1325 $value = esc_attr( $value ); 1323 1326 } else if ( 'db' == $context ) { 1324 $value = apply_filters( "pre_term_$field", $value, $taxonomy);1325 $value = apply_filters( "pre_${taxonomy}_$field", $value);1327 $value = apply_filters( "pre_term_$field", $value, $taxonomy ); 1328 $value = apply_filters( "pre_${taxonomy}_$field", $value ); 1326 1329 // Back compat filters 1327 1330 if ( 'slug' == $field ) 1328 $value = apply_filters( 'pre_category_nicename', $value);1331 $value = apply_filters( 'pre_category_nicename', $value ); 1329 1332 1330 1333 } else if ( 'rss' == $context ) { 1331 $value = apply_filters( "term_${field}_rss", $value, $taxonomy);1332 $value = apply_filters( "${taxonomy}_${field}_rss", $value);1334 $value = apply_filters( "term_${field}_rss", $value, $taxonomy ); 1335 $value = apply_filters( "${taxonomy}_${field}_rss", $value ); 1333 1336 } else { 1334 1337 // Use display filters by default. 1335 $value = apply_filters( "term_$field", $value, $term_id, $taxonomy, $context);1336 $value = apply_filters( "${taxonomy}_$field", $value, $term_id, $context);1338 $value = apply_filters( "term_$field", $value, $term_id, $taxonomy, $context ); 1339 $value = apply_filters( "${taxonomy}_$field", $value, $term_id, $context ); 1337 1340 } 1338 1341 1339 1342 if ( 'attribute' == $context ) 1340 $value = esc_attr( $value);1343 $value = esc_attr( $value ); 1341 1344 else if ( 'js' == $context ) 1342 $value = esc_js( $value);1345 $value = esc_js( $value ); 1343 1346 1344 1347 return $value; 1345 1348 } … … 1361 1364 * @return int How many terms are in $taxonomy 1362 1365 */ 1363 1366 function wp_count_terms( $taxonomy, $args = array() ) { 1364 $defaults = array( 'hide_empty' => false);1365 $args = wp_parse_args( $args, $defaults);1367 $defaults = array( 'hide_empty' => false ); 1368 $args = wp_parse_args( $args, $defaults ); 1366 1369 1367 1370 // backwards compatibility 1368 if ( isset( $args['ignore_empty']) ) {1371 if ( isset( $args['ignore_empty'] ) ) { 1369 1372 $args['hide_empty'] = $args['ignore_empty']; 1370 unset( $args['ignore_empty']);1373 unset( $args['ignore_empty'] ); 1371 1374 } 1372 1375 1373 1376 $args['fields'] = 'count'; 1374 1377 1375 return get_terms( $taxonomy, $args);1378 return get_terms( $taxonomy, $args ); 1376 1379 } 1377 1380 1378 1381 /** … … 1395 1398 1396 1399 $object_id = (int) $object_id; 1397 1400 1398 if ( !is_array( $taxonomies) )1399 $taxonomies = array( $taxonomies);1401 if ( !is_array( $taxonomies ) ) 1402 $taxonomies = array( $taxonomies ); 1400 1403 1401 1404 foreach ( (array) $taxonomies as $taxonomy ) { 1402 $tt_ids = wp_get_object_terms( $object_id, $taxonomy, array('fields' => 'tt_ids'));1403 $in_tt_ids = "'" . implode( "', '", $tt_ids) . "'";1405 $tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) ); 1406 $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; 1404 1407 do_action( 'delete_term_relationships', $object_id, $tt_ids ); 1405 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );1408 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); 1406 1409 do_action( 'deleted_term_relationships', $object_id, $tt_ids ); 1407 wp_update_term_count( $tt_ids, $taxonomy);1410 wp_update_term_count( $tt_ids, $taxonomy ); 1408 1411 } 1409 1412 } 1410 1413 … … 1438 1441 1439 1442 $term = (int) $term; 1440 1443 1441 if ( ! $ids = term_exists( $term, $taxonomy) )1444 if ( ! $ids = term_exists( $term, $taxonomy ) ) 1442 1445 return false; 1443 1446 if ( is_wp_error( $ids ) ) 1444 1447 return $ids; … … 1453 1456 return 0; // Don't delete the default category 1454 1457 } 1455 1458 1456 $args = wp_parse_args( $args, $defaults);1457 extract( $args, EXTR_SKIP);1459 $args = wp_parse_args( $args, $defaults ); 1460 extract( $args, EXTR_SKIP ); 1458 1461 1459 1462 if ( isset( $default ) ) { 1460 1463 $default = (int) $default; 1461 if ( ! term_exists( $default, $taxonomy) )1462 unset( $default);1464 if ( ! term_exists( $default, $taxonomy ) ) 1465 unset( $default ); 1463 1466 } 1464 1467 1465 1468 // Update children to point to new parent 1466 if ( is_taxonomy_hierarchical( $taxonomy) ) {1467 $term_obj = get_term( $term, $taxonomy);1469 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 1470 $term_obj = get_term( $term, $taxonomy ); 1468 1471 if ( is_wp_error( $term_obj ) ) 1469 1472 return $term_obj; 1470 1473 $parent = $term_obj->parent; 1471 1474 1472 1475 $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id ); 1473 1476 do_action( 'edit_term_taxonomies', $edit_tt_ids ); 1474 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );1477 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); 1475 1478 do_action( 'edited_term_taxonomies', $edit_tt_ids ); 1476 1479 } 1477 1480 1478 1481 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); 1479 1482 1480 1483 foreach ( (array) $objects as $object ) { 1481 $terms = wp_get_object_terms( $object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));1482 if ( 1 == count( $terms) && isset($default) ) {1483 $terms = array( $default);1484 $terms = wp_get_object_terms( $object, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 1485 if ( 1 == count( $terms ) && isset( $default ) ) { 1486 $terms = array( $default ); 1484 1487 } else { 1485 $terms = array_diff( $terms, array($term));1486 if ( isset($default) && isset($force_default) && $force_default)1487 $terms = array_merge( $terms, array($default));1488 $terms = array_diff( $terms, array( $term ) ); 1489 if ( isset( $default ) && isset( $force_default ) && $force_default ) 1490 $terms = array_merge( $terms, array( $default ) ); 1488 1491 } 1489 $terms = array_map( 'intval', $terms);1490 wp_set_object_terms( $object, $terms, $taxonomy);1492 $terms = array_map( 'intval', $terms ); 1493 wp_set_object_terms( $object, $terms, $taxonomy ); 1491 1494 } 1492 1495 1493 1496 do_action( 'delete_term_taxonomy', $tt_id ); … … 1495 1498 do_action( 'deleted_term_taxonomy', $tt_id ); 1496 1499 1497 1500 // Delete the term if no taxonomies use it. 1498 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) )1499 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term ) );1501 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) 1502 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term ) ); 1500 1503 1501 clean_term_cache( $term, $taxonomy);1504 clean_term_cache( $term, $taxonomy ); 1502 1505 1503 do_action( 'delete_term', $term, $tt_id, $taxonomy);1504 do_action( "delete_$taxonomy", $term, $tt_id);1506 do_action( 'delete_term', $term, $tt_id, $taxonomy ); 1507 do_action( "delete_$taxonomy", $term, $tt_id ); 1505 1508 1506 1509 return true; 1507 1510 } … … 1552 1555 * @param array|string $args Change what is returned 1553 1556 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist. 1554 1557 */ 1555 function wp_get_object_terms( $object_ids, $taxonomies, $args = array()) {1558 function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 1556 1559 global $wpdb; 1557 1560 1558 if ( !is_array( $taxonomies) )1559 $taxonomies = array( $taxonomies);1561 if ( !is_array( $taxonomies ) ) 1562 $taxonomies = array( $taxonomies ); 1560 1563 1561 1564 foreach ( (array) $taxonomies as $taxonomy ) { 1562 if ( ! taxonomy_exists( $taxonomy) )1563 return new WP_Error( 'invalid_taxonomy', __('Invalid Taxonomy'));1565 if ( ! taxonomy_exists( $taxonomy ) ) 1566 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 1564 1567 } 1565 1568 1566 if ( !is_array( $object_ids) )1567 $object_ids = array( $object_ids);1568 $object_ids = array_map( 'intval', $object_ids);1569 if ( !is_array( $object_ids ) ) 1570 $object_ids = array( $object_ids ); 1571 $object_ids = array_map( 'intval', $object_ids ); 1569 1572 1570 $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');1573 $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all' ); 1571 1574 $args = wp_parse_args( $args, $defaults ); 1572 1575 1573 1576 $terms = array(); 1574 if ( count( $taxonomies) > 1 ) {1577 if ( count( $taxonomies ) > 1 ) { 1575 1578 foreach ( $taxonomies as $index => $taxonomy ) { 1576 $t = get_taxonomy( $taxonomy);1577 if ( isset( $t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {1578 unset( $taxonomies[$index]);1579 $terms = array_merge( $terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));1579 $t = get_taxonomy( $taxonomy ); 1580 if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) { 1581 unset( $taxonomies[$index] ); 1582 $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) ); 1580 1583 } 1581 1584 } 1582 1585 } else { 1583 $t = get_taxonomy( $taxonomies[0]);1584 if ( isset( $t->args) && is_array($t->args) )1585 $args = array_merge( $args, $t->args);1586 $t = get_taxonomy( $taxonomies[0] ); 1587 if ( isset( $t->args ) && is_array( $t->args ) ) 1588 $args = array_merge( $args, $t->args ); 1586 1589 } 1587 1590 1588 extract( $args, EXTR_SKIP);1591 extract( $args, EXTR_SKIP ); 1589 1592 1590 1593 if ( 'count' == $orderby ) 1591 1594 $orderby = 'tt.count'; … … 1605 1608 } 1606 1609 1607 1610 // tt_ids queries can only be none or tr.term_taxonomy_id 1608 if ( ( 'tt_ids' == $fields) && !empty($orderby) )1611 if ( ( 'tt_ids' == $fields ) && !empty( $orderby ) ) 1609 1612 $orderby = 'tr.term_taxonomy_id'; 1610 1613 1611 if ( !empty( $orderby) )1614 if ( !empty( $orderby ) ) 1612 1615 $orderby = "ORDER BY $orderby"; 1613 1616 1614 $taxonomies = "'" . implode( "', '", $taxonomies) . "'";1615 $object_ids = implode( ', ', $object_ids);1617 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; 1618 $object_ids = implode( ', ', $object_ids ); 1616 1619 1617 1620 $select_this = ''; 1618 1621 if ( 'all' == $fields ) … … 1627 1630 $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"; 1628 1631 1629 1632 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 1630 $terms = array_merge( $terms, $wpdb->get_results($query));1631 update_term_cache( $terms);1633 $terms = array_merge( $terms, $wpdb->get_results( $query ) ); 1634 update_term_cache( $terms ); 1632 1635 } else if ( 'ids' == $fields || 'names' == $fields ) { 1633 $terms = array_merge( $terms, $wpdb->get_col($query));1636 $terms = array_merge( $terms, $wpdb->get_col( $query ) ); 1634 1637 } else if ( 'tt_ids' == $fields ) { 1635 $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");1638 $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" ); 1636 1639 } 1637 1640 1638 1641 if ( ! $terms ) 1639 1642 $terms = array(); 1640 1643 1641 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);1644 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); 1642 1645 } 1643 1646 1644 1647 /** … … 1697 1700 function wp_insert_term( $term, $taxonomy, $args = array() ) { 1698 1701 global $wpdb; 1699 1702 1700 if ( ! taxonomy_exists( $taxonomy) )1701 return new WP_Error( 'invalid_taxonomy', __('Invalid taxonomy'));1703 if ( ! taxonomy_exists( $taxonomy ) ) 1704 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 1702 1705 1703 1706 $term = apply_filters( 'pre_insert_term', $term, $taxonomy ); 1704 1707 if ( is_wp_error( $term ) ) 1705 1708 return $term; 1706 1709 1707 if ( is_int( $term) && 0 == $term )1708 return new WP_Error( 'invalid_term_id', __('Invalid term ID'));1710 if ( is_int( $term ) && 0 == $term ) 1711 return new WP_Error( 'invalid_term_id', __( 'Invalid term ID' ) ); 1709 1712 1710 if ( '' == trim( $term) )1711 return new WP_Error( 'empty_term_name', __('A name is required for this term'));1713 if ( '' == trim( $term ) ) 1714 return new WP_Error( 'empty_term_name', __( 'A name is required for this term' ) ); 1712 1715 1713 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' );1714 $args = wp_parse_args( $args, $defaults);1716 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' ); 1717 $args = wp_parse_args( $args, $defaults ); 1715 1718 $args['name'] = $term; 1716 1719 $args['taxonomy'] = $taxonomy; 1717 $args = sanitize_term( $args, $taxonomy, 'db');1718 extract( $args, EXTR_SKIP);1720 $args = sanitize_term( $args, $taxonomy, 'db' ); 1721 extract( $args, EXTR_SKIP ); 1719 1722 1720 1723 // expected_slashed ($name) 1721 $name = stripslashes( $name);1722 $description = stripslashes( $description);1724 $name = stripslashes( $name ); 1725 $description = stripslashes( $description ); 1723 1726 1724 if ( empty( $slug) )1725 $slug = sanitize_title( $name);1727 if ( empty( $slug ) ) 1728 $slug = sanitize_title( $name ); 1726 1729 1727 1730 $term_group = 0; 1728 1731 if ( $alias_of ) { 1729 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of ) );1732 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of ) ); 1730 1733 if ( $alias->term_group ) { 1731 1734 // The alias we want is already in a group, so let's use that one. 1732 1735 $term_group = $alias->term_group; 1733 1736 } else { 1734 1737 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. 1735 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms") + 1;1738 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 1736 1739 do_action( 'edit_terms', $alias->term_id ); 1737 $wpdb->update( $wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );1740 $wpdb->update( $wpdb->terms, compact( 'term_group' ), array( 'term_id' => $alias->term_id ) ); 1738 1741 do_action( 'edited_terms', $alias->term_id ); 1739 1742 } 1740 1743 } 1741 1744 1742 if ( $term_id = term_exists( $slug) ) {1743 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id ), ARRAY_A );1745 if ( $term_id = term_exists( $slug ) ) { 1746 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id ), ARRAY_A ); 1744 1747 // We've got an existing term in the same taxonomy, which matches the name of the new term: 1745 if ( is_taxonomy_hierarchical( $taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {1748 if ( is_taxonomy_hierarchical( $taxonomy ) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) { 1746 1749 // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level. 1747 $siblings = get_terms( $taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );1748 if ( in_array( $name, $siblings) ) {1749 return new WP_Error( 'term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);1750 $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => (int)$parent ) ); 1751 if ( in_array( $name, $siblings ) ) { 1752 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] ); 1750 1753 } else { 1751 $slug = wp_unique_term_slug( $slug, (object) $args);1754 $slug = wp_unique_term_slug( $slug, (object) $args ); 1752 1755 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1753 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1756 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1754 1757 $term_id = (int) $wpdb->insert_id; 1755 1758 } 1756 1759 } elseif ( $existing_term['name'] != $name ) { 1757 1760 // We've got an existing term, with a different name, Create the new term. 1758 $slug = wp_unique_term_slug( $slug, (object) $args);1761 $slug = wp_unique_term_slug( $slug, (object) $args ); 1759 1762 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1760 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1763 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1761 1764 $term_id = (int) $wpdb->insert_id; 1762 1765 } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) { 1763 1766 // Same name, same slug. 1764 return new WP_Error( 'term_exists', __('A term with the name provided already exists.'), $exists['term_id']);1767 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists.' ), $exists['term_id'] ); 1765 1768 } 1766 1769 } else { 1767 1770 // This term does not exist at all in the database, Create it. 1768 $slug = wp_unique_term_slug( $slug, (object) $args);1771 $slug = wp_unique_term_slug( $slug, (object) $args ); 1769 1772 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) 1770 return new WP_Error( 'db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);1773 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error ); 1771 1774 $term_id = (int) $wpdb->insert_id; 1772 1775 } 1773 1776 1774 1777 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. 1775 if ( empty( $slug) ) {1776 $slug = sanitize_title( $slug, $term_id);1778 if ( empty( $slug ) ) { 1779 $slug = sanitize_title( $slug, $term_id ); 1777 1780 do_action( 'edit_terms', $term_id ); 1778 1781 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 1779 1782 do_action( 'edited_terms', $term_id ); … … 1781 1784 1782 1785 $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 ) ); 1783 1786 1784 if ( !empty( $tt_id) )1785 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);1787 if ( !empty( $tt_id ) ) 1788 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 1786 1789 1787 1790 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); 1788 1791 $tt_id = (int) $wpdb->insert_id; 1789 1792 1790 do_action( "create_term", $term_id, $tt_id, $taxonomy);1791 do_action( "create_$taxonomy", $term_id, $tt_id);1793 do_action( "create_term", $term_id, $tt_id, $taxonomy ); 1794 do_action( "create_$taxonomy", $term_id, $tt_id ); 1792 1795 1793 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id);1796 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 1794 1797 1795 clean_term_cache( $term_id, $taxonomy);1798 clean_term_cache( $term_id, $taxonomy ); 1796 1799 1797 do_action( "created_term", $term_id, $tt_id, $taxonomy);1798 do_action( "created_$taxonomy", $term_id, $tt_id);1800 do_action( "created_term", $term_id, $tt_id, $taxonomy ); 1801 do_action( "created_$taxonomy", $term_id, $tt_id ); 1799 1802 1800 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);1803 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 1801 1804 } 1802 1805 1803 1806 /**
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)