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