Changeset 42343 for trunk/src/wp-includes/taxonomy.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r42211 r42343 26 26 27 27 if ( ! did_action( 'init' ) ) { 28 $rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false ); 28 $rewrite = array( 29 'category' => false, 30 'post_tag' => false, 31 'post_format' => false, 32 ); 29 33 } else { 30 34 … … 37 41 */ 38 42 $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' ); 39 $rewrite = array(40 'category' => array(43 $rewrite = array( 44 'category' => array( 41 45 'hierarchical' => true, 42 'slug' => get_option('category_base') ? get_option('category_base') : 'category',43 'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),44 'ep_mask' => EP_CATEGORIES,46 'slug' => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category', 47 'with_front' => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(), 48 'ep_mask' => EP_CATEGORIES, 45 49 ), 46 'post_tag' => array(50 'post_tag' => array( 47 51 'hierarchical' => false, 48 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',49 'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),50 'ep_mask' => EP_TAGS,52 'slug' => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag', 53 'with_front' => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(), 54 'ep_mask' => EP_TAGS, 51 55 ), 52 56 'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false, … … 54 58 } 55 59 56 register_taxonomy( 'category', 'post', array( 57 'hierarchical' => true, 58 'query_var' => 'category_name', 59 'rewrite' => $rewrite['category'], 60 'public' => true, 61 'show_ui' => true, 62 'show_admin_column' => true, 63 '_builtin' => true, 64 'capabilities' => array( 65 'manage_terms' => 'manage_categories', 66 'edit_terms' => 'edit_categories', 67 'delete_terms' => 'delete_categories', 68 'assign_terms' => 'assign_categories', 69 ), 70 'show_in_rest' => true, 71 'rest_base' => 'categories', 72 'rest_controller_class' => 'WP_REST_Terms_Controller', 73 ) ); 74 75 register_taxonomy( 'post_tag', 'post', array( 76 'hierarchical' => false, 77 'query_var' => 'tag', 78 'rewrite' => $rewrite['post_tag'], 79 'public' => true, 80 'show_ui' => true, 81 'show_admin_column' => true, 82 '_builtin' => true, 83 'capabilities' => array( 84 'manage_terms' => 'manage_post_tags', 85 'edit_terms' => 'edit_post_tags', 86 'delete_terms' => 'delete_post_tags', 87 'assign_terms' => 'assign_post_tags', 88 ), 89 'show_in_rest' => true, 90 'rest_base' => 'tags', 91 'rest_controller_class' => 'WP_REST_Terms_Controller', 92 ) ); 93 94 register_taxonomy( 'nav_menu', 'nav_menu_item', array( 95 'public' => false, 96 'hierarchical' => false, 97 'labels' => array( 98 'name' => __( 'Navigation Menus' ), 99 'singular_name' => __( 'Navigation Menu' ), 100 ), 101 'query_var' => false, 102 'rewrite' => false, 103 'show_ui' => false, 104 '_builtin' => true, 105 'show_in_nav_menus' => false, 106 ) ); 107 108 register_taxonomy( 'link_category', 'link', array( 109 'hierarchical' => false, 110 'labels' => array( 111 'name' => __( 'Link Categories' ), 112 'singular_name' => __( 'Link Category' ), 113 'search_items' => __( 'Search Link Categories' ), 114 'popular_items' => null, 115 'all_items' => __( 'All Link Categories' ), 116 'edit_item' => __( 'Edit Link Category' ), 117 'update_item' => __( 'Update Link Category' ), 118 'add_new_item' => __( 'Add New Link Category' ), 119 'new_item_name' => __( 'New Link Category Name' ), 120 'separate_items_with_commas' => null, 121 'add_or_remove_items' => null, 122 'choose_from_most_used' => null, 123 'back_to_items' => __( '← Back to Link Categories' ), 124 ), 125 'capabilities' => array( 126 'manage_terms' => 'manage_links', 127 'edit_terms' => 'manage_links', 128 'delete_terms' => 'manage_links', 129 'assign_terms' => 'manage_links', 130 ), 131 'query_var' => false, 132 'rewrite' => false, 133 'public' => false, 134 'show_ui' => true, 135 '_builtin' => true, 136 ) ); 137 138 register_taxonomy( 'post_format', 'post', array( 139 'public' => true, 140 'hierarchical' => false, 141 'labels' => array( 142 'name' => _x( 'Format', 'post format' ), 143 'singular_name' => _x( 'Format', 'post format' ), 144 ), 145 'query_var' => true, 146 'rewrite' => $rewrite['post_format'], 147 'show_ui' => false, 148 '_builtin' => true, 149 'show_in_nav_menus' => current_theme_supports( 'post-formats' ), 150 ) ); 60 register_taxonomy( 61 'category', 'post', array( 62 'hierarchical' => true, 63 'query_var' => 'category_name', 64 'rewrite' => $rewrite['category'], 65 'public' => true, 66 'show_ui' => true, 67 'show_admin_column' => true, 68 '_builtin' => true, 69 'capabilities' => array( 70 'manage_terms' => 'manage_categories', 71 'edit_terms' => 'edit_categories', 72 'delete_terms' => 'delete_categories', 73 'assign_terms' => 'assign_categories', 74 ), 75 'show_in_rest' => true, 76 'rest_base' => 'categories', 77 'rest_controller_class' => 'WP_REST_Terms_Controller', 78 ) 79 ); 80 81 register_taxonomy( 82 'post_tag', 'post', array( 83 'hierarchical' => false, 84 'query_var' => 'tag', 85 'rewrite' => $rewrite['post_tag'], 86 'public' => true, 87 'show_ui' => true, 88 'show_admin_column' => true, 89 '_builtin' => true, 90 'capabilities' => array( 91 'manage_terms' => 'manage_post_tags', 92 'edit_terms' => 'edit_post_tags', 93 'delete_terms' => 'delete_post_tags', 94 'assign_terms' => 'assign_post_tags', 95 ), 96 'show_in_rest' => true, 97 'rest_base' => 'tags', 98 'rest_controller_class' => 'WP_REST_Terms_Controller', 99 ) 100 ); 101 102 register_taxonomy( 103 'nav_menu', 'nav_menu_item', array( 104 'public' => false, 105 'hierarchical' => false, 106 'labels' => array( 107 'name' => __( 'Navigation Menus' ), 108 'singular_name' => __( 'Navigation Menu' ), 109 ), 110 'query_var' => false, 111 'rewrite' => false, 112 'show_ui' => false, 113 '_builtin' => true, 114 'show_in_nav_menus' => false, 115 ) 116 ); 117 118 register_taxonomy( 119 'link_category', 'link', array( 120 'hierarchical' => false, 121 'labels' => array( 122 'name' => __( 'Link Categories' ), 123 'singular_name' => __( 'Link Category' ), 124 'search_items' => __( 'Search Link Categories' ), 125 'popular_items' => null, 126 'all_items' => __( 'All Link Categories' ), 127 'edit_item' => __( 'Edit Link Category' ), 128 'update_item' => __( 'Update Link Category' ), 129 'add_new_item' => __( 'Add New Link Category' ), 130 'new_item_name' => __( 'New Link Category Name' ), 131 'separate_items_with_commas' => null, 132 'add_or_remove_items' => null, 133 'choose_from_most_used' => null, 134 'back_to_items' => __( '← Back to Link Categories' ), 135 ), 136 'capabilities' => array( 137 'manage_terms' => 'manage_links', 138 'edit_terms' => 'manage_links', 139 'delete_terms' => 'manage_links', 140 'assign_terms' => 'manage_links', 141 ), 142 'query_var' => false, 143 'rewrite' => false, 144 'public' => false, 145 'show_ui' => true, 146 '_builtin' => true, 147 ) 148 ); 149 150 register_taxonomy( 151 'post_format', 'post', array( 152 'public' => true, 153 'hierarchical' => false, 154 'labels' => array( 155 'name' => _x( 'Format', 'post format' ), 156 'singular_name' => _x( 'Format', 'post format' ), 157 ), 158 'query_var' => true, 159 'rewrite' => $rewrite['post_format'], 160 'show_ui' => false, 161 '_builtin' => true, 162 'show_in_nav_menus' => current_theme_supports( 'post-formats' ), 163 ) 164 ); 151 165 } 152 166 … … 170 184 global $wp_taxonomies; 171 185 172 $field = ( 'names' == $output) ? 'name' : false;173 174 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field);186 $field = ( 'names' == $output ) ? 'name' : false; 187 188 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field ); 175 189 } 176 190 … … 199 213 global $wp_taxonomies; 200 214 201 if ( is_object( $object) ) {202 if ( $object->post_type == 'attachment' ) 215 if ( is_object( $object ) ) { 216 if ( $object->post_type == 'attachment' ) { 203 217 return get_attachment_taxonomies( $object, $output ); 218 } 204 219 $object = $object->post_type; 205 220 } … … 209 224 $taxonomies = array(); 210 225 foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { 211 if ( array_intersect( $object, (array) $tax_obj->object_type) ) {212 if ( 'names' == $output ) 226 if ( array_intersect( $object, (array) $tax_obj->object_type ) ) { 227 if ( 'names' == $output ) { 213 228 $taxonomies[] = $tax_name; 214 else229 } else { 215 230 $taxonomies[ $tax_name ] = $tax_obj; 231 } 216 232 } 217 233 } … … 236 252 global $wp_taxonomies; 237 253 238 if ( ! taxonomy_exists( $taxonomy ) ) 254 if ( ! taxonomy_exists( $taxonomy ) ) { 239 255 return false; 240 241 return $wp_taxonomies[$taxonomy]; 256 } 257 258 return $wp_taxonomies[ $taxonomy ]; 242 259 } 243 260 … … 257 274 global $wp_taxonomies; 258 275 259 return isset( $wp_taxonomies[ $taxonomy] );276 return isset( $wp_taxonomies[ $taxonomy ] ); 260 277 } 261 278 … … 273 290 * @return bool Whether the taxonomy is hierarchical. 274 291 */ 275 function is_taxonomy_hierarchical( $taxonomy) {276 if ( ! taxonomy_exists( $taxonomy) )292 function is_taxonomy_hierarchical( $taxonomy ) { 293 if ( ! taxonomy_exists( $taxonomy ) ) { 277 294 return false; 278 279 $taxonomy = get_taxonomy($taxonomy); 295 } 296 297 $taxonomy = get_taxonomy( $taxonomy ); 280 298 return $taxonomy->hierarchical; 281 299 } … … 377 395 global $wp_taxonomies; 378 396 379 if ( ! is_array( $wp_taxonomies ) ) 397 if ( ! is_array( $wp_taxonomies ) ) { 380 398 $wp_taxonomies = array(); 399 } 381 400 382 401 $args = wp_parse_args( $args ); … … 393 412 394 413 $taxonomy_object->add_hooks(); 395 396 414 397 415 /** … … 500 518 $tax->labels = (array) $tax->labels; 501 519 502 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) 520 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) { 503 521 $tax->labels['separate_items_with_commas'] = $tax->helps; 504 505 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) 522 } 523 524 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) { 506 525 $tax->labels['not_found'] = $tax->no_tagcloud; 526 } 507 527 508 528 $nohier_vs_hier_defaults = array( 509 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),510 'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),511 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),512 'popular_items' => array( __( 'Popular Tags' ), null ),513 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),514 'parent_item' => array( null, __( 'Parent Category' ) ),515 'parent_item_colon' => array( null, __( 'Parent Category:' ) ),516 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),517 'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),518 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),519 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),520 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),529 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ), 530 'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ), 531 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ), 532 'popular_items' => array( __( 'Popular Tags' ), null ), 533 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ), 534 'parent_item' => array( null, __( 'Parent Category' ) ), 535 'parent_item_colon' => array( null, __( 'Parent Category:' ) ), 536 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ), 537 'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ), 538 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ), 539 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ), 540 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ), 521 541 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ), 522 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),523 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),524 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),525 'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ),526 'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),527 'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ),542 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ), 543 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ), 544 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ), 545 'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ), 546 'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ), 547 'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ), 528 548 /* translators: Tab heading when selecting from the most used terms */ 529 'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),530 'back_to_items' => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ),549 'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ), 550 'back_to_items' => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ), 531 551 ); 532 552 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; … … 568 588 * @return bool True if successful, false if not. 569 589 */ 570 function register_taxonomy_for_object_type( $taxonomy, $object_type ) {590 function register_taxonomy_for_object_type( $taxonomy, $object_type ) { 571 591 global $wp_taxonomies; 572 592 573 if ( ! isset($wp_taxonomies[$taxonomy]) )593 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { 574 594 return false; 575 576 if ( ! get_post_type_object($object_type) ) 595 } 596 597 if ( ! get_post_type_object( $object_type ) ) { 577 598 return false; 578 579 if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) ) 580 $wp_taxonomies[$taxonomy]->object_type[] = $object_type; 599 } 600 601 if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type ) ) { 602 $wp_taxonomies[ $taxonomy ]->object_type[] = $object_type; 603 } 581 604 582 605 // Filter out empties. … … 600 623 global $wp_taxonomies; 601 624 602 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) 625 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { 603 626 return false; 604 605 if ( ! get_post_type_object( $object_type ) ) 627 } 628 629 if ( ! get_post_type_object( $object_type ) ) { 606 630 return false; 631 } 607 632 608 633 $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); 609 if ( false === $key ) 634 if ( false === $key ) { 610 635 return false; 636 } 611 637 612 638 unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] ); … … 641 667 * @param array|string $args Change the order of the object_ids, either ASC or DESC. 642 668 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success. 643 * 669 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. 644 670 */ 645 671 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { … … 659 685 660 686 $defaults = array( 'order' => 'ASC' ); 661 $args = wp_parse_args( $args, $defaults );687 $args = wp_parse_args( $args, $defaults ); 662 688 663 689 $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; 664 690 665 $term_ids = array_map( 'intval', $term_ids );691 $term_ids = array_map( 'intval', $term_ids ); 666 692 667 693 $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 668 $term_ids = "'" . implode( "', '", $term_ids ) . "'";694 $term_ids = "'" . implode( "', '", $term_ids ) . "'"; 669 695 670 696 $sql = "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"; 671 697 672 698 $last_changed = wp_cache_get_last_changed( 'terms' ); 673 $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";674 $cache = wp_cache_get( $cache_key, 'terms' );699 $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed"; 700 $cache = wp_cache_get( $cache_key, 'terms' ); 675 701 if ( false === $cache ) { 676 702 $object_ids = $wpdb->get_col( $sql ); … … 680 706 } 681 707 682 if ( ! $object_ids ) {708 if ( ! $object_ids ) { 683 709 return array(); 684 710 } … … 888 914 889 915 switch ( $field ) { 890 case 'slug' 916 case 'slug': 891 917 $args['slug'] = $value; 892 918 break; 893 case 'name' 919 case 'name': 894 920 $args['name'] = $value; 895 921 break; 896 case 'term_taxonomy_id' 922 case 'term_taxonomy_id': 897 923 $args['term_taxonomy_id'] = $value; 898 unset( $args[ 'taxonomy'] );924 unset( $args['taxonomy'] ); 899 925 break; 900 default 926 default: 901 927 return false; 902 928 } … … 938 964 $term_id = intval( $term_id ); 939 965 940 $terms = _get_term_hierarchy( $taxonomy);941 942 if ( ! isset( $terms[$term_id]) )966 $terms = _get_term_hierarchy( $taxonomy ); 967 968 if ( ! isset( $terms[ $term_id ] ) ) { 943 969 return array(); 944 945 $children = $terms[$term_id]; 946 947 foreach ( (array) $terms[$term_id] as $child ) { 970 } 971 972 $children = $terms[ $term_id ]; 973 974 foreach ( (array) $terms[ $term_id ] as $child ) { 948 975 if ( $term_id == $child ) { 949 976 continue; 950 977 } 951 978 952 if ( isset($terms[$child]) ) 953 $children = array_merge($children, get_term_children($child, $taxonomy)); 979 if ( isset( $terms[ $child ] ) ) { 980 $children = array_merge( $children, get_term_children( $child, $taxonomy ) ); 981 } 954 982 } 955 983 … … 975 1003 function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { 976 1004 $term = get_term( $term, $taxonomy ); 977 if ( is_wp_error( $term) )1005 if ( is_wp_error( $term ) ) { 978 1006 return $term; 979 980 if ( !is_object($term) ) 1007 } 1008 1009 if ( ! is_object( $term ) ) { 981 1010 return ''; 982 983 if ( !isset($term->$field) ) 1011 } 1012 1013 if ( ! isset( $term->$field ) ) { 984 1014 return ''; 1015 } 985 1016 986 1017 return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context ); … … 1002 1033 $term = get_term( $id, $taxonomy ); 1003 1034 1004 if ( is_wp_error( $term) )1035 if ( is_wp_error( $term ) ) { 1005 1036 return $term; 1006 1007 if ( !is_object($term) ) 1037 } 1038 1039 if ( ! is_object( $term ) ) { 1008 1040 return ''; 1009 1010 return sanitize_term($term, $taxonomy, 'edit'); 1041 } 1042 1043 return sanitize_term( $term, $taxonomy, 'edit' ); 1011 1044 } 1012 1045 … … 1074 1107 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies) 1075 1108 */ 1076 $_args = wp_parse_args( $args );1109 $_args = wp_parse_args( $args ); 1077 1110 $key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $_args ); 1078 1111 $do_legacy_args = $deprecated || empty( $key_intersect ); 1079 1112 1080 1113 if ( $do_legacy_args ) { 1081 $taxonomies = (array) $args;1082 $args = wp_parse_args( $deprecated, $defaults );1114 $taxonomies = (array) $args; 1115 $args = wp_parse_args( $deprecated, $defaults ); 1083 1116 $args['taxonomy'] = $taxonomies; 1084 1117 } else { … … 1146 1179 1147 1180 if ( wp_term_is_shared( $term_id ) ) { 1148 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );1181 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); 1149 1182 } 1150 1183 … … 1228 1261 1229 1262 if ( wp_term_is_shared( $term_id ) ) { 1230 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );1263 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); 1231 1264 } 1232 1265 … … 1302 1335 global $wpdb; 1303 1336 1304 $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";1337 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1305 1338 $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 "; 1306 1339 1307 if ( is_int( $term) ) {1308 if ( 0 == $term ) 1340 if ( is_int( $term ) ) { 1341 if ( 0 == $term ) { 1309 1342 return 0; 1343 } 1310 1344 $where = 't.term_id = %d'; 1311 if ( ! empty($taxonomy) )1312 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );1313 else1345 if ( ! empty( $taxonomy ) ) { 1346 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A ); 1347 } else { 1314 1348 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); 1349 } 1315 1350 } 1316 1351 … … 1318 1353 $slug = sanitize_title( $term ); 1319 1354 1320 $where = 't.slug = %s';1321 $else_where = 't.name = %s';1322 $where_fields = array($slug);1323 $else_where_fields = array( $term);1324 $orderby = 'ORDER BY t.term_id ASC';1325 $limit = 'LIMIT 1';1326 if ( ! empty($taxonomy) ) {1355 $where = 't.slug = %s'; 1356 $else_where = 't.name = %s'; 1357 $where_fields = array( $slug ); 1358 $else_where_fields = array( $term ); 1359 $orderby = 'ORDER BY t.term_id ASC'; 1360 $limit = 'LIMIT 1'; 1361 if ( ! empty( $taxonomy ) ) { 1327 1362 if ( is_numeric( $parent ) ) { 1328 $parent = (int) $parent;1329 $where_fields[] = $parent;1363 $parent = (int) $parent; 1364 $where_fields[] = $parent; 1330 1365 $else_where_fields[] = $parent; 1331 $where .= ' AND tt.parent = %d';1332 $else_where .= ' AND tt.parent = %d';1333 } 1334 1335 $where_fields[] = $taxonomy;1366 $where .= ' AND tt.parent = %d'; 1367 $else_where .= ' AND tt.parent = %d'; 1368 } 1369 1370 $where_fields[] = $taxonomy; 1336 1371 $else_where_fields[] = $taxonomy; 1337 1372 1338 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 $orderby $limit", $where_fields), ARRAY_A) )1373 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 $orderby $limit", $where_fields ), ARRAY_A ) ) { 1339 1374 return $result; 1340 1341 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 $orderby $limit", $else_where_fields), ARRAY_A); 1342 } 1343 1344 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) ) 1375 } 1376 1377 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 $orderby $limit", $else_where_fields ), ARRAY_A ); 1378 } 1379 1380 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) { 1345 1381 return $result; 1346 1347 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) ); 1382 } 1383 1384 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) ); 1348 1385 } 1349 1386 … … 1361 1398 */ 1362 1399 function term_is_ancestor_of( $term1, $term2, $taxonomy ) { 1363 if ( ! isset( $term1->term_id ) ) 1400 if ( ! isset( $term1->term_id ) ) { 1364 1401 $term1 = get_term( $term1, $taxonomy ); 1365 if ( ! isset( $term2->parent ) ) 1402 } 1403 if ( ! isset( $term2->parent ) ) { 1366 1404 $term2 = get_term( $term2, $taxonomy ); 1367 1368 if ( empty( $term1->term_id ) || empty( $term2->parent ) ) 1405 } 1406 1407 if ( empty( $term1->term_id ) || empty( $term2->parent ) ) { 1369 1408 return false; 1370 if ( $term2->parent == $term1->term_id ) 1409 } 1410 if ( $term2->parent == $term1->term_id ) { 1371 1411 return true; 1412 } 1372 1413 1373 1414 return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); … … 1391 1432 * @return array|object Term with all fields sanitized. 1392 1433 */ 1393 function sanitize_term( $term, $taxonomy, $context = 'display') {1434 function sanitize_term( $term, $taxonomy, $context = 'display' ) { 1394 1435 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); 1395 1436 1396 1437 $do_object = is_object( $term ); 1397 1438 1398 $term_id = $do_object ? $term->term_id : ( isset($term['term_id']) ? $term['term_id'] : 0);1439 $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 ); 1399 1440 1400 1441 foreach ( (array) $fields as $field ) { 1401 1442 if ( $do_object ) { 1402 if ( isset($term->$field) ) 1403 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); 1443 if ( isset( $term->$field ) ) { 1444 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); 1445 } 1404 1446 } else { 1405 if ( isset($term[$field]) ) 1406 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); 1407 } 1408 } 1409 1410 if ( $do_object ) 1447 if ( isset( $term[ $field ] ) ) { 1448 $term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context ); 1449 } 1450 } 1451 } 1452 1453 if ( $do_object ) { 1411 1454 $term->filter = $context; 1412 else1455 } else { 1413 1456 $term['filter'] = $context; 1457 } 1414 1458 1415 1459 return $term; … … 1439 1483 * @return mixed Sanitized field. 1440 1484 */ 1441 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context) {1485 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { 1442 1486 $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); 1443 1487 if ( in_array( $field, $int_fields ) ) { 1444 1488 $value = (int) $value; 1445 if ( $value < 0 ) 1489 if ( $value < 0 ) { 1446 1490 $value = 0; 1447 } 1448 1449 if ( 'raw' == $context ) 1491 } 1492 } 1493 1494 if ( 'raw' == $context ) { 1450 1495 return $value; 1496 } 1451 1497 1452 1498 if ( 'edit' == $context ) { … … 1478 1524 $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id ); 1479 1525 1480 if ( 'description' == $field ) 1481 $value = esc_html($value); // textarea_escaped 1482 else 1483 $value = esc_attr($value); 1526 if ( 'description' == $field ) { 1527 $value = esc_html( $value ); // textarea_escaped 1528 } else { 1529 $value = esc_attr( $value ); 1530 } 1484 1531 } elseif ( 'db' == $context ) { 1485 1532 … … 1521 1568 $value = apply_filters( 'pre_category_nicename', $value ); 1522 1569 } 1523 1524 1570 } elseif ( 'rss' == $context ) { 1525 1571 … … 1580 1626 1581 1627 if ( 'attribute' == $context ) { 1582 $value = esc_attr( $value);1628 $value = esc_attr( $value ); 1583 1629 } elseif ( 'js' == $context ) { 1584 $value = esc_js( $value);1630 $value = esc_js( $value ); 1585 1631 } 1586 1632 return $value; … … 1600 1646 */ 1601 1647 function wp_count_terms( $taxonomy, $args = array() ) { 1602 $defaults = array( 'hide_empty' => false);1603 $args = wp_parse_args($args, $defaults);1648 $defaults = array( 'hide_empty' => false ); 1649 $args = wp_parse_args( $args, $defaults ); 1604 1650 1605 1651 // backward compatibility 1606 if ( isset( $args['ignore_empty']) ) {1652 if ( isset( $args['ignore_empty'] ) ) { 1607 1653 $args['hide_empty'] = $args['ignore_empty']; 1608 unset( $args['ignore_empty']);1654 unset( $args['ignore_empty'] ); 1609 1655 } 1610 1656 1611 1657 $args['fields'] = 'count'; 1612 1658 1613 return get_terms( $taxonomy, $args);1659 return get_terms( $taxonomy, $args ); 1614 1660 } 1615 1661 … … 1629 1675 $object_id = (int) $object_id; 1630 1676 1631 if ( !is_array($taxonomies) ) 1632 $taxonomies = array($taxonomies); 1677 if ( ! is_array( $taxonomies ) ) { 1678 $taxonomies = array( $taxonomies ); 1679 } 1633 1680 1634 1681 foreach ( (array) $taxonomies as $taxonomy ) { … … 1671 1718 $term = (int) $term; 1672 1719 1673 if ( ! $ids = term_exists( $term, $taxonomy) )1720 if ( ! $ids = term_exists( $term, $taxonomy ) ) { 1674 1721 return false; 1675 if ( is_wp_error( $ids ) ) 1722 } 1723 if ( is_wp_error( $ids ) ) { 1676 1724 return $ids; 1725 } 1677 1726 1678 1727 $tt_id = $ids['term_taxonomy_id']; … … 1682 1731 if ( 'category' == $taxonomy ) { 1683 1732 $defaults['default'] = get_option( 'default_category' ); 1684 if ( $defaults['default'] == $term ) 1733 if ( $defaults['default'] == $term ) { 1685 1734 return 0; // Don't delete the default category 1686 } 1687 1688 $args = wp_parse_args($args, $defaults); 1735 } 1736 } 1737 1738 $args = wp_parse_args( $args, $defaults ); 1689 1739 1690 1740 if ( isset( $args['default'] ) ) { … … 1710 1760 1711 1761 // Update children to point to new parent 1712 if ( is_taxonomy_hierarchical( $taxonomy) ) {1713 $term_obj = get_term( $term, $taxonomy);1714 if ( is_wp_error( $term_obj ) ) 1762 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 1763 $term_obj = get_term( $term, $taxonomy ); 1764 if ( is_wp_error( $term_obj ) ) { 1715 1765 return $term_obj; 1766 } 1716 1767 $parent = $term_obj->parent; 1717 1768 1718 $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );1769 $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id ); 1719 1770 $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); 1720 1771 … … 1728 1779 do_action( 'edit_term_taxonomies', $edit_tt_ids ); 1729 1780 1730 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );1781 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); 1731 1782 1732 1783 // Clean the cache for all child terms. … … 1750 1801 1751 1802 foreach ( $object_ids as $object_id ) { 1752 $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 1753 if ( 1 == count($terms) && isset($default) ) { 1754 $terms = array($default); 1803 $terms = wp_get_object_terms( 1804 $object_id, $taxonomy, array( 1805 'fields' => 'ids', 1806 'orderby' => 'none', 1807 ) 1808 ); 1809 if ( 1 == count( $terms ) && isset( $default ) ) { 1810 $terms = array( $default ); 1755 1811 } else { 1756 $terms = array_diff($terms, array($term)); 1757 if (isset($default) && isset($force_default) && $force_default) 1758 $terms = array_merge($terms, array($default)); 1759 } 1760 $terms = array_map('intval', $terms); 1812 $terms = array_diff( $terms, array( $term ) ); 1813 if ( isset( $default ) && isset( $force_default ) && $force_default ) { 1814 $terms = array_merge( $terms, array( $default ) ); 1815 } 1816 } 1817 $terms = array_map( 'intval', $terms ); 1761 1818 wp_set_object_terms( $object_id, $terms, $taxonomy ); 1762 1819 } … … 1764 1821 // Clean the relationship caches for all object types using this term. 1765 1822 $tax_object = get_taxonomy( $taxonomy ); 1766 foreach ( $tax_object->object_type as $object_type ) 1823 foreach ( $tax_object->object_type as $object_type ) { 1767 1824 clean_object_term_cache( $object_ids, $object_type ); 1825 } 1768 1826 1769 1827 $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); … … 1792 1850 1793 1851 // Delete the term if no taxonomies use it. 1794 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )1852 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) { 1795 1853 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); 1796 1797 clean_term_cache($term, $taxonomy); 1854 } 1855 1856 clean_term_cache( $term, $taxonomy ); 1798 1857 1799 1858 /** … … 1839 1898 * @param int $cat_ID Category term ID. 1840 1899 * @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist; 1841 * 1900 * Zero on attempted deletion of default Category; WP_Error object is also a possibility. 1842 1901 */ 1843 1902 function wp_delete_category( $cat_ID ) { … … 1861 1920 * WP_Error if any of the $taxonomies don't exist. 1862 1921 */ 1863 function wp_get_object_terms( $object_ids, $taxonomies, $args = array()) {1864 if ( empty( $object_ids ) || empty( $taxonomies ) ) 1922 function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 1923 if ( empty( $object_ids ) || empty( $taxonomies ) ) { 1865 1924 return array(); 1866 1867 if ( !is_array($taxonomies) ) 1868 $taxonomies = array($taxonomies); 1925 } 1926 1927 if ( ! is_array( $taxonomies ) ) { 1928 $taxonomies = array( $taxonomies ); 1929 } 1869 1930 1870 1931 foreach ( $taxonomies as $taxonomy ) { 1871 if ( ! taxonomy_exists( $taxonomy) )1932 if ( ! taxonomy_exists( $taxonomy ) ) { 1872 1933 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 1873 } 1874 1875 if ( !is_array($object_ids) ) 1876 $object_ids = array($object_ids); 1877 $object_ids = array_map('intval', $object_ids); 1934 } 1935 } 1936 1937 if ( ! is_array( $object_ids ) ) { 1938 $object_ids = array( $object_ids ); 1939 } 1940 $object_ids = array_map( 'intval', $object_ids ); 1878 1941 1879 1942 $args = wp_parse_args( $args ); … … 1911 1974 } 1912 1975 1913 $args['taxonomy'] = $taxonomies;1976 $args['taxonomy'] = $taxonomies; 1914 1977 $args['object_ids'] = $object_ids; 1915 1978 … … 2005 2068 global $wpdb; 2006 2069 2007 if ( ! taxonomy_exists( $taxonomy) ) {2070 if ( ! taxonomy_exists( $taxonomy ) ) { 2008 2071 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 2009 2072 } … … 2026 2089 return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); 2027 2090 } 2028 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); 2029 $args = wp_parse_args( $args, $defaults ); 2091 $defaults = array( 2092 'alias_of' => '', 2093 'description' => '', 2094 'parent' => 0, 2095 'slug' => '', 2096 ); 2097 $args = wp_parse_args( $args, $defaults ); 2030 2098 2031 2099 if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { … … 2033 2101 } 2034 2102 2035 $args['name'] = $term;2103 $args['name'] = $term; 2036 2104 $args['taxonomy'] = $taxonomy; 2037 2105 … … 2039 2107 $args['description'] = (string) $args['description']; 2040 2108 2041 $args = sanitize_term( $args, $taxonomy, 'db');2109 $args = sanitize_term( $args, $taxonomy, 'db' ); 2042 2110 2043 2111 // expected_slashed ($name) 2044 $name = wp_unslash( $args['name'] );2112 $name = wp_unslash( $args['name'] ); 2045 2113 $description = wp_unslash( $args['description'] ); 2046 $parent = (int) $args['parent'];2114 $parent = (int) $args['parent']; 2047 2115 2048 2116 $slug_provided = ! empty( $args['slug'] ); … … 2064 2132 * and add the alias to it. 2065 2133 */ 2066 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; 2067 2068 wp_update_term( $alias->term_id, $taxonomy, array( 2069 'term_group' => $term_group, 2070 ) ); 2134 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 2135 2136 wp_update_term( 2137 $alias->term_id, $taxonomy, array( 2138 'term_group' => $term_group, 2139 ) 2140 ); 2071 2141 } 2072 2142 } … … 2076 2146 * unless a unique slug has been explicitly provided. 2077 2147 */ 2078 $name_matches = get_terms( $taxonomy, array( 2079 'name' => $name, 2080 'hide_empty' => false, 2081 'parent' => $args['parent'], 2082 ) ); 2148 $name_matches = get_terms( 2149 $taxonomy, array( 2150 'name' => $name, 2151 'hide_empty' => false, 2152 'parent' => $args['parent'], 2153 ) 2154 ); 2083 2155 2084 2156 /* … … 2100 2172 if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { 2101 2173 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 2102 $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) ); 2174 $siblings = get_terms( 2175 $taxonomy, array( 2176 'get' => 'all', 2177 'parent' => $parent, 2178 ) 2179 ); 2103 2180 2104 2181 $existing_term = null; … … 2140 2217 2141 2218 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. 2142 if ( empty( $slug) ) {2143 $slug = sanitize_title( $slug, $term_id);2219 if ( empty( $slug ) ) { 2220 $slug = sanitize_title( $slug, $term_id ); 2144 2221 2145 2222 /** This action is documented in wp-includes/taxonomy.php */ … … 2153 2230 $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 ) ); 2154 2231 2155 if ( !empty($tt_id) ) { 2156 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); 2157 } 2158 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) ); 2232 if ( ! empty( $tt_id ) ) { 2233 return array( 2234 'term_id' => $term_id, 2235 'term_taxonomy_id' => $tt_id, 2236 ); 2237 } 2238 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ); 2159 2239 $tt_id = (int) $wpdb->insert_id; 2160 2240 … … 2174 2254 2175 2255 clean_term_cache( $term_id, $taxonomy ); 2176 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 2256 return array( 2257 'term_id' => $term_id, 2258 'term_taxonomy_id' => $tt_id, 2259 ); 2177 2260 } 2178 2261 … … 2186 2269 * @param string $taxonomy Taxonomy slug. 2187 2270 */ 2188 do_action( "create_term", $term_id, $tt_id, $taxonomy );2271 do_action( 'create_term', $term_id, $tt_id, $taxonomy ); 2189 2272 2190 2273 /** … … 2211 2294 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 2212 2295 2213 clean_term_cache( $term_id, $taxonomy);2296 clean_term_cache( $term_id, $taxonomy ); 2214 2297 2215 2298 /** … … 2237 2320 do_action( "created_{$taxonomy}", $term_id, $tt_id ); 2238 2321 2239 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); 2322 return array( 2323 'term_id' => $term_id, 2324 'term_taxonomy_id' => $tt_id, 2325 ); 2240 2326 } 2241 2327 … … 2272 2358 } 2273 2359 2274 if ( !is_array($terms) ) 2275 $terms = array($terms); 2276 2277 if ( ! $append ) 2278 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); 2279 else 2360 if ( ! is_array( $terms ) ) { 2361 $terms = array( $terms ); 2362 } 2363 2364 if ( ! $append ) { 2365 $old_tt_ids = wp_get_object_terms( 2366 $object_id, $taxonomy, array( 2367 'fields' => 'tt_ids', 2368 'orderby' => 'none', 2369 ) 2370 ); 2371 } else { 2280 2372 $old_tt_ids = array(); 2281 2282 $tt_ids = array(); 2283 $term_ids = array(); 2373 } 2374 2375 $tt_ids = array(); 2376 $term_ids = array(); 2284 2377 $new_tt_ids = array(); 2285 2378 2286 foreach ( (array) $terms as $term ) {2287 if ( ! strlen(trim($term)) )2379 foreach ( (array) $terms as $term ) { 2380 if ( ! strlen( trim( $term ) ) ) { 2288 2381 continue; 2289 2290 if ( !$term_info = term_exists($term, $taxonomy) ) { 2382 } 2383 2384 if ( ! $term_info = term_exists( $term, $taxonomy ) ) { 2291 2385 // Skip if a non-existent term ID is passed. 2292 if ( is_int( $term) )2386 if ( is_int( $term ) ) { 2293 2387 continue; 2294 $term_info = wp_insert_term($term, $taxonomy); 2295 } 2296 if ( is_wp_error($term_info) ) 2388 } 2389 $term_info = wp_insert_term( $term, $taxonomy ); 2390 } 2391 if ( is_wp_error( $term_info ) ) { 2297 2392 return $term_info; 2393 } 2298 2394 $term_ids[] = $term_info['term_id']; 2299 $tt_id = $term_info['term_taxonomy_id'];2300 $tt_ids[] = $tt_id;2301 2302 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 ) ) ) 2395 $tt_id = $term_info['term_taxonomy_id']; 2396 $tt_ids[] = $tt_id; 2397 2398 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 ) ) ) { 2303 2399 continue; 2400 } 2304 2401 2305 2402 /** … … 2314 2411 */ 2315 2412 do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy ); 2316 $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) ); 2413 $wpdb->insert( 2414 $wpdb->term_relationships, array( 2415 'object_id' => $object_id, 2416 'term_taxonomy_id' => $tt_id, 2417 ) 2418 ); 2317 2419 2318 2420 /** … … 2330 2432 } 2331 2433 2332 if ( $new_tt_ids ) 2434 if ( $new_tt_ids ) { 2333 2435 wp_update_term_count( $new_tt_ids, $taxonomy ); 2436 } 2334 2437 2335 2438 if ( ! $append ) { … … 2338 2441 if ( $delete_tt_ids ) { 2339 2442 $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; 2340 $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );2341 $delete_term_ids = array_map( 'intval', $delete_term_ids );2443 $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) ); 2444 $delete_term_ids = array_map( 'intval', $delete_term_ids ); 2342 2445 2343 2446 $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); … … 2348 2451 } 2349 2452 2350 $t = get_taxonomy($taxonomy); 2351 if ( ! $append && isset($t->sort) && $t->sort ) { 2352 $values = array(); 2353 $term_order = 0; 2354 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); 2355 foreach ( $tt_ids as $tt_id ) 2356 if ( in_array($tt_id, $final_tt_ids) ) 2357 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order); 2358 if ( $values ) 2359 if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" ) ) 2453 $t = get_taxonomy( $taxonomy ); 2454 if ( ! $append && isset( $t->sort ) && $t->sort ) { 2455 $values = array(); 2456 $term_order = 0; 2457 $final_tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) ); 2458 foreach ( $tt_ids as $tt_id ) { 2459 if ( in_array( $tt_id, $final_tt_ids ) ) { 2460 $values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order ); 2461 } 2462 } 2463 if ( $values ) { 2464 if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) { 2360 2465 return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error ); 2466 } 2467 } 2361 2468 } 2362 2469 … … 2505 2612 global $wpdb; 2506 2613 2507 $needs_suffix = true;2614 $needs_suffix = true; 2508 2615 $original_slug = $slug; 2509 2616 … … 2520 2627 if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) { 2521 2628 $the_parent = $term->parent; 2522 while ( ! empty( $the_parent) ) {2523 $parent_term = get_term( $the_parent, $term->taxonomy);2524 if ( is_wp_error( $parent_term) || empty($parent_term) )2629 while ( ! empty( $the_parent ) ) { 2630 $parent_term = get_term( $the_parent, $term->taxonomy ); 2631 if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) { 2525 2632 break; 2633 } 2526 2634 $parent_suffix .= '-' . $parent_term->slug; 2527 2635 if ( ! term_exists( $slug . $parent_suffix ) ) { … … 2529 2637 } 2530 2638 2531 if ( empty( $parent_term->parent) )2639 if ( empty( $parent_term->parent ) ) { 2532 2640 break; 2641 } 2533 2642 $the_parent = $parent_term->parent; 2534 2643 } … … 2550 2659 $slug .= $parent_suffix; 2551 2660 } else { 2552 if ( ! empty( $term->term_id ) ) 2661 if ( ! empty( $term->term_id ) ) { 2553 2662 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); 2554 else2663 } else { 2555 2664 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); 2665 } 2556 2666 2557 2667 if ( $wpdb->get_var( $query ) ) { … … 2635 2745 2636 2746 // Merge old and new args with new args overwriting old ones. 2637 $args = array_merge($term, $args); 2638 2639 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); 2640 $args = wp_parse_args($args, $defaults); 2641 $args = sanitize_term($args, $taxonomy, 'db'); 2747 $args = array_merge( $term, $args ); 2748 2749 $defaults = array( 2750 'alias_of' => '', 2751 'description' => '', 2752 'parent' => 0, 2753 'slug' => '', 2754 ); 2755 $args = wp_parse_args( $args, $defaults ); 2756 $args = sanitize_term( $args, $taxonomy, 'db' ); 2642 2757 $parsed_args = $args; 2643 2758 2644 2759 // expected_slashed ($name) 2645 $name = wp_unslash( $args['name'] );2760 $name = wp_unslash( $args['name'] ); 2646 2761 $description = wp_unslash( $args['description'] ); 2647 2762 2648 $parsed_args['name'] = $name;2763 $parsed_args['name'] = $name; 2649 2764 $parsed_args['description'] = $description; 2650 2765 … … 2660 2775 if ( empty( $args['slug'] ) ) { 2661 2776 $empty_slug = true; 2662 $slug = sanitize_title($name);2777 $slug = sanitize_title( $name ); 2663 2778 } else { 2664 2779 $slug = $args['slug']; … … 2678 2793 * and add the alias to it. 2679 2794 */ 2680 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; 2681 2682 wp_update_term( $alias->term_id, $taxonomy, array( 2683 'term_group' => $term_group, 2684 ) ); 2795 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 2796 2797 wp_update_term( 2798 $alias->term_id, $taxonomy, array( 2799 'term_group' => $term_group, 2800 ) 2801 ); 2685 2802 } 2686 2803 … … 2708 2825 // If an empty slug was passed or the parent changed, reset the slug to something unique. 2709 2826 // Otherwise, bail. 2710 if ( $empty_slug || ( $parent != $term['parent'] ) ) {2711 $slug = wp_unique_term_slug( $slug, (object) $args);2827 if ( $empty_slug || ( $parent != $term['parent'] ) ) { 2828 $slug = wp_unique_term_slug( $slug, (object) $args ); 2712 2829 } else { 2713 2830 /* translators: 1: Taxonomy term slug */ … … 2716 2833 } 2717 2834 2718 $tt_id = (int) $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 ) );2835 $tt_id = (int) $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 ) ); 2719 2836 2720 2837 // Check whether this is a shared term that needs splitting. … … 2749 2866 2750 2867 $wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) ); 2751 if ( empty( $slug) ) {2752 $slug = sanitize_title( $name, $term_id);2868 if ( empty( $slug ) ) { 2869 $slug = sanitize_title( $name, $term_id ); 2753 2870 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 2754 2871 } … … 2795 2912 * @param string $taxonomy Taxonomy slug. 2796 2913 */ 2797 do_action( "edit_term", $term_id, $tt_id, $taxonomy );2914 do_action( 'edit_term', $term_id, $tt_id, $taxonomy ); 2798 2915 2799 2916 /** … … 2813 2930 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 2814 2931 2815 clean_term_cache( $term_id, $taxonomy);2932 clean_term_cache( $term_id, $taxonomy ); 2816 2933 2817 2934 /** … … 2824 2941 * @param string $taxonomy Taxonomy slug. 2825 2942 */ 2826 do_action( "edited_term", $term_id, $tt_id, $taxonomy );2943 do_action( 'edited_term', $term_id, $tt_id, $taxonomy ); 2827 2944 2828 2945 /** … … 2839 2956 do_action( "edited_{$taxonomy}", $term_id, $tt_id ); 2840 2957 2841 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); 2958 return array( 2959 'term_id' => $term_id, 2960 'term_taxonomy_id' => $tt_id, 2961 ); 2842 2962 } 2843 2963 … … 2852 2972 * @return bool Whether term counting is enabled or disabled. 2853 2973 */ 2854 function wp_defer_term_counting( $defer=null) {2974 function wp_defer_term_counting( $defer = null ) { 2855 2975 static $_defer = false; 2856 2976 2857 if ( is_bool( $defer) ) {2977 if ( is_bool( $defer ) ) { 2858 2978 $_defer = $defer; 2859 2979 // flush any deferred counts 2860 if ( ! $defer )2980 if ( ! $defer ) { 2861 2981 wp_update_term_count( null, null, true ); 2982 } 2862 2983 } 2863 2984 … … 2887 3008 2888 3009 if ( $do_deferred ) { 2889 foreach ( (array) array_keys( $_deferred) as $tax ) {2890 wp_update_term_count_now( $_deferred[ $tax], $tax );2891 unset( $_deferred[ $tax] );2892 } 2893 } 2894 2895 if ( empty( $terms) )3010 foreach ( (array) array_keys( $_deferred ) as $tax ) { 3011 wp_update_term_count_now( $_deferred[ $tax ], $tax ); 3012 unset( $_deferred[ $tax ] ); 3013 } 3014 } 3015 3016 if ( empty( $terms ) ) { 2896 3017 return false; 2897 2898 if ( !is_array($terms) ) 2899 $terms = array($terms); 3018 } 3019 3020 if ( ! is_array( $terms ) ) { 3021 $terms = array( $terms ); 3022 } 2900 3023 2901 3024 if ( wp_defer_term_counting() ) { 2902 if ( !isset($_deferred[$taxonomy]) ) 2903 $_deferred[$taxonomy] = array(); 2904 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); 3025 if ( ! isset( $_deferred[ $taxonomy ] ) ) { 3026 $_deferred[ $taxonomy ] = array(); 3027 } 3028 $_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) ); 2905 3029 return true; 2906 3030 } … … 2919 3043 */ 2920 3044 function wp_update_term_count_now( $terms, $taxonomy ) { 2921 $terms = array_map( 'intval', $terms);2922 2923 $taxonomy = get_taxonomy( $taxonomy);2924 if ( ! empty($taxonomy->update_count_callback) ) {2925 call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy);3045 $terms = array_map( 'intval', $terms ); 3046 3047 $taxonomy = get_taxonomy( $taxonomy ); 3048 if ( ! empty( $taxonomy->update_count_callback ) ) { 3049 call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy ); 2926 3050 } else { 2927 3051 $object_types = (array) $taxonomy->object_type; 2928 3052 foreach ( $object_types as &$object_type ) { 2929 if ( 0 === strpos( $object_type, 'attachment:' ) ) 3053 if ( 0 === strpos( $object_type, 'attachment:' ) ) { 2930 3054 list( $object_type ) = explode( ':', $object_type ); 3055 } 2931 3056 } 2932 3057 … … 2940 3065 } 2941 3066 2942 clean_term_cache( $terms, '', false);3067 clean_term_cache( $terms, '', false ); 2943 3068 2944 3069 return true; … … 2965 3090 * @param array|string $object_type The taxonomy object type. 2966 3091 */ 2967 function clean_object_term_cache( $object_ids, $object_type) {3092 function clean_object_term_cache( $object_ids, $object_type ) { 2968 3093 global $_wp_suspend_cache_invalidation; 2969 3094 … … 2972 3097 } 2973 3098 2974 if ( !is_array($object_ids) ) 2975 $object_ids = array($object_ids); 3099 if ( ! is_array( $object_ids ) ) { 3100 $object_ids = array( $object_ids ); 3101 } 2976 3102 2977 3103 $taxonomies = get_object_taxonomies( $object_type ); … … 2979 3105 foreach ( $object_ids as $id ) { 2980 3106 foreach ( $taxonomies as $taxonomy ) { 2981 wp_cache_delete( $id, "{$taxonomy}_relationships");3107 wp_cache_delete( $id, "{$taxonomy}_relationships" ); 2982 3108 } 2983 3109 } … … 3008 3134 * term object caches (false). Default true. 3009 3135 */ 3010 function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true) {3136 function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) { 3011 3137 global $wpdb, $_wp_suspend_cache_invalidation; 3012 3138 … … 3015 3141 } 3016 3142 3017 if ( !is_array($ids) ) 3018 $ids = array($ids); 3143 if ( ! is_array( $ids ) ) { 3144 $ids = array( $ids ); 3145 } 3019 3146 3020 3147 $taxonomies = array(); 3021 3148 // If no taxonomy, assume tt_ids. 3022 if ( empty( $taxonomy) ) {3023 $tt_ids = array_map( 'intval', $ids);3024 $tt_ids = implode( ', ', $tt_ids);3025 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");3026 $ids = array();3149 if ( empty( $taxonomy ) ) { 3150 $tt_ids = array_map( 'intval', $ids ); 3151 $tt_ids = implode( ', ', $tt_ids ); 3152 $terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" ); 3153 $ids = array(); 3027 3154 foreach ( (array) $terms as $term ) { 3028 3155 $taxonomies[] = $term->taxonomy; 3029 $ids[] = $term->term_id;3156 $ids[] = $term->term_id; 3030 3157 wp_cache_delete( $term->term_id, 'terms' ); 3031 3158 } 3032 $taxonomies = array_unique( $taxonomies);3159 $taxonomies = array_unique( $taxonomies ); 3033 3160 } else { 3034 $taxonomies = array( $taxonomy);3161 $taxonomies = array( $taxonomy ); 3035 3162 foreach ( $taxonomies as $taxonomy ) { 3036 3163 foreach ( $ids as $id ) { … … 3153 3280 * @return void|false False if all of the terms in `$object_ids` are already cached. 3154 3281 */ 3155 function update_object_term_cache( $object_ids, $object_type) {3156 if ( empty( $object_ids) )3282 function update_object_term_cache( $object_ids, $object_type ) { 3283 if ( empty( $object_ids ) ) { 3157 3284 return; 3158 3159 if ( !is_array($object_ids) ) 3160 $object_ids = explode(',', $object_ids); 3161 3162 $object_ids = array_map('intval', $object_ids); 3163 3164 $taxonomies = get_object_taxonomies($object_type); 3285 } 3286 3287 if ( ! is_array( $object_ids ) ) { 3288 $object_ids = explode( ',', $object_ids ); 3289 } 3290 3291 $object_ids = array_map( 'intval', $object_ids ); 3292 3293 $taxonomies = get_object_taxonomies( $object_type ); 3165 3294 3166 3295 $ids = array(); 3167 3296 foreach ( (array) $object_ids as $id ) { 3168 3297 foreach ( $taxonomies as $taxonomy ) { 3169 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships") ) {3298 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships" ) ) { 3170 3299 $ids[] = $id; 3171 3300 break; … … 3174 3303 } 3175 3304 3176 if ( empty( $ids ) ) 3305 if ( empty( $ids ) ) { 3177 3306 return false; 3178 3179 $terms = wp_get_object_terms( $ids, $taxonomies, array( 3180 'fields' => 'all_with_object_id', 3181 'orderby' => 'name', 3182 'update_term_meta_cache' => false, 3183 ) ); 3307 } 3308 3309 $terms = wp_get_object_terms( 3310 $ids, $taxonomies, array( 3311 'fields' => 'all_with_object_id', 3312 'orderby' => 'name', 3313 'update_term_meta_cache' => false, 3314 ) 3315 ); 3184 3316 3185 3317 $object_terms = array(); … … 3190 3322 foreach ( $ids as $id ) { 3191 3323 foreach ( $taxonomies as $taxonomy ) { 3192 if ( ! isset($object_terms[$id][$taxonomy]) ) { 3193 if ( !isset($object_terms[$id]) ) 3194 $object_terms[$id] = array(); 3195 $object_terms[$id][$taxonomy] = array(); 3324 if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) { 3325 if ( ! isset( $object_terms[ $id ] ) ) { 3326 $object_terms[ $id ] = array(); 3327 } 3328 $object_terms[ $id ][ $taxonomy ] = array(); 3196 3329 } 3197 3330 } … … 3239 3372 */ 3240 3373 function _get_term_hierarchy( $taxonomy ) { 3241 if ( ! is_taxonomy_hierarchical($taxonomy) )3374 if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { 3242 3375 return array(); 3243 $children = get_option("{$taxonomy}_children"); 3244 3245 if ( is_array($children) ) 3376 } 3377 $children = get_option( "{$taxonomy}_children" ); 3378 3379 if ( is_array( $children ) ) { 3246 3380 return $children; 3381 } 3247 3382 $children = array(); 3248 $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent')); 3383 $terms = get_terms( 3384 $taxonomy, array( 3385 'get' => 'all', 3386 'orderby' => 'id', 3387 'fields' => 'id=>parent', 3388 ) 3389 ); 3249 3390 foreach ( $terms as $term_id => $parent ) { 3250 if ( $parent > 0 ) 3251 $children[$parent][] = $term_id; 3252 } 3253 update_option("{$taxonomy}_children", $children); 3391 if ( $parent > 0 ) { 3392 $children[ $parent ][] = $term_id; 3393 } 3394 } 3395 update_option( "{$taxonomy}_children", $children ); 3254 3396 3255 3397 return $children; … … 3277 3419 function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { 3278 3420 $empty_array = array(); 3279 if ( empty( $terms) )3421 if ( empty( $terms ) ) { 3280 3422 return $empty_array; 3281 3282 $term_list = array(); 3283 $has_children = _get_term_hierarchy($taxonomy); 3284 3285 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) 3423 } 3424 3425 $term_list = array(); 3426 $has_children = _get_term_hierarchy( $taxonomy ); 3427 3428 if ( ( 0 != $term_id ) && ! isset( $has_children[ $term_id ] ) ) { 3286 3429 return $empty_array; 3430 } 3287 3431 3288 3432 // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. … … 3293 3437 foreach ( (array) $terms as $term ) { 3294 3438 $use_id = false; 3295 if ( ! is_object($term) ) {3296 $term = get_term( $term, $taxonomy);3297 if ( is_wp_error( $term ) ) 3439 if ( ! is_object( $term ) ) { 3440 $term = get_term( $term, $taxonomy ); 3441 if ( is_wp_error( $term ) ) { 3298 3442 return $term; 3443 } 3299 3444 $use_id = true; 3300 3445 } … … 3306 3451 3307 3452 if ( $term->parent == $term_id ) { 3308 if ( $use_id ) 3453 if ( $use_id ) { 3309 3454 $term_list[] = $term->term_id; 3310 else3455 } else { 3311 3456 $term_list[] = $term; 3312 3313 if ( !isset($has_children[$term->term_id]) ) 3457 } 3458 3459 if ( ! isset( $has_children[ $term->term_id ] ) ) { 3314 3460 continue; 3461 } 3315 3462 3316 3463 $ancestors[ $term->term_id ] = 1; 3317 3464 3318 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) ) 3319 $term_list = array_merge($term_list, $children); 3465 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ) ) { 3466 $term_list = array_merge( $term_list, $children ); 3467 } 3320 3468 } 3321 3469 } … … 3342 3490 3343 3491 // This function only works for hierarchical taxonomies like post categories. 3344 if ( ! is_taxonomy_hierarchical( $taxonomy ) )3492 if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { 3345 3493 return; 3346 3347 $term_hier = _get_term_hierarchy($taxonomy); 3348 3349 if ( empty($term_hier) ) 3494 } 3495 3496 $term_hier = _get_term_hierarchy( $taxonomy ); 3497 3498 if ( empty( $term_hier ) ) { 3350 3499 return; 3351 3352 $term_items = array(); 3500 } 3501 3502 $term_items = array(); 3353 3503 $terms_by_id = array(); 3354 $term_ids = array();3504 $term_ids = array(); 3355 3505 3356 3506 foreach ( (array) $terms as $key => $term ) { 3357 $terms_by_id[ $term->term_id] = & $terms[$key];3358 $term_ids[ $term->term_taxonomy_id] = $term->term_id;3507 $terms_by_id[ $term->term_id ] = & $terms[ $key ]; 3508 $term_ids[ $term->term_taxonomy_id ] = $term->term_id; 3359 3509 } 3360 3510 3361 3511 // Get the object and term ids and stick them in a lookup table. 3362 $tax_obj = get_taxonomy($taxonomy);3363 $object_types = esc_sql( $tax_obj->object_type);3364 $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'");3512 $tax_obj = get_taxonomy( $taxonomy ); 3513 $object_types = esc_sql( $tax_obj->object_type ); 3514 $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'" ); 3365 3515 foreach ( $results as $row ) { 3366 $id = $term_ids[$row->term_taxonomy_id];3367 $term_items[ $id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;3516 $id = $term_ids[ $row->term_taxonomy_id ]; 3517 $term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1; 3368 3518 } 3369 3519 3370 3520 // Touch every ancestor's lookup row for each post in each term. 3371 3521 foreach ( $term_ids as $term_id ) { 3372 $child = $term_id;3522 $child = $term_id; 3373 3523 $ancestors = array(); 3374 while ( ! empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {3524 while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) { 3375 3525 $ancestors[] = $child; 3376 if ( ! empty( $term_items[$term_id] ) )3377 foreach ( $term_items[ $term_id] as $item_id => $touches ) {3378 $term_items[ $parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;3526 if ( ! empty( $term_items[ $term_id ] ) ) { 3527 foreach ( $term_items[ $term_id ] as $item_id => $touches ) { 3528 $term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1; 3379 3529 } 3530 } 3380 3531 $child = $parent; 3381 3532 … … 3387 3538 3388 3539 // Transfer the touched cells. 3389 foreach ( (array) $term_items as $id => $items ) 3390 if ( isset($terms_by_id[$id]) ) 3391 $terms_by_id[$id]->count = count($items); 3540 foreach ( (array) $term_items as $id => $items ) { 3541 if ( isset( $terms_by_id[ $id ] ) ) { 3542 $terms_by_id[ $id ]->count = count( $items ); 3543 } 3544 } 3392 3545 } 3393 3546 … … 3408 3561 $non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' ); 3409 3562 if ( ! empty( $non_cached_ids ) ) { 3410 $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );3563 $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); 3411 3564 3412 3565 update_term_cache( $fresh_terms, $update_meta_cache ); … … 3441 3594 $object_types = (array) $taxonomy->object_type; 3442 3595 3443 foreach ( $object_types as &$object_type ) 3596 foreach ( $object_types as &$object_type ) { 3444 3597 list( $object_type ) = explode( ':', $object_type ); 3598 } 3445 3599 3446 3600 $object_types = array_unique( $object_types ); … … 3451 3605 } 3452 3606 3453 if ( $object_types ) 3607 if ( $object_types ) { 3454 3608 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 3609 } 3455 3610 3456 3611 foreach ( (array) $terms as $term ) { … … 3458 3613 3459 3614 // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 3460 if ( $check_attachments ) 3615 if ( $check_attachments ) { 3461 3616 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); 3462 3463 if ( $object_types ) 3464 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 3617 } 3618 3619 if ( $object_types ) { 3620 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 3621 } 3465 3622 3466 3623 /** This action is documented in wp-includes/taxonomy.php */ … … 3528 3685 if ( is_object( $term_id ) ) { 3529 3686 $shared_term = $term_id; 3530 $term_id = intval( $shared_term->term_id );3687 $term_id = intval( $shared_term->term_id ); 3531 3688 } 3532 3689 3533 3690 if ( is_object( $term_taxonomy_id ) ) { 3534 $term_taxonomy = $term_taxonomy_id;3691 $term_taxonomy = $term_taxonomy_id; 3535 3692 $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id ); 3536 3693 } … … 3558 3715 3559 3716 $new_term_data = array( 3560 'name' => $shared_term->name,3561 'slug' => $shared_term->slug,3717 'name' => $shared_term->name, 3718 'slug' => $shared_term->slug, 3562 3719 'term_group' => $shared_term->term_group, 3563 3720 ); … … 3570 3727 3571 3728 // Update the existing term_taxonomy to point to the newly created term. 3572 $wpdb->update( $wpdb->term_taxonomy, 3729 $wpdb->update( 3730 $wpdb->term_taxonomy, 3573 3731 array( 'term_id' => $new_term_id ), 3574 3732 array( 'term_taxonomy_id' => $term_taxonomy_id ) … … 3583 3741 if ( ! empty( $children_tt_ids ) ) { 3584 3742 foreach ( $children_tt_ids as $child_tt_id ) { 3585 $wpdb->update( $wpdb->term_taxonomy, 3743 $wpdb->update( 3744 $wpdb->term_taxonomy, 3586 3745 array( 'parent' => $new_term_id ), 3587 3746 array( 'term_taxonomy_id' => $child_tt_id ) … … 3604 3763 // Clean the cache for term taxonomies formerly shared with the current term. 3605 3764 $shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); 3606 $taxonomies_to_clean = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );3765 $taxonomies_to_clean = array_merge( $taxonomies_to_clean, $shared_term_taxonomies ); 3607 3766 3608 3767 foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) { … … 3698 3857 $_shared_terms = array(); 3699 3858 foreach ( $shared_terms as $shared_term ) { 3700 $term_id = intval( $shared_term->term_id );3859 $term_id = intval( $shared_term->term_id ); 3701 3860 $_shared_terms[ $term_id ] = $shared_term; 3702 3861 } … … 3705 3864 // Get term taxonomy data for all shared terms. 3706 3865 $shared_term_ids = implode( ',', array_keys( $shared_terms ) ); 3707 $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );3866 $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); 3708 3867 3709 3868 // Split term data recording is slow, so we do it just once, outside the loop. 3710 $split_term_data = get_option( '_split_terms', array() );3869 $split_term_data = get_option( '_split_terms', array() ); 3711 3870 $skipped_first_term = $taxonomies = array(); 3712 3871 foreach ( $shared_tts as $shared_tt ) { … … 3794 3953 function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { 3795 3954 global $wpdb; 3796 $post_ids = $wpdb->get_col( $wpdb->prepare( 3797 "SELECT m1.post_id 3955 $post_ids = $wpdb->get_col( 3956 $wpdb->prepare( 3957 "SELECT m1.post_id 3798 3958 FROM {$wpdb->postmeta} AS m1 3799 3959 INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id ) … … 3802 3962 AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s ) 3803 3963 AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )", 3804 $taxonomy, 3805 $term_id 3806 ) ); 3964 $taxonomy, 3965 $term_id 3966 ) 3967 ); 3807 3968 3808 3969 if ( $post_ids ) { … … 3918 4079 global $wp_rewrite; 3919 4080 3920 if ( ! is_object($term) ) {4081 if ( ! is_object( $term ) ) { 3921 4082 if ( is_int( $term ) ) { 3922 4083 $term = get_term( $term, $taxonomy ); … … 3926 4087 } 3927 4088 3928 if ( ! is_object($term) )4089 if ( ! is_object( $term ) ) { 3929 4090 $term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); 3930 3931 if ( is_wp_error( $term ) ) 4091 } 4092 4093 if ( is_wp_error( $term ) ) { 3932 4094 return $term; 4095 } 3933 4096 3934 4097 $taxonomy = $term->taxonomy; 3935 4098 3936 $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy);4099 $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy ); 3937 4100 3938 4101 /** … … 3947 4110 3948 4111 $slug = $term->slug; 3949 $t = get_taxonomy($taxonomy);3950 3951 if ( empty( $termlink) ) {3952 if ( 'category' == $taxonomy ) 4112 $t = get_taxonomy( $taxonomy ); 4113 4114 if ( empty( $termlink ) ) { 4115 if ( 'category' == $taxonomy ) { 3953 4116 $termlink = '?cat=' . $term->term_id; 3954 elseif ( $t->query_var )4117 } elseif ( $t->query_var ) { 3955 4118 $termlink = "?$t->query_var=$slug"; 3956 else4119 } else { 3957 4120 $termlink = "?taxonomy=$taxonomy&term=$slug"; 3958 $termlink = home_url($termlink); 4121 } 4122 $termlink = home_url( $termlink ); 3959 4123 } else { 3960 4124 if ( $t->rewrite['hierarchical'] ) { 3961 4125 $hierarchical_slugs = array(); 3962 $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );3963 foreach ( (array) $ancestors as $ancestor ) {3964 $ancestor_term = get_term($ancestor, $taxonomy);4126 $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); 4127 foreach ( (array) $ancestors as $ancestor ) { 4128 $ancestor_term = get_term( $ancestor, $taxonomy ); 3965 4129 $hierarchical_slugs[] = $ancestor_term->slug; 3966 4130 } 3967 $hierarchical_slugs = array_reverse($hierarchical_slugs);4131 $hierarchical_slugs = array_reverse( $hierarchical_slugs ); 3968 4132 $hierarchical_slugs[] = $slug; 3969 $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);4133 $termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink ); 3970 4134 } else { 3971 $termlink = str_replace( "%$taxonomy%", $slug, $termlink);3972 } 3973 $termlink = home_url( user_trailingslashit( $termlink, 'category') );4135 $termlink = str_replace( "%$taxonomy%", $slug, $termlink ); 4136 } 4137 $termlink = home_url( user_trailingslashit( $termlink, 'category' ) ); 3974 4138 } 3975 4139 // Back Compat filters. … … 4033 4197 function the_taxonomies( $args = array() ) { 4034 4198 $defaults = array( 4035 'post' => 0,4199 'post' => 0, 4036 4200 'before' => '', 4037 'sep' => ' ',4038 'after' => '',4201 'sep' => ' ', 4202 'after' => '', 4039 4203 ); 4040 4204 … … 4066 4230 $post = get_post( $post ); 4067 4231 4068 $args = wp_parse_args( $args, array( 4069 /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */ 4070 'template' => __( '%s: %l.' ), 4071 'term_template' => '<a href="%1$s">%2$s</a>', 4072 ) ); 4232 $args = wp_parse_args( 4233 $args, array( 4234 /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */ 4235 'template' => __( '%s: %l.' ), 4236 'term_template' => '<a href="%1$s">%2$s</a>', 4237 ) 4238 ); 4073 4239 4074 4240 $taxonomies = array(); … … 4103 4269 } 4104 4270 if ( $links ) { 4105 $taxonomies[ $taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );4271 $taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); 4106 4272 } 4107 4273 } … … 4120 4286 $post = get_post( $post ); 4121 4287 4122 return get_object_taxonomies( $post);4288 return get_object_taxonomies( $post ); 4123 4289 } 4124 4290 … … 4138 4304 */ 4139 4305 function is_object_in_term( $object_id, $taxonomy, $terms = null ) { 4140 if ( ! $object_id = (int) $object_id )4306 if ( ! $object_id = (int) $object_id ) { 4141 4307 return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) ); 4308 } 4142 4309 4143 4310 $object_terms = get_object_term_cache( $object_id, $taxonomy ); … … 4151 4318 } 4152 4319 4153 if ( is_wp_error( $object_terms ) ) 4320 if ( is_wp_error( $object_terms ) ) { 4154 4321 return $object_terms; 4155 if ( empty( $object_terms ) ) 4322 } 4323 if ( empty( $object_terms ) ) { 4156 4324 return false; 4157 if ( empty( $terms ) ) 4158 return ( !empty( $object_terms ) ); 4325 } 4326 if ( empty( $terms ) ) { 4327 return ( ! empty( $object_terms ) ); 4328 } 4159 4329 4160 4330 $terms = (array) $terms; 4161 4331 4162 if ( $ints = array_filter( $terms, 'is_int' ) ) 4332 if ( $ints = array_filter( $terms, 'is_int' ) ) { 4163 4333 $strs = array_diff( $terms, $ints ); 4164 else4334 } else { 4165 4335 $strs =& $terms; 4336 } 4166 4337 4167 4338 foreach ( $object_terms as $object_term ) { … … 4178 4349 } 4179 4350 4180 if ( in_array( $object_term->name, $strs ) ) return true; 4181 if ( in_array( $object_term->slug, $strs ) ) return true; 4351 if ( in_array( $object_term->name, $strs ) ) { 4352 return true; 4353 } 4354 if ( in_array( $object_term->slug, $strs ) ) { 4355 return true; 4356 } 4182 4357 } 4183 4358 } … … 4236 4411 4237 4412 if ( 'taxonomy' === $resource_type ) { 4238 $term = get_term( $object_id, $object_type);4239 while ( ! is_wp_error( $term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {4413 $term = get_term( $object_id, $object_type ); 4414 while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { 4240 4415 $ancestors[] = (int) $term->parent; 4241 $term = get_term($term->parent, $object_type);4416 $term = get_term( $term->parent, $object_type ); 4242 4417 } 4243 4418 } elseif ( 'post_type' === $resource_type ) { 4244 $ancestors = get_post_ancestors( $object_id);4419 $ancestors = get_post_ancestors( $object_id ); 4245 4420 } 4246 4421 … … 4292 4467 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { 4293 4468 // Nothing fancy here - bail 4294 if ( ! $parent )4469 if ( ! $parent ) { 4295 4470 return 0; 4471 } 4296 4472 4297 4473 // Can't be its own parent. 4298 if ( $parent == $term_id ) 4474 if ( $parent == $term_id ) { 4299 4475 return 0; 4476 } 4300 4477 4301 4478 // Now look for larger loops. 4302 if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )4479 if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) { 4303 4480 return $parent; // No loop 4481 } 4304 4482 4305 4483 // Setting $parent to the given value causes a loop. 4306 if ( isset( $loop[ $term_id] ) )4484 if ( isset( $loop[ $term_id ] ) ) { 4307 4485 return 0; 4486 } 4308 4487 4309 4488 // There's a loop, but it doesn't contain $term_id. Break the loop. 4310 foreach ( array_keys( $loop ) as $loop_member ) 4489 foreach ( array_keys( $loop ) as $loop_member ) { 4311 4490 wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); 4491 } 4312 4492 4313 4493 return $parent;
Note: See TracChangeset
for help on using the changeset viewer.