equal
deleted
inserted
replaced
5 * @package WordPress |
5 * @package WordPress |
6 * @subpackage Administration |
6 * @subpackage Administration |
7 */ |
7 */ |
8 |
8 |
9 // |
9 // |
10 // Category |
10 // Category. |
11 // |
11 // |
12 |
12 |
13 /** |
13 /** |
14 * Check whether a category exists. |
14 * Check whether a category exists. |
15 * |
15 * |
51 * @param int|string $cat_name |
51 * @param int|string $cat_name |
52 * @param int $parent |
52 * @param int $parent |
53 * @return int|WP_Error |
53 * @return int|WP_Error |
54 */ |
54 */ |
55 function wp_create_category( $cat_name, $parent = 0 ) { |
55 function wp_create_category( $cat_name, $parent = 0 ) { |
56 if ( $id = category_exists( $cat_name, $parent ) ) { |
56 $id = category_exists( $cat_name, $parent ); |
|
57 if ( $id ) { |
57 return $id; |
58 return $id; |
58 } |
59 } |
59 |
60 |
60 return wp_insert_category( |
61 return wp_insert_category( |
61 array( |
62 array( |
70 * |
71 * |
71 * @since 2.0.0 |
72 * @since 2.0.0 |
72 * |
73 * |
73 * @param string[] $categories Array of category names to create. |
74 * @param string[] $categories Array of category names to create. |
74 * @param int $post_id Optional. The post ID. Default empty. |
75 * @param int $post_id Optional. The post ID. Default empty. |
75 * @return array List of categories to create for the given post. |
76 * @return int[] Array of IDs of categories assigned to the given post. |
76 */ |
77 */ |
77 function wp_create_categories( $categories, $post_id = '' ) { |
78 function wp_create_categories( $categories, $post_id = '' ) { |
78 $cat_ids = array(); |
79 $cat_ids = array(); |
79 foreach ( $categories as $category ) { |
80 foreach ( $categories as $category ) { |
80 if ( $id = category_exists( $category ) ) { |
81 $id = category_exists( $category ); |
|
82 if ( $id ) { |
81 $cat_ids[] = $id; |
83 $cat_ids[] = $id; |
82 } elseif ( $id = wp_create_category( $category ) ) { |
84 } else { |
83 $cat_ids[] = $id; |
85 $id = wp_create_category( $category ); |
|
86 if ( $id ) { |
|
87 $cat_ids[] = $id; |
|
88 } |
84 } |
89 } |
85 } |
90 } |
86 |
91 |
87 if ( $post_id ) { |
92 if ( $post_id ) { |
88 wp_set_post_categories( $post_id, $cat_ids ); |
93 wp_set_post_categories( $post_id, $cat_ids ); |
122 'category_nicename' => '', |
127 'category_nicename' => '', |
123 'category_parent' => '', |
128 'category_parent' => '', |
124 ); |
129 ); |
125 $catarr = wp_parse_args( $catarr, $cat_defaults ); |
130 $catarr = wp_parse_args( $catarr, $cat_defaults ); |
126 |
131 |
127 if ( trim( $catarr['cat_name'] ) == '' ) { |
132 if ( '' === trim( $catarr['cat_name'] ) ) { |
128 if ( ! $wp_error ) { |
133 if ( ! $wp_error ) { |
129 return 0; |
134 return 0; |
130 } else { |
135 } else { |
131 return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) ); |
136 return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) ); |
132 } |
137 } |
185 |
190 |
186 if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) { |
191 if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) { |
187 return false; |
192 return false; |
188 } |
193 } |
189 |
194 |
190 // First, get all of the original fields |
195 // First, get all of the original fields. |
191 $category = get_term( $cat_ID, 'category', ARRAY_A ); |
196 $category = get_term( $cat_ID, 'category', ARRAY_A ); |
192 _make_cat_compat( $category ); |
197 _make_cat_compat( $category ); |
193 |
198 |
194 // Escape data pulled from DB. |
199 // Escape data pulled from DB. |
195 $category = wp_slash( $category ); |
200 $category = wp_slash( $category ); |
199 |
204 |
200 return wp_insert_category( $catarr ); |
205 return wp_insert_category( $catarr ); |
201 } |
206 } |
202 |
207 |
203 // |
208 // |
204 // Tags |
209 // Tags. |
205 // |
210 // |
206 |
211 |
207 /** |
212 /** |
208 * Check whether a post tag with a given name exists. |
213 * Check whether a post tag with a given name exists. |
209 * |
214 * |
293 /** |
298 /** |
294 * Add a new term to the database if it does not already exist. |
299 * Add a new term to the database if it does not already exist. |
295 * |
300 * |
296 * @since 2.8.0 |
301 * @since 2.8.0 |
297 * |
302 * |
298 * @param int|string $tag_name |
303 * @param string $tag_name The term name. |
299 * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. |
304 * @param string $taxonomy Optional. The taxonomy within which to create the term. Default 'post_tag'. |
300 * @return array|WP_Error |
305 * @return array|WP_Error |
301 */ |
306 */ |
302 function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) { |
307 function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) { |
303 if ( $id = term_exists( $tag_name, $taxonomy ) ) { |
308 $id = term_exists( $tag_name, $taxonomy ); |
|
309 if ( $id ) { |
304 return $id; |
310 return $id; |
305 } |
311 } |
306 |
312 |
307 return wp_insert_term( $tag_name, $taxonomy ); |
313 return wp_insert_term( $tag_name, $taxonomy ); |
308 } |
314 } |