Ticket #39825: docs.patch
File docs.patch, 60.1 KB (added by , 8 years ago) |
---|
-
class-wp-term-query.php
176 176 * Can be used in conjunction with `$meta_value`. 177 177 * @type string $meta_value Limit terms to those matching a specific metadata value. 178 178 * Usually used in conjunction with `$meta_key`. 179 * @type string $meta_type Type of object metadata is for (e.g., comment, post, or user). 180 * @type string $meta_compare Comparison operator to test the 'meta_value'. 179 181 * } 180 182 */ 181 183 public function __construct( $query = '' ) { … … 429 431 (array) get_terms( $taxonomies[0], array( 430 432 'child_of' => intval( $extrunk ), 431 433 'fields' => 'ids', 432 'hide_empty' => 0 434 'hide_empty' => 0, 433 435 ) ) 434 436 ); 435 437 } … … 496 498 $tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) ); 497 499 $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; 498 500 } else { 499 $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );501 $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( 'tt.term_taxonomy_id = %d', $args['term_taxonomy_id'] ); 500 502 } 501 503 } 502 504 503 505 if ( ! empty( $args['name__like'] ) ) { 504 $this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );506 $this->sql_clauses['where']['name__like'] = $wpdb->prepare( 't.name LIKE %s', '%' . $wpdb->esc_like( $args['name__like'] ) . '%' ); 505 507 } 506 508 507 509 if ( ! empty( $args['description__like'] ) ) { 508 $this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );510 $this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' ); 509 511 } 510 512 511 513 if ( ! empty( $args['object_ids'] ) ) { … … 535 537 if ( 'count' == $args['fields'] ) { 536 538 $hierarchical = false; 537 539 } 538 if ( $args['hide_empty'] && ! $hierarchical ) {540 if ( $args['hide_empty'] && ! $hierarchical ) { 539 541 $this->sql_clauses['where']['count'] = 'tt.count > 0'; 540 542 } 541 543 … … 553 555 $limits = ''; 554 556 } 555 557 556 557 558 if ( ! empty( $args['search'] ) ) { 558 559 $this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] ); 559 560 } … … 570 571 if ( ! empty( $meta_clauses ) ) { 571 572 $join .= $mq_sql['join']; 572 573 $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] ); 573 $distinct .= "DISTINCT";574 $distinct .= 'DISTINCT'; 574 575 575 576 } 576 577 … … 644 645 */ 645 646 $clauses = apply_filters( 'terms_clauses', compact( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ), $taxonomies, $args ); 646 647 647 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields'] : '';648 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join'] : '';649 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where'] : '';650 $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct'] : '';651 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby'] : '';652 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order'] : '';653 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits'] : '';648 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; 649 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; 650 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; 651 $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; 652 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; 653 $order = isset( $clauses['order'] ) ? $clauses['order'] : ''; 654 $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; 654 655 655 656 if ( $where ) { 656 657 $where = "WHERE $where"; … … 900 901 return $orderby; 901 902 } 902 903 903 switch ( $orderby_raw ) {904 switch ( $orderby_raw ) { 904 905 case $primary_meta_key: 905 906 case 'meta_value': 906 907 if ( ! empty( $primary_meta_query['type'] ) ) { -
taxonomy.php
39 39 $rewrite = array( 40 40 'category' => array( 41 41 '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(),42 'slug' => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category', 43 'with_front' => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(), 44 44 'ep_mask' => EP_CATEGORIES, 45 45 ), 46 46 'post_tag' => array( 47 47 '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(),48 'slug' => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag', 49 'with_front' => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(), 50 50 'ep_mask' => EP_TAGS, 51 51 ), 52 52 'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false, … … 170 170 171 171 $field = ('names' == $output) ? 'name' : false; 172 172 173 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field);173 return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field ); 174 174 } 175 175 176 176 /** … … 197 197 function get_object_taxonomies( $object, $output = 'names' ) { 198 198 global $wp_taxonomies; 199 199 200 if ( is_object( $object) ) {201 if ( $object->post_type == 'attachment' ) 200 if ( is_object( $object ) ) { 201 if ( $object->post_type == 'attachment' ) { 202 202 return get_attachment_taxonomies( $object, $output ); 203 } 203 204 $object = $object->post_type; 204 205 } 205 206 … … 207 208 208 209 $taxonomies = array(); 209 210 foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { 210 if ( array_intersect( $object, (array) $tax_obj->object_type) ) {211 if ( 'names' == $output ) 211 if ( array_intersect( $object, (array) $tax_obj->object_type ) ) { 212 if ( 'names' == $output ) { 212 213 $taxonomies[] = $tax_name; 213 else214 $taxonomies[ $tax_name ] = $tax_obj;214 } else { $taxonomies[ $tax_name ] = $tax_obj; 215 } 215 216 } 216 217 } 217 218 … … 234 235 function get_taxonomy( $taxonomy ) { 235 236 global $wp_taxonomies; 236 237 237 if ( ! taxonomy_exists( $taxonomy ) ) 238 if ( ! taxonomy_exists( $taxonomy ) ) { 238 239 return false; 240 } 239 241 240 return $wp_taxonomies[ $taxonomy];242 return $wp_taxonomies[ $taxonomy ]; 241 243 } 242 244 243 245 /** … … 255 257 function taxonomy_exists( $taxonomy ) { 256 258 global $wp_taxonomies; 257 259 258 return isset( $wp_taxonomies[ $taxonomy] );260 return isset( $wp_taxonomies[ $taxonomy ] ); 259 261 } 260 262 261 263 /** … … 271 273 * @param string $taxonomy Name of taxonomy object. 272 274 * @return bool Whether the taxonomy is hierarchical. 273 275 */ 274 function is_taxonomy_hierarchical( $taxonomy) {275 if ( ! taxonomy_exists( $taxonomy) )276 function is_taxonomy_hierarchical( $taxonomy ) { 277 if ( ! taxonomy_exists( $taxonomy ) ) { 276 278 return false; 279 } 277 280 278 $taxonomy = get_taxonomy( $taxonomy);281 $taxonomy = get_taxonomy( $taxonomy ); 279 282 return $taxonomy->hierarchical; 280 283 } 281 284 … … 371 374 function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 372 375 global $wp_taxonomies; 373 376 374 if ( ! is_array( $wp_taxonomies ) ) 377 if ( ! is_array( $wp_taxonomies ) ) { 375 378 $wp_taxonomies = array(); 379 } 376 380 377 381 $args = wp_parse_args( $args ); 378 382 … … 388 392 389 393 $taxonomy_object->add_hooks(); 390 394 391 392 395 /** 393 396 * Fires after a taxonomy is registered. 394 397 * … … 485 488 function get_taxonomy_labels( $tax ) { 486 489 $tax->labels = (array) $tax->labels; 487 490 488 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) 491 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) { 489 492 $tax->labels['separate_items_with_commas'] = $tax->helps; 493 } 490 494 491 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) 495 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) { 492 496 $tax->labels['not_found'] = $tax->no_tagcloud; 497 } 493 498 494 499 $nohier_vs_hier_defaults = array( 495 500 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ), … … 550 555 * @param string $object_type Name of the object type. 551 556 * @return bool True if successful, false if not. 552 557 */ 553 function register_taxonomy_for_object_type( $taxonomy, $object_type ) {558 function register_taxonomy_for_object_type( $taxonomy, $object_type ) { 554 559 global $wp_taxonomies; 555 560 556 if ( ! isset($wp_taxonomies[$taxonomy]) )561 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { 557 562 return false; 563 } 558 564 559 if ( ! get_post_type_object( $object_type) )565 if ( ! get_post_type_object( $object_type ) ) { 560 566 return false; 567 } 561 568 562 if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) ) 563 $wp_taxonomies[$taxonomy]->object_type[] = $object_type; 569 if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type ) ) { 570 $wp_taxonomies[ $taxonomy ]->object_type[] = $object_type; 571 } 564 572 565 573 // Filter out empties. 566 574 $wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type ); … … 582 590 function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) { 583 591 global $wp_taxonomies; 584 592 585 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) 593 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { 586 594 return false; 595 } 587 596 588 if ( ! get_post_type_object( $object_type ) ) 597 if ( ! get_post_type_object( $object_type ) ) { 589 598 return false; 599 } 590 600 591 601 $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); 592 if ( false === $key ) 602 if ( false === $key ) { 593 603 return false; 604 } 594 605 595 606 unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] ); 596 607 return true; … … 645 656 646 657 $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; 647 658 648 $term_ids = array_map( 'intval', $term_ids );659 $term_ids = array_map( 'intval', $term_ids ); 649 660 650 661 $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 651 662 $term_ids = "'" . implode( "', '", $term_ids ) . "'"; 652 663 653 $object_ids = $wpdb->get_col( "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");664 $object_ids = $wpdb->get_col( "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order" ); 654 665 655 if ( ! $object_ids ) {666 if ( ! $object_ids ) { 656 667 return array(); 657 668 } 658 669 return $object_ids; … … 860 871 break; 861 872 case 'term_taxonomy_id' : 862 873 $args['term_taxonomy_id'] = $value; 863 unset( $args[ 'taxonomy'] );874 unset( $args['taxonomy'] ); 864 875 break; 865 876 default : 866 877 return false; … … 902 913 903 914 $term_id = intval( $term_id ); 904 915 905 $terms = _get_term_hierarchy( $taxonomy);916 $terms = _get_term_hierarchy( $taxonomy ); 906 917 907 if ( ! isset( $terms[$term_id]) )918 if ( ! isset( $terms[ $term_id ] ) ) { 908 919 return array(); 920 } 909 921 910 $children = $terms[ $term_id];922 $children = $terms[ $term_id ]; 911 923 912 foreach ( (array) $terms[ $term_id] as $child ) {924 foreach ( (array) $terms[ $term_id ] as $child ) { 913 925 if ( $term_id == $child ) { 914 926 continue; 915 927 } 916 928 917 if ( isset($terms[$child]) ) 918 $children = array_merge($children, get_term_children($child, $taxonomy)); 929 if ( isset( $terms[ $child ] ) ) { 930 $children = array_merge( $children, get_term_children( $child, $taxonomy ) ); 931 } 919 932 } 920 933 921 934 return $children; … … 939 952 */ 940 953 function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { 941 954 $term = get_term( $term, $taxonomy ); 942 if ( is_wp_error( $term) )955 if ( is_wp_error( $term ) ) { 943 956 return $term; 957 } 944 958 945 if ( ! is_object($term) )959 if ( ! is_object( $term ) ) { 946 960 return ''; 961 } 947 962 948 if ( ! isset($term->$field) )963 if ( ! isset( $term->$field ) ) { 949 964 return ''; 965 } 950 966 951 967 return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context ); 952 968 } … … 966 982 function get_term_to_edit( $id, $taxonomy ) { 967 983 $term = get_term( $id, $taxonomy ); 968 984 969 if ( is_wp_error( $term) )985 if ( is_wp_error( $term ) ) { 970 986 return $term; 987 } 971 988 972 if ( ! is_object($term) )989 if ( ! is_object( $term ) ) { 973 990 return ''; 991 } 974 992 975 return sanitize_term( $term, $taxonomy, 'edit');993 return sanitize_term( $term, $taxonomy, 'edit' ); 976 994 } 977 995 978 996 /** … … 1080 1098 * conjunction with `$meta_value`. 1081 1099 * @type string $meta_value Limit terms to those matching a specific metadata value. Usually used 1082 1100 * in conjunction with `$meta_key`. 1101 * @type string $meta_type Type of object metadata is for (e.g., comment, post, or user). 1102 * @type string $meta_compare Comparison operator to test the 'meta_value'. 1083 1103 * } 1084 1104 * @param array $deprecated Argument array, when using the legacy function parameter format. If present, this 1085 1105 * parameter will be interpreted as `$args`, and the first function parameter will … … 1163 1183 } 1164 1184 1165 1185 if ( wp_term_is_shared( $term_id ) ) { 1166 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );1186 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); 1167 1187 } 1168 1188 1169 1189 $added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique ); … … 1245 1265 } 1246 1266 1247 1267 if ( wp_term_is_shared( $term_id ) ) { 1248 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );1268 return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); 1249 1269 } 1250 1270 1251 1271 $updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value ); … … 1301 1321 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1302 1322 $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 "; 1303 1323 1304 if ( is_int( $term) ) {1305 if ( 0 == $term ) 1324 if ( is_int( $term ) ) { 1325 if ( 0 == $term ) { 1306 1326 return 0; 1327 } 1307 1328 $where = 't.term_id = %d'; 1308 if ( ! empty($taxonomy) )1309 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );1310 else1311 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );1329 if ( ! empty( $taxonomy ) ) { 1330 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A ); 1331 } else { return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); 1332 } 1312 1333 } 1313 1334 1314 1335 $term = trim( wp_unslash( $term ) ); … … 1316 1337 1317 1338 $where = 't.slug = %s'; 1318 1339 $else_where = 't.name = %s'; 1319 $where_fields = array( $slug);1320 $else_where_fields = array( $term);1340 $where_fields = array( $slug ); 1341 $else_where_fields = array( $term ); 1321 1342 $orderby = 'ORDER BY t.term_id ASC'; 1322 1343 $limit = 'LIMIT 1'; 1323 if ( ! empty($taxonomy) ) {1344 if ( ! empty( $taxonomy ) ) { 1324 1345 if ( is_numeric( $parent ) ) { 1325 1346 $parent = (int) $parent; 1326 1347 $where_fields[] = $parent; … … 1332 1353 $where_fields[] = $taxonomy; 1333 1354 $else_where_fields[] = $taxonomy; 1334 1355 1335 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) )1356 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 ) ) { 1336 1357 return $result; 1358 } 1337 1359 1338 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);1360 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 ); 1339 1361 } 1340 1362 1341 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )1363 if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) { 1342 1364 return $result; 1365 } 1343 1366 1344 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );1367 return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) ); 1345 1368 } 1346 1369 1347 1370 /** … … 1357 1380 * @return bool Whether `$term2` is a child of `$term1`. 1358 1381 */ 1359 1382 function term_is_ancestor_of( $term1, $term2, $taxonomy ) { 1360 if ( ! isset( $term1->term_id ) ) 1383 if ( ! isset( $term1->term_id ) ) { 1361 1384 $term1 = get_term( $term1, $taxonomy ); 1362 if ( ! isset( $term2->parent ) ) 1385 } 1386 if ( ! isset( $term2->parent ) ) { 1363 1387 $term2 = get_term( $term2, $taxonomy ); 1388 } 1364 1389 1365 if ( empty( $term1->term_id ) || empty( $term2->parent ) ) 1390 if ( empty( $term1->term_id ) || empty( $term2->parent ) ) { 1366 1391 return false; 1367 if ( $term2->parent == $term1->term_id ) 1392 } 1393 if ( $term2->parent == $term1->term_id ) { 1368 1394 return true; 1395 } 1369 1396 1370 1397 return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); 1371 1398 } … … 1387 1414 * 'display', 'attribute', or 'js'. Default 'display'. 1388 1415 * @return array|object Term with all fields sanitized. 1389 1416 */ 1390 function sanitize_term( $term, $taxonomy, $context = 'display') {1417 function sanitize_term( $term, $taxonomy, $context = 'display' ) { 1391 1418 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); 1392 1419 1393 1420 $do_object = is_object( $term ); 1394 1421 1395 $term_id = $do_object ? $term->term_id : (isset( $term['term_id']) ? $term['term_id'] : 0);1422 $term_id = $do_object ? $term->term_id : (isset( $term['term_id'] ) ? $term['term_id'] : 0); 1396 1423 1397 1424 foreach ( (array) $fields as $field ) { 1398 1425 if ( $do_object ) { 1399 if ( isset($term->$field) ) 1400 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); 1426 if ( isset( $term->$field ) ) { 1427 $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); 1428 } 1401 1429 } else { 1402 if ( isset($term[$field]) ) 1403 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); 1430 if ( isset( $term[ $field ] ) ) { 1431 $term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context ); 1432 } 1404 1433 } 1405 1434 } 1406 1435 1407 if ( $do_object ) 1436 if ( $do_object ) { 1408 1437 $term->filter = $context; 1409 else1410 $term['filter'] = $context;1438 } else { $term['filter'] = $context; 1439 } 1411 1440 1412 1441 return $term; 1413 1442 } … … 1435 1464 * 'attribute', or 'js'. 1436 1465 * @return mixed Sanitized field. 1437 1466 */ 1438 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context) {1467 function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { 1439 1468 $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); 1440 1469 if ( in_array( $field, $int_fields ) ) { 1441 1470 $value = (int) $value; 1442 if ( $value < 0 ) 1471 if ( $value < 0 ) { 1443 1472 $value = 0; 1473 } 1444 1474 } 1445 1475 1446 if ( 'raw' == $context ) 1476 if ( 'raw' == $context ) { 1447 1477 return $value; 1478 } 1448 1479 1449 1480 if ( 'edit' == $context ) { 1450 1481 … … 1474 1505 */ 1475 1506 $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id ); 1476 1507 1477 if ( 'description' == $field ) 1478 $value = esc_html( $value); // textarea_escaped1479 else1480 $value = esc_attr($value);1508 if ( 'description' == $field ) { 1509 $value = esc_html( $value ); // textarea_escaped 1510 } else { $value = esc_attr( $value ); 1511 } 1481 1512 } elseif ( 'db' == $context ) { 1482 1513 1483 1514 /** … … 1517 1548 */ 1518 1549 $value = apply_filters( 'pre_category_nicename', $value ); 1519 1550 } 1520 1521 1551 } elseif ( 'rss' == $context ) { 1522 1552 1523 1553 /** … … 1576 1606 } 1577 1607 1578 1608 if ( 'attribute' == $context ) { 1579 $value = esc_attr( $value);1609 $value = esc_attr( $value ); 1580 1610 } elseif ( 'js' == $context ) { 1581 $value = esc_js( $value);1611 $value = esc_js( $value ); 1582 1612 } 1583 1613 return $value; 1584 1614 } … … 1596 1626 * @return array|int|WP_Error Number of terms in that taxonomy or WP_Error if the taxonomy does not exist. 1597 1627 */ 1598 1628 function wp_count_terms( $taxonomy, $args = array() ) { 1599 $defaults = array( 'hide_empty' => false);1600 $args = wp_parse_args( $args, $defaults);1629 $defaults = array( 'hide_empty' => false ); 1630 $args = wp_parse_args( $args, $defaults ); 1601 1631 1602 1632 // backward compatibility 1603 if ( isset( $args['ignore_empty']) ) {1633 if ( isset( $args['ignore_empty'] ) ) { 1604 1634 $args['hide_empty'] = $args['ignore_empty']; 1605 unset( $args['ignore_empty']);1635 unset( $args['ignore_empty'] ); 1606 1636 } 1607 1637 1608 1638 $args['fields'] = 'count'; 1609 1639 1610 return get_terms( $taxonomy, $args);1640 return get_terms( $taxonomy, $args ); 1611 1641 } 1612 1642 1613 1643 /** … … 1625 1655 function wp_delete_object_term_relationships( $object_id, $taxonomies ) { 1626 1656 $object_id = (int) $object_id; 1627 1657 1628 if ( !is_array($taxonomies) ) 1629 $taxonomies = array($taxonomies); 1658 if ( ! is_array( $taxonomies ) ) { 1659 $taxonomies = array( $taxonomies ); 1660 } 1630 1661 1631 1662 foreach ( (array) $taxonomies as $taxonomy ) { 1632 1663 $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); … … 1667 1698 1668 1699 $term = (int) $term; 1669 1700 1670 if ( ! $ids = term_exists( $term, $taxonomy) )1701 if ( ! $ids = term_exists( $term, $taxonomy ) ) { 1671 1702 return false; 1672 if ( is_wp_error( $ids ) ) 1703 } 1704 if ( is_wp_error( $ids ) ) { 1673 1705 return $ids; 1706 } 1674 1707 1675 1708 $tt_id = $ids['term_taxonomy_id']; 1676 1709 … … 1678 1711 1679 1712 if ( 'category' == $taxonomy ) { 1680 1713 $defaults['default'] = get_option( 'default_category' ); 1681 if ( $defaults['default'] == $term ) 1714 if ( $defaults['default'] == $term ) { 1682 1715 return 0; // Don't delete the default category 1716 } 1683 1717 } 1684 1718 1685 $args = wp_parse_args( $args, $defaults);1719 $args = wp_parse_args( $args, $defaults ); 1686 1720 1687 1721 if ( isset( $args['default'] ) ) { 1688 1722 $default = (int) $args['default']; … … 1706 1740 do_action( 'pre_delete_term', $term, $taxonomy ); 1707 1741 1708 1742 // Update children to point to new parent 1709 if ( is_taxonomy_hierarchical( $taxonomy) ) {1710 $term_obj = get_term( $term, $taxonomy);1711 if ( is_wp_error( $term_obj ) ) 1743 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 1744 $term_obj = get_term( $term, $taxonomy ); 1745 if ( is_wp_error( $term_obj ) ) { 1712 1746 return $term_obj; 1747 } 1713 1748 $parent = $term_obj->parent; 1714 1749 1715 $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id );1750 $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id ); 1716 1751 $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); 1717 1752 1718 1753 /** … … 1724 1759 */ 1725 1760 do_action( 'edit_term_taxonomies', $edit_tt_ids ); 1726 1761 1727 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );1762 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); 1728 1763 1729 1764 // Clean the cache for all child terms. 1730 1765 $edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' ); … … 1747 1782 1748 1783 foreach ( $object_ids as $object_id ) { 1749 1784 $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) ); 1750 if ( 1 == count( $terms) && isset($default) ) {1751 $terms = array( $default);1785 if ( 1 == count( $terms ) && isset( $default ) ) { 1786 $terms = array( $default ); 1752 1787 } else { 1753 $terms = array_diff($terms, array($term)); 1754 if (isset($default) && isset($force_default) && $force_default) 1755 $terms = array_merge($terms, array($default)); 1788 $terms = array_diff( $terms, array( $term ) ); 1789 if ( isset( $default ) && isset( $force_default ) && $force_default ) { 1790 $terms = array_merge( $terms, array( $default ) ); 1791 } 1756 1792 } 1757 $terms = array_map( 'intval', $terms);1793 $terms = array_map( 'intval', $terms ); 1758 1794 wp_set_object_terms( $object_id, $terms, $taxonomy ); 1759 1795 } 1760 1796 1761 1797 // Clean the relationship caches for all object types using this term. 1762 1798 $tax_object = get_taxonomy( $taxonomy ); 1763 foreach ( $tax_object->object_type as $object_type ) 1799 foreach ( $tax_object->object_type as $object_type ) { 1764 1800 clean_object_term_cache( $object_ids, $object_type ); 1801 } 1765 1802 1766 1803 $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); 1767 1804 foreach ( $term_meta_ids as $mid ) { … … 1788 1825 do_action( 'deleted_term_taxonomy', $tt_id ); 1789 1826 1790 1827 // Delete the term if no taxonomies use it. 1791 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )1828 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) { 1792 1829 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); 1830 } 1793 1831 1794 clean_term_cache( $term, $taxonomy);1832 clean_term_cache( $term, $taxonomy ); 1795 1833 1796 1834 /** 1797 1835 * Fires after a term is deleted from the database and the cache is cleaned. … … 1859 1897 * @return array|WP_Error The requested term data or empty array if no terms found. 1860 1898 * WP_Error if any of the $taxonomies don't exist. 1861 1899 */ 1862 function wp_get_object_terms( $object_ids, $taxonomies, $args = array()) {1900 function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 1863 1901 global $wpdb; 1864 1902 1865 if ( empty( $object_ids ) || empty( $taxonomies ) ) 1903 if ( empty( $object_ids ) || empty( $taxonomies ) ) { 1866 1904 return array(); 1905 } 1867 1906 1868 if ( !is_array($taxonomies) ) 1869 $taxonomies = array($taxonomies); 1907 if ( ! is_array( $taxonomies ) ) { 1908 $taxonomies = array( $taxonomies ); 1909 } 1870 1910 1871 1911 foreach ( $taxonomies as $taxonomy ) { 1872 if ( ! taxonomy_exists( $taxonomy) )1912 if ( ! taxonomy_exists( $taxonomy ) ) { 1873 1913 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 1914 } 1874 1915 } 1875 1916 1876 if ( !is_array($object_ids) ) 1877 $object_ids = array($object_ids); 1878 $object_ids = array_map('intval', $object_ids); 1917 if ( ! is_array( $object_ids ) ) { 1918 $object_ids = array( $object_ids ); 1919 } 1920 $object_ids = array_map( 'intval', $object_ids ); 1879 1921 1880 1922 $args = wp_parse_args( $args ); 1881 1923 … … 1963 2005 function wp_insert_term( $term, $taxonomy, $args = array() ) { 1964 2006 global $wpdb; 1965 2007 1966 if ( ! taxonomy_exists( $taxonomy) ) {2008 if ( ! taxonomy_exists( $taxonomy ) ) { 1967 2009 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 1968 2010 } 1969 2011 /** … … 1984 2026 if ( '' == trim( $term ) ) { 1985 2027 return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); 1986 2028 } 1987 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' );2029 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' ); 1988 2030 $args = wp_parse_args( $args, $defaults ); 1989 2031 1990 2032 if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { … … 1997 2039 // Coerce null description to strings, to avoid database errors. 1998 2040 $args['description'] = (string) $args['description']; 1999 2041 2000 $args = sanitize_term( $args, $taxonomy, 'db');2042 $args = sanitize_term( $args, $taxonomy, 'db' ); 2001 2043 2002 2044 // expected_slashed ($name) 2003 2045 $name = wp_unslash( $args['name'] ); … … 2022 2064 * The alias is not in a group, so we create a new one 2023 2065 * and add the alias to it. 2024 2066 */ 2025 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms") + 1;2067 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 2026 2068 2027 2069 wp_update_term( $alias->term_id, $taxonomy, array( 2028 2070 'term_group' => $term_group, … … 2097 2139 $term_id = (int) $wpdb->insert_id; 2098 2140 2099 2141 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. 2100 if ( empty( $slug) ) {2101 $slug = sanitize_title( $slug, $term_id);2142 if ( empty( $slug ) ) { 2143 $slug = sanitize_title( $slug, $term_id ); 2102 2144 2103 2145 /** This action is documented in wp-includes/taxonomy.php */ 2104 2146 do_action( 'edit_terms', $term_id, $taxonomy ); … … 2110 2152 2111 2153 $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 ) ); 2112 2154 2113 if ( ! empty($tt_id) ) {2114 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);2155 if ( ! empty( $tt_id ) ) { 2156 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 2115 2157 } 2116 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) );2158 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ); 2117 2159 $tt_id = (int) $wpdb->insert_id; 2118 2160 2119 2161 /* … … 2143 2185 * @param int $tt_id Term taxonomy ID. 2144 2186 * @param string $taxonomy Taxonomy slug. 2145 2187 */ 2146 do_action( "create_term", $term_id, $tt_id, $taxonomy );2188 do_action( 'create_term', $term_id, $tt_id, $taxonomy ); 2147 2189 2148 2190 /** 2149 2191 * Fires after a new term is created for a specific taxonomy. … … 2168 2210 */ 2169 2211 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 2170 2212 2171 clean_term_cache( $term_id, $taxonomy);2213 clean_term_cache( $term_id, $taxonomy ); 2172 2214 2173 2215 /** 2174 2216 * Fires after a new term is created, and after the term cache has been cleaned. … … 2194 2236 */ 2195 2237 do_action( "created_{$taxonomy}", $term_id, $tt_id ); 2196 2238 2197 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);2239 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 2198 2240 } 2199 2241 2200 2242 /** … … 2229 2271 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 2230 2272 } 2231 2273 2232 if ( !is_array($terms) ) 2233 $terms = array($terms); 2274 if ( ! is_array( $terms ) ) { 2275 $terms = array( $terms ); 2276 } 2234 2277 2235 if ( ! $append ) 2236 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));2237 else2238 $old_tt_ids = array();2278 if ( ! $append ) { 2279 $old_tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids', 'orderby' => 'none' ) ); 2280 } else { $old_tt_ids = array(); 2281 } 2239 2282 2240 2283 $tt_ids = array(); 2241 2284 $term_ids = array(); 2242 2285 $new_tt_ids = array(); 2243 2286 2244 foreach ( (array) $terms as $term ) {2245 if ( ! strlen(trim($term)) )2287 foreach ( (array) $terms as $term ) { 2288 if ( ! strlen( trim( $term ) ) ) { 2246 2289 continue; 2290 } 2247 2291 2248 if ( ! $term_info = term_exists($term, $taxonomy) ) {2292 if ( ! $term_info = term_exists( $term, $taxonomy ) ) { 2249 2293 // Skip if a non-existent term ID is passed. 2250 if ( is_int( $term) )2294 if ( is_int( $term ) ) { 2251 2295 continue; 2252 $term_info = wp_insert_term($term, $taxonomy); 2296 } 2297 $term_info = wp_insert_term( $term, $taxonomy ); 2253 2298 } 2254 if ( is_wp_error( $term_info) )2299 if ( is_wp_error( $term_info ) ) { 2255 2300 return $term_info; 2301 } 2256 2302 $term_ids[] = $term_info['term_id']; 2257 2303 $tt_id = $term_info['term_taxonomy_id']; 2258 2304 $tt_ids[] = $tt_id; 2259 2305 2260 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 ) ) ) 2306 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 ) ) ) { 2261 2307 continue; 2308 } 2262 2309 2263 2310 /** 2264 2311 * Fires immediately before an object-term relationship is added. … … 2287 2334 $new_tt_ids[] = $tt_id; 2288 2335 } 2289 2336 2290 if ( $new_tt_ids ) 2337 if ( $new_tt_ids ) { 2291 2338 wp_update_term_count( $new_tt_ids, $taxonomy ); 2339 } 2292 2340 2293 2341 if ( ! $append ) { 2294 2342 $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids ); … … 2305 2353 } 2306 2354 } 2307 2355 2308 $t = get_taxonomy( $taxonomy);2309 if ( ! $append && isset( $t->sort) && $t->sort ) {2356 $t = get_taxonomy( $taxonomy ); 2357 if ( ! $append && isset( $t->sort ) && $t->sort ) { 2310 2358 $values = array(); 2311 2359 $term_order = 0; 2312 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids')); 2313 foreach ( $tt_ids as $tt_id ) 2314 if ( in_array($tt_id, $final_tt_ids) ) 2315 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order); 2316 if ( $values ) 2317 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 $final_tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) ); 2361 foreach ( $tt_ids as $tt_id ) { 2362 if ( in_array( $tt_id, $final_tt_ids ) ) { 2363 $values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order ); 2364 } 2365 } 2366 if ( $values ) { 2367 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)' ) ) { 2318 2368 return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database' ), $wpdb->last_error ); 2369 } 2370 } 2319 2371 } 2320 2372 2321 2373 wp_cache_delete( $object_id, $taxonomy . '_relationships' ); … … 2475 2527 $parent_suffix = ''; 2476 2528 if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) { 2477 2529 $the_parent = $term->parent; 2478 while ( ! empty( $the_parent) ) {2479 $parent_term = get_term( $the_parent, $term->taxonomy);2480 if ( is_wp_error( $parent_term) || empty($parent_term) )2530 while ( ! empty( $the_parent ) ) { 2531 $parent_term = get_term( $the_parent, $term->taxonomy ); 2532 if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) { 2481 2533 break; 2534 } 2482 2535 $parent_suffix .= '-' . $parent_term->slug; 2483 2536 if ( ! term_exists( $slug . $parent_suffix ) ) { 2484 2537 break; 2485 2538 } 2486 2539 2487 if ( empty( $parent_term->parent) )2540 if ( empty( $parent_term->parent ) ) { 2488 2541 break; 2542 } 2489 2543 $the_parent = $parent_term->parent; 2490 2544 } 2491 2545 } … … 2505 2559 if ( $parent_suffix ) { 2506 2560 $slug .= $parent_suffix; 2507 2561 } else { 2508 if ( ! empty( $term->term_id ) ) 2562 if ( ! empty( $term->term_id ) ) { 2509 2563 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); 2510 else2511 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );2564 } else { $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); 2565 } 2512 2566 2513 2567 if ( $wpdb->get_var( $query ) ) { 2514 2568 $num = 2; … … 2590 2644 $term = wp_slash( $term ); 2591 2645 2592 2646 // Merge old and new args with new args overwriting old ones. 2593 $args = array_merge( $term, $args);2647 $args = array_merge( $term, $args ); 2594 2648 2595 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' );2596 $args = wp_parse_args( $args, $defaults);2597 $args = sanitize_term( $args, $taxonomy, 'db');2649 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' ); 2650 $args = wp_parse_args( $args, $defaults ); 2651 $args = sanitize_term( $args, $taxonomy, 'db' ); 2598 2652 $parsed_args = $args; 2599 2653 2600 2654 // expected_slashed ($name) … … 2615 2669 $empty_slug = false; 2616 2670 if ( empty( $args['slug'] ) ) { 2617 2671 $empty_slug = true; 2618 $slug = sanitize_title( $name);2672 $slug = sanitize_title( $name ); 2619 2673 } else { 2620 2674 $slug = $args['slug']; 2621 2675 } … … 2633 2687 * The alias is not in a group, so we create a new one 2634 2688 * and add the alias to it. 2635 2689 */ 2636 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms") + 1;2690 $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; 2637 2691 2638 2692 wp_update_term( $alias->term_id, $taxonomy, array( 2639 2693 'term_group' => $term_group, … … 2664 2718 // If an empty slug was passed or the parent changed, reset the slug to something unique. 2665 2719 // Otherwise, bail. 2666 2720 if ( $empty_slug || ( $parent != $term['parent']) ) { 2667 $slug = wp_unique_term_slug( $slug, (object) $args);2721 $slug = wp_unique_term_slug( $slug, (object) $args ); 2668 2722 } else { 2669 2723 /* translators: 1: Taxonomy term slug */ 2670 return new WP_Error( 'duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));2724 return new WP_Error( 'duplicate_term_slug', sprintf( __( 'The slug “%s” is already in use by another term' ), $slug ) ); 2671 2725 } 2672 2726 } 2673 2727 2674 $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 ) );2728 $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 ) ); 2675 2729 2676 2730 // Check whether this is a shared term that needs splitting. 2677 2731 $_term_id = _split_shared_term( $term_id, $tt_id ); … … 2704 2758 $data = apply_filters( 'wp_update_term_data', $data, $term_id, $taxonomy, $args ); 2705 2759 2706 2760 $wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) ); 2707 if ( empty( $slug) ) {2708 $slug = sanitize_title( $name, $term_id);2761 if ( empty( $slug ) ) { 2762 $slug = sanitize_title( $name, $term_id ); 2709 2763 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 2710 2764 } 2711 2765 … … 2750 2804 * @param int $tt_id Term taxonomy ID. 2751 2805 * @param string $taxonomy Taxonomy slug. 2752 2806 */ 2753 do_action( "edit_term", $term_id, $tt_id, $taxonomy );2807 do_action( 'edit_term', $term_id, $tt_id, $taxonomy ); 2754 2808 2755 2809 /** 2756 2810 * Fires after a term in a specific taxonomy has been updated, but before the term … … 2768 2822 /** This filter is documented in wp-includes/taxonomy.php */ 2769 2823 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); 2770 2824 2771 clean_term_cache( $term_id, $taxonomy);2825 clean_term_cache( $term_id, $taxonomy ); 2772 2826 2773 2827 /** 2774 2828 * Fires after a term has been updated, and the term cache has been cleaned. … … 2779 2833 * @param int $tt_id Term taxonomy ID. 2780 2834 * @param string $taxonomy Taxonomy slug. 2781 2835 */ 2782 do_action( "edited_term", $term_id, $tt_id, $taxonomy );2836 do_action( 'edited_term', $term_id, $tt_id, $taxonomy ); 2783 2837 2784 2838 /** 2785 2839 * Fires after a term for a specific taxonomy has been updated, and the term … … 2794 2848 */ 2795 2849 do_action( "edited_{$taxonomy}", $term_id, $tt_id ); 2796 2850 2797 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id);2851 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id ); 2798 2852 } 2799 2853 2800 2854 /** … … 2807 2861 * @param bool $defer Optional. Enable if true, disable if false. 2808 2862 * @return bool Whether term counting is enabled or disabled. 2809 2863 */ 2810 function wp_defer_term_counting( $defer=null) {2864 function wp_defer_term_counting( $defer = null ) { 2811 2865 static $_defer = false; 2812 2866 2813 if ( is_bool( $defer) ) {2867 if ( is_bool( $defer ) ) { 2814 2868 $_defer = $defer; 2815 2869 // flush any deferred counts 2816 if ( ! $defer )2870 if ( ! $defer ) { 2817 2871 wp_update_term_count( null, null, true ); 2872 } 2818 2873 } 2819 2874 2820 2875 return $_defer; … … 2842 2897 static $_deferred = array(); 2843 2898 2844 2899 if ( $do_deferred ) { 2845 foreach ( (array) array_keys( $_deferred) as $tax ) {2846 wp_update_term_count_now( $_deferred[ $tax], $tax );2847 unset( $_deferred[ $tax] );2900 foreach ( (array) array_keys( $_deferred ) as $tax ) { 2901 wp_update_term_count_now( $_deferred[ $tax ], $tax ); 2902 unset( $_deferred[ $tax ] ); 2848 2903 } 2849 2904 } 2850 2905 2851 if ( empty( $terms) )2906 if ( empty( $terms ) ) { 2852 2907 return false; 2908 } 2853 2909 2854 if ( !is_array($terms) ) 2855 $terms = array($terms); 2910 if ( ! is_array( $terms ) ) { 2911 $terms = array( $terms ); 2912 } 2856 2913 2857 2914 if ( wp_defer_term_counting() ) { 2858 if ( !isset($_deferred[$taxonomy]) ) 2859 $_deferred[$taxonomy] = array(); 2860 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) ); 2915 if ( ! isset( $_deferred[ $taxonomy ] ) ) { 2916 $_deferred[ $taxonomy ] = array(); 2917 } 2918 $_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) ); 2861 2919 return true; 2862 2920 } 2863 2921 … … 2874 2932 * @return true Always true when complete. 2875 2933 */ 2876 2934 function wp_update_term_count_now( $terms, $taxonomy ) { 2877 $terms = array_map( 'intval', $terms);2935 $terms = array_map( 'intval', $terms ); 2878 2936 2879 $taxonomy = get_taxonomy( $taxonomy);2880 if ( ! empty($taxonomy->update_count_callback) ) {2881 call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy);2937 $taxonomy = get_taxonomy( $taxonomy ); 2938 if ( ! empty( $taxonomy->update_count_callback ) ) { 2939 call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy ); 2882 2940 } else { 2883 2941 $object_types = (array) $taxonomy->object_type; 2884 2942 foreach ( $object_types as &$object_type ) { 2885 if ( 0 === strpos( $object_type, 'attachment:' ) ) 2943 if ( 0 === strpos( $object_type, 'attachment:' ) ) { 2886 2944 list( $object_type ) = explode( ':', $object_type ); 2945 } 2887 2946 } 2888 2947 2889 2948 if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) { … … 2895 2954 } 2896 2955 } 2897 2956 2898 clean_term_cache( $terms, '', false);2957 clean_term_cache( $terms, '', false ); 2899 2958 2900 2959 return true; 2901 2960 } … … 2920 2979 * @param int|array $object_ids Single or list of term object ID(s). 2921 2980 * @param array|string $object_type The taxonomy object type. 2922 2981 */ 2923 function clean_object_term_cache( $object_ids, $object_type) {2982 function clean_object_term_cache( $object_ids, $object_type ) { 2924 2983 global $_wp_suspend_cache_invalidation; 2925 2984 2926 2985 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { … … 2927 2986 return; 2928 2987 } 2929 2988 2930 if ( !is_array($object_ids) ) 2931 $object_ids = array($object_ids); 2989 if ( ! is_array( $object_ids ) ) { 2990 $object_ids = array( $object_ids ); 2991 } 2932 2992 2933 2993 $taxonomies = get_object_taxonomies( $object_type ); 2934 2994 2935 2995 foreach ( $object_ids as $id ) { 2936 2996 foreach ( $taxonomies as $taxonomy ) { 2937 wp_cache_delete( $id, "{$taxonomy}_relationships");2997 wp_cache_delete( $id, "{$taxonomy}_relationships" ); 2938 2998 } 2939 2999 } 2940 3000 … … 2963 3023 * @param bool $clean_taxonomy Optional. Whether to clean taxonomy wide caches (true), or just individual 2964 3024 * term object caches (false). Default true. 2965 3025 */ 2966 function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true) {3026 function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) { 2967 3027 global $wpdb, $_wp_suspend_cache_invalidation; 2968 3028 2969 3029 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { … … 2970 3030 return; 2971 3031 } 2972 3032 2973 if ( !is_array($ids) ) 2974 $ids = array($ids); 3033 if ( ! is_array( $ids ) ) { 3034 $ids = array( $ids ); 3035 } 2975 3036 2976 3037 $taxonomies = array(); 2977 3038 // If no taxonomy, assume tt_ids. 2978 if ( empty( $taxonomy) ) {2979 $tt_ids = array_map( 'intval', $ids);2980 $tt_ids = implode( ', ', $tt_ids);2981 $terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");3039 if ( empty( $taxonomy ) ) { 3040 $tt_ids = array_map( 'intval', $ids ); 3041 $tt_ids = implode( ', ', $tt_ids ); 3042 $terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" ); 2982 3043 $ids = array(); 2983 3044 foreach ( (array) $terms as $term ) { 2984 3045 $taxonomies[] = $term->taxonomy; … … 2985 3046 $ids[] = $term->term_id; 2986 3047 wp_cache_delete( $term->term_id, 'terms' ); 2987 3048 } 2988 $taxonomies = array_unique( $taxonomies);3049 $taxonomies = array_unique( $taxonomies ); 2989 3050 } else { 2990 $taxonomies = array( $taxonomy);3051 $taxonomies = array( $taxonomy ); 2991 3052 foreach ( $taxonomies as $taxonomy ) { 2992 3053 foreach ( $ids as $id ) { 2993 3054 wp_cache_delete( $id, 'terms' ); … … 2997 3058 2998 3059 foreach ( $taxonomies as $taxonomy ) { 2999 3060 if ( $clean_taxonomy ) { 3000 wp_cache_delete( 'all_ids', $taxonomy);3001 wp_cache_delete( 'get', $taxonomy);3002 delete_option( "{$taxonomy}_children");3061 wp_cache_delete( 'all_ids', $taxonomy ); 3062 wp_cache_delete( 'get', $taxonomy ); 3063 delete_option( "{$taxonomy}_children" ); 3003 3064 // Regenerate {$taxonomy}_children 3004 _get_term_hierarchy( $taxonomy);3065 _get_term_hierarchy( $taxonomy ); 3005 3066 } 3006 3067 3007 3068 /** … … 3087 3148 * @param array|string $object_type The taxonomy object type. 3088 3149 * @return void|false False if all of the terms in `$object_ids` are already cached. 3089 3150 */ 3090 function update_object_term_cache( $object_ids, $object_type) {3091 if ( empty( $object_ids) )3151 function update_object_term_cache( $object_ids, $object_type ) { 3152 if ( empty( $object_ids ) ) { 3092 3153 return; 3154 } 3093 3155 3094 if ( !is_array($object_ids) ) 3095 $object_ids = explode(',', $object_ids); 3156 if ( ! is_array( $object_ids ) ) { 3157 $object_ids = explode( ',', $object_ids ); 3158 } 3096 3159 3097 $object_ids = array_map( 'intval', $object_ids);3160 $object_ids = array_map( 'intval', $object_ids ); 3098 3161 3099 $taxonomies = get_object_taxonomies( $object_type);3162 $taxonomies = get_object_taxonomies( $object_type ); 3100 3163 3101 3164 $ids = array(); 3102 3165 foreach ( (array) $object_ids as $id ) { 3103 3166 foreach ( $taxonomies as $taxonomy ) { 3104 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships") ) {3167 if ( false === wp_cache_get( $id, "{$taxonomy}_relationships" ) ) { 3105 3168 $ids[] = $id; 3106 3169 break; 3107 3170 } … … 3108 3171 } 3109 3172 } 3110 3173 3111 if ( empty( $ids ) ) 3174 if ( empty( $ids ) ) { 3112 3175 return false; 3176 } 3113 3177 3114 3178 $terms = wp_get_object_terms( $ids, $taxonomies, array( 3115 3179 'fields' => 'all_with_object_id', … … 3124 3188 3125 3189 foreach ( $ids as $id ) { 3126 3190 foreach ( $taxonomies as $taxonomy ) { 3127 if ( ! isset($object_terms[$id][$taxonomy]) ) { 3128 if ( !isset($object_terms[$id]) ) 3129 $object_terms[$id] = array(); 3130 $object_terms[$id][$taxonomy] = array(); 3191 if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) { 3192 if ( ! isset( $object_terms[ $id ] ) ) { 3193 $object_terms[ $id ] = array(); 3194 } 3195 $object_terms[ $id ][ $taxonomy ] = array(); 3131 3196 } 3132 3197 } 3133 3198 } … … 3173 3238 * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs. 3174 3239 */ 3175 3240 function _get_term_hierarchy( $taxonomy ) { 3176 if ( ! is_taxonomy_hierarchical($taxonomy) )3241 if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { 3177 3242 return array(); 3178 $children = get_option("{$taxonomy}_children"); 3243 } 3244 $children = get_option( "{$taxonomy}_children" ); 3179 3245 3180 if ( is_array( $children) )3246 if ( is_array( $children ) ) { 3181 3247 return $children; 3248 } 3182 3249 $children = array(); 3183 $terms = get_terms( $taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));3250 $terms = get_terms( $taxonomy, array( 'get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent' ) ); 3184 3251 foreach ( $terms as $term_id => $parent ) { 3185 if ( $parent > 0 ) 3186 $children[$parent][] = $term_id; 3252 if ( $parent > 0 ) { 3253 $children[ $parent ][] = $term_id; 3254 } 3187 3255 } 3188 update_option( "{$taxonomy}_children", $children);3256 update_option( "{$taxonomy}_children", $children ); 3189 3257 3190 3258 return $children; 3191 3259 } … … 3211 3279 */ 3212 3280 function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { 3213 3281 $empty_array = array(); 3214 if ( empty( $terms) )3282 if ( empty( $terms ) ) { 3215 3283 return $empty_array; 3284 } 3216 3285 3217 3286 $term_list = array(); 3218 $has_children = _get_term_hierarchy( $taxonomy);3287 $has_children = _get_term_hierarchy( $taxonomy ); 3219 3288 3220 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )3289 if ( ( 0 != $term_id ) && ! isset( $has_children[ $term_id ] ) ) { 3221 3290 return $empty_array; 3291 } 3222 3292 3223 3293 // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. 3224 3294 if ( empty( $ancestors ) ) { … … 3227 3297 3228 3298 foreach ( (array) $terms as $term ) { 3229 3299 $use_id = false; 3230 if ( ! is_object($term) ) {3231 $term = get_term( $term, $taxonomy);3232 if ( is_wp_error( $term ) ) 3300 if ( ! is_object( $term ) ) { 3301 $term = get_term( $term, $taxonomy ); 3302 if ( is_wp_error( $term ) ) { 3233 3303 return $term; 3304 } 3234 3305 $use_id = true; 3235 3306 } 3236 3307 … … 3240 3311 } 3241 3312 3242 3313 if ( $term->parent == $term_id ) { 3243 if ( $use_id ) 3314 if ( $use_id ) { 3244 3315 $term_list[] = $term->term_id; 3245 else3246 $term_list[] = $term;3316 } else { $term_list[] = $term; 3317 } 3247 3318 3248 if ( ! isset($has_children[$term->term_id]) )3319 if ( ! isset( $has_children[ $term->term_id ] ) ) { 3249 3320 continue; 3321 } 3250 3322 3251 3323 $ancestors[ $term->term_id ] = 1; 3252 3324 3253 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) ) 3254 $term_list = array_merge($term_list, $children); 3325 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ) ) { 3326 $term_list = array_merge( $term_list, $children ); 3327 } 3255 3328 } 3256 3329 } 3257 3330 … … 3276 3349 global $wpdb; 3277 3350 3278 3351 // This function only works for hierarchical taxonomies like post categories. 3279 if ( ! is_taxonomy_hierarchical( $taxonomy ) )3352 if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { 3280 3353 return; 3354 } 3281 3355 3282 $term_hier = _get_term_hierarchy( $taxonomy);3356 $term_hier = _get_term_hierarchy( $taxonomy ); 3283 3357 3284 if ( empty( $term_hier) )3358 if ( empty( $term_hier ) ) { 3285 3359 return; 3360 } 3286 3361 3287 3362 $term_items = array(); 3288 3363 $terms_by_id = array(); … … 3289 3364 $term_ids = array(); 3290 3365 3291 3366 foreach ( (array) $terms as $key => $term ) { 3292 $terms_by_id[ $term->term_id] = & $terms[$key];3293 $term_ids[ $term->term_taxonomy_id] = $term->term_id;3367 $terms_by_id[ $term->term_id ] = & $terms[ $key ]; 3368 $term_ids[ $term->term_taxonomy_id ] = $term->term_id; 3294 3369 } 3295 3370 3296 3371 // Get the object and term ids and stick them in a lookup table. 3297 $tax_obj = get_taxonomy( $taxonomy);3298 $object_types = esc_sql( $tax_obj->object_type);3299 $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'");3372 $tax_obj = get_taxonomy( $taxonomy ); 3373 $object_types = esc_sql( $tax_obj->object_type ); 3374 $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'" ); 3300 3375 foreach ( $results as $row ) { 3301 $id = $term_ids[ $row->term_taxonomy_id];3302 $term_items[ $id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;3376 $id = $term_ids[ $row->term_taxonomy_id ]; 3377 $term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1; 3303 3378 } 3304 3379 3305 3380 // Touch every ancestor's lookup row for each post in each term. … … 3306 3381 foreach ( $term_ids as $term_id ) { 3307 3382 $child = $term_id; 3308 3383 $ancestors = array(); 3309 while ( ! empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {3384 while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) { 3310 3385 $ancestors[] = $child; 3311 if ( ! empty( $term_items[$term_id] ) )3312 foreach ( $term_items[ $term_id] as $item_id => $touches ) {3313 $term_items[ $parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;3386 if ( ! empty( $term_items[ $term_id ] ) ) { 3387 foreach ( $term_items[ $term_id ] as $item_id => $touches ) { 3388 $term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ]: 1; 3314 3389 } 3390 } 3315 3391 $child = $parent; 3316 3392 3317 3393 if ( in_array( $parent, $ancestors ) ) { … … 3321 3397 } 3322 3398 3323 3399 // Transfer the touched cells. 3324 foreach ( (array) $term_items as $id => $items ) 3325 if ( isset($terms_by_id[$id]) ) 3326 $terms_by_id[$id]->count = count($items); 3400 foreach ( (array) $term_items as $id => $items ) { 3401 if ( isset( $terms_by_id[ $id ] ) ) { 3402 $terms_by_id[ $id ]->count = count( $items ); 3403 } 3404 } 3327 3405 } 3328 3406 3329 3407 /** … … 3342 3420 3343 3421 $non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' ); 3344 3422 if ( ! empty( $non_cached_ids ) ) { 3345 $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 ) ) ) );3423 $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 ) ) ) ); 3346 3424 3347 3425 update_term_cache( $fresh_terms, $update_meta_cache ); 3348 3426 … … 3375 3453 3376 3454 $object_types = (array) $taxonomy->object_type; 3377 3455 3378 foreach ( $object_types as &$object_type ) 3456 foreach ( $object_types as &$object_type ) { 3379 3457 list( $object_type ) = explode( ':', $object_type ); 3458 } 3380 3459 3381 3460 $object_types = array_unique( $object_types ); 3382 3461 … … 3385 3464 $check_attachments = true; 3386 3465 } 3387 3466 3388 if ( $object_types ) 3467 if ( $object_types ) { 3389 3468 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 3469 } 3390 3470 3391 3471 foreach ( (array) $terms as $term ) { 3392 3472 $count = 0; 3393 3473 3394 3474 // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 3395 if ( $check_attachments ) 3475 if ( $check_attachments ) { 3396 3476 $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 ) ); 3477 } 3397 3478 3398 if ( $object_types ) 3399 $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 ) ); 3479 if ( $object_types ) { 3480 $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 ) ); 3481 } 3400 3482 3401 3483 /** This action is documented in wp-includes/taxonomy.php */ 3402 3484 do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); … … 3843 3925 function get_term_link( $term, $taxonomy = '' ) { 3844 3926 global $wp_rewrite; 3845 3927 3846 if ( ! is_object($term) ) {3928 if ( ! is_object( $term ) ) { 3847 3929 if ( is_int( $term ) ) { 3848 3930 $term = get_term( $term, $taxonomy ); 3849 3931 } else { … … 3851 3933 } 3852 3934 } 3853 3935 3854 if ( !is_object($term) ) 3855 $term = new WP_Error('invalid_term', __('Empty Term')); 3936 if ( ! is_object( $term ) ) { 3937 $term = new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 3938 } 3856 3939 3857 if ( is_wp_error( $term ) ) 3940 if ( is_wp_error( $term ) ) { 3858 3941 return $term; 3942 } 3859 3943 3860 3944 $taxonomy = $term->taxonomy; 3861 3945 3862 $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy);3946 $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy ); 3863 3947 3864 3948 $slug = $term->slug; 3865 $t = get_taxonomy( $taxonomy);3949 $t = get_taxonomy( $taxonomy ); 3866 3950 3867 if ( empty( $termlink) ) {3868 if ( 'category' == $taxonomy ) 3951 if ( empty( $termlink ) ) { 3952 if ( 'category' == $taxonomy ) { 3869 3953 $termlink = '?cat=' . $term->term_id; 3870 elseif ( $t->query_var )3954 } elseif ( $t->query_var ) { 3871 3955 $termlink = "?$t->query_var=$slug"; 3872 else3873 $termlink = "?taxonomy=$taxonomy&term=$slug";3874 $termlink = home_url( $termlink);3956 } else { $termlink = "?taxonomy=$taxonomy&term=$slug"; 3957 } 3958 $termlink = home_url( $termlink ); 3875 3959 } else { 3876 3960 if ( $t->rewrite['hierarchical'] ) { 3877 3961 $hierarchical_slugs = array(); 3878 3962 $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); 3879 foreach ( (array) $ancestors as $ancestor ) {3880 $ancestor_term = get_term( $ancestor, $taxonomy);3963 foreach ( (array) $ancestors as $ancestor ) { 3964 $ancestor_term = get_term( $ancestor, $taxonomy ); 3881 3965 $hierarchical_slugs[] = $ancestor_term->slug; 3882 3966 } 3883 $hierarchical_slugs = array_reverse( $hierarchical_slugs);3967 $hierarchical_slugs = array_reverse( $hierarchical_slugs ); 3884 3968 $hierarchical_slugs[] = $slug; 3885 $termlink = str_replace( "%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);3969 $termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink ); 3886 3970 } else { 3887 $termlink = str_replace( "%$taxonomy%", $slug, $termlink);3971 $termlink = str_replace( "%$taxonomy%", $slug, $termlink ); 3888 3972 } 3889 $termlink = home_url( user_trailingslashit( $termlink, 'category') );3973 $termlink = home_url( user_trailingslashit( $termlink, 'category' ) ); 3890 3974 } 3891 3975 // Back Compat filters. 3892 3976 if ( 'post_tag' == $taxonomy ) { … … 4018 4102 $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name ); 4019 4103 } 4020 4104 if ( $links ) { 4021 $taxonomies[ $taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );4105 $taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); 4022 4106 } 4023 4107 } 4024 4108 return $taxonomies; … … 4035 4119 function get_post_taxonomies( $post = 0 ) { 4036 4120 $post = get_post( $post ); 4037 4121 4038 return get_object_taxonomies( $post);4122 return get_object_taxonomies( $post ); 4039 4123 } 4040 4124 4041 4125 /** … … 4053 4137 * @return bool|WP_Error WP_Error on input error. 4054 4138 */ 4055 4139 function is_object_in_term( $object_id, $taxonomy, $terms = null ) { 4056 if ( ! $object_id = (int) $object_id )4140 if ( ! $object_id = (int) $object_id ) { 4057 4141 return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) ); 4142 } 4058 4143 4059 4144 $object_terms = get_object_term_cache( $object_id, $taxonomy ); 4060 4145 if ( false === $object_terms ) { … … 4066 4151 wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" ); 4067 4152 } 4068 4153 4069 if ( is_wp_error( $object_terms ) ) 4154 if ( is_wp_error( $object_terms ) ) { 4070 4155 return $object_terms; 4071 if ( empty( $object_terms ) ) 4156 } 4157 if ( empty( $object_terms ) ) { 4072 4158 return false; 4073 if ( empty( $terms ) ) 4074 return ( !empty( $object_terms ) ); 4159 } 4160 if ( empty( $terms ) ) { 4161 return ( ! empty( $object_terms ) ); 4162 } 4075 4163 4076 4164 $terms = (array) $terms; 4077 4165 4078 if ( $ints = array_filter( $terms, 'is_int' ) ) 4166 if ( $ints = array_filter( $terms, 'is_int' ) ) { 4079 4167 $strs = array_diff( $terms, $ints ); 4080 else4081 $strs =& $terms;4168 } else { $strs =& $terms; 4169 } 4082 4170 4083 4171 foreach ( $object_terms as $object_term ) { 4084 4172 // If term is an int, check against term_ids only. … … 4093 4181 return true; 4094 4182 } 4095 4183 4096 if ( in_array( $object_term->name, $strs ) ) return true; 4097 if ( in_array( $object_term->slug, $strs ) ) return true; 4184 if ( in_array( $object_term->name, $strs ) ) { return true; 4185 } 4186 if ( in_array( $object_term->slug, $strs ) ) { return true; 4187 } 4098 4188 } 4099 4189 } 4100 4190 … … 4151 4241 } 4152 4242 4153 4243 if ( 'taxonomy' === $resource_type ) { 4154 $term = get_term( $object_id, $object_type);4155 while ( ! is_wp_error( $term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {4244 $term = get_term( $object_id, $object_type ); 4245 while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { 4156 4246 $ancestors[] = (int) $term->parent; 4157 $term = get_term( $term->parent, $object_type);4247 $term = get_term( $term->parent, $object_type ); 4158 4248 } 4159 4249 } elseif ( 'post_type' === $resource_type ) { 4160 $ancestors = get_post_ancestors( $object_id);4250 $ancestors = get_post_ancestors( $object_id ); 4161 4251 } 4162 4252 4163 4253 /** … … 4207 4297 */ 4208 4298 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { 4209 4299 // Nothing fancy here - bail 4210 if ( ! $parent )4300 if ( ! $parent ) { 4211 4301 return 0; 4302 } 4212 4303 4213 4304 // Can't be its own parent. 4214 if ( $parent == $term_id ) 4305 if ( $parent == $term_id ) { 4215 4306 return 0; 4307 } 4216 4308 4217 4309 // Now look for larger loops. 4218 if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )4310 if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) { 4219 4311 return $parent; // No loop 4312 } 4220 4313 4221 4314 // Setting $parent to the given value causes a loop. 4222 if ( isset( $loop[ $term_id] ) )4315 if ( isset( $loop[ $term_id ] ) ) { 4223 4316 return 0; 4317 } 4224 4318 4225 4319 // There's a loop, but it doesn't contain $term_id. Break the loop. 4226 foreach ( array_keys( $loop ) as $loop_member ) 4320 foreach ( array_keys( $loop ) as $loop_member ) { 4227 4321 wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); 4322 } 4228 4323 4229 4324 return $parent; 4230 4325 }