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