Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r42211 r42343  
    2626
    2727    if ( ! did_action( 'init' ) ) {
    28         $rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );
     28        $rewrite = array(
     29            'category'    => false,
     30            'post_tag'    => false,
     31            'post_format' => false,
     32        );
    2933    } else {
    3034
     
    3741         */
    3842        $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
    39         $rewrite = array(
    40             'category' => array(
     43        $rewrite          = array(
     44            'category'    => array(
    4145                'hierarchical' => true,
    42                 'slug' => get_option('category_base') ? get_option('category_base') : 'category',
    43                 'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
    44                 'ep_mask' => EP_CATEGORIES,
     46                'slug'         => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category',
     47                'with_front'   => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(),
     48                'ep_mask'      => EP_CATEGORIES,
    4549            ),
    46             'post_tag' => array(
     50            'post_tag'    => array(
    4751                'hierarchical' => false,
    48                 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
    49                 'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
    50                 'ep_mask' => EP_TAGS,
     52                'slug'         => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag',
     53                'with_front'   => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(),
     54                'ep_mask'      => EP_TAGS,
    5155            ),
    5256            'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
     
    5458    }
    5559
    56     register_taxonomy( 'category', 'post', array(
    57         'hierarchical' => true,
    58         'query_var' => 'category_name',
    59         'rewrite' => $rewrite['category'],
    60         'public' => true,
    61         'show_ui' => true,
    62         'show_admin_column' => true,
    63         '_builtin' => true,
    64         'capabilities' => array(
    65             'manage_terms' => 'manage_categories',
    66             'edit_terms'   => 'edit_categories',
    67             'delete_terms' => 'delete_categories',
    68             'assign_terms' => 'assign_categories',
    69         ),
    70         'show_in_rest' => true,
    71         'rest_base' => 'categories',
    72         'rest_controller_class' => 'WP_REST_Terms_Controller',
    73     ) );
    74 
    75     register_taxonomy( 'post_tag', 'post', array(
    76         'hierarchical' => false,
    77         'query_var' => 'tag',
    78         'rewrite' => $rewrite['post_tag'],
    79         'public' => true,
    80         'show_ui' => true,
    81         'show_admin_column' => true,
    82         '_builtin' => true,
    83         'capabilities' => array(
    84             'manage_terms' => 'manage_post_tags',
    85             'edit_terms'   => 'edit_post_tags',
    86             'delete_terms' => 'delete_post_tags',
    87             'assign_terms' => 'assign_post_tags',
    88         ),
    89         'show_in_rest' => true,
    90         'rest_base' => 'tags',
    91         'rest_controller_class' => 'WP_REST_Terms_Controller',
    92     ) );
    93 
    94     register_taxonomy( 'nav_menu', 'nav_menu_item', array(
    95         'public' => false,
    96         'hierarchical' => false,
    97         'labels' => array(
    98             'name' => __( 'Navigation Menus' ),
    99             'singular_name' => __( 'Navigation Menu' ),
    100         ),
    101         'query_var' => false,
    102         'rewrite' => false,
    103         'show_ui' => false,
    104         '_builtin' => true,
    105         'show_in_nav_menus' => false,
    106     ) );
    107 
    108     register_taxonomy( 'link_category', 'link', array(
    109         'hierarchical' => false,
    110         'labels' => array(
    111             'name' => __( 'Link Categories' ),
    112             'singular_name' => __( 'Link Category' ),
    113             'search_items' => __( 'Search Link Categories' ),
    114             'popular_items' => null,
    115             'all_items' => __( 'All Link Categories' ),
    116             'edit_item' => __( 'Edit Link Category' ),
    117             'update_item' => __( 'Update Link Category' ),
    118             'add_new_item' => __( 'Add New Link Category' ),
    119             'new_item_name' => __( 'New Link Category Name' ),
    120             'separate_items_with_commas' => null,
    121             'add_or_remove_items' => null,
    122             'choose_from_most_used' => null,
    123             'back_to_items' => __( '← Back to Link Categories' ),
    124         ),
    125         'capabilities' => array(
    126             'manage_terms' => 'manage_links',
    127             'edit_terms'   => 'manage_links',
    128             'delete_terms' => 'manage_links',
    129             'assign_terms' => 'manage_links',
    130         ),
    131         'query_var' => false,
    132         'rewrite' => false,
    133         'public' => false,
    134         'show_ui' => true,
    135         '_builtin' => true,
    136     ) );
    137 
    138     register_taxonomy( 'post_format', 'post', array(
    139         'public' => true,
    140         'hierarchical' => false,
    141         'labels' => array(
    142             'name' => _x( 'Format', 'post format' ),
    143             'singular_name' => _x( 'Format', 'post format' ),
    144         ),
    145         'query_var' => true,
    146         'rewrite' => $rewrite['post_format'],
    147         'show_ui' => false,
    148         '_builtin' => true,
    149         'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
    150     ) );
     60    register_taxonomy(
     61        'category', 'post', array(
     62            'hierarchical'          => true,
     63            'query_var'             => 'category_name',
     64            'rewrite'               => $rewrite['category'],
     65            'public'                => true,
     66            'show_ui'               => true,
     67            'show_admin_column'     => true,
     68            '_builtin'              => true,
     69            'capabilities'          => array(
     70                'manage_terms' => 'manage_categories',
     71                'edit_terms'   => 'edit_categories',
     72                'delete_terms' => 'delete_categories',
     73                'assign_terms' => 'assign_categories',
     74            ),
     75            'show_in_rest'          => true,
     76            'rest_base'             => 'categories',
     77            'rest_controller_class' => 'WP_REST_Terms_Controller',
     78        )
     79    );
     80
     81    register_taxonomy(
     82        'post_tag', 'post', array(
     83            'hierarchical'          => false,
     84            'query_var'             => 'tag',
     85            'rewrite'               => $rewrite['post_tag'],
     86            'public'                => true,
     87            'show_ui'               => true,
     88            'show_admin_column'     => true,
     89            '_builtin'              => true,
     90            'capabilities'          => array(
     91                'manage_terms' => 'manage_post_tags',
     92                'edit_terms'   => 'edit_post_tags',
     93                'delete_terms' => 'delete_post_tags',
     94                'assign_terms' => 'assign_post_tags',
     95            ),
     96            'show_in_rest'          => true,
     97            'rest_base'             => 'tags',
     98            'rest_controller_class' => 'WP_REST_Terms_Controller',
     99        )
     100    );
     101
     102    register_taxonomy(
     103        'nav_menu', 'nav_menu_item', array(
     104            'public'            => false,
     105            'hierarchical'      => false,
     106            'labels'            => array(
     107                'name'          => __( 'Navigation Menus' ),
     108                'singular_name' => __( 'Navigation Menu' ),
     109            ),
     110            'query_var'         => false,
     111            'rewrite'           => false,
     112            'show_ui'           => false,
     113            '_builtin'          => true,
     114            'show_in_nav_menus' => false,
     115        )
     116    );
     117
     118    register_taxonomy(
     119        'link_category', 'link', array(
     120            'hierarchical' => false,
     121            'labels'       => array(
     122                'name'                       => __( 'Link Categories' ),
     123                'singular_name'              => __( 'Link Category' ),
     124                'search_items'               => __( 'Search Link Categories' ),
     125                'popular_items'              => null,
     126                'all_items'                  => __( 'All Link Categories' ),
     127                'edit_item'                  => __( 'Edit Link Category' ),
     128                'update_item'                => __( 'Update Link Category' ),
     129                'add_new_item'               => __( 'Add New Link Category' ),
     130                'new_item_name'              => __( 'New Link Category Name' ),
     131                'separate_items_with_commas' => null,
     132                'add_or_remove_items'        => null,
     133                'choose_from_most_used'      => null,
     134                'back_to_items'              => __( '← Back to Link Categories' ),
     135            ),
     136            'capabilities' => array(
     137                'manage_terms' => 'manage_links',
     138                'edit_terms'   => 'manage_links',
     139                'delete_terms' => 'manage_links',
     140                'assign_terms' => 'manage_links',
     141            ),
     142            'query_var'    => false,
     143            'rewrite'      => false,
     144            'public'       => false,
     145            'show_ui'      => true,
     146            '_builtin'     => true,
     147        )
     148    );
     149
     150    register_taxonomy(
     151        'post_format', 'post', array(
     152            'public'            => true,
     153            'hierarchical'      => false,
     154            'labels'            => array(
     155                'name'          => _x( 'Format', 'post format' ),
     156                'singular_name' => _x( 'Format', 'post format' ),
     157            ),
     158            'query_var'         => true,
     159            'rewrite'           => $rewrite['post_format'],
     160            'show_ui'           => false,
     161            '_builtin'          => true,
     162            'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
     163        )
     164    );
    151165}
    152166
     
    170184    global $wp_taxonomies;
    171185
    172     $field = ('names' == $output) ? 'name' : false;
    173 
    174     return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
     186    $field = ( 'names' == $output ) ? 'name' : false;
     187
     188    return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
    175189}
    176190
     
    199213    global $wp_taxonomies;
    200214
    201     if ( is_object($object) ) {
    202         if ( $object->post_type == 'attachment' )
     215    if ( is_object( $object ) ) {
     216        if ( $object->post_type == 'attachment' ) {
    203217            return get_attachment_taxonomies( $object, $output );
     218        }
    204219        $object = $object->post_type;
    205220    }
     
    209224    $taxonomies = array();
    210225    foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
    211         if ( array_intersect($object, (array) $tax_obj->object_type) ) {
    212             if ( 'names' == $output )
     226        if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
     227            if ( 'names' == $output ) {
    213228                $taxonomies[] = $tax_name;
    214             else
     229            } else {
    215230                $taxonomies[ $tax_name ] = $tax_obj;
     231            }
    216232        }
    217233    }
     
    236252    global $wp_taxonomies;
    237253
    238     if ( ! taxonomy_exists( $taxonomy ) )
     254    if ( ! taxonomy_exists( $taxonomy ) ) {
    239255        return false;
    240 
    241     return $wp_taxonomies[$taxonomy];
     256    }
     257
     258    return $wp_taxonomies[ $taxonomy ];
    242259}
    243260
     
    257274    global $wp_taxonomies;
    258275
    259     return isset( $wp_taxonomies[$taxonomy] );
     276    return isset( $wp_taxonomies[ $taxonomy ] );
    260277}
    261278
     
    273290 * @return bool Whether the taxonomy is hierarchical.
    274291 */
    275 function is_taxonomy_hierarchical($taxonomy) {
    276     if ( ! taxonomy_exists($taxonomy) )
     292function is_taxonomy_hierarchical( $taxonomy ) {
     293    if ( ! taxonomy_exists( $taxonomy ) ) {
    277294        return false;
    278 
    279     $taxonomy = get_taxonomy($taxonomy);
     295    }
     296
     297    $taxonomy = get_taxonomy( $taxonomy );
    280298    return $taxonomy->hierarchical;
    281299}
     
    377395    global $wp_taxonomies;
    378396
    379     if ( ! is_array( $wp_taxonomies ) )
     397    if ( ! is_array( $wp_taxonomies ) ) {
    380398        $wp_taxonomies = array();
     399    }
    381400
    382401    $args = wp_parse_args( $args );
     
    393412
    394413    $taxonomy_object->add_hooks();
    395 
    396414
    397415    /**
     
    500518    $tax->labels = (array) $tax->labels;
    501519
    502     if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
     520    if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
    503521        $tax->labels['separate_items_with_commas'] = $tax->helps;
    504 
    505     if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) )
     522    }
     523
     524    if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
    506525        $tax->labels['not_found'] = $tax->no_tagcloud;
     526    }
    507527
    508528    $nohier_vs_hier_defaults = array(
    509         'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
    510         'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
    511         'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
    512         'popular_items' => array( __( 'Popular Tags' ), null ),
    513         'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
    514         'parent_item' => array( null, __( 'Parent Category' ) ),
    515         'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
    516         'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
    517         'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
    518         'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
    519         'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
    520         'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
     529        'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
     530        'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
     531        'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
     532        'popular_items'              => array( __( 'Popular Tags' ), null ),
     533        'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
     534        'parent_item'                => array( null, __( 'Parent Category' ) ),
     535        'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
     536        'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
     537        'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
     538        'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
     539        'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
     540        'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
    521541        'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
    522         'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
    523         'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
    524         'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
    525         'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ),
    526         'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
    527         'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ),
     542        'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
     543        'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
     544        'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
     545        'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
     546        'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
     547        'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
    528548        /* translators: Tab heading when selecting from the most used terms */
    529         'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
    530         'back_to_items' => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ),
     549        'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
     550        'back_to_items'              => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ),
    531551    );
    532552    $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
     
    568588 * @return bool True if successful, false if not.
    569589 */
    570 function register_taxonomy_for_object_type( $taxonomy, $object_type) {
     590function register_taxonomy_for_object_type( $taxonomy, $object_type ) {
    571591    global $wp_taxonomies;
    572592
    573     if ( !isset($wp_taxonomies[$taxonomy]) )
     593    if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
    574594        return false;
    575 
    576     if ( ! get_post_type_object($object_type) )
     595    }
     596
     597    if ( ! get_post_type_object( $object_type ) ) {
    577598        return false;
    578 
    579     if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
    580         $wp_taxonomies[$taxonomy]->object_type[] = $object_type;
     599    }
     600
     601    if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type ) ) {
     602        $wp_taxonomies[ $taxonomy ]->object_type[] = $object_type;
     603    }
    581604
    582605    // Filter out empties.
     
    600623    global $wp_taxonomies;
    601624
    602     if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
     625    if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
    603626        return false;
    604 
    605     if ( ! get_post_type_object( $object_type ) )
     627    }
     628
     629    if ( ! get_post_type_object( $object_type ) ) {
    606630        return false;
     631    }
    607632
    608633    $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
    609     if ( false === $key )
     634    if ( false === $key ) {
    610635        return false;
     636    }
    611637
    612638    unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
     
    641667 * @param array|string $args       Change the order of the object_ids, either ASC or DESC.
    642668 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success.
    643  *  the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
     669 *  the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
    644670 */
    645671function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
     
    659685
    660686    $defaults = array( 'order' => 'ASC' );
    661     $args = wp_parse_args( $args, $defaults );
     687    $args     = wp_parse_args( $args, $defaults );
    662688
    663689    $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
    664690
    665     $term_ids = array_map('intval', $term_ids );
     691    $term_ids = array_map( 'intval', $term_ids );
    666692
    667693    $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
    668     $term_ids = "'" . implode( "', '", $term_ids ) . "'";
     694    $term_ids   = "'" . implode( "', '", $term_ids ) . "'";
    669695
    670696    $sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order";
    671697
    672698    $last_changed = wp_cache_get_last_changed( 'terms' );
    673     $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
    674     $cache = wp_cache_get( $cache_key, 'terms' );
     699    $cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
     700    $cache        = wp_cache_get( $cache_key, 'terms' );
    675701    if ( false === $cache ) {
    676702        $object_ids = $wpdb->get_col( $sql );
     
    680706    }
    681707
    682     if ( ! $object_ids ){
     708    if ( ! $object_ids ) {
    683709        return array();
    684710    }
     
    888914
    889915    switch ( $field ) {
    890         case 'slug' :
     916        case 'slug':
    891917            $args['slug'] = $value;
    892918            break;
    893         case 'name' :
     919        case 'name':
    894920            $args['name'] = $value;
    895921            break;
    896         case 'term_taxonomy_id' :
     922        case 'term_taxonomy_id':
    897923            $args['term_taxonomy_id'] = $value;
    898             unset( $args[ 'taxonomy' ] );
     924            unset( $args['taxonomy'] );
    899925            break;
    900         default :
     926        default:
    901927            return false;
    902928    }
     
    938964    $term_id = intval( $term_id );
    939965
    940     $terms = _get_term_hierarchy($taxonomy);
    941 
    942     if ( ! isset($terms[$term_id]) )
     966    $terms = _get_term_hierarchy( $taxonomy );
     967
     968    if ( ! isset( $terms[ $term_id ] ) ) {
    943969        return array();
    944 
    945     $children = $terms[$term_id];
    946 
    947     foreach ( (array) $terms[$term_id] as $child ) {
     970    }
     971
     972    $children = $terms[ $term_id ];
     973
     974    foreach ( (array) $terms[ $term_id ] as $child ) {
    948975        if ( $term_id == $child ) {
    949976            continue;
    950977        }
    951978
    952         if ( isset($terms[$child]) )
    953             $children = array_merge($children, get_term_children($child, $taxonomy));
     979        if ( isset( $terms[ $child ] ) ) {
     980            $children = array_merge( $children, get_term_children( $child, $taxonomy ) );
     981        }
    954982    }
    955983
     
    9751003function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
    9761004    $term = get_term( $term, $taxonomy );
    977     if ( is_wp_error($term) )
     1005    if ( is_wp_error( $term ) ) {
    9781006        return $term;
    979 
    980     if ( !is_object($term) )
     1007    }
     1008
     1009    if ( ! is_object( $term ) ) {
    9811010        return '';
    982 
    983     if ( !isset($term->$field) )
     1011    }
     1012
     1013    if ( ! isset( $term->$field ) ) {
    9841014        return '';
     1015    }
    9851016
    9861017    return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
     
    10021033    $term = get_term( $id, $taxonomy );
    10031034
    1004     if ( is_wp_error($term) )
     1035    if ( is_wp_error( $term ) ) {
    10051036        return $term;
    1006 
    1007     if ( !is_object($term) )
     1037    }
     1038
     1039    if ( ! is_object( $term ) ) {
    10081040        return '';
    1009 
    1010     return sanitize_term($term, $taxonomy, 'edit');
     1041    }
     1042
     1043    return sanitize_term( $term, $taxonomy, 'edit' );
    10111044}
    10121045
     
    10741107     * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
    10751108     */
    1076     $_args = wp_parse_args( $args );
     1109    $_args          = wp_parse_args( $args );
    10771110    $key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
    10781111    $do_legacy_args = $deprecated || empty( $key_intersect );
    10791112
    10801113    if ( $do_legacy_args ) {
    1081         $taxonomies = (array) $args;
    1082         $args = wp_parse_args( $deprecated, $defaults );
     1114        $taxonomies       = (array) $args;
     1115        $args             = wp_parse_args( $deprecated, $defaults );
    10831116        $args['taxonomy'] = $taxonomies;
    10841117    } else {
     
    11461179
    11471180    if ( wp_term_is_shared( $term_id ) ) {
    1148         return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
     1181        return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
    11491182    }
    11501183
     
    12281261
    12291262    if ( wp_term_is_shared( $term_id ) ) {
    1230         return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
     1263        return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
    12311264    }
    12321265
     
    13021335    global $wpdb;
    13031336
    1304     $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
     1337    $select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
    13051338    $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 ";
    13061339
    1307     if ( is_int($term) ) {
    1308         if ( 0 == $term )
     1340    if ( is_int( $term ) ) {
     1341        if ( 0 == $term ) {
    13091342            return 0;
     1343        }
    13101344        $where = 't.term_id = %d';
    1311         if ( !empty($taxonomy) )
    1312             return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
    1313         else
     1345        if ( ! empty( $taxonomy ) ) {
     1346            return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
     1347        } else {
    13141348            return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
     1349        }
    13151350    }
    13161351
     
    13181353    $slug = sanitize_title( $term );
    13191354
    1320     $where = 't.slug = %s';
    1321     $else_where = 't.name = %s';
    1322     $where_fields = array($slug);
    1323     $else_where_fields = array($term);
    1324     $orderby = 'ORDER BY t.term_id ASC';
    1325     $limit = 'LIMIT 1';
    1326     if ( !empty($taxonomy) ) {
     1355    $where             = 't.slug = %s';
     1356    $else_where        = 't.name = %s';
     1357    $where_fields      = array( $slug );
     1358    $else_where_fields = array( $term );
     1359    $orderby           = 'ORDER BY t.term_id ASC';
     1360    $limit             = 'LIMIT 1';
     1361    if ( ! empty( $taxonomy ) ) {
    13271362        if ( is_numeric( $parent ) ) {
    1328             $parent = (int) $parent;
    1329             $where_fields[] = $parent;
     1363            $parent              = (int) $parent;
     1364            $where_fields[]      = $parent;
    13301365            $else_where_fields[] = $parent;
    1331             $where .= ' AND tt.parent = %d';
    1332             $else_where .= ' AND tt.parent = %d';
    1333         }
    1334 
    1335         $where_fields[] = $taxonomy;
     1366            $where              .= ' AND tt.parent = %d';
     1367            $else_where         .= ' AND tt.parent = %d';
     1368        }
     1369
     1370        $where_fields[]      = $taxonomy;
    13361371        $else_where_fields[] = $taxonomy;
    13371372
    1338         if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields), ARRAY_A) )
     1373        if ( $result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields ), ARRAY_A ) ) {
    13391374            return $result;
    1340 
    1341         return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields), ARRAY_A);
    1342     }
    1343 
    1344     if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
     1375        }
     1376
     1377        return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields ), ARRAY_A );
     1378    }
     1379
     1380    if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) {
    13451381        return $result;
    1346 
    1347     return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
     1382    }
     1383
     1384    return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
    13481385}
    13491386
     
    13611398 */
    13621399function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
    1363     if ( ! isset( $term1->term_id ) )
     1400    if ( ! isset( $term1->term_id ) ) {
    13641401        $term1 = get_term( $term1, $taxonomy );
    1365     if ( ! isset( $term2->parent ) )
     1402    }
     1403    if ( ! isset( $term2->parent ) ) {
    13661404        $term2 = get_term( $term2, $taxonomy );
    1367 
    1368     if ( empty( $term1->term_id ) || empty( $term2->parent ) )
     1405    }
     1406
     1407    if ( empty( $term1->term_id ) || empty( $term2->parent ) ) {
    13691408        return false;
    1370     if ( $term2->parent == $term1->term_id )
     1409    }
     1410    if ( $term2->parent == $term1->term_id ) {
    13711411        return true;
     1412    }
    13721413
    13731414    return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
     
    13911432 * @return array|object Term with all fields sanitized.
    13921433 */
    1393 function sanitize_term($term, $taxonomy, $context = 'display') {
     1434function sanitize_term( $term, $taxonomy, $context = 'display' ) {
    13941435    $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
    13951436
    13961437    $do_object = is_object( $term );
    13971438
    1398     $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
     1439    $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );
    13991440
    14001441    foreach ( (array) $fields as $field ) {
    14011442        if ( $do_object ) {
    1402             if ( isset($term->$field) )
    1403                 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
     1443            if ( isset( $term->$field ) ) {
     1444                $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
     1445            }
    14041446        } else {
    1405             if ( isset($term[$field]) )
    1406                 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
    1407         }
    1408     }
    1409 
    1410     if ( $do_object )
     1447            if ( isset( $term[ $field ] ) ) {
     1448                $term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context );
     1449            }
     1450        }
     1451    }
     1452
     1453    if ( $do_object ) {
    14111454        $term->filter = $context;
    1412     else
     1455    } else {
    14131456        $term['filter'] = $context;
     1457    }
    14141458
    14151459    return $term;
     
    14391483 * @return mixed Sanitized field.
    14401484 */
    1441 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
     1485function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
    14421486    $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
    14431487    if ( in_array( $field, $int_fields ) ) {
    14441488        $value = (int) $value;
    1445         if ( $value < 0 )
     1489        if ( $value < 0 ) {
    14461490            $value = 0;
    1447     }
    1448 
    1449     if ( 'raw' == $context )
     1491        }
     1492    }
     1493
     1494    if ( 'raw' == $context ) {
    14501495        return $value;
     1496    }
    14511497
    14521498    if ( 'edit' == $context ) {
     
    14781524        $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );
    14791525
    1480         if ( 'description' == $field )
    1481             $value = esc_html($value); // textarea_escaped
    1482         else
    1483             $value = esc_attr($value);
     1526        if ( 'description' == $field ) {
     1527            $value = esc_html( $value ); // textarea_escaped
     1528        } else {
     1529            $value = esc_attr( $value );
     1530        }
    14841531    } elseif ( 'db' == $context ) {
    14851532
     
    15211568            $value = apply_filters( 'pre_category_nicename', $value );
    15221569        }
    1523 
    15241570    } elseif ( 'rss' == $context ) {
    15251571
     
    15801626
    15811627    if ( 'attribute' == $context ) {
    1582         $value = esc_attr($value);
     1628        $value = esc_attr( $value );
    15831629    } elseif ( 'js' == $context ) {
    1584         $value = esc_js($value);
     1630        $value = esc_js( $value );
    15851631    }
    15861632    return $value;
     
    16001646 */
    16011647function wp_count_terms( $taxonomy, $args = array() ) {
    1602     $defaults = array('hide_empty' => false);
    1603     $args = wp_parse_args($args, $defaults);
     1648    $defaults = array( 'hide_empty' => false );
     1649    $args     = wp_parse_args( $args, $defaults );
    16041650
    16051651    // backward compatibility
    1606     if ( isset($args['ignore_empty']) ) {
     1652    if ( isset( $args['ignore_empty'] ) ) {
    16071653        $args['hide_empty'] = $args['ignore_empty'];
    1608         unset($args['ignore_empty']);
     1654        unset( $args['ignore_empty'] );
    16091655    }
    16101656
    16111657    $args['fields'] = 'count';
    16121658
    1613     return get_terms($taxonomy, $args);
     1659    return get_terms( $taxonomy, $args );
    16141660}
    16151661
     
    16291675    $object_id = (int) $object_id;
    16301676
    1631     if ( !is_array($taxonomies) )
    1632         $taxonomies = array($taxonomies);
     1677    if ( ! is_array( $taxonomies ) ) {
     1678        $taxonomies = array( $taxonomies );
     1679    }
    16331680
    16341681    foreach ( (array) $taxonomies as $taxonomy ) {
     
    16711718    $term = (int) $term;
    16721719
    1673     if ( ! $ids = term_exists($term, $taxonomy) )
     1720    if ( ! $ids = term_exists( $term, $taxonomy ) ) {
    16741721        return false;
    1675     if ( is_wp_error( $ids ) )
     1722    }
     1723    if ( is_wp_error( $ids ) ) {
    16761724        return $ids;
     1725    }
    16771726
    16781727    $tt_id = $ids['term_taxonomy_id'];
     
    16821731    if ( 'category' == $taxonomy ) {
    16831732        $defaults['default'] = get_option( 'default_category' );
    1684         if ( $defaults['default'] == $term )
     1733        if ( $defaults['default'] == $term ) {
    16851734            return 0; // Don't delete the default category
    1686     }
    1687 
    1688     $args = wp_parse_args($args, $defaults);
     1735        }
     1736    }
     1737
     1738    $args = wp_parse_args( $args, $defaults );
    16891739
    16901740    if ( isset( $args['default'] ) ) {
     
    17101760
    17111761    // Update children to point to new parent
    1712     if ( is_taxonomy_hierarchical($taxonomy) ) {
    1713         $term_obj = get_term($term, $taxonomy);
    1714         if ( is_wp_error( $term_obj ) )
     1762    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     1763        $term_obj = get_term( $term, $taxonomy );
     1764        if ( is_wp_error( $term_obj ) ) {
    17151765            return $term_obj;
     1766        }
    17161767        $parent = $term_obj->parent;
    17171768
    1718         $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
     1769        $edit_ids    = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id );
    17191770        $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
    17201771
     
    17281779        do_action( 'edit_term_taxonomies', $edit_tt_ids );
    17291780
    1730         $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
     1781        $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
    17311782
    17321783        // Clean the cache for all child terms.
     
    17501801
    17511802    foreach ( $object_ids as $object_id ) {
    1752         $terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) );
    1753         if ( 1 == count($terms) && isset($default) ) {
    1754             $terms = array($default);
     1803        $terms = wp_get_object_terms(
     1804            $object_id, $taxonomy, array(
     1805                'fields'  => 'ids',
     1806                'orderby' => 'none',
     1807            )
     1808        );
     1809        if ( 1 == count( $terms ) && isset( $default ) ) {
     1810            $terms = array( $default );
    17551811        } else {
    1756             $terms = array_diff($terms, array($term));
    1757             if (isset($default) && isset($force_default) && $force_default)
    1758                 $terms = array_merge($terms, array($default));
    1759         }
    1760         $terms = array_map('intval', $terms);
     1812            $terms = array_diff( $terms, array( $term ) );
     1813            if ( isset( $default ) && isset( $force_default ) && $force_default ) {
     1814                $terms = array_merge( $terms, array( $default ) );
     1815            }
     1816        }
     1817        $terms = array_map( 'intval', $terms );
    17611818        wp_set_object_terms( $object_id, $terms, $taxonomy );
    17621819    }
     
    17641821    // Clean the relationship caches for all object types using this term.
    17651822    $tax_object = get_taxonomy( $taxonomy );
    1766     foreach ( $tax_object->object_type as $object_type )
     1823    foreach ( $tax_object->object_type as $object_type ) {
    17671824        clean_object_term_cache( $object_ids, $object_type );
     1825    }
    17681826
    17691827    $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
     
    17921850
    17931851    // Delete the term if no taxonomies use it.
    1794     if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
     1852    if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) {
    17951853        $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
    1796 
    1797     clean_term_cache($term, $taxonomy);
     1854    }
     1855
     1856    clean_term_cache( $term, $taxonomy );
    17981857
    17991858    /**
     
    18391898 * @param int $cat_ID Category term ID.
    18401899 * @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist;
    1841  *  Zero on attempted deletion of default Category; WP_Error object is also a possibility.
     1900 *  Zero on attempted deletion of default Category; WP_Error object is also a possibility.
    18421901 */
    18431902function wp_delete_category( $cat_ID ) {
     
    18611920 *                        WP_Error if any of the $taxonomies don't exist.
    18621921 */
    1863 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
    1864     if ( empty( $object_ids ) || empty( $taxonomies ) )
     1922function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
     1923    if ( empty( $object_ids ) || empty( $taxonomies ) ) {
    18651924        return array();
    1866 
    1867     if ( !is_array($taxonomies) )
    1868         $taxonomies = array($taxonomies);
     1925    }
     1926
     1927    if ( ! is_array( $taxonomies ) ) {
     1928        $taxonomies = array( $taxonomies );
     1929    }
    18691930
    18701931    foreach ( $taxonomies as $taxonomy ) {
    1871         if ( ! taxonomy_exists($taxonomy) )
     1932        if ( ! taxonomy_exists( $taxonomy ) ) {
    18721933            return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
    1873     }
    1874 
    1875     if ( !is_array($object_ids) )
    1876         $object_ids = array($object_ids);
    1877     $object_ids = array_map('intval', $object_ids);
     1934        }
     1935    }
     1936
     1937    if ( ! is_array( $object_ids ) ) {
     1938        $object_ids = array( $object_ids );
     1939    }
     1940    $object_ids = array_map( 'intval', $object_ids );
    18781941
    18791942    $args = wp_parse_args( $args );
     
    19111974    }
    19121975
    1913     $args['taxonomy'] = $taxonomies;
     1976    $args['taxonomy']   = $taxonomies;
    19141977    $args['object_ids'] = $object_ids;
    19151978
     
    20052068    global $wpdb;
    20062069
    2007     if ( ! taxonomy_exists($taxonomy) ) {
     2070    if ( ! taxonomy_exists( $taxonomy ) ) {
    20082071        return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
    20092072    }
     
    20262089        return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
    20272090    }
    2028     $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    2029     $args = wp_parse_args( $args, $defaults );
     2091    $defaults = array(
     2092        'alias_of'    => '',
     2093        'description' => '',
     2094        'parent'      => 0,
     2095        'slug'        => '',
     2096    );
     2097    $args     = wp_parse_args( $args, $defaults );
    20302098
    20312099    if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
     
    20332101    }
    20342102
    2035     $args['name'] = $term;
     2103    $args['name']     = $term;
    20362104    $args['taxonomy'] = $taxonomy;
    20372105
     
    20392107    $args['description'] = (string) $args['description'];
    20402108
    2041     $args = sanitize_term($args, $taxonomy, 'db');
     2109    $args = sanitize_term( $args, $taxonomy, 'db' );
    20422110
    20432111    // expected_slashed ($name)
    2044     $name = wp_unslash( $args['name'] );
     2112    $name        = wp_unslash( $args['name'] );
    20452113    $description = wp_unslash( $args['description'] );
    2046     $parent = (int) $args['parent'];
     2114    $parent      = (int) $args['parent'];
    20472115
    20482116    $slug_provided = ! empty( $args['slug'] );
     
    20642132             * and add the alias to it.
    20652133             */
    2066             $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    2067 
    2068             wp_update_term( $alias->term_id, $taxonomy, array(
    2069                 'term_group' => $term_group,
    2070             ) );
     2134            $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
     2135
     2136            wp_update_term(
     2137                $alias->term_id, $taxonomy, array(
     2138                    'term_group' => $term_group,
     2139                )
     2140            );
    20712141        }
    20722142    }
     
    20762146     * unless a unique slug has been explicitly provided.
    20772147     */
    2078     $name_matches = get_terms( $taxonomy, array(
    2079         'name' => $name,
    2080         'hide_empty' => false,
    2081         'parent' => $args['parent'],
    2082     ) );
     2148    $name_matches = get_terms(
     2149        $taxonomy, array(
     2150            'name'       => $name,
     2151            'hide_empty' => false,
     2152            'parent'     => $args['parent'],
     2153        )
     2154    );
    20832155
    20842156    /*
     
    21002172        if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
    21012173            if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    2102                 $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
     2174                $siblings = get_terms(
     2175                    $taxonomy, array(
     2176                        'get'    => 'all',
     2177                        'parent' => $parent,
     2178                    )
     2179                );
    21032180
    21042181                $existing_term = null;
     
    21402217
    21412218    // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
    2142     if ( empty($slug) ) {
    2143         $slug = sanitize_title($slug, $term_id);
     2219    if ( empty( $slug ) ) {
     2220        $slug = sanitize_title( $slug, $term_id );
    21442221
    21452222        /** This action is documented in wp-includes/taxonomy.php */
     
    21532230    $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 ) );
    21542231
    2155     if ( !empty($tt_id) ) {
    2156         return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
    2157     }
    2158     $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
     2232    if ( ! empty( $tt_id ) ) {
     2233        return array(
     2234            'term_id'          => $term_id,
     2235            'term_taxonomy_id' => $tt_id,
     2236        );
     2237    }
     2238    $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) );
    21592239    $tt_id = (int) $wpdb->insert_id;
    21602240
     
    21742254
    21752255        clean_term_cache( $term_id, $taxonomy );
    2176         return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
     2256        return array(
     2257            'term_id'          => $term_id,
     2258            'term_taxonomy_id' => $tt_id,
     2259        );
    21772260    }
    21782261
     
    21862269     * @param string $taxonomy Taxonomy slug.
    21872270     */
    2188     do_action( "create_term", $term_id, $tt_id, $taxonomy );
     2271    do_action( 'create_term', $term_id, $tt_id, $taxonomy );
    21892272
    21902273    /**
     
    22112294    $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
    22122295
    2213     clean_term_cache($term_id, $taxonomy);
     2296    clean_term_cache( $term_id, $taxonomy );
    22142297
    22152298    /**
     
    22372320    do_action( "created_{$taxonomy}", $term_id, $tt_id );
    22382321
    2239     return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     2322    return array(
     2323        'term_id'          => $term_id,
     2324        'term_taxonomy_id' => $tt_id,
     2325    );
    22402326}
    22412327
     
    22722358    }
    22732359
    2274     if ( !is_array($terms) )
    2275         $terms = array($terms);
    2276 
    2277     if ( ! $append )
    2278         $old_tt_ids =  wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));
    2279     else
     2360    if ( ! is_array( $terms ) ) {
     2361        $terms = array( $terms );
     2362    }
     2363
     2364    if ( ! $append ) {
     2365        $old_tt_ids = wp_get_object_terms(
     2366            $object_id, $taxonomy, array(
     2367                'fields'  => 'tt_ids',
     2368                'orderby' => 'none',
     2369            )
     2370        );
     2371    } else {
    22802372        $old_tt_ids = array();
    2281 
    2282     $tt_ids = array();
    2283     $term_ids = array();
     2373    }
     2374
     2375    $tt_ids     = array();
     2376    $term_ids   = array();
    22842377    $new_tt_ids = array();
    22852378
    2286     foreach ( (array) $terms as $term) {
    2287         if ( !strlen(trim($term)) )
     2379    foreach ( (array) $terms as $term ) {
     2380        if ( ! strlen( trim( $term ) ) ) {
    22882381            continue;
    2289 
    2290         if ( !$term_info = term_exists($term, $taxonomy) ) {
     2382        }
     2383
     2384        if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
    22912385            // Skip if a non-existent term ID is passed.
    2292             if ( is_int($term) )
     2386            if ( is_int( $term ) ) {
    22932387                continue;
    2294             $term_info = wp_insert_term($term, $taxonomy);
    2295         }
    2296         if ( is_wp_error($term_info) )
     2388            }
     2389            $term_info = wp_insert_term( $term, $taxonomy );
     2390        }
     2391        if ( is_wp_error( $term_info ) ) {
    22972392            return $term_info;
     2393        }
    22982394        $term_ids[] = $term_info['term_id'];
    2299         $tt_id = $term_info['term_taxonomy_id'];
    2300         $tt_ids[] = $tt_id;
    2301 
    2302         if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
     2395        $tt_id      = $term_info['term_taxonomy_id'];
     2396        $tt_ids[]   = $tt_id;
     2397
     2398        if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) {
    23032399            continue;
     2400        }
    23042401
    23052402        /**
     
    23142411         */
    23152412        do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy );
    2316         $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
     2413        $wpdb->insert(
     2414            $wpdb->term_relationships, array(
     2415                'object_id'        => $object_id,
     2416                'term_taxonomy_id' => $tt_id,
     2417            )
     2418        );
    23172419
    23182420        /**
     
    23302432    }
    23312433
    2332     if ( $new_tt_ids )
     2434    if ( $new_tt_ids ) {
    23332435        wp_update_term_count( $new_tt_ids, $taxonomy );
     2436    }
    23342437
    23352438    if ( ! $append ) {
     
    23382441        if ( $delete_tt_ids ) {
    23392442            $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
    2340             $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
    2341             $delete_term_ids = array_map( 'intval', $delete_term_ids );
     2443            $delete_term_ids  = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
     2444            $delete_term_ids  = array_map( 'intval', $delete_term_ids );
    23422445
    23432446            $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
     
    23482451    }
    23492452
    2350     $t = get_taxonomy($taxonomy);
    2351     if ( ! $append && isset($t->sort) && $t->sort ) {
    2352         $values = array();
    2353         $term_order = 0;
    2354         $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
    2355         foreach ( $tt_ids as $tt_id )
    2356             if ( in_array($tt_id, $final_tt_ids) )
    2357                 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
    2358         if ( $values )
    2359             if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" ) )
     2453    $t = get_taxonomy( $taxonomy );
     2454    if ( ! $append && isset( $t->sort ) && $t->sort ) {
     2455        $values       = array();
     2456        $term_order   = 0;
     2457        $final_tt_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'tt_ids' ) );
     2458        foreach ( $tt_ids as $tt_id ) {
     2459            if ( in_array( $tt_id, $final_tt_ids ) ) {
     2460                $values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order );
     2461            }
     2462        }
     2463        if ( $values ) {
     2464            if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) {
    23602465                return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error );
     2466            }
     2467        }
    23612468    }
    23622469
     
    25052612    global $wpdb;
    25062613
    2507     $needs_suffix = true;
     2614    $needs_suffix  = true;
    25082615    $original_slug = $slug;
    25092616
     
    25202627    if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
    25212628        $the_parent = $term->parent;
    2522         while ( ! empty($the_parent) ) {
    2523             $parent_term = get_term($the_parent, $term->taxonomy);
    2524             if ( is_wp_error($parent_term) || empty($parent_term) )
     2629        while ( ! empty( $the_parent ) ) {
     2630            $parent_term = get_term( $the_parent, $term->taxonomy );
     2631            if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
    25252632                break;
     2633            }
    25262634            $parent_suffix .= '-' . $parent_term->slug;
    25272635            if ( ! term_exists( $slug . $parent_suffix ) ) {
     
    25292637            }
    25302638
    2531             if ( empty($parent_term->parent) )
     2639            if ( empty( $parent_term->parent ) ) {
    25322640                break;
     2641            }
    25332642            $the_parent = $parent_term->parent;
    25342643        }
     
    25502659            $slug .= $parent_suffix;
    25512660        } else {
    2552             if ( ! empty( $term->term_id ) )
     2661            if ( ! empty( $term->term_id ) ) {
    25532662                $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
    2554             else
     2663            } else {
    25552664                $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
     2665            }
    25562666
    25572667            if ( $wpdb->get_var( $query ) ) {
     
    26352745
    26362746    // Merge old and new args with new args overwriting old ones.
    2637     $args = array_merge($term, $args);
    2638 
    2639     $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    2640     $args = wp_parse_args($args, $defaults);
    2641     $args = sanitize_term($args, $taxonomy, 'db');
     2747    $args = array_merge( $term, $args );
     2748
     2749    $defaults    = array(
     2750        'alias_of'    => '',
     2751        'description' => '',
     2752        'parent'      => 0,
     2753        'slug'        => '',
     2754    );
     2755    $args        = wp_parse_args( $args, $defaults );
     2756    $args        = sanitize_term( $args, $taxonomy, 'db' );
    26422757    $parsed_args = $args;
    26432758
    26442759    // expected_slashed ($name)
    2645     $name = wp_unslash( $args['name'] );
     2760    $name        = wp_unslash( $args['name'] );
    26462761    $description = wp_unslash( $args['description'] );
    26472762
    2648     $parsed_args['name'] = $name;
     2763    $parsed_args['name']        = $name;
    26492764    $parsed_args['description'] = $description;
    26502765
     
    26602775    if ( empty( $args['slug'] ) ) {
    26612776        $empty_slug = true;
    2662         $slug = sanitize_title($name);
     2777        $slug       = sanitize_title( $name );
    26632778    } else {
    26642779        $slug = $args['slug'];
     
    26782793             * and add the alias to it.
    26792794             */
    2680             $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    2681 
    2682             wp_update_term( $alias->term_id, $taxonomy, array(
    2683                 'term_group' => $term_group,
    2684             ) );
     2795            $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
     2796
     2797            wp_update_term(
     2798                $alias->term_id, $taxonomy, array(
     2799                    'term_group' => $term_group,
     2800                )
     2801            );
    26852802        }
    26862803
     
    27082825        // If an empty slug was passed or the parent changed, reset the slug to something unique.
    27092826        // Otherwise, bail.
    2710         if ( $empty_slug || ( $parent != $term['parent']) ) {
    2711             $slug = wp_unique_term_slug($slug, (object) $args);
     2827        if ( $empty_slug || ( $parent != $term['parent'] ) ) {
     2828            $slug = wp_unique_term_slug( $slug, (object) $args );
    27122829        } else {
    27132830            /* translators: 1: Taxonomy term slug */
     
    27162833    }
    27172834
    2718     $tt_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
     2835    $tt_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );
    27192836
    27202837    // Check whether this is a shared term that needs splitting.
     
    27492866
    27502867    $wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) );
    2751     if ( empty($slug) ) {
    2752         $slug = sanitize_title($name, $term_id);
     2868    if ( empty( $slug ) ) {
     2869        $slug = sanitize_title( $name, $term_id );
    27532870        $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
    27542871    }
     
    27952912     * @param string $taxonomy Taxonomy slug.
    27962913     */
    2797     do_action( "edit_term", $term_id, $tt_id, $taxonomy );
     2914    do_action( 'edit_term', $term_id, $tt_id, $taxonomy );
    27982915
    27992916    /**
     
    28132930    $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
    28142931
    2815     clean_term_cache($term_id, $taxonomy);
     2932    clean_term_cache( $term_id, $taxonomy );
    28162933
    28172934    /**
     
    28242941     * @param string $taxonomy Taxonomy slug.
    28252942     */
    2826     do_action( "edited_term", $term_id, $tt_id, $taxonomy );
     2943    do_action( 'edited_term', $term_id, $tt_id, $taxonomy );
    28272944
    28282945    /**
     
    28392956    do_action( "edited_{$taxonomy}", $term_id, $tt_id );
    28402957
    2841     return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     2958    return array(
     2959        'term_id'          => $term_id,
     2960        'term_taxonomy_id' => $tt_id,
     2961    );
    28422962}
    28432963
     
    28522972 * @return bool Whether term counting is enabled or disabled.
    28532973 */
    2854 function wp_defer_term_counting($defer=null) {
     2974function wp_defer_term_counting( $defer = null ) {
    28552975    static $_defer = false;
    28562976
    2857     if ( is_bool($defer) ) {
     2977    if ( is_bool( $defer ) ) {
    28582978        $_defer = $defer;
    28592979        // flush any deferred counts
    2860         if ( !$defer )
     2980        if ( ! $defer ) {
    28612981            wp_update_term_count( null, null, true );
     2982        }
    28622983    }
    28632984
     
    28873008
    28883009    if ( $do_deferred ) {
    2889         foreach ( (array) array_keys($_deferred) as $tax ) {
    2890             wp_update_term_count_now( $_deferred[$tax], $tax );
    2891             unset( $_deferred[$tax] );
    2892         }
    2893     }
    2894 
    2895     if ( empty($terms) )
     3010        foreach ( (array) array_keys( $_deferred ) as $tax ) {
     3011            wp_update_term_count_now( $_deferred[ $tax ], $tax );
     3012            unset( $_deferred[ $tax ] );
     3013        }
     3014    }
     3015
     3016    if ( empty( $terms ) ) {
    28963017        return false;
    2897 
    2898     if ( !is_array($terms) )
    2899         $terms = array($terms);
     3018    }
     3019
     3020    if ( ! is_array( $terms ) ) {
     3021        $terms = array( $terms );
     3022    }
    29003023
    29013024    if ( wp_defer_term_counting() ) {
    2902         if ( !isset($_deferred[$taxonomy]) )
    2903             $_deferred[$taxonomy] = array();
    2904         $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
     3025        if ( ! isset( $_deferred[ $taxonomy ] ) ) {
     3026            $_deferred[ $taxonomy ] = array();
     3027        }
     3028        $_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
    29053029        return true;
    29063030    }
     
    29193043 */
    29203044function wp_update_term_count_now( $terms, $taxonomy ) {
    2921     $terms = array_map('intval', $terms);
    2922 
    2923     $taxonomy = get_taxonomy($taxonomy);
    2924     if ( !empty($taxonomy->update_count_callback) ) {
    2925         call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
     3045    $terms = array_map( 'intval', $terms );
     3046
     3047    $taxonomy = get_taxonomy( $taxonomy );
     3048    if ( ! empty( $taxonomy->update_count_callback ) ) {
     3049        call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
    29263050    } else {
    29273051        $object_types = (array) $taxonomy->object_type;
    29283052        foreach ( $object_types as &$object_type ) {
    2929             if ( 0 === strpos( $object_type, 'attachment:' ) )
     3053            if ( 0 === strpos( $object_type, 'attachment:' ) ) {
    29303054                list( $object_type ) = explode( ':', $object_type );
     3055            }
    29313056        }
    29323057
     
    29403065    }
    29413066
    2942     clean_term_cache($terms, '', false);
     3067    clean_term_cache( $terms, '', false );
    29433068
    29443069    return true;
     
    29653090 * @param array|string $object_type The taxonomy object type.
    29663091 */
    2967 function clean_object_term_cache($object_ids, $object_type) {
     3092function clean_object_term_cache( $object_ids, $object_type ) {
    29683093    global $_wp_suspend_cache_invalidation;
    29693094
     
    29723097    }
    29733098
    2974     if ( !is_array($object_ids) )
    2975         $object_ids = array($object_ids);
     3099    if ( ! is_array( $object_ids ) ) {
     3100        $object_ids = array( $object_ids );
     3101    }
    29763102
    29773103    $taxonomies = get_object_taxonomies( $object_type );
     
    29793105    foreach ( $object_ids as $id ) {
    29803106        foreach ( $taxonomies as $taxonomy ) {
    2981             wp_cache_delete($id, "{$taxonomy}_relationships");
     3107            wp_cache_delete( $id, "{$taxonomy}_relationships" );
    29823108        }
    29833109    }
     
    30083134 *                                  term object caches (false). Default true.
    30093135 */
    3010 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
     3136function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
    30113137    global $wpdb, $_wp_suspend_cache_invalidation;
    30123138
     
    30153141    }
    30163142
    3017     if ( !is_array($ids) )
    3018         $ids = array($ids);
     3143    if ( ! is_array( $ids ) ) {
     3144        $ids = array( $ids );
     3145    }
    30193146
    30203147    $taxonomies = array();
    30213148    // If no taxonomy, assume tt_ids.
    3022     if ( empty($taxonomy) ) {
    3023         $tt_ids = array_map('intval', $ids);
    3024         $tt_ids = implode(', ', $tt_ids);
    3025         $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
    3026         $ids = array();
     3149    if ( empty( $taxonomy ) ) {
     3150        $tt_ids = array_map( 'intval', $ids );
     3151        $tt_ids = implode( ', ', $tt_ids );
     3152        $terms  = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
     3153        $ids    = array();
    30273154        foreach ( (array) $terms as $term ) {
    30283155            $taxonomies[] = $term->taxonomy;
    3029             $ids[] = $term->term_id;
     3156            $ids[]        = $term->term_id;
    30303157            wp_cache_delete( $term->term_id, 'terms' );
    30313158        }
    3032         $taxonomies = array_unique($taxonomies);
     3159        $taxonomies = array_unique( $taxonomies );
    30333160    } else {
    3034         $taxonomies = array($taxonomy);
     3161        $taxonomies = array( $taxonomy );
    30353162        foreach ( $taxonomies as $taxonomy ) {
    30363163            foreach ( $ids as $id ) {
     
    31533280 * @return void|false False if all of the terms in `$object_ids` are already cached.
    31543281 */
    3155 function update_object_term_cache($object_ids, $object_type) {
    3156     if ( empty($object_ids) )
     3282function update_object_term_cache( $object_ids, $object_type ) {
     3283    if ( empty( $object_ids ) ) {
    31573284        return;
    3158 
    3159     if ( !is_array($object_ids) )
    3160         $object_ids = explode(',', $object_ids);
    3161 
    3162     $object_ids = array_map('intval', $object_ids);
    3163 
    3164     $taxonomies = get_object_taxonomies($object_type);
     3285    }
     3286
     3287    if ( ! is_array( $object_ids ) ) {
     3288        $object_ids = explode( ',', $object_ids );
     3289    }
     3290
     3291    $object_ids = array_map( 'intval', $object_ids );
     3292
     3293    $taxonomies = get_object_taxonomies( $object_type );
    31653294
    31663295    $ids = array();
    31673296    foreach ( (array) $object_ids as $id ) {
    31683297        foreach ( $taxonomies as $taxonomy ) {
    3169             if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
     3298            if ( false === wp_cache_get( $id, "{$taxonomy}_relationships" ) ) {
    31703299                $ids[] = $id;
    31713300                break;
     
    31743303    }
    31753304
    3176     if ( empty( $ids ) )
     3305    if ( empty( $ids ) ) {
    31773306        return false;
    3178 
    3179     $terms = wp_get_object_terms( $ids, $taxonomies, array(
    3180         'fields' => 'all_with_object_id',
    3181         'orderby' => 'name',
    3182         'update_term_meta_cache' => false,
    3183     ) );
     3307    }
     3308
     3309    $terms = wp_get_object_terms(
     3310        $ids, $taxonomies, array(
     3311            'fields'                 => 'all_with_object_id',
     3312            'orderby'                => 'name',
     3313            'update_term_meta_cache' => false,
     3314        )
     3315    );
    31843316
    31853317    $object_terms = array();
     
    31903322    foreach ( $ids as $id ) {
    31913323        foreach ( $taxonomies as $taxonomy ) {
    3192             if ( ! isset($object_terms[$id][$taxonomy]) ) {
    3193                 if ( !isset($object_terms[$id]) )
    3194                     $object_terms[$id] = array();
    3195                 $object_terms[$id][$taxonomy] = array();
     3324            if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
     3325                if ( ! isset( $object_terms[ $id ] ) ) {
     3326                    $object_terms[ $id ] = array();
     3327                }
     3328                $object_terms[ $id ][ $taxonomy ] = array();
    31963329            }
    31973330        }
     
    32393372 */
    32403373function _get_term_hierarchy( $taxonomy ) {
    3241     if ( !is_taxonomy_hierarchical($taxonomy) )
     3374    if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
    32423375        return array();
    3243     $children = get_option("{$taxonomy}_children");
    3244 
    3245     if ( is_array($children) )
     3376    }
     3377    $children = get_option( "{$taxonomy}_children" );
     3378
     3379    if ( is_array( $children ) ) {
    32463380        return $children;
     3381    }
    32473382    $children = array();
    3248     $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));
     3383    $terms    = get_terms(
     3384        $taxonomy, array(
     3385            'get'     => 'all',
     3386            'orderby' => 'id',
     3387            'fields'  => 'id=>parent',
     3388        )
     3389    );
    32493390    foreach ( $terms as $term_id => $parent ) {
    3250         if ( $parent > 0 )
    3251             $children[$parent][] = $term_id;
    3252     }
    3253     update_option("{$taxonomy}_children", $children);
     3391        if ( $parent > 0 ) {
     3392            $children[ $parent ][] = $term_id;
     3393        }
     3394    }
     3395    update_option( "{$taxonomy}_children", $children );
    32543396
    32553397    return $children;
     
    32773419function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
    32783420    $empty_array = array();
    3279     if ( empty($terms) )
     3421    if ( empty( $terms ) ) {
    32803422        return $empty_array;
    3281 
    3282     $term_list = array();
    3283     $has_children = _get_term_hierarchy($taxonomy);
    3284 
    3285     if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
     3423    }
     3424
     3425    $term_list    = array();
     3426    $has_children = _get_term_hierarchy( $taxonomy );
     3427
     3428    if ( ( 0 != $term_id ) && ! isset( $has_children[ $term_id ] ) ) {
    32863429        return $empty_array;
     3430    }
    32873431
    32883432    // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
     
    32933437    foreach ( (array) $terms as $term ) {
    32943438        $use_id = false;
    3295         if ( !is_object($term) ) {
    3296             $term = get_term($term, $taxonomy);
    3297             if ( is_wp_error( $term ) )
     3439        if ( ! is_object( $term ) ) {
     3440            $term = get_term( $term, $taxonomy );
     3441            if ( is_wp_error( $term ) ) {
    32983442                return $term;
     3443            }
    32993444            $use_id = true;
    33003445        }
     
    33063451
    33073452        if ( $term->parent == $term_id ) {
    3308             if ( $use_id )
     3453            if ( $use_id ) {
    33093454                $term_list[] = $term->term_id;
    3310             else
     3455            } else {
    33113456                $term_list[] = $term;
    3312 
    3313             if ( !isset($has_children[$term->term_id]) )
     3457            }
     3458
     3459            if ( ! isset( $has_children[ $term->term_id ] ) ) {
    33143460                continue;
     3461            }
    33153462
    33163463            $ancestors[ $term->term_id ] = 1;
    33173464
    3318             if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
    3319                 $term_list = array_merge($term_list, $children);
     3465            if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ) ) {
     3466                $term_list = array_merge( $term_list, $children );
     3467            }
    33203468        }
    33213469    }
     
    33423490
    33433491    // This function only works for hierarchical taxonomies like post categories.
    3344     if ( !is_taxonomy_hierarchical( $taxonomy ) )
     3492    if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
    33453493        return;
    3346 
    3347     $term_hier = _get_term_hierarchy($taxonomy);
    3348 
    3349     if ( empty($term_hier) )
     3494    }
     3495
     3496    $term_hier = _get_term_hierarchy( $taxonomy );
     3497
     3498    if ( empty( $term_hier ) ) {
    33503499        return;
    3351 
    3352     $term_items = array();
     3500    }
     3501
     3502    $term_items  = array();
    33533503    $terms_by_id = array();
    3354     $term_ids = array();
     3504    $term_ids    = array();
    33553505
    33563506    foreach ( (array) $terms as $key => $term ) {
    3357         $terms_by_id[$term->term_id] = & $terms[$key];
    3358         $term_ids[$term->term_taxonomy_id] = $term->term_id;
     3507        $terms_by_id[ $term->term_id ]       = & $terms[ $key ];
     3508        $term_ids[ $term->term_taxonomy_id ] = $term->term_id;
    33593509    }
    33603510
    33613511    // Get the object and term ids and stick them in a lookup table.
    3362     $tax_obj = get_taxonomy($taxonomy);
    3363     $object_types = esc_sql($tax_obj->object_type);
    3364     $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode(',', array_keys($term_ids)) . ") AND post_type IN ('" . implode("', '", $object_types) . "') AND post_status = 'publish'");
     3512    $tax_obj      = get_taxonomy( $taxonomy );
     3513    $object_types = esc_sql( $tax_obj->object_type );
     3514    $results      = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" );
    33653515    foreach ( $results as $row ) {
    3366         $id = $term_ids[$row->term_taxonomy_id];
    3367         $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
     3516        $id                                   = $term_ids[ $row->term_taxonomy_id ];
     3517        $term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1;
    33683518    }
    33693519
    33703520    // Touch every ancestor's lookup row for each post in each term.
    33713521    foreach ( $term_ids as $term_id ) {
    3372         $child = $term_id;
     3522        $child     = $term_id;
    33733523        $ancestors = array();
    3374         while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
     3524        while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) {
    33753525            $ancestors[] = $child;
    3376             if ( !empty( $term_items[$term_id] ) )
    3377                 foreach ( $term_items[$term_id] as $item_id => $touches ) {
    3378                     $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
     3526            if ( ! empty( $term_items[ $term_id ] ) ) {
     3527                foreach ( $term_items[ $term_id ] as $item_id => $touches ) {
     3528                    $term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1;
    33793529                }
     3530            }
    33803531            $child = $parent;
    33813532
     
    33873538
    33883539    // Transfer the touched cells.
    3389     foreach ( (array) $term_items as $id => $items )
    3390         if ( isset($terms_by_id[$id]) )
    3391             $terms_by_id[$id]->count = count($items);
     3540    foreach ( (array) $term_items as $id => $items ) {
     3541        if ( isset( $terms_by_id[ $id ] ) ) {
     3542            $terms_by_id[ $id ]->count = count( $items );
     3543        }
     3544    }
    33923545}
    33933546
     
    34083561    $non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
    34093562    if ( ! empty( $non_cached_ids ) ) {
    3410         $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
     3563        $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
    34113564
    34123565        update_term_cache( $fresh_terms, $update_meta_cache );
     
    34413594    $object_types = (array) $taxonomy->object_type;
    34423595
    3443     foreach ( $object_types as &$object_type )
     3596    foreach ( $object_types as &$object_type ) {
    34443597        list( $object_type ) = explode( ':', $object_type );
     3598    }
    34453599
    34463600    $object_types = array_unique( $object_types );
     
    34513605    }
    34523606
    3453     if ( $object_types )
     3607    if ( $object_types ) {
    34543608        $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
     3609    }
    34553610
    34563611    foreach ( (array) $terms as $term ) {
     
    34583613
    34593614        // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    3460         if ( $check_attachments )
     3615        if ( $check_attachments ) {
    34613616            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
    3462 
    3463         if ( $object_types )
    3464             $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
     3617        }
     3618
     3619        if ( $object_types ) {
     3620            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
     3621        }
    34653622
    34663623        /** This action is documented in wp-includes/taxonomy.php */
     
    35283685    if ( is_object( $term_id ) ) {
    35293686        $shared_term = $term_id;
    3530         $term_id = intval( $shared_term->term_id );
     3687        $term_id     = intval( $shared_term->term_id );
    35313688    }
    35323689
    35333690    if ( is_object( $term_taxonomy_id ) ) {
    3534         $term_taxonomy = $term_taxonomy_id;
     3691        $term_taxonomy    = $term_taxonomy_id;
    35353692        $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id );
    35363693    }
     
    35583715
    35593716    $new_term_data = array(
    3560         'name' => $shared_term->name,
    3561         'slug' => $shared_term->slug,
     3717        'name'       => $shared_term->name,
     3718        'slug'       => $shared_term->slug,
    35623719        'term_group' => $shared_term->term_group,
    35633720    );
     
    35703727
    35713728    // Update the existing term_taxonomy to point to the newly created term.
    3572     $wpdb->update( $wpdb->term_taxonomy,
     3729    $wpdb->update(
     3730        $wpdb->term_taxonomy,
    35733731        array( 'term_id' => $new_term_id ),
    35743732        array( 'term_taxonomy_id' => $term_taxonomy_id )
     
    35833741    if ( ! empty( $children_tt_ids ) ) {
    35843742        foreach ( $children_tt_ids as $child_tt_id ) {
    3585             $wpdb->update( $wpdb->term_taxonomy,
     3743            $wpdb->update(
     3744                $wpdb->term_taxonomy,
    35863745                array( 'parent' => $new_term_id ),
    35873746                array( 'term_taxonomy_id' => $child_tt_id )
     
    36043763    // Clean the cache for term taxonomies formerly shared with the current term.
    36053764    $shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
    3606     $taxonomies_to_clean = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );
     3765    $taxonomies_to_clean    = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );
    36073766
    36083767    foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) {
     
    36983857    $_shared_terms = array();
    36993858    foreach ( $shared_terms as $shared_term ) {
    3700         $term_id = intval( $shared_term->term_id );
     3859        $term_id                   = intval( $shared_term->term_id );
    37013860        $_shared_terms[ $term_id ] = $shared_term;
    37023861    }
     
    37053864    // Get term taxonomy data for all shared terms.
    37063865    $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
    3707     $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
     3866    $shared_tts      = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
    37083867
    37093868    // Split term data recording is slow, so we do it just once, outside the loop.
    3710     $split_term_data = get_option( '_split_terms', array() );
     3869    $split_term_data    = get_option( '_split_terms', array() );
    37113870    $skipped_first_term = $taxonomies = array();
    37123871    foreach ( $shared_tts as $shared_tt ) {
     
    37943953function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
    37953954    global $wpdb;
    3796     $post_ids = $wpdb->get_col( $wpdb->prepare(
    3797         "SELECT m1.post_id
     3955    $post_ids = $wpdb->get_col(
     3956        $wpdb->prepare(
     3957            "SELECT m1.post_id
    37983958        FROM {$wpdb->postmeta} AS m1
    37993959            INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
     
    38023962            AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s )
    38033963            AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
    3804         $taxonomy,
    3805         $term_id
    3806     ) );
     3964            $taxonomy,
     3965            $term_id
     3966        )
     3967    );
    38073968
    38083969    if ( $post_ids ) {
     
    39184079    global $wp_rewrite;
    39194080
    3920     if ( !is_object($term) ) {
     4081    if ( ! is_object( $term ) ) {
    39214082        if ( is_int( $term ) ) {
    39224083            $term = get_term( $term, $taxonomy );
     
    39264087    }
    39274088
    3928     if ( !is_object($term) )
     4089    if ( ! is_object( $term ) ) {
    39294090        $term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
    3930 
    3931     if ( is_wp_error( $term ) )
     4091    }
     4092
     4093    if ( is_wp_error( $term ) ) {
    39324094        return $term;
     4095    }
    39334096
    39344097    $taxonomy = $term->taxonomy;
    39354098
    3936     $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
     4099    $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
    39374100
    39384101    /**
     
    39474110
    39484111    $slug = $term->slug;
    3949     $t = get_taxonomy($taxonomy);
    3950 
    3951     if ( empty($termlink) ) {
    3952         if ( 'category' == $taxonomy )
     4112    $t    = get_taxonomy( $taxonomy );
     4113
     4114    if ( empty( $termlink ) ) {
     4115        if ( 'category' == $taxonomy ) {
    39534116            $termlink = '?cat=' . $term->term_id;
    3954         elseif ( $t->query_var )
     4117        } elseif ( $t->query_var ) {
    39554118            $termlink = "?$t->query_var=$slug";
    3956         else
     4119        } else {
    39574120            $termlink = "?taxonomy=$taxonomy&term=$slug";
    3958         $termlink = home_url($termlink);
     4121        }
     4122        $termlink = home_url( $termlink );
    39594123    } else {
    39604124        if ( $t->rewrite['hierarchical'] ) {
    39614125            $hierarchical_slugs = array();
    3962             $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
    3963             foreach ( (array)$ancestors as $ancestor ) {
    3964                 $ancestor_term = get_term($ancestor, $taxonomy);
     4126            $ancestors          = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
     4127            foreach ( (array) $ancestors as $ancestor ) {
     4128                $ancestor_term        = get_term( $ancestor, $taxonomy );
    39654129                $hierarchical_slugs[] = $ancestor_term->slug;
    39664130            }
    3967             $hierarchical_slugs = array_reverse($hierarchical_slugs);
     4131            $hierarchical_slugs   = array_reverse( $hierarchical_slugs );
    39684132            $hierarchical_slugs[] = $slug;
    3969             $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
     4133            $termlink             = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
    39704134        } else {
    3971             $termlink = str_replace("%$taxonomy%", $slug, $termlink);
    3972         }
    3973         $termlink = home_url( user_trailingslashit($termlink, 'category') );
     4135            $termlink = str_replace( "%$taxonomy%", $slug, $termlink );
     4136        }
     4137        $termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
    39744138    }
    39754139    // Back Compat filters.
     
    40334197function the_taxonomies( $args = array() ) {
    40344198    $defaults = array(
    4035         'post' => 0,
     4199        'post'   => 0,
    40364200        'before' => '',
    4037         'sep' => ' ',
    4038         'after' => '',
     4201        'sep'    => ' ',
     4202        'after'  => '',
    40394203    );
    40404204
     
    40664230    $post = get_post( $post );
    40674231
    4068     $args = wp_parse_args( $args, array(
    4069         /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
    4070         'template' => __( '%s: %l.' ),
    4071         'term_template' => '<a href="%1$s">%2$s</a>',
    4072     ) );
     4232    $args = wp_parse_args(
     4233        $args, array(
     4234            /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
     4235            'template'      => __( '%s: %l.' ),
     4236            'term_template' => '<a href="%1$s">%2$s</a>',
     4237        )
     4238    );
    40734239
    40744240    $taxonomies = array();
     
    41034269        }
    41044270        if ( $links ) {
    4105             $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
     4271            $taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
    41064272        }
    41074273    }
     
    41204286    $post = get_post( $post );
    41214287
    4122     return get_object_taxonomies($post);
     4288    return get_object_taxonomies( $post );
    41234289}
    41244290
     
    41384304 */
    41394305function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
    4140     if ( !$object_id = (int) $object_id )
     4306    if ( ! $object_id = (int) $object_id ) {
    41414307        return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
     4308    }
    41424309
    41434310    $object_terms = get_object_term_cache( $object_id, $taxonomy );
     
    41514318    }
    41524319
    4153     if ( is_wp_error( $object_terms ) )
     4320    if ( is_wp_error( $object_terms ) ) {
    41544321        return $object_terms;
    4155     if ( empty( $object_terms ) )
     4322    }
     4323    if ( empty( $object_terms ) ) {
    41564324        return false;
    4157     if ( empty( $terms ) )
    4158         return ( !empty( $object_terms ) );
     4325    }
     4326    if ( empty( $terms ) ) {
     4327        return ( ! empty( $object_terms ) );
     4328    }
    41594329
    41604330    $terms = (array) $terms;
    41614331
    4162     if ( $ints = array_filter( $terms, 'is_int' ) )
     4332    if ( $ints = array_filter( $terms, 'is_int' ) ) {
    41634333        $strs = array_diff( $terms, $ints );
    4164     else
     4334    } else {
    41654335        $strs =& $terms;
     4336    }
    41664337
    41674338    foreach ( $object_terms as $object_term ) {
     
    41784349            }
    41794350
    4180             if ( in_array( $object_term->name, $strs ) ) return true;
    4181             if ( in_array( $object_term->slug, $strs ) ) return true;
     4351            if ( in_array( $object_term->name, $strs ) ) {
     4352                return true;
     4353            }
     4354            if ( in_array( $object_term->slug, $strs ) ) {
     4355                return true;
     4356            }
    41824357        }
    41834358    }
     
    42364411
    42374412    if ( 'taxonomy' === $resource_type ) {
    4238         $term = get_term($object_id, $object_type);
    4239         while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
     4413        $term = get_term( $object_id, $object_type );
     4414        while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
    42404415            $ancestors[] = (int) $term->parent;
    4241             $term = get_term($term->parent, $object_type);
     4416            $term        = get_term( $term->parent, $object_type );
    42424417        }
    42434418    } elseif ( 'post_type' === $resource_type ) {
    4244         $ancestors = get_post_ancestors($object_id);
     4419        $ancestors = get_post_ancestors( $object_id );
    42454420    }
    42464421
     
    42924467function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
    42934468    // Nothing fancy here - bail
    4294     if ( !$parent )
     4469    if ( ! $parent ) {
    42954470        return 0;
     4471    }
    42964472
    42974473    // Can't be its own parent.
    4298     if ( $parent == $term_id )
     4474    if ( $parent == $term_id ) {
    42994475        return 0;
     4476    }
    43004477
    43014478    // Now look for larger loops.
    4302     if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
     4479    if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) {
    43034480        return $parent; // No loop
     4481    }
    43044482
    43054483    // Setting $parent to the given value causes a loop.
    4306     if ( isset( $loop[$term_id] ) )
     4484    if ( isset( $loop[ $term_id ] ) ) {
    43074485        return 0;
     4486    }
    43084487
    43094488    // There's a loop, but it doesn't contain $term_id. Break the loop.
    4310     foreach ( array_keys( $loop ) as $loop_member )
     4489    foreach ( array_keys( $loop ) as $loop_member ) {
    43114490        wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
     4491    }
    43124492
    43134493    return $parent;
Note: See TracChangeset for help on using the changeset viewer.