Make WordPress Core

Changeset 28374


Ignore:
Timestamp:
05/13/2014 03:26:51 AM (12 years ago)
Author:
wonderboymusic
Message:

Eliminate use of extract() in wp_insert_category(). Adds unit tests. All unit tests pass.

Props MikeHansenMe.
See #22400.

Location:
trunk
Files:
2 edited

Legend:

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

    r27674 r28374  
    9090 * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.
    9191 */
    92 function wp_insert_category($catarr, $wp_error = false) {
    93         $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
    94         $catarr = wp_parse_args($catarr, $cat_defaults);
    95         extract($catarr, EXTR_SKIP);
    96 
    97         if ( trim( $cat_name ) == '' ) {
    98                 if ( ! $wp_error )
     92function wp_insert_category( $catarr, $wp_error = false ) {
     93        $cat_defaults = array( 'cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '' );
     94        $catarr = wp_parse_args( $catarr, $cat_defaults );
     95
     96        if ( trim( $catarr['cat_name'] ) == '' ) {
     97                if ( ! $wp_error ) {
    9998                        return 0;
    100                 else
    101                         return new WP_Error( 'cat_name', __('You did not enter a category name.') );
    102         }
    103 
    104         $cat_ID = (int) $cat_ID;
     99                } else {
     100                        return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );
     101                }
     102        }
     103
     104        $catarr['cat_ID'] = (int) $catarr['cat_ID'];
    105105
    106106        // Are we updating or creating?
    107         if ( !empty ($cat_ID) )
    108                 $update = true;
    109         else
    110                 $update = false;
    111 
    112         $name = $cat_name;
    113         $description = $category_description;
    114         $slug = $category_nicename;
    115         $parent = $category_parent;
    116 
    117         $parent = (int) $parent;
    118         if ( $parent < 0 )
     107        $update = ! empty ( $catarr['cat_ID'] );
     108
     109        $name = $catarr['cat_name'];
     110        $description = $catarr['category_description'];
     111        $slug = $catarr['category_nicename'];
     112        $parent = (int) $catarr['category_parent'];
     113        if ( $parent < 0 ) {
    119114                $parent = 0;
    120 
    121         if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
     115        }
     116
     117        if ( empty( $parent )
     118                || ! term_exists( $parent, $catarr['taxonomy'] )
     119                || ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
    122120                $parent = 0;
     121        }
    123122
    124123        $args = compact('name', 'slug', 'parent', 'description');
    125124
    126         if ( $update )
    127                 $cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
    128         else
    129                 $cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
    130 
    131         if ( is_wp_error($cat_ID) ) {
    132                 if ( $wp_error )
    133                         return $cat_ID;
    134                 else
     125        if ( $update ) {
     126                $catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
     127        } else {
     128                $catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
     129        }
     130
     131        if ( is_wp_error( $catarr['cat_ID'] ) ) {
     132                if ( $wp_error ) {
     133                        return $catarr['cat_ID'];
     134                } else {
    135135                        return 0;
    136         }
    137 
    138         return $cat_ID['term_id'];
     136                }
     137        }
     138        return $catarr['cat_ID']['term_id'];
    139139}
    140140
  • trunk/tests/phpunit/tests/taxonomy.php

    r25923 r28374  
    169169                $this->assertTrue( in_category( $term['term_id'], $post ) );
    170170        }
     171
     172        function test_insert_category_create() {
     173                $cat = array(
     174                        'cat_ID' => 0,
     175                        'taxonomy' => 'category',
     176                        'cat_name' => 'test1'
     177                );
     178                $this->assertTrue( is_numeric( wp_insert_category( $cat, true ) ) );
     179        }
     180
     181        function test_insert_category_update() {
     182                $cat = array(
     183                        'cat_ID' => 1,
     184                        'taxonomy' => 'category',
     185                        'cat_name' => 'Updated Name'
     186                );
     187                $this->assertEquals( 1, wp_insert_category( $cat ) );
     188        }
     189
     190        function test_insert_category_force_error_handle() {
     191                $cat = array(
     192                        'cat_ID' => 0,
     193                        'taxonomy' => 'force_error',
     194                        'cat_name' => 'Error'
     195                );
     196                $this->assertTrue( is_a( wp_insert_category( $cat, true ), 'WP_Error' ) );
     197        }
     198
     199        function test_insert_category_force_error_no_handle() {
     200                $cat = array(
     201                        'cat_ID' => 0,
     202                        'taxonomy' => 'force_error',
     203                        'cat_name' => 'Error'
     204                );
     205                $this->assertEquals( 0, wp_insert_category( $cat, false ) );
     206        }
    171207}
Note: See TracChangeset for help on using the changeset viewer.