author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Taxonomy API |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage Taxonomy |
|
7 |
*/ |
|
8 |
||
9 |
// |
|
16 | 10 |
// Taxonomy registration. |
0 | 11 |
// |
12 |
||
13 |
/** |
|
14 |
* Creates the initial taxonomies. |
|
15 |
* |
|
16 |
* This function fires twice: in wp-settings.php before plugins are loaded (for |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* backward compatibility reasons), and again on the {@see 'init'} action. We must |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* avoid registering rewrite rules before the {@see 'init'} action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* |
16 | 22 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
0 | 23 |
*/ |
24 |
function create_initial_taxonomies() { |
|
25 |
global $wp_rewrite; |
|
26 |
||
27 |
if ( ! did_action( 'init' ) ) { |
|
9 | 28 |
$rewrite = array( |
29 |
'category' => false, |
|
30 |
'post_tag' => false, |
|
31 |
'post_format' => false, |
|
32 |
); |
|
0 | 33 |
} else { |
5 | 34 |
|
35 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* Filters the post formats rewrite base. |
5 | 37 |
* |
38 |
* @since 3.1.0 |
|
39 |
* |
|
40 |
* @param string $context Context of the rewrite base. Default 'type'. |
|
41 |
*/ |
|
0 | 42 |
$post_format_base = apply_filters( 'post_format_rewrite_base', 'type' ); |
9 | 43 |
$rewrite = array( |
44 |
'category' => array( |
|
0 | 45 |
'hierarchical' => true, |
9 | 46 |
'slug' => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category', |
47 |
'with_front' => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(), |
|
48 |
'ep_mask' => EP_CATEGORIES, |
|
0 | 49 |
), |
9 | 50 |
'post_tag' => array( |
5 | 51 |
'hierarchical' => false, |
9 | 52 |
'slug' => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag', |
53 |
'with_front' => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(), |
|
54 |
'ep_mask' => EP_TAGS, |
|
0 | 55 |
), |
56 |
'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false, |
|
57 |
); |
|
58 |
} |
|
59 |
||
9 | 60 |
register_taxonomy( |
61 |
'category', |
|
62 |
'post', |
|
63 |
array( |
|
64 |
'hierarchical' => true, |
|
65 |
'query_var' => 'category_name', |
|
66 |
'rewrite' => $rewrite['category'], |
|
67 |
'public' => true, |
|
68 |
'show_ui' => true, |
|
69 |
'show_admin_column' => true, |
|
70 |
'_builtin' => true, |
|
71 |
'capabilities' => array( |
|
72 |
'manage_terms' => 'manage_categories', |
|
73 |
'edit_terms' => 'edit_categories', |
|
74 |
'delete_terms' => 'delete_categories', |
|
75 |
'assign_terms' => 'assign_categories', |
|
76 |
), |
|
77 |
'show_in_rest' => true, |
|
78 |
'rest_base' => 'categories', |
|
79 |
'rest_controller_class' => 'WP_REST_Terms_Controller', |
|
80 |
) |
|
81 |
); |
|
82 |
||
83 |
register_taxonomy( |
|
84 |
'post_tag', |
|
85 |
'post', |
|
86 |
array( |
|
87 |
'hierarchical' => false, |
|
88 |
'query_var' => 'tag', |
|
89 |
'rewrite' => $rewrite['post_tag'], |
|
90 |
'public' => true, |
|
91 |
'show_ui' => true, |
|
92 |
'show_admin_column' => true, |
|
93 |
'_builtin' => true, |
|
94 |
'capabilities' => array( |
|
95 |
'manage_terms' => 'manage_post_tags', |
|
96 |
'edit_terms' => 'edit_post_tags', |
|
97 |
'delete_terms' => 'delete_post_tags', |
|
98 |
'assign_terms' => 'assign_post_tags', |
|
99 |
), |
|
100 |
'show_in_rest' => true, |
|
101 |
'rest_base' => 'tags', |
|
102 |
'rest_controller_class' => 'WP_REST_Terms_Controller', |
|
103 |
) |
|
104 |
); |
|
105 |
||
106 |
register_taxonomy( |
|
107 |
'nav_menu', |
|
108 |
'nav_menu_item', |
|
109 |
array( |
|
110 |
'public' => false, |
|
111 |
'hierarchical' => false, |
|
112 |
'labels' => array( |
|
113 |
'name' => __( 'Navigation Menus' ), |
|
114 |
'singular_name' => __( 'Navigation Menu' ), |
|
115 |
), |
|
116 |
'query_var' => false, |
|
117 |
'rewrite' => false, |
|
118 |
'show_ui' => false, |
|
119 |
'_builtin' => true, |
|
120 |
'show_in_nav_menus' => false, |
|
121 |
) |
|
122 |
); |
|
123 |
||
124 |
register_taxonomy( |
|
125 |
'link_category', |
|
126 |
'link', |
|
127 |
array( |
|
128 |
'hierarchical' => false, |
|
129 |
'labels' => array( |
|
130 |
'name' => __( 'Link Categories' ), |
|
131 |
'singular_name' => __( 'Link Category' ), |
|
132 |
'search_items' => __( 'Search Link Categories' ), |
|
133 |
'popular_items' => null, |
|
134 |
'all_items' => __( 'All Link Categories' ), |
|
135 |
'edit_item' => __( 'Edit Link Category' ), |
|
136 |
'update_item' => __( 'Update Link Category' ), |
|
137 |
'add_new_item' => __( 'Add New Link Category' ), |
|
138 |
'new_item_name' => __( 'New Link Category Name' ), |
|
139 |
'separate_items_with_commas' => null, |
|
140 |
'add_or_remove_items' => null, |
|
141 |
'choose_from_most_used' => null, |
|
18 | 142 |
'back_to_items' => __( '← Go to Link Categories' ), |
9 | 143 |
), |
144 |
'capabilities' => array( |
|
145 |
'manage_terms' => 'manage_links', |
|
146 |
'edit_terms' => 'manage_links', |
|
147 |
'delete_terms' => 'manage_links', |
|
148 |
'assign_terms' => 'manage_links', |
|
149 |
), |
|
150 |
'query_var' => false, |
|
151 |
'rewrite' => false, |
|
152 |
'public' => false, |
|
153 |
'show_ui' => true, |
|
154 |
'_builtin' => true, |
|
155 |
) |
|
156 |
); |
|
157 |
||
158 |
register_taxonomy( |
|
159 |
'post_format', |
|
160 |
'post', |
|
161 |
array( |
|
162 |
'public' => true, |
|
163 |
'hierarchical' => false, |
|
164 |
'labels' => array( |
|
165 |
'name' => _x( 'Formats', 'post format' ), |
|
166 |
'singular_name' => _x( 'Format', 'post format' ), |
|
167 |
), |
|
168 |
'query_var' => true, |
|
169 |
'rewrite' => $rewrite['post_format'], |
|
170 |
'show_ui' => false, |
|
171 |
'_builtin' => true, |
|
172 |
'show_in_nav_menus' => current_theme_supports( 'post-formats' ), |
|
173 |
) |
|
174 |
); |
|
18 | 175 |
|
176 |
register_taxonomy( |
|
177 |
'wp_theme', |
|
178 |
array( 'wp_template' ), |
|
179 |
array( |
|
180 |
'public' => false, |
|
181 |
'hierarchical' => false, |
|
182 |
'labels' => array( |
|
183 |
'name' => __( 'Themes' ), |
|
184 |
'singular_name' => __( 'Theme' ), |
|
185 |
), |
|
186 |
'query_var' => false, |
|
187 |
'rewrite' => false, |
|
188 |
'show_ui' => false, |
|
189 |
'_builtin' => true, |
|
190 |
'show_in_nav_menus' => false, |
|
191 |
'show_in_rest' => false, |
|
192 |
) |
|
193 |
); |
|
0 | 194 |
} |
195 |
||
196 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
* Retrieves a list of registered taxonomy names or objects. |
0 | 198 |
* |
199 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* @param array $args Optional. An array of `key => value` arguments to match against the taxonomy objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
* @param string $output Optional. The type of output to return in the array. Accepts either taxonomy 'names' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
* or 'objects'. Default 'names'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
* @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* one element from the array needs to match; 'and' means all elements must match. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
* Default 'and'. |
9 | 210 |
* @return string[]|WP_Taxonomy[] An array of taxonomy names or objects. |
0 | 211 |
*/ |
212 |
function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) { |
|
213 |
global $wp_taxonomies; |
|
214 |
||
16 | 215 |
$field = ( 'names' === $output ) ? 'name' : false; |
9 | 216 |
|
217 |
return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field ); |
|
0 | 218 |
} |
219 |
||
220 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* Return the names or objects of the taxonomies which are registered for the requested object or object type, such as |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* a post object or post type name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
* Example: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* $taxonomies = get_object_taxonomies( 'post' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
* This results in: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
* Array( 'category', 'post_tag' ) |
5 | 231 |
* |
0 | 232 |
* @since 2.3.0 |
233 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
* |
16 | 236 |
* @param string|string[]|WP_Post $object Name of the type of taxonomy object, or an object (row from posts) |
237 |
* @param string $output Optional. The type of output to return in the array. Accepts either |
|
238 |
* 'names' or 'objects'. Default 'names'. |
|
239 |
* @return string[]|WP_Taxonomy[] The names or objects of all taxonomies of `$object_type`. |
|
0 | 240 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
function get_object_taxonomies( $object, $output = 'names' ) { |
0 | 242 |
global $wp_taxonomies; |
243 |
||
9 | 244 |
if ( is_object( $object ) ) { |
16 | 245 |
if ( 'attachment' === $object->post_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
return get_attachment_taxonomies( $object, $output ); |
9 | 247 |
} |
0 | 248 |
$object = $object->post_type; |
249 |
} |
|
250 |
||
251 |
$object = (array) $object; |
|
252 |
||
253 |
$taxonomies = array(); |
|
254 |
foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) { |
|
9 | 255 |
if ( array_intersect( $object, (array) $tax_obj->object_type ) ) { |
16 | 256 |
if ( 'names' === $output ) { |
0 | 257 |
$taxonomies[] = $tax_name; |
9 | 258 |
} else { |
0 | 259 |
$taxonomies[ $tax_name ] = $tax_obj; |
9 | 260 |
} |
0 | 261 |
} |
262 |
} |
|
263 |
||
264 |
return $taxonomies; |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Retrieves the taxonomy object of $taxonomy. |
|
269 |
* |
|
270 |
* The get_taxonomy function will first check that the parameter string given |
|
271 |
* is a taxonomy object and if it is, it will return it. |
|
272 |
* |
|
273 |
* @since 2.3.0 |
|
274 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* @param string $taxonomy Name of taxonomy object to return. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
* @return WP_Taxonomy|false The Taxonomy Object or false if $taxonomy doesn't exist. |
0 | 279 |
*/ |
280 |
function get_taxonomy( $taxonomy ) { |
|
281 |
global $wp_taxonomies; |
|
282 |
||
9 | 283 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
0 | 284 |
return false; |
9 | 285 |
} |
286 |
||
287 |
return $wp_taxonomies[ $taxonomy ]; |
|
0 | 288 |
} |
289 |
||
290 |
/** |
|
9 | 291 |
* Determines whether the taxonomy name exists. |
0 | 292 |
* |
293 |
* Formerly is_taxonomy(), introduced in 2.3.0. |
|
294 |
* |
|
9 | 295 |
* For more information on this and similar theme functions, check out |
296 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
297 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
298 |
* |
|
0 | 299 |
* @since 3.0.0 |
300 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* @param string $taxonomy Name of taxonomy object. |
0 | 304 |
* @return bool Whether the taxonomy exists. |
305 |
*/ |
|
306 |
function taxonomy_exists( $taxonomy ) { |
|
307 |
global $wp_taxonomies; |
|
308 |
||
9 | 309 |
return isset( $wp_taxonomies[ $taxonomy ] ); |
0 | 310 |
} |
311 |
||
312 |
/** |
|
9 | 313 |
* Determines whether the taxonomy object is hierarchical. |
0 | 314 |
* |
315 |
* Checks to make sure that the taxonomy is an object first. Then Gets the |
|
316 |
* object, and finally returns the hierarchical value in the object. |
|
317 |
* |
|
318 |
* A false return value might also mean that the taxonomy does not exist. |
|
319 |
* |
|
9 | 320 |
* For more information on this and similar theme functions, check out |
321 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
322 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
323 |
* |
|
0 | 324 |
* @since 2.3.0 |
325 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @param string $taxonomy Name of taxonomy object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* @return bool Whether the taxonomy is hierarchical. |
0 | 328 |
*/ |
9 | 329 |
function is_taxonomy_hierarchical( $taxonomy ) { |
330 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
|
0 | 331 |
return false; |
9 | 332 |
} |
333 |
||
334 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
0 | 335 |
return $taxonomy->hierarchical; |
336 |
} |
|
337 |
||
338 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* Creates or modifies a taxonomy object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* Note: Do not use before the {@see 'init'} hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* A simple function for creating or modifying a taxonomy object based on |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* the parameters given. If modifying an existing taxonomy object, note |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* that the `$object_type` value from the original registration will be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* overwritten. |
0 | 347 |
* |
348 |
* @since 2.3.0 |
|
5 | 349 |
* @since 4.2.0 Introduced `show_in_quick_edit` argument. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* @since 4.4.0 The `show_ui` argument is now enforced on the term editing screen. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
* @since 4.4.0 The `public` argument now controls whether the taxonomy can be queried on the front end. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
* @since 4.5.0 Introduced `publicly_queryable` argument. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
* @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* arguments to register the Taxonomy in REST API. |
9 | 355 |
* @since 5.1.0 Introduced `meta_box_sanitize_cb` argument. |
16 | 356 |
* @since 5.4.0 Added the registered taxonomy object as a return value. |
357 |
* @since 5.5.0 Introduced `default_term` argument. |
|
5 | 358 |
* |
359 |
* @global array $wp_taxonomies Registered taxonomies. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* @param string $taxonomy Taxonomy key, must not exceed 32 characters. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* @param array|string $object_type Object type or array of object types with which the taxonomy should be associated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* @param array|string $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* Optional. Array or query string of arguments for registering a taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* |
18 | 366 |
* @type string[] $labels An array of labels for this taxonomy. By default, Tag labels are |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
* used for non-hierarchical taxonomies, and Category labels are used |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* for hierarchical taxonomies. See accepted values in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* get_taxonomy_labels(). Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
* @type string $description A short descriptive summary of what the taxonomy is for. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
* @type bool $public Whether a taxonomy is intended for use publicly either via |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* the admin interface or by front-end users. The default settings |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
* are inherited from `$public`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
* @type bool $publicly_queryable Whether the taxonomy is publicly queryable. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
* If not set, the default is inherited from `$public` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* @type bool $hierarchical Whether the taxonomy is hierarchical. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
* @type bool $show_ui Whether to generate and allow a UI for managing terms in this taxonomy in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
* the admin. If not set, the default is inherited from `$public` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
* (default true). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
* @type bool $show_in_menu Whether to show the taxonomy in the admin menu. If true, the taxonomy is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
* shown as a submenu of the object type menu. If false, no menu is shown. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
* `$show_ui` must be true. If not set, default is inherited from `$show_ui` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* (default true). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
* @type bool $show_in_nav_menus Makes this taxonomy available for selection in navigation menus. If not |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
* set, the default is inherited from `$public` (default true). |
16 | 387 |
* @type bool $show_in_rest Whether to include the taxonomy in the REST API. Set this to true |
388 |
* for the taxonomy to be available in the block editor. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
* @type string $rest_base To change the base url of REST API route. Default is $taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
* @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
* @type bool $show_tagcloud Whether to list the taxonomy in the Tag Cloud Widget controls. If not set, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* the default is inherited from `$show_ui` (default true). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* @type bool $show_in_quick_edit Whether to show the taxonomy in the quick/bulk edit panel. It not set, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* the default is inherited from `$show_ui` (default true). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* @type bool $show_admin_column Whether to display a column for the taxonomy on its post type listing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* screens. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* @type bool|callable $meta_box_cb Provide a callback function for the meta box display. If not set, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* post_categories_meta_box() is used for hierarchical taxonomies, and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* post_tags_meta_box() is used for non-hierarchical. If false, no meta |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* box is shown. |
9 | 401 |
* @type callable $meta_box_sanitize_cb Callback function for sanitizing taxonomy data saved from a meta |
402 |
* box. If no callback is defined, an appropriate one is determined |
|
403 |
* based on the value of `$meta_box_cb`. |
|
18 | 404 |
* @type string[] $capabilities { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* Array of capabilities for this taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* @type string $manage_terms Default 'manage_categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @type string $edit_terms Default 'manage_categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* @type string $delete_terms Default 'manage_categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
* @type string $assign_terms Default 'edit_posts'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
* @type bool|array $rewrite { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
* @type string $slug Customize the permastruct slug. Default `$taxonomy` key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
* @type bool $with_front Should the permastruct be prepended with WP_Rewrite::$front. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
* @type bool $hierarchical Either hierarchical rewrite tag or not. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* @type int $ep_mask Assign an endpoint mask. Default `EP_NONE`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
* } |
16 | 421 |
* @type string|bool $query_var Sets the query var key for this taxonomy. Default `$taxonomy` key. If |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
* false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
* string, the query `?{query_var}={term_slug}` will be valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* @type callable $update_count_callback Works much like a hook, in that it will be called when the count is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
* updated. Default _update_post_term_count() for taxonomies attached |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* to post types, which confirms that the objects are published before |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* counting them. Default _update_generic_term_count() for taxonomies |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
* attached to other object types, such as users. |
16 | 429 |
* @type string|array $default_term { |
430 |
* Default term to be used for the taxonomy. |
|
431 |
* |
|
432 |
* @type string $name Name of default term. |
|
433 |
* @type string $slug Slug for default term. Default empty. |
|
434 |
* @type string $description Description for default term. Default empty. |
|
435 |
* } |
|
18 | 436 |
* @type bool $sort Whether terms in this taxonomy should be sorted in the order they are |
437 |
* provided to `wp_set_object_terms()`. Default null which equates to false. |
|
438 |
* @type array $args Array of arguments to automatically use inside `wp_get_object_terms()` |
|
439 |
* for this taxonomy. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
* @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* } |
16 | 443 |
* @return WP_Taxonomy|WP_Error The registered taxonomy object on success, WP_Error object on failure. |
0 | 444 |
*/ |
445 |
function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
global $wp_taxonomies; |
0 | 447 |
|
9 | 448 |
if ( ! is_array( $wp_taxonomies ) ) { |
0 | 449 |
$wp_taxonomies = array(); |
9 | 450 |
} |
0 | 451 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
$args = wp_parse_args( $args ); |
0 | 453 |
|
5 | 454 |
if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
_doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2.0' ); |
5 | 456 |
return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) ); |
457 |
} |
|
0 | 458 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
$taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
$taxonomy_object->add_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
$wp_taxonomies[ $taxonomy ] = $taxonomy_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
$taxonomy_object->add_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
|
16 | 466 |
// Add default term. |
467 |
if ( ! empty( $taxonomy_object->default_term ) ) { |
|
468 |
$term = term_exists( $taxonomy_object->default_term['name'], $taxonomy ); |
|
469 |
if ( $term ) { |
|
470 |
update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] ); |
|
471 |
} else { |
|
472 |
$term = wp_insert_term( |
|
473 |
$taxonomy_object->default_term['name'], |
|
474 |
$taxonomy, |
|
475 |
array( |
|
476 |
'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ), |
|
477 |
'description' => $taxonomy_object->default_term['description'], |
|
478 |
) |
|
479 |
); |
|
480 |
||
481 |
// Update `term_id` in options. |
|
482 |
if ( ! is_wp_error( $term ) ) { |
|
483 |
update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] ); |
|
484 |
} |
|
485 |
} |
|
486 |
} |
|
487 |
||
5 | 488 |
/** |
489 |
* Fires after a taxonomy is registered. |
|
490 |
* |
|
491 |
* @since 3.3.0 |
|
492 |
* |
|
493 |
* @param string $taxonomy Taxonomy slug. |
|
494 |
* @param array|string $object_type Object type or array of object types. |
|
495 |
* @param array $args Array of taxonomy registration arguments. |
|
496 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object ); |
16 | 498 |
|
499 |
return $taxonomy_object; |
|
0 | 500 |
} |
501 |
||
502 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
* Unregisters a taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
* Can not be used to unregister built-in taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
* @global WP $wp Current WordPress environment instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
* @global array $wp_taxonomies List of taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
* @param string $taxonomy Taxonomy name. |
18 | 513 |
* @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
function unregister_taxonomy( $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
$taxonomy_object = get_taxonomy( $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
// Do not allow unregistering internal taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
if ( $taxonomy_object->_builtin ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
global $wp_taxonomies; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
$taxonomy_object->remove_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
$taxonomy_object->remove_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
|
16 | 532 |
// Remove custom taxonomy default term option. |
533 |
if ( ! empty( $taxonomy_object->default_term ) ) { |
|
534 |
delete_option( 'default_term_' . $taxonomy_object->name ); |
|
535 |
} |
|
536 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
// Remove the taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
unset( $wp_taxonomies[ $taxonomy ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* Fires after a taxonomy is unregistered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
* @param string $taxonomy Taxonomy name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
do_action( 'unregistered_taxonomy', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
548 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
* Builds an object with all taxonomy labels out of a taxonomy object. |
0 | 554 |
* |
555 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* @since 4.3.0 Added the `no_terms` label. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
* @since 4.4.0 Added the `items_list_navigation` and `items_list` labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
* @since 4.9.0 Added the `most_used` and `back_to_items` labels. |
18 | 559 |
* @since 5.7.0 Added the `filter_by_item` label. |
560 |
* @since 5.8.0 Added the `item_link` and `item_link_description` labels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
* @param WP_Taxonomy $tax Taxonomy object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
* @return object { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* Taxonomy labels object. The first default value is for non-hierarchical taxonomies |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* (like tags) and the second one is for hierarchical taxonomies (like categories). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
* @type string $name General name for the taxonomy, usually plural. The same |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
* as and overridden by `$tax->label`. Default 'Tags'/'Categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
* @type string $singular_name Name for one object of this taxonomy. Default 'Tag'/'Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
* @type string $search_items Default 'Search Tags'/'Search Categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
* @type string $popular_items This label is only used for non-hierarchical taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* Default 'Popular Tags'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* @type string $all_items Default 'All Tags'/'All Categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @type string $parent_item This label is only used for hierarchical taxonomies. Default |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
* 'Parent Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
* @type string $parent_item_colon The same as `parent_item`, but with colon `:` in the end. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
* @type string $edit_item Default 'Edit Tag'/'Edit Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
* @type string $view_item Default 'View Tag'/'View Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
* @type string $update_item Default 'Update Tag'/'Update Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
* @type string $add_new_item Default 'Add New Tag'/'Add New Category'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* @type string $new_item_name Default 'New Tag Name'/'New Category Name'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* @type string $separate_items_with_commas This label is only used for non-hierarchical taxonomies. Default |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
* 'Separate tags with commas', used in the meta box. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
* @type string $add_or_remove_items This label is only used for non-hierarchical taxonomies. Default |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* 'Add or remove tags', used in the meta box when JavaScript |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
* is disabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
* @type string $choose_from_most_used This label is only used on non-hierarchical taxonomies. Default |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
* 'Choose from the most used tags', used in the meta box. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
* @type string $not_found Default 'No tags found'/'No categories found', used in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* the meta box and taxonomy list table. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
* @type string $no_terms Default 'No tags'/'No categories', used in the posts and media |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
* list tables. |
18 | 593 |
* @type string $filter_by_item This label is only used for hierarchical taxonomies. Default |
594 |
* 'Filter by category', used in the posts list table. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
* @type string $items_list_navigation Label for the table pagination hidden heading. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
* @type string $items_list Label for the table hidden heading. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
* @type string $most_used Title for the Most Used tab. Default 'Most Used'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
* @type string $back_to_items Label displayed after a term has been updated. |
18 | 599 |
* @type string $item_link Used in the block editor. Title for a navigation link block variation. |
600 |
* Default 'Tag Link'/'Category Link'. |
|
601 |
* @type string $item_link_description Used in the block editor. Description for a navigation link block |
|
602 |
* variation. Default 'A link to a tag'/'A link to a category'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
* } |
0 | 604 |
*/ |
605 |
function get_taxonomy_labels( $tax ) { |
|
606 |
$tax->labels = (array) $tax->labels; |
|
607 |
||
9 | 608 |
if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) { |
0 | 609 |
$tax->labels['separate_items_with_commas'] = $tax->helps; |
9 | 610 |
} |
611 |
||
612 |
if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) { |
|
0 | 613 |
$tax->labels['not_found'] = $tax->no_tagcloud; |
9 | 614 |
} |
0 | 615 |
|
616 |
$nohier_vs_hier_defaults = array( |
|
9 | 617 |
'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ), |
618 |
'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ), |
|
619 |
'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ), |
|
620 |
'popular_items' => array( __( 'Popular Tags' ), null ), |
|
621 |
'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ), |
|
622 |
'parent_item' => array( null, __( 'Parent Category' ) ), |
|
623 |
'parent_item_colon' => array( null, __( 'Parent Category:' ) ), |
|
624 |
'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ), |
|
625 |
'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ), |
|
626 |
'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ), |
|
627 |
'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ), |
|
628 |
'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ), |
|
0 | 629 |
'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ), |
9 | 630 |
'add_or_remove_items' => array( __( 'Add or remove tags' ), null ), |
631 |
'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ), |
|
632 |
'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ), |
|
633 |
'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ), |
|
18 | 634 |
'filter_by_item' => array( null, __( 'Filter by category' ) ), |
9 | 635 |
'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ), |
636 |
'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ), |
|
16 | 637 |
/* translators: Tab heading when selecting from the most used terms. */ |
9 | 638 |
'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ), |
18 | 639 |
'back_to_items' => array( __( '← Go to Tags' ), __( '← Go to Categories' ) ), |
640 |
'item_link' => array( |
|
641 |
_x( 'Tag Link', 'navigation link block title' ), |
|
642 |
_x( 'Category Link', 'navigation link block description' ), |
|
643 |
), |
|
644 |
'item_link_description' => array( |
|
645 |
_x( 'A link to a tag.', 'navigation link block description' ), |
|
646 |
_x( 'A link to a category.', 'navigation link block description' ), |
|
647 |
), |
|
0 | 648 |
); |
18 | 649 |
|
0 | 650 |
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
651 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
$labels = _get_custom_object_labels( $tax, $nohier_vs_hier_defaults ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
$taxonomy = $tax->name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
$default_labels = clone $labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* Filters the labels of a specific taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* |
18 | 663 |
* Possible hook names include: |
664 |
* |
|
665 |
* - `taxonomy_labels_category` |
|
666 |
* - `taxonomy_labels_post_tag` |
|
667 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* @see get_taxonomy_labels() for the full list of taxonomy labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* @param object $labels Object with labels for the taxonomy as member variables. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
$labels = apply_filters( "taxonomy_labels_{$taxonomy}", $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
// Ensure that the filtered labels contain all required default values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
$labels = (object) array_merge( (array) $default_labels, (array) $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
return $labels; |
0 | 680 |
} |
681 |
||
682 |
/** |
|
683 |
* Add an already registered taxonomy to an object type. |
|
684 |
* |
|
685 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
* @param string $taxonomy Name of taxonomy object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* @param string $object_type Name of the object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* @return bool True if successful, false if not. |
0 | 692 |
*/ |
9 | 693 |
function register_taxonomy_for_object_type( $taxonomy, $object_type ) { |
0 | 694 |
global $wp_taxonomies; |
695 |
||
9 | 696 |
if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { |
0 | 697 |
return false; |
9 | 698 |
} |
699 |
||
700 |
if ( ! get_post_type_object( $object_type ) ) { |
|
0 | 701 |
return false; |
9 | 702 |
} |
703 |
||
16 | 704 |
if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ) ) { |
9 | 705 |
$wp_taxonomies[ $taxonomy ]->object_type[] = $object_type; |
706 |
} |
|
0 | 707 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
// Filter out empties. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
709 |
$wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
710 |
|
9 | 711 |
/** |
712 |
* Fires after a taxonomy is registered for an object type. |
|
713 |
* |
|
714 |
* @since 5.1.0 |
|
715 |
* |
|
716 |
* @param string $taxonomy Taxonomy name. |
|
717 |
* @param string $object_type Name of the object type. |
|
718 |
*/ |
|
719 |
do_action( 'registered_taxonomy_for_object_type', $taxonomy, $object_type ); |
|
720 |
||
0 | 721 |
return true; |
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* Remove an already registered taxonomy from an object type. |
|
726 |
* |
|
727 |
* @since 3.7.0 |
|
728 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
* @global array $wp_taxonomies The registered taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* |
0 | 731 |
* @param string $taxonomy Name of taxonomy object. |
732 |
* @param string $object_type Name of the object type. |
|
733 |
* @return bool True if successful, false if not. |
|
734 |
*/ |
|
735 |
function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) { |
|
736 |
global $wp_taxonomies; |
|
737 |
||
9 | 738 |
if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) { |
0 | 739 |
return false; |
9 | 740 |
} |
741 |
||
742 |
if ( ! get_post_type_object( $object_type ) ) { |
|
0 | 743 |
return false; |
9 | 744 |
} |
0 | 745 |
|
746 |
$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); |
|
9 | 747 |
if ( false === $key ) { |
0 | 748 |
return false; |
9 | 749 |
} |
0 | 750 |
|
751 |
unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] ); |
|
9 | 752 |
|
753 |
/** |
|
754 |
* Fires after a taxonomy is unregistered for an object type. |
|
755 |
* |
|
756 |
* @since 5.1.0 |
|
757 |
* |
|
758 |
* @param string $taxonomy Taxonomy name. |
|
759 |
* @param string $object_type Name of the object type. |
|
760 |
*/ |
|
761 |
do_action( 'unregistered_taxonomy_for_object_type', $taxonomy, $object_type ); |
|
762 |
||
0 | 763 |
return true; |
764 |
} |
|
765 |
||
766 |
// |
|
16 | 767 |
// Term API. |
0 | 768 |
// |
769 |
||
770 |
/** |
|
771 |
* Retrieve object_ids of valid taxonomy and term. |
|
772 |
* |
|
18 | 773 |
* The strings of $taxonomies must exist before this function will continue. |
774 |
* On failure of finding a valid taxonomy, it will return a WP_Error class, |
|
775 |
* kind of like Exceptions in PHP 5, except you can't catch them. Even so, |
|
776 |
* you can still test for the WP_Error class and get the error message. |
|
0 | 777 |
* |
778 |
* The $terms aren't checked the same as $taxonomies, but still need to exist |
|
779 |
* for $object_ids to be returned. |
|
780 |
* |
|
781 |
* It is possible to change the order that object_ids is returned by either |
|
782 |
* using PHP sort family functions or using the database by using $args with |
|
783 |
* either ASC or DESC array. The value should be in the key named 'order'. |
|
784 |
* |
|
785 |
* @since 2.3.0 |
|
786 |
* |
|
5 | 787 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 788 |
* |
16 | 789 |
* @param int|array $term_ids Term ID or array of term IDs of terms that will be used. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
* @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
* @param array|string $args Change the order of the object_ids, either ASC or DESC. |
18 | 792 |
* @return array|WP_Error An array of $object_ids on success, WP_Error if the taxonomy does not exist. |
0 | 793 |
*/ |
794 |
function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { |
|
795 |
global $wpdb; |
|
796 |
||
5 | 797 |
if ( ! is_array( $term_ids ) ) { |
0 | 798 |
$term_ids = array( $term_ids ); |
5 | 799 |
} |
800 |
if ( ! is_array( $taxonomies ) ) { |
|
0 | 801 |
$taxonomies = array( $taxonomies ); |
5 | 802 |
} |
0 | 803 |
foreach ( (array) $taxonomies as $taxonomy ) { |
5 | 804 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
5 | 806 |
} |
0 | 807 |
} |
808 |
||
809 |
$defaults = array( 'order' => 'ASC' ); |
|
9 | 810 |
$args = wp_parse_args( $args, $defaults ); |
5 | 811 |
|
16 | 812 |
$order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; |
0 | 813 |
|
9 | 814 |
$term_ids = array_map( 'intval', $term_ids ); |
0 | 815 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; |
9 | 817 |
$term_ids = "'" . implode( "', '", $term_ids ) . "'"; |
0 | 818 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
$sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
$last_changed = wp_cache_get_last_changed( 'terms' ); |
9 | 822 |
$cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed"; |
823 |
$cache = wp_cache_get( $cache_key, 'terms' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
if ( false === $cache ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
$object_ids = $wpdb->get_col( $sql ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
wp_cache_set( $cache_key, $object_ids, 'terms' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
$object_ids = (array) $cache; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
} |
0 | 830 |
|
9 | 831 |
if ( ! $object_ids ) { |
0 | 832 |
return array(); |
5 | 833 |
} |
0 | 834 |
return $object_ids; |
835 |
} |
|
836 |
||
837 |
/** |
|
838 |
* Given a taxonomy query, generates SQL to be appended to a main query. |
|
839 |
* |
|
840 |
* @since 3.1.0 |
|
841 |
* |
|
842 |
* @see WP_Tax_Query |
|
843 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
* @param array $tax_query A compact tax query |
0 | 845 |
* @param string $primary_table |
846 |
* @param string $primary_id_column |
|
847 |
* @return array |
|
848 |
*/ |
|
849 |
function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { |
|
850 |
$tax_query_obj = new WP_Tax_Query( $tax_query ); |
|
851 |
return $tax_query_obj->get_sql( $primary_table, $primary_id_column ); |
|
852 |
} |
|
853 |
||
854 |
/** |
|
855 |
* Get all Term data from database by Term ID. |
|
856 |
* |
|
857 |
* The usage of the get_term function is to apply filters to a term object. It |
|
858 |
* is possible to get a term object from the database before applying the |
|
859 |
* filters. |
|
860 |
* |
|
861 |
* $term ID must be part of $taxonomy, to get from the database. Failure, might |
|
862 |
* be able to be captured by the hooks. Failure would be the same value as $wpdb |
|
863 |
* returns for the get_row method. |
|
864 |
* |
|
865 |
* There are two hooks, one is specifically for each term, named 'get_term', and |
|
866 |
* the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the |
|
867 |
* term object, and the taxonomy name as parameters. Both hooks are expected to |
|
868 |
* return a Term object. |
|
869 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
* {@see 'get_term'} hook - Takes two parameters the term Object and the taxonomy name. |
0 | 871 |
* Must return term object. Used in get_term() as a catch-all filter for every |
872 |
* $term. |
|
873 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
* {@see 'get_$taxonomy'} hook - Takes two parameters the term Object and the taxonomy |
0 | 875 |
* name. Must return term object. $taxonomy will be the taxonomy name, so for |
876 |
* example, if 'category', it would be 'get_category' as the filter name. Useful |
|
877 |
* for custom taxonomies or plugging into default taxonomies. |
|
878 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
* @todo Better formatting for DocBlock |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
* |
0 | 881 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
882 |
* @since 4.4.0 Converted to return a WP_Term object if `$output` is `OBJECT`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
* The `$taxonomy` parameter was made optional. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
* |
0 | 885 |
* @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
886 |
* |
|
16 | 887 |
* @param int|WP_Term|object $term If integer, term data will be fetched from the database, |
888 |
* or from the cache if available. |
|
889 |
* If stdClass object (as in the results of a database query), |
|
890 |
* will apply filters and return a `WP_Term` object with the `$term` data. |
|
891 |
* If `WP_Term`, will return `$term`. |
|
892 |
* @param string $taxonomy Optional. Taxonomy name that `$term` is part of. |
|
893 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
894 |
* correspond to a WP_Term object, an associative array, or a numeric array, |
|
895 |
* respectively. Default OBJECT. |
|
896 |
* @param string $filter Optional. How to sanitize term fields. Default 'raw'. |
|
897 |
* @return WP_Term|array|WP_Error|null WP_Term instance (or array) on success, depending on the `$output` value. |
|
898 |
* WP_Error if `$taxonomy` does not exist. Null for miscellaneous failure. |
|
0 | 899 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
900 |
function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
if ( empty( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); |
0 | 903 |
} |
904 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
906 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
0 | 907 |
} |
908 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
if ( $term instanceof WP_Term ) { |
0 | 910 |
$_term = $term; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
} elseif ( is_object( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
if ( empty( $term->filter ) || 'raw' === $term->filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
$_term = sanitize_term( $term, $taxonomy, 'raw' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
$_term = new WP_Term( $_term ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
$_term = WP_Term::get_instance( $term->term_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
917 |
} |
0 | 918 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
$_term = WP_Term::get_instance( $term, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
921 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
if ( is_wp_error( $_term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
923 |
return $_term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
} elseif ( ! $_term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
return null; |
0 | 926 |
} |
927 |
||
9 | 928 |
// Ensure for filters that this is not empty. |
929 |
$taxonomy = $_term->taxonomy; |
|
930 |
||
5 | 931 |
/** |
9 | 932 |
* Filters a taxonomy term object. |
5 | 933 |
* |
18 | 934 |
* The {@see 'get_$taxonomy'} hook is also available for targeting a specific |
935 |
* taxonomy. |
|
936 |
* |
|
5 | 937 |
* @since 2.3.0 |
9 | 938 |
* @since 4.4.0 `$_term` is now a `WP_Term` object. |
5 | 939 |
* |
9 | 940 |
* @param WP_Term $_term Term object. |
941 |
* @param string $taxonomy The taxonomy slug. |
|
5 | 942 |
*/ |
943 |
$_term = apply_filters( 'get_term', $_term, $taxonomy ); |
|
944 |
||
945 |
/** |
|
9 | 946 |
* Filters a taxonomy term object. |
5 | 947 |
* |
948 |
* The dynamic portion of the filter name, `$taxonomy`, refers |
|
9 | 949 |
* to the slug of the term's taxonomy. |
5 | 950 |
* |
951 |
* @since 2.3.0 |
|
9 | 952 |
* @since 4.4.0 `$_term` is now a `WP_Term` object. |
5 | 953 |
* |
9 | 954 |
* @param WP_Term $_term Term object. |
955 |
* @param string $taxonomy The taxonomy slug. |
|
5 | 956 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
$_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
// Bail if a filter callback has changed the type of the `$_term` object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
if ( ! ( $_term instanceof WP_Term ) ) { |
0 | 961 |
return $_term; |
962 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
// Sanitize term, according to the specified filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
$_term->filter( $filter ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
966 |
|
16 | 967 |
if ( ARRAY_A === $output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
return $_term->to_array(); |
16 | 969 |
} elseif ( ARRAY_N === $output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
return array_values( $_term->to_array() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
return $_term; |
0 | 974 |
} |
975 |
||
976 |
/** |
|
977 |
* Get all Term data from database by Term field and data. |
|
978 |
* |
|
979 |
* Warning: $value is not escaped for 'name' $field. You must do it yourself, if |
|
980 |
* required. |
|
981 |
* |
|
982 |
* The default $field is 'id', therefore it is possible to also use null for |
|
983 |
* field, but not recommended that you do so. |
|
984 |
* |
|
985 |
* If $value does not exist, the return value will be false. If $taxonomy exists |
|
986 |
* and $field and $value combinations exist, the Term will be returned. |
|
987 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
* This function will always return the first term that matches the `$field`- |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* `$value`-`$taxonomy` combination specified in the parameters. If your query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* is likely to match more than one term (as is likely to be the case when |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
* `$field` is 'name', for example), consider using get_terms() instead; that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
* way, you will get all matching terms, and can provide your own logic for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
* deciding which one was intended. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
* @todo Better formatting for DocBlock. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
* |
0 | 997 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
* @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
* a WP_Term object if `$output` is `OBJECT`. |
16 | 1000 |
* @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
* |
0 | 1002 |
* @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. |
1003 |
* |
|
16 | 1004 |
* @param string $field Either 'slug', 'name', 'id' or 'ID' (term_id), or 'term_taxonomy_id'. |
1005 |
* @param string|int $value Search for this term value. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
* @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'. |
16 | 1007 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
1008 |
* correspond to a WP_Term object, an associative array, or a numeric array, |
|
1009 |
* respectively. Default OBJECT. |
|
1010 |
* @param string $filter Optional. How to sanitize term fields. Default 'raw'. |
|
1011 |
* @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value. |
|
1012 |
* False if `$taxonomy` does not exist or `$term` was not found. |
|
0 | 1013 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
// 'term_taxonomy_id' lookups don't require taxonomy checks. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1017 |
if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) { |
0 | 1018 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1020 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1021 |
// No need to perform a query for empty 'slug' or 'name'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1022 |
if ( 'slug' === $field || 'name' === $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1023 |
$value = (string) $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1024 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1025 |
if ( 0 === strlen( $value ) ) { |
0 | 1026 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1027 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1028 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1029 |
|
16 | 1030 |
if ( 'id' === $field || 'ID' === $field || 'term_id' === $field ) { |
5 | 1031 |
$term = get_term( (int) $value, $taxonomy, $output, $filter ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1032 |
if ( is_wp_error( $term ) || null === $term ) { |
0 | 1033 |
$term = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1034 |
} |
0 | 1035 |
return $term; |
1036 |
} |
|
1037 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1038 |
$args = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1039 |
'get' => 'all', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
'number' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1041 |
'taxonomy' => $taxonomy, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
'update_term_meta_cache' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1043 |
'orderby' => 'none', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1044 |
'suppress_filter' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1045 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1046 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1047 |
switch ( $field ) { |
9 | 1048 |
case 'slug': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1049 |
$args['slug'] = $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1050 |
break; |
9 | 1051 |
case 'name': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1052 |
$args['name'] = $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
break; |
9 | 1054 |
case 'term_taxonomy_id': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1055 |
$args['term_taxonomy_id'] = $value; |
9 | 1056 |
unset( $args['taxonomy'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1057 |
break; |
9 | 1058 |
default: |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1059 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1060 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1062 |
$terms = get_terms( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1063 |
if ( is_wp_error( $terms ) || empty( $terms ) ) { |
0 | 1064 |
return false; |
1065 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1067 |
$term = array_shift( $terms ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1068 |
|
16 | 1069 |
// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
if ( 'term_taxonomy_id' === $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
$taxonomy = $term->taxonomy; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
return get_term( $term, $taxonomy, $output, $filter ); |
0 | 1075 |
} |
1076 |
||
1077 |
/** |
|
1078 |
* Merge all term children into a single array of their IDs. |
|
1079 |
* |
|
1080 |
* This recursive function will merge all of the children of $term into the same |
|
1081 |
* array of term IDs. Only useful for taxonomies which are hierarchical. |
|
1082 |
* |
|
1083 |
* Will return an empty array if $term does not exist in $taxonomy. |
|
1084 |
* |
|
1085 |
* @since 2.3.0 |
|
1086 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1087 |
* @param int $term_id ID of Term to get children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1088 |
* @param string $taxonomy Taxonomy Name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1089 |
* @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist. |
0 | 1090 |
*/ |
1091 |
function get_term_children( $term_id, $taxonomy ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1093 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
} |
0 | 1095 |
|
18 | 1096 |
$term_id = (int) $term_id; |
0 | 1097 |
|
9 | 1098 |
$terms = _get_term_hierarchy( $taxonomy ); |
1099 |
||
1100 |
if ( ! isset( $terms[ $term_id ] ) ) { |
|
0 | 1101 |
return array(); |
9 | 1102 |
} |
1103 |
||
1104 |
$children = $terms[ $term_id ]; |
|
1105 |
||
1106 |
foreach ( (array) $terms[ $term_id ] as $child ) { |
|
16 | 1107 |
if ( $term_id === $child ) { |
5 | 1108 |
continue; |
1109 |
} |
|
1110 |
||
9 | 1111 |
if ( isset( $terms[ $child ] ) ) { |
1112 |
$children = array_merge( $children, get_term_children( $child, $taxonomy ) ); |
|
1113 |
} |
|
0 | 1114 |
} |
1115 |
||
1116 |
return $children; |
|
1117 |
} |
|
1118 |
||
1119 |
/** |
|
1120 |
* Get sanitized Term field. |
|
1121 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
* The function is for contextual reasons and for simplicity of usage. |
0 | 1123 |
* |
1124 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
* @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
* @see sanitize_term_field() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
* @param string $field Term field to fetch. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
* @param int|WP_Term $term Term ID or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
* @param string $taxonomy Optional. Taxonomy Name. Default empty. |
16 | 1132 |
* @param string $context Optional. How to sanitize term fields. Look at sanitize_term_field() for available options. |
1133 |
* Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
* @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term. |
0 | 1135 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { |
0 | 1137 |
$term = get_term( $term, $taxonomy ); |
9 | 1138 |
if ( is_wp_error( $term ) ) { |
0 | 1139 |
return $term; |
9 | 1140 |
} |
1141 |
||
1142 |
if ( ! is_object( $term ) ) { |
|
0 | 1143 |
return ''; |
9 | 1144 |
} |
1145 |
||
1146 |
if ( ! isset( $term->$field ) ) { |
|
0 | 1147 |
return ''; |
9 | 1148 |
} |
0 | 1149 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context ); |
0 | 1151 |
} |
1152 |
||
1153 |
/** |
|
1154 |
* Sanitizes Term for editing. |
|
1155 |
* |
|
1156 |
* Return value is sanitize_term() and usage is for sanitizing the term for |
|
1157 |
* editing. Function is for contextual and simplicity. |
|
1158 |
* |
|
1159 |
* @since 2.3.0 |
|
1160 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
* @param int|object $id Term ID or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
* @param string $taxonomy Taxonomy name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
* @return string|int|null|WP_Error Will return empty string if $term is not an object. |
0 | 1164 |
*/ |
1165 |
function get_term_to_edit( $id, $taxonomy ) { |
|
1166 |
$term = get_term( $id, $taxonomy ); |
|
1167 |
||
9 | 1168 |
if ( is_wp_error( $term ) ) { |
0 | 1169 |
return $term; |
9 | 1170 |
} |
1171 |
||
1172 |
if ( ! is_object( $term ) ) { |
|
0 | 1173 |
return ''; |
9 | 1174 |
} |
1175 |
||
1176 |
return sanitize_term( $term, $taxonomy, 'edit' ); |
|
0 | 1177 |
} |
1178 |
||
1179 |
/** |
|
18 | 1180 |
* Retrieves the terms in a given taxonomy or list of taxonomies. |
0 | 1181 |
* |
1182 |
* You can fully inject any customizations to the query before it is sent, as |
|
1183 |
* well as control the output with a filter. |
|
1184 |
* |
|
18 | 1185 |
* The return type varies depending on the value passed to `$args['fields']`. See |
1186 |
* WP_Term_Query::get_terms() for details. In all cases, a `WP_Error` object will |
|
1187 |
* be returned if an invalid taxonomy is requested. |
|
1188 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
* The {@see 'get_terms'} filter will be called when the cache has the term and will |
0 | 1190 |
* pass the found term along with the array of $taxonomies and array of $args. |
1191 |
* This filter is also called before the array of terms is passed and will pass |
|
1192 |
* the array of terms, along with the $taxonomies and $args. |
|
1193 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
* The {@see 'list_terms_exclusions'} filter passes the compiled exclusions along with |
0 | 1195 |
* the $args. |
1196 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
* The {@see 'get_terms_orderby'} filter passes the `ORDER BY` clause for the query |
0 | 1198 |
* along with the $args array. |
1199 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
* Prior to 4.5.0, the first parameter of `get_terms()` was a taxonomy or list of taxonomies: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
* $terms = get_terms( 'post_tag', array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
* 'hide_empty' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
* ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
* Since 4.5.0, taxonomies should be passed via the 'taxonomy' argument in the `$args` array: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
* $terms = get_terms( array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
* 'taxonomy' => 'post_tag', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
* 'hide_empty' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
* ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
* |
0 | 1213 |
* @since 2.3.0 |
5 | 1214 |
* @since 4.2.0 Introduced 'name' and 'childless' parameters. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
* @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
* Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
* a list of WP_Term objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
* @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
* Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
* @since 4.8.0 Introduced 'suppress_filter' parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
* @internal The `$deprecated` parameter is parsed for backward compatibility only. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* |
16 | 1224 |
* @param array|string $args Optional. Array or string of arguments. See WP_Term_Query::__construct() |
18 | 1225 |
* for information on accepted arguments. Default empty array. |
1226 |
* @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format. |
|
1227 |
* If present, this parameter will be interpreted as `$args`, and the first |
|
1228 |
* function parameter will be parsed as a taxonomy or array of taxonomies. |
|
1229 |
* Default empty. |
|
1230 |
* @return WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string, |
|
1231 |
* or WP_Error if any of the taxonomies do not exist. |
|
1232 |
* See the function description for more information. |
|
0 | 1233 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1234 |
function get_terms( $args = array(), $deprecated = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
$term_query = new WP_Term_Query(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
$defaults = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
'suppress_filter' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
* Legacy argument format ($taxonomy, $args) takes precedence. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
* We detect legacy argument format by checking if |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
* (a) a second non-empty parameter is passed, or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
*/ |
9 | 1248 |
$_args = wp_parse_args( $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
$key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $_args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
$do_legacy_args = $deprecated || empty( $key_intersect ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1252 |
if ( $do_legacy_args ) { |
9 | 1253 |
$taxonomies = (array) $args; |
1254 |
$args = wp_parse_args( $deprecated, $defaults ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
$args['taxonomy'] = $taxonomies; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1256 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
$args = wp_parse_args( $args, $defaults ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
$args['taxonomy'] = (array) $args['taxonomy']; |
0 | 1260 |
} |
1261 |
} |
|
1262 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
if ( ! empty( $args['taxonomy'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
foreach ( $args['taxonomy'] as $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
} |
5 | 1268 |
} |
1269 |
} |
|
1270 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1271 |
// Don't pass suppress_filter to WP_Term_Query. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
$suppress_filter = $args['suppress_filter']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
unset( $args['suppress_filter'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
$terms = $term_query->query( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
// Count queries are not filtered, for legacy reasons. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
if ( ! is_array( $terms ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
return $terms; |
0 | 1280 |
} |
1281 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
if ( $suppress_filter ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
return $terms; |
0 | 1284 |
} |
1285 |
||
5 | 1286 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
* Filters the found terms. |
5 | 1288 |
* |
1289 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
* @since 4.6.0 Added the `$term_query` parameter. |
5 | 1291 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* @param array $terms Array of found terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
* @param array $taxonomies An array of taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* @param array $args An array of get_terms() arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1295 |
* @param WP_Term_Query $term_query The WP_Term_Query object. |
5 | 1296 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
* Adds metadata to a term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1306 |
* @param string $meta_key Metadata name. |
16 | 1307 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
1308 |
* @param bool $unique Optional. Whether the same key should not be added. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
* Default false. |
16 | 1310 |
* @return int|false|WP_Error Meta ID on success, false on failure. |
1311 |
* WP_Error when term_id is ambiguous between taxonomies. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1314 |
if ( wp_term_is_shared( $term_id ) ) { |
9 | 1315 |
return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
|
9 | 1318 |
return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
* Removes metadata matching criteria from a term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
* @param string $meta_key Metadata name. |
16 | 1328 |
* @param mixed $meta_value Optional. Metadata value. If provided, |
1329 |
* rows will only be removed that match the value. |
|
1330 |
* Must be serializable if non-scalar. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) { |
9 | 1334 |
return delete_metadata( 'term', $term_id, $meta_key, $meta_value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
* Retrieves metadata for a term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
* @param int $term_id Term ID. |
16 | 1343 |
* @param string $key Optional. The meta key to retrieve. By default, |
1344 |
* returns data for all keys. Default empty. |
|
1345 |
* @param bool $single Optional. Whether to return a single value. |
|
18 | 1346 |
* This parameter has no effect if `$key` is not specified. |
16 | 1347 |
* Default false. |
18 | 1348 |
* @return mixed An array of values if `$single` is false. |
1349 |
* The value of the meta field if `$single` is true. |
|
1350 |
* False for an invalid `$term_id` (non-numeric, zero, or negative value). |
|
1351 |
* An empty string if a valid but non-existing term ID is passed. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
function get_term_meta( $term_id, $key = '', $single = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
return get_metadata( 'term', $term_id, $key, $single ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
* Updates term metadata. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
* Use the `$prev_value` parameter to differentiate between meta fields with the same key and term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
* If the meta field for the term does not exist, it will be added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* @param string $meta_key Metadata key. |
16 | 1368 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
1369 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
|
1370 |
* If specified, only update existing metadata entries with |
|
1371 |
* this value. Otherwise, update all entries. Default empty. |
|
1372 |
* @return int|bool|WP_Error Meta ID if the key didn't exist. true on successful update, |
|
1373 |
* false on failure or if the value passed to the function |
|
1374 |
* is the same as the one that is already in the database. |
|
1375 |
* WP_Error when term_id is ambiguous between taxonomies. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
if ( wp_term_is_shared( $term_id ) ) { |
9 | 1379 |
return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
|
9 | 1382 |
return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
* Updates metadata cache for list of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
* Performs SQL query to retrieve all metadata for the terms matching `$term_ids` and stores them in the cache. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
* Subsequent calls to `get_term_meta()` will not need to query the database. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
* @param array $term_ids List of term IDs. |
16 | 1394 |
* @return array|false An array of metadata on success, false if there is nothing to update. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
function update_termmeta_cache( $term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
return update_meta_cache( 'term', $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
* Get all meta data, including meta IDs, for the given term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
* @return array|false Array with meta data, or false when the meta table is not installed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
function has_term_meta( $term_id ) { |
9 | 1411 |
$check = wp_check_term_meta_support_prefilter( null ); |
1412 |
if ( null !== $check ) { |
|
1413 |
return $check; |
|
0 | 1414 |
} |
1415 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, term_id FROM $wpdb->termmeta WHERE term_id = %d ORDER BY meta_key,meta_id", $term_id ), ARRAY_A ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
* Registers a meta key for terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
* @param string $taxonomy Taxonomy to register a meta key for. Pass an empty string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
* to register the meta key across all existing taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
* @param string $meta_key The meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
* @param array $args Data used to describe the meta key when registered. See |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
* {@see register_meta()} for a list of supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
* @return bool True if the meta key was successfully registered, false if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
function register_term_meta( $taxonomy, $meta_key, array $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1434 |
$args['object_subtype'] = $taxonomy; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
return register_meta( 'term', $meta_key, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1440 |
* Unregisters a meta key for terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1443 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1444 |
* @param string $taxonomy Taxonomy the meta key is currently registered for. Pass |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
* an empty string if the meta key is registered across all |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
* existing taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1447 |
* @param string $meta_key The meta key to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1448 |
* @return bool True on success, false if the meta key was not previously registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
function unregister_term_meta( $taxonomy, $meta_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
return unregister_meta_key( 'term', $meta_key, $taxonomy ); |
0 | 1452 |
} |
1453 |
||
1454 |
/** |
|
16 | 1455 |
* Determines whether a taxonomy term exists. |
0 | 1456 |
* |
1457 |
* Formerly is_term(), introduced in 2.3.0. |
|
1458 |
* |
|
9 | 1459 |
* For more information on this and similar theme functions, check out |
1460 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1461 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
1462 |
* |
|
0 | 1463 |
* @since 3.0.0 |
1464 |
* |
|
5 | 1465 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 1466 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
* @param int|string $term The term to check. Accepts term ID, slug, or name. |
16 | 1468 |
* @param string $taxonomy Optional. The taxonomy name to use. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
* @param int $parent Optional. ID of parent term under which to confine the exists search. |
16 | 1470 |
* @return mixed Returns null if the term does not exist. |
1471 |
* Returns the term ID if no taxonomy is specified and the term ID exists. |
|
1472 |
* Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists. |
|
1473 |
* Returns 0 if term ID 0 is passed to the function. |
|
0 | 1474 |
*/ |
5 | 1475 |
function term_exists( $term, $taxonomy = '', $parent = null ) { |
0 | 1476 |
global $wpdb; |
1477 |
||
9 | 1478 |
$select = "SELECT term_id FROM $wpdb->terms as t WHERE "; |
0 | 1479 |
$tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE "; |
1480 |
||
9 | 1481 |
if ( is_int( $term ) ) { |
16 | 1482 |
if ( 0 === $term ) { |
0 | 1483 |
return 0; |
9 | 1484 |
} |
0 | 1485 |
$where = 't.term_id = %d'; |
9 | 1486 |
if ( ! empty( $taxonomy ) ) { |
16 | 1487 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
9 | 1488 |
return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A ); |
1489 |
} else { |
|
0 | 1490 |
return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); |
9 | 1491 |
} |
0 | 1492 |
} |
1493 |
||
1494 |
$term = trim( wp_unslash( $term ) ); |
|
5 | 1495 |
$slug = sanitize_title( $term ); |
0 | 1496 |
|
9 | 1497 |
$where = 't.slug = %s'; |
1498 |
$else_where = 't.name = %s'; |
|
1499 |
$where_fields = array( $slug ); |
|
1500 |
$else_where_fields = array( $term ); |
|
1501 |
$orderby = 'ORDER BY t.term_id ASC'; |
|
1502 |
$limit = 'LIMIT 1'; |
|
1503 |
if ( ! empty( $taxonomy ) ) { |
|
5 | 1504 |
if ( is_numeric( $parent ) ) { |
9 | 1505 |
$parent = (int) $parent; |
1506 |
$where_fields[] = $parent; |
|
0 | 1507 |
$else_where_fields[] = $parent; |
9 | 1508 |
$where .= ' AND tt.parent = %d'; |
1509 |
$else_where .= ' AND tt.parent = %d'; |
|
0 | 1510 |
} |
1511 |
||
9 | 1512 |
$where_fields[] = $taxonomy; |
0 | 1513 |
$else_where_fields[] = $taxonomy; |
1514 |
||
16 | 1515 |
$result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields ), ARRAY_A ); |
1516 |
if ( $result ) { |
|
0 | 1517 |
return $result; |
9 | 1518 |
} |
1519 |
||
1520 |
return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields ), ARRAY_A ); |
|
0 | 1521 |
} |
1522 |
||
16 | 1523 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
1524 |
$result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ); |
|
1525 |
if ( $result ) { |
|
0 | 1526 |
return $result; |
9 | 1527 |
} |
1528 |
||
16 | 1529 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
9 | 1530 |
return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) ); |
0 | 1531 |
} |
1532 |
||
1533 |
/** |
|
1534 |
* Check if a term is an ancestor of another term. |
|
1535 |
* |
|
16 | 1536 |
* You can use either an ID or the term object for both parameters. |
0 | 1537 |
* |
1538 |
* @since 3.4.0 |
|
1539 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* @param int|object $term1 ID or object to check if this is the parent term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
* @param int|object $term2 The child term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1542 |
* @param string $taxonomy Taxonomy name that $term1 and `$term2` belong to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
* @return bool Whether `$term2` is a child of `$term1`. |
0 | 1544 |
*/ |
1545 |
function term_is_ancestor_of( $term1, $term2, $taxonomy ) { |
|
9 | 1546 |
if ( ! isset( $term1->term_id ) ) { |
0 | 1547 |
$term1 = get_term( $term1, $taxonomy ); |
9 | 1548 |
} |
1549 |
if ( ! isset( $term2->parent ) ) { |
|
0 | 1550 |
$term2 = get_term( $term2, $taxonomy ); |
9 | 1551 |
} |
1552 |
||
1553 |
if ( empty( $term1->term_id ) || empty( $term2->parent ) ) { |
|
0 | 1554 |
return false; |
9 | 1555 |
} |
16 | 1556 |
if ( $term2->parent === $term1->term_id ) { |
0 | 1557 |
return true; |
9 | 1558 |
} |
0 | 1559 |
|
1560 |
return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); |
|
1561 |
} |
|
1562 |
||
1563 |
/** |
|
18 | 1564 |
* Sanitize all term fields. |
0 | 1565 |
* |
1566 |
* Relies on sanitize_term_field() to sanitize the term. The difference is that |
|
18 | 1567 |
* this function will sanitize **all** fields. The context is based |
0 | 1568 |
* on sanitize_term_field(). |
1569 |
* |
|
18 | 1570 |
* The `$term` is expected to be either an array or an object. |
0 | 1571 |
* |
1572 |
* @since 2.3.0 |
|
1573 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
* @param array|object $term The term to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
* @param string $taxonomy The taxonomy name to use. |
18 | 1576 |
* @param string $context Optional. Context in which to sanitize the term. |
1577 |
* Accepts 'raw', 'edit', 'db', 'display', 'rss', |
|
1578 |
* 'attribute', or 'js'. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
* @return array|object Term with all fields sanitized. |
0 | 1580 |
*/ |
9 | 1581 |
function sanitize_term( $term, $taxonomy, $context = 'display' ) { |
5 | 1582 |
$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); |
1583 |
||
1584 |
$do_object = is_object( $term ); |
|
0 | 1585 |
|
9 | 1586 |
$term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 ); |
0 | 1587 |
|
1588 |
foreach ( (array) $fields as $field ) { |
|
1589 |
if ( $do_object ) { |
|
9 | 1590 |
if ( isset( $term->$field ) ) { |
1591 |
$term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); |
|
1592 |
} |
|
0 | 1593 |
} else { |
9 | 1594 |
if ( isset( $term[ $field ] ) ) { |
1595 |
$term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context ); |
|
1596 |
} |
|
0 | 1597 |
} |
1598 |
} |
|
1599 |
||
9 | 1600 |
if ( $do_object ) { |
0 | 1601 |
$term->filter = $context; |
9 | 1602 |
} else { |
0 | 1603 |
$term['filter'] = $context; |
9 | 1604 |
} |
0 | 1605 |
|
1606 |
return $term; |
|
1607 |
} |
|
1608 |
||
1609 |
/** |
|
1610 |
* Cleanse the field value in the term based on the context. |
|
1611 |
* |
|
1612 |
* Passing a term field value through the function should be assumed to have |
|
1613 |
* cleansed the value for whatever context the term field is going to be used. |
|
1614 |
* |
|
1615 |
* If no context or an unsupported context is given, then default filters will |
|
1616 |
* be applied. |
|
1617 |
* |
|
1618 |
* There are enough filters for each context to support a custom filtering |
|
1619 |
* without creating your own filter function. Simply create a function that |
|
1620 |
* hooks into the filter you need. |
|
1621 |
* |
|
1622 |
* @since 2.3.0 |
|
1623 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
* @param string $field Term field to sanitize. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1625 |
* @param string $value Search for this term value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1626 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1627 |
* @param string $taxonomy Taxonomy Name. |
18 | 1628 |
* @param string $context Context in which to sanitize the term field. |
1629 |
* Accepts 'raw', 'edit', 'db', 'display', 'rss', |
|
1630 |
* 'attribute', or 'js'. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1631 |
* @return mixed Sanitized field. |
0 | 1632 |
*/ |
9 | 1633 |
function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { |
5 | 1634 |
$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); |
16 | 1635 |
if ( in_array( $field, $int_fields, true ) ) { |
0 | 1636 |
$value = (int) $value; |
9 | 1637 |
if ( $value < 0 ) { |
0 | 1638 |
$value = 0; |
9 | 1639 |
} |
0 | 1640 |
} |
1641 |
||
16 | 1642 |
$context = strtolower( $context ); |
1643 |
||
1644 |
if ( 'raw' === $context ) { |
|
0 | 1645 |
return $value; |
9 | 1646 |
} |
0 | 1647 |
|
16 | 1648 |
if ( 'edit' === $context ) { |
5 | 1649 |
|
1650 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1651 |
* Filters a term field to edit before it is sanitized. |
5 | 1652 |
* |
1653 |
* The dynamic portion of the filter name, `$field`, refers to the term field. |
|
1654 |
* |
|
1655 |
* @since 2.3.0 |
|
1656 |
* |
|
1657 |
* @param mixed $value Value of the term field. |
|
1658 |
* @param int $term_id Term ID. |
|
1659 |
* @param string $taxonomy Taxonomy slug. |
|
1660 |
*/ |
|
1661 |
$value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy ); |
|
1662 |
||
1663 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1664 |
* Filters the taxonomy field to edit before it is sanitized. |
5 | 1665 |
* |
1666 |
* The dynamic portions of the filter name, `$taxonomy` and `$field`, refer |
|
1667 |
* to the taxonomy slug and taxonomy field, respectively. |
|
1668 |
* |
|
1669 |
* @since 2.3.0 |
|
1670 |
* |
|
1671 |
* @param mixed $value Value of the taxonomy field to edit. |
|
1672 |
* @param int $term_id Term ID. |
|
1673 |
*/ |
|
1674 |
$value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
|
16 | 1676 |
if ( 'description' === $field ) { |
9 | 1677 |
$value = esc_html( $value ); // textarea_escaped |
1678 |
} else { |
|
1679 |
$value = esc_attr( $value ); |
|
1680 |
} |
|
16 | 1681 |
} elseif ( 'db' === $context ) { |
5 | 1682 |
|
1683 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1684 |
* Filters a term field value before it is sanitized. |
5 | 1685 |
* |
1686 |
* The dynamic portion of the filter name, `$field`, refers to the term field. |
|
1687 |
* |
|
1688 |
* @since 2.3.0 |
|
1689 |
* |
|
1690 |
* @param mixed $value Value of the term field. |
|
1691 |
* @param string $taxonomy Taxonomy slug. |
|
1692 |
*/ |
|
1693 |
$value = apply_filters( "pre_term_{$field}", $value, $taxonomy ); |
|
1694 |
||
1695 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
* Filters a taxonomy field before it is sanitized. |
5 | 1697 |
* |
1698 |
* The dynamic portions of the filter name, `$taxonomy` and `$field`, refer |
|
1699 |
* to the taxonomy slug and field name, respectively. |
|
1700 |
* |
|
1701 |
* @since 2.3.0 |
|
1702 |
* |
|
1703 |
* @param mixed $value Value of the taxonomy field. |
|
1704 |
*/ |
|
1705 |
$value = apply_filters( "pre_{$taxonomy}_{$field}", $value ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1706 |
|
16 | 1707 |
// Back compat filters. |
1708 |
if ( 'slug' === $field ) { |
|
5 | 1709 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
* Filters the category nicename before it is sanitized. |
5 | 1711 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1712 |
* Use the {@see 'pre_$taxonomy_$field'} hook instead. |
5 | 1713 |
* |
1714 |
* @since 2.0.3 |
|
1715 |
* |
|
1716 |
* @param string $value The category nicename. |
|
1717 |
*/ |
|
1718 |
$value = apply_filters( 'pre_category_nicename', $value ); |
|
1719 |
} |
|
16 | 1720 |
} elseif ( 'rss' === $context ) { |
5 | 1721 |
|
1722 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
* Filters the term field for use in RSS. |
5 | 1724 |
* |
1725 |
* The dynamic portion of the filter name, `$field`, refers to the term field. |
|
1726 |
* |
|
1727 |
* @since 2.3.0 |
|
1728 |
* |
|
1729 |
* @param mixed $value Value of the term field. |
|
1730 |
* @param string $taxonomy Taxonomy slug. |
|
1731 |
*/ |
|
1732 |
$value = apply_filters( "term_{$field}_rss", $value, $taxonomy ); |
|
1733 |
||
1734 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1735 |
* Filters the taxonomy field for use in RSS. |
5 | 1736 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1737 |
* The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer |
5 | 1738 |
* to the taxonomy slug and field name, respectively. |
1739 |
* |
|
1740 |
* @since 2.3.0 |
|
1741 |
* |
|
1742 |
* @param mixed $value Value of the taxonomy field. |
|
1743 |
*/ |
|
1744 |
$value = apply_filters( "{$taxonomy}_{$field}_rss", $value ); |
|
0 | 1745 |
} else { |
1746 |
// Use display filters by default. |
|
5 | 1747 |
|
1748 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1749 |
* Filters the term field sanitized for display. |
5 | 1750 |
* |
1751 |
* The dynamic portion of the filter name, `$field`, refers to the term field name. |
|
1752 |
* |
|
1753 |
* @since 2.3.0 |
|
1754 |
* |
|
1755 |
* @param mixed $value Value of the term field. |
|
1756 |
* @param int $term_id Term ID. |
|
1757 |
* @param string $taxonomy Taxonomy slug. |
|
1758 |
* @param string $context Context to retrieve the term field value. |
|
1759 |
*/ |
|
1760 |
$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context ); |
|
1761 |
||
1762 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1763 |
* Filters the taxonomy field sanitized for display. |
5 | 1764 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
* The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer |
5 | 1766 |
* to the taxonomy slug and taxonomy field, respectively. |
1767 |
* |
|
1768 |
* @since 2.3.0 |
|
1769 |
* |
|
1770 |
* @param mixed $value Value of the taxonomy field. |
|
1771 |
* @param int $term_id Term ID. |
|
1772 |
* @param string $context Context to retrieve the taxonomy field value. |
|
1773 |
*/ |
|
1774 |
$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context ); |
|
0 | 1775 |
} |
1776 |
||
16 | 1777 |
if ( 'attribute' === $context ) { |
9 | 1778 |
$value = esc_attr( $value ); |
16 | 1779 |
} elseif ( 'js' === $context ) { |
9 | 1780 |
$value = esc_js( $value ); |
5 | 1781 |
} |
18 | 1782 |
|
1783 |
// Restore the type for integer fields after esc_attr(). |
|
1784 |
if ( in_array( $field, $int_fields, true ) ) { |
|
1785 |
$value = (int) $value; |
|
1786 |
} |
|
1787 |
||
0 | 1788 |
return $value; |
1789 |
} |
|
1790 |
||
1791 |
/** |
|
1792 |
* Count how many terms are in Taxonomy. |
|
1793 |
* |
|
1794 |
* Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). |
|
1795 |
* |
|
1796 |
* @since 2.3.0 |
|
18 | 1797 |
* @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter. |
1798 |
* |
|
1799 |
* @internal The `$deprecated` parameter is parsed for backward compatibility only. |
|
1800 |
* |
|
1801 |
* @param array|string $args Optional. Array of arguments that get passed to get_terms(). |
|
1802 |
* Default empty array. |
|
1803 |
* @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format. |
|
1804 |
* If present, this parameter will be interpreted as `$args`, and the first |
|
1805 |
* function parameter will be parsed as a taxonomy or array of taxonomies. |
|
1806 |
* Default empty. |
|
1807 |
* @return string|WP_Error Numeric string containing the number of terms in that |
|
1808 |
* taxonomy or WP_Error if the taxonomy does not exist. |
|
0 | 1809 |
*/ |
18 | 1810 |
function wp_count_terms( $args = array(), $deprecated = '' ) { |
1811 |
$use_legacy_args = false; |
|
1812 |
||
1813 |
// Check whether function is used with legacy signature: `$taxonomy` and `$args`. |
|
1814 |
if ( $args |
|
1815 |
&& ( is_string( $args ) && taxonomy_exists( $args ) |
|
1816 |
|| is_array( $args ) && wp_is_numeric_array( $args ) ) |
|
1817 |
) { |
|
1818 |
$use_legacy_args = true; |
|
1819 |
} |
|
1820 |
||
1821 |
$defaults = array( 'hide_empty' => false ); |
|
1822 |
||
1823 |
if ( $use_legacy_args ) { |
|
1824 |
$defaults['taxonomy'] = $args; |
|
1825 |
$args = $deprecated; |
|
1826 |
} |
|
1827 |
||
1828 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 1829 |
|
16 | 1830 |
// Backward compatibility. |
9 | 1831 |
if ( isset( $args['ignore_empty'] ) ) { |
0 | 1832 |
$args['hide_empty'] = $args['ignore_empty']; |
9 | 1833 |
unset( $args['ignore_empty'] ); |
0 | 1834 |
} |
1835 |
||
1836 |
$args['fields'] = 'count'; |
|
1837 |
||
16 | 1838 |
return get_terms( $args ); |
0 | 1839 |
} |
1840 |
||
1841 |
/** |
|
1842 |
* Will unlink the object from the taxonomy or taxonomies. |
|
1843 |
* |
|
1844 |
* Will remove all relationships between the object and any terms in |
|
1845 |
* a particular taxonomy or taxonomies. Does not remove the term or |
|
1846 |
* taxonomy itself. |
|
1847 |
* |
|
1848 |
* @since 2.3.0 |
|
1849 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
* @param int $object_id The term Object Id that refers to the term. |
0 | 1851 |
* @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. |
1852 |
*/ |
|
1853 |
function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|
1854 |
$object_id = (int) $object_id; |
|
1855 |
||
9 | 1856 |
if ( ! is_array( $taxonomies ) ) { |
1857 |
$taxonomies = array( $taxonomies ); |
|
1858 |
} |
|
0 | 1859 |
|
1860 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
1861 |
$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
|
1862 |
$term_ids = array_map( 'intval', $term_ids ); |
|
1863 |
wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); |
|
1864 |
} |
|
1865 |
} |
|
1866 |
||
1867 |
/** |
|
1868 |
* Removes a term from the database. |
|
1869 |
* |
|
1870 |
* If the term is a parent of other terms, then the children will be updated to |
|
1871 |
* that term's parent. |
|
1872 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1873 |
* Metadata associated with the term will be deleted. |
5 | 1874 |
* |
0 | 1875 |
* @since 2.3.0 |
1876 |
* |
|
5 | 1877 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 1878 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1879 |
* @param int $term Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1880 |
* @param string $taxonomy Taxonomy Name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1881 |
* @param array|string $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1882 |
* Optional. Array of arguments to override the default term ID. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1883 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1884 |
* @type int $default The term ID to make the default term. This will only override |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1885 |
* the terms found if there is only one term found. Any other and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1886 |
* the found terms are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1887 |
* @type bool $force_default Optional. Whether to force the supplied term as default to be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1888 |
* assigned even if the object was not going to be term-less. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1889 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1890 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1891 |
* @return bool|int|WP_Error True on success, false if term does not exist. Zero on attempted |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1892 |
* deletion of default Category. WP_Error if the taxonomy does not exist. |
0 | 1893 |
*/ |
1894 |
function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|
1895 |
global $wpdb; |
|
1896 |
||
1897 |
$term = (int) $term; |
|
1898 |
||
16 | 1899 |
$ids = term_exists( $term, $taxonomy ); |
1900 |
if ( ! $ids ) { |
|
0 | 1901 |
return false; |
9 | 1902 |
} |
1903 |
if ( is_wp_error( $ids ) ) { |
|
0 | 1904 |
return $ids; |
9 | 1905 |
} |
0 | 1906 |
|
1907 |
$tt_id = $ids['term_taxonomy_id']; |
|
1908 |
||
1909 |
$defaults = array(); |
|
1910 |
||
16 | 1911 |
if ( 'category' === $taxonomy ) { |
1912 |
$defaults['default'] = (int) get_option( 'default_category' ); |
|
1913 |
if ( $defaults['default'] === $term ) { |
|
1914 |
return 0; // Don't delete the default category. |
|
1915 |
} |
|
1916 |
} |
|
1917 |
||
1918 |
// Don't delete the default custom taxonomy term. |
|
1919 |
$taxonomy_object = get_taxonomy( $taxonomy ); |
|
1920 |
if ( ! empty( $taxonomy_object->default_term ) ) { |
|
1921 |
$defaults['default'] = (int) get_option( 'default_term_' . $taxonomy ); |
|
1922 |
if ( $defaults['default'] === $term ) { |
|
1923 |
return 0; |
|
9 | 1924 |
} |
0 | 1925 |
} |
1926 |
||
9 | 1927 |
$args = wp_parse_args( $args, $defaults ); |
5 | 1928 |
|
1929 |
if ( isset( $args['default'] ) ) { |
|
1930 |
$default = (int) $args['default']; |
|
1931 |
if ( ! term_exists( $default, $taxonomy ) ) { |
|
1932 |
unset( $default ); |
|
1933 |
} |
|
1934 |
} |
|
1935 |
||
1936 |
if ( isset( $args['force_default'] ) ) { |
|
1937 |
$force_default = $args['force_default']; |
|
0 | 1938 |
} |
1939 |
||
5 | 1940 |
/** |
1941 |
* Fires when deleting a term, before any modifications are made to posts or terms. |
|
1942 |
* |
|
1943 |
* @since 4.1.0 |
|
1944 |
* |
|
1945 |
* @param int $term Term ID. |
|
1946 |
* @param string $taxonomy Taxonomy Name. |
|
1947 |
*/ |
|
1948 |
do_action( 'pre_delete_term', $term, $taxonomy ); |
|
1949 |
||
16 | 1950 |
// Update children to point to new parent. |
9 | 1951 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
1952 |
$term_obj = get_term( $term, $taxonomy ); |
|
1953 |
if ( is_wp_error( $term_obj ) ) { |
|
0 | 1954 |
return $term_obj; |
9 | 1955 |
} |
0 | 1956 |
$parent = $term_obj->parent; |
1957 |
||
9 | 1958 |
$edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id ); |
5 | 1959 |
$edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); |
1960 |
||
1961 |
/** |
|
1962 |
* Fires immediately before a term to delete's children are reassigned a parent. |
|
1963 |
* |
|
1964 |
* @since 2.9.0 |
|
1965 |
* |
|
1966 |
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term. |
|
1967 |
*/ |
|
0 | 1968 |
do_action( 'edit_term_taxonomies', $edit_tt_ids ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1969 |
|
9 | 1970 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); |
5 | 1971 |
|
1972 |
// Clean the cache for all child terms. |
|
1973 |
$edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' ); |
|
1974 |
clean_term_cache( $edit_term_ids, $taxonomy ); |
|
1975 |
||
1976 |
/** |
|
1977 |
* Fires immediately after a term to delete's children are reassigned a parent. |
|
1978 |
* |
|
1979 |
* @since 2.9.0 |
|
1980 |
* |
|
1981 |
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term. |
|
1982 |
*/ |
|
0 | 1983 |
do_action( 'edited_term_taxonomies', $edit_tt_ids ); |
1984 |
} |
|
1985 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1986 |
// Get the term before deleting it or its term relationships so we can pass to actions below. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1987 |
$deleted_term = get_term( $term, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1988 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1989 |
$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1990 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1991 |
foreach ( $object_ids as $object_id ) { |
18 | 1992 |
if ( ! isset( $default ) ) { |
1993 |
wp_remove_object_terms( $object_id, $term, $taxonomy ); |
|
1994 |
continue; |
|
1995 |
} |
|
1996 |
||
9 | 1997 |
$terms = wp_get_object_terms( |
1998 |
$object_id, |
|
1999 |
$taxonomy, |
|
2000 |
array( |
|
2001 |
'fields' => 'ids', |
|
2002 |
'orderby' => 'none', |
|
2003 |
) |
|
2004 |
); |
|
18 | 2005 |
|
16 | 2006 |
if ( 1 === count( $terms ) && isset( $default ) ) { |
9 | 2007 |
$terms = array( $default ); |
0 | 2008 |
} else { |
9 | 2009 |
$terms = array_diff( $terms, array( $term ) ); |
2010 |
if ( isset( $default ) && isset( $force_default ) && $force_default ) { |
|
2011 |
$terms = array_merge( $terms, array( $default ) ); |
|
2012 |
} |
|
0 | 2013 |
} |
18 | 2014 |
|
9 | 2015 |
$terms = array_map( 'intval', $terms ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
wp_set_object_terms( $object_id, $terms, $taxonomy ); |
0 | 2017 |
} |
2018 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2019 |
// Clean the relationship caches for all object types using this term. |
0 | 2020 |
$tax_object = get_taxonomy( $taxonomy ); |
9 | 2021 |
foreach ( $tax_object->object_type as $object_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2022 |
clean_object_term_cache( $object_ids, $object_type ); |
9 | 2023 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2024 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
$term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2026 |
foreach ( $term_meta_ids as $mid ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2027 |
delete_metadata_by_mid( 'term', $mid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2028 |
} |
0 | 2029 |
|
5 | 2030 |
/** |
2031 |
* Fires immediately before a term taxonomy ID is deleted. |
|
2032 |
* |
|
2033 |
* @since 2.9.0 |
|
2034 |
* |
|
2035 |
* @param int $tt_id Term taxonomy ID. |
|
2036 |
*/ |
|
0 | 2037 |
do_action( 'delete_term_taxonomy', $tt_id ); |
16 | 2038 |
|
0 | 2039 |
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); |
5 | 2040 |
|
2041 |
/** |
|
2042 |
* Fires immediately after a term taxonomy ID is deleted. |
|
2043 |
* |
|
2044 |
* @since 2.9.0 |
|
2045 |
* |
|
2046 |
* @param int $tt_id Term taxonomy ID. |
|
2047 |
*/ |
|
0 | 2048 |
do_action( 'deleted_term_taxonomy', $tt_id ); |
2049 |
||
2050 |
// Delete the term if no taxonomies use it. |
|
9 | 2051 |
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) { |
0 | 2052 |
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); |
9 | 2053 |
} |
2054 |
||
2055 |
clean_term_cache( $term, $taxonomy ); |
|
0 | 2056 |
|
5 | 2057 |
/** |
2058 |
* Fires after a term is deleted from the database and the cache is cleaned. |
|
2059 |
* |
|
18 | 2060 |
* The {@see 'delete_$taxonomy'} hook is also available for targeting a specific |
2061 |
* taxonomy. |
|
2062 |
* |
|
5 | 2063 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2064 |
* @since 4.5.0 Introduced the `$object_ids` argument. |
5 | 2065 |
* |
2066 |
* @param int $term Term ID. |
|
2067 |
* @param int $tt_id Term taxonomy ID. |
|
2068 |
* @param string $taxonomy Taxonomy slug. |
|
18 | 2069 |
* @param WP_Term $deleted_term Copy of the already-deleted term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2070 |
* @param array $object_ids List of term object IDs. |
5 | 2071 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2072 |
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids ); |
5 | 2073 |
|
2074 |
/** |
|
2075 |
* Fires after a term in a specific taxonomy is deleted. |
|
2076 |
* |
|
2077 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the specific |
|
2078 |
* taxonomy the term belonged to. |
|
2079 |
* |
|
18 | 2080 |
* Possible hook names include: |
2081 |
* |
|
2082 |
* - `delete_category` |
|
2083 |
* - `delete_post_tag` |
|
2084 |
* |
|
5 | 2085 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2086 |
* @since 4.5.0 Introduced the `$object_ids` argument. |
5 | 2087 |
* |
2088 |
* @param int $term Term ID. |
|
2089 |
* @param int $tt_id Term taxonomy ID. |
|
18 | 2090 |
* @param WP_Term $deleted_term Copy of the already-deleted term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2091 |
* @param array $object_ids List of term object IDs. |
5 | 2092 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2093 |
do_action( "delete_{$taxonomy}", $term, $tt_id, $deleted_term, $object_ids ); |
0 | 2094 |
|
2095 |
return true; |
|
2096 |
} |
|
2097 |
||
2098 |
/** |
|
2099 |
* Deletes one existing category. |
|
2100 |
* |
|
2101 |
* @since 2.0.0 |
|
2102 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2103 |
* @param int $cat_ID Category term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2104 |
* @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist; |
9 | 2105 |
* Zero on attempted deletion of default Category; WP_Error object is also a possibility. |
0 | 2106 |
*/ |
2107 |
function wp_delete_category( $cat_ID ) { |
|
2108 |
return wp_delete_term( $cat_ID, 'category' ); |
|
2109 |
} |
|
2110 |
||
2111 |
/** |
|
2112 |
* Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
|
2113 |
* |
|
2114 |
* @since 2.3.0 |
|
5 | 2115 |
* @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`. |
2116 |
* Introduced `$parent` argument. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2117 |
* @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2118 |
* 'all_with_object_id', an array of `WP_Term` objects will be returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2119 |
* @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments. |
5 | 2120 |
* |
16 | 2121 |
* @param int|int[] $object_ids The ID(s) of the object(s) to retrieve. |
2122 |
* @param string|string[] $taxonomies The taxonomy names to retrieve terms from. |
|
2123 |
* @param array|string $args See WP_Term_Query::__construct() for supported arguments. |
|
18 | 2124 |
* @return WP_Term[]|WP_Error Array of terms or empty array if no terms found. |
2125 |
* WP_Error if any of the taxonomies don't exist. |
|
0 | 2126 |
*/ |
9 | 2127 |
function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { |
2128 |
if ( empty( $object_ids ) || empty( $taxonomies ) ) { |
|
0 | 2129 |
return array(); |
9 | 2130 |
} |
2131 |
||
2132 |
if ( ! is_array( $taxonomies ) ) { |
|
2133 |
$taxonomies = array( $taxonomies ); |
|
2134 |
} |
|
0 | 2135 |
|
5 | 2136 |
foreach ( $taxonomies as $taxonomy ) { |
9 | 2137 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2138 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
9 | 2139 |
} |
0 | 2140 |
} |
2141 |
||
9 | 2142 |
if ( ! is_array( $object_ids ) ) { |
2143 |
$object_ids = array( $object_ids ); |
|
2144 |
} |
|
2145 |
$object_ids = array_map( 'intval', $object_ids ); |
|
0 | 2146 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2147 |
$args = wp_parse_args( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2148 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2149 |
/** |
18 | 2150 |
* Filters arguments for retrieving object terms. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2151 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2152 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2153 |
* |
16 | 2154 |
* @param array $args An array of arguments for retrieving terms for the given object(s). |
2155 |
* See {@see wp_get_object_terms()} for details. |
|
2156 |
* @param int[] $object_ids Array of object IDs. |
|
2157 |
* @param string[] $taxonomies Array of taxonomy names to retrieve terms from. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2158 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2159 |
$args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2160 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2161 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2162 |
* When one or more queried taxonomies is registered with an 'args' array, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2163 |
* those params override the `$args` passed to this function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2164 |
*/ |
0 | 2165 |
$terms = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2166 |
if ( count( $taxonomies ) > 1 ) { |
0 | 2167 |
foreach ( $taxonomies as $index => $taxonomy ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2168 |
$t = get_taxonomy( $taxonomy ); |
16 | 2169 |
if ( isset( $t->args ) && is_array( $t->args ) && array_merge( $args, $t->args ) != $args ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2170 |
unset( $taxonomies[ $index ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2171 |
$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) ); |
0 | 2172 |
} |
2173 |
} |
|
2174 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2175 |
$t = get_taxonomy( $taxonomies[0] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2176 |
if ( isset( $t->args ) && is_array( $t->args ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2177 |
$args = array_merge( $args, $t->args ); |
5 | 2178 |
} |
0 | 2179 |
} |
2180 |
||
9 | 2181 |
$args['taxonomy'] = $taxonomies; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2182 |
$args['object_ids'] = $object_ids; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2183 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2184 |
// Taxonomies registered without an 'args' param are handled here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2185 |
if ( ! empty( $taxonomies ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2186 |
$terms_from_remaining_taxonomies = get_terms( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2187 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2188 |
// Array keys should be preserved for values of $fields that use term_id for keys. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2189 |
if ( ! empty( $args['fields'] ) && 0 === strpos( $args['fields'], 'id=>' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2190 |
$terms = $terms + $terms_from_remaining_taxonomies; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2191 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2192 |
$terms = array_merge( $terms, $terms_from_remaining_taxonomies ); |
5 | 2193 |
} |
2194 |
} |
|
2195 |
||
2196 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2197 |
* Filters the terms for a given object or objects. |
5 | 2198 |
* |
2199 |
* @since 4.2.0 |
|
2200 |
* |
|
18 | 2201 |
* @param WP_Term[] $terms Array of terms for the given object or objects. |
2202 |
* @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
2203 |
* @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
2204 |
* @param array $args Array of arguments for retrieving terms for the given |
|
2205 |
* object(s). See wp_get_object_terms() for details. |
|
5 | 2206 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2207 |
$terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2208 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2209 |
$object_ids = implode( ',', $object_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2210 |
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; |
5 | 2211 |
|
2212 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2213 |
* Filters the terms for a given object or objects. |
5 | 2214 |
* |
2215 |
* The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The |
|
2216 |
* {@see 'get_object_terms'} filter is recommended as an alternative. |
|
2217 |
* |
|
2218 |
* @since 2.8.0 |
|
2219 |
* |
|
18 | 2220 |
* @param WP_Term[] $terms Array of terms for the given object or objects. |
2221 |
* @param string $object_ids Comma separated list of object IDs for which terms were retrieved. |
|
2222 |
* @param string $taxonomies SQL fragment of taxonomy names from which terms were retrieved. |
|
2223 |
* @param array $args Array of arguments for retrieving terms for the given |
|
2224 |
* object(s). See wp_get_object_terms() for details. |
|
5 | 2225 |
*/ |
2226 |
return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); |
|
0 | 2227 |
} |
2228 |
||
2229 |
/** |
|
2230 |
* Add a new term to the database. |
|
2231 |
* |
|
2232 |
* A non-existent term is inserted in the following sequence: |
|
2233 |
* 1. The term is added to the term table, then related to the taxonomy. |
|
2234 |
* 2. If everything is correct, several actions are fired. |
|
2235 |
* 3. The 'term_id_filter' is evaluated. |
|
2236 |
* 4. The term cache is cleaned. |
|
2237 |
* 5. Several more actions are fired. |
|
16 | 2238 |
* 6. An array is returned containing the `term_id` and `term_taxonomy_id`. |
0 | 2239 |
* |
2240 |
* If the 'slug' argument is not empty, then it is checked to see if the term |
|
2241 |
* is invalid. If it is not a valid, existing term, it is added and the term_id |
|
2242 |
* is given. |
|
2243 |
* |
|
2244 |
* If the taxonomy is hierarchical, and the 'parent' argument is not empty, |
|
2245 |
* the term is inserted and the term_id will be given. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2246 |
* |
0 | 2247 |
* Error handling: |
16 | 2248 |
* If `$taxonomy` does not exist or `$term` is empty, |
0 | 2249 |
* a WP_Error object will be returned. |
2250 |
* |
|
2251 |
* If the term already exists on the same hierarchical level, |
|
2252 |
* or the term slug and name are not unique, a WP_Error object will be returned. |
|
2253 |
* |
|
5 | 2254 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2255 |
* |
0 | 2256 |
* @since 2.3.0 |
2257 |
* |
|
16 | 2258 |
* @param string $term The term name to add. |
5 | 2259 |
* @param string $taxonomy The taxonomy to which to add the term. |
0 | 2260 |
* @param array|string $args { |
18 | 2261 |
* Optional. Array or query string of arguments for inserting a term. |
5 | 2262 |
* |
2263 |
* @type string $alias_of Slug of the term to make this term an alias of. |
|
2264 |
* Default empty string. Accepts a term slug. |
|
2265 |
* @type string $description The term description. Default empty string. |
|
2266 |
* @type int $parent The id of the parent term. Default 0. |
|
2267 |
* @type string $slug The term slug to use. Default empty string. |
|
0 | 2268 |
* } |
18 | 2269 |
* @return array|WP_Error { |
2270 |
* An array of the new term data, WP_Error otherwise. |
|
2271 |
* |
|
2272 |
* @type int $term_id The new term ID. |
|
2273 |
* @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string. |
|
2274 |
* } |
|
0 | 2275 |
*/ |
2276 |
function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|
2277 |
global $wpdb; |
|
2278 |
||
9 | 2279 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2280 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
5 | 2281 |
} |
16 | 2282 |
|
5 | 2283 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2284 |
* Filters a term before it is sanitized and inserted into the database. |
5 | 2285 |
* |
2286 |
* @since 3.0.0 |
|
2287 |
* |
|
16 | 2288 |
* @param string|WP_Error $term The term name to add, or a WP_Error object if there's an error. |
2289 |
* @param string $taxonomy Taxonomy slug. |
|
5 | 2290 |
*/ |
0 | 2291 |
$term = apply_filters( 'pre_insert_term', $term, $taxonomy ); |
16 | 2292 |
|
5 | 2293 |
if ( is_wp_error( $term ) ) { |
2294 |
return $term; |
|
2295 |
} |
|
16 | 2296 |
|
2297 |
if ( is_int( $term ) && 0 === $term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2298 |
return new WP_Error( 'invalid_term_id', __( 'Invalid term ID.' ) ); |
5 | 2299 |
} |
16 | 2300 |
|
2301 |
if ( '' === trim( $term ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2302 |
return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); |
5 | 2303 |
} |
16 | 2304 |
|
9 | 2305 |
$defaults = array( |
2306 |
'alias_of' => '', |
|
2307 |
'description' => '', |
|
2308 |
'parent' => 0, |
|
2309 |
'slug' => '', |
|
2310 |
); |
|
2311 |
$args = wp_parse_args( $args, $defaults ); |
|
5 | 2312 |
|
18 | 2313 |
if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { |
5 | 2314 |
return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); |
2315 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
|
9 | 2317 |
$args['name'] = $term; |
0 | 2318 |
$args['taxonomy'] = $taxonomy; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2320 |
// Coerce null description to strings, to avoid database errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2321 |
$args['description'] = (string) $args['description']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2322 |
|
9 | 2323 |
$args = sanitize_term( $args, $taxonomy, 'db' ); |
0 | 2324 |
|
2325 |
// expected_slashed ($name) |
|
9 | 2326 |
$name = wp_unslash( $args['name'] ); |
5 | 2327 |
$description = wp_unslash( $args['description'] ); |
9 | 2328 |
$parent = (int) $args['parent']; |
5 | 2329 |
|
2330 |
$slug_provided = ! empty( $args['slug'] ); |
|
2331 |
if ( ! $slug_provided ) { |
|
2332 |
$slug = sanitize_title( $name ); |
|
2333 |
} else { |
|
2334 |
$slug = $args['slug']; |
|
2335 |
} |
|
0 | 2336 |
|
2337 |
$term_group = 0; |
|
5 | 2338 |
if ( $args['alias_of'] ) { |
2339 |
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); |
|
2340 |
if ( ! empty( $alias->term_group ) ) { |
|
0 | 2341 |
// The alias we want is already in a group, so let's use that one. |
2342 |
$term_group = $alias->term_group; |
|
5 | 2343 |
} elseif ( ! empty( $alias->term_id ) ) { |
2344 |
/* |
|
2345 |
* The alias is not in a group, so we create a new one |
|
2346 |
* and add the alias to it. |
|
2347 |
*/ |
|
9 | 2348 |
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; |
2349 |
||
2350 |
wp_update_term( |
|
2351 |
$alias->term_id, |
|
2352 |
$taxonomy, |
|
2353 |
array( |
|
2354 |
'term_group' => $term_group, |
|
2355 |
) |
|
2356 |
); |
|
0 | 2357 |
} |
2358 |
} |
|
2359 |
||
5 | 2360 |
/* |
2361 |
* Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy, |
|
2362 |
* unless a unique slug has been explicitly provided. |
|
2363 |
*/ |
|
9 | 2364 |
$name_matches = get_terms( |
2365 |
array( |
|
16 | 2366 |
'taxonomy' => $taxonomy, |
9 | 2367 |
'name' => $name, |
2368 |
'hide_empty' => false, |
|
2369 |
'parent' => $args['parent'], |
|
2370 |
'update_term_meta_cache' => false, |
|
2371 |
) |
|
2372 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2373 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2374 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2375 |
* The `name` match in `get_terms()` doesn't differentiate accented characters, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2376 |
* so we do a stricter comparison here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2377 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2378 |
$name_match = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2379 |
if ( $name_matches ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2380 |
foreach ( $name_matches as $_match ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2381 |
if ( strtolower( $name ) === strtolower( $_match->name ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2382 |
$name_match = $_match; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2383 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2384 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2385 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2386 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2387 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2388 |
if ( $name_match ) { |
5 | 2389 |
$slug_match = get_term_by( 'slug', $slug, $taxonomy ); |
2390 |
if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { |
|
2391 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
9 | 2392 |
$siblings = get_terms( |
2393 |
array( |
|
16 | 2394 |
'taxonomy' => $taxonomy, |
9 | 2395 |
'get' => 'all', |
2396 |
'parent' => $parent, |
|
2397 |
'update_term_meta_cache' => false, |
|
2398 |
) |
|
2399 |
); |
|
5 | 2400 |
|
2401 |
$existing_term = null; |
|
16 | 2402 |
$sibling_names = wp_list_pluck( $siblings, 'name' ); |
2403 |
$sibling_slugs = wp_list_pluck( $siblings, 'slug' ); |
|
2404 |
||
2405 |
if ( ( ! $slug_provided || $name_match->slug === $slug ) && in_array( $name, $sibling_names, true ) ) { |
|
5 | 2406 |
$existing_term = $name_match; |
16 | 2407 |
} elseif ( $slug_match && in_array( $slug, $sibling_slugs, true ) ) { |
5 | 2408 |
$existing_term = $slug_match; |
2409 |
} |
|
2410 |
||
2411 |
if ( $existing_term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2412 |
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $existing_term->term_id ); |
5 | 2413 |
} |
0 | 2414 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2415 |
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists in this taxonomy.' ), $name_match->term_id ); |
0 | 2416 |
} |
2417 |
} |
|
2418 |
} |
|
2419 |
||
5 | 2420 |
$slug = wp_unique_term_slug( $slug, (object) $args ); |
2421 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2422 |
$data = compact( 'name', 'slug', 'term_group' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2423 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2424 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2425 |
* Filters term data before it is inserted into the database. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2426 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2427 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2428 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2429 |
* @param array $data Term data to be inserted. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2430 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2431 |
* @param array $args Arguments passed to wp_insert_term(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2432 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2433 |
$data = apply_filters( 'wp_insert_term_data', $data, $taxonomy, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2434 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2435 |
if ( false === $wpdb->insert( $wpdb->terms, $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2436 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database.' ), $wpdb->last_error ); |
5 | 2437 |
} |
2438 |
||
2439 |
$term_id = (int) $wpdb->insert_id; |
|
2440 |
||
16 | 2441 |
// Seems unreachable. However, is used in the case that a term name is provided, which sanitizes to an empty string. |
9 | 2442 |
if ( empty( $slug ) ) { |
2443 |
$slug = sanitize_title( $slug, $term_id ); |
|
5 | 2444 |
|
2445 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
0 | 2446 |
do_action( 'edit_terms', $term_id, $taxonomy ); |
2447 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
5 | 2448 |
|
2449 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
0 | 2450 |
do_action( 'edited_terms', $term_id, $taxonomy ); |
2451 |
} |
|
2452 |
||
2453 |
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) ); |
|
2454 |
||
9 | 2455 |
if ( ! empty( $tt_id ) ) { |
2456 |
return array( |
|
2457 |
'term_id' => $term_id, |
|
2458 |
'term_taxonomy_id' => $tt_id, |
|
2459 |
); |
|
5 | 2460 |
} |
16 | 2461 |
|
2462 |
if ( false === $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ) ) { |
|
2463 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term taxonomy into the database.' ), $wpdb->last_error ); |
|
2464 |
} |
|
2465 |
||
0 | 2466 |
$tt_id = (int) $wpdb->insert_id; |
2467 |
||
5 | 2468 |
/* |
2469 |
* Sanity check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than |
|
2470 |
* an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id |
|
2471 |
* and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks |
|
2472 |
* are not fired. |
|
2473 |
*/ |
|
9 | 2474 |
$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, t.slug, tt.term_taxonomy_id, tt.taxonomy FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) ); |
2475 |
||
2476 |
/** |
|
2477 |
* Filters the duplicate term check that takes place during term creation. |
|
2478 |
* |
|
2479 |
* Term parent+taxonomy+slug combinations are meant to be unique, and wp_insert_term() |
|
2480 |
* performs a last-minute confirmation of this uniqueness before allowing a new term |
|
2481 |
* to be created. Plugins with different uniqueness requirements may use this filter |
|
2482 |
* to bypass or modify the duplicate-term check. |
|
2483 |
* |
|
2484 |
* @since 5.1.0 |
|
2485 |
* |
|
2486 |
* @param object $duplicate_term Duplicate term row from terms table, if found. |
|
2487 |
* @param string $term Term being inserted. |
|
2488 |
* @param string $taxonomy Taxonomy name. |
|
2489 |
* @param array $args Term arguments passed to the function. |
|
2490 |
* @param int $tt_id term_taxonomy_id for the newly created term. |
|
2491 |
*/ |
|
2492 |
$duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id ); |
|
2493 |
||
5 | 2494 |
if ( $duplicate_term ) { |
2495 |
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) ); |
|
2496 |
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); |
|
2497 |
||
2498 |
$term_id = (int) $duplicate_term->term_id; |
|
2499 |
$tt_id = (int) $duplicate_term->term_taxonomy_id; |
|
2500 |
||
2501 |
clean_term_cache( $term_id, $taxonomy ); |
|
9 | 2502 |
return array( |
2503 |
'term_id' => $term_id, |
|
2504 |
'term_taxonomy_id' => $tt_id, |
|
2505 |
); |
|
5 | 2506 |
} |
2507 |
||
2508 |
/** |
|
2509 |
* Fires immediately after a new term is created, before the term cache is cleaned. |
|
2510 |
* |
|
18 | 2511 |
* The {@see 'create_$taxonomy'} hook is also available for targeting a specific |
2512 |
* taxonomy. |
|
2513 |
* |
|
5 | 2514 |
* @since 2.3.0 |
2515 |
* |
|
2516 |
* @param int $term_id Term ID. |
|
2517 |
* @param int $tt_id Term taxonomy ID. |
|
2518 |
* @param string $taxonomy Taxonomy slug. |
|
2519 |
*/ |
|
9 | 2520 |
do_action( 'create_term', $term_id, $tt_id, $taxonomy ); |
5 | 2521 |
|
2522 |
/** |
|
2523 |
* Fires after a new term is created for a specific taxonomy. |
|
2524 |
* |
|
2525 |
* The dynamic portion of the hook name, `$taxonomy`, refers |
|
2526 |
* to the slug of the taxonomy the term was created for. |
|
2527 |
* |
|
18 | 2528 |
* Possible hook names include: |
2529 |
* |
|
2530 |
* - `create_category` |
|
2531 |
* - `create_post_tag` |
|
2532 |
* |
|
5 | 2533 |
* @since 2.3.0 |
2534 |
* |
|
2535 |
* @param int $term_id Term ID. |
|
2536 |
* @param int $tt_id Term taxonomy ID. |
|
2537 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2538 |
do_action( "create_{$taxonomy}", $term_id, $tt_id ); |
5 | 2539 |
|
2540 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2541 |
* Filters the term ID after a new term is created. |
5 | 2542 |
* |
2543 |
* @since 2.3.0 |
|
2544 |
* |
|
2545 |
* @param int $term_id Term ID. |
|
16 | 2546 |
* @param int $tt_id Term taxonomy ID. |
5 | 2547 |
*/ |
2548 |
$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); |
|
0 | 2549 |
|
9 | 2550 |
clean_term_cache( $term_id, $taxonomy ); |
0 | 2551 |
|
5 | 2552 |
/** |
2553 |
* Fires after a new term is created, and after the term cache has been cleaned. |
|
2554 |
* |
|
18 | 2555 |
* The {@see 'created_$taxonomy'} hook is also available for targeting a specific |
2556 |
* taxonomy. |
|
2557 |
* |
|
5 | 2558 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2559 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2560 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2561 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2562 |
* @param string $taxonomy Taxonomy slug. |
5 | 2563 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2564 |
do_action( 'created_term', $term_id, $tt_id, $taxonomy ); |
5 | 2565 |
|
2566 |
/** |
|
2567 |
* Fires after a new term in a specific taxonomy is created, and after the term |
|
2568 |
* cache has been cleaned. |
|
2569 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2570 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2571 |
* |
18 | 2572 |
* Possible hook names include: |
2573 |
* |
|
2574 |
* - `created_category` |
|
2575 |
* - `created_post_tag` |
|
2576 |
* |
|
5 | 2577 |
* @since 2.3.0 |
2578 |
* |
|
2579 |
* @param int $term_id Term ID. |
|
2580 |
* @param int $tt_id Term taxonomy ID. |
|
2581 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2582 |
do_action( "created_{$taxonomy}", $term_id, $tt_id ); |
0 | 2583 |
|
16 | 2584 |
/** |
2585 |
* Fires after a term has been saved, and the term cache has been cleared. |
|
2586 |
* |
|
18 | 2587 |
* The {@see 'saved_$taxonomy'} hook is also available for targeting a specific |
2588 |
* taxonomy. |
|
2589 |
* |
|
16 | 2590 |
* @since 5.5.0 |
2591 |
* |
|
2592 |
* @param int $term_id Term ID. |
|
2593 |
* @param int $tt_id Term taxonomy ID. |
|
2594 |
* @param string $taxonomy Taxonomy slug. |
|
2595 |
* @param bool $update Whether this is an existing term being updated. |
|
2596 |
*/ |
|
2597 |
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false ); |
|
2598 |
||
2599 |
/** |
|
2600 |
* Fires after a term in a specific taxonomy has been saved, and the term |
|
2601 |
* cache has been cleared. |
|
2602 |
* |
|
2603 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
2604 |
* |
|
18 | 2605 |
* Possible hook names include: |
2606 |
* |
|
2607 |
* - `saved_category` |
|
2608 |
* - `saved_post_tag` |
|
2609 |
* |
|
16 | 2610 |
* @since 5.5.0 |
2611 |
* |
|
2612 |
* @param int $term_id Term ID. |
|
2613 |
* @param int $tt_id Term taxonomy ID. |
|
2614 |
* @param bool $update Whether this is an existing term being updated. |
|
2615 |
*/ |
|
2616 |
do_action( "saved_{$taxonomy}", $term_id, $tt_id, false ); |
|
2617 |
||
9 | 2618 |
return array( |
2619 |
'term_id' => $term_id, |
|
2620 |
'term_taxonomy_id' => $tt_id, |
|
2621 |
); |
|
0 | 2622 |
} |
2623 |
||
2624 |
/** |
|
2625 |
* Create Term and Taxonomy Relationships. |
|
2626 |
* |
|
2627 |
* Relates an object (post, link etc) to a term and taxonomy type. Creates the |
|
2628 |
* term and taxonomy relationship if it doesn't already exist. Creates a term if |
|
2629 |
* it doesn't exist (using the slug). |
|
2630 |
* |
|
2631 |
* A relationship means that the term is grouped in or belongs to the taxonomy. |
|
2632 |
* A term has no meaning until it is given context by defining which taxonomy it |
|
2633 |
* exists under. |
|
2634 |
* |
|
2635 |
* @since 2.3.0 |
|
5 | 2636 |
* |
16 | 2637 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2638 |
* |
5 | 2639 |
* @param int $object_id The object to relate to. |
16 | 2640 |
* @param string|int|array $terms A single term slug, single term ID, or array of either term slugs or IDs. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2641 |
* Will replace all existing related terms in this taxonomy. Passing an |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2642 |
* empty value will remove all related terms. |
5 | 2643 |
* @param string $taxonomy The context in which to relate the term to the object. |
2644 |
* @param bool $append Optional. If false will delete difference of terms. Default false. |
|
16 | 2645 |
* @return array|WP_Error Term taxonomy IDs of the affected terms or WP_Error on failure. |
0 | 2646 |
*/ |
5 | 2647 |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
0 | 2648 |
global $wpdb; |
2649 |
||
2650 |
$object_id = (int) $object_id; |
|
2651 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2652 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2653 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2654 |
} |
0 | 2655 |
|
9 | 2656 |
if ( ! is_array( $terms ) ) { |
2657 |
$terms = array( $terms ); |
|
2658 |
} |
|
2659 |
||
2660 |
if ( ! $append ) { |
|
2661 |
$old_tt_ids = wp_get_object_terms( |
|
2662 |
$object_id, |
|
2663 |
$taxonomy, |
|
2664 |
array( |
|
2665 |
'fields' => 'tt_ids', |
|
2666 |
'orderby' => 'none', |
|
2667 |
'update_term_meta_cache' => false, |
|
2668 |
) |
|
2669 |
); |
|
2670 |
} else { |
|
0 | 2671 |
$old_tt_ids = array(); |
9 | 2672 |
} |
2673 |
||
2674 |
$tt_ids = array(); |
|
2675 |
$term_ids = array(); |
|
0 | 2676 |
$new_tt_ids = array(); |
2677 |
||
9 | 2678 |
foreach ( (array) $terms as $term ) { |
16 | 2679 |
if ( '' === trim( $term ) ) { |
0 | 2680 |
continue; |
9 | 2681 |
} |
2682 |
||
16 | 2683 |
$term_info = term_exists( $term, $taxonomy ); |
2684 |
||
2685 |
if ( ! $term_info ) { |
|
0 | 2686 |
// Skip if a non-existent term ID is passed. |
9 | 2687 |
if ( is_int( $term ) ) { |
0 | 2688 |
continue; |
9 | 2689 |
} |
16 | 2690 |
|
9 | 2691 |
$term_info = wp_insert_term( $term, $taxonomy ); |
0 | 2692 |
} |
16 | 2693 |
|
9 | 2694 |
if ( is_wp_error( $term_info ) ) { |
0 | 2695 |
return $term_info; |
9 | 2696 |
} |
16 | 2697 |
|
0 | 2698 |
$term_ids[] = $term_info['term_id']; |
9 | 2699 |
$tt_id = $term_info['term_taxonomy_id']; |
2700 |
$tt_ids[] = $tt_id; |
|
2701 |
||
2702 |
if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) { |
|
0 | 2703 |
continue; |
9 | 2704 |
} |
5 | 2705 |
|
2706 |
/** |
|
2707 |
* Fires immediately before an object-term relationship is added. |
|
2708 |
* |
|
2709 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2710 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2711 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2712 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2713 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2714 |
* @param string $taxonomy Taxonomy slug. |
5 | 2715 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2716 |
do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy ); |
16 | 2717 |
|
9 | 2718 |
$wpdb->insert( |
2719 |
$wpdb->term_relationships, |
|
2720 |
array( |
|
2721 |
'object_id' => $object_id, |
|
2722 |
'term_taxonomy_id' => $tt_id, |
|
2723 |
) |
|
2724 |
); |
|
5 | 2725 |
|
2726 |
/** |
|
2727 |
* Fires immediately after an object-term relationship is added. |
|
2728 |
* |
|
2729 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2730 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2731 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2732 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2733 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2734 |
* @param string $taxonomy Taxonomy slug. |
5 | 2735 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2736 |
do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy ); |
16 | 2737 |
|
0 | 2738 |
$new_tt_ids[] = $tt_id; |
2739 |
} |
|
2740 |
||
9 | 2741 |
if ( $new_tt_ids ) { |
0 | 2742 |
wp_update_term_count( $new_tt_ids, $taxonomy ); |
9 | 2743 |
} |
0 | 2744 |
|
2745 |
if ( ! $append ) { |
|
2746 |
$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids ); |
|
2747 |
||
2748 |
if ( $delete_tt_ids ) { |
|
2749 |
$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; |
|
9 | 2750 |
$delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) ); |
2751 |
$delete_term_ids = array_map( 'intval', $delete_term_ids ); |
|
0 | 2752 |
|
2753 |
$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); |
|
2754 |
if ( is_wp_error( $remove ) ) { |
|
2755 |
return $remove; |
|
2756 |
} |
|
2757 |
} |
|
2758 |
} |
|
2759 |
||
9 | 2760 |
$t = get_taxonomy( $taxonomy ); |
16 | 2761 |
|
9 | 2762 |
if ( ! $append && isset( $t->sort ) && $t->sort ) { |
16 | 2763 |
$values = array(); |
2764 |
$term_order = 0; |
|
2765 |
||
9 | 2766 |
$final_tt_ids = wp_get_object_terms( |
2767 |
$object_id, |
|
2768 |
$taxonomy, |
|
2769 |
array( |
|
2770 |
'fields' => 'tt_ids', |
|
2771 |
'update_term_meta_cache' => false, |
|
2772 |
) |
|
2773 |
); |
|
16 | 2774 |
|
9 | 2775 |
foreach ( $tt_ids as $tt_id ) { |
16 | 2776 |
if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) { |
9 | 2777 |
$values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order ); |
2778 |
} |
|
2779 |
} |
|
16 | 2780 |
|
9 | 2781 |
if ( $values ) { |
18 | 2782 |
if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . implode( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2783 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error ); |
9 | 2784 |
} |
2785 |
} |
|
0 | 2786 |
} |
2787 |
||
2788 |
wp_cache_delete( $object_id, $taxonomy . '_relationships' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2789 |
wp_cache_delete( 'last_changed', 'terms' ); |
0 | 2790 |
|
5 | 2791 |
/** |
2792 |
* Fires after an object's terms have been set. |
|
2793 |
* |
|
2794 |
* @since 2.8.0 |
|
2795 |
* |
|
2796 |
* @param int $object_id Object ID. |
|
18 | 2797 |
* @param array $terms An array of object term IDs or slugs. |
5 | 2798 |
* @param array $tt_ids An array of term taxonomy IDs. |
2799 |
* @param string $taxonomy Taxonomy slug. |
|
2800 |
* @param bool $append Whether to append new terms to the old terms. |
|
2801 |
* @param array $old_tt_ids Old array of term taxonomy IDs. |
|
2802 |
*/ |
|
2803 |
do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); |
|
16 | 2804 |
|
0 | 2805 |
return $tt_ids; |
2806 |
} |
|
2807 |
||
2808 |
/** |
|
2809 |
* Add term(s) associated with a given object. |
|
2810 |
* |
|
5 | 2811 |
* @since 3.6.0 |
0 | 2812 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2813 |
* @param int $object_id The ID of the object to which the terms will be added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2814 |
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to add. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2815 |
* @param array|string $taxonomy Taxonomy name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2816 |
* @return array|WP_Error Term taxonomy IDs of the affected terms. |
0 | 2817 |
*/ |
2818 |
function wp_add_object_terms( $object_id, $terms, $taxonomy ) { |
|
2819 |
return wp_set_object_terms( $object_id, $terms, $taxonomy, true ); |
|
2820 |
} |
|
2821 |
||
2822 |
/** |
|
2823 |
* Remove term(s) associated with a given object. |
|
2824 |
* |
|
5 | 2825 |
* @since 3.6.0 |
2826 |
* |
|
2827 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 2828 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2829 |
* @param int $object_id The ID of the object from which the terms will be removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2830 |
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to remove. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2831 |
* @param array|string $taxonomy Taxonomy name. |
0 | 2832 |
* @return bool|WP_Error True on success, false or WP_Error on failure. |
2833 |
*/ |
|
2834 |
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { |
|
2835 |
global $wpdb; |
|
2836 |
||
2837 |
$object_id = (int) $object_id; |
|
2838 |
||
2839 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2840 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
0 | 2841 |
} |
2842 |
||
2843 |
if ( ! is_array( $terms ) ) { |
|
2844 |
$terms = array( $terms ); |
|
2845 |
} |
|
2846 |
||
2847 |
$tt_ids = array(); |
|
2848 |
||
2849 |
foreach ( (array) $terms as $term ) { |
|
16 | 2850 |
if ( '' === trim( $term ) ) { |
0 | 2851 |
continue; |
2852 |
} |
|
2853 |
||
16 | 2854 |
$term_info = term_exists( $term, $taxonomy ); |
2855 |
if ( ! $term_info ) { |
|
0 | 2856 |
// Skip if a non-existent term ID is passed. |
2857 |
if ( is_int( $term ) ) { |
|
2858 |
continue; |
|
2859 |
} |
|
2860 |
} |
|
2861 |
||
2862 |
if ( is_wp_error( $term_info ) ) { |
|
2863 |
return $term_info; |
|
2864 |
} |
|
2865 |
||
2866 |
$tt_ids[] = $term_info['term_taxonomy_id']; |
|
2867 |
} |
|
2868 |
||
2869 |
if ( $tt_ids ) { |
|
2870 |
$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; |
|
5 | 2871 |
|
2872 |
/** |
|
2873 |
* Fires immediately before an object-term relationship is deleted. |
|
2874 |
* |
|
2875 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2876 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2877 |
* |
2878 |
* @param int $object_id Object ID. |
|
2879 |
* @param array $tt_ids An array of term taxonomy IDs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2880 |
* @param string $taxonomy Taxonomy slug. |
5 | 2881 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2882 |
do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy ); |
16 | 2883 |
|
0 | 2884 |
$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) ); |
5 | 2885 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2886 |
wp_cache_delete( $object_id, $taxonomy . '_relationships' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2887 |
wp_cache_delete( 'last_changed', 'terms' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2888 |
|
5 | 2889 |
/** |
2890 |
* Fires immediately after an object-term relationship is deleted. |
|
2891 |
* |
|
2892 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2893 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2894 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2895 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2896 |
* @param array $tt_ids An array of term taxonomy IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2897 |
* @param string $taxonomy Taxonomy slug. |
5 | 2898 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2899 |
do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2900 |
|
0 | 2901 |
wp_update_term_count( $tt_ids, $taxonomy ); |
2902 |
||
2903 |
return (bool) $deleted; |
|
2904 |
} |
|
2905 |
||
2906 |
return false; |
|
2907 |
} |
|
2908 |
||
2909 |
/** |
|
2910 |
* Will make slug unique, if it isn't already. |
|
2911 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2912 |
* The `$slug` has to be unique global to every taxonomy, meaning that one |
0 | 2913 |
* taxonomy term can't have a matching slug with another taxonomy term. Each |
2914 |
* slug has to be globally unique for every taxonomy. |
|
2915 |
* |
|
2916 |
* The way this works is that if the taxonomy that the term belongs to is |
|
2917 |
* hierarchical and has a parent, it will append that parent to the $slug. |
|
2918 |
* |
|
9 | 2919 |
* If that still doesn't return a unique slug, then it tries to append a number |
0 | 2920 |
* until it finds a number that is truly unique. |
2921 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2922 |
* The only purpose for `$term` is for appending a parent, if one exists. |
0 | 2923 |
* |
2924 |
* @since 2.3.0 |
|
5 | 2925 |
* |
2926 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 2927 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2928 |
* @param string $slug The string that will be tried for a unique slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2929 |
* @param object $term The term object that the `$slug` will belong to. |
0 | 2930 |
* @return string Will return a true unique slug. |
2931 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2932 |
function wp_unique_term_slug( $slug, $term ) { |
0 | 2933 |
global $wpdb; |
2934 |
||
9 | 2935 |
$needs_suffix = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2936 |
$original_slug = $slug; |
0 | 2937 |
|
5 | 2938 |
// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2939 |
if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2940 |
$needs_suffix = false; |
5 | 2941 |
} |
2942 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2943 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2944 |
* If the taxonomy supports hierarchy and the term has a parent, make the slug unique |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2945 |
* by incorporating parent slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2946 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2947 |
$parent_suffix = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2948 |
if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) { |
0 | 2949 |
$the_parent = $term->parent; |
9 | 2950 |
while ( ! empty( $the_parent ) ) { |
2951 |
$parent_term = get_term( $the_parent, $term->taxonomy ); |
|
2952 |
if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) { |
|
0 | 2953 |
break; |
9 | 2954 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2955 |
$parent_suffix .= '-' . $parent_term->slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2956 |
if ( ! term_exists( $slug . $parent_suffix ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2957 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2958 |
} |
0 | 2959 |
|
9 | 2960 |
if ( empty( $parent_term->parent ) ) { |
0 | 2961 |
break; |
9 | 2962 |
} |
0 | 2963 |
$the_parent = $parent_term->parent; |
2964 |
} |
|
2965 |
} |
|
2966 |
||
2967 |
// If we didn't get a unique slug, try appending a number to make it unique. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2968 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2969 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2970 |
* Filters whether the proposed unique term slug is bad. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2971 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2972 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2973 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2974 |
* @param bool $needs_suffix Whether the slug needs to be made unique with a suffix. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2975 |
* @param string $slug The slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2976 |
* @param object $term Term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2977 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2978 |
if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2979 |
if ( $parent_suffix ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2980 |
$slug .= $parent_suffix; |
16 | 2981 |
} |
2982 |
||
2983 |
if ( ! empty( $term->term_id ) ) { |
|
2984 |
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2985 |
} else { |
16 | 2986 |
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
2987 |
} |
|
2988 |
||
2989 |
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
2990 |
$num = 2; |
|
2991 |
do { |
|
2992 |
$alt_slug = $slug . "-$num"; |
|
2993 |
$num++; |
|
2994 |
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
|
2995 |
} while ( $slug_check ); |
|
2996 |
$slug = $alt_slug; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2997 |
} |
0 | 2998 |
} |
2999 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3000 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3001 |
* Filters the unique term slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3002 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3003 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3004 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3005 |
* @param string $slug Unique term slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3006 |
* @param object $term Term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3007 |
* @param string $original_slug Slug originally passed to the function for testing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3008 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3009 |
return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug ); |
0 | 3010 |
} |
3011 |
||
3012 |
/** |
|
3013 |
* Update term based on arguments provided. |
|
3014 |
* |
|
16 | 3015 |
* The `$args` will indiscriminately override all values with the same field name. |
0 | 3016 |
* Care must be taken to not override important information need to update or |
3017 |
* update will fail (or perhaps create a new term, neither would be acceptable). |
|
3018 |
* |
|
3019 |
* Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not |
|
16 | 3020 |
* defined in `$args` already. |
3021 |
* |
|
3022 |
* 'alias_of' will create a term group, if it doesn't already exist, and |
|
3023 |
* update it for the `$term`. |
|
3024 |
* |
|
3025 |
* If the 'slug' argument in `$args` is missing, then the 'name' will be used. |
|
3026 |
* If you set 'slug' and it isn't unique, then a WP_Error is returned. |
|
3027 |
* If you don't pass any slug, then a unique one will be created. |
|
0 | 3028 |
* |
3029 |
* @since 2.3.0 |
|
3030 |
* |
|
5 | 3031 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 3032 |
* |
16 | 3033 |
* @param int $term_id The ID of the term. |
3034 |
* @param string $taxonomy The taxonomy of the term. |
|
3035 |
* @param array|string $args { |
|
3036 |
* Optional. Array or string of arguments for updating a term. |
|
3037 |
* |
|
3038 |
* @type string $alias_of Slug of the term to make this term an alias of. |
|
3039 |
* Default empty string. Accepts a term slug. |
|
3040 |
* @type string $description The term description. Default empty string. |
|
3041 |
* @type int $parent The id of the parent term. Default 0. |
|
3042 |
* @type string $slug The term slug to use. Default empty string. |
|
3043 |
* } |
|
3044 |
* @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`, |
|
3045 |
* WP_Error otherwise. |
|
0 | 3046 |
*/ |
3047 |
function wp_update_term( $term_id, $taxonomy, $args = array() ) { |
|
3048 |
global $wpdb; |
|
3049 |
||
5 | 3050 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3051 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
5 | 3052 |
} |
0 | 3053 |
|
3054 |
$term_id = (int) $term_id; |
|
3055 |
||
16 | 3056 |
// First, get all of the original args. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3057 |
$term = get_term( $term_id, $taxonomy ); |
5 | 3058 |
|
3059 |
if ( is_wp_error( $term ) ) { |
|
0 | 3060 |
return $term; |
5 | 3061 |
} |
3062 |
||
3063 |
if ( ! $term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3064 |
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); |
5 | 3065 |
} |
0 | 3066 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3067 |
$term = (array) $term->data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3068 |
|
0 | 3069 |
// Escape data pulled from DB. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3070 |
$term = wp_slash( $term ); |
0 | 3071 |
|
3072 |
// Merge old and new args with new args overwriting old ones. |
|
9 | 3073 |
$args = array_merge( $term, $args ); |
3074 |
||
3075 |
$defaults = array( |
|
3076 |
'alias_of' => '', |
|
3077 |
'description' => '', |
|
3078 |
'parent' => 0, |
|
3079 |
'slug' => '', |
|
3080 |
); |
|
3081 |
$args = wp_parse_args( $args, $defaults ); |
|
3082 |
$args = sanitize_term( $args, $taxonomy, 'db' ); |
|
5 | 3083 |
$parsed_args = $args; |
0 | 3084 |
|
3085 |
// expected_slashed ($name) |
|
9 | 3086 |
$name = wp_unslash( $args['name'] ); |
5 | 3087 |
$description = wp_unslash( $args['description'] ); |
3088 |
||
9 | 3089 |
$parsed_args['name'] = $name; |
5 | 3090 |
$parsed_args['description'] = $description; |
0 | 3091 |
|
16 | 3092 |
if ( '' === trim( $name ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3093 |
return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3094 |
} |
0 | 3095 |
|
18 | 3096 |
if ( (int) $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) { |
5 | 3097 |
return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); |
3098 |
} |
|
3099 |
||
0 | 3100 |
$empty_slug = false; |
5 | 3101 |
if ( empty( $args['slug'] ) ) { |
0 | 3102 |
$empty_slug = true; |
9 | 3103 |
$slug = sanitize_title( $name ); |
5 | 3104 |
} else { |
3105 |
$slug = $args['slug']; |
|
0 | 3106 |
} |
3107 |
||
5 | 3108 |
$parsed_args['slug'] = $slug; |
3109 |
||
3110 |
$term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0; |
|
3111 |
if ( $args['alias_of'] ) { |
|
3112 |
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); |
|
3113 |
if ( ! empty( $alias->term_group ) ) { |
|
0 | 3114 |
// The alias we want is already in a group, so let's use that one. |
3115 |
$term_group = $alias->term_group; |
|
5 | 3116 |
} elseif ( ! empty( $alias->term_id ) ) { |
3117 |
/* |
|
3118 |
* The alias is not in a group, so we create a new one |
|
3119 |
* and add the alias to it. |
|
3120 |
*/ |
|
9 | 3121 |
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; |
3122 |
||
3123 |
wp_update_term( |
|
3124 |
$alias->term_id, |
|
3125 |
$taxonomy, |
|
3126 |
array( |
|
3127 |
'term_group' => $term_group, |
|
3128 |
) |
|
3129 |
); |
|
0 | 3130 |
} |
5 | 3131 |
|
3132 |
$parsed_args['term_group'] = $term_group; |
|
0 | 3133 |
} |
3134 |
||
5 | 3135 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3136 |
* Filters the term parent. |
5 | 3137 |
* |
3138 |
* Hook to this filter to see if it will cause a hierarchy loop. |
|
3139 |
* |
|
3140 |
* @since 3.1.0 |
|
3141 |
* |
|
3142 |
* @param int $parent ID of the parent term. |
|
3143 |
* @param int $term_id Term ID. |
|
3144 |
* @param string $taxonomy Taxonomy slug. |
|
3145 |
* @param array $parsed_args An array of potentially altered update arguments for the given term. |
|
3146 |
* @param array $args An array of update arguments for the given term. |
|
3147 |
*/ |
|
16 | 3148 |
$parent = (int) apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args ); |
3149 |
||
3150 |
// Check for duplicate slug. |
|
5 | 3151 |
$duplicate = get_term_by( 'slug', $slug, $taxonomy ); |
16 | 3152 |
if ( $duplicate && $duplicate->term_id !== $term_id ) { |
0 | 3153 |
// If an empty slug was passed or the parent changed, reset the slug to something unique. |
3154 |
// Otherwise, bail. |
|
16 | 3155 |
if ( $empty_slug || ( $parent !== (int) $term['parent'] ) ) { |
9 | 3156 |
$slug = wp_unique_term_slug( $slug, (object) $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3157 |
} else { |
16 | 3158 |
/* translators: %s: Taxonomy term slug. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3159 |
return new WP_Error( 'duplicate_term_slug', sprintf( __( 'The slug “%s” is already in use by another term.' ), $slug ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3160 |
} |
0 | 3161 |
} |
5 | 3162 |
|
9 | 3163 |
$tt_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) ); |
5 | 3164 |
|
3165 |
// Check whether this is a shared term that needs splitting. |
|
3166 |
$_term_id = _split_shared_term( $term_id, $tt_id ); |
|
3167 |
if ( ! is_wp_error( $_term_id ) ) { |
|
3168 |
$term_id = $_term_id; |
|
3169 |
} |
|
3170 |
||
3171 |
/** |
|
3172 |
* Fires immediately before the given terms are edited. |
|
3173 |
* |
|
3174 |
* @since 2.9.0 |
|
3175 |
* |
|
3176 |
* @param int $term_id Term ID. |
|
3177 |
* @param string $taxonomy Taxonomy slug. |
|
3178 |
*/ |
|
0 | 3179 |
do_action( 'edit_terms', $term_id, $taxonomy ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3181 |
$data = compact( 'name', 'slug', 'term_group' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3182 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3183 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3184 |
* Filters term data before it is updated in the database. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3186 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3187 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3188 |
* @param array $data Term data to be updated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3189 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3190 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3191 |
* @param array $args Arguments passed to wp_update_term(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3192 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3193 |
$data = apply_filters( 'wp_update_term_data', $data, $term_id, $taxonomy, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3194 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3195 |
$wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) ); |
16 | 3196 |
|
9 | 3197 |
if ( empty( $slug ) ) { |
3198 |
$slug = sanitize_title( $name, $term_id ); |
|
0 | 3199 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
3200 |
} |
|
5 | 3201 |
|
3202 |
/** |
|
18 | 3203 |
* Fires immediately after a term is updated in the database, but before its |
3204 |
* term-taxonomy relationship is updated. |
|
5 | 3205 |
* |
3206 |
* @since 2.9.0 |
|
3207 |
* |
|
3208 |
* @param int $term_id Term ID |
|
3209 |
* @param string $taxonomy Taxonomy slug. |
|
3210 |
*/ |
|
0 | 3211 |
do_action( 'edited_terms', $term_id, $taxonomy ); |
3212 |
||
5 | 3213 |
/** |
3214 |
* Fires immediate before a term-taxonomy relationship is updated. |
|
3215 |
* |
|
3216 |
* @since 2.9.0 |
|
3217 |
* |
|
3218 |
* @param int $tt_id Term taxonomy ID. |
|
3219 |
* @param string $taxonomy Taxonomy slug. |
|
3220 |
*/ |
|
0 | 3221 |
do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3222 |
|
0 | 3223 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); |
5 | 3224 |
|
3225 |
/** |
|
3226 |
* Fires immediately after a term-taxonomy relationship is updated. |
|
3227 |
* |
|
3228 |
* @since 2.9.0 |
|
3229 |
* |
|
3230 |
* @param int $tt_id Term taxonomy ID. |
|
3231 |
* @param string $taxonomy Taxonomy slug. |
|
3232 |
*/ |
|
0 | 3233 |
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); |
3234 |
||
5 | 3235 |
/** |
3236 |
* Fires after a term has been updated, but before the term cache has been cleaned. |
|
3237 |
* |
|
18 | 3238 |
* The {@see 'edit_$taxonomy'} hook is also available for targeting a specific |
3239 |
* taxonomy. |
|
3240 |
* |
|
5 | 3241 |
* @since 2.3.0 |
3242 |
* |
|
3243 |
* @param int $term_id Term ID. |
|
3244 |
* @param int $tt_id Term taxonomy ID. |
|
3245 |
* @param string $taxonomy Taxonomy slug. |
|
3246 |
*/ |
|
9 | 3247 |
do_action( 'edit_term', $term_id, $tt_id, $taxonomy ); |
5 | 3248 |
|
3249 |
/** |
|
3250 |
* Fires after a term in a specific taxonomy has been updated, but before the term |
|
3251 |
* cache has been cleaned. |
|
3252 |
* |
|
3253 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
3254 |
* |
|
18 | 3255 |
* Possible hook names include: |
3256 |
* |
|
3257 |
* - `edit_category` |
|
3258 |
* - `edit_post_tag` |
|
3259 |
* |
|
5 | 3260 |
* @since 2.3.0 |
3261 |
* |
|
3262 |
* @param int $term_id Term ID. |
|
3263 |
* @param int $tt_id Term taxonomy ID. |
|
3264 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3265 |
do_action( "edit_{$taxonomy}", $term_id, $tt_id ); |
5 | 3266 |
|
3267 |
/** This filter is documented in wp-includes/taxonomy.php */ |
|
3268 |
$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); |
|
0 | 3269 |
|
9 | 3270 |
clean_term_cache( $term_id, $taxonomy ); |
0 | 3271 |
|
5 | 3272 |
/** |
3273 |
* Fires after a term has been updated, and the term cache has been cleaned. |
|
3274 |
* |
|
18 | 3275 |
* The {@see 'edited_$taxonomy'} hook is also available for targeting a specific |
3276 |
* taxonomy. |
|
3277 |
* |
|
5 | 3278 |
* @since 2.3.0 |
3279 |
* |
|
3280 |
* @param int $term_id Term ID. |
|
3281 |
* @param int $tt_id Term taxonomy ID. |
|
3282 |
* @param string $taxonomy Taxonomy slug. |
|
3283 |
*/ |
|
9 | 3284 |
do_action( 'edited_term', $term_id, $tt_id, $taxonomy ); |
5 | 3285 |
|
3286 |
/** |
|
3287 |
* Fires after a term for a specific taxonomy has been updated, and the term |
|
3288 |
* cache has been cleaned. |
|
3289 |
* |
|
3290 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
3291 |
* |
|
18 | 3292 |
* Possible hook names include: |
3293 |
* |
|
3294 |
* - `edited_category` |
|
3295 |
* - `edited_post_tag` |
|
3296 |
* |
|
5 | 3297 |
* @since 2.3.0 |
3298 |
* |
|
3299 |
* @param int $term_id Term ID. |
|
3300 |
* @param int $tt_id Term taxonomy ID. |
|
3301 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3302 |
do_action( "edited_{$taxonomy}", $term_id, $tt_id ); |
0 | 3303 |
|
16 | 3304 |
/** This action is documented in wp-includes/taxonomy.php */ |
3305 |
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, true ); |
|
3306 |
||
3307 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
3308 |
do_action( "saved_{$taxonomy}", $term_id, $tt_id, true ); |
|
3309 |
||
9 | 3310 |
return array( |
3311 |
'term_id' => $term_id, |
|
3312 |
'term_taxonomy_id' => $tt_id, |
|
3313 |
); |
|
0 | 3314 |
} |
3315 |
||
3316 |
/** |
|
3317 |
* Enable or disable term counting. |
|
3318 |
* |
|
3319 |
* @since 2.5.0 |
|
3320 |
* |
|
3321 |
* @param bool $defer Optional. Enable if true, disable if false. |
|
3322 |
* @return bool Whether term counting is enabled or disabled. |
|
3323 |
*/ |
|
9 | 3324 |
function wp_defer_term_counting( $defer = null ) { |
0 | 3325 |
static $_defer = false; |
3326 |
||
9 | 3327 |
if ( is_bool( $defer ) ) { |
0 | 3328 |
$_defer = $defer; |
16 | 3329 |
// Flush any deferred counts. |
9 | 3330 |
if ( ! $defer ) { |
0 | 3331 |
wp_update_term_count( null, null, true ); |
9 | 3332 |
} |
0 | 3333 |
} |
3334 |
||
3335 |
return $_defer; |
|
3336 |
} |
|
3337 |
||
3338 |
/** |
|
3339 |
* Updates the amount of terms in taxonomy. |
|
3340 |
* |
|
3341 |
* If there is a taxonomy callback applied, then it will be called for updating |
|
3342 |
* the count. |
|
3343 |
* |
|
3344 |
* The default action is to count what the amount of terms have the relationship |
|
3345 |
* of term ID. Once that is done, then update the database. |
|
3346 |
* |
|
3347 |
* @since 2.3.0 |
|
5 | 3348 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3349 |
* @param int|array $terms The term_taxonomy_id of the terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3350 |
* @param string $taxonomy The context of the term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3351 |
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false. |
0 | 3352 |
* @return bool If no terms will return false, and if successful will return true. |
3353 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3354 |
function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) { |
0 | 3355 |
static $_deferred = array(); |
3356 |
||
3357 |
if ( $do_deferred ) { |
|
9 | 3358 |
foreach ( (array) array_keys( $_deferred ) as $tax ) { |
3359 |
wp_update_term_count_now( $_deferred[ $tax ], $tax ); |
|
3360 |
unset( $_deferred[ $tax ] ); |
|
0 | 3361 |
} |
3362 |
} |
|
3363 |
||
9 | 3364 |
if ( empty( $terms ) ) { |
0 | 3365 |
return false; |
9 | 3366 |
} |
3367 |
||
3368 |
if ( ! is_array( $terms ) ) { |
|
3369 |
$terms = array( $terms ); |
|
3370 |
} |
|
0 | 3371 |
|
3372 |
if ( wp_defer_term_counting() ) { |
|
9 | 3373 |
if ( ! isset( $_deferred[ $taxonomy ] ) ) { |
3374 |
$_deferred[ $taxonomy ] = array(); |
|
3375 |
} |
|
3376 |
$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) ); |
|
0 | 3377 |
return true; |
3378 |
} |
|
3379 |
||
3380 |
return wp_update_term_count_now( $terms, $taxonomy ); |
|
3381 |
} |
|
3382 |
||
3383 |
/** |
|
3384 |
* Perform term count update immediately. |
|
3385 |
* |
|
3386 |
* @since 2.5.0 |
|
3387 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3388 |
* @param array $terms The term_taxonomy_id of terms to update. |
0 | 3389 |
* @param string $taxonomy The context of the term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3390 |
* @return true Always true when complete. |
0 | 3391 |
*/ |
3392 |
function wp_update_term_count_now( $terms, $taxonomy ) { |
|
9 | 3393 |
$terms = array_map( 'intval', $terms ); |
3394 |
||
3395 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
3396 |
if ( ! empty( $taxonomy->update_count_callback ) ) { |
|
3397 |
call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy ); |
|
0 | 3398 |
} else { |
3399 |
$object_types = (array) $taxonomy->object_type; |
|
3400 |
foreach ( $object_types as &$object_type ) { |
|
9 | 3401 |
if ( 0 === strpos( $object_type, 'attachment:' ) ) { |
0 | 3402 |
list( $object_type ) = explode( ':', $object_type ); |
9 | 3403 |
} |
0 | 3404 |
} |
3405 |
||
16 | 3406 |
if ( array_filter( $object_types, 'post_type_exists' ) == $object_types ) { |
3407 |
// Only post types are attached to this taxonomy. |
|
0 | 3408 |
_update_post_term_count( $terms, $taxonomy ); |
3409 |
} else { |
|
16 | 3410 |
// Default count updater. |
0 | 3411 |
_update_generic_term_count( $terms, $taxonomy ); |
3412 |
} |
|
3413 |
} |
|
3414 |
||
9 | 3415 |
clean_term_cache( $terms, '', false ); |
0 | 3416 |
|
3417 |
return true; |
|
3418 |
} |
|
3419 |
||
3420 |
// |
|
16 | 3421 |
// Cache. |
0 | 3422 |
// |
3423 |
||
3424 |
/** |
|
3425 |
* Removes the taxonomy relationship to terms from the cache. |
|
3426 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3427 |
* Will remove the entire taxonomy relationship containing term `$object_id`. The |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3428 |
* term IDs have to exist within the taxonomy `$object_type` for the deletion to |
0 | 3429 |
* take place. |
3430 |
* |
|
3431 |
* @since 2.3.0 |
|
3432 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3433 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3434 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3435 |
* @see get_object_taxonomies() for more on $object_type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3436 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3437 |
* @param int|array $object_ids Single or list of term object ID(s). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3438 |
* @param array|string $object_type The taxonomy object type. |
0 | 3439 |
*/ |
9 | 3440 |
function clean_object_term_cache( $object_ids, $object_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3441 |
global $_wp_suspend_cache_invalidation; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3442 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3443 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3444 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3445 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3446 |
|
9 | 3447 |
if ( ! is_array( $object_ids ) ) { |
3448 |
$object_ids = array( $object_ids ); |
|
3449 |
} |
|
0 | 3450 |
|
3451 |
$taxonomies = get_object_taxonomies( $object_type ); |
|
3452 |
||
5 | 3453 |
foreach ( $object_ids as $id ) { |
3454 |
foreach ( $taxonomies as $taxonomy ) { |
|
9 | 3455 |
wp_cache_delete( $id, "{$taxonomy}_relationships" ); |
5 | 3456 |
} |
3457 |
} |
|
3458 |
||
3459 |
/** |
|
3460 |
* Fires after the object term cache has been cleaned. |
|
3461 |
* |
|
3462 |
* @since 2.5.0 |
|
3463 |
* |
|
3464 |
* @param array $object_ids An array of object IDs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3465 |
* @param string $object_type Object type. |
5 | 3466 |
*/ |
3467 |
do_action( 'clean_object_term_cache', $object_ids, $object_type ); |
|
0 | 3468 |
} |
3469 |
||
3470 |
/** |
|
16 | 3471 |
* Will remove all of the term IDs from the cache. |
0 | 3472 |
* |
3473 |
* @since 2.3.0 |
|
5 | 3474 |
* |
16 | 3475 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3476 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3477 |
* |
16 | 3478 |
* @param int|int[] $ids Single or array of term IDs. |
3479 |
* @param string $taxonomy Optional. Taxonomy slug. Can be empty, in which case the taxonomies of the passed |
|
3480 |
* term IDs will be used. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3481 |
* @param bool $clean_taxonomy Optional. Whether to clean taxonomy wide caches (true), or just individual |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3482 |
* term object caches (false). Default true. |
0 | 3483 |
*/ |
9 | 3484 |
function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3485 |
global $wpdb, $_wp_suspend_cache_invalidation; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3486 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3487 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3488 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3489 |
} |
0 | 3490 |
|
9 | 3491 |
if ( ! is_array( $ids ) ) { |
3492 |
$ids = array( $ids ); |
|
3493 |
} |
|
0 | 3494 |
|
3495 |
$taxonomies = array(); |
|
3496 |
// If no taxonomy, assume tt_ids. |
|
9 | 3497 |
if ( empty( $taxonomy ) ) { |
3498 |
$tt_ids = array_map( 'intval', $ids ); |
|
3499 |
$tt_ids = implode( ', ', $tt_ids ); |
|
3500 |
$terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" ); |
|
3501 |
$ids = array(); |
|
16 | 3502 |
|
0 | 3503 |
foreach ( (array) $terms as $term ) { |
3504 |
$taxonomies[] = $term->taxonomy; |
|
9 | 3505 |
$ids[] = $term->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3506 |
wp_cache_delete( $term->term_id, 'terms' ); |
0 | 3507 |
} |
16 | 3508 |
|
9 | 3509 |
$taxonomies = array_unique( $taxonomies ); |
0 | 3510 |
} else { |
9 | 3511 |
$taxonomies = array( $taxonomy ); |
16 | 3512 |
|
0 | 3513 |
foreach ( $taxonomies as $taxonomy ) { |
3514 |
foreach ( $ids as $id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3515 |
wp_cache_delete( $id, 'terms' ); |
0 | 3516 |
} |
3517 |
} |
|
3518 |
} |
|
3519 |
||
3520 |
foreach ( $taxonomies as $taxonomy ) { |
|
3521 |
if ( $clean_taxonomy ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3522 |
clean_taxonomy_cache( $taxonomy ); |
0 | 3523 |
} |
3524 |
||
5 | 3525 |
/** |
3526 |
* Fires once after each taxonomy's term cache has been cleaned. |
|
3527 |
* |
|
3528 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3529 |
* @since 4.5.0 Added the `$clean_taxonomy` parameter. |
5 | 3530 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3531 |
* @param array $ids An array of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3532 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3533 |
* @param bool $clean_taxonomy Whether or not to clean taxonomy-wide caches |
5 | 3534 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3535 |
do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy ); |
0 | 3536 |
} |
3537 |
||
3538 |
wp_cache_set( 'last_changed', microtime(), 'terms' ); |
|
3539 |
} |
|
3540 |
||
3541 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3542 |
* Clean the caches for a taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3543 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3544 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3545 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3546 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3547 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3548 |
function clean_taxonomy_cache( $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3549 |
wp_cache_delete( 'all_ids', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3550 |
wp_cache_delete( 'get', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3551 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3552 |
// Regenerate cached hierarchy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3553 |
delete_option( "{$taxonomy}_children" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3554 |
_get_term_hierarchy( $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3555 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3556 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3557 |
* Fires after a taxonomy's caches have been cleaned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3558 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3559 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3560 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3561 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3562 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3563 |
do_action( 'clean_taxonomy_cache', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3564 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3565 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3566 |
/** |
16 | 3567 |
* Retrieves the cached term objects for the given object ID. |
0 | 3568 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3569 |
* Upstream functions (like get_the_terms() and is_object_in_term()) are |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3570 |
* responsible for populating the object-term relationship cache. The current |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3571 |
* function only fetches relationship data that is already in the cache. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3572 |
* |
0 | 3573 |
* @since 2.3.0 |
16 | 3574 |
* @since 4.7.0 Returns a `WP_Error` object if there's an error with |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3575 |
* any of the matched terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3576 |
* |
16 | 3577 |
* @param int $id Term object ID, for example a post, comment, or user ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3578 |
* @param string $taxonomy Taxonomy name. |
16 | 3579 |
* @return bool|WP_Term[]|WP_Error Array of `WP_Term` objects, if cached. |
3580 |
* False if cache is empty for `$taxonomy` and `$id`. |
|
3581 |
* WP_Error if get_term() returns an error object for any term. |
|
0 | 3582 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3583 |
function get_object_term_cache( $id, $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3584 |
$_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3585 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3586 |
// We leave the priming of relationship caches to upstream functions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3587 |
if ( false === $_term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3588 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3589 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3590 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3591 |
// Backward compatibility for if a plugin is putting objects into the cache, rather than IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3592 |
$term_ids = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3593 |
foreach ( $_term_ids as $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3594 |
if ( is_numeric( $term_id ) ) { |
18 | 3595 |
$term_ids[] = (int) $term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3596 |
} elseif ( isset( $term_id->term_id ) ) { |
18 | 3597 |
$term_ids[] = (int) $term_id->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3598 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3599 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3600 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3601 |
// Fill the term objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3602 |
_prime_term_caches( $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3603 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3604 |
$terms = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3605 |
foreach ( $term_ids as $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3606 |
$term = get_term( $term_id, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3607 |
if ( is_wp_error( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3608 |
return $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3609 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3610 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3611 |
$terms[] = $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3612 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3613 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3614 |
return $terms; |
0 | 3615 |
} |
3616 |
||
3617 |
/** |
|
5 | 3618 |
* Updates the cache for the given term object ID(s). |
3619 |
* |
|
3620 |
* Note: Due to performance concerns, great care should be taken to only update |
|
3621 |
* term caches when necessary. Processing time can increase exponentially depending |
|
3622 |
* on both the number of passed term IDs and the number of taxonomies those terms |
|
3623 |
* belong to. |
|
3624 |
* |
|
3625 |
* Caches will only be updated for terms not already cached. |
|
3626 |
* |
|
0 | 3627 |
* @since 2.3.0 |
5 | 3628 |
* |
16 | 3629 |
* @param string|int[] $object_ids Comma-separated list or array of term object IDs. |
3630 |
* @param string|string[] $object_type The taxonomy object type or array of the same. |
|
18 | 3631 |
* @return void|false Void on success or if the `$object_ids` parameter is empty, |
3632 |
* false if all of the terms in `$object_ids` are already cached. |
|
0 | 3633 |
*/ |
9 | 3634 |
function update_object_term_cache( $object_ids, $object_type ) { |
3635 |
if ( empty( $object_ids ) ) { |
|
0 | 3636 |
return; |
9 | 3637 |
} |
3638 |
||
3639 |
if ( ! is_array( $object_ids ) ) { |
|
3640 |
$object_ids = explode( ',', $object_ids ); |
|
3641 |
} |
|
3642 |
||
16 | 3643 |
$object_ids = array_map( 'intval', $object_ids ); |
3644 |
$non_cached_ids = array(); |
|
9 | 3645 |
|
3646 |
$taxonomies = get_object_taxonomies( $object_type ); |
|
0 | 3647 |
|
16 | 3648 |
foreach ( $taxonomies as $taxonomy ) { |
3649 |
$cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" ); |
|
3650 |
||
3651 |
foreach ( $cache_values as $id => $value ) { |
|
3652 |
if ( false === $value ) { |
|
3653 |
$non_cached_ids[] = $id; |
|
0 | 3654 |
} |
3655 |
} |
|
3656 |
} |
|
3657 |
||
16 | 3658 |
if ( empty( $non_cached_ids ) ) { |
0 | 3659 |
return false; |
9 | 3660 |
} |
3661 |
||
16 | 3662 |
$non_cached_ids = array_unique( $non_cached_ids ); |
3663 |
||
9 | 3664 |
$terms = wp_get_object_terms( |
16 | 3665 |
$non_cached_ids, |
9 | 3666 |
$taxonomies, |
3667 |
array( |
|
3668 |
'fields' => 'all_with_object_id', |
|
3669 |
'orderby' => 'name', |
|
3670 |
'update_term_meta_cache' => false, |
|
3671 |
) |
|
3672 |
); |
|
0 | 3673 |
|
3674 |
$object_terms = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3675 |
foreach ( (array) $terms as $term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3676 |
$object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3677 |
} |
0 | 3678 |
|
16 | 3679 |
foreach ( $non_cached_ids as $id ) { |
0 | 3680 |
foreach ( $taxonomies as $taxonomy ) { |
9 | 3681 |
if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) { |
3682 |
if ( ! isset( $object_terms[ $id ] ) ) { |
|
3683 |
$object_terms[ $id ] = array(); |
|
3684 |
} |
|
3685 |
$object_terms[ $id ][ $taxonomy ] = array(); |
|
0 | 3686 |
} |
3687 |
} |
|
3688 |
} |
|
3689 |
||
3690 |
foreach ( $object_terms as $id => $value ) { |
|
3691 |
foreach ( $value as $taxonomy => $terms ) { |
|
3692 |
wp_cache_add( $id, $terms, "{$taxonomy}_relationships" ); |
|
3693 |
} |
|
3694 |
} |
|
3695 |
} |
|
3696 |
||
3697 |
/** |
|
3698 |
* Updates Terms to Taxonomy in cache. |
|
3699 |
* |
|
3700 |
* @since 2.3.0 |
|
3701 |
* |
|
16 | 3702 |
* @param WP_Term[] $terms Array of term objects to change. |
3703 |
* @param string $taxonomy Not used. |
|
0 | 3704 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3705 |
function update_term_cache( $terms, $taxonomy = '' ) { |
0 | 3706 |
foreach ( (array) $terms as $term ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3707 |
// Create a copy in case the array was passed by reference. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3708 |
$_term = clone $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3709 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3710 |
// Object ID should not be cached. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3711 |
unset( $_term->object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3712 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3713 |
wp_cache_add( $term->term_id, $_term, 'terms' ); |
0 | 3714 |
} |
3715 |
} |
|
3716 |
||
3717 |
// |
|
16 | 3718 |
// Private. |
0 | 3719 |
// |
3720 |
||
3721 |
/** |
|
3722 |
* Retrieves children of taxonomy as Term IDs. |
|
3723 |
* |
|
9 | 3724 |
* @access private |
0 | 3725 |
* @since 2.3.0 |
3726 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3727 |
* @param string $taxonomy Taxonomy name. |
0 | 3728 |
* @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs. |
3729 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3730 |
function _get_term_hierarchy( $taxonomy ) { |
9 | 3731 |
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
0 | 3732 |
return array(); |
9 | 3733 |
} |
3734 |
$children = get_option( "{$taxonomy}_children" ); |
|
3735 |
||
3736 |
if ( is_array( $children ) ) { |
|
0 | 3737 |
return $children; |
9 | 3738 |
} |
0 | 3739 |
$children = array(); |
9 | 3740 |
$terms = get_terms( |
3741 |
array( |
|
16 | 3742 |
'taxonomy' => $taxonomy, |
9 | 3743 |
'get' => 'all', |
3744 |
'orderby' => 'id', |
|
3745 |
'fields' => 'id=>parent', |
|
3746 |
'update_term_meta_cache' => false, |
|
3747 |
) |
|
3748 |
); |
|
0 | 3749 |
foreach ( $terms as $term_id => $parent ) { |
9 | 3750 |
if ( $parent > 0 ) { |
3751 |
$children[ $parent ][] = $term_id; |
|
3752 |
} |
|
0 | 3753 |
} |
9 | 3754 |
update_option( "{$taxonomy}_children", $children ); |
0 | 3755 |
|
3756 |
return $children; |
|
3757 |
} |
|
3758 |
||
3759 |
/** |
|
3760 |
* Get the subset of $terms that are descendants of $term_id. |
|
3761 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3762 |
* If `$terms` is an array of objects, then _get_term_children() returns an array of objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3763 |
* If `$terms` is an array of IDs, then _get_term_children() returns an array of IDs. |
0 | 3764 |
* |
3765 |
* @access private |
|
3766 |
* @since 2.3.0 |
|
3767 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3768 |
* @param int $term_id The ancestor term: all returned terms should be descendants of `$term_id`. |
5 | 3769 |
* @param array $terms The set of terms - either an array of term objects or term IDs - from which those that |
3770 |
* are descendants of $term_id will be chosen. |
|
3771 |
* @param string $taxonomy The taxonomy which determines the hierarchy of the terms. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3772 |
* @param array $ancestors Optional. Term ancestors that have already been identified. Passed by reference, to keep |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3773 |
* track of found terms when recursing the hierarchy. The array of located ancestors is used |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3774 |
* to prevent infinite recursion loops. For performance, `term_ids` are used as array keys, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3775 |
* with 1 as value. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3776 |
* @return array|WP_Error The subset of $terms that are descendants of $term_id. |
0 | 3777 |
*/ |
5 | 3778 |
function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { |
0 | 3779 |
$empty_array = array(); |
9 | 3780 |
if ( empty( $terms ) ) { |
0 | 3781 |
return $empty_array; |
9 | 3782 |
} |
3783 |
||
16 | 3784 |
$term_id = (int) $term_id; |
9 | 3785 |
$term_list = array(); |
3786 |
$has_children = _get_term_hierarchy( $taxonomy ); |
|
3787 |
||
16 | 3788 |
if ( $term_id && ! isset( $has_children[ $term_id ] ) ) { |
0 | 3789 |
return $empty_array; |
9 | 3790 |
} |
0 | 3791 |
|
5 | 3792 |
// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. |
3793 |
if ( empty( $ancestors ) ) { |
|
3794 |
$ancestors[ $term_id ] = 1; |
|
3795 |
} |
|
3796 |
||
0 | 3797 |
foreach ( (array) $terms as $term ) { |
3798 |
$use_id = false; |
|
9 | 3799 |
if ( ! is_object( $term ) ) { |
3800 |
$term = get_term( $term, $taxonomy ); |
|
3801 |
if ( is_wp_error( $term ) ) { |
|
0 | 3802 |
return $term; |
9 | 3803 |
} |
0 | 3804 |
$use_id = true; |
3805 |
} |
|
3806 |
||
5 | 3807 |
// Don't recurse if we've already identified the term as a child - this indicates a loop. |
3808 |
if ( isset( $ancestors[ $term->term_id ] ) ) { |
|
0 | 3809 |
continue; |
5 | 3810 |
} |
0 | 3811 |
|
16 | 3812 |
if ( (int) $term->parent === $term_id ) { |
9 | 3813 |
if ( $use_id ) { |
0 | 3814 |
$term_list[] = $term->term_id; |
9 | 3815 |
} else { |
0 | 3816 |
$term_list[] = $term; |
9 | 3817 |
} |
3818 |
||
3819 |
if ( ! isset( $has_children[ $term->term_id ] ) ) { |
|
0 | 3820 |
continue; |
9 | 3821 |
} |
0 | 3822 |
|
5 | 3823 |
$ancestors[ $term->term_id ] = 1; |
3824 |
||
16 | 3825 |
$children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ); |
3826 |
if ( $children ) { |
|
9 | 3827 |
$term_list = array_merge( $term_list, $children ); |
3828 |
} |
|
0 | 3829 |
} |
3830 |
} |
|
3831 |
||
3832 |
return $term_list; |
|
3833 |
} |
|
3834 |
||
3835 |
/** |
|
3836 |
* Add count of children to parent count. |
|
3837 |
* |
|
3838 |
* Recalculates term counts by including items from child terms. Assumes all |
|
3839 |
* relevant children are already in the $terms argument. |
|
3840 |
* |
|
3841 |
* @access private |
|
3842 |
* @since 2.3.0 |
|
5 | 3843 |
* |
3844 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3845 |
* |
18 | 3846 |
* @param object[]|WP_Term[] $terms List of term objects (passed by reference). |
3847 |
* @param string $taxonomy Term context. |
|
0 | 3848 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3849 |
function _pad_term_counts( &$terms, $taxonomy ) { |
0 | 3850 |
global $wpdb; |
3851 |
||
3852 |
// This function only works for hierarchical taxonomies like post categories. |
|
9 | 3853 |
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
0 | 3854 |
return; |
9 | 3855 |
} |
3856 |
||
3857 |
$term_hier = _get_term_hierarchy( $taxonomy ); |
|
3858 |
||
3859 |
if ( empty( $term_hier ) ) { |
|
0 | 3860 |
return; |
9 | 3861 |
} |
3862 |
||
3863 |
$term_items = array(); |
|
5 | 3864 |
$terms_by_id = array(); |
9 | 3865 |
$term_ids = array(); |
0 | 3866 |
|
3867 |
foreach ( (array) $terms as $key => $term ) { |
|
9 | 3868 |
$terms_by_id[ $term->term_id ] = & $terms[ $key ]; |
3869 |
$term_ids[ $term->term_taxonomy_id ] = $term->term_id; |
|
0 | 3870 |
} |
3871 |
||
16 | 3872 |
// Get the object and term IDs and stick them in a lookup table. |
9 | 3873 |
$tax_obj = get_taxonomy( $taxonomy ); |
3874 |
$object_types = esc_sql( $tax_obj->object_type ); |
|
3875 |
$results = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" ); |
|
16 | 3876 |
|
0 | 3877 |
foreach ( $results as $row ) { |
16 | 3878 |
$id = $term_ids[ $row->term_taxonomy_id ]; |
3879 |
||
9 | 3880 |
$term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1; |
0 | 3881 |
} |
3882 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3883 |
// Touch every ancestor's lookup row for each post in each term. |
0 | 3884 |
foreach ( $term_ids as $term_id ) { |
9 | 3885 |
$child = $term_id; |
5 | 3886 |
$ancestors = array(); |
9 | 3887 |
while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) { |
5 | 3888 |
$ancestors[] = $child; |
16 | 3889 |
|
9 | 3890 |
if ( ! empty( $term_items[ $term_id ] ) ) { |
3891 |
foreach ( $term_items[ $term_id ] as $item_id => $touches ) { |
|
3892 |
$term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1; |
|
0 | 3893 |
} |
9 | 3894 |
} |
16 | 3895 |
|
0 | 3896 |
$child = $parent; |
5 | 3897 |
|
16 | 3898 |
if ( in_array( $parent, $ancestors, true ) ) { |
5 | 3899 |
break; |
3900 |
} |
|
0 | 3901 |
} |
3902 |
} |
|
3903 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3904 |
// Transfer the touched cells. |
9 | 3905 |
foreach ( (array) $term_items as $id => $items ) { |
3906 |
if ( isset( $terms_by_id[ $id ] ) ) { |
|
3907 |
$terms_by_id[ $id ]->count = count( $items ); |
|
3908 |
} |
|
3909 |
} |
|
0 | 3910 |
} |
3911 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3912 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3913 |
* Adds any terms from the given IDs to the cache that do not already exist in cache. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3914 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3915 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3916 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3917 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3918 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3919 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3920 |
* @param array $term_ids Array of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3921 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3922 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3923 |
function _prime_term_caches( $term_ids, $update_meta_cache = true ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3924 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3925 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3926 |
$non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3927 |
if ( ! empty( $non_cached_ids ) ) { |
18 | 3928 |
$fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3929 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3930 |
update_term_cache( $fresh_terms, $update_meta_cache ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3931 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3932 |
if ( $update_meta_cache ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3933 |
update_termmeta_cache( $non_cached_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3934 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3935 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3936 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3937 |
|
0 | 3938 |
// |
16 | 3939 |
// Default callbacks. |
0 | 3940 |
// |
3941 |
||
3942 |
/** |
|
3943 |
* Will update term count based on object types of the current taxonomy. |
|
3944 |
* |
|
3945 |
* Private function for the default callback for post_tag and category |
|
3946 |
* taxonomies. |
|
3947 |
* |
|
3948 |
* @access private |
|
3949 |
* @since 2.3.0 |
|
5 | 3950 |
* |
3951 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3952 |
* |
16 | 3953 |
* @param int[] $terms List of Term taxonomy IDs. |
3954 |
* @param WP_Taxonomy $taxonomy Current taxonomy object of terms. |
|
0 | 3955 |
*/ |
3956 |
function _update_post_term_count( $terms, $taxonomy ) { |
|
3957 |
global $wpdb; |
|
3958 |
||
3959 |
$object_types = (array) $taxonomy->object_type; |
|
3960 |
||
9 | 3961 |
foreach ( $object_types as &$object_type ) { |
0 | 3962 |
list( $object_type ) = explode( ':', $object_type ); |
9 | 3963 |
} |
0 | 3964 |
|
3965 |
$object_types = array_unique( $object_types ); |
|
3966 |
||
16 | 3967 |
$check_attachments = array_search( 'attachment', $object_types, true ); |
3968 |
if ( false !== $check_attachments ) { |
|
0 | 3969 |
unset( $object_types[ $check_attachments ] ); |
3970 |
$check_attachments = true; |
|
3971 |
} |
|
3972 |
||
9 | 3973 |
if ( $object_types ) { |
0 | 3974 |
$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); |
9 | 3975 |
} |
0 | 3976 |
|
18 | 3977 |
$post_statuses = array( 'publish' ); |
3978 |
||
3979 |
/** |
|
3980 |
* Filters the post statuses for updating the term count. |
|
3981 |
* |
|
3982 |
* @since 5.7.0 |
|
3983 |
* |
|
3984 |
* @param string[] $post_statuses List of post statuses to include in the count. Default is 'publish'. |
|
3985 |
* @param WP_Taxonomy $taxonomy Current taxonomy object. |
|
3986 |
*/ |
|
3987 |
$post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) ); |
|
3988 |
||
0 | 3989 |
foreach ( (array) $terms as $term ) { |
3990 |
$count = 0; |
|
3991 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3992 |
// Attachments can be 'inherit' status, we need to base count off the parent's status if so. |
9 | 3993 |
if ( $check_attachments ) { |
18 | 3994 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration |
3995 |
$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); |
|
9 | 3996 |
} |
3997 |
||
3998 |
if ( $object_types ) { |
|
16 | 3999 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration |
18 | 4000 |
$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); |
9 | 4001 |
} |
0 | 4002 |
|
5 | 4003 |
/** This action is documented in wp-includes/taxonomy.php */ |
4004 |
do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4005 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
5 | 4006 |
|
4007 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
4008 |
do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4009 |
} |
4010 |
} |
|
4011 |
||
4012 |
/** |
|
4013 |
* Will update term count based on number of objects. |
|
4014 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4015 |
* Default callback for the 'link_category' taxonomy. |
0 | 4016 |
* |
4017 |
* @since 3.3.0 |
|
5 | 4018 |
* |
4019 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4020 |
* |
16 | 4021 |
* @param int[] $terms List of term taxonomy IDs. |
4022 |
* @param WP_Taxonomy $taxonomy Current taxonomy object of terms. |
|
0 | 4023 |
*/ |
4024 |
function _update_generic_term_count( $terms, $taxonomy ) { |
|
4025 |
global $wpdb; |
|
4026 |
||
4027 |
foreach ( (array) $terms as $term ) { |
|
4028 |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|
4029 |
||
5 | 4030 |
/** This action is documented in wp-includes/taxonomy.php */ |
4031 |
do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4032 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
5 | 4033 |
|
4034 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
4035 |
do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4036 |
} |
4037 |
} |
|
4038 |
||
4039 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4040 |
* Create a new term for a term_taxonomy item that currently shares its term |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4041 |
* with another term_taxonomy. |
5 | 4042 |
* |
4043 |
* @ignore |
|
4044 |
* @since 4.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4045 |
* @since 4.3.0 Introduced `$record` parameter. Also, `$term_id` and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4046 |
* `$term_taxonomy_id` can now accept objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4047 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4048 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4049 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4050 |
* @param int|object $term_id ID of the shared term, or the shared term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4051 |
* @param int|object $term_taxonomy_id ID of the term_taxonomy item to receive a new term, or the term_taxonomy object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4052 |
* (corresponding to a row from the term_taxonomy table). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4053 |
* @param bool $record Whether to record data about the split term in the options table. The recording |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4054 |
* process has the potential to be resource-intensive, so during batch operations |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4055 |
* it can be beneficial to skip inline recording and do it just once, after the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4056 |
* batch is processed. Only set this to `false` if you know what you are doing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4057 |
* Default: true. |
5 | 4058 |
* @return int|WP_Error When the current term does not need to be split (or cannot be split on the current |
4059 |
* database schema), `$term_id` is returned. When the term is successfully split, the |
|
4060 |
* new term_id is returned. A WP_Error is returned for miscellaneous errors. |
|
4061 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4062 |
function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { |
5 | 4063 |
global $wpdb; |
4064 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4065 |
if ( is_object( $term_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4066 |
$shared_term = $term_id; |
18 | 4067 |
$term_id = (int) $shared_term->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4068 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4069 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4070 |
if ( is_object( $term_taxonomy_id ) ) { |
9 | 4071 |
$term_taxonomy = $term_taxonomy_id; |
18 | 4072 |
$term_taxonomy_id = (int) $term_taxonomy->term_taxonomy_id; |
5 | 4073 |
} |
4074 |
||
4075 |
// If there are no shared term_taxonomy rows, there's nothing to do here. |
|
16 | 4076 |
$shared_tt_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4077 |
|
5 | 4078 |
if ( ! $shared_tt_count ) { |
4079 |
return $term_id; |
|
4080 |
} |
|
4081 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4082 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4083 |
* Verify that the term_taxonomy_id passed to the function is actually associated with the term_id. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4084 |
* If there's a mismatch, it may mean that the term is already split. Return the actual term_id from the db. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4085 |
*/ |
16 | 4086 |
$check_term_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); |
4087 |
if ( $check_term_id !== $term_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4088 |
return $check_term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4089 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4090 |
|
5 | 4091 |
// Pull up data about the currently shared slug, which we'll use to populate the new one. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4092 |
if ( empty( $shared_term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4093 |
$shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4094 |
} |
5 | 4095 |
|
4096 |
$new_term_data = array( |
|
9 | 4097 |
'name' => $shared_term->name, |
4098 |
'slug' => $shared_term->slug, |
|
5 | 4099 |
'term_group' => $shared_term->term_group, |
4100 |
); |
|
4101 |
||
4102 |
if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) { |
|
4103 |
return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error ); |
|
4104 |
} |
|
4105 |
||
4106 |
$new_term_id = (int) $wpdb->insert_id; |
|
4107 |
||
4108 |
// Update the existing term_taxonomy to point to the newly created term. |
|
9 | 4109 |
$wpdb->update( |
4110 |
$wpdb->term_taxonomy, |
|
5 | 4111 |
array( 'term_id' => $new_term_id ), |
4112 |
array( 'term_taxonomy_id' => $term_taxonomy_id ) |
|
4113 |
); |
|
4114 |
||
4115 |
// Reassign child terms to the new parent. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4116 |
if ( empty( $term_taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4117 |
$term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4118 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4119 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4120 |
$children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = %s", $term_id, $term_taxonomy->taxonomy ) ); |
5 | 4121 |
if ( ! empty( $children_tt_ids ) ) { |
4122 |
foreach ( $children_tt_ids as $child_tt_id ) { |
|
9 | 4123 |
$wpdb->update( |
4124 |
$wpdb->term_taxonomy, |
|
5 | 4125 |
array( 'parent' => $new_term_id ), |
4126 |
array( 'term_taxonomy_id' => $child_tt_id ) |
|
4127 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4128 |
clean_term_cache( (int) $child_tt_id, '', false ); |
5 | 4129 |
} |
4130 |
} else { |
|
4131 |
// If the term has no children, we must force its taxonomy cache to be rebuilt separately. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4132 |
clean_term_cache( $new_term_id, $term_taxonomy->taxonomy, false ); |
5 | 4133 |
} |
4134 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4135 |
clean_term_cache( $term_id, $term_taxonomy->taxonomy, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4136 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4137 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4138 |
* Taxonomy cache clearing is delayed to avoid race conditions that may occur when |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4139 |
* regenerating the taxonomy's hierarchy tree. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4140 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4141 |
$taxonomies_to_clean = array( $term_taxonomy->taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4142 |
|
5 | 4143 |
// Clean the cache for term taxonomies formerly shared with the current term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4144 |
$shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); |
9 | 4145 |
$taxonomies_to_clean = array_merge( $taxonomies_to_clean, $shared_term_taxonomies ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4146 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4147 |
foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4148 |
clean_taxonomy_cache( $taxonomy_to_clean ); |
5 | 4149 |
} |
4150 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4151 |
// Keep a record of term_ids that have been split, keyed by old term_id. See wp_get_split_term(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4152 |
if ( $record ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4153 |
$split_term_data = get_option( '_split_terms', array() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4154 |
if ( ! isset( $split_term_data[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4155 |
$split_term_data[ $term_id ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4156 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4157 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4158 |
$split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4159 |
update_option( '_split_terms', $split_term_data ); |
5 | 4160 |
} |
4161 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4162 |
// If we've just split the final shared term, set the "finished" flag. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4163 |
$shared_terms_exist = $wpdb->get_results( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4164 |
"SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4165 |
LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4166 |
GROUP BY t.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4167 |
HAVING term_tt_count > 1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4168 |
LIMIT 1" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4169 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4170 |
if ( ! $shared_terms_exist ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4171 |
update_option( 'finished_splitting_shared_terms', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4172 |
} |
5 | 4173 |
|
4174 |
/** |
|
4175 |
* Fires after a previously shared taxonomy term is split into two separate terms. |
|
4176 |
* |
|
4177 |
* @since 4.2.0 |
|
4178 |
* |
|
4179 |
* @param int $term_id ID of the formerly shared term. |
|
4180 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4181 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4182 |
* @param string $taxonomy Taxonomy for the split term. |
|
4183 |
*/ |
|
4184 |
do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy ); |
|
4185 |
||
4186 |
return $new_term_id; |
|
4187 |
} |
|
4188 |
||
4189 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4190 |
* Splits a batch of shared taxonomy terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4191 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4192 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4193 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4194 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4195 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4196 |
function _wp_batch_split_terms() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4197 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4199 |
$lock_name = 'term_split.lock'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4200 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4201 |
// Try to lock. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4202 |
$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4203 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4204 |
if ( ! $lock_result ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4205 |
$lock_result = get_option( $lock_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4206 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4207 |
// Bail if we were unable to create a lock, or if the existing lock is still valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4208 |
if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4209 |
wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4210 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4211 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4212 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4213 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4214 |
// Update the lock, as by this point we've definitely got a lock, just need to fire the actions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4215 |
update_option( $lock_name, time() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4216 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4217 |
// Get a list of shared terms (those with more than one associated row in term_taxonomy). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4218 |
$shared_terms = $wpdb->get_results( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4219 |
"SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4220 |
LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4221 |
GROUP BY t.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4222 |
HAVING term_tt_count > 1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4223 |
LIMIT 10" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4224 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4225 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4226 |
// No more terms, we're done here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4227 |
if ( ! $shared_terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4228 |
update_option( 'finished_splitting_shared_terms', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4229 |
delete_option( $lock_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4230 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4231 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4232 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4233 |
// Shared terms found? We'll need to run this script again. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4234 |
wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4235 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4236 |
// Rekey shared term array for faster lookups. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4237 |
$_shared_terms = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4238 |
foreach ( $shared_terms as $shared_term ) { |
18 | 4239 |
$term_id = (int) $shared_term->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4240 |
$_shared_terms[ $term_id ] = $shared_term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4241 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4242 |
$shared_terms = $_shared_terms; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4243 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4244 |
// Get term taxonomy data for all shared terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4245 |
$shared_term_ids = implode( ',', array_keys( $shared_terms ) ); |
9 | 4246 |
$shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4247 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4248 |
// Split term data recording is slow, so we do it just once, outside the loop. |
9 | 4249 |
$split_term_data = get_option( '_split_terms', array() ); |
16 | 4250 |
$skipped_first_term = array(); |
4251 |
$taxonomies = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4252 |
foreach ( $shared_tts as $shared_tt ) { |
18 | 4253 |
$term_id = (int) $shared_tt->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4254 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4255 |
// Don't split the first tt belonging to a given term_id. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4256 |
if ( ! isset( $skipped_first_term[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4257 |
$skipped_first_term[ $term_id ] = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4258 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4259 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4260 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4261 |
if ( ! isset( $split_term_data[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4262 |
$split_term_data[ $term_id ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4263 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4264 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4265 |
// Keep track of taxonomies whose hierarchies need flushing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4266 |
if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4267 |
$taxonomies[ $shared_tt->taxonomy ] = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4268 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4269 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4270 |
// Split the term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4271 |
$split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4272 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4273 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4274 |
// Rebuild the cached hierarchy for each affected taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4275 |
foreach ( array_keys( $taxonomies ) as $tax ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4276 |
delete_option( "{$tax}_children" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4277 |
_get_term_hierarchy( $tax ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4278 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4279 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4280 |
update_option( '_split_terms', $split_term_data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4281 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4282 |
delete_option( $lock_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4283 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4284 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4285 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4286 |
* In order to avoid the _wp_batch_split_terms() job being accidentally removed, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4287 |
* check that it's still scheduled while we haven't finished splitting terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4288 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4289 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4290 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4291 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4292 |
function _wp_check_for_scheduled_split_terms() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4293 |
if ( ! get_option( 'finished_splitting_shared_terms' ) && ! wp_next_scheduled( 'wp_split_shared_term_batch' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4294 |
wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_split_shared_term_batch' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4295 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4296 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4297 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4298 |
/** |
5 | 4299 |
* Check default categories when a term gets split to see if any of them need to be updated. |
4300 |
* |
|
4301 |
* @ignore |
|
4302 |
* @since 4.2.0 |
|
4303 |
* |
|
4304 |
* @param int $term_id ID of the formerly shared term. |
|
4305 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4306 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4307 |
* @param string $taxonomy Taxonomy for the split term. |
|
4308 |
*/ |
|
4309 |
function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
|
16 | 4310 |
if ( 'category' !== $taxonomy ) { |
5 | 4311 |
return; |
4312 |
} |
|
4313 |
||
4314 |
foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) { |
|
16 | 4315 |
if ( (int) get_option( $option, -1 ) === $term_id ) { |
5 | 4316 |
update_option( $option, $new_term_id ); |
4317 |
} |
|
4318 |
} |
|
4319 |
} |
|
4320 |
||
4321 |
/** |
|
4322 |
* Check menu items when a term gets split to see if any of them need to be updated. |
|
4323 |
* |
|
4324 |
* @ignore |
|
4325 |
* @since 4.2.0 |
|
4326 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4327 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4328 |
* |
5 | 4329 |
* @param int $term_id ID of the formerly shared term. |
4330 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4331 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4332 |
* @param string $taxonomy Taxonomy for the split term. |
|
4333 |
*/ |
|
4334 |
function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
|
4335 |
global $wpdb; |
|
9 | 4336 |
$post_ids = $wpdb->get_col( |
4337 |
$wpdb->prepare( |
|
4338 |
"SELECT m1.post_id |
|
5 | 4339 |
FROM {$wpdb->postmeta} AS m1 |
4340 |
INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id ) |
|
4341 |
INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id ) |
|
4342 |
WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4343 |
AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s ) |
5 | 4344 |
AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )", |
9 | 4345 |
$taxonomy, |
4346 |
$term_id |
|
4347 |
) |
|
4348 |
); |
|
5 | 4349 |
|
4350 |
if ( $post_ids ) { |
|
4351 |
foreach ( $post_ids as $post_id ) { |
|
4352 |
update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id ); |
|
4353 |
} |
|
4354 |
} |
|
4355 |
} |
|
4356 |
||
4357 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4358 |
* If the term being split is a nav_menu, change associations. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4359 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4360 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4361 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4362 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4363 |
* @param int $term_id ID of the formerly shared term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4364 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4365 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4366 |
* @param string $taxonomy Taxonomy for the split term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4367 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4368 |
function _wp_check_split_nav_menu_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4369 |
if ( 'nav_menu' !== $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4370 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4371 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4372 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4373 |
// Update menu locations. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4374 |
$locations = get_nav_menu_locations(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4375 |
foreach ( $locations as $location => $menu_id ) { |
16 | 4376 |
if ( $term_id === $menu_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4377 |
$locations[ $location ] = $new_term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4378 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4379 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4380 |
set_theme_mod( 'nav_menu_locations', $locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4381 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4382 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4383 |
/** |
5 | 4384 |
* Get data about terms that previously shared a single term_id, but have since been split. |
4385 |
* |
|
4386 |
* @since 4.2.0 |
|
4387 |
* |
|
4388 |
* @param int $old_term_id Term ID. This is the old, pre-split term ID. |
|
4389 |
* @return array Array of new term IDs, keyed by taxonomy. |
|
4390 |
*/ |
|
4391 |
function wp_get_split_terms( $old_term_id ) { |
|
4392 |
$split_terms = get_option( '_split_terms', array() ); |
|
4393 |
||
4394 |
$terms = array(); |
|
4395 |
if ( isset( $split_terms[ $old_term_id ] ) ) { |
|
4396 |
$terms = $split_terms[ $old_term_id ]; |
|
4397 |
} |
|
4398 |
||
4399 |
return $terms; |
|
4400 |
} |
|
4401 |
||
4402 |
/** |
|
4403 |
* Get the new term ID corresponding to a previously split term. |
|
4404 |
* |
|
4405 |
* @since 4.2.0 |
|
4406 |
* |
|
4407 |
* @param int $old_term_id Term ID. This is the old, pre-split term ID. |
|
4408 |
* @param string $taxonomy Taxonomy that the term belongs to. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4409 |
* @return int|false If a previously split term is found corresponding to the old term_id and taxonomy, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4410 |
* the new term_id will be returned. If no previously split term is found matching |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4411 |
* the parameters, returns false. |
5 | 4412 |
*/ |
4413 |
function wp_get_split_term( $old_term_id, $taxonomy ) { |
|
4414 |
$split_terms = wp_get_split_terms( $old_term_id ); |
|
4415 |
||
4416 |
$term_id = false; |
|
4417 |
if ( isset( $split_terms[ $taxonomy ] ) ) { |
|
4418 |
$term_id = (int) $split_terms[ $taxonomy ]; |
|
4419 |
} |
|
4420 |
||
4421 |
return $term_id; |
|
4422 |
} |
|
4423 |
||
4424 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4425 |
* Determine whether a term is shared between multiple taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4426 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4427 |
* Shared taxonomy terms began to be split in 4.3, but failed cron tasks or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4428 |
* other delays in upgrade routines may cause shared terms to remain. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4429 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4430 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4431 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4432 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4433 |
* @return bool Returns false if a term is not shared between multiple taxonomies or |
9 | 4434 |
* if splitting shared taxonomy terms is finished. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4435 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4436 |
function wp_term_is_shared( $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4437 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4438 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4439 |
if ( get_option( 'finished_splitting_shared_terms' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4440 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4441 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4442 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4443 |
$tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4444 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4445 |
return $tt_count > 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4446 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4447 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4448 |
/** |
5 | 4449 |
* Generate a permalink for a taxonomy term archive. |
0 | 4450 |
* |
4451 |
* @since 2.5.0 |
|
4452 |
* |
|
16 | 4453 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
4454 |
* |
|
4455 |
* @param WP_Term|int|string $term The term object, ID, or slug whose link will be retrieved. |
|
4456 |
* @param string $taxonomy Optional. Taxonomy. Default empty. |
|
4457 |
* @return string|WP_Error URL of the taxonomy term archive on success, WP_Error if term does not exist. |
|
0 | 4458 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4459 |
function get_term_link( $term, $taxonomy = '' ) { |
0 | 4460 |
global $wp_rewrite; |
4461 |
||
9 | 4462 |
if ( ! is_object( $term ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4463 |
if ( is_int( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4464 |
$term = get_term( $term, $taxonomy ); |
0 | 4465 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4466 |
$term = get_term_by( 'slug', $term, $taxonomy ); |
0 | 4467 |
} |
4468 |
} |
|
4469 |
||
9 | 4470 |
if ( ! is_object( $term ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4471 |
$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); |
9 | 4472 |
} |
4473 |
||
4474 |
if ( is_wp_error( $term ) ) { |
|
0 | 4475 |
return $term; |
9 | 4476 |
} |
0 | 4477 |
|
4478 |
$taxonomy = $term->taxonomy; |
|
4479 |
||
9 | 4480 |
$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy ); |
0 | 4481 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4482 |
/** |
18 | 4483 |
* Filters the permalink structure for a term before token replacement occurs. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4484 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4485 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4486 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4487 |
* @param string $termlink The permalink structure for the term's taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4488 |
* @param WP_Term $term The term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4489 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4490 |
$termlink = apply_filters( 'pre_term_link', $termlink, $term ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4491 |
|
0 | 4492 |
$slug = $term->slug; |
9 | 4493 |
$t = get_taxonomy( $taxonomy ); |
4494 |
||
4495 |
if ( empty( $termlink ) ) { |
|
16 | 4496 |
if ( 'category' === $taxonomy ) { |
0 | 4497 |
$termlink = '?cat=' . $term->term_id; |
9 | 4498 |
} elseif ( $t->query_var ) { |
0 | 4499 |
$termlink = "?$t->query_var=$slug"; |
9 | 4500 |
} else { |
0 | 4501 |
$termlink = "?taxonomy=$taxonomy&term=$slug"; |
9 | 4502 |
} |
4503 |
$termlink = home_url( $termlink ); |
|
0 | 4504 |
} else { |
18 | 4505 |
if ( ! empty( $t->rewrite['hierarchical'] ) ) { |
0 | 4506 |
$hierarchical_slugs = array(); |
9 | 4507 |
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); |
4508 |
foreach ( (array) $ancestors as $ancestor ) { |
|
4509 |
$ancestor_term = get_term( $ancestor, $taxonomy ); |
|
0 | 4510 |
$hierarchical_slugs[] = $ancestor_term->slug; |
4511 |
} |
|
9 | 4512 |
$hierarchical_slugs = array_reverse( $hierarchical_slugs ); |
0 | 4513 |
$hierarchical_slugs[] = $slug; |
9 | 4514 |
$termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink ); |
0 | 4515 |
} else { |
9 | 4516 |
$termlink = str_replace( "%$taxonomy%", $slug, $termlink ); |
0 | 4517 |
} |
9 | 4518 |
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) ); |
0 | 4519 |
} |
16 | 4520 |
|
4521 |
// Back compat filters. |
|
4522 |
if ( 'post_tag' === $taxonomy ) { |
|
5 | 4523 |
|
4524 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4525 |
* Filters the tag link. |
5 | 4526 |
* |
4527 |
* @since 2.3.0 |
|
16 | 4528 |
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter. |
4529 |
* @since 5.4.1 Restored (un-deprecated). |
|
5 | 4530 |
* |
4531 |
* @param string $termlink Tag link URL. |
|
4532 |
* @param int $term_id Term ID. |
|
4533 |
*/ |
|
0 | 4534 |
$termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); |
16 | 4535 |
} elseif ( 'category' === $taxonomy ) { |
5 | 4536 |
|
4537 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4538 |
* Filters the category link. |
5 | 4539 |
* |
4540 |
* @since 1.5.0 |
|
16 | 4541 |
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter. |
4542 |
* @since 5.4.1 Restored (un-deprecated). |
|
5 | 4543 |
* |
4544 |
* @param string $termlink Category link URL. |
|
4545 |
* @param int $term_id Term ID. |
|
4546 |
*/ |
|
0 | 4547 |
$termlink = apply_filters( 'category_link', $termlink, $term->term_id ); |
5 | 4548 |
} |
4549 |
||
4550 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4551 |
* Filters the term link. |
5 | 4552 |
* |
4553 |
* @since 2.5.0 |
|
4554 |
* |
|
16 | 4555 |
* @param string $termlink Term link URL. |
4556 |
* @param WP_Term $term Term object. |
|
4557 |
* @param string $taxonomy Taxonomy slug. |
|
5 | 4558 |
*/ |
4559 |
return apply_filters( 'term_link', $termlink, $term, $taxonomy ); |
|
0 | 4560 |
} |
4561 |
||
4562 |
/** |
|
4563 |
* Display the taxonomies of a post with available options. |
|
4564 |
* |
|
4565 |
* This function can be used within the loop to display the taxonomies for a |
|
4566 |
* post without specifying the Post ID. You can also use it outside the Loop to |
|
4567 |
* display the taxonomies for a specific post. |
|
4568 |
* |
|
4569 |
* @since 2.5.0 |
|
5 | 4570 |
* |
4571 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4572 |
* Arguments about which post to use and how to format the output. Shares all of the arguments |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4573 |
* supported by get_the_taxonomies(), in addition to the following. |
5 | 4574 |
* |
4575 |
* @type int|WP_Post $post Post ID or object to get taxonomies of. Default current post. |
|
4576 |
* @type string $before Displays before the taxonomies. Default empty string. |
|
4577 |
* @type string $sep Separates each taxonomy. Default is a space. |
|
4578 |
* @type string $after Displays after the taxonomies. Default empty string. |
|
4579 |
* } |
|
0 | 4580 |
*/ |
5 | 4581 |
function the_taxonomies( $args = array() ) { |
0 | 4582 |
$defaults = array( |
9 | 4583 |
'post' => 0, |
0 | 4584 |
'before' => '', |
9 | 4585 |
'sep' => ' ', |
4586 |
'after' => '', |
|
0 | 4587 |
); |
4588 |
||
16 | 4589 |
$parsed_args = wp_parse_args( $args, $defaults ); |
4590 |
||
18 | 4591 |
echo $parsed_args['before'] . implode( $parsed_args['sep'], get_the_taxonomies( $parsed_args['post'], $parsed_args ) ) . $parsed_args['after']; |
0 | 4592 |
} |
4593 |
||
4594 |
/** |
|
4595 |
* Retrieve all taxonomies associated with a post. |
|
4596 |
* |
|
4597 |
* This function can be used within the loop. It will also return an array of |
|
4598 |
* the taxonomies with links to the taxonomy and name. |
|
4599 |
* |
|
4600 |
* @since 2.5.0 |
|
4601 |
* |
|
5 | 4602 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
16 | 4603 |
* @param array $args { |
4604 |
* Optional. Arguments about how to format the list of taxonomies. Default empty array. |
|
5 | 4605 |
* |
4606 |
* @type string $template Template for displaying a taxonomy label and list of terms. |
|
4607 |
* Default is "Label: Terms." |
|
4608 |
* @type string $term_template Template for displaying a single term in the list. Default is the term name |
|
4609 |
* linked to its archive. |
|
4610 |
* } |
|
4611 |
* @return array List of taxonomies. |
|
0 | 4612 |
*/ |
5 | 4613 |
function get_the_taxonomies( $post = 0, $args = array() ) { |
0 | 4614 |
$post = get_post( $post ); |
4615 |
||
9 | 4616 |
$args = wp_parse_args( |
4617 |
$args, |
|
4618 |
array( |
|
16 | 4619 |
/* translators: %s: Taxonomy label, %l: List of terms formatted as per $term_template. */ |
9 | 4620 |
'template' => __( '%s: %l.' ), |
4621 |
'term_template' => '<a href="%1$s">%2$s</a>', |
|
4622 |
) |
|
4623 |
); |
|
0 | 4624 |
|
4625 |
$taxonomies = array(); |
|
4626 |
||
5 | 4627 |
if ( ! $post ) { |
0 | 4628 |
return $taxonomies; |
5 | 4629 |
} |
4630 |
||
4631 |
foreach ( get_object_taxonomies( $post ) as $taxonomy ) { |
|
4632 |
$t = (array) get_taxonomy( $taxonomy ); |
|
4633 |
if ( empty( $t['label'] ) ) { |
|
0 | 4634 |
$t['label'] = $taxonomy; |
5 | 4635 |
} |
4636 |
if ( empty( $t['args'] ) ) { |
|
0 | 4637 |
$t['args'] = array(); |
5 | 4638 |
} |
4639 |
if ( empty( $t['template'] ) ) { |
|
4640 |
$t['template'] = $args['template']; |
|
4641 |
} |
|
4642 |
if ( empty( $t['term_template'] ) ) { |
|
4643 |
$t['term_template'] = $args['term_template']; |
|
4644 |
} |
|
4645 |
||
4646 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
|
4647 |
if ( false === $terms ) { |
|
4648 |
$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); |
|
4649 |
} |
|
0 | 4650 |
$links = array(); |
4651 |
||
5 | 4652 |
foreach ( $terms as $term ) { |
4653 |
$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name ); |
|
4654 |
} |
|
4655 |
if ( $links ) { |
|
9 | 4656 |
$taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); |
5 | 4657 |
} |
0 | 4658 |
} |
4659 |
return $taxonomies; |
|
4660 |
} |
|
4661 |
||
4662 |
/** |
|
16 | 4663 |
* Retrieve all taxonomy names for the given post. |
0 | 4664 |
* |
4665 |
* @since 2.5.0 |
|
5 | 4666 |
* |
4667 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
|
16 | 4668 |
* @return string[] An array of all taxonomy names for the given post. |
0 | 4669 |
*/ |
5 | 4670 |
function get_post_taxonomies( $post = 0 ) { |
0 | 4671 |
$post = get_post( $post ); |
4672 |
||
9 | 4673 |
return get_object_taxonomies( $post ); |
0 | 4674 |
} |
4675 |
||
4676 |
/** |
|
4677 |
* Determine if the given object is associated with any of the given terms. |
|
4678 |
* |
|
4679 |
* The given terms are checked against the object's terms' term_ids, names and slugs. |
|
4680 |
* Terms given as integers will only be checked against the object's terms' term_ids. |
|
4681 |
* If no terms are given, determines if object is associated with any terms in the given taxonomy. |
|
4682 |
* |
|
4683 |
* @since 2.7.0 |
|
4684 |
* |
|
18 | 4685 |
* @param int $object_id ID of the object (post ID, link ID, ...). |
4686 |
* @param string $taxonomy Single taxonomy name. |
|
4687 |
* @param int|string|int[]|string[] $terms Optional. Term ID, name, slug, or array of such |
|
4688 |
* to check against. Default null. |
|
5 | 4689 |
* @return bool|WP_Error WP_Error on input error. |
0 | 4690 |
*/ |
4691 |
function is_object_in_term( $object_id, $taxonomy, $terms = null ) { |
|
16 | 4692 |
$object_id = (int) $object_id; |
4693 |
if ( ! $object_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4694 |
return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) ); |
9 | 4695 |
} |
0 | 4696 |
|
4697 |
$object_terms = get_object_term_cache( $object_id, $taxonomy ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4698 |
if ( false === $object_terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4699 |
$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4700 |
if ( is_wp_error( $object_terms ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4701 |
return $object_terms; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4702 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4703 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4704 |
wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4705 |
} |
0 | 4706 |
|
9 | 4707 |
if ( is_wp_error( $object_terms ) ) { |
0 | 4708 |
return $object_terms; |
9 | 4709 |
} |
4710 |
if ( empty( $object_terms ) ) { |
|
0 | 4711 |
return false; |
9 | 4712 |
} |
4713 |
if ( empty( $terms ) ) { |
|
4714 |
return ( ! empty( $object_terms ) ); |
|
4715 |
} |
|
0 | 4716 |
|
4717 |
$terms = (array) $terms; |
|
4718 |
||
16 | 4719 |
$ints = array_filter( $terms, 'is_int' ); |
4720 |
if ( $ints ) { |
|
0 | 4721 |
$strs = array_diff( $terms, $ints ); |
9 | 4722 |
} else { |
0 | 4723 |
$strs =& $terms; |
9 | 4724 |
} |
0 | 4725 |
|
4726 |
foreach ( $object_terms as $object_term ) { |
|
5 | 4727 |
// If term is an int, check against term_ids only. |
16 | 4728 |
if ( $ints && in_array( $object_term->term_id, $ints, true ) ) { |
5 | 4729 |
return true; |
4730 |
} |
|
4731 |
||
0 | 4732 |
if ( $strs ) { |
5 | 4733 |
// Only check numeric strings against term_id, to avoid false matches due to type juggling. |
4734 |
$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) ); |
|
4735 |
if ( in_array( $object_term->term_id, $numeric_strs, true ) ) { |
|
4736 |
return true; |
|
4737 |
} |
|
4738 |
||
16 | 4739 |
if ( in_array( $object_term->name, $strs, true ) ) { |
9 | 4740 |
return true; |
4741 |
} |
|
16 | 4742 |
if ( in_array( $object_term->slug, $strs, true ) ) { |
9 | 4743 |
return true; |
4744 |
} |
|
0 | 4745 |
} |
4746 |
} |
|
4747 |
||
4748 |
return false; |
|
4749 |
} |
|
4750 |
||
4751 |
/** |
|
4752 |
* Determine if the given object type is associated with the given taxonomy. |
|
4753 |
* |
|
4754 |
* @since 3.0.0 |
|
4755 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4756 |
* @param string $object_type Object type string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4757 |
* @param string $taxonomy Single taxonomy name. |
0 | 4758 |
* @return bool True if object is associated with the taxonomy, otherwise false. |
4759 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4760 |
function is_object_in_taxonomy( $object_type, $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4761 |
$taxonomies = get_object_taxonomies( $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4762 |
if ( empty( $taxonomies ) ) { |
0 | 4763 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4764 |
} |
16 | 4765 |
return in_array( $taxonomy, $taxonomies, true ); |
0 | 4766 |
} |
4767 |
||
4768 |
/** |
|
4769 |
* Get an array of ancestor IDs for a given object. |
|
4770 |
* |
|
5 | 4771 |
* @since 3.1.0 |
4772 |
* @since 4.1.0 Introduced the `$resource_type` argument. |
|
4773 |
* |
|
4774 |
* @param int $object_id Optional. The ID of the object. Default 0. |
|
4775 |
* @param string $object_type Optional. The type of object for which we'll be retrieving |
|
4776 |
* ancestors. Accepts a post type or a taxonomy name. Default empty. |
|
4777 |
* @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type' |
|
4778 |
* or 'taxonomy'. Default empty. |
|
16 | 4779 |
* @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy. |
0 | 4780 |
*/ |
5 | 4781 |
function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) { |
0 | 4782 |
$object_id = (int) $object_id; |
4783 |
||
4784 |
$ancestors = array(); |
|
4785 |
||
4786 |
if ( empty( $object_id ) ) { |
|
5 | 4787 |
|
4788 |
/** This filter is documented in wp-includes/taxonomy.php */ |
|
4789 |
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type ); |
|
0 | 4790 |
} |
4791 |
||
5 | 4792 |
if ( ! $resource_type ) { |
4793 |
if ( is_taxonomy_hierarchical( $object_type ) ) { |
|
4794 |
$resource_type = 'taxonomy'; |
|
4795 |
} elseif ( post_type_exists( $object_type ) ) { |
|
4796 |
$resource_type = 'post_type'; |
|
4797 |
} |
|
4798 |
} |
|
4799 |
||
4800 |
if ( 'taxonomy' === $resource_type ) { |
|
9 | 4801 |
$term = get_term( $object_id, $object_type ); |
16 | 4802 |
while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors, true ) ) { |
0 | 4803 |
$ancestors[] = (int) $term->parent; |
9 | 4804 |
$term = get_term( $term->parent, $object_type ); |
0 | 4805 |
} |
5 | 4806 |
} elseif ( 'post_type' === $resource_type ) { |
9 | 4807 |
$ancestors = get_post_ancestors( $object_id ); |
0 | 4808 |
} |
4809 |
||
5 | 4810 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4811 |
* Filters a given object's ancestors. |
5 | 4812 |
* |
4813 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4814 |
* @since 4.1.1 Introduced the `$resource_type` parameter. |
5 | 4815 |
* |
16 | 4816 |
* @param int[] $ancestors An array of IDs of object ancestors. |
5 | 4817 |
* @param int $object_id Object ID. |
4818 |
* @param string $object_type Type of object. |
|
4819 |
* @param string $resource_type Type of resource $object_type is. |
|
4820 |
*/ |
|
4821 |
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type ); |
|
0 | 4822 |
} |
4823 |
||
4824 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4825 |
* Returns the term's parent's term_ID. |
0 | 4826 |
* |
4827 |
* @since 3.1.0 |
|
4828 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4829 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4830 |
* @param string $taxonomy Taxonomy name. |
18 | 4831 |
* @return int|false Parent term ID on success, false on failure. |
0 | 4832 |
*/ |
4833 |
function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) { |
|
4834 |
$term = get_term( $term_id, $taxonomy ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4835 |
if ( ! $term || is_wp_error( $term ) ) { |
0 | 4836 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4837 |
} |
0 | 4838 |
return (int) $term->parent; |
4839 |
} |
|
4840 |
||
4841 |
/** |
|
4842 |
* Checks the given subset of the term hierarchy for hierarchy loops. |
|
4843 |
* Prevents loops from forming and breaks those that it finds. |
|
4844 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4845 |
* Attached to the {@see 'wp_update_term_parent'} filter. |
0 | 4846 |
* |
4847 |
* @since 3.1.0 |
|
4848 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4849 |
* @param int $parent `term_id` of the parent for the term we're checking. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4850 |
* @param int $term_id The term we're checking. |
0 | 4851 |
* @param string $taxonomy The taxonomy of the term we're checking. |
4852 |
* @return int The new parent for the term. |
|
4853 |
*/ |
|
4854 |
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { |
|
16 | 4855 |
// Nothing fancy here - bail. |
9 | 4856 |
if ( ! $parent ) { |
0 | 4857 |
return 0; |
9 | 4858 |
} |
0 | 4859 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4860 |
// Can't be its own parent. |
16 | 4861 |
if ( $parent === $term_id ) { |
0 | 4862 |
return 0; |
9 | 4863 |
} |
0 | 4864 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4865 |
// Now look for larger loops. |
16 | 4866 |
$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ); |
4867 |
if ( ! $loop ) { |
|
4868 |
return $parent; // No loop. |
|
9 | 4869 |
} |
0 | 4870 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4871 |
// Setting $parent to the given value causes a loop. |
9 | 4872 |
if ( isset( $loop[ $term_id ] ) ) { |
0 | 4873 |
return 0; |
9 | 4874 |
} |
0 | 4875 |
|
4876 |
// There's a loop, but it doesn't contain $term_id. Break the loop. |
|
9 | 4877 |
foreach ( array_keys( $loop ) as $loop_member ) { |
0 | 4878 |
wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); |
9 | 4879 |
} |
0 | 4880 |
|
4881 |
return $parent; |
|
4882 |
} |
|
9 | 4883 |
|
4884 |
/** |
|
4885 |
* Determines whether a taxonomy is considered "viewable". |
|
4886 |
* |
|
4887 |
* @since 5.1.0 |
|
4888 |
* |
|
4889 |
* @param string|WP_Taxonomy $taxonomy Taxonomy name or object. |
|
4890 |
* @return bool Whether the taxonomy should be considered viewable. |
|
4891 |
*/ |
|
4892 |
function is_taxonomy_viewable( $taxonomy ) { |
|
4893 |
if ( is_scalar( $taxonomy ) ) { |
|
4894 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
4895 |
if ( ! $taxonomy ) { |
|
4896 |
return false; |
|
4897 |
} |
|
4898 |
} |
|
4899 |
||
4900 |
return $taxonomy->publicly_queryable; |
|
4901 |
} |
|
4902 |
||
4903 |
/** |
|
4904 |
* Sets the last changed time for the 'terms' cache group. |
|
4905 |
* |
|
4906 |
* @since 5.0.0 |
|
4907 |
*/ |
|
4908 |
function wp_cache_set_terms_last_changed() { |
|
4909 |
wp_cache_set( 'last_changed', microtime(), 'terms' ); |
|
4910 |
} |
|
4911 |
||
4912 |
/** |
|
4913 |
* Aborts calls to term meta if it is not supported. |
|
4914 |
* |
|
4915 |
* @since 5.0.0 |
|
4916 |
* |
|
4917 |
* @param mixed $check Skip-value for whether to proceed term meta function execution. |
|
4918 |
* @return mixed Original value of $check, or false if term meta is not supported. |
|
4919 |
*/ |
|
4920 |
function wp_check_term_meta_support_prefilter( $check ) { |
|
4921 |
if ( get_option( 'db_version' ) < 34370 ) { |
|
4922 |
return false; |
|
4923 |
} |
|
4924 |
||
4925 |
return $check; |
|
4926 |
} |