author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Taxonomy Administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
// |
|
10 |
// Category |
|
11 |
// |
|
12 |
||
13 |
/** |
|
14 |
* {@internal Missing Short Description}} |
|
15 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
16 |
* @since 2.0.0 |
136 | 17 |
* |
18 |
* @param unknown_type $cat_name |
|
19 |
* @return unknown |
|
20 |
*/ |
|
21 |
function category_exists($cat_name, $parent = 0) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
22 |
$id = term_exists($cat_name, 'category', $parent); |
136 | 23 |
if ( is_array($id) ) |
24 |
$id = $id['term_id']; |
|
25 |
return $id; |
|
26 |
} |
|
27 |
||
28 |
/** |
|
29 |
* {@internal Missing Short Description}} |
|
30 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
* @since 2.0.0 |
136 | 32 |
* |
33 |
* @param unknown_type $id |
|
34 |
* @return unknown |
|
35 |
*/ |
|
36 |
function get_category_to_edit( $id ) { |
|
37 |
$category = get_category( $id, OBJECT, 'edit' ); |
|
38 |
return $category; |
|
39 |
} |
|
40 |
||
41 |
/** |
|
42 |
* {@internal Missing Short Description}} |
|
43 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
* @since 2.0.0 |
136 | 45 |
* |
46 |
* @param unknown_type $cat_name |
|
47 |
* @param unknown_type $parent |
|
48 |
* @return unknown |
|
49 |
*/ |
|
50 |
function wp_create_category( $cat_name, $parent = 0 ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
if ( $id = category_exists($cat_name, $parent) ) |
136 | 52 |
return $id; |
53 |
||
54 |
return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) ); |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* {@internal Missing Short Description}} |
|
59 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
* @since 2.0.0 |
136 | 61 |
* |
62 |
* @param unknown_type $categories |
|
63 |
* @param unknown_type $post_id |
|
64 |
* @return unknown |
|
65 |
*/ |
|
66 |
function wp_create_categories($categories, $post_id = '') { |
|
67 |
$cat_ids = array (); |
|
68 |
foreach ($categories as $category) { |
|
69 |
if ($id = category_exists($category)) |
|
70 |
$cat_ids[] = $id; |
|
71 |
else |
|
72 |
if ($id = wp_create_category($category)) |
|
73 |
$cat_ids[] = $id; |
|
74 |
} |
|
75 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
76 |
if ( $post_id ) |
136 | 77 |
wp_set_post_categories($post_id, $cat_ids); |
78 |
||
79 |
return $cat_ids; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
83 |
* Updates an existing Category or creates a new Category. |
136 | 84 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
85 |
* @since 2.0.0 |
136 | 86 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
* @param mixed $catarr See defaults below. Set 'cat_ID' to a non-zero value to update an existing category. The 'taxonomy' key was added in 3.0.0. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
88 |
* @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
* @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. |
136 | 90 |
*/ |
91 |
function wp_insert_category($catarr, $wp_error = false) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
$cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => ''); |
136 | 93 |
$catarr = wp_parse_args($catarr, $cat_defaults); |
94 |
extract($catarr, EXTR_SKIP); |
|
95 |
||
96 |
if ( trim( $cat_name ) == '' ) { |
|
97 |
if ( ! $wp_error ) |
|
98 |
return 0; |
|
99 |
else |
|
100 |
return new WP_Error( 'cat_name', __('You did not enter a category name.') ); |
|
101 |
} |
|
102 |
||
103 |
$cat_ID = (int) $cat_ID; |
|
104 |
||
105 |
// Are we updating or creating? |
|
106 |
if ( !empty ($cat_ID) ) |
|
107 |
$update = true; |
|
108 |
else |
|
109 |
$update = false; |
|
110 |
||
111 |
$name = $cat_name; |
|
112 |
$description = $category_description; |
|
113 |
$slug = $category_nicename; |
|
114 |
$parent = $category_parent; |
|
115 |
||
116 |
$parent = (int) $parent; |
|
117 |
if ( $parent < 0 ) |
|
118 |
$parent = 0; |
|
119 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
120 |
if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) ) |
136 | 121 |
$parent = 0; |
122 |
||
123 |
$args = compact('name', 'slug', 'parent', 'description'); |
|
124 |
||
125 |
if ( $update ) |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
126 |
$cat_ID = wp_update_term($cat_ID, $taxonomy, $args); |
136 | 127 |
else |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
128 |
$cat_ID = wp_insert_term($cat_name, $taxonomy, $args); |
136 | 129 |
|
130 |
if ( is_wp_error($cat_ID) ) { |
|
131 |
if ( $wp_error ) |
|
132 |
return $cat_ID; |
|
133 |
else |
|
134 |
return 0; |
|
135 |
} |
|
136 |
||
137 |
return $cat_ID['term_id']; |
|
138 |
} |
|
139 |
||
140 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
141 |
* Aliases wp_insert_category() with minimal args. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
142 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
143 |
* If you want to update only some fields of an existing category, call this |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
144 |
* function with only the new values set inside $catarr. |
136 | 145 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
146 |
* @since 2.0.0 |
136 | 147 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
148 |
* @param array $catarr The 'cat_ID' value is required. All other keys are optional. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
149 |
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure. |
136 | 150 |
*/ |
151 |
function wp_update_category($catarr) { |
|
152 |
$cat_ID = (int) $catarr['cat_ID']; |
|
153 |
||
154 |
if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) ) |
|
155 |
return false; |
|
156 |
||
157 |
// First, get all of the original fields |
|
158 |
$category = get_category($cat_ID, ARRAY_A); |
|
159 |
||
160 |
// Escape data pulled from DB. |
|
161 |
$category = add_magic_quotes($category); |
|
162 |
||
163 |
// Merge old and new fields with new fields overwriting old ones. |
|
164 |
$catarr = array_merge($category, $catarr); |
|
165 |
||
166 |
return wp_insert_category($catarr); |
|
167 |
} |
|
168 |
||
169 |
// |
|
170 |
// Tags |
|
171 |
// |
|
172 |
||
173 |
/** |
|
174 |
* {@internal Missing Short Description}} |
|
175 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
176 |
* @since 2.3.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
177 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
178 |
* @param unknown_type $tag_name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
179 |
* @return unknown |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
180 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
181 |
function tag_exists($tag_name) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
182 |
return term_exists($tag_name, 'post_tag'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
183 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
184 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
185 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
186 |
* {@internal Missing Short Description}} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
187 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
188 |
* @since 2.3.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
189 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
190 |
* @param unknown_type $tag_name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
191 |
* @return unknown |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
192 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
193 |
function wp_create_tag($tag_name) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
194 |
return wp_create_term( $tag_name, 'post_tag'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
195 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
196 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
197 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
198 |
* {@internal Missing Short Description}} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
199 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
200 |
* @since 2.3.0 |
136 | 201 |
* |
202 |
* @param unknown_type $post_id |
|
203 |
* @return unknown |
|
204 |
*/ |
|
205 |
function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) { |
|
206 |
return get_terms_to_edit( $post_id, $taxonomy); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* {@internal Missing Short Description}} |
|
211 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
212 |
* @since 2.8.0 |
136 | 213 |
* |
214 |
* @param unknown_type $post_id |
|
215 |
* @return unknown |
|
216 |
*/ |
|
217 |
function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) { |
|
218 |
$post_id = (int) $post_id; |
|
219 |
if ( !$post_id ) |
|
220 |
return false; |
|
221 |
||
222 |
$tags = wp_get_post_terms($post_id, $taxonomy, array()); |
|
223 |
||
224 |
if ( !$tags ) |
|
225 |
return false; |
|
226 |
||
227 |
if ( is_wp_error($tags) ) |
|
228 |
return $tags; |
|
229 |
||
230 |
foreach ( $tags as $tag ) |
|
231 |
$tag_names[] = $tag->name; |
|
232 |
$tags_to_edit = join( ',', $tag_names ); |
|
233 |
$tags_to_edit = esc_attr( $tags_to_edit ); |
|
234 |
$tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy ); |
|
235 |
||
236 |
return $tags_to_edit; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* {@internal Missing Short Description}} |
|
241 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
242 |
* @since 2.8.0 |
136 | 243 |
* |
244 |
* @param unknown_type $tag_name |
|
245 |
* @return unknown |
|
246 |
*/ |
|
247 |
function wp_create_term($tag_name, $taxonomy = 'post_tag') { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
if ( $id = term_exists($tag_name, $taxonomy) ) |
136 | 249 |
return $id; |
250 |
||
251 |
return wp_insert_term($tag_name, $taxonomy); |
|
252 |
} |