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