Ticket #22400: 22400.taxonomy.php.2.diff
File 22400.taxonomy.php.2.diff, 4.3 KB (added by , 7 years ago) |
---|
-
tests/phpunit/tests/taxonomy.php
168 168 wp_set_object_terms( $post->ID, $term['term_id'], 'category' ); 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 } -
src/wp-admin/includes/taxonomy.php
89 89 * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values. 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); 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 ); 96 95 97 if ( trim( $cat _name) == '' ) {98 if ( ! $wp_error ) 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.') ); 99 } else { 100 return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) ); 101 } 102 102 } 103 103 104 $cat _ID = (int) $cat_ID;104 $catarr['cat_ID'] = (int) $catarr['cat_ID']; 105 105 106 106 // Are we updating or creating? 107 if ( !empty ( $cat_ID) )107 if ( !empty ( $catarr['cat_ID'] ) ) { 108 108 $update = true; 109 else109 } else { 110 110 $update = false; 111 } 111 112 112 $name = $cat _name;113 $description = $cat egory_description;114 $slug = $cat egory_nicename;115 $parent = $cat egory_parent;113 $name = $catarr['cat_name']; 114 $description = $catarr['category_description']; 115 $slug = $catarr['category_nicename']; 116 $parent = $catarr['category_parent']; 116 117 117 118 $parent = (int) $parent; 118 if ( $parent < 0 ) 119 if ( $parent < 0 ) { 119 120 $parent = 0; 121 } 120 122 121 if ( empty( $parent ) || ! term_exists( $parent, $ taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )123 if ( empty( $parent ) || ! term_exists( $parent, $catarr['taxonomy'] ) || ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) { 122 124 $parent = 0; 125 } 123 126 124 127 $args = compact('name', 'slug', 'parent', 'description'); 125 128 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); 129 if ( $update ) { 130 $catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args); 131 } else { 132 $catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args); 133 } 130 134 131 if ( is_wp_error( $cat_ID) ) {132 if ( $wp_error ) 133 return $cat _ID;134 else135 if ( is_wp_error( $catarr['cat_ID'] ) ) { 136 if ( $wp_error ) { 137 return $catarr['cat_ID']; 138 } else { 135 139 return 0; 140 } 136 141 } 137 138 return $cat_ID['term_id']; 142 return $catarr['cat_ID']['term_id']; 139 143 } 140 144 141 145 /**