Changeset 28374
- Timestamp:
- 05/13/2014 03:26:51 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/taxonomy.php
r27674 r28374 90 90 * @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. 91 91 */ 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 ) 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 96 if ( trim( $catarr['cat_name'] ) == '' ) { 97 if ( ! $wp_error ) { 99 98 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']; 105 105 106 106 // 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 ) { 119 114 $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'] ) ) ) { 122 120 $parent = 0; 121 } 123 122 124 123 $args = compact('name', 'slug', 'parent', 'description'); 125 124 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 { 135 135 return 0; 136 }137 138 return $cat _ID['term_id'];136 } 137 } 138 return $catarr['cat_ID']['term_id']; 139 139 } 140 140 -
trunk/tests/phpunit/tests/taxonomy.php
r25923 r28374 169 169 $this->assertTrue( in_category( $term['term_id'], $post ) ); 170 170 } 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 } 171 207 }
Note: See TracChangeset
for help on using the changeset viewer.