WordPress.org

Make WordPress Core

Ticket #15242: 15242.4.diff

File 15242.4.diff, 25.4 KB (added by wpmuguru, 8 years ago)

lines 1000-1800 of 15242.diff

  • wp-includes/taxonomy.php

     
    10561059                $where .= ' AND tt.count > 0';
    10571060
    10581061        // don't limit the query results when we have to descend the family tree
    1059         if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
     1062        if ( ! empty( $number ) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
    10601063                if ( $offset )
    10611064                        $limit = 'LIMIT ' . $offset . ',' . $number;
    10621065                else
     
    11361139
    11371140        $_terms = array();
    11381141        if ( 'id=>parent' == $fields ) {
    1139                 while ( $term = array_shift($terms) )
     1142                while ( $term = array_shift( $terms ) )
    11401143                        $_terms[$term->term_id] = $term->parent;
    11411144                $terms = $_terms;
    11421145        } elseif ( 'ids' == $fields ) {
    1143                 while ( $term = array_shift($terms) )
     1146                while ( $term = array_shift( $terms ) )
    11441147                        $_terms[] = $term->term_id;
    11451148                $terms = $_terms;
    11461149        } elseif ( 'names' == $fields ) {
    1147                 while ( $term = array_shift($terms) )
     1150                while ( $term = array_shift( $terms ) )
    11481151                        $_terms[] = $term->name;
    11491152                $terms = $_terms;
    11501153        }
    11511154
    1152         if ( 0 < $number && intval(@count($terms)) > $number ) {
    1153                 $terms = array_slice($terms, $offset, $number);
     1155        if ( 0 < $number && intval( @count( $terms ) ) > $number ) {
     1156                $terms = array_slice( $terms, $offset, $number );
    11541157        }
    11551158
    11561159        wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day
    11571160
    1158         $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
     1161        $terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
    11591162        return $terms;
    11601163}
    11611164
     
    11771180 * @param int $parent ID of parent term under which to confine the exists search.
    11781181 * @return mixed Get the term id or Term Object, if exists.
    11791182 */
    1180 function term_exists($term, $taxonomy = '', $parent = 0) {
     1183function term_exists( $term, $taxonomy = '', $parent = 0 ) {
    11811184        global $wpdb;
    11821185
    11831186        $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
    11841187        $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 ";
    11851188
    1186         if ( is_int($term) ) {
     1189        if ( is_int( $term ) ) {
    11871190                if ( 0 == $term )
    11881191                        return 0;
    11891192                $where = 't.term_id = %d';
    1190                 if ( !empty($taxonomy) )
     1193                if ( !empty( $taxonomy ) )
    11911194                        return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
    11921195                else
    11931196                        return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
     
    11951198
    11961199        $term = trim( stripslashes( $term ) );
    11971200
    1198         if ( '' === $slug = sanitize_title($term) )
     1201        if ( '' === $slug = sanitize_title( $term ) )
    11991202                return 0;
    12001203
    12011204        $where = 't.slug = %s';
    12021205        $else_where = 't.name = %s';
    1203         $where_fields = array($slug);
    1204         $else_where_fields = array($term);
    1205         if ( !empty($taxonomy) ) {
     1206        $where_fields = array( $slug );
     1207        $else_where_fields = array( $term );
     1208        if ( !empty( $taxonomy ) ) {
    12061209                $parent = (int) $parent;
    12071210                if ( $parent > 0 ) {
    12081211                        $where_fields[] = $parent;
     
    12141217                $where_fields[] = $taxonomy;
    12151218                $else_where_fields[] = $taxonomy;
    12161219
    1217                 if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields), ARRAY_A) )
     1220                if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields ), ARRAY_A ) )
    12181221                        return $result;
    12191222
    1220                 return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
     1223                return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields ), ARRAY_A );
    12211224        }
    12221225
    1223         if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )
     1226        if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields ) ) )
    12241227                return $result;
    12251228
    1226         return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
     1229        return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields ) );
    12271230}
    12281231
    12291232/**
     
    12461249 * @param string $context Default is 'display'.
    12471250 * @return array|object Term with all fields sanitized
    12481251 */
    1249 function sanitize_term($term, $taxonomy, $context = 'display') {
     1252function sanitize_term( $term, $taxonomy, $context = 'display' ) {
    12501253
    12511254        if ( 'raw' == $context )
    12521255                return $term;
    12531256
    1254         $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
     1257        $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group' );
    12551258
    12561259        $do_object = false;
    1257         if ( is_object($term) )
     1260        if ( is_object( $term ) )
    12581261                $do_object = true;
    12591262
    1260         $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
     1263        $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );
    12611264
    12621265        foreach ( (array) $fields as $field ) {
    12631266                if ( $do_object ) {
    1264                         if ( isset($term->$field) )
    1265                                 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
     1267                        if ( isset( $term->$field ) )
     1268                                $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
    12661269                } else {
    1267                         if ( isset($term[$field]) )
    1268                                 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
     1270                        if ( isset( $term[$field] ) )
     1271                                $term[$field] = sanitize_term_field( $field, $term[$field], $term_id, $taxonomy, $context );
    12691272                }
    12701273        }
    12711274
     
    13031306 * @param string $context Either edit, db, display, attribute, or js.
    13041307 * @return mixed sanitized field
    13051308 */
    1306 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
     1309function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
    13071310        if ( 'parent' == $field  || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
    13081311                $value = (int) $value;
    13091312                if ( $value < 0 )
     
    13141317                return $value;
    13151318
    13161319        if ( 'edit' == $context ) {
    1317                 $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
    1318                 $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
     1320                $value = apply_filters( "edit_term_$field", $value, $term_id, $taxonomy );
     1321                $value = apply_filters( "edit_${taxonomy}_$field", $value, $term_id );
    13191322                if ( 'description' == $field )
    1320                         $value = format_to_edit($value);
     1323                        $value = format_to_edit( $value );
    13211324                else
    1322                         $value = esc_attr($value);
     1325                        $value = esc_attr( $value );
    13231326        } else if ( 'db' == $context ) {
    1324                 $value = apply_filters("pre_term_$field", $value, $taxonomy);
    1325                 $value = apply_filters("pre_${taxonomy}_$field", $value);
     1327                $value = apply_filters( "pre_term_$field", $value, $taxonomy );
     1328                $value = apply_filters( "pre_${taxonomy}_$field", $value );
    13261329                // Back compat filters
    13271330                if ( 'slug' == $field )
    1328                         $value = apply_filters('pre_category_nicename', $value);
     1331                        $value = apply_filters( 'pre_category_nicename', $value );
    13291332
    13301333        } else if ( 'rss' == $context ) {
    1331                 $value = apply_filters("term_${field}_rss", $value, $taxonomy);
    1332                 $value = apply_filters("${taxonomy}_${field}_rss", $value);
     1334                $value = apply_filters( "term_${field}_rss", $value, $taxonomy );
     1335                $value = apply_filters( "${taxonomy}_${field}_rss", $value );
    13331336        } else {
    13341337                // Use display filters by default.
    1335                 $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
    1336                 $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
     1338                $value = apply_filters( "term_$field", $value, $term_id, $taxonomy, $context );
     1339                $value = apply_filters( "${taxonomy}_$field", $value, $term_id, $context );
    13371340        }
    13381341
    13391342        if ( 'attribute' == $context )
    1340                 $value = esc_attr($value);
     1343                $value = esc_attr( $value );
    13411344        else if ( 'js' == $context )
    1342                 $value = esc_js($value);
     1345                $value = esc_js( $value );
    13431346
    13441347        return $value;
    13451348}
     
    13611364 * @return int How many terms are in $taxonomy
    13621365 */
    13631366function wp_count_terms( $taxonomy, $args = array() ) {
    1364         $defaults = array('hide_empty' => false);
    1365         $args = wp_parse_args($args, $defaults);
     1367        $defaults = array( 'hide_empty' => false );
     1368        $args = wp_parse_args( $args, $defaults );
    13661369
    13671370        // backwards compatibility
    1368         if ( isset($args['ignore_empty']) ) {
     1371        if ( isset( $args['ignore_empty'] ) ) {
    13691372                $args['hide_empty'] = $args['ignore_empty'];
    1370                 unset($args['ignore_empty']);
     1373                unset( $args['ignore_empty'] );
    13711374        }
    13721375
    13731376        $args['fields'] = 'count';
    13741377
    1375         return get_terms($taxonomy, $args);
     1378        return get_terms( $taxonomy, $args );
    13761379}
    13771380
    13781381/**
     
    13951398
    13961399        $object_id = (int) $object_id;
    13971400
    1398         if ( !is_array($taxonomies) )
    1399                 $taxonomies = array($taxonomies);
     1401        if ( !is_array( $taxonomies ) )
     1402                $taxonomies = array( $taxonomies );
    14001403
    14011404        foreach ( (array) $taxonomies as $taxonomy ) {
    1402                 $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
    1403                 $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
     1405                $tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) );
     1406                $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
    14041407                do_action( 'delete_term_relationships', $object_id, $tt_ids );
    1405                 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
     1408                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
    14061409                do_action( 'deleted_term_relationships', $object_id, $tt_ids );
    1407                 wp_update_term_count($tt_ids, $taxonomy);
     1410                wp_update_term_count( $tt_ids, $taxonomy );
    14081411        }
    14091412}
    14101413
     
    14381441
    14391442        $term = (int) $term;
    14401443
    1441         if ( ! $ids = term_exists($term, $taxonomy) )
     1444        if ( ! $ids = term_exists( $term, $taxonomy ) )
    14421445                return false;
    14431446        if ( is_wp_error( $ids ) )
    14441447                return $ids;
     
    14531456                        return 0; // Don't delete the default category
    14541457        }
    14551458
    1456         $args = wp_parse_args($args, $defaults);
    1457         extract($args, EXTR_SKIP);
     1459        $args = wp_parse_args( $args, $defaults );
     1460        extract( $args, EXTR_SKIP );
    14581461
    14591462        if ( isset( $default ) ) {
    14601463                $default = (int) $default;
    1461                 if ( ! term_exists($default, $taxonomy) )
    1462                         unset($default);
     1464                if ( ! term_exists( $default, $taxonomy ) )
     1465                        unset( $default );
    14631466        }
    14641467
    14651468        // Update children to point to new parent
    1466         if ( is_taxonomy_hierarchical($taxonomy) ) {
    1467                 $term_obj = get_term($term, $taxonomy);
     1469        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     1470                $term_obj = get_term( $term, $taxonomy );
    14681471                if ( is_wp_error( $term_obj ) )
    14691472                        return $term_obj;
    14701473                $parent = $term_obj->parent;
    14711474
    14721475                $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
    14731476                do_action( 'edit_term_taxonomies', $edit_tt_ids );
    1474                 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
     1477                $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
    14751478                do_action( 'edited_term_taxonomies', $edit_tt_ids );
    14761479        }
    14771480
    14781481        $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
    14791482
    14801483        foreach ( (array) $objects as $object ) {
    1481                 $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
    1482                 if ( 1 == count($terms) && isset($default) ) {
    1483                         $terms = array($default);
     1484                $terms = wp_get_object_terms( $object, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) );
     1485                if ( 1 == count( $terms ) && isset( $default ) ) {
     1486                        $terms = array( $default );
    14841487                } else {
    1485                         $terms = array_diff($terms, array($term));
    1486                         if (isset($default) && isset($force_default) && $force_default)
    1487                                 $terms = array_merge($terms, array($default));
     1488                        $terms = array_diff( $terms, array( $term ) );
     1489                        if ( isset( $default ) && isset( $force_default ) && $force_default )
     1490                                $terms = array_merge( $terms, array( $default ) );
    14881491                }
    1489                 $terms = array_map('intval', $terms);
    1490                 wp_set_object_terms($object, $terms, $taxonomy);
     1492                $terms = array_map( 'intval', $terms );
     1493                wp_set_object_terms( $object, $terms, $taxonomy );
    14911494        }
    14921495
    14931496        do_action( 'delete_term_taxonomy', $tt_id );
     
    14951498        do_action( 'deleted_term_taxonomy', $tt_id );
    14961499
    14971500        // Delete the term if no taxonomies use it.
    1498         if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
    1499                 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
     1501        if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) )
     1502                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term ) );
    15001503
    1501         clean_term_cache($term, $taxonomy);
     1504        clean_term_cache( $term, $taxonomy );
    15021505
    1503         do_action('delete_term', $term, $tt_id, $taxonomy);
    1504         do_action("delete_$taxonomy", $term, $tt_id);
     1506        do_action( 'delete_term', $term, $tt_id, $taxonomy );
     1507        do_action( "delete_$taxonomy", $term, $tt_id );
    15051508
    15061509        return true;
    15071510}
     
    15521555 * @param array|string $args Change what is returned
    15531556 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
    15541557 */
    1555 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
     1558function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
    15561559        global $wpdb;
    15571560
    1558         if ( !is_array($taxonomies) )
    1559                 $taxonomies = array($taxonomies);
     1561        if ( !is_array( $taxonomies ) )
     1562                $taxonomies = array( $taxonomies );
    15601563
    15611564        foreach ( (array) $taxonomies as $taxonomy ) {
    1562                 if ( ! taxonomy_exists($taxonomy) )
    1563                         return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     1565                if ( ! taxonomy_exists( $taxonomy ) )
     1566                        return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
    15641567        }
    15651568
    1566         if ( !is_array($object_ids) )
    1567                 $object_ids = array($object_ids);
    1568         $object_ids = array_map('intval', $object_ids);
     1569        if ( !is_array( $object_ids ) )
     1570                $object_ids = array( $object_ids );
     1571        $object_ids = array_map( 'intval', $object_ids );
    15691572
    1570         $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
     1573        $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all' );
    15711574        $args = wp_parse_args( $args, $defaults );
    15721575
    15731576        $terms = array();
    1574         if ( count($taxonomies) > 1 ) {
     1577        if ( count( $taxonomies ) > 1 ) {
    15751578                foreach ( $taxonomies as $index => $taxonomy ) {
    1576                         $t = get_taxonomy($taxonomy);
    1577                         if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
    1578                                 unset($taxonomies[$index]);
    1579                                 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
     1579                        $t = get_taxonomy( $taxonomy );
     1580                        if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) {
     1581                                unset( $taxonomies[$index] );
     1582                                $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
    15801583                        }
    15811584                }
    15821585        } else {
    1583                 $t = get_taxonomy($taxonomies[0]);
    1584                 if ( isset($t->args) && is_array($t->args) )
    1585                         $args = array_merge($args, $t->args);
     1586                $t = get_taxonomy( $taxonomies[0] );
     1587                if ( isset( $t->args ) && is_array( $t->args ) )
     1588                        $args = array_merge( $args, $t->args );
    15861589        }
    15871590
    1588         extract($args, EXTR_SKIP);
     1591        extract( $args, EXTR_SKIP );
    15891592
    15901593        if ( 'count' == $orderby )
    15911594                $orderby = 'tt.count';
     
    16051608        }
    16061609
    16071610        // tt_ids queries can only be none or tr.term_taxonomy_id
    1608         if ( ('tt_ids' == $fields) && !empty($orderby) )
     1611        if ( ( 'tt_ids' == $fields ) && !empty( $orderby ) )
    16091612                $orderby = 'tr.term_taxonomy_id';
    16101613
    1611         if ( !empty($orderby) )
     1614        if ( !empty( $orderby ) )
    16121615                $orderby = "ORDER BY $orderby";
    16131616
    1614         $taxonomies = "'" . implode("', '", $taxonomies) . "'";
    1615         $object_ids = implode(', ', $object_ids);
     1617        $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
     1618        $object_ids = implode( ', ', $object_ids );
    16161619
    16171620        $select_this = '';
    16181621        if ( 'all' == $fields )
     
    16271630        $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
    16281631
    16291632        if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    1630                 $terms = array_merge($terms, $wpdb->get_results($query));
    1631                 update_term_cache($terms);
     1633                $terms = array_merge( $terms, $wpdb->get_results( $query ) );
     1634                update_term_cache( $terms );
    16321635        } else if ( 'ids' == $fields || 'names' == $fields ) {
    1633                 $terms = array_merge($terms, $wpdb->get_col($query));
     1636                $terms = array_merge( $terms, $wpdb->get_col( $query ) );
    16341637        } else if ( 'tt_ids' == $fields ) {
    1635                 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
     1638                $terms = $wpdb->get_col( "SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order" );
    16361639        }
    16371640
    16381641        if ( ! $terms )
    16391642                $terms = array();
    16401643
    1641         return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);
     1644        return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
    16421645}
    16431646
    16441647/**
     
    16971700function wp_insert_term( $term, $taxonomy, $args = array() ) {
    16981701        global $wpdb;
    16991702
    1700         if ( ! taxonomy_exists($taxonomy) )
    1701                 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     1703        if ( ! taxonomy_exists( $taxonomy ) )
     1704                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    17021705
    17031706        $term = apply_filters( 'pre_insert_term', $term, $taxonomy );
    17041707                if ( is_wp_error( $term ) )
    17051708                        return $term;
    17061709
    1707         if ( is_int($term) && 0 == $term )
    1708                 return new WP_Error('invalid_term_id', __('Invalid term ID'));
     1710        if ( is_int( $term ) && 0 == $term )
     1711                return new WP_Error( 'invalid_term_id', __( 'Invalid term ID' ) );
    17091712
    1710         if ( '' == trim($term) )
    1711                 return new WP_Error('empty_term_name', __('A name is required for this term'));
     1713        if ( '' == trim( $term ) )
     1714                return new WP_Error( 'empty_term_name', __( 'A name is required for this term' ) );
    17121715
    1713         $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    1714         $args = wp_parse_args($args, $defaults);
     1716        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '' );
     1717        $args = wp_parse_args( $args, $defaults );
    17151718        $args['name'] = $term;
    17161719        $args['taxonomy'] = $taxonomy;
    1717         $args = sanitize_term($args, $taxonomy, 'db');
    1718         extract($args, EXTR_SKIP);
     1720        $args = sanitize_term( $args, $taxonomy, 'db' );
     1721        extract( $args, EXTR_SKIP );
    17191722
    17201723        // expected_slashed ($name)
    1721         $name = stripslashes($name);
    1722         $description = stripslashes($description);
     1724        $name = stripslashes( $name );
     1725        $description = stripslashes( $description );
    17231726
    1724         if ( empty($slug) )
    1725                 $slug = sanitize_title($name);
     1727        if ( empty( $slug ) )
     1728                $slug = sanitize_title( $name );
    17261729
    17271730        $term_group = 0;
    17281731        if ( $alias_of ) {
    1729                 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
     1732                $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of ) );
    17301733                if ( $alias->term_group ) {
    17311734                        // The alias we want is already in a group, so let's use that one.
    17321735                        $term_group = $alias->term_group;
    17331736                } else {
    17341737                        // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
    1735                         $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
     1738                        $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
    17361739                        do_action( 'edit_terms', $alias->term_id );
    1737                         $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
     1740                        $wpdb->update( $wpdb->terms, compact( 'term_group' ), array( 'term_id' => $alias->term_id ) );
    17381741                        do_action( 'edited_terms', $alias->term_id );
    17391742                }
    17401743        }
    17411744
    1742         if ( $term_id = term_exists($slug) ) {
    1743                 $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );
     1745        if ( $term_id = term_exists( $slug ) ) {
     1746                $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id ), ARRAY_A );
    17441747                // We've got an existing term in the same taxonomy, which matches the name of the new term:
    1745                 if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {
     1748                if ( is_taxonomy_hierarchical( $taxonomy ) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {
    17461749                        // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.
    1747                         $siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );
    1748                         if ( in_array($name, $siblings) ) {
    1749                                 return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);
     1750                        $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => (int)$parent ) );
     1751                        if ( in_array( $name, $siblings ) ) {
     1752                                return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] );
    17501753                        } else {
    1751                                 $slug = wp_unique_term_slug($slug, (object) $args);
     1754                                $slug = wp_unique_term_slug( $slug, (object) $args );
    17521755                                if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    1753                                         return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     1756                                        return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
    17541757                                $term_id = (int) $wpdb->insert_id;
    17551758                        }
    17561759                } elseif ( $existing_term['name'] != $name ) {
    17571760                        // We've got an existing term, with a different name, Create the new term.
    1758                         $slug = wp_unique_term_slug($slug, (object) $args);
     1761                        $slug = wp_unique_term_slug( $slug, (object) $args );
    17591762                        if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    1760                                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     1763                                return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
    17611764                        $term_id = (int) $wpdb->insert_id;
    17621765                } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {
    17631766                        // Same name, same slug.
    1764                         return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);
     1767                        return new WP_Error( 'term_exists', __( 'A term with the name provided already exists.' ), $exists['term_id'] );
    17651768                }
    17661769        } else {
    17671770                // This term does not exist at all in the database, Create it.
    1768                 $slug = wp_unique_term_slug($slug, (object) $args);
     1771                $slug = wp_unique_term_slug( $slug, (object) $args );
    17691772                if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    1770                         return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     1773                        return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
    17711774                $term_id = (int) $wpdb->insert_id;
    17721775        }
    17731776
    17741777        // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
    1775         if ( empty($slug) ) {
    1776                 $slug = sanitize_title($slug, $term_id);
     1778        if ( empty( $slug ) ) {
     1779                $slug = sanitize_title( $slug, $term_id );
    17771780                do_action( 'edit_terms', $term_id );
    17781781                $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
    17791782                do_action( 'edited_terms', $term_id );
     
    17811784
    17821785        $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 ) );
    17831786
    1784         if ( !empty($tt_id) )
    1785                 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     1787        if ( !empty( $tt_id ) )
     1788                return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
    17861789
    17871790        $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
    17881791        $tt_id = (int) $wpdb->insert_id;
    17891792
    1790         do_action("create_term", $term_id, $tt_id, $taxonomy);
    1791         do_action("create_$taxonomy", $term_id, $tt_id);
     1793        do_action( "create_term", $term_id, $tt_id, $taxonomy );
     1794        do_action( "create_$taxonomy", $term_id, $tt_id );
    17921795
    1793         $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
     1796        $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
    17941797
    1795         clean_term_cache($term_id, $taxonomy);
     1798        clean_term_cache( $term_id, $taxonomy );
    17961799
    1797         do_action("created_term", $term_id, $tt_id, $taxonomy);
    1798         do_action("created_$taxonomy", $term_id, $tt_id);
     1800        do_action( "created_term", $term_id, $tt_id, $taxonomy );
     1801        do_action( "created_$taxonomy", $term_id, $tt_id );
    17991802
    1800         return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     1803        return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
    18011804}
    18021805
    18031806/**