author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 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' ), |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
149 |
'add_new_item' => __( 'Add Link Category' ), |
9 | 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' ), |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
236 |
'add_new_item' => __( 'Add Category' ), |
21
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'. |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
681 |
* @type string $add_new_item Default 'Add Tag'/'Add Category'. |
7
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. |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1389 |
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1390 |
* will be returned as the same type when retrieved. Other data types will |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1391 |
* be stored as strings in the database: |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1392 |
* - false is stored and retrieved as an empty string ('') |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1393 |
* - true is stored and retrieved as '1' |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1394 |
* - numbers (both integer and float) are stored and retrieved as strings |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1395 |
* Must be serializable if non-scalar. |
16 | 1396 |
* @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
|
1397 |
* Default false. |
16 | 1398 |
* @return int|false|WP_Error Meta ID on success, false on failure. |
1399 |
* 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
|
1400 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
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
|
1402 |
if ( wp_term_is_shared( $term_id ) ) { |
9 | 1403 |
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
|
1404 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
|
9 | 1406 |
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
|
1407 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* Removes metadata matching criteria from a term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
* @param string $meta_key Metadata name. |
16 | 1416 |
* @param mixed $meta_value Optional. Metadata value. If provided, |
1417 |
* rows will only be removed that match the value. |
|
1418 |
* Must be serializable if non-scalar. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) { |
9 | 1422 |
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
|
1423 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
* Retrieves metadata for a term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
* @param int $term_id Term ID. |
16 | 1431 |
* @param string $key Optional. The meta key to retrieve. By default, |
1432 |
* returns data for all keys. Default empty. |
|
1433 |
* @param bool $single Optional. Whether to return a single value. |
|
18 | 1434 |
* This parameter has no effect if `$key` is not specified. |
16 | 1435 |
* Default false. |
18 | 1436 |
* @return mixed An array of values if `$single` is false. |
1437 |
* The value of the meta field if `$single` is true. |
|
1438 |
* False for an invalid `$term_id` (non-numeric, zero, or negative value). |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1439 |
* An empty array if a valid but non-existing term ID is passed and `$single` is false. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1440 |
* An empty string if a valid but non-existing term ID is passed and `$single` is true. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1441 |
* Note: Non-serialized values are returned as strings: |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1442 |
* - false values are returned as empty strings ('') |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1443 |
* - true values are returned as '1' |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1444 |
* - numbers are returned as strings |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1445 |
* Arrays and objects retain their original type. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1447 |
function get_term_meta( $term_id, $key = '', $single = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1448 |
return get_metadata( 'term', $term_id, $key, $single ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
* Updates term metadata. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
* 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
|
1455 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1456 |
* 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
|
1457 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
* @param string $meta_key Metadata key. |
16 | 1462 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
1463 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
|
1464 |
* If specified, only update existing metadata entries with |
|
1465 |
* this value. Otherwise, update all entries. Default empty. |
|
1466 |
* @return int|bool|WP_Error Meta ID if the key didn't exist. true on successful update, |
|
1467 |
* false on failure or if the value passed to the function |
|
1468 |
* is the same as the one that is already in the database. |
|
1469 |
* 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
|
1470 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1471 |
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
|
1472 |
if ( wp_term_is_shared( $term_id ) ) { |
9 | 1473 |
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
|
1474 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1475 |
|
9 | 1476 |
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
|
1477 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1478 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1479 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
* Updates metadata cache for list of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1481 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
* 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
|
1483 |
* 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
|
1484 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1485 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1486 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
* @param array $term_ids List of term IDs. |
16 | 1488 |
* @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
|
1489 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
function update_termmeta_cache( $term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
return update_meta_cache( 'term', $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1494 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1495 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1496 |
* 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
|
1497 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1498 |
* @since 6.3.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1499 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1500 |
* @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
|
1501 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1502 |
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
|
1503 |
if ( empty( $term_ids ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1504 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1505 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1506 |
$lazyloader = wp_metadata_lazyloader(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1507 |
$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
|
1508 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1509 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
/** |
19 | 1511 |
* 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
|
1512 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1516 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1517 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1518 |
* @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
|
1519 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1520 |
function has_term_meta( $term_id ) { |
9 | 1521 |
$check = wp_check_term_meta_support_prefilter( null ); |
1522 |
if ( null !== $check ) { |
|
1523 |
return $check; |
|
0 | 1524 |
} |
1525 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
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
|
1529 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
* Registers a meta key for terms. |
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 |
* @since 4.9.8 |
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 |
* @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
|
1537 |
* to register the meta key across all existing taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
* @param string $meta_key The meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
* @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
|
1540 |
* {@see register_meta()} for a list of supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
* @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
|
1542 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
function register_term_meta( $taxonomy, $meta_key, array $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
$args['object_subtype'] = $taxonomy; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
return register_meta( 'term', $meta_key, $args ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1550 |
* Unregisters a meta key for terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1551 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1552 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1553 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1554 |
* @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
|
1555 |
* 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
|
1556 |
* existing taxonomies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
* @param string $meta_key The meta key to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1558 |
* @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
|
1559 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
function unregister_term_meta( $taxonomy, $meta_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
return unregister_meta_key( 'term', $meta_key, $taxonomy ); |
0 | 1562 |
} |
1563 |
||
1564 |
/** |
|
16 | 1565 |
* Determines whether a taxonomy term exists. |
0 | 1566 |
* |
1567 |
* Formerly is_term(), introduced in 2.3.0. |
|
1568 |
* |
|
9 | 1569 |
* For more information on this and similar theme functions, check out |
1570 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1571 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
1572 |
* |
|
0 | 1573 |
* @since 3.0.0 |
19 | 1574 |
* @since 6.0.0 Converted to use `get_terms()`. |
1575 |
* |
|
1576 |
* @global bool $_wp_suspend_cache_invalidation |
|
0 | 1577 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1578 |
* @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
|
1579 |
* @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
|
1580 |
* @param int $parent_term Optional. ID of parent term under which to confine the exists search. |
16 | 1581 |
* @return mixed Returns null if the term does not exist. |
1582 |
* Returns the term ID if no taxonomy is specified and the term ID exists. |
|
1583 |
* Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists. |
|
1584 |
* Returns 0 if term ID 0 is passed to the function. |
|
0 | 1585 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1586 |
function term_exists( $term, $taxonomy = '', $parent_term = null ) { |
19 | 1587 |
global $_wp_suspend_cache_invalidation; |
1588 |
||
1589 |
if ( null === $term ) { |
|
1590 |
return null; |
|
1591 |
} |
|
1592 |
||
1593 |
$defaults = array( |
|
1594 |
'get' => 'all', |
|
1595 |
'fields' => 'ids', |
|
1596 |
'number' => 1, |
|
1597 |
'update_term_meta_cache' => false, |
|
1598 |
'order' => 'ASC', |
|
1599 |
'orderby' => 'term_id', |
|
1600 |
'suppress_filter' => true, |
|
1601 |
); |
|
1602 |
||
1603 |
// Ensure that while importing, queries are not cached. |
|
1604 |
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
|
1605 |
$defaults['cache_results'] = false; |
19 | 1606 |
} |
1607 |
||
1608 |
if ( ! empty( $taxonomy ) ) { |
|
1609 |
$defaults['taxonomy'] = $taxonomy; |
|
1610 |
$defaults['fields'] = 'all'; |
|
1611 |
} |
|
1612 |
||
1613 |
/** |
|
1614 |
* Filters default query arguments for checking if a term exists. |
|
1615 |
* |
|
1616 |
* @since 6.0.0 |
|
1617 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1618 |
* @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
|
1619 |
* @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
|
1620 |
* @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
|
1621 |
* 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
|
1622 |
* @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
|
1623 |
* Null indicates the search is unconfined. |
19 | 1624 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1625 |
$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term ); |
0 | 1626 |
|
9 | 1627 |
if ( is_int( $term ) ) { |
16 | 1628 |
if ( 0 === $term ) { |
0 | 1629 |
return 0; |
9 | 1630 |
} |
19 | 1631 |
$args = wp_parse_args( array( 'include' => array( $term ) ), $defaults ); |
1632 |
$terms = get_terms( $args ); |
|
1633 |
} else { |
|
1634 |
$term = trim( wp_unslash( $term ) ); |
|
1635 |
if ( '' === $term ) { |
|
1636 |
return null; |
|
1637 |
} |
|
1638 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1639 |
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
|
1640 |
$defaults['parent'] = (int) $parent_term; |
9 | 1641 |
} |
19 | 1642 |
|
1643 |
$args = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults ); |
|
1644 |
$terms = get_terms( $args ); |
|
1645 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
|
1646 |
$args = wp_parse_args( array( 'name' => $term ), $defaults ); |
|
1647 |
$terms = get_terms( $args ); |
|
1648 |
} |
|
1649 |
} |
|
1650 |
||
1651 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
|
1652 |
return null; |
|
1653 |
} |
|
1654 |
||
1655 |
$_term = array_shift( $terms ); |
|
1656 |
||
9 | 1657 |
if ( ! empty( $taxonomy ) ) { |
19 | 1658 |
return array( |
1659 |
'term_id' => (string) $_term->term_id, |
|
1660 |
'term_taxonomy_id' => (string) $_term->term_taxonomy_id, |
|
1661 |
); |
|
1662 |
} |
|
1663 |
||
1664 |
return (string) $_term; |
|
0 | 1665 |
} |
1666 |
||
1667 |
/** |
|
19 | 1668 |
* Checks if a term is an ancestor of another term. |
0 | 1669 |
* |
16 | 1670 |
* You can use either an ID or the term object for both parameters. |
0 | 1671 |
* |
1672 |
* @since 3.4.0 |
|
1673 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
* @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
|
1675 |
* @param int|object $term2 The child term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
* @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
|
1677 |
* @return bool Whether `$term2` is a child of `$term1`. |
0 | 1678 |
*/ |
1679 |
function term_is_ancestor_of( $term1, $term2, $taxonomy ) { |
|
9 | 1680 |
if ( ! isset( $term1->term_id ) ) { |
0 | 1681 |
$term1 = get_term( $term1, $taxonomy ); |
9 | 1682 |
} |
1683 |
if ( ! isset( $term2->parent ) ) { |
|
0 | 1684 |
$term2 = get_term( $term2, $taxonomy ); |
9 | 1685 |
} |
1686 |
||
1687 |
if ( empty( $term1->term_id ) || empty( $term2->parent ) ) { |
|
0 | 1688 |
return false; |
9 | 1689 |
} |
16 | 1690 |
if ( $term2->parent === $term1->term_id ) { |
0 | 1691 |
return true; |
9 | 1692 |
} |
0 | 1693 |
|
1694 |
return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy ); |
|
1695 |
} |
|
1696 |
||
1697 |
/** |
|
19 | 1698 |
* Sanitizes all term fields. |
0 | 1699 |
* |
1700 |
* Relies on sanitize_term_field() to sanitize the term. The difference is that |
|
18 | 1701 |
* this function will sanitize **all** fields. The context is based |
0 | 1702 |
* on sanitize_term_field(). |
1703 |
* |
|
18 | 1704 |
* The `$term` is expected to be either an array or an object. |
0 | 1705 |
* |
1706 |
* @since 2.3.0 |
|
1707 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
* @param array|object $term The term to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
* @param string $taxonomy The taxonomy name to use. |
18 | 1710 |
* @param string $context Optional. Context in which to sanitize the term. |
1711 |
* Accepts 'raw', 'edit', 'db', 'display', 'rss', |
|
1712 |
* 'attribute', or 'js'. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1713 |
* @return array|object Term with all fields sanitized. |
0 | 1714 |
*/ |
9 | 1715 |
function sanitize_term( $term, $taxonomy, $context = 'display' ) { |
5 | 1716 |
$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); |
1717 |
||
1718 |
$do_object = is_object( $term ); |
|
0 | 1719 |
|
9 | 1720 |
$term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 ); |
0 | 1721 |
|
1722 |
foreach ( (array) $fields as $field ) { |
|
1723 |
if ( $do_object ) { |
|
9 | 1724 |
if ( isset( $term->$field ) ) { |
1725 |
$term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context ); |
|
1726 |
} |
|
0 | 1727 |
} else { |
9 | 1728 |
if ( isset( $term[ $field ] ) ) { |
1729 |
$term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context ); |
|
1730 |
} |
|
0 | 1731 |
} |
1732 |
} |
|
1733 |
||
9 | 1734 |
if ( $do_object ) { |
0 | 1735 |
$term->filter = $context; |
9 | 1736 |
} else { |
0 | 1737 |
$term['filter'] = $context; |
9 | 1738 |
} |
0 | 1739 |
|
1740 |
return $term; |
|
1741 |
} |
|
1742 |
||
1743 |
/** |
|
19 | 1744 |
* Sanitizes the field value in the term based on the context. |
0 | 1745 |
* |
1746 |
* Passing a term field value through the function should be assumed to have |
|
1747 |
* cleansed the value for whatever context the term field is going to be used. |
|
1748 |
* |
|
1749 |
* If no context or an unsupported context is given, then default filters will |
|
1750 |
* be applied. |
|
1751 |
* |
|
1752 |
* There are enough filters for each context to support a custom filtering |
|
1753 |
* without creating your own filter function. Simply create a function that |
|
1754 |
* hooks into the filter you need. |
|
1755 |
* |
|
1756 |
* @since 2.3.0 |
|
1757 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1758 |
* @param string $field Term field to sanitize. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
* @param string $value Search for this term value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1760 |
* @param int $term_id Term ID. |
19 | 1761 |
* @param string $taxonomy Taxonomy name. |
18 | 1762 |
* @param string $context Context in which to sanitize the term field. |
1763 |
* Accepts 'raw', 'edit', 'db', 'display', 'rss', |
|
1764 |
* 'attribute', or 'js'. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
* @return mixed Sanitized field. |
0 | 1766 |
*/ |
9 | 1767 |
function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { |
5 | 1768 |
$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); |
16 | 1769 |
if ( in_array( $field, $int_fields, true ) ) { |
0 | 1770 |
$value = (int) $value; |
9 | 1771 |
if ( $value < 0 ) { |
0 | 1772 |
$value = 0; |
9 | 1773 |
} |
0 | 1774 |
} |
1775 |
||
16 | 1776 |
$context = strtolower( $context ); |
1777 |
||
1778 |
if ( 'raw' === $context ) { |
|
0 | 1779 |
return $value; |
9 | 1780 |
} |
0 | 1781 |
|
16 | 1782 |
if ( 'edit' === $context ) { |
5 | 1783 |
|
1784 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1785 |
* Filters a term field to edit before it is sanitized. |
5 | 1786 |
* |
19 | 1787 |
* The dynamic portion of the hook name, `$field`, refers to the term field. |
5 | 1788 |
* |
1789 |
* @since 2.3.0 |
|
1790 |
* |
|
1791 |
* @param mixed $value Value of the term field. |
|
1792 |
* @param int $term_id Term ID. |
|
1793 |
* @param string $taxonomy Taxonomy slug. |
|
1794 |
*/ |
|
1795 |
$value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy ); |
|
1796 |
||
1797 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1798 |
* Filters the taxonomy field to edit before it is sanitized. |
5 | 1799 |
* |
1800 |
* The dynamic portions of the filter name, `$taxonomy` and `$field`, refer |
|
1801 |
* to the taxonomy slug and taxonomy field, respectively. |
|
1802 |
* |
|
1803 |
* @since 2.3.0 |
|
1804 |
* |
|
1805 |
* @param mixed $value Value of the taxonomy field to edit. |
|
1806 |
* @param int $term_id Term ID. |
|
1807 |
*/ |
|
1808 |
$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
|
1809 |
|
16 | 1810 |
if ( 'description' === $field ) { |
9 | 1811 |
$value = esc_html( $value ); // textarea_escaped |
1812 |
} else { |
|
1813 |
$value = esc_attr( $value ); |
|
1814 |
} |
|
16 | 1815 |
} elseif ( 'db' === $context ) { |
5 | 1816 |
|
1817 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1818 |
* Filters a term field value before it is sanitized. |
5 | 1819 |
* |
19 | 1820 |
* The dynamic portion of the hook name, `$field`, refers to the term field. |
5 | 1821 |
* |
1822 |
* @since 2.3.0 |
|
1823 |
* |
|
1824 |
* @param mixed $value Value of the term field. |
|
1825 |
* @param string $taxonomy Taxonomy slug. |
|
1826 |
*/ |
|
1827 |
$value = apply_filters( "pre_term_{$field}", $value, $taxonomy ); |
|
1828 |
||
1829 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1830 |
* Filters a taxonomy field before it is sanitized. |
5 | 1831 |
* |
1832 |
* The dynamic portions of the filter name, `$taxonomy` and `$field`, refer |
|
1833 |
* to the taxonomy slug and field name, respectively. |
|
1834 |
* |
|
1835 |
* @since 2.3.0 |
|
1836 |
* |
|
1837 |
* @param mixed $value Value of the taxonomy field. |
|
1838 |
*/ |
|
1839 |
$value = apply_filters( "pre_{$taxonomy}_{$field}", $value ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1840 |
|
16 | 1841 |
// Back compat filters. |
1842 |
if ( 'slug' === $field ) { |
|
5 | 1843 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
* Filters the category nicename before it is sanitized. |
5 | 1845 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
* Use the {@see 'pre_$taxonomy_$field'} hook instead. |
5 | 1847 |
* |
1848 |
* @since 2.0.3 |
|
1849 |
* |
|
1850 |
* @param string $value The category nicename. |
|
1851 |
*/ |
|
1852 |
$value = apply_filters( 'pre_category_nicename', $value ); |
|
1853 |
} |
|
16 | 1854 |
} elseif ( 'rss' === $context ) { |
5 | 1855 |
|
1856 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1857 |
* Filters the term field for use in RSS. |
5 | 1858 |
* |
19 | 1859 |
* The dynamic portion of the hook name, `$field`, refers to the term field. |
5 | 1860 |
* |
1861 |
* @since 2.3.0 |
|
1862 |
* |
|
1863 |
* @param mixed $value Value of the term field. |
|
1864 |
* @param string $taxonomy Taxonomy slug. |
|
1865 |
*/ |
|
1866 |
$value = apply_filters( "term_{$field}_rss", $value, $taxonomy ); |
|
1867 |
||
1868 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1869 |
* Filters the taxonomy field for use in RSS. |
5 | 1870 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1871 |
* The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer |
5 | 1872 |
* to the taxonomy slug and field name, respectively. |
1873 |
* |
|
1874 |
* @since 2.3.0 |
|
1875 |
* |
|
1876 |
* @param mixed $value Value of the taxonomy field. |
|
1877 |
*/ |
|
1878 |
$value = apply_filters( "{$taxonomy}_{$field}_rss", $value ); |
|
0 | 1879 |
} else { |
1880 |
// Use display filters by default. |
|
5 | 1881 |
|
1882 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1883 |
* Filters the term field sanitized for display. |
5 | 1884 |
* |
19 | 1885 |
* The dynamic portion of the hook name, `$field`, refers to the term field name. |
5 | 1886 |
* |
1887 |
* @since 2.3.0 |
|
1888 |
* |
|
1889 |
* @param mixed $value Value of the term field. |
|
1890 |
* @param int $term_id Term ID. |
|
1891 |
* @param string $taxonomy Taxonomy slug. |
|
1892 |
* @param string $context Context to retrieve the term field value. |
|
1893 |
*/ |
|
1894 |
$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context ); |
|
1895 |
||
1896 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1897 |
* Filters the taxonomy field sanitized for display. |
5 | 1898 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1899 |
* The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer |
5 | 1900 |
* to the taxonomy slug and taxonomy field, respectively. |
1901 |
* |
|
1902 |
* @since 2.3.0 |
|
1903 |
* |
|
1904 |
* @param mixed $value Value of the taxonomy field. |
|
1905 |
* @param int $term_id Term ID. |
|
1906 |
* @param string $context Context to retrieve the taxonomy field value. |
|
1907 |
*/ |
|
1908 |
$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context ); |
|
0 | 1909 |
} |
1910 |
||
16 | 1911 |
if ( 'attribute' === $context ) { |
9 | 1912 |
$value = esc_attr( $value ); |
16 | 1913 |
} elseif ( 'js' === $context ) { |
9 | 1914 |
$value = esc_js( $value ); |
5 | 1915 |
} |
18 | 1916 |
|
1917 |
// Restore the type for integer fields after esc_attr(). |
|
1918 |
if ( in_array( $field, $int_fields, true ) ) { |
|
1919 |
$value = (int) $value; |
|
1920 |
} |
|
1921 |
||
0 | 1922 |
return $value; |
1923 |
} |
|
1924 |
||
1925 |
/** |
|
19 | 1926 |
* Counts how many terms are in taxonomy. |
0 | 1927 |
* |
1928 |
* Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). |
|
1929 |
* |
|
1930 |
* @since 2.3.0 |
|
18 | 1931 |
* @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter. |
1932 |
* |
|
1933 |
* @internal The `$deprecated` parameter is parsed for backward compatibility only. |
|
1934 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1935 |
* @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
|
1936 |
* for information on accepted arguments. Default empty array. |
18 | 1937 |
* @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format. |
1938 |
* If present, this parameter will be interpreted as `$args`, and the first |
|
1939 |
* function parameter will be parsed as a taxonomy or array of taxonomies. |
|
1940 |
* Default empty. |
|
1941 |
* @return string|WP_Error Numeric string containing the number of terms in that |
|
1942 |
* taxonomy or WP_Error if the taxonomy does not exist. |
|
0 | 1943 |
*/ |
18 | 1944 |
function wp_count_terms( $args = array(), $deprecated = '' ) { |
1945 |
$use_legacy_args = false; |
|
1946 |
||
1947 |
// Check whether function is used with legacy signature: `$taxonomy` and `$args`. |
|
1948 |
if ( $args |
|
1949 |
&& ( is_string( $args ) && taxonomy_exists( $args ) |
|
1950 |
|| is_array( $args ) && wp_is_numeric_array( $args ) ) |
|
1951 |
) { |
|
1952 |
$use_legacy_args = true; |
|
1953 |
} |
|
1954 |
||
1955 |
$defaults = array( 'hide_empty' => false ); |
|
1956 |
||
1957 |
if ( $use_legacy_args ) { |
|
1958 |
$defaults['taxonomy'] = $args; |
|
1959 |
$args = $deprecated; |
|
1960 |
} |
|
1961 |
||
1962 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 1963 |
|
16 | 1964 |
// Backward compatibility. |
9 | 1965 |
if ( isset( $args['ignore_empty'] ) ) { |
0 | 1966 |
$args['hide_empty'] = $args['ignore_empty']; |
9 | 1967 |
unset( $args['ignore_empty'] ); |
0 | 1968 |
} |
1969 |
||
1970 |
$args['fields'] = 'count'; |
|
1971 |
||
16 | 1972 |
return get_terms( $args ); |
0 | 1973 |
} |
1974 |
||
1975 |
/** |
|
19 | 1976 |
* Unlinks the object from the taxonomy or taxonomies. |
0 | 1977 |
* |
1978 |
* Will remove all relationships between the object and any terms in |
|
1979 |
* a particular taxonomy or taxonomies. Does not remove the term or |
|
1980 |
* taxonomy itself. |
|
1981 |
* |
|
1982 |
* @since 2.3.0 |
|
1983 |
* |
|
19 | 1984 |
* @param int $object_id The term object ID that refers to the term. |
1985 |
* @param string|array $taxonomies List of taxonomy names or single taxonomy name. |
|
0 | 1986 |
*/ |
1987 |
function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|
1988 |
$object_id = (int) $object_id; |
|
1989 |
||
9 | 1990 |
if ( ! is_array( $taxonomies ) ) { |
1991 |
$taxonomies = array( $taxonomies ); |
|
1992 |
} |
|
0 | 1993 |
|
1994 |
foreach ( (array) $taxonomies as $taxonomy ) { |
|
1995 |
$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) ); |
|
1996 |
$term_ids = array_map( 'intval', $term_ids ); |
|
1997 |
wp_remove_object_terms( $object_id, $term_ids, $taxonomy ); |
|
1998 |
} |
|
1999 |
} |
|
2000 |
||
2001 |
/** |
|
2002 |
* Removes a term from the database. |
|
2003 |
* |
|
2004 |
* If the term is a parent of other terms, then the children will be updated to |
|
2005 |
* that term's parent. |
|
2006 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2007 |
* Metadata associated with the term will be deleted. |
5 | 2008 |
* |
0 | 2009 |
* @since 2.3.0 |
2010 |
* |
|
5 | 2011 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 2012 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2013 |
* @param int $term Term ID. |
19 | 2014 |
* @param string $taxonomy Taxonomy name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2015 |
* @param array|string $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
* 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
|
2017 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2018 |
* @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
|
2019 |
* 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
|
2020 |
* the found terms are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2021 |
* @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
|
2022 |
* 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
|
2023 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2024 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
* @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
|
2026 |
* deletion of default Category. WP_Error if the taxonomy does not exist. |
0 | 2027 |
*/ |
2028 |
function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|
2029 |
global $wpdb; |
|
2030 |
||
2031 |
$term = (int) $term; |
|
2032 |
||
16 | 2033 |
$ids = term_exists( $term, $taxonomy ); |
2034 |
if ( ! $ids ) { |
|
0 | 2035 |
return false; |
9 | 2036 |
} |
2037 |
if ( is_wp_error( $ids ) ) { |
|
0 | 2038 |
return $ids; |
9 | 2039 |
} |
0 | 2040 |
|
2041 |
$tt_id = $ids['term_taxonomy_id']; |
|
2042 |
||
2043 |
$defaults = array(); |
|
2044 |
||
16 | 2045 |
if ( 'category' === $taxonomy ) { |
2046 |
$defaults['default'] = (int) get_option( 'default_category' ); |
|
2047 |
if ( $defaults['default'] === $term ) { |
|
2048 |
return 0; // Don't delete the default category. |
|
2049 |
} |
|
2050 |
} |
|
2051 |
||
2052 |
// Don't delete the default custom taxonomy term. |
|
2053 |
$taxonomy_object = get_taxonomy( $taxonomy ); |
|
2054 |
if ( ! empty( $taxonomy_object->default_term ) ) { |
|
2055 |
$defaults['default'] = (int) get_option( 'default_term_' . $taxonomy ); |
|
2056 |
if ( $defaults['default'] === $term ) { |
|
2057 |
return 0; |
|
9 | 2058 |
} |
0 | 2059 |
} |
2060 |
||
9 | 2061 |
$args = wp_parse_args( $args, $defaults ); |
5 | 2062 |
|
2063 |
if ( isset( $args['default'] ) ) { |
|
2064 |
$default = (int) $args['default']; |
|
2065 |
if ( ! term_exists( $default, $taxonomy ) ) { |
|
2066 |
unset( $default ); |
|
2067 |
} |
|
2068 |
} |
|
2069 |
||
2070 |
if ( isset( $args['force_default'] ) ) { |
|
2071 |
$force_default = $args['force_default']; |
|
0 | 2072 |
} |
2073 |
||
5 | 2074 |
/** |
2075 |
* Fires when deleting a term, before any modifications are made to posts or terms. |
|
2076 |
* |
|
2077 |
* @since 4.1.0 |
|
2078 |
* |
|
2079 |
* @param int $term Term ID. |
|
19 | 2080 |
* @param string $taxonomy Taxonomy name. |
5 | 2081 |
*/ |
2082 |
do_action( 'pre_delete_term', $term, $taxonomy ); |
|
2083 |
||
16 | 2084 |
// Update children to point to new parent. |
9 | 2085 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
2086 |
$term_obj = get_term( $term, $taxonomy ); |
|
2087 |
if ( is_wp_error( $term_obj ) ) { |
|
0 | 2088 |
return $term_obj; |
9 | 2089 |
} |
0 | 2090 |
$parent = $term_obj->parent; |
2091 |
||
9 | 2092 |
$edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id ); |
5 | 2093 |
$edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' ); |
2094 |
||
2095 |
/** |
|
2096 |
* Fires immediately before a term to delete's children are reassigned a parent. |
|
2097 |
* |
|
2098 |
* @since 2.9.0 |
|
2099 |
* |
|
2100 |
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term. |
|
2101 |
*/ |
|
0 | 2102 |
do_action( 'edit_term_taxonomies', $edit_tt_ids ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2103 |
|
9 | 2104 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) ); |
5 | 2105 |
|
2106 |
// Clean the cache for all child terms. |
|
2107 |
$edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' ); |
|
2108 |
clean_term_cache( $edit_term_ids, $taxonomy ); |
|
2109 |
||
2110 |
/** |
|
2111 |
* Fires immediately after a term to delete's children are reassigned a parent. |
|
2112 |
* |
|
2113 |
* @since 2.9.0 |
|
2114 |
* |
|
2115 |
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term. |
|
2116 |
*/ |
|
0 | 2117 |
do_action( 'edited_term_taxonomies', $edit_tt_ids ); |
2118 |
} |
|
2119 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2120 |
// 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
|
2121 |
$deleted_term = get_term( $term, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2122 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2123 |
$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
|
2124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2125 |
foreach ( $object_ids as $object_id ) { |
18 | 2126 |
if ( ! isset( $default ) ) { |
2127 |
wp_remove_object_terms( $object_id, $term, $taxonomy ); |
|
2128 |
continue; |
|
2129 |
} |
|
2130 |
||
9 | 2131 |
$terms = wp_get_object_terms( |
2132 |
$object_id, |
|
2133 |
$taxonomy, |
|
2134 |
array( |
|
2135 |
'fields' => 'ids', |
|
2136 |
'orderby' => 'none', |
|
2137 |
) |
|
2138 |
); |
|
18 | 2139 |
|
16 | 2140 |
if ( 1 === count( $terms ) && isset( $default ) ) { |
9 | 2141 |
$terms = array( $default ); |
0 | 2142 |
} else { |
9 | 2143 |
$terms = array_diff( $terms, array( $term ) ); |
2144 |
if ( isset( $default ) && isset( $force_default ) && $force_default ) { |
|
2145 |
$terms = array_merge( $terms, array( $default ) ); |
|
2146 |
} |
|
0 | 2147 |
} |
18 | 2148 |
|
9 | 2149 |
$terms = array_map( 'intval', $terms ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2150 |
wp_set_object_terms( $object_id, $terms, $taxonomy ); |
0 | 2151 |
} |
2152 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2153 |
// Clean the relationship caches for all object types using this term. |
0 | 2154 |
$tax_object = get_taxonomy( $taxonomy ); |
9 | 2155 |
foreach ( $tax_object->object_type as $object_type ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2156 |
clean_object_term_cache( $object_ids, $object_type ); |
9 | 2157 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2158 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2159 |
$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
|
2160 |
foreach ( $term_meta_ids as $mid ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2161 |
delete_metadata_by_mid( 'term', $mid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2162 |
} |
0 | 2163 |
|
5 | 2164 |
/** |
2165 |
* Fires immediately before a term taxonomy ID is deleted. |
|
2166 |
* |
|
2167 |
* @since 2.9.0 |
|
2168 |
* |
|
2169 |
* @param int $tt_id Term taxonomy ID. |
|
2170 |
*/ |
|
0 | 2171 |
do_action( 'delete_term_taxonomy', $tt_id ); |
16 | 2172 |
|
0 | 2173 |
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); |
5 | 2174 |
|
2175 |
/** |
|
2176 |
* Fires immediately after a term taxonomy ID is deleted. |
|
2177 |
* |
|
2178 |
* @since 2.9.0 |
|
2179 |
* |
|
2180 |
* @param int $tt_id Term taxonomy ID. |
|
2181 |
*/ |
|
0 | 2182 |
do_action( 'deleted_term_taxonomy', $tt_id ); |
2183 |
||
2184 |
// Delete the term if no taxonomies use it. |
|
9 | 2185 |
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) { |
0 | 2186 |
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); |
9 | 2187 |
} |
2188 |
||
2189 |
clean_term_cache( $term, $taxonomy ); |
|
0 | 2190 |
|
5 | 2191 |
/** |
2192 |
* Fires after a term is deleted from the database and the cache is cleaned. |
|
2193 |
* |
|
18 | 2194 |
* The {@see 'delete_$taxonomy'} hook is also available for targeting a specific |
2195 |
* taxonomy. |
|
2196 |
* |
|
5 | 2197 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2198 |
* @since 4.5.0 Introduced the `$object_ids` argument. |
5 | 2199 |
* |
2200 |
* @param int $term Term ID. |
|
2201 |
* @param int $tt_id Term taxonomy ID. |
|
2202 |
* @param string $taxonomy Taxonomy slug. |
|
18 | 2203 |
* @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
|
2204 |
* @param array $object_ids List of term object IDs. |
5 | 2205 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2206 |
do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids ); |
5 | 2207 |
|
2208 |
/** |
|
2209 |
* Fires after a term in a specific taxonomy is deleted. |
|
2210 |
* |
|
2211 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the specific |
|
2212 |
* taxonomy the term belonged to. |
|
2213 |
* |
|
18 | 2214 |
* Possible hook names include: |
2215 |
* |
|
2216 |
* - `delete_category` |
|
2217 |
* - `delete_post_tag` |
|
2218 |
* |
|
5 | 2219 |
* @since 2.3.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2220 |
* @since 4.5.0 Introduced the `$object_ids` argument. |
5 | 2221 |
* |
2222 |
* @param int $term Term ID. |
|
2223 |
* @param int $tt_id Term taxonomy ID. |
|
18 | 2224 |
* @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
|
2225 |
* @param array $object_ids List of term object IDs. |
5 | 2226 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2227 |
do_action( "delete_{$taxonomy}", $term, $tt_id, $deleted_term, $object_ids ); |
0 | 2228 |
|
2229 |
return true; |
|
2230 |
} |
|
2231 |
||
2232 |
/** |
|
2233 |
* Deletes one existing category. |
|
2234 |
* |
|
2235 |
* @since 2.0.0 |
|
2236 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2237 |
* @param int $cat_id Category term ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2238 |
* @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
|
2239 |
* 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
|
2240 |
* also a possibility. |
0 | 2241 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2242 |
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
|
2243 |
return wp_delete_term( $cat_id, 'category' ); |
0 | 2244 |
} |
2245 |
||
2246 |
/** |
|
2247 |
* Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
|
2248 |
* |
|
2249 |
* @since 2.3.0 |
|
5 | 2250 |
* @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`. |
2251 |
* Introduced `$parent` argument. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2252 |
* @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
|
2253 |
* '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
|
2254 |
* @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
|
2255 |
* @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
|
2256 |
* prime the term meta cache. |
5 | 2257 |
* |
16 | 2258 |
* @param int|int[] $object_ids The ID(s) of the object(s) to retrieve. |
2259 |
* @param string|string[] $taxonomies The taxonomy names to retrieve terms from. |
|
2260 |
* @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
|
2261 |
* @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
|
2262 |
* 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
|
2263 |
* See WP_Term_Query::get_terms() for more information. |
0 | 2264 |
*/ |
9 | 2265 |
function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { |
2266 |
if ( empty( $object_ids ) || empty( $taxonomies ) ) { |
|
0 | 2267 |
return array(); |
9 | 2268 |
} |
2269 |
||
2270 |
if ( ! is_array( $taxonomies ) ) { |
|
2271 |
$taxonomies = array( $taxonomies ); |
|
2272 |
} |
|
0 | 2273 |
|
5 | 2274 |
foreach ( $taxonomies as $taxonomy ) { |
9 | 2275 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2276 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
9 | 2277 |
} |
0 | 2278 |
} |
2279 |
||
9 | 2280 |
if ( ! is_array( $object_ids ) ) { |
2281 |
$object_ids = array( $object_ids ); |
|
2282 |
} |
|
2283 |
$object_ids = array_map( 'intval', $object_ids ); |
|
0 | 2284 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2285 |
$defaults = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2286 |
'update_term_meta_cache' => false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2287 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2288 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2289 |
$args = wp_parse_args( $args, $defaults ); |
7
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 |
/** |
18 | 2292 |
* Filters arguments for retrieving object terms. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2294 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2295 |
* |
16 | 2296 |
* @param array $args An array of arguments for retrieving terms for the given object(s). |
2297 |
* See {@see wp_get_object_terms()} for details. |
|
2298 |
* @param int[] $object_ids Array of object IDs. |
|
2299 |
* @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
|
2300 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2301 |
$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
|
2302 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2303 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2304 |
* 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
|
2305 |
* those params override the `$args` passed to this function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2306 |
*/ |
0 | 2307 |
$terms = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
if ( count( $taxonomies ) > 1 ) { |
0 | 2309 |
foreach ( $taxonomies as $index => $taxonomy ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2310 |
$t = get_taxonomy( $taxonomy ); |
16 | 2311 |
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
|
2312 |
unset( $taxonomies[ $index ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2313 |
$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) ); |
0 | 2314 |
} |
2315 |
} |
|
2316 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2317 |
$t = get_taxonomy( $taxonomies[0] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2318 |
if ( isset( $t->args ) && is_array( $t->args ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
$args = array_merge( $args, $t->args ); |
5 | 2320 |
} |
0 | 2321 |
} |
2322 |
||
9 | 2323 |
$args['taxonomy'] = $taxonomies; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
$args['object_ids'] = $object_ids; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
// Taxonomies registered without an 'args' param are handled here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
if ( ! empty( $taxonomies ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2328 |
$terms_from_remaining_taxonomies = get_terms( $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2329 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2330 |
// 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
|
2331 |
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
|
2332 |
$terms = $terms + $terms_from_remaining_taxonomies; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2333 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2334 |
$terms = array_merge( $terms, $terms_from_remaining_taxonomies ); |
5 | 2335 |
} |
2336 |
} |
|
2337 |
||
2338 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2339 |
* Filters the terms for a given object or objects. |
5 | 2340 |
* |
2341 |
* @since 4.2.0 |
|
2342 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2343 |
* @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
|
2344 |
* @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
|
2345 |
* @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
|
2346 |
* @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
|
2347 |
* object(s). See wp_get_object_terms() for details. |
5 | 2348 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2349 |
$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
|
2350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2351 |
$object_ids = implode( ',', $object_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2352 |
$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; |
5 | 2353 |
|
2354 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2355 |
* Filters the terms for a given object or objects. |
5 | 2356 |
* |
2357 |
* The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The |
|
2358 |
* {@see 'get_object_terms'} filter is recommended as an alternative. |
|
2359 |
* |
|
2360 |
* @since 2.8.0 |
|
2361 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2362 |
* @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
|
2363 |
* @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
|
2364 |
* @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
|
2365 |
* @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
|
2366 |
* object(s). See wp_get_object_terms() for details. |
5 | 2367 |
*/ |
2368 |
return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); |
|
0 | 2369 |
} |
2370 |
||
2371 |
/** |
|
19 | 2372 |
* Adds a new term to the database. |
0 | 2373 |
* |
2374 |
* A non-existent term is inserted in the following sequence: |
|
2375 |
* 1. The term is added to the term table, then related to the taxonomy. |
|
2376 |
* 2. If everything is correct, several actions are fired. |
|
2377 |
* 3. The 'term_id_filter' is evaluated. |
|
2378 |
* 4. The term cache is cleaned. |
|
2379 |
* 5. Several more actions are fired. |
|
16 | 2380 |
* 6. An array is returned containing the `term_id` and `term_taxonomy_id`. |
0 | 2381 |
* |
2382 |
* If the 'slug' argument is not empty, then it is checked to see if the term |
|
2383 |
* is invalid. If it is not a valid, existing term, it is added and the term_id |
|
2384 |
* is given. |
|
2385 |
* |
|
2386 |
* If the taxonomy is hierarchical, and the 'parent' argument is not empty, |
|
2387 |
* 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
|
2388 |
* |
0 | 2389 |
* Error handling: |
16 | 2390 |
* If `$taxonomy` does not exist or `$term` is empty, |
0 | 2391 |
* a WP_Error object will be returned. |
2392 |
* |
|
2393 |
* If the term already exists on the same hierarchical level, |
|
2394 |
* or the term slug and name are not unique, a WP_Error object will be returned. |
|
2395 |
* |
|
5 | 2396 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2397 |
* |
0 | 2398 |
* @since 2.3.0 |
2399 |
* |
|
16 | 2400 |
* @param string $term The term name to add. |
5 | 2401 |
* @param string $taxonomy The taxonomy to which to add the term. |
0 | 2402 |
* @param array|string $args { |
18 | 2403 |
* Optional. Array or query string of arguments for inserting a term. |
5 | 2404 |
* |
2405 |
* @type string $alias_of Slug of the term to make this term an alias of. |
|
2406 |
* Default empty string. Accepts a term slug. |
|
2407 |
* @type string $description The term description. Default empty string. |
|
2408 |
* @type int $parent The id of the parent term. Default 0. |
|
2409 |
* @type string $slug The term slug to use. Default empty string. |
|
0 | 2410 |
* } |
18 | 2411 |
* @return array|WP_Error { |
2412 |
* An array of the new term data, WP_Error otherwise. |
|
2413 |
* |
|
2414 |
* @type int $term_id The new term ID. |
|
2415 |
* @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string. |
|
2416 |
* } |
|
0 | 2417 |
*/ |
2418 |
function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|
2419 |
global $wpdb; |
|
2420 |
||
9 | 2421 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2422 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
5 | 2423 |
} |
16 | 2424 |
|
5 | 2425 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2426 |
* Filters a term before it is sanitized and inserted into the database. |
5 | 2427 |
* |
2428 |
* @since 3.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2429 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 2430 |
* |
16 | 2431 |
* @param string|WP_Error $term The term name to add, or a WP_Error object if there's an error. |
2432 |
* @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
|
2433 |
* @param array|string $args Array or query string of arguments passed to wp_insert_term(). |
5 | 2434 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2435 |
$term = apply_filters( 'pre_insert_term', $term, $taxonomy, $args ); |
16 | 2436 |
|
5 | 2437 |
if ( is_wp_error( $term ) ) { |
2438 |
return $term; |
|
2439 |
} |
|
16 | 2440 |
|
2441 |
if ( is_int( $term ) && 0 === $term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2442 |
return new WP_Error( 'invalid_term_id', __( 'Invalid term ID.' ) ); |
5 | 2443 |
} |
16 | 2444 |
|
2445 |
if ( '' === trim( $term ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2446 |
return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) ); |
5 | 2447 |
} |
16 | 2448 |
|
9 | 2449 |
$defaults = array( |
2450 |
'alias_of' => '', |
|
2451 |
'description' => '', |
|
2452 |
'parent' => 0, |
|
2453 |
'slug' => '', |
|
2454 |
); |
|
2455 |
$args = wp_parse_args( $args, $defaults ); |
|
5 | 2456 |
|
18 | 2457 |
if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { |
5 | 2458 |
return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); |
2459 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2460 |
|
9 | 2461 |
$args['name'] = $term; |
0 | 2462 |
$args['taxonomy'] = $taxonomy; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2463 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2464 |
// Coerce null description to strings, to avoid database errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2465 |
$args['description'] = (string) $args['description']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2466 |
|
9 | 2467 |
$args = sanitize_term( $args, $taxonomy, 'db' ); |
0 | 2468 |
|
2469 |
// expected_slashed ($name) |
|
9 | 2470 |
$name = wp_unslash( $args['name'] ); |
5 | 2471 |
$description = wp_unslash( $args['description'] ); |
9 | 2472 |
$parent = (int) $args['parent']; |
5 | 2473 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2474 |
// 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
|
2475 |
if ( '' === $name ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2476 |
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
|
2477 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2478 |
|
5 | 2479 |
$slug_provided = ! empty( $args['slug'] ); |
2480 |
if ( ! $slug_provided ) { |
|
2481 |
$slug = sanitize_title( $name ); |
|
2482 |
} else { |
|
2483 |
$slug = $args['slug']; |
|
2484 |
} |
|
0 | 2485 |
|
2486 |
$term_group = 0; |
|
5 | 2487 |
if ( $args['alias_of'] ) { |
2488 |
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); |
|
2489 |
if ( ! empty( $alias->term_group ) ) { |
|
0 | 2490 |
// The alias we want is already in a group, so let's use that one. |
2491 |
$term_group = $alias->term_group; |
|
5 | 2492 |
} elseif ( ! empty( $alias->term_id ) ) { |
2493 |
/* |
|
2494 |
* The alias is not in a group, so we create a new one |
|
2495 |
* and add the alias to it. |
|
2496 |
*/ |
|
9 | 2497 |
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; |
2498 |
||
2499 |
wp_update_term( |
|
2500 |
$alias->term_id, |
|
2501 |
$taxonomy, |
|
2502 |
array( |
|
2503 |
'term_group' => $term_group, |
|
2504 |
) |
|
2505 |
); |
|
0 | 2506 |
} |
2507 |
} |
|
2508 |
||
5 | 2509 |
/* |
2510 |
* Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy, |
|
2511 |
* unless a unique slug has been explicitly provided. |
|
2512 |
*/ |
|
9 | 2513 |
$name_matches = get_terms( |
2514 |
array( |
|
16 | 2515 |
'taxonomy' => $taxonomy, |
9 | 2516 |
'name' => $name, |
2517 |
'hide_empty' => false, |
|
2518 |
'parent' => $args['parent'], |
|
2519 |
'update_term_meta_cache' => false, |
|
2520 |
) |
|
2521 |
); |
|
7
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 |
* 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
|
2525 |
* so we do a stricter comparison here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2526 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2527 |
$name_match = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2528 |
if ( $name_matches ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2529 |
foreach ( $name_matches as $_match ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2530 |
if ( strtolower( $name ) === strtolower( $_match->name ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2531 |
$name_match = $_match; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2532 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2533 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2534 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2535 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2536 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2537 |
if ( $name_match ) { |
5 | 2538 |
$slug_match = get_term_by( 'slug', $slug, $taxonomy ); |
2539 |
if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { |
|
2540 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|
9 | 2541 |
$siblings = get_terms( |
2542 |
array( |
|
16 | 2543 |
'taxonomy' => $taxonomy, |
9 | 2544 |
'get' => 'all', |
2545 |
'parent' => $parent, |
|
2546 |
'update_term_meta_cache' => false, |
|
2547 |
) |
|
2548 |
); |
|
5 | 2549 |
|
2550 |
$existing_term = null; |
|
16 | 2551 |
$sibling_names = wp_list_pluck( $siblings, 'name' ); |
2552 |
$sibling_slugs = wp_list_pluck( $siblings, 'slug' ); |
|
2553 |
||
2554 |
if ( ( ! $slug_provided || $name_match->slug === $slug ) && in_array( $name, $sibling_names, true ) ) { |
|
5 | 2555 |
$existing_term = $name_match; |
16 | 2556 |
} elseif ( $slug_match && in_array( $slug, $sibling_slugs, true ) ) { |
5 | 2557 |
$existing_term = $slug_match; |
2558 |
} |
|
2559 |
||
2560 |
if ( $existing_term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2561 |
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $existing_term->term_id ); |
5 | 2562 |
} |
0 | 2563 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2564 |
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists in this taxonomy.' ), $name_match->term_id ); |
0 | 2565 |
} |
2566 |
} |
|
2567 |
} |
|
2568 |
||
5 | 2569 |
$slug = wp_unique_term_slug( $slug, (object) $args ); |
2570 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2571 |
$data = compact( 'name', 'slug', 'term_group' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2572 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2573 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2574 |
* 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
|
2575 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2576 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2577 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2578 |
* @param array $data Term data to be inserted. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2579 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2580 |
* @param array $args Arguments passed to wp_insert_term(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2581 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2582 |
$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
|
2583 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2584 |
if ( false === $wpdb->insert( $wpdb->terms, $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2585 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database.' ), $wpdb->last_error ); |
5 | 2586 |
} |
2587 |
||
2588 |
$term_id = (int) $wpdb->insert_id; |
|
2589 |
||
16 | 2590 |
// Seems unreachable. However, is used in the case that a term name is provided, which sanitizes to an empty string. |
9 | 2591 |
if ( empty( $slug ) ) { |
2592 |
$slug = sanitize_title( $slug, $term_id ); |
|
5 | 2593 |
|
2594 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
0 | 2595 |
do_action( 'edit_terms', $term_id, $taxonomy ); |
2596 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
|
5 | 2597 |
|
2598 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
0 | 2599 |
do_action( 'edited_terms', $term_id, $taxonomy ); |
2600 |
} |
|
2601 |
||
2602 |
$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 ) ); |
|
2603 |
||
9 | 2604 |
if ( ! empty( $tt_id ) ) { |
2605 |
return array( |
|
2606 |
'term_id' => $term_id, |
|
2607 |
'term_taxonomy_id' => $tt_id, |
|
2608 |
); |
|
5 | 2609 |
} |
16 | 2610 |
|
2611 |
if ( false === $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ) ) { |
|
2612 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term taxonomy into the database.' ), $wpdb->last_error ); |
|
2613 |
} |
|
2614 |
||
0 | 2615 |
$tt_id = (int) $wpdb->insert_id; |
2616 |
||
5 | 2617 |
/* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2618 |
* Confidence check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than |
5 | 2619 |
* an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id |
2620 |
* and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks |
|
2621 |
* are not fired. |
|
2622 |
*/ |
|
19 | 2623 |
$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 | 2624 |
|
2625 |
/** |
|
2626 |
* Filters the duplicate term check that takes place during term creation. |
|
2627 |
* |
|
19 | 2628 |
* Term parent + taxonomy + slug combinations are meant to be unique, and wp_insert_term() |
9 | 2629 |
* performs a last-minute confirmation of this uniqueness before allowing a new term |
2630 |
* to be created. Plugins with different uniqueness requirements may use this filter |
|
2631 |
* to bypass or modify the duplicate-term check. |
|
2632 |
* |
|
2633 |
* @since 5.1.0 |
|
2634 |
* |
|
2635 |
* @param object $duplicate_term Duplicate term row from terms table, if found. |
|
2636 |
* @param string $term Term being inserted. |
|
2637 |
* @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
|
2638 |
* @param array $args Arguments passed to wp_insert_term(). |
9 | 2639 |
* @param int $tt_id term_taxonomy_id for the newly created term. |
2640 |
*/ |
|
2641 |
$duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id ); |
|
2642 |
||
5 | 2643 |
if ( $duplicate_term ) { |
2644 |
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) ); |
|
2645 |
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); |
|
2646 |
||
2647 |
$term_id = (int) $duplicate_term->term_id; |
|
2648 |
$tt_id = (int) $duplicate_term->term_taxonomy_id; |
|
2649 |
||
2650 |
clean_term_cache( $term_id, $taxonomy ); |
|
9 | 2651 |
return array( |
2652 |
'term_id' => $term_id, |
|
2653 |
'term_taxonomy_id' => $tt_id, |
|
2654 |
); |
|
5 | 2655 |
} |
2656 |
||
2657 |
/** |
|
2658 |
* Fires immediately after a new term is created, before the term cache is cleaned. |
|
2659 |
* |
|
18 | 2660 |
* The {@see 'create_$taxonomy'} hook is also available for targeting a specific |
2661 |
* taxonomy. |
|
2662 |
* |
|
5 | 2663 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2664 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 2665 |
* |
2666 |
* @param int $term_id Term ID. |
|
2667 |
* @param int $tt_id Term taxonomy ID. |
|
2668 |
* @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
|
2669 |
* @param array $args Arguments passed to wp_insert_term(). |
5 | 2670 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2671 |
do_action( 'create_term', $term_id, $tt_id, $taxonomy, $args ); |
5 | 2672 |
|
2673 |
/** |
|
2674 |
* Fires after a new term is created for a specific taxonomy. |
|
2675 |
* |
|
2676 |
* The dynamic portion of the hook name, `$taxonomy`, refers |
|
2677 |
* to the slug of the taxonomy the term was created for. |
|
2678 |
* |
|
18 | 2679 |
* Possible hook names include: |
2680 |
* |
|
2681 |
* - `create_category` |
|
2682 |
* - `create_post_tag` |
|
2683 |
* |
|
5 | 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 |
do_action( "create_{$taxonomy}", $term_id, $tt_id, $args ); |
5 | 2692 |
|
2693 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2694 |
* Filters the term ID after a new term is created. |
5 | 2695 |
* |
2696 |
* @since 2.3.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2697 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 2698 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2699 |
* @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
|
2700 |
* @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
|
2701 |
* @param array $args Arguments passed to wp_insert_term(). |
5 | 2702 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2703 |
$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id, $args ); |
0 | 2704 |
|
9 | 2705 |
clean_term_cache( $term_id, $taxonomy ); |
0 | 2706 |
|
5 | 2707 |
/** |
2708 |
* Fires after a new term is created, and after the term cache has been cleaned. |
|
2709 |
* |
|
18 | 2710 |
* The {@see 'created_$taxonomy'} hook is also available for targeting a specific |
2711 |
* taxonomy. |
|
2712 |
* |
|
5 | 2713 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2714 |
* @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
|
2715 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2716 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2717 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2718 |
* @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
|
2719 |
* @param array $args Arguments passed to wp_insert_term(). |
5 | 2720 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2721 |
do_action( 'created_term', $term_id, $tt_id, $taxonomy, $args ); |
5 | 2722 |
|
2723 |
/** |
|
2724 |
* Fires after a new term in a specific taxonomy is created, and after the term |
|
2725 |
* cache has been cleaned. |
|
2726 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2727 |
* 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
|
2728 |
* |
18 | 2729 |
* Possible hook names include: |
2730 |
* |
|
2731 |
* - `created_category` |
|
2732 |
* - `created_post_tag` |
|
2733 |
* |
|
5 | 2734 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2735 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 2736 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2737 |
* @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
|
2738 |
* @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
|
2739 |
* @param array $args Arguments passed to wp_insert_term(). |
5 | 2740 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2741 |
do_action( "created_{$taxonomy}", $term_id, $tt_id, $args ); |
0 | 2742 |
|
16 | 2743 |
/** |
2744 |
* Fires after a term has been saved, and the term cache has been cleared. |
|
2745 |
* |
|
18 | 2746 |
* The {@see 'saved_$taxonomy'} hook is also available for targeting a specific |
2747 |
* taxonomy. |
|
2748 |
* |
|
16 | 2749 |
* @since 5.5.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2750 |
* @since 6.1.0 The `$args` parameter was added. |
16 | 2751 |
* |
2752 |
* @param int $term_id Term ID. |
|
2753 |
* @param int $tt_id Term taxonomy ID. |
|
2754 |
* @param string $taxonomy Taxonomy slug. |
|
2755 |
* @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
|
2756 |
* @param array $args Arguments passed to wp_insert_term(). |
16 | 2757 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2758 |
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false, $args ); |
16 | 2759 |
|
2760 |
/** |
|
2761 |
* Fires after a term in a specific taxonomy has been saved, and the term |
|
2762 |
* cache has been cleared. |
|
2763 |
* |
|
2764 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
2765 |
* |
|
18 | 2766 |
* Possible hook names include: |
2767 |
* |
|
2768 |
* - `saved_category` |
|
2769 |
* - `saved_post_tag` |
|
2770 |
* |
|
16 | 2771 |
* @since 5.5.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2772 |
* @since 6.1.0 The `$args` parameter was added. |
16 | 2773 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2774 |
* @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
|
2775 |
* @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
|
2776 |
* @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
|
2777 |
* @param array $args Arguments passed to wp_insert_term(). |
16 | 2778 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2779 |
do_action( "saved_{$taxonomy}", $term_id, $tt_id, false, $args ); |
16 | 2780 |
|
9 | 2781 |
return array( |
2782 |
'term_id' => $term_id, |
|
2783 |
'term_taxonomy_id' => $tt_id, |
|
2784 |
); |
|
0 | 2785 |
} |
2786 |
||
2787 |
/** |
|
19 | 2788 |
* Creates term and taxonomy relationships. |
2789 |
* |
|
2790 |
* Relates an object (post, link, etc.) to a term and taxonomy type. Creates the |
|
0 | 2791 |
* term and taxonomy relationship if it doesn't already exist. Creates a term if |
2792 |
* it doesn't exist (using the slug). |
|
2793 |
* |
|
2794 |
* A relationship means that the term is grouped in or belongs to the taxonomy. |
|
2795 |
* A term has no meaning until it is given context by defining which taxonomy it |
|
2796 |
* exists under. |
|
2797 |
* |
|
2798 |
* @since 2.3.0 |
|
5 | 2799 |
* |
16 | 2800 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2801 |
* |
5 | 2802 |
* @param int $object_id The object to relate to. |
16 | 2803 |
* @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
|
2804 |
* 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
|
2805 |
* empty array will remove all related terms. |
5 | 2806 |
* @param string $taxonomy The context in which to relate the term to the object. |
2807 |
* @param bool $append Optional. If false will delete difference of terms. Default false. |
|
16 | 2808 |
* @return array|WP_Error Term taxonomy IDs of the affected terms or WP_Error on failure. |
0 | 2809 |
*/ |
5 | 2810 |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
0 | 2811 |
global $wpdb; |
2812 |
||
2813 |
$object_id = (int) $object_id; |
|
2814 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2815 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2816 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2817 |
} |
0 | 2818 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2819 |
if ( empty( $terms ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2820 |
$terms = array(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2821 |
} elseif ( ! is_array( $terms ) ) { |
9 | 2822 |
$terms = array( $terms ); |
2823 |
} |
|
2824 |
||
2825 |
if ( ! $append ) { |
|
2826 |
$old_tt_ids = wp_get_object_terms( |
|
2827 |
$object_id, |
|
2828 |
$taxonomy, |
|
2829 |
array( |
|
2830 |
'fields' => 'tt_ids', |
|
2831 |
'orderby' => 'none', |
|
2832 |
'update_term_meta_cache' => false, |
|
2833 |
) |
|
2834 |
); |
|
2835 |
} else { |
|
0 | 2836 |
$old_tt_ids = array(); |
9 | 2837 |
} |
2838 |
||
2839 |
$tt_ids = array(); |
|
0 | 2840 |
$new_tt_ids = array(); |
2841 |
||
9 | 2842 |
foreach ( (array) $terms as $term ) { |
16 | 2843 |
if ( '' === trim( $term ) ) { |
0 | 2844 |
continue; |
9 | 2845 |
} |
2846 |
||
16 | 2847 |
$term_info = term_exists( $term, $taxonomy ); |
2848 |
||
2849 |
if ( ! $term_info ) { |
|
0 | 2850 |
// Skip if a non-existent term ID is passed. |
9 | 2851 |
if ( is_int( $term ) ) { |
0 | 2852 |
continue; |
9 | 2853 |
} |
16 | 2854 |
|
9 | 2855 |
$term_info = wp_insert_term( $term, $taxonomy ); |
0 | 2856 |
} |
16 | 2857 |
|
9 | 2858 |
if ( is_wp_error( $term_info ) ) { |
0 | 2859 |
return $term_info; |
9 | 2860 |
} |
16 | 2861 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2862 |
$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
|
2863 |
$tt_ids[] = $tt_id; |
9 | 2864 |
|
2865 |
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 | 2866 |
continue; |
9 | 2867 |
} |
5 | 2868 |
|
2869 |
/** |
|
2870 |
* Fires immediately before an object-term relationship is added. |
|
2871 |
* |
|
2872 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2873 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2874 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2875 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2876 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2877 |
* @param string $taxonomy Taxonomy slug. |
5 | 2878 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2879 |
do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy ); |
16 | 2880 |
|
9 | 2881 |
$wpdb->insert( |
2882 |
$wpdb->term_relationships, |
|
2883 |
array( |
|
2884 |
'object_id' => $object_id, |
|
2885 |
'term_taxonomy_id' => $tt_id, |
|
2886 |
) |
|
2887 |
); |
|
5 | 2888 |
|
2889 |
/** |
|
2890 |
* Fires immediately after an object-term relationship is added. |
|
2891 |
* |
|
2892 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2893 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 2894 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2895 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2896 |
* @param int $tt_id Term taxonomy ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2897 |
* @param string $taxonomy Taxonomy slug. |
5 | 2898 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2899 |
do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy ); |
16 | 2900 |
|
0 | 2901 |
$new_tt_ids[] = $tt_id; |
2902 |
} |
|
2903 |
||
9 | 2904 |
if ( $new_tt_ids ) { |
0 | 2905 |
wp_update_term_count( $new_tt_ids, $taxonomy ); |
9 | 2906 |
} |
0 | 2907 |
|
2908 |
if ( ! $append ) { |
|
2909 |
$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids ); |
|
2910 |
||
2911 |
if ( $delete_tt_ids ) { |
|
2912 |
$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'"; |
|
9 | 2913 |
$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 ) ); |
2914 |
$delete_term_ids = array_map( 'intval', $delete_term_ids ); |
|
0 | 2915 |
|
2916 |
$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy ); |
|
2917 |
if ( is_wp_error( $remove ) ) { |
|
2918 |
return $remove; |
|
2919 |
} |
|
2920 |
} |
|
2921 |
} |
|
2922 |
||
9 | 2923 |
$t = get_taxonomy( $taxonomy ); |
16 | 2924 |
|
9 | 2925 |
if ( ! $append && isset( $t->sort ) && $t->sort ) { |
16 | 2926 |
$values = array(); |
2927 |
$term_order = 0; |
|
2928 |
||
9 | 2929 |
$final_tt_ids = wp_get_object_terms( |
2930 |
$object_id, |
|
2931 |
$taxonomy, |
|
2932 |
array( |
|
2933 |
'fields' => 'tt_ids', |
|
2934 |
'update_term_meta_cache' => false, |
|
2935 |
) |
|
2936 |
); |
|
16 | 2937 |
|
9 | 2938 |
foreach ( $tt_ids as $tt_id ) { |
16 | 2939 |
if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) { |
9 | 2940 |
$values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order ); |
2941 |
} |
|
2942 |
} |
|
16 | 2943 |
|
9 | 2944 |
if ( $values ) { |
18 | 2945 |
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
|
2946 |
return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error ); |
9 | 2947 |
} |
2948 |
} |
|
0 | 2949 |
} |
2950 |
||
2951 |
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
|
2952 |
wp_cache_set_terms_last_changed(); |
0 | 2953 |
|
5 | 2954 |
/** |
2955 |
* Fires after an object's terms have been set. |
|
2956 |
* |
|
2957 |
* @since 2.8.0 |
|
2958 |
* |
|
2959 |
* @param int $object_id Object ID. |
|
18 | 2960 |
* @param array $terms An array of object term IDs or slugs. |
5 | 2961 |
* @param array $tt_ids An array of term taxonomy IDs. |
2962 |
* @param string $taxonomy Taxonomy slug. |
|
2963 |
* @param bool $append Whether to append new terms to the old terms. |
|
2964 |
* @param array $old_tt_ids Old array of term taxonomy IDs. |
|
2965 |
*/ |
|
2966 |
do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); |
|
16 | 2967 |
|
0 | 2968 |
return $tt_ids; |
2969 |
} |
|
2970 |
||
2971 |
/** |
|
19 | 2972 |
* Adds term(s) associated with a given object. |
0 | 2973 |
* |
5 | 2974 |
* @since 3.6.0 |
0 | 2975 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2976 |
* @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
|
2977 |
* @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
|
2978 |
* @param array|string $taxonomy Taxonomy name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2979 |
* @return array|WP_Error Term taxonomy IDs of the affected terms. |
0 | 2980 |
*/ |
2981 |
function wp_add_object_terms( $object_id, $terms, $taxonomy ) { |
|
2982 |
return wp_set_object_terms( $object_id, $terms, $taxonomy, true ); |
|
2983 |
} |
|
2984 |
||
2985 |
/** |
|
19 | 2986 |
* Removes term(s) associated with a given object. |
0 | 2987 |
* |
5 | 2988 |
* @since 3.6.0 |
2989 |
* |
|
2990 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 2991 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2992 |
* @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
|
2993 |
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to remove. |
19 | 2994 |
* @param string $taxonomy Taxonomy name. |
0 | 2995 |
* @return bool|WP_Error True on success, false or WP_Error on failure. |
2996 |
*/ |
|
2997 |
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { |
|
2998 |
global $wpdb; |
|
2999 |
||
3000 |
$object_id = (int) $object_id; |
|
3001 |
||
3002 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3003 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
0 | 3004 |
} |
3005 |
||
3006 |
if ( ! is_array( $terms ) ) { |
|
3007 |
$terms = array( $terms ); |
|
3008 |
} |
|
3009 |
||
3010 |
$tt_ids = array(); |
|
3011 |
||
3012 |
foreach ( (array) $terms as $term ) { |
|
16 | 3013 |
if ( '' === trim( $term ) ) { |
0 | 3014 |
continue; |
3015 |
} |
|
3016 |
||
16 | 3017 |
$term_info = term_exists( $term, $taxonomy ); |
3018 |
if ( ! $term_info ) { |
|
0 | 3019 |
// Skip if a non-existent term ID is passed. |
3020 |
if ( is_int( $term ) ) { |
|
3021 |
continue; |
|
3022 |
} |
|
3023 |
} |
|
3024 |
||
3025 |
if ( is_wp_error( $term_info ) ) { |
|
3026 |
return $term_info; |
|
3027 |
} |
|
3028 |
||
3029 |
$tt_ids[] = $term_info['term_taxonomy_id']; |
|
3030 |
} |
|
3031 |
||
3032 |
if ( $tt_ids ) { |
|
3033 |
$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'"; |
|
5 | 3034 |
|
3035 |
/** |
|
3036 |
* Fires immediately before an object-term relationship is deleted. |
|
3037 |
* |
|
3038 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3039 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 3040 |
* |
19 | 3041 |
* @param int $object_id Object ID. |
3042 |
* @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
|
3043 |
* @param string $taxonomy Taxonomy slug. |
5 | 3044 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3045 |
do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy ); |
16 | 3046 |
|
0 | 3047 |
$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 | 3048 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3049 |
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
|
3050 |
wp_cache_set_terms_last_changed(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3051 |
|
5 | 3052 |
/** |
3053 |
* Fires immediately after an object-term relationship is deleted. |
|
3054 |
* |
|
3055 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3056 |
* @since 4.7.0 Added the `$taxonomy` parameter. |
5 | 3057 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3058 |
* @param int $object_id Object ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3059 |
* @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
|
3060 |
* @param string $taxonomy Taxonomy slug. |
5 | 3061 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3062 |
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
|
3063 |
|
0 | 3064 |
wp_update_term_count( $tt_ids, $taxonomy ); |
3065 |
||
3066 |
return (bool) $deleted; |
|
3067 |
} |
|
3068 |
||
3069 |
return false; |
|
3070 |
} |
|
3071 |
||
3072 |
/** |
|
19 | 3073 |
* Makes term slug unique, if it isn't already. |
0 | 3074 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3075 |
* The `$slug` has to be unique global to every taxonomy, meaning that one |
0 | 3076 |
* taxonomy term can't have a matching slug with another taxonomy term. Each |
3077 |
* slug has to be globally unique for every taxonomy. |
|
3078 |
* |
|
3079 |
* The way this works is that if the taxonomy that the term belongs to is |
|
3080 |
* hierarchical and has a parent, it will append that parent to the $slug. |
|
3081 |
* |
|
9 | 3082 |
* If that still doesn't return a unique slug, then it tries to append a number |
0 | 3083 |
* until it finds a number that is truly unique. |
3084 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3085 |
* The only purpose for `$term` is for appending a parent, if one exists. |
0 | 3086 |
* |
3087 |
* @since 2.3.0 |
|
5 | 3088 |
* |
3089 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3090 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3091 |
* @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
|
3092 |
* @param object $term The term object that the `$slug` will belong to. |
0 | 3093 |
* @return string Will return a true unique slug. |
3094 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3095 |
function wp_unique_term_slug( $slug, $term ) { |
0 | 3096 |
global $wpdb; |
3097 |
||
9 | 3098 |
$needs_suffix = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3099 |
$original_slug = $slug; |
0 | 3100 |
|
5 | 3101 |
// 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
|
3102 |
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
|
3103 |
$needs_suffix = false; |
5 | 3104 |
} |
3105 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3106 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3107 |
* 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
|
3108 |
* by incorporating parent slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3109 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3110 |
$parent_suffix = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3111 |
if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) { |
0 | 3112 |
$the_parent = $term->parent; |
9 | 3113 |
while ( ! empty( $the_parent ) ) { |
3114 |
$parent_term = get_term( $the_parent, $term->taxonomy ); |
|
3115 |
if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) { |
|
0 | 3116 |
break; |
9 | 3117 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3118 |
$parent_suffix .= '-' . $parent_term->slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3119 |
if ( ! term_exists( $slug . $parent_suffix ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3120 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3121 |
} |
0 | 3122 |
|
9 | 3123 |
if ( empty( $parent_term->parent ) ) { |
0 | 3124 |
break; |
9 | 3125 |
} |
0 | 3126 |
$the_parent = $parent_term->parent; |
3127 |
} |
|
3128 |
} |
|
3129 |
||
3130 |
// 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
|
3131 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3132 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3133 |
* Filters whether the proposed unique term slug is bad. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3134 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3135 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3137 |
* @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
|
3138 |
* @param string $slug The slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3139 |
* @param object $term Term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3140 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3141 |
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
|
3142 |
if ( $parent_suffix ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3143 |
$slug .= $parent_suffix; |
16 | 3144 |
} |
3145 |
||
3146 |
if ( ! empty( $term->term_id ) ) { |
|
3147 |
$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
|
3148 |
} else { |
16 | 3149 |
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug ); |
3150 |
} |
|
3151 |
||
3152 |
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
3153 |
$num = 2; |
|
3154 |
do { |
|
3155 |
$alt_slug = $slug . "-$num"; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3156 |
++$num; |
16 | 3157 |
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) ); |
3158 |
} while ( $slug_check ); |
|
3159 |
$slug = $alt_slug; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3160 |
} |
0 | 3161 |
} |
3162 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3163 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3164 |
* Filters the unique term slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3165 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3166 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3167 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3168 |
* @param string $slug Unique term slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3169 |
* @param object $term Term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3170 |
* @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
|
3171 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3172 |
return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug ); |
0 | 3173 |
} |
3174 |
||
3175 |
/** |
|
19 | 3176 |
* Updates term based on arguments provided. |
0 | 3177 |
* |
16 | 3178 |
* The `$args` will indiscriminately override all values with the same field name. |
0 | 3179 |
* Care must be taken to not override important information need to update or |
3180 |
* update will fail (or perhaps create a new term, neither would be acceptable). |
|
3181 |
* |
|
3182 |
* Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not |
|
16 | 3183 |
* defined in `$args` already. |
3184 |
* |
|
3185 |
* 'alias_of' will create a term group, if it doesn't already exist, and |
|
3186 |
* update it for the `$term`. |
|
3187 |
* |
|
3188 |
* If the 'slug' argument in `$args` is missing, then the 'name' will be used. |
|
3189 |
* If you set 'slug' and it isn't unique, then a WP_Error is returned. |
|
3190 |
* If you don't pass any slug, then a unique one will be created. |
|
0 | 3191 |
* |
3192 |
* @since 2.3.0 |
|
3193 |
* |
|
5 | 3194 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 3195 |
* |
16 | 3196 |
* @param int $term_id The ID of the term. |
3197 |
* @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
|
3198 |
* @param array $args { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3199 |
* Optional. Array of arguments for updating a term. |
16 | 3200 |
* |
3201 |
* @type string $alias_of Slug of the term to make this term an alias of. |
|
3202 |
* Default empty string. Accepts a term slug. |
|
3203 |
* @type string $description The term description. Default empty string. |
|
3204 |
* @type int $parent The id of the parent term. Default 0. |
|
3205 |
* @type string $slug The term slug to use. Default empty string. |
|
3206 |
* } |
|
3207 |
* @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`, |
|
3208 |
* WP_Error otherwise. |
|
0 | 3209 |
*/ |
3210 |
function wp_update_term( $term_id, $taxonomy, $args = array() ) { |
|
3211 |
global $wpdb; |
|
3212 |
||
5 | 3213 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3214 |
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
5 | 3215 |
} |
0 | 3216 |
|
3217 |
$term_id = (int) $term_id; |
|
3218 |
||
16 | 3219 |
// First, get all of the original args. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3220 |
$term = get_term( $term_id, $taxonomy ); |
5 | 3221 |
|
3222 |
if ( is_wp_error( $term ) ) { |
|
0 | 3223 |
return $term; |
5 | 3224 |
} |
3225 |
||
3226 |
if ( ! $term ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3227 |
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); |
5 | 3228 |
} |
0 | 3229 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3230 |
$term = (array) $term->data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3231 |
|
0 | 3232 |
// Escape data pulled from DB. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3233 |
$term = wp_slash( $term ); |
0 | 3234 |
|
3235 |
// Merge old and new args with new args overwriting old ones. |
|
9 | 3236 |
$args = array_merge( $term, $args ); |
3237 |
||
3238 |
$defaults = array( |
|
3239 |
'alias_of' => '', |
|
3240 |
'description' => '', |
|
3241 |
'parent' => 0, |
|
3242 |
'slug' => '', |
|
3243 |
); |
|
3244 |
$args = wp_parse_args( $args, $defaults ); |
|
3245 |
$args = sanitize_term( $args, $taxonomy, 'db' ); |
|
5 | 3246 |
$parsed_args = $args; |
0 | 3247 |
|
3248 |
// expected_slashed ($name) |
|
9 | 3249 |
$name = wp_unslash( $args['name'] ); |
5 | 3250 |
$description = wp_unslash( $args['description'] ); |
3251 |
||
9 | 3252 |
$parsed_args['name'] = $name; |
5 | 3253 |
$parsed_args['description'] = $description; |
0 | 3254 |
|
16 | 3255 |
if ( '' === trim( $name ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3256 |
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
|
3257 |
} |
0 | 3258 |
|
18 | 3259 |
if ( (int) $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) { |
5 | 3260 |
return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); |
3261 |
} |
|
3262 |
||
0 | 3263 |
$empty_slug = false; |
5 | 3264 |
if ( empty( $args['slug'] ) ) { |
0 | 3265 |
$empty_slug = true; |
9 | 3266 |
$slug = sanitize_title( $name ); |
5 | 3267 |
} else { |
3268 |
$slug = $args['slug']; |
|
0 | 3269 |
} |
3270 |
||
5 | 3271 |
$parsed_args['slug'] = $slug; |
3272 |
||
3273 |
$term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0; |
|
3274 |
if ( $args['alias_of'] ) { |
|
3275 |
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); |
|
3276 |
if ( ! empty( $alias->term_group ) ) { |
|
0 | 3277 |
// The alias we want is already in a group, so let's use that one. |
3278 |
$term_group = $alias->term_group; |
|
5 | 3279 |
} elseif ( ! empty( $alias->term_id ) ) { |
3280 |
/* |
|
3281 |
* The alias is not in a group, so we create a new one |
|
3282 |
* and add the alias to it. |
|
3283 |
*/ |
|
9 | 3284 |
$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1; |
3285 |
||
3286 |
wp_update_term( |
|
3287 |
$alias->term_id, |
|
3288 |
$taxonomy, |
|
3289 |
array( |
|
3290 |
'term_group' => $term_group, |
|
3291 |
) |
|
3292 |
); |
|
0 | 3293 |
} |
5 | 3294 |
|
3295 |
$parsed_args['term_group'] = $term_group; |
|
0 | 3296 |
} |
3297 |
||
5 | 3298 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3299 |
* Filters the term parent. |
5 | 3300 |
* |
3301 |
* Hook to this filter to see if it will cause a hierarchy loop. |
|
3302 |
* |
|
3303 |
* @since 3.1.0 |
|
3304 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3305 |
* @param int $parent_term ID of the parent term. |
5 | 3306 |
* @param int $term_id Term ID. |
3307 |
* @param string $taxonomy Taxonomy slug. |
|
3308 |
* @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
|
3309 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3310 |
*/ |
16 | 3311 |
$parent = (int) apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args ); |
3312 |
||
3313 |
// Check for duplicate slug. |
|
5 | 3314 |
$duplicate = get_term_by( 'slug', $slug, $taxonomy ); |
16 | 3315 |
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
|
3316 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3317 |
* 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
|
3318 |
* Otherwise, bail. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3319 |
*/ |
16 | 3320 |
if ( $empty_slug || ( $parent !== (int) $term['parent'] ) ) { |
9 | 3321 |
$slug = wp_unique_term_slug( $slug, (object) $args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3322 |
} else { |
16 | 3323 |
/* translators: %s: Taxonomy term slug. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3324 |
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
|
3325 |
} |
0 | 3326 |
} |
5 | 3327 |
|
9 | 3328 |
$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 | 3329 |
|
3330 |
// Check whether this is a shared term that needs splitting. |
|
3331 |
$_term_id = _split_shared_term( $term_id, $tt_id ); |
|
3332 |
if ( ! is_wp_error( $_term_id ) ) { |
|
3333 |
$term_id = $_term_id; |
|
3334 |
} |
|
3335 |
||
3336 |
/** |
|
3337 |
* Fires immediately before the given terms are edited. |
|
3338 |
* |
|
3339 |
* @since 2.9.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3340 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3341 |
* |
3342 |
* @param int $term_id Term ID. |
|
3343 |
* @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
|
3344 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3345 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3346 |
do_action( 'edit_terms', $term_id, $taxonomy, $args ); |
7
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 = compact( 'name', 'slug', 'term_group' ); |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3351 |
* 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
|
3352 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3353 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3354 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3355 |
* @param array $data Term data to be updated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3356 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3357 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3358 |
* @param array $args Arguments passed to wp_update_term(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3359 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3360 |
$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
|
3361 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3362 |
$wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) ); |
16 | 3363 |
|
9 | 3364 |
if ( empty( $slug ) ) { |
3365 |
$slug = sanitize_title( $name, $term_id ); |
|
0 | 3366 |
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); |
3367 |
} |
|
5 | 3368 |
|
3369 |
/** |
|
18 | 3370 |
* Fires immediately after a term is updated in the database, but before its |
3371 |
* term-taxonomy relationship is updated. |
|
5 | 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 |
* |
19 | 3376 |
* @param int $term_id Term ID. |
5 | 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( 'edited_terms', $term_id, $taxonomy, $args ); |
0 | 3381 |
|
5 | 3382 |
/** |
3383 |
* Fires immediate before a term-taxonomy relationship is updated. |
|
3384 |
* |
|
3385 |
* @since 2.9.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3386 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3387 |
* |
3388 |
* @param int $tt_id Term taxonomy ID. |
|
3389 |
* @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
|
3390 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3391 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3392 |
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
|
3393 |
|
0 | 3394 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); |
5 | 3395 |
|
3396 |
/** |
|
3397 |
* Fires immediately after a term-taxonomy relationship is updated. |
|
3398 |
* |
|
3399 |
* @since 2.9.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3400 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3401 |
* |
3402 |
* @param int $tt_id Term taxonomy ID. |
|
3403 |
* @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
|
3404 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3405 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3406 |
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy, $args ); |
0 | 3407 |
|
5 | 3408 |
/** |
3409 |
* Fires after a term has been updated, but before the term cache has been cleaned. |
|
3410 |
* |
|
18 | 3411 |
* The {@see 'edit_$taxonomy'} hook is also available for targeting a specific |
3412 |
* taxonomy. |
|
3413 |
* |
|
5 | 3414 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3415 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3416 |
* |
3417 |
* @param int $term_id Term ID. |
|
3418 |
* @param int $tt_id Term taxonomy ID. |
|
3419 |
* @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
|
3420 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3421 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3422 |
do_action( 'edit_term', $term_id, $tt_id, $taxonomy, $args ); |
5 | 3423 |
|
3424 |
/** |
|
3425 |
* Fires after a term in a specific taxonomy has been updated, but before the term |
|
3426 |
* cache has been cleaned. |
|
3427 |
* |
|
3428 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
3429 |
* |
|
18 | 3430 |
* Possible hook names include: |
3431 |
* |
|
3432 |
* - `edit_category` |
|
3433 |
* - `edit_post_tag` |
|
3434 |
* |
|
5 | 3435 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3436 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3437 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3438 |
* @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
|
3439 |
* @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
|
3440 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3441 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3442 |
do_action( "edit_{$taxonomy}", $term_id, $tt_id, $args ); |
5 | 3443 |
|
3444 |
/** This filter is documented in wp-includes/taxonomy.php */ |
|
3445 |
$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); |
|
0 | 3446 |
|
9 | 3447 |
clean_term_cache( $term_id, $taxonomy ); |
0 | 3448 |
|
5 | 3449 |
/** |
3450 |
* Fires after a term has been updated, and the term cache has been cleaned. |
|
3451 |
* |
|
18 | 3452 |
* The {@see 'edited_$taxonomy'} hook is also available for targeting a specific |
3453 |
* taxonomy. |
|
3454 |
* |
|
5 | 3455 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3456 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3457 |
* |
3458 |
* @param int $term_id Term ID. |
|
3459 |
* @param int $tt_id Term taxonomy ID. |
|
3460 |
* @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
|
3461 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3462 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3463 |
do_action( 'edited_term', $term_id, $tt_id, $taxonomy, $args ); |
5 | 3464 |
|
3465 |
/** |
|
3466 |
* Fires after a term for a specific taxonomy has been updated, and the term |
|
3467 |
* cache has been cleaned. |
|
3468 |
* |
|
3469 |
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. |
|
3470 |
* |
|
18 | 3471 |
* Possible hook names include: |
3472 |
* |
|
3473 |
* - `edited_category` |
|
3474 |
* - `edited_post_tag` |
|
3475 |
* |
|
5 | 3476 |
* @since 2.3.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3477 |
* @since 6.1.0 The `$args` parameter was added. |
5 | 3478 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3479 |
* @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
|
3480 |
* @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
|
3481 |
* @param array $args Arguments passed to wp_update_term(). |
5 | 3482 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3483 |
do_action( "edited_{$taxonomy}", $term_id, $tt_id, $args ); |
0 | 3484 |
|
16 | 3485 |
/** 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
|
3486 |
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, true, $args ); |
16 | 3487 |
|
3488 |
/** 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
|
3489 |
do_action( "saved_{$taxonomy}", $term_id, $tt_id, true, $args ); |
16 | 3490 |
|
9 | 3491 |
return array( |
3492 |
'term_id' => $term_id, |
|
3493 |
'term_taxonomy_id' => $tt_id, |
|
3494 |
); |
|
0 | 3495 |
} |
3496 |
||
3497 |
/** |
|
19 | 3498 |
* Enables or disables term counting. |
0 | 3499 |
* |
3500 |
* @since 2.5.0 |
|
3501 |
* |
|
3502 |
* @param bool $defer Optional. Enable if true, disable if false. |
|
3503 |
* @return bool Whether term counting is enabled or disabled. |
|
3504 |
*/ |
|
9 | 3505 |
function wp_defer_term_counting( $defer = null ) { |
0 | 3506 |
static $_defer = false; |
3507 |
||
9 | 3508 |
if ( is_bool( $defer ) ) { |
0 | 3509 |
$_defer = $defer; |
16 | 3510 |
// Flush any deferred counts. |
9 | 3511 |
if ( ! $defer ) { |
0 | 3512 |
wp_update_term_count( null, null, true ); |
9 | 3513 |
} |
0 | 3514 |
} |
3515 |
||
3516 |
return $_defer; |
|
3517 |
} |
|
3518 |
||
3519 |
/** |
|
3520 |
* Updates the amount of terms in taxonomy. |
|
3521 |
* |
|
3522 |
* If there is a taxonomy callback applied, then it will be called for updating |
|
3523 |
* the count. |
|
3524 |
* |
|
3525 |
* The default action is to count what the amount of terms have the relationship |
|
3526 |
* of term ID. Once that is done, then update the database. |
|
3527 |
* |
|
3528 |
* @since 2.3.0 |
|
5 | 3529 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3530 |
* @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
|
3531 |
* @param string $taxonomy The context of the term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3532 |
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false. |
0 | 3533 |
* @return bool If no terms will return false, and if successful will return true. |
3534 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3535 |
function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) { |
0 | 3536 |
static $_deferred = array(); |
3537 |
||
3538 |
if ( $do_deferred ) { |
|
9 | 3539 |
foreach ( (array) array_keys( $_deferred ) as $tax ) { |
3540 |
wp_update_term_count_now( $_deferred[ $tax ], $tax ); |
|
3541 |
unset( $_deferred[ $tax ] ); |
|
0 | 3542 |
} |
3543 |
} |
|
3544 |
||
9 | 3545 |
if ( empty( $terms ) ) { |
0 | 3546 |
return false; |
9 | 3547 |
} |
3548 |
||
3549 |
if ( ! is_array( $terms ) ) { |
|
3550 |
$terms = array( $terms ); |
|
3551 |
} |
|
0 | 3552 |
|
3553 |
if ( wp_defer_term_counting() ) { |
|
9 | 3554 |
if ( ! isset( $_deferred[ $taxonomy ] ) ) { |
3555 |
$_deferred[ $taxonomy ] = array(); |
|
3556 |
} |
|
3557 |
$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) ); |
|
0 | 3558 |
return true; |
3559 |
} |
|
3560 |
||
3561 |
return wp_update_term_count_now( $terms, $taxonomy ); |
|
3562 |
} |
|
3563 |
||
3564 |
/** |
|
19 | 3565 |
* Performs term count update immediately. |
0 | 3566 |
* |
3567 |
* @since 2.5.0 |
|
3568 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3569 |
* @param array $terms The term_taxonomy_id of terms to update. |
0 | 3570 |
* @param string $taxonomy The context of the term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3571 |
* @return true Always true when complete. |
0 | 3572 |
*/ |
3573 |
function wp_update_term_count_now( $terms, $taxonomy ) { |
|
9 | 3574 |
$terms = array_map( 'intval', $terms ); |
3575 |
||
3576 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
3577 |
if ( ! empty( $taxonomy->update_count_callback ) ) { |
|
3578 |
call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy ); |
|
0 | 3579 |
} else { |
3580 |
$object_types = (array) $taxonomy->object_type; |
|
3581 |
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
|
3582 |
if ( str_starts_with( $object_type, 'attachment:' ) ) { |
0 | 3583 |
list( $object_type ) = explode( ':', $object_type ); |
9 | 3584 |
} |
0 | 3585 |
} |
3586 |
||
16 | 3587 |
if ( array_filter( $object_types, 'post_type_exists' ) == $object_types ) { |
3588 |
// Only post types are attached to this taxonomy. |
|
0 | 3589 |
_update_post_term_count( $terms, $taxonomy ); |
3590 |
} else { |
|
16 | 3591 |
// Default count updater. |
0 | 3592 |
_update_generic_term_count( $terms, $taxonomy ); |
3593 |
} |
|
3594 |
} |
|
3595 |
||
9 | 3596 |
clean_term_cache( $terms, '', false ); |
0 | 3597 |
|
3598 |
return true; |
|
3599 |
} |
|
3600 |
||
3601 |
// |
|
16 | 3602 |
// Cache. |
0 | 3603 |
// |
3604 |
||
3605 |
/** |
|
3606 |
* Removes the taxonomy relationship to terms from the cache. |
|
3607 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3608 |
* 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
|
3609 |
* term IDs have to exist within the taxonomy `$object_type` for the deletion to |
0 | 3610 |
* take place. |
3611 |
* |
|
3612 |
* @since 2.3.0 |
|
3613 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3614 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3615 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3616 |
* @see get_object_taxonomies() for more on $object_type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3617 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3618 |
* @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
|
3619 |
* @param array|string $object_type The taxonomy object type. |
0 | 3620 |
*/ |
9 | 3621 |
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
|
3622 |
global $_wp_suspend_cache_invalidation; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3623 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3624 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3625 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3626 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3627 |
|
9 | 3628 |
if ( ! is_array( $object_ids ) ) { |
3629 |
$object_ids = array( $object_ids ); |
|
3630 |
} |
|
0 | 3631 |
|
3632 |
$taxonomies = get_object_taxonomies( $object_type ); |
|
3633 |
||
19 | 3634 |
foreach ( $taxonomies as $taxonomy ) { |
3635 |
wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" ); |
|
3636 |
} |
|
3637 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3638 |
wp_cache_set_terms_last_changed(); |
5 | 3639 |
|
3640 |
/** |
|
3641 |
* Fires after the object term cache has been cleaned. |
|
3642 |
* |
|
3643 |
* @since 2.5.0 |
|
3644 |
* |
|
3645 |
* @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
|
3646 |
* @param string $object_type Object type. |
5 | 3647 |
*/ |
3648 |
do_action( 'clean_object_term_cache', $object_ids, $object_type ); |
|
0 | 3649 |
} |
3650 |
||
3651 |
/** |
|
19 | 3652 |
* Removes all of the term IDs from the cache. |
0 | 3653 |
* |
3654 |
* @since 2.3.0 |
|
5 | 3655 |
* |
16 | 3656 |
* @global wpdb $wpdb WordPress database abstraction object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3657 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3658 |
* |
16 | 3659 |
* @param int|int[] $ids Single or array of term IDs. |
3660 |
* @param string $taxonomy Optional. Taxonomy slug. Can be empty, in which case the taxonomies of the passed |
|
3661 |
* term IDs will be used. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3662 |
* @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
|
3663 |
* term object caches (false). Default true. |
0 | 3664 |
*/ |
9 | 3665 |
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
|
3666 |
global $wpdb, $_wp_suspend_cache_invalidation; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3667 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3668 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3669 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3670 |
} |
0 | 3671 |
|
9 | 3672 |
if ( ! is_array( $ids ) ) { |
3673 |
$ids = array( $ids ); |
|
3674 |
} |
|
0 | 3675 |
|
3676 |
$taxonomies = array(); |
|
3677 |
// If no taxonomy, assume tt_ids. |
|
9 | 3678 |
if ( empty( $taxonomy ) ) { |
3679 |
$tt_ids = array_map( 'intval', $ids ); |
|
3680 |
$tt_ids = implode( ', ', $tt_ids ); |
|
3681 |
$terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" ); |
|
3682 |
$ids = array(); |
|
16 | 3683 |
|
0 | 3684 |
foreach ( (array) $terms as $term ) { |
3685 |
$taxonomies[] = $term->taxonomy; |
|
9 | 3686 |
$ids[] = $term->term_id; |
0 | 3687 |
} |
19 | 3688 |
wp_cache_delete_multiple( $ids, 'terms' ); |
9 | 3689 |
$taxonomies = array_unique( $taxonomies ); |
0 | 3690 |
} else { |
19 | 3691 |
wp_cache_delete_multiple( $ids, 'terms' ); |
9 | 3692 |
$taxonomies = array( $taxonomy ); |
0 | 3693 |
} |
3694 |
||
3695 |
foreach ( $taxonomies as $taxonomy ) { |
|
3696 |
if ( $clean_taxonomy ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3697 |
clean_taxonomy_cache( $taxonomy ); |
0 | 3698 |
} |
3699 |
||
5 | 3700 |
/** |
3701 |
* Fires once after each taxonomy's term cache has been cleaned. |
|
3702 |
* |
|
3703 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3704 |
* @since 4.5.0 Added the `$clean_taxonomy` parameter. |
5 | 3705 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3706 |
* @param array $ids An array of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3707 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3708 |
* @param bool $clean_taxonomy Whether or not to clean taxonomy-wide caches |
5 | 3709 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3710 |
do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy ); |
0 | 3711 |
} |
3712 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3713 |
wp_cache_set_terms_last_changed(); |
0 | 3714 |
} |
3715 |
||
3716 |
/** |
|
19 | 3717 |
* Cleans the caches for a taxonomy. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3718 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3719 |
* @since 4.9.0 |
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 |
* @param string $taxonomy Taxonomy slug. |
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 |
function clean_taxonomy_cache( $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3724 |
wp_cache_delete( 'all_ids', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3725 |
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
|
3726 |
wp_cache_set_terms_last_changed(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3727 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3728 |
// Regenerate cached hierarchy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3729 |
delete_option( "{$taxonomy}_children" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3730 |
_get_term_hierarchy( $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3731 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3732 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3733 |
* Fires after a taxonomy's caches have been cleaned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3734 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3735 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3736 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3737 |
* @param string $taxonomy Taxonomy slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3738 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3739 |
do_action( 'clean_taxonomy_cache', $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3740 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3741 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3742 |
/** |
16 | 3743 |
* Retrieves the cached term objects for the given object ID. |
0 | 3744 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3745 |
* 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
|
3746 |
* 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
|
3747 |
* 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
|
3748 |
* |
0 | 3749 |
* @since 2.3.0 |
16 | 3750 |
* @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
|
3751 |
* any of the matched terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3752 |
* |
16 | 3753 |
* @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
|
3754 |
* @param string $taxonomy Taxonomy name. |
16 | 3755 |
* @return bool|WP_Term[]|WP_Error Array of `WP_Term` objects, if cached. |
3756 |
* False if cache is empty for `$taxonomy` and `$id`. |
|
3757 |
* WP_Error if get_term() returns an error object for any term. |
|
0 | 3758 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3759 |
function get_object_term_cache( $id, $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3760 |
$_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3761 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3762 |
// 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
|
3763 |
if ( false === $_term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3764 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3765 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3766 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3767 |
// 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
|
3768 |
$term_ids = 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 |
if ( is_numeric( $term_id ) ) { |
18 | 3771 |
$term_ids[] = (int) $term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3772 |
} elseif ( isset( $term_id->term_id ) ) { |
18 | 3773 |
$term_ids[] = (int) $term_id->term_id; |
7
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 |
} |
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 |
// Fill the term objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3778 |
_prime_term_caches( $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3779 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3780 |
$terms = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3781 |
foreach ( $term_ids as $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3782 |
$term = get_term( $term_id, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3783 |
if ( is_wp_error( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3784 |
return $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3785 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3786 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3787 |
$terms[] = $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3788 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3789 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3790 |
return $terms; |
0 | 3791 |
} |
3792 |
||
3793 |
/** |
|
5 | 3794 |
* Updates the cache for the given term object ID(s). |
3795 |
* |
|
3796 |
* Note: Due to performance concerns, great care should be taken to only update |
|
3797 |
* term caches when necessary. Processing time can increase exponentially depending |
|
3798 |
* on both the number of passed term IDs and the number of taxonomies those terms |
|
3799 |
* belong to. |
|
3800 |
* |
|
3801 |
* Caches will only be updated for terms not already cached. |
|
3802 |
* |
|
0 | 3803 |
* @since 2.3.0 |
5 | 3804 |
* |
16 | 3805 |
* @param string|int[] $object_ids Comma-separated list or array of term object IDs. |
3806 |
* @param string|string[] $object_type The taxonomy object type or array of the same. |
|
18 | 3807 |
* @return void|false Void on success or if the `$object_ids` parameter is empty, |
3808 |
* false if all of the terms in `$object_ids` are already cached. |
|
0 | 3809 |
*/ |
9 | 3810 |
function update_object_term_cache( $object_ids, $object_type ) { |
3811 |
if ( empty( $object_ids ) ) { |
|
0 | 3812 |
return; |
9 | 3813 |
} |
3814 |
||
3815 |
if ( ! is_array( $object_ids ) ) { |
|
3816 |
$object_ids = explode( ',', $object_ids ); |
|
3817 |
} |
|
3818 |
||
16 | 3819 |
$object_ids = array_map( 'intval', $object_ids ); |
3820 |
$non_cached_ids = array(); |
|
9 | 3821 |
|
3822 |
$taxonomies = get_object_taxonomies( $object_type ); |
|
0 | 3823 |
|
16 | 3824 |
foreach ( $taxonomies as $taxonomy ) { |
3825 |
$cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" ); |
|
3826 |
||
3827 |
foreach ( $cache_values as $id => $value ) { |
|
3828 |
if ( false === $value ) { |
|
3829 |
$non_cached_ids[] = $id; |
|
0 | 3830 |
} |
3831 |
} |
|
3832 |
} |
|
3833 |
||
16 | 3834 |
if ( empty( $non_cached_ids ) ) { |
0 | 3835 |
return false; |
9 | 3836 |
} |
3837 |
||
16 | 3838 |
$non_cached_ids = array_unique( $non_cached_ids ); |
3839 |
||
9 | 3840 |
$terms = wp_get_object_terms( |
16 | 3841 |
$non_cached_ids, |
9 | 3842 |
$taxonomies, |
3843 |
array( |
|
3844 |
'fields' => 'all_with_object_id', |
|
3845 |
'orderby' => 'name', |
|
3846 |
'update_term_meta_cache' => false, |
|
3847 |
) |
|
3848 |
); |
|
0 | 3849 |
|
3850 |
$object_terms = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3851 |
foreach ( (array) $terms as $term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3852 |
$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
|
3853 |
} |
0 | 3854 |
|
16 | 3855 |
foreach ( $non_cached_ids as $id ) { |
0 | 3856 |
foreach ( $taxonomies as $taxonomy ) { |
9 | 3857 |
if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) { |
3858 |
if ( ! isset( $object_terms[ $id ] ) ) { |
|
3859 |
$object_terms[ $id ] = array(); |
|
3860 |
} |
|
3861 |
$object_terms[ $id ][ $taxonomy ] = array(); |
|
0 | 3862 |
} |
3863 |
} |
|
3864 |
} |
|
3865 |
||
19 | 3866 |
$cache_values = array(); |
0 | 3867 |
foreach ( $object_terms as $id => $value ) { |
3868 |
foreach ( $value as $taxonomy => $terms ) { |
|
19 | 3869 |
$cache_values[ $taxonomy ][ $id ] = $terms; |
0 | 3870 |
} |
3871 |
} |
|
19 | 3872 |
foreach ( $cache_values as $taxonomy => $data ) { |
3873 |
wp_cache_add_multiple( $data, "{$taxonomy}_relationships" ); |
|
3874 |
} |
|
0 | 3875 |
} |
3876 |
||
3877 |
/** |
|
19 | 3878 |
* Updates terms in cache. |
0 | 3879 |
* |
3880 |
* @since 2.3.0 |
|
3881 |
* |
|
16 | 3882 |
* @param WP_Term[] $terms Array of term objects to change. |
3883 |
* @param string $taxonomy Not used. |
|
0 | 3884 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3885 |
function update_term_cache( $terms, $taxonomy = '' ) { |
19 | 3886 |
$data = array(); |
0 | 3887 |
foreach ( (array) $terms as $term ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3888 |
// 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
|
3889 |
$_term = clone $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3890 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3891 |
// Object ID should not be cached. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3892 |
unset( $_term->object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3893 |
|
19 | 3894 |
$data[ $term->term_id ] = $_term; |
3895 |
} |
|
3896 |
wp_cache_add_multiple( $data, 'terms' ); |
|
0 | 3897 |
} |
3898 |
||
3899 |
// |
|
16 | 3900 |
// Private. |
0 | 3901 |
// |
3902 |
||
3903 |
/** |
|
19 | 3904 |
* Retrieves children of taxonomy as term IDs. |
0 | 3905 |
* |
9 | 3906 |
* @access private |
0 | 3907 |
* @since 2.3.0 |
3908 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3909 |
* @param string $taxonomy Taxonomy name. |
19 | 3910 |
* @return array Empty if $taxonomy isn't hierarchical or returns children as term IDs. |
0 | 3911 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3912 |
function _get_term_hierarchy( $taxonomy ) { |
9 | 3913 |
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
0 | 3914 |
return array(); |
9 | 3915 |
} |
3916 |
$children = get_option( "{$taxonomy}_children" ); |
|
3917 |
||
3918 |
if ( is_array( $children ) ) { |
|
0 | 3919 |
return $children; |
9 | 3920 |
} |
0 | 3921 |
$children = array(); |
9 | 3922 |
$terms = get_terms( |
3923 |
array( |
|
16 | 3924 |
'taxonomy' => $taxonomy, |
9 | 3925 |
'get' => 'all', |
3926 |
'orderby' => 'id', |
|
3927 |
'fields' => 'id=>parent', |
|
3928 |
'update_term_meta_cache' => false, |
|
3929 |
) |
|
3930 |
); |
|
0 | 3931 |
foreach ( $terms as $term_id => $parent ) { |
9 | 3932 |
if ( $parent > 0 ) { |
3933 |
$children[ $parent ][] = $term_id; |
|
3934 |
} |
|
0 | 3935 |
} |
9 | 3936 |
update_option( "{$taxonomy}_children", $children ); |
0 | 3937 |
|
3938 |
return $children; |
|
3939 |
} |
|
3940 |
||
3941 |
/** |
|
19 | 3942 |
* Gets the subset of $terms that are descendants of $term_id. |
0 | 3943 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3944 |
* 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
|
3945 |
* If `$terms` is an array of IDs, then _get_term_children() returns an array of IDs. |
0 | 3946 |
* |
3947 |
* @access private |
|
3948 |
* @since 2.3.0 |
|
3949 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3950 |
* @param int $term_id The ancestor term: all returned terms should be descendants of `$term_id`. |
5 | 3951 |
* @param array $terms The set of terms - either an array of term objects or term IDs - from which those that |
3952 |
* are descendants of $term_id will be chosen. |
|
3953 |
* @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
|
3954 |
* @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
|
3955 |
* 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
|
3956 |
* 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
|
3957 |
* with 1 as value. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3958 |
* @return array|WP_Error The subset of $terms that are descendants of $term_id. |
0 | 3959 |
*/ |
5 | 3960 |
function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { |
0 | 3961 |
$empty_array = array(); |
9 | 3962 |
if ( empty( $terms ) ) { |
0 | 3963 |
return $empty_array; |
9 | 3964 |
} |
3965 |
||
16 | 3966 |
$term_id = (int) $term_id; |
9 | 3967 |
$term_list = array(); |
3968 |
$has_children = _get_term_hierarchy( $taxonomy ); |
|
3969 |
||
16 | 3970 |
if ( $term_id && ! isset( $has_children[ $term_id ] ) ) { |
0 | 3971 |
return $empty_array; |
9 | 3972 |
} |
0 | 3973 |
|
5 | 3974 |
// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. |
3975 |
if ( empty( $ancestors ) ) { |
|
3976 |
$ancestors[ $term_id ] = 1; |
|
3977 |
} |
|
3978 |
||
0 | 3979 |
foreach ( (array) $terms as $term ) { |
3980 |
$use_id = false; |
|
9 | 3981 |
if ( ! is_object( $term ) ) { |
3982 |
$term = get_term( $term, $taxonomy ); |
|
3983 |
if ( is_wp_error( $term ) ) { |
|
0 | 3984 |
return $term; |
9 | 3985 |
} |
0 | 3986 |
$use_id = true; |
3987 |
} |
|
3988 |
||
5 | 3989 |
// Don't recurse if we've already identified the term as a child - this indicates a loop. |
3990 |
if ( isset( $ancestors[ $term->term_id ] ) ) { |
|
0 | 3991 |
continue; |
5 | 3992 |
} |
0 | 3993 |
|
16 | 3994 |
if ( (int) $term->parent === $term_id ) { |
9 | 3995 |
if ( $use_id ) { |
0 | 3996 |
$term_list[] = $term->term_id; |
9 | 3997 |
} else { |
0 | 3998 |
$term_list[] = $term; |
9 | 3999 |
} |
4000 |
||
4001 |
if ( ! isset( $has_children[ $term->term_id ] ) ) { |
|
0 | 4002 |
continue; |
9 | 4003 |
} |
0 | 4004 |
|
5 | 4005 |
$ancestors[ $term->term_id ] = 1; |
4006 |
||
16 | 4007 |
$children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ); |
4008 |
if ( $children ) { |
|
9 | 4009 |
$term_list = array_merge( $term_list, $children ); |
4010 |
} |
|
0 | 4011 |
} |
4012 |
} |
|
4013 |
||
4014 |
return $term_list; |
|
4015 |
} |
|
4016 |
||
4017 |
/** |
|
19 | 4018 |
* Adds count of children to parent count. |
0 | 4019 |
* |
4020 |
* Recalculates term counts by including items from child terms. Assumes all |
|
4021 |
* relevant children are already in the $terms argument. |
|
4022 |
* |
|
4023 |
* @access private |
|
4024 |
* @since 2.3.0 |
|
5 | 4025 |
* |
4026 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4027 |
* |
18 | 4028 |
* @param object[]|WP_Term[] $terms List of term objects (passed by reference). |
4029 |
* @param string $taxonomy Term context. |
|
0 | 4030 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4031 |
function _pad_term_counts( &$terms, $taxonomy ) { |
0 | 4032 |
global $wpdb; |
4033 |
||
4034 |
// This function only works for hierarchical taxonomies like post categories. |
|
9 | 4035 |
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) { |
0 | 4036 |
return; |
9 | 4037 |
} |
4038 |
||
4039 |
$term_hier = _get_term_hierarchy( $taxonomy ); |
|
4040 |
||
4041 |
if ( empty( $term_hier ) ) { |
|
0 | 4042 |
return; |
9 | 4043 |
} |
4044 |
||
4045 |
$term_items = array(); |
|
5 | 4046 |
$terms_by_id = array(); |
9 | 4047 |
$term_ids = array(); |
0 | 4048 |
|
4049 |
foreach ( (array) $terms as $key => $term ) { |
|
9 | 4050 |
$terms_by_id[ $term->term_id ] = & $terms[ $key ]; |
4051 |
$term_ids[ $term->term_taxonomy_id ] = $term->term_id; |
|
0 | 4052 |
} |
4053 |
||
16 | 4054 |
// Get the object and term IDs and stick them in a lookup table. |
9 | 4055 |
$tax_obj = get_taxonomy( $taxonomy ); |
4056 |
$object_types = esc_sql( $tax_obj->object_type ); |
|
4057 |
$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 | 4058 |
|
0 | 4059 |
foreach ( $results as $row ) { |
16 | 4060 |
$id = $term_ids[ $row->term_taxonomy_id ]; |
4061 |
||
9 | 4062 |
$term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1; |
0 | 4063 |
} |
4064 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4065 |
// Touch every ancestor's lookup row for each post in each term. |
0 | 4066 |
foreach ( $term_ids as $term_id ) { |
9 | 4067 |
$child = $term_id; |
5 | 4068 |
$ancestors = array(); |
9 | 4069 |
while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) { |
5 | 4070 |
$ancestors[] = $child; |
16 | 4071 |
|
9 | 4072 |
if ( ! empty( $term_items[ $term_id ] ) ) { |
4073 |
foreach ( $term_items[ $term_id ] as $item_id => $touches ) { |
|
4074 |
$term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1; |
|
0 | 4075 |
} |
9 | 4076 |
} |
16 | 4077 |
|
0 | 4078 |
$child = $parent; |
5 | 4079 |
|
16 | 4080 |
if ( in_array( $parent, $ancestors, true ) ) { |
5 | 4081 |
break; |
4082 |
} |
|
0 | 4083 |
} |
4084 |
} |
|
4085 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4086 |
// Transfer the touched cells. |
9 | 4087 |
foreach ( (array) $term_items as $id => $items ) { |
4088 |
if ( isset( $terms_by_id[ $id ] ) ) { |
|
4089 |
$terms_by_id[ $id ]->count = count( $items ); |
|
4090 |
} |
|
4091 |
} |
|
0 | 4092 |
} |
4093 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4094 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4095 |
* 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
|
4096 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4097 |
* @since 4.6.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4098 |
* @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
|
4099 |
* @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
|
4100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4101 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4102 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4103 |
* @param array $term_ids Array of term IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4104 |
* @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
|
4105 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4106 |
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
|
4107 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4108 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4109 |
$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
|
4110 |
if ( ! empty( $non_cached_ids ) ) { |
18 | 4111 |
$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
|
4112 |
|
19 | 4113 |
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
|
4114 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4115 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4116 |
if ( $update_meta_cache ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4117 |
wp_lazyload_term_meta( $term_ids ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4118 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4119 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4120 |
|
0 | 4121 |
// |
16 | 4122 |
// Default callbacks. |
0 | 4123 |
// |
4124 |
||
4125 |
/** |
|
19 | 4126 |
* Updates term count based on object types of the current taxonomy. |
0 | 4127 |
* |
4128 |
* Private function for the default callback for post_tag and category |
|
4129 |
* taxonomies. |
|
4130 |
* |
|
4131 |
* @access private |
|
4132 |
* @since 2.3.0 |
|
5 | 4133 |
* |
4134 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4135 |
* |
19 | 4136 |
* @param int[] $terms List of term taxonomy IDs. |
16 | 4137 |
* @param WP_Taxonomy $taxonomy Current taxonomy object of terms. |
0 | 4138 |
*/ |
4139 |
function _update_post_term_count( $terms, $taxonomy ) { |
|
4140 |
global $wpdb; |
|
4141 |
||
4142 |
$object_types = (array) $taxonomy->object_type; |
|
4143 |
||
9 | 4144 |
foreach ( $object_types as &$object_type ) { |
0 | 4145 |
list( $object_type ) = explode( ':', $object_type ); |
9 | 4146 |
} |
0 | 4147 |
|
4148 |
$object_types = array_unique( $object_types ); |
|
4149 |
||
16 | 4150 |
$check_attachments = array_search( 'attachment', $object_types, true ); |
4151 |
if ( false !== $check_attachments ) { |
|
0 | 4152 |
unset( $object_types[ $check_attachments ] ); |
4153 |
$check_attachments = true; |
|
4154 |
} |
|
4155 |
||
9 | 4156 |
if ( $object_types ) { |
0 | 4157 |
$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); |
9 | 4158 |
} |
0 | 4159 |
|
18 | 4160 |
$post_statuses = array( 'publish' ); |
4161 |
||
4162 |
/** |
|
4163 |
* Filters the post statuses for updating the term count. |
|
4164 |
* |
|
4165 |
* @since 5.7.0 |
|
4166 |
* |
|
4167 |
* @param string[] $post_statuses List of post statuses to include in the count. Default is 'publish'. |
|
4168 |
* @param WP_Taxonomy $taxonomy Current taxonomy object. |
|
4169 |
*/ |
|
4170 |
$post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) ); |
|
4171 |
||
0 | 4172 |
foreach ( (array) $terms as $term ) { |
4173 |
$count = 0; |
|
4174 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4175 |
// Attachments can be 'inherit' status, we need to base count off the parent's status if so. |
9 | 4176 |
if ( $check_attachments ) { |
18 | 4177 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration |
4178 |
$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 | 4179 |
} |
4180 |
||
4181 |
if ( $object_types ) { |
|
16 | 4182 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration |
18 | 4183 |
$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 | 4184 |
} |
0 | 4185 |
|
5 | 4186 |
/** This action is documented in wp-includes/taxonomy.php */ |
4187 |
do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4188 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
5 | 4189 |
|
4190 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
4191 |
do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4192 |
} |
4193 |
} |
|
4194 |
||
4195 |
/** |
|
19 | 4196 |
* Updates term count based on number of objects. |
0 | 4197 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4198 |
* Default callback for the 'link_category' taxonomy. |
0 | 4199 |
* |
4200 |
* @since 3.3.0 |
|
5 | 4201 |
* |
4202 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4203 |
* |
16 | 4204 |
* @param int[] $terms List of term taxonomy IDs. |
4205 |
* @param WP_Taxonomy $taxonomy Current taxonomy object of terms. |
|
0 | 4206 |
*/ |
4207 |
function _update_generic_term_count( $terms, $taxonomy ) { |
|
4208 |
global $wpdb; |
|
4209 |
||
4210 |
foreach ( (array) $terms as $term ) { |
|
4211 |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|
4212 |
||
5 | 4213 |
/** This action is documented in wp-includes/taxonomy.php */ |
4214 |
do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4215 |
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
5 | 4216 |
|
4217 |
/** This action is documented in wp-includes/taxonomy.php */ |
|
4218 |
do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); |
|
0 | 4219 |
} |
4220 |
} |
|
4221 |
||
4222 |
/** |
|
19 | 4223 |
* 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
|
4224 |
* with another term_taxonomy. |
5 | 4225 |
* |
4226 |
* @ignore |
|
4227 |
* @since 4.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4228 |
* @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
|
4229 |
* `$term_taxonomy_id` can now accept objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4230 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4231 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4232 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4233 |
* @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
|
4234 |
* @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
|
4235 |
* (corresponding to a row from the term_taxonomy table). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4236 |
* @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
|
4237 |
* 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
|
4238 |
* 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
|
4239 |
* 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
|
4240 |
* Default: true. |
5 | 4241 |
* @return int|WP_Error When the current term does not need to be split (or cannot be split on the current |
4242 |
* database schema), `$term_id` is returned. When the term is successfully split, the |
|
4243 |
* new term_id is returned. A WP_Error is returned for miscellaneous errors. |
|
4244 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4245 |
function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { |
5 | 4246 |
global $wpdb; |
4247 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4248 |
if ( is_object( $term_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4249 |
$shared_term = $term_id; |
18 | 4250 |
$term_id = (int) $shared_term->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4251 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4252 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4253 |
if ( is_object( $term_taxonomy_id ) ) { |
9 | 4254 |
$term_taxonomy = $term_taxonomy_id; |
18 | 4255 |
$term_taxonomy_id = (int) $term_taxonomy->term_taxonomy_id; |
5 | 4256 |
} |
4257 |
||
4258 |
// If there are no shared term_taxonomy rows, there's nothing to do here. |
|
16 | 4259 |
$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
|
4260 |
|
5 | 4261 |
if ( ! $shared_tt_count ) { |
4262 |
return $term_id; |
|
4263 |
} |
|
4264 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4265 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4266 |
* 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
|
4267 |
* 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
|
4268 |
*/ |
16 | 4269 |
$check_term_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); |
4270 |
if ( $check_term_id !== $term_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4271 |
return $check_term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4272 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4273 |
|
5 | 4274 |
// 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
|
4275 |
if ( empty( $shared_term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4276 |
$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
|
4277 |
} |
5 | 4278 |
|
4279 |
$new_term_data = array( |
|
9 | 4280 |
'name' => $shared_term->name, |
4281 |
'slug' => $shared_term->slug, |
|
5 | 4282 |
'term_group' => $shared_term->term_group, |
4283 |
); |
|
4284 |
||
4285 |
if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) { |
|
4286 |
return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error ); |
|
4287 |
} |
|
4288 |
||
4289 |
$new_term_id = (int) $wpdb->insert_id; |
|
4290 |
||
4291 |
// Update the existing term_taxonomy to point to the newly created term. |
|
9 | 4292 |
$wpdb->update( |
4293 |
$wpdb->term_taxonomy, |
|
5 | 4294 |
array( 'term_id' => $new_term_id ), |
4295 |
array( 'term_taxonomy_id' => $term_taxonomy_id ) |
|
4296 |
); |
|
4297 |
||
4298 |
// Reassign child terms to the new parent. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4299 |
if ( empty( $term_taxonomy ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4300 |
$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
|
4301 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4302 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4303 |
$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 | 4304 |
if ( ! empty( $children_tt_ids ) ) { |
4305 |
foreach ( $children_tt_ids as $child_tt_id ) { |
|
9 | 4306 |
$wpdb->update( |
4307 |
$wpdb->term_taxonomy, |
|
5 | 4308 |
array( 'parent' => $new_term_id ), |
4309 |
array( 'term_taxonomy_id' => $child_tt_id ) |
|
4310 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4311 |
clean_term_cache( (int) $child_tt_id, '', false ); |
5 | 4312 |
} |
4313 |
} else { |
|
4314 |
// 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
|
4315 |
clean_term_cache( $new_term_id, $term_taxonomy->taxonomy, false ); |
5 | 4316 |
} |
4317 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4318 |
clean_term_cache( $term_id, $term_taxonomy->taxonomy, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4320 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4321 |
* 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
|
4322 |
* regenerating the taxonomy's hierarchy tree. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4323 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4324 |
$taxonomies_to_clean = array( $term_taxonomy->taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4325 |
|
5 | 4326 |
// 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
|
4327 |
$shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) ); |
9 | 4328 |
$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
|
4329 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4330 |
foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4331 |
clean_taxonomy_cache( $taxonomy_to_clean ); |
5 | 4332 |
} |
4333 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4334 |
// 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
|
4335 |
if ( $record ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4336 |
$split_term_data = get_option( '_split_terms', array() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4337 |
if ( ! isset( $split_term_data[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4338 |
$split_term_data[ $term_id ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4339 |
} |
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 |
$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
|
4342 |
update_option( '_split_terms', $split_term_data ); |
5 | 4343 |
} |
4344 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4345 |
// 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
|
4346 |
$shared_terms_exist = $wpdb->get_results( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4347 |
"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
|
4348 |
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
|
4349 |
GROUP BY t.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4350 |
HAVING term_tt_count > 1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4351 |
LIMIT 1" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4352 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4353 |
if ( ! $shared_terms_exist ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4354 |
update_option( 'finished_splitting_shared_terms', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4355 |
} |
5 | 4356 |
|
4357 |
/** |
|
4358 |
* Fires after a previously shared taxonomy term is split into two separate terms. |
|
4359 |
* |
|
4360 |
* @since 4.2.0 |
|
4361 |
* |
|
4362 |
* @param int $term_id ID of the formerly shared term. |
|
4363 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4364 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4365 |
* @param string $taxonomy Taxonomy for the split term. |
|
4366 |
*/ |
|
4367 |
do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy ); |
|
4368 |
||
4369 |
return $new_term_id; |
|
4370 |
} |
|
4371 |
||
4372 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4373 |
* Splits a batch of shared taxonomy terms. |
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 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4376 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4377 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4378 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4379 |
function _wp_batch_split_terms() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4380 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4381 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4382 |
$lock_name = 'term_split.lock'; |
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 |
// Try to lock. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4385 |
$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
|
4386 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4387 |
if ( ! $lock_result ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4388 |
$lock_result = get_option( $lock_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4389 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4390 |
// 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
|
4391 |
if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4392 |
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
|
4393 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4394 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4395 |
} |
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 |
// 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
|
4398 |
update_option( $lock_name, time() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4399 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4400 |
// 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
|
4401 |
$shared_terms = $wpdb->get_results( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4402 |
"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
|
4403 |
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
|
4404 |
GROUP BY t.term_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4405 |
HAVING term_tt_count > 1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4406 |
LIMIT 10" |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4407 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4408 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4409 |
// No more terms, we're done here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4410 |
if ( ! $shared_terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4411 |
update_option( 'finished_splitting_shared_terms', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4412 |
delete_option( $lock_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4413 |
return; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4416 |
// 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
|
4417 |
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
|
4418 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4419 |
// Rekey shared term array for faster lookups. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4420 |
$_shared_terms = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4421 |
foreach ( $shared_terms as $shared_term ) { |
18 | 4422 |
$term_id = (int) $shared_term->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4423 |
$_shared_terms[ $term_id ] = $shared_term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4425 |
$shared_terms = $_shared_terms; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4426 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4427 |
// Get term taxonomy data for all shared terms. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4428 |
$shared_term_ids = implode( ',', array_keys( $shared_terms ) ); |
9 | 4429 |
$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
|
4430 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4431 |
// Split term data recording is slow, so we do it just once, outside the loop. |
9 | 4432 |
$split_term_data = get_option( '_split_terms', array() ); |
16 | 4433 |
$skipped_first_term = array(); |
4434 |
$taxonomies = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4435 |
foreach ( $shared_tts as $shared_tt ) { |
18 | 4436 |
$term_id = (int) $shared_tt->term_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4437 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4438 |
// 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
|
4439 |
if ( ! isset( $skipped_first_term[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4440 |
$skipped_first_term[ $term_id ] = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4441 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4442 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4443 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4444 |
if ( ! isset( $split_term_data[ $term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4445 |
$split_term_data[ $term_id ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4446 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4447 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4448 |
// Keep track of taxonomies whose hierarchies need flushing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4449 |
if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4450 |
$taxonomies[ $shared_tt->taxonomy ] = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4451 |
} |
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 |
// Split the term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4454 |
$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
|
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 |
// Rebuild the cached hierarchy for each affected taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4458 |
foreach ( array_keys( $taxonomies ) as $tax ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4459 |
delete_option( "{$tax}_children" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4460 |
_get_term_hierarchy( $tax ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4461 |
} |
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 |
update_option( '_split_terms', $split_term_data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4464 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4465 |
delete_option( $lock_name ); |
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 |
* In order to avoid the _wp_batch_split_terms() job being accidentally removed, |
19 | 4470 |
* 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
|
4471 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4472 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4473 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4474 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4475 |
function _wp_check_for_scheduled_split_terms() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4476 |
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
|
4477 |
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
|
4478 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4479 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4480 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4481 |
/** |
19 | 4482 |
* Checks default categories when a term gets split to see if any of them need to be updated. |
5 | 4483 |
* |
4484 |
* @ignore |
|
4485 |
* @since 4.2.0 |
|
4486 |
* |
|
4487 |
* @param int $term_id ID of the formerly shared term. |
|
4488 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4489 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4490 |
* @param string $taxonomy Taxonomy for the split term. |
|
4491 |
*/ |
|
4492 |
function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
|
16 | 4493 |
if ( 'category' !== $taxonomy ) { |
5 | 4494 |
return; |
4495 |
} |
|
4496 |
||
4497 |
foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) { |
|
16 | 4498 |
if ( (int) get_option( $option, -1 ) === $term_id ) { |
5 | 4499 |
update_option( $option, $new_term_id ); |
4500 |
} |
|
4501 |
} |
|
4502 |
} |
|
4503 |
||
4504 |
/** |
|
19 | 4505 |
* Checks menu items when a term gets split to see if any of them need to be updated. |
5 | 4506 |
* |
4507 |
* @ignore |
|
4508 |
* @since 4.2.0 |
|
4509 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4510 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4511 |
* |
5 | 4512 |
* @param int $term_id ID of the formerly shared term. |
4513 |
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id. |
|
4514 |
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. |
|
4515 |
* @param string $taxonomy Taxonomy for the split term. |
|
4516 |
*/ |
|
4517 |
function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
|
4518 |
global $wpdb; |
|
9 | 4519 |
$post_ids = $wpdb->get_col( |
4520 |
$wpdb->prepare( |
|
4521 |
"SELECT m1.post_id |
|
5 | 4522 |
FROM {$wpdb->postmeta} AS m1 |
4523 |
INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id ) |
|
4524 |
INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id ) |
|
4525 |
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
|
4526 |
AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s ) |
5 | 4527 |
AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )", |
9 | 4528 |
$taxonomy, |
4529 |
$term_id |
|
4530 |
) |
|
4531 |
); |
|
5 | 4532 |
|
4533 |
if ( $post_ids ) { |
|
4534 |
foreach ( $post_ids as $post_id ) { |
|
4535 |
update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id ); |
|
4536 |
} |
|
4537 |
} |
|
4538 |
} |
|
4539 |
||
4540 |
/** |
|
19 | 4541 |
* 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
|
4542 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4543 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4544 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4545 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4546 |
* @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
|
4547 |
* @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
|
4548 |
* @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
|
4549 |
* @param string $taxonomy Taxonomy for the split term. |
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 |
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
|
4552 |
if ( 'nav_menu' !== $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4553 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4554 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4555 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4556 |
// Update menu locations. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4557 |
$locations = get_nav_menu_locations(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4558 |
foreach ( $locations as $location => $menu_id ) { |
16 | 4559 |
if ( $term_id === $menu_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4560 |
$locations[ $location ] = $new_term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4561 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4562 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4563 |
set_theme_mod( 'nav_menu_locations', $locations ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4564 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4565 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4566 |
/** |
19 | 4567 |
* Gets data about terms that previously shared a single term_id, but have since been split. |
5 | 4568 |
* |
4569 |
* @since 4.2.0 |
|
4570 |
* |
|
4571 |
* @param int $old_term_id Term ID. This is the old, pre-split term ID. |
|
4572 |
* @return array Array of new term IDs, keyed by taxonomy. |
|
4573 |
*/ |
|
4574 |
function wp_get_split_terms( $old_term_id ) { |
|
4575 |
$split_terms = get_option( '_split_terms', array() ); |
|
4576 |
||
4577 |
$terms = array(); |
|
4578 |
if ( isset( $split_terms[ $old_term_id ] ) ) { |
|
4579 |
$terms = $split_terms[ $old_term_id ]; |
|
4580 |
} |
|
4581 |
||
4582 |
return $terms; |
|
4583 |
} |
|
4584 |
||
4585 |
/** |
|
19 | 4586 |
* Gets the new term ID corresponding to a previously split term. |
5 | 4587 |
* |
4588 |
* @since 4.2.0 |
|
4589 |
* |
|
4590 |
* @param int $old_term_id Term ID. This is the old, pre-split term ID. |
|
4591 |
* @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
|
4592 |
* @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
|
4593 |
* 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
|
4594 |
* the parameters, returns false. |
5 | 4595 |
*/ |
4596 |
function wp_get_split_term( $old_term_id, $taxonomy ) { |
|
4597 |
$split_terms = wp_get_split_terms( $old_term_id ); |
|
4598 |
||
4599 |
$term_id = false; |
|
4600 |
if ( isset( $split_terms[ $taxonomy ] ) ) { |
|
4601 |
$term_id = (int) $split_terms[ $taxonomy ]; |
|
4602 |
} |
|
4603 |
||
4604 |
return $term_id; |
|
4605 |
} |
|
4606 |
||
4607 |
/** |
|
19 | 4608 |
* 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
|
4609 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4610 |
* 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
|
4611 |
* 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
|
4612 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4613 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4614 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4615 |
* @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
|
4616 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4617 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4618 |
* @return bool Returns false if a term is not shared between multiple taxonomies or |
9 | 4619 |
* if splitting shared taxonomy terms is finished. |
7
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 |
function wp_term_is_shared( $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4622 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4623 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4624 |
if ( get_option( 'finished_splitting_shared_terms' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4625 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4626 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4627 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4628 |
$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
|
4629 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4630 |
return $tt_count > 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4631 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4632 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4633 |
/** |
19 | 4634 |
* Generates a permalink for a taxonomy term archive. |
0 | 4635 |
* |
4636 |
* @since 2.5.0 |
|
4637 |
* |
|
16 | 4638 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
4639 |
* |
|
4640 |
* @param WP_Term|int|string $term The term object, ID, or slug whose link will be retrieved. |
|
4641 |
* @param string $taxonomy Optional. Taxonomy. Default empty. |
|
4642 |
* @return string|WP_Error URL of the taxonomy term archive on success, WP_Error if term does not exist. |
|
0 | 4643 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4644 |
function get_term_link( $term, $taxonomy = '' ) { |
0 | 4645 |
global $wp_rewrite; |
4646 |
||
9 | 4647 |
if ( ! is_object( $term ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4648 |
if ( is_int( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4649 |
$term = get_term( $term, $taxonomy ); |
0 | 4650 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4651 |
$term = get_term_by( 'slug', $term, $taxonomy ); |
0 | 4652 |
} |
4653 |
} |
|
4654 |
||
9 | 4655 |
if ( ! is_object( $term ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4656 |
$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); |
9 | 4657 |
} |
4658 |
||
4659 |
if ( is_wp_error( $term ) ) { |
|
0 | 4660 |
return $term; |
9 | 4661 |
} |
0 | 4662 |
|
4663 |
$taxonomy = $term->taxonomy; |
|
4664 |
||
9 | 4665 |
$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy ); |
0 | 4666 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4667 |
/** |
18 | 4668 |
* 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
|
4669 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4670 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4671 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4672 |
* @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
|
4673 |
* @param WP_Term $term The term object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4674 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4675 |
$termlink = apply_filters( 'pre_term_link', $termlink, $term ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4676 |
|
0 | 4677 |
$slug = $term->slug; |
9 | 4678 |
$t = get_taxonomy( $taxonomy ); |
4679 |
||
4680 |
if ( empty( $termlink ) ) { |
|
16 | 4681 |
if ( 'category' === $taxonomy ) { |
0 | 4682 |
$termlink = '?cat=' . $term->term_id; |
9 | 4683 |
} elseif ( $t->query_var ) { |
0 | 4684 |
$termlink = "?$t->query_var=$slug"; |
9 | 4685 |
} else { |
0 | 4686 |
$termlink = "?taxonomy=$taxonomy&term=$slug"; |
9 | 4687 |
} |
4688 |
$termlink = home_url( $termlink ); |
|
0 | 4689 |
} else { |
18 | 4690 |
if ( ! empty( $t->rewrite['hierarchical'] ) ) { |
0 | 4691 |
$hierarchical_slugs = array(); |
9 | 4692 |
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); |
4693 |
foreach ( (array) $ancestors as $ancestor ) { |
|
4694 |
$ancestor_term = get_term( $ancestor, $taxonomy ); |
|
0 | 4695 |
$hierarchical_slugs[] = $ancestor_term->slug; |
4696 |
} |
|
9 | 4697 |
$hierarchical_slugs = array_reverse( $hierarchical_slugs ); |
0 | 4698 |
$hierarchical_slugs[] = $slug; |
9 | 4699 |
$termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink ); |
0 | 4700 |
} else { |
9 | 4701 |
$termlink = str_replace( "%$taxonomy%", $slug, $termlink ); |
0 | 4702 |
} |
9 | 4703 |
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) ); |
0 | 4704 |
} |
16 | 4705 |
|
4706 |
// Back compat filters. |
|
4707 |
if ( 'post_tag' === $taxonomy ) { |
|
5 | 4708 |
|
4709 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4710 |
* Filters the tag link. |
5 | 4711 |
* |
4712 |
* @since 2.3.0 |
|
16 | 4713 |
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter. |
4714 |
* @since 5.4.1 Restored (un-deprecated). |
|
5 | 4715 |
* |
4716 |
* @param string $termlink Tag link URL. |
|
4717 |
* @param int $term_id Term ID. |
|
4718 |
*/ |
|
0 | 4719 |
$termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); |
16 | 4720 |
} elseif ( 'category' === $taxonomy ) { |
5 | 4721 |
|
4722 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4723 |
* Filters the category link. |
5 | 4724 |
* |
4725 |
* @since 1.5.0 |
|
16 | 4726 |
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter. |
4727 |
* @since 5.4.1 Restored (un-deprecated). |
|
5 | 4728 |
* |
4729 |
* @param string $termlink Category link URL. |
|
4730 |
* @param int $term_id Term ID. |
|
4731 |
*/ |
|
0 | 4732 |
$termlink = apply_filters( 'category_link', $termlink, $term->term_id ); |
5 | 4733 |
} |
4734 |
||
4735 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4736 |
* Filters the term link. |
5 | 4737 |
* |
4738 |
* @since 2.5.0 |
|
4739 |
* |
|
16 | 4740 |
* @param string $termlink Term link URL. |
4741 |
* @param WP_Term $term Term object. |
|
4742 |
* @param string $taxonomy Taxonomy slug. |
|
5 | 4743 |
*/ |
4744 |
return apply_filters( 'term_link', $termlink, $term, $taxonomy ); |
|
0 | 4745 |
} |
4746 |
||
4747 |
/** |
|
19 | 4748 |
* Displays the taxonomies of a post with available options. |
0 | 4749 |
* |
4750 |
* This function can be used within the loop to display the taxonomies for a |
|
4751 |
* post without specifying the Post ID. You can also use it outside the Loop to |
|
4752 |
* display the taxonomies for a specific post. |
|
4753 |
* |
|
4754 |
* @since 2.5.0 |
|
5 | 4755 |
* |
4756 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4757 |
* 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
|
4758 |
* supported by get_the_taxonomies(), in addition to the following. |
5 | 4759 |
* |
19 | 4760 |
* @type int|WP_Post $post Post ID or object to get taxonomies of. Default current post. |
4761 |
* @type string $before Displays before the taxonomies. Default empty string. |
|
4762 |
* @type string $sep Separates each taxonomy. Default is a space. |
|
4763 |
* @type string $after Displays after the taxonomies. Default empty string. |
|
5 | 4764 |
* } |
0 | 4765 |
*/ |
5 | 4766 |
function the_taxonomies( $args = array() ) { |
0 | 4767 |
$defaults = array( |
9 | 4768 |
'post' => 0, |
0 | 4769 |
'before' => '', |
9 | 4770 |
'sep' => ' ', |
4771 |
'after' => '', |
|
0 | 4772 |
); |
4773 |
||
16 | 4774 |
$parsed_args = wp_parse_args( $args, $defaults ); |
4775 |
||
18 | 4776 |
echo $parsed_args['before'] . implode( $parsed_args['sep'], get_the_taxonomies( $parsed_args['post'], $parsed_args ) ) . $parsed_args['after']; |
0 | 4777 |
} |
4778 |
||
4779 |
/** |
|
19 | 4780 |
* Retrieves all taxonomies associated with a post. |
0 | 4781 |
* |
4782 |
* This function can be used within the loop. It will also return an array of |
|
4783 |
* the taxonomies with links to the taxonomy and name. |
|
4784 |
* |
|
4785 |
* @since 2.5.0 |
|
4786 |
* |
|
5 | 4787 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
16 | 4788 |
* @param array $args { |
4789 |
* Optional. Arguments about how to format the list of taxonomies. Default empty array. |
|
5 | 4790 |
* |
4791 |
* @type string $template Template for displaying a taxonomy label and list of terms. |
|
4792 |
* Default is "Label: Terms." |
|
4793 |
* @type string $term_template Template for displaying a single term in the list. Default is the term name |
|
4794 |
* linked to its archive. |
|
4795 |
* } |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4796 |
* @return string[] List of taxonomies. |
0 | 4797 |
*/ |
5 | 4798 |
function get_the_taxonomies( $post = 0, $args = array() ) { |
0 | 4799 |
$post = get_post( $post ); |
4800 |
||
9 | 4801 |
$args = wp_parse_args( |
4802 |
$args, |
|
4803 |
array( |
|
16 | 4804 |
/* translators: %s: Taxonomy label, %l: List of terms formatted as per $term_template. */ |
9 | 4805 |
'template' => __( '%s: %l.' ), |
4806 |
'term_template' => '<a href="%1$s">%2$s</a>', |
|
4807 |
) |
|
4808 |
); |
|
0 | 4809 |
|
4810 |
$taxonomies = array(); |
|
4811 |
||
5 | 4812 |
if ( ! $post ) { |
0 | 4813 |
return $taxonomies; |
5 | 4814 |
} |
4815 |
||
4816 |
foreach ( get_object_taxonomies( $post ) as $taxonomy ) { |
|
4817 |
$t = (array) get_taxonomy( $taxonomy ); |
|
4818 |
if ( empty( $t['label'] ) ) { |
|
0 | 4819 |
$t['label'] = $taxonomy; |
5 | 4820 |
} |
4821 |
if ( empty( $t['args'] ) ) { |
|
0 | 4822 |
$t['args'] = array(); |
5 | 4823 |
} |
4824 |
if ( empty( $t['template'] ) ) { |
|
4825 |
$t['template'] = $args['template']; |
|
4826 |
} |
|
4827 |
if ( empty( $t['term_template'] ) ) { |
|
4828 |
$t['term_template'] = $args['term_template']; |
|
4829 |
} |
|
4830 |
||
4831 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
|
4832 |
if ( false === $terms ) { |
|
4833 |
$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); |
|
4834 |
} |
|
0 | 4835 |
$links = array(); |
4836 |
||
5 | 4837 |
foreach ( $terms as $term ) { |
4838 |
$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name ); |
|
4839 |
} |
|
4840 |
if ( $links ) { |
|
9 | 4841 |
$taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); |
5 | 4842 |
} |
0 | 4843 |
} |
4844 |
return $taxonomies; |
|
4845 |
} |
|
4846 |
||
4847 |
/** |
|
19 | 4848 |
* Retrieves all taxonomy names for the given post. |
0 | 4849 |
* |
4850 |
* @since 2.5.0 |
|
5 | 4851 |
* |
4852 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
|
16 | 4853 |
* @return string[] An array of all taxonomy names for the given post. |
0 | 4854 |
*/ |
5 | 4855 |
function get_post_taxonomies( $post = 0 ) { |
0 | 4856 |
$post = get_post( $post ); |
4857 |
||
9 | 4858 |
return get_object_taxonomies( $post ); |
0 | 4859 |
} |
4860 |
||
4861 |
/** |
|
19 | 4862 |
* Determines if the given object is associated with any of the given terms. |
0 | 4863 |
* |
4864 |
* The given terms are checked against the object's terms' term_ids, names and slugs. |
|
4865 |
* Terms given as integers will only be checked against the object's terms' term_ids. |
|
4866 |
* If no terms are given, determines if object is associated with any terms in the given taxonomy. |
|
4867 |
* |
|
4868 |
* @since 2.7.0 |
|
4869 |
* |
|
18 | 4870 |
* @param int $object_id ID of the object (post ID, link ID, ...). |
4871 |
* @param string $taxonomy Single taxonomy name. |
|
4872 |
* @param int|string|int[]|string[] $terms Optional. Term ID, name, slug, or array of such |
|
4873 |
* to check against. Default null. |
|
5 | 4874 |
* @return bool|WP_Error WP_Error on input error. |
0 | 4875 |
*/ |
4876 |
function is_object_in_term( $object_id, $taxonomy, $terms = null ) { |
|
16 | 4877 |
$object_id = (int) $object_id; |
4878 |
if ( ! $object_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4879 |
return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) ); |
9 | 4880 |
} |
0 | 4881 |
|
4882 |
$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
|
4883 |
if ( false === $object_terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4884 |
$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
|
4885 |
if ( is_wp_error( $object_terms ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4886 |
return $object_terms; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4887 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4888 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4889 |
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
|
4890 |
} |
0 | 4891 |
|
9 | 4892 |
if ( is_wp_error( $object_terms ) ) { |
0 | 4893 |
return $object_terms; |
9 | 4894 |
} |
4895 |
if ( empty( $object_terms ) ) { |
|
0 | 4896 |
return false; |
9 | 4897 |
} |
4898 |
if ( empty( $terms ) ) { |
|
4899 |
return ( ! empty( $object_terms ) ); |
|
4900 |
} |
|
0 | 4901 |
|
4902 |
$terms = (array) $terms; |
|
4903 |
||
16 | 4904 |
$ints = array_filter( $terms, 'is_int' ); |
4905 |
if ( $ints ) { |
|
0 | 4906 |
$strs = array_diff( $terms, $ints ); |
9 | 4907 |
} else { |
0 | 4908 |
$strs =& $terms; |
9 | 4909 |
} |
0 | 4910 |
|
4911 |
foreach ( $object_terms as $object_term ) { |
|
5 | 4912 |
// If term is an int, check against term_ids only. |
16 | 4913 |
if ( $ints && in_array( $object_term->term_id, $ints, true ) ) { |
5 | 4914 |
return true; |
4915 |
} |
|
4916 |
||
0 | 4917 |
if ( $strs ) { |
5 | 4918 |
// Only check numeric strings against term_id, to avoid false matches due to type juggling. |
4919 |
$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) ); |
|
4920 |
if ( in_array( $object_term->term_id, $numeric_strs, true ) ) { |
|
4921 |
return true; |
|
4922 |
} |
|
4923 |
||
16 | 4924 |
if ( in_array( $object_term->name, $strs, true ) ) { |
9 | 4925 |
return true; |
4926 |
} |
|
16 | 4927 |
if ( in_array( $object_term->slug, $strs, true ) ) { |
9 | 4928 |
return true; |
4929 |
} |
|
0 | 4930 |
} |
4931 |
} |
|
4932 |
||
4933 |
return false; |
|
4934 |
} |
|
4935 |
||
4936 |
/** |
|
19 | 4937 |
* Determines if the given object type is associated with the given taxonomy. |
0 | 4938 |
* |
4939 |
* @since 3.0.0 |
|
4940 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4941 |
* @param string $object_type Object type string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4942 |
* @param string $taxonomy Single taxonomy name. |
0 | 4943 |
* @return bool True if object is associated with the taxonomy, otherwise false. |
4944 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4945 |
function is_object_in_taxonomy( $object_type, $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4946 |
$taxonomies = get_object_taxonomies( $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4947 |
if ( empty( $taxonomies ) ) { |
0 | 4948 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4949 |
} |
16 | 4950 |
return in_array( $taxonomy, $taxonomies, true ); |
0 | 4951 |
} |
4952 |
||
4953 |
/** |
|
19 | 4954 |
* Gets an array of ancestor IDs for a given object. |
0 | 4955 |
* |
5 | 4956 |
* @since 3.1.0 |
4957 |
* @since 4.1.0 Introduced the `$resource_type` argument. |
|
4958 |
* |
|
4959 |
* @param int $object_id Optional. The ID of the object. Default 0. |
|
4960 |
* @param string $object_type Optional. The type of object for which we'll be retrieving |
|
4961 |
* ancestors. Accepts a post type or a taxonomy name. Default empty. |
|
4962 |
* @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type' |
|
4963 |
* or 'taxonomy'. Default empty. |
|
16 | 4964 |
* @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy. |
0 | 4965 |
*/ |
5 | 4966 |
function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) { |
0 | 4967 |
$object_id = (int) $object_id; |
4968 |
||
4969 |
$ancestors = array(); |
|
4970 |
||
4971 |
if ( empty( $object_id ) ) { |
|
5 | 4972 |
|
4973 |
/** This filter is documented in wp-includes/taxonomy.php */ |
|
4974 |
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type ); |
|
0 | 4975 |
} |
4976 |
||
5 | 4977 |
if ( ! $resource_type ) { |
4978 |
if ( is_taxonomy_hierarchical( $object_type ) ) { |
|
4979 |
$resource_type = 'taxonomy'; |
|
4980 |
} elseif ( post_type_exists( $object_type ) ) { |
|
4981 |
$resource_type = 'post_type'; |
|
4982 |
} |
|
4983 |
} |
|
4984 |
||
4985 |
if ( 'taxonomy' === $resource_type ) { |
|
9 | 4986 |
$term = get_term( $object_id, $object_type ); |
16 | 4987 |
while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors, true ) ) { |
0 | 4988 |
$ancestors[] = (int) $term->parent; |
9 | 4989 |
$term = get_term( $term->parent, $object_type ); |
0 | 4990 |
} |
5 | 4991 |
} elseif ( 'post_type' === $resource_type ) { |
9 | 4992 |
$ancestors = get_post_ancestors( $object_id ); |
0 | 4993 |
} |
4994 |
||
5 | 4995 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4996 |
* Filters a given object's ancestors. |
5 | 4997 |
* |
4998 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4999 |
* @since 4.1.1 Introduced the `$resource_type` parameter. |
5 | 5000 |
* |
16 | 5001 |
* @param int[] $ancestors An array of IDs of object ancestors. |
5 | 5002 |
* @param int $object_id Object ID. |
5003 |
* @param string $object_type Type of object. |
|
5004 |
* @param string $resource_type Type of resource $object_type is. |
|
5005 |
*/ |
|
5006 |
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type ); |
|
0 | 5007 |
} |
5008 |
||
5009 |
/** |
|
19 | 5010 |
* Returns the term's parent's term ID. |
0 | 5011 |
* |
5012 |
* @since 3.1.0 |
|
5013 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5014 |
* @param int $term_id Term ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5015 |
* @param string $taxonomy Taxonomy name. |
18 | 5016 |
* @return int|false Parent term ID on success, false on failure. |
0 | 5017 |
*/ |
5018 |
function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) { |
|
5019 |
$term = get_term( $term_id, $taxonomy ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5020 |
if ( ! $term || is_wp_error( $term ) ) { |
0 | 5021 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5022 |
} |
0 | 5023 |
return (int) $term->parent; |
5024 |
} |
|
5025 |
||
5026 |
/** |
|
5027 |
* Checks the given subset of the term hierarchy for hierarchy loops. |
|
5028 |
* Prevents loops from forming and breaks those that it finds. |
|
5029 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5030 |
* Attached to the {@see 'wp_update_term_parent'} filter. |
0 | 5031 |
* |
5032 |
* @since 3.1.0 |
|
5033 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5034 |
* @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
|
5035 |
* @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
|
5036 |
* @param string $taxonomy The taxonomy of the term we're checking. |
0 | 5037 |
* @return int The new parent for the term. |
5038 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5039 |
function wp_check_term_hierarchy_for_loops( $parent_term, $term_id, $taxonomy ) { |
16 | 5040 |
// Nothing fancy here - bail. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5041 |
if ( ! $parent_term ) { |
0 | 5042 |
return 0; |
9 | 5043 |
} |
0 | 5044 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5045 |
// 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
|
5046 |
if ( $parent_term === $term_id ) { |
0 | 5047 |
return 0; |
9 | 5048 |
} |
0 | 5049 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5050 |
// 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
|
5051 |
$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent_term, array( $taxonomy ) ); |
16 | 5052 |
if ( ! $loop ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5053 |
return $parent_term; // No loop. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5054 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5055 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5056 |
// Setting $parent_term to the given value causes a loop. |
9 | 5057 |
if ( isset( $loop[ $term_id ] ) ) { |
0 | 5058 |
return 0; |
9 | 5059 |
} |
0 | 5060 |
|
5061 |
// There's a loop, but it doesn't contain $term_id. Break the loop. |
|
9 | 5062 |
foreach ( array_keys( $loop ) as $loop_member ) { |
0 | 5063 |
wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); |
9 | 5064 |
} |
0 | 5065 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5066 |
return $parent_term; |
0 | 5067 |
} |
9 | 5068 |
|
5069 |
/** |
|
5070 |
* Determines whether a taxonomy is considered "viewable". |
|
5071 |
* |
|
5072 |
* @since 5.1.0 |
|
5073 |
* |
|
5074 |
* @param string|WP_Taxonomy $taxonomy Taxonomy name or object. |
|
5075 |
* @return bool Whether the taxonomy should be considered viewable. |
|
5076 |
*/ |
|
5077 |
function is_taxonomy_viewable( $taxonomy ) { |
|
5078 |
if ( is_scalar( $taxonomy ) ) { |
|
5079 |
$taxonomy = get_taxonomy( $taxonomy ); |
|
5080 |
if ( ! $taxonomy ) { |
|
5081 |
return false; |
|
5082 |
} |
|
5083 |
} |
|
5084 |
||
5085 |
return $taxonomy->publicly_queryable; |
|
5086 |
} |
|
5087 |
||
5088 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5089 |
* 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
|
5090 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5091 |
* 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
|
5092 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5093 |
* @since 6.1.0 |
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 |
* @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
|
5096 |
* @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
|
5097 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5098 |
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
|
5099 |
$term = get_term( $term ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5100 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5101 |
if ( ! $term ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5102 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5103 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5104 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5105 |
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
|
5106 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5107 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5108 |
/** |
9 | 5109 |
* Sets the last changed time for the 'terms' cache group. |
5110 |
* |
|
5111 |
* @since 5.0.0 |
|
5112 |
*/ |
|
5113 |
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
|
5114 |
wp_cache_set_last_changed( 'terms' ); |
9 | 5115 |
} |
5116 |
||
5117 |
/** |
|
5118 |
* Aborts calls to term meta if it is not supported. |
|
5119 |
* |
|
5120 |
* @since 5.0.0 |
|
5121 |
* |
|
5122 |
* @param mixed $check Skip-value for whether to proceed term meta function execution. |
|
5123 |
* @return mixed Original value of $check, or false if term meta is not supported. |
|
5124 |
*/ |
|
5125 |
function wp_check_term_meta_support_prefilter( $check ) { |
|
5126 |
if ( get_option( 'db_version' ) < 34370 ) { |
|
5127 |
return false; |
|
5128 |
} |
|
5129 |
||
5130 |
return $check; |
|
5131 |
} |