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-admin/includes/taxonomy.php

    r37573 r42343  
    2323 */
    2424function category_exists( $cat_name, $parent = null ) {
    25     $id = term_exists($cat_name, 'category', $parent);
    26     if ( is_array($id) )
     25    $id = term_exists( $cat_name, 'category', $parent );
     26    if ( is_array( $id ) ) {
    2727        $id = $id['term_id'];
     28    }
    2829    return $id;
    2930}
     
    5354 */
    5455function wp_create_category( $cat_name, $parent = 0 ) {
    55     if ( $id = category_exists($cat_name, $parent) )
     56    if ( $id = category_exists( $cat_name, $parent ) ) {
    5657        return $id;
    57 
    58     return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
     58    }
     59
     60    return wp_insert_category(
     61        array(
     62            'cat_name'        => $cat_name,
     63            'category_parent' => $parent,
     64        )
     65    );
    5966}
    6067
     
    6976 */
    7077function wp_create_categories( $categories, $post_id = '' ) {
    71     $cat_ids = array ();
     78    $cat_ids = array();
    7279    foreach ( $categories as $category ) {
    7380        if ( $id = category_exists( $category ) ) {
     
    7885    }
    7986
    80     if ( $post_id )
    81         wp_set_post_categories($post_id, $cat_ids);
     87    if ( $post_id ) {
     88        wp_set_post_categories( $post_id, $cat_ids );
     89    }
    8290
    8391    return $cat_ids;
     
    107115 */
    108116function wp_insert_category( $catarr, $wp_error = false ) {
    109     $cat_defaults = array( 'cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '' );
    110     $catarr = wp_parse_args( $catarr, $cat_defaults );
     117    $cat_defaults = array(
     118        'cat_ID'               => 0,
     119        'taxonomy'             => 'category',
     120        'cat_name'             => '',
     121        'category_description' => '',
     122        'category_nicename'    => '',
     123        'category_parent'      => '',
     124    );
     125    $catarr       = wp_parse_args( $catarr, $cat_defaults );
    111126
    112127    if ( trim( $catarr['cat_name'] ) == '' ) {
     
    121136
    122137    // Are we updating or creating?
    123     $update = ! empty ( $catarr['cat_ID'] );
    124 
    125     $name = $catarr['cat_name'];
     138    $update = ! empty( $catarr['cat_ID'] );
     139
     140    $name        = $catarr['cat_name'];
    126141    $description = $catarr['category_description'];
    127     $slug = $catarr['category_nicename'];
    128     $parent = (int) $catarr['category_parent'];
     142    $slug        = $catarr['category_nicename'];
     143    $parent      = (int) $catarr['category_parent'];
    129144    if ( $parent < 0 ) {
    130145        $parent = 0;
     
    137152    }
    138153
    139     $args = compact('name', 'slug', 'parent', 'description');
     154    $args = compact( 'name', 'slug', 'parent', 'description' );
    140155
    141156    if ( $update ) {
     
    166181 * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
    167182 */
    168 function wp_update_category($catarr) {
     183function wp_update_category( $catarr ) {
    169184    $cat_ID = (int) $catarr['cat_ID'];
    170185
    171     if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
     186    if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) {
    172187        return false;
     188    }
    173189
    174190    // First, get all of the original fields
     
    177193
    178194    // Escape data pulled from DB.
    179     $category = wp_slash($category);
     195    $category = wp_slash( $category );
    180196
    181197    // Merge old and new fields with new fields overwriting old ones.
    182     $catarr = array_merge($category, $catarr);
    183 
    184     return wp_insert_category($catarr);
     198    $catarr = array_merge( $category, $catarr );
     199
     200    return wp_insert_category( $catarr );
    185201}
    186202
     
    197213 * @return mixed
    198214 */
    199 function tag_exists($tag_name) {
    200     return term_exists($tag_name, 'post_tag');
     215function tag_exists( $tag_name ) {
     216    return term_exists( $tag_name, 'post_tag' );
    201217}
    202218
     
    209225 * @return array|WP_Error
    210226 */
    211 function wp_create_tag($tag_name) {
    212     return wp_create_term( $tag_name, 'post_tag');
     227function wp_create_tag( $tag_name ) {
     228    return wp_create_term( $tag_name, 'post_tag' );
    213229}
    214230
     
    223239 */
    224240function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
    225     return get_terms_to_edit( $post_id, $taxonomy);
     241    return get_terms_to_edit( $post_id, $taxonomy );
    226242}
    227243
     
    237253function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
    238254    $post_id = (int) $post_id;
    239     if ( !$post_id )
     255    if ( ! $post_id ) {
    240256        return false;
     257    }
    241258
    242259    $terms = get_object_term_cache( $post_id, $taxonomy );
     
    283300 * @return array|WP_Error
    284301 */
    285 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
    286     if ( $id = term_exists($tag_name, $taxonomy) )
     302function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
     303    if ( $id = term_exists( $tag_name, $taxonomy ) ) {
    287304        return $id;
    288 
    289     return wp_insert_term($tag_name, $taxonomy);
    290 }
     305    }
     306
     307    return wp_insert_term( $tag_name, $taxonomy );
     308}
Note: See TracChangeset for help on using the changeset viewer.