0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* WordPress Taxonomy Administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
|
|
9 |
// |
|
10 |
// Category |
|
11 |
// |
|
12 |
|
|
13 |
/** |
5
|
14 |
* Check whether a category exists. |
0
|
15 |
* |
|
16 |
* @since 2.0.0 |
|
17 |
* |
5
|
18 |
* @see term_exists() |
|
19 |
* |
|
20 |
* @param int|string $cat_name Category name. |
|
21 |
* @param int $parent Optional. ID of parent term. |
|
22 |
* @return mixed |
0
|
23 |
*/ |
5
|
24 |
function category_exists( $cat_name, $parent = null ) { |
0
|
25 |
$id = term_exists($cat_name, 'category', $parent); |
|
26 |
if ( is_array($id) ) |
|
27 |
$id = $id['term_id']; |
|
28 |
return $id; |
|
29 |
} |
|
30 |
|
|
31 |
/** |
5
|
32 |
* Get category object for given ID and 'edit' filter context. |
0
|
33 |
* |
|
34 |
* @since 2.0.0 |
|
35 |
* |
5
|
36 |
* @param int $id |
|
37 |
* @return object |
0
|
38 |
*/ |
|
39 |
function get_category_to_edit( $id ) { |
|
40 |
$category = get_term( $id, 'category', OBJECT, 'edit' ); |
|
41 |
_make_cat_compat( $category ); |
|
42 |
return $category; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
5
|
46 |
* Add a new category to the database if it does not already exist. |
0
|
47 |
* |
|
48 |
* @since 2.0.0 |
|
49 |
* |
5
|
50 |
* @param int|string $cat_name |
|
51 |
* @param int $parent |
|
52 |
* @return int|WP_Error |
0
|
53 |
*/ |
|
54 |
function wp_create_category( $cat_name, $parent = 0 ) { |
|
55 |
if ( $id = category_exists($cat_name, $parent) ) |
|
56 |
return $id; |
|
57 |
|
|
58 |
return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) ); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
5
|
62 |
* Create categories for the given post. |
0
|
63 |
* |
|
64 |
* @since 2.0.0 |
|
65 |
* |
5
|
66 |
* @param array $categories List of categories to create. |
|
67 |
* @param int $post_id Optional. The post ID. Default empty. |
|
68 |
* @return List of categories to create for the given post. |
0
|
69 |
*/ |
5
|
70 |
function wp_create_categories( $categories, $post_id = '' ) { |
0
|
71 |
$cat_ids = array (); |
5
|
72 |
foreach ( $categories as $category ) { |
|
73 |
if ( $id = category_exists( $category ) ) { |
0
|
74 |
$cat_ids[] = $id; |
5
|
75 |
} elseif ( $id = wp_create_category( $category ) ) { |
|
76 |
$cat_ids[] = $id; |
|
77 |
} |
0
|
78 |
} |
|
79 |
|
|
80 |
if ( $post_id ) |
|
81 |
wp_set_post_categories($post_id, $cat_ids); |
|
82 |
|
|
83 |
return $cat_ids; |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Updates an existing Category or creates a new Category. |
|
88 |
* |
|
89 |
* @since 2.0.0 |
5
|
90 |
* @since 2.5.0 $wp_error parameter was added. |
|
91 |
* @since 3.0.0 The 'taxonomy' argument was added. |
0
|
92 |
* |
5
|
93 |
* @param array $catarr { |
|
94 |
* Array of arguments for inserting a new category. |
|
95 |
* |
|
96 |
* @type int $cat_ID Categoriy ID. A non-zero value updates an existing category. |
|
97 |
* Default 0. |
|
98 |
* @type string $taxonomy Taxonomy slug. Defualt 'category'. |
|
99 |
* @type string $cat_nam Category name. Default empty. |
|
100 |
* @type string $category_description Category description. Default empty. |
|
101 |
* @type string $category_nicename Category nice (display) name. Default empty. |
|
102 |
* @type int|string $category_parent Category parent ID. Default empty. |
|
103 |
* } |
|
104 |
* @param bool $wp_error Optional. Default false. |
|
105 |
* @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, |
|
106 |
* depending on param $wp_error. |
0
|
107 |
*/ |
5
|
108 |
function 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 ); |
0
|
111 |
|
5
|
112 |
if ( trim( $catarr['cat_name'] ) == '' ) { |
|
113 |
if ( ! $wp_error ) { |
0
|
114 |
return 0; |
5
|
115 |
} else { |
|
116 |
return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) ); |
|
117 |
} |
0
|
118 |
} |
|
119 |
|
5
|
120 |
$catarr['cat_ID'] = (int) $catarr['cat_ID']; |
0
|
121 |
|
|
122 |
// Are we updating or creating? |
5
|
123 |
$update = ! empty ( $catarr['cat_ID'] ); |
0
|
124 |
|
5
|
125 |
$name = $catarr['cat_name']; |
|
126 |
$description = $catarr['category_description']; |
|
127 |
$slug = $catarr['category_nicename']; |
|
128 |
$parent = (int) $catarr['category_parent']; |
|
129 |
if ( $parent < 0 ) { |
|
130 |
$parent = 0; |
|
131 |
} |
0
|
132 |
|
5
|
133 |
if ( empty( $parent ) |
|
134 |
|| ! term_exists( $parent, $catarr['taxonomy'] ) |
|
135 |
|| ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) { |
0
|
136 |
$parent = 0; |
5
|
137 |
} |
0
|
138 |
|
|
139 |
$args = compact('name', 'slug', 'parent', 'description'); |
|
140 |
|
5
|
141 |
if ( $update ) { |
|
142 |
$catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args ); |
|
143 |
} else { |
|
144 |
$catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args ); |
0
|
145 |
} |
|
146 |
|
5
|
147 |
if ( is_wp_error( $catarr['cat_ID'] ) ) { |
|
148 |
if ( $wp_error ) { |
|
149 |
return $catarr['cat_ID']; |
|
150 |
} else { |
|
151 |
return 0; |
|
152 |
} |
|
153 |
} |
|
154 |
return $catarr['cat_ID']['term_id']; |
0
|
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* Aliases wp_insert_category() with minimal args. |
|
159 |
* |
|
160 |
* If you want to update only some fields of an existing category, call this |
|
161 |
* function with only the new values set inside $catarr. |
|
162 |
* |
|
163 |
* @since 2.0.0 |
|
164 |
* |
|
165 |
* @param array $catarr The 'cat_ID' value is required. All other keys are optional. |
|
166 |
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure. |
|
167 |
*/ |
|
168 |
function wp_update_category($catarr) { |
|
169 |
$cat_ID = (int) $catarr['cat_ID']; |
|
170 |
|
|
171 |
if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) ) |
|
172 |
return false; |
|
173 |
|
|
174 |
// First, get all of the original fields |
|
175 |
$category = get_term( $cat_ID, 'category', ARRAY_A ); |
|
176 |
_make_cat_compat( $category ); |
|
177 |
|
|
178 |
// Escape data pulled from DB. |
|
179 |
$category = wp_slash($category); |
|
180 |
|
|
181 |
// Merge old and new fields with new fields overwriting old ones. |
|
182 |
$catarr = array_merge($category, $catarr); |
|
183 |
|
|
184 |
return wp_insert_category($catarr); |
|
185 |
} |
|
186 |
|
|
187 |
// |
|
188 |
// Tags |
|
189 |
// |
|
190 |
|
|
191 |
/** |
5
|
192 |
* Check whether a post tag with a given name exists. |
0
|
193 |
* |
|
194 |
* @since 2.3.0 |
|
195 |
* |
5
|
196 |
* @param int|string $tag_name |
|
197 |
* @return mixed |
0
|
198 |
*/ |
|
199 |
function tag_exists($tag_name) { |
|
200 |
return term_exists($tag_name, 'post_tag'); |
|
201 |
} |
|
202 |
|
|
203 |
/** |
5
|
204 |
* Add a new tag to the database if it does not already exist. |
0
|
205 |
* |
|
206 |
* @since 2.3.0 |
|
207 |
* |
5
|
208 |
* @param int|string $tag_name |
|
209 |
* @return array|WP_Error |
0
|
210 |
*/ |
|
211 |
function wp_create_tag($tag_name) { |
|
212 |
return wp_create_term( $tag_name, 'post_tag'); |
|
213 |
} |
|
214 |
|
|
215 |
/** |
5
|
216 |
* Get comma-separated list of tags available to edit. |
0
|
217 |
* |
|
218 |
* @since 2.3.0 |
|
219 |
* |
5
|
220 |
* @param int $post_id |
|
221 |
* @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. |
|
222 |
* @return string|bool|WP_Error |
0
|
223 |
*/ |
|
224 |
function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) { |
|
225 |
return get_terms_to_edit( $post_id, $taxonomy); |
|
226 |
} |
|
227 |
|
|
228 |
/** |
5
|
229 |
* Get comma-separated list of terms available to edit for the given post ID. |
0
|
230 |
* |
|
231 |
* @since 2.8.0 |
|
232 |
* |
5
|
233 |
* @param int $post_id |
|
234 |
* @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. |
|
235 |
* @return string|bool|WP_Error |
0
|
236 |
*/ |
|
237 |
function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) { |
|
238 |
$post_id = (int) $post_id; |
|
239 |
if ( !$post_id ) |
|
240 |
return false; |
|
241 |
|
5
|
242 |
$terms = get_object_term_cache( $post_id, $taxonomy ); |
|
243 |
if ( false === $terms ) { |
|
244 |
$terms = wp_get_object_terms( $post_id, $taxonomy ); |
|
245 |
wp_cache_add( $post_id, $terms, $taxonomy . '_relationships' ); |
|
246 |
} |
0
|
247 |
|
5
|
248 |
if ( ! $terms ) { |
|
249 |
return false; |
|
250 |
} |
|
251 |
if ( is_wp_error( $terms ) ) { |
|
252 |
return $terms; |
|
253 |
} |
|
254 |
$term_names = array(); |
|
255 |
foreach ( $terms as $term ) { |
|
256 |
$term_names[] = $term->name; |
|
257 |
} |
|
258 |
|
|
259 |
$terms_to_edit = esc_attr( join( ',', $term_names ) ); |
0
|
260 |
|
5
|
261 |
/** |
|
262 |
* Filter the comma-separated list of terms available to edit. |
|
263 |
* |
|
264 |
* @since 2.8.0 |
|
265 |
* |
|
266 |
* @see get_terms_to_edit() |
|
267 |
* |
|
268 |
* @param array $terms_to_edit An array of terms. |
|
269 |
* @param string $taxonomy The taxonomy for which to retrieve terms. Default 'post_tag'. |
|
270 |
*/ |
|
271 |
$terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy ); |
|
272 |
|
|
273 |
return $terms_to_edit; |
0
|
274 |
} |
|
275 |
|
|
276 |
/** |
5
|
277 |
* Add a new term to the database if it does not already exist. |
0
|
278 |
* |
|
279 |
* @since 2.8.0 |
|
280 |
* |
5
|
281 |
* @param int|string $tag_name |
|
282 |
* @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. |
|
283 |
* @return array|WP_Error |
0
|
284 |
*/ |
|
285 |
function wp_create_term($tag_name, $taxonomy = 'post_tag') { |
|
286 |
if ( $id = term_exists($tag_name, $taxonomy) ) |
|
287 |
return $id; |
|
288 |
|
|
289 |
return wp_insert_term($tag_name, $taxonomy); |
|
290 |
} |