author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 16 | a86126ab1dd4 |
child 21 | 48c4eec2b7e6 |
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 |
* Taxonomy API: Core category-specific functionality |
0 | 4 |
* |
5 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* @subpackage Taxonomy |
0 | 7 |
*/ |
8 |
||
9 |
/** |
|
16 | 10 |
* Retrieves a list of category objects. |
0 | 11 |
* |
16 | 12 |
* If you set the 'taxonomy' argument to 'link_category', the link categories |
13 |
* will be returned instead. |
|
0 | 14 |
* |
15 |
* @since 2.1.0 |
|
16 | 16 |
* |
0 | 17 |
* @see get_terms() Type of arguments that can be changed. |
18 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* @param string|array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* Optional. Arguments to retrieve categories. See get_terms() for additional options. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* |
16 | 22 |
* @type string $taxonomy Taxonomy to retrieve terms for. Default 'category'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* } |
16 | 24 |
* @return array List of category objects. |
0 | 25 |
*/ |
26 |
function get_categories( $args = '' ) { |
|
27 |
$defaults = array( 'taxonomy' => 'category' ); |
|
9 | 28 |
$args = wp_parse_args( $args, $defaults ); |
0 | 29 |
|
30 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* Filters the taxonomy used to retrieve terms when calling get_categories(). |
0 | 32 |
* |
33 |
* @since 2.7.0 |
|
34 |
* |
|
35 |
* @param string $taxonomy Taxonomy to retrieve terms from. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* @param array $args An array of arguments. See get_terms(). |
0 | 37 |
*/ |
16 | 38 |
$args['taxonomy'] = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args ); |
0 | 39 |
|
16 | 40 |
// Back compat. |
41 |
if ( isset( $args['type'] ) && 'link' === $args['type'] ) { |
|
9 | 42 |
_deprecated_argument( |
43 |
__FUNCTION__, |
|
44 |
'3.0.0', |
|
45 |
sprintf( |
|
16 | 46 |
/* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
9 | 47 |
__( '%1$s is deprecated. Use %2$s instead.' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
'<code>type => link</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
'<code>taxonomy => link_category</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
); |
16 | 52 |
$args['taxonomy'] = 'link_category'; |
0 | 53 |
} |
54 |
||
16 | 55 |
$categories = get_terms( $args ); |
0 | 56 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
if ( is_wp_error( $categories ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
$categories = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
$categories = (array) $categories; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
foreach ( array_keys( $categories ) as $k ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
_make_cat_compat( $categories[ $k ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
} |
0 | 65 |
|
66 |
return $categories; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Retrieves category data given a category ID or category object. |
|
71 |
* |
|
72 |
* If you pass the $category parameter an object, which is assumed to be the |
|
73 |
* category row object retrieved the database. It will cache the category data. |
|
74 |
* |
|
75 |
* If you pass $category an integer of the category ID, then that category will |
|
76 |
* be retrieved from the database, if it isn't already cached, and pass it back. |
|
77 |
* |
|
78 |
* If you look at get_term(), then both types will be passed through several |
|
79 |
* filters and finally sanitized based on the $filter parameter value. |
|
80 |
* |
|
81 |
* @since 1.5.1 |
|
82 |
* |
|
16 | 83 |
* @param int|object $category Category ID or category row object. |
84 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
85 |
* correspond to a WP_Term object, an associative array, or a numeric array, |
|
86 |
* respectively. Default OBJECT. |
|
87 |
* @param string $filter Optional. How to sanitize category fields. Default 'raw'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
* @return object|array|WP_Error|null Category data in type defined by $output parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
* WP_Error if $category is empty, null if it does not exist. |
0 | 90 |
*/ |
91 |
function get_category( $category, $output = OBJECT, $filter = 'raw' ) { |
|
92 |
$category = get_term( $category, 'category', $output, $filter ); |
|
5 | 93 |
|
9 | 94 |
if ( is_wp_error( $category ) ) { |
0 | 95 |
return $category; |
9 | 96 |
} |
0 | 97 |
|
98 |
_make_cat_compat( $category ); |
|
99 |
||
100 |
return $category; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
16 | 104 |
* Retrieves a category based on URL containing the category slug. |
0 | 105 |
* |
106 |
* Breaks the $category_path parameter up to get the category slug. |
|
107 |
* |
|
108 |
* Tries to find the child path and will return it. If it doesn't find a |
|
109 |
* match, then it will return the first category matching slug, if $full_match, |
|
110 |
* is set to false. If it does not, then it will return null. |
|
111 |
* |
|
112 |
* It is also possible that it will return a WP_Error object on failure. Check |
|
113 |
* for it when using this function. |
|
114 |
* |
|
115 |
* @since 2.1.0 |
|
116 |
* |
|
117 |
* @param string $category_path URL containing category slugs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* @param bool $full_match Optional. Whether full path should be matched. |
16 | 119 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
120 |
* correspond to a WP_Term object, an associative array, or a numeric array, |
|
121 |
* respectively. Default OBJECT. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* @return WP_Term|array|WP_Error|null Type is based on $output value. |
0 | 123 |
*/ |
124 |
function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
|
9 | 125 |
$category_path = rawurlencode( urldecode( $category_path ) ); |
126 |
$category_path = str_replace( '%2F', '/', $category_path ); |
|
127 |
$category_path = str_replace( '%20', ' ', $category_path ); |
|
0 | 128 |
$category_paths = '/' . trim( $category_path, '/' ); |
9 | 129 |
$leaf_path = sanitize_title( basename( $category_paths ) ); |
0 | 130 |
$category_paths = explode( '/', $category_paths ); |
9 | 131 |
$full_path = ''; |
16 | 132 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
foreach ( (array) $category_paths as $pathdir ) { |
16 | 134 |
$full_path .= ( '' !== $pathdir ? '/' : '' ) . sanitize_title( $pathdir ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
} |
16 | 136 |
|
9 | 137 |
$categories = get_terms( |
138 |
array( |
|
16 | 139 |
'taxonomy' => 'category', |
140 |
'get' => 'all', |
|
141 |
'slug' => $leaf_path, |
|
9 | 142 |
) |
143 |
); |
|
0 | 144 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
if ( empty( $categories ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
} |
0 | 148 |
|
149 |
foreach ( $categories as $category ) { |
|
9 | 150 |
$path = '/' . $leaf_path; |
0 | 151 |
$curcategory = $category; |
16 | 152 |
while ( ( 0 != $curcategory->parent ) && ( $curcategory->parent != $curcategory->term_id ) ) { |
0 | 153 |
$curcategory = get_term( $curcategory->parent, 'category' ); |
16 | 154 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
if ( is_wp_error( $curcategory ) ) { |
0 | 156 |
return $curcategory; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
} |
16 | 158 |
|
0 | 159 |
$path = '/' . $curcategory->slug . $path; |
160 |
} |
|
161 |
||
162 |
if ( $path == $full_path ) { |
|
163 |
$category = get_term( $category->term_id, 'category', $output ); |
|
164 |
_make_cat_compat( $category ); |
|
16 | 165 |
|
0 | 166 |
return $category; |
167 |
} |
|
168 |
} |
|
169 |
||
170 |
// If full matching is not required, return the first cat that matches the leaf. |
|
171 |
if ( ! $full_match ) { |
|
172 |
$category = get_term( reset( $categories )->term_id, 'category', $output ); |
|
173 |
_make_cat_compat( $category ); |
|
16 | 174 |
|
0 | 175 |
return $category; |
176 |
} |
|
177 |
} |
|
178 |
||
179 |
/** |
|
16 | 180 |
* Retrieves a category object by category slug. |
0 | 181 |
* |
182 |
* @since 2.3.0 |
|
183 |
* |
|
184 |
* @param string $slug The category slug. |
|
16 | 185 |
* @return object|false Category data object on success, false if not found. |
0 | 186 |
*/ |
9 | 187 |
function get_category_by_slug( $slug ) { |
0 | 188 |
$category = get_term_by( 'slug', $slug, 'category' ); |
16 | 189 |
|
9 | 190 |
if ( $category ) { |
0 | 191 |
_make_cat_compat( $category ); |
9 | 192 |
} |
0 | 193 |
|
194 |
return $category; |
|
195 |
} |
|
196 |
||
197 |
/** |
|
16 | 198 |
* Retrieves the ID of a category from its name. |
0 | 199 |
* |
200 |
* @since 1.0.0 |
|
201 |
* |
|
202 |
* @param string $cat_name Category name. |
|
16 | 203 |
* @return int Category ID on success, 0 if the category doesn't exist. |
0 | 204 |
*/ |
16 | 205 |
function get_cat_ID( $cat_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
0 | 206 |
$cat = get_term_by( 'name', $cat_name, 'category' ); |
16 | 207 |
|
9 | 208 |
if ( $cat ) { |
0 | 209 |
return $cat->term_id; |
9 | 210 |
} |
16 | 211 |
|
0 | 212 |
return 0; |
213 |
} |
|
214 |
||
215 |
/** |
|
16 | 216 |
* Retrieves the name of a category from its ID. |
0 | 217 |
* |
218 |
* @since 1.0.0 |
|
219 |
* |
|
16 | 220 |
* @param int $cat_id Category ID. |
221 |
* @return string Category name, or an empty string if the category doesn't exist. |
|
0 | 222 |
*/ |
223 |
function get_cat_name( $cat_id ) { |
|
9 | 224 |
$cat_id = (int) $cat_id; |
0 | 225 |
$category = get_term( $cat_id, 'category' ); |
16 | 226 |
|
9 | 227 |
if ( ! $category || is_wp_error( $category ) ) { |
0 | 228 |
return ''; |
9 | 229 |
} |
16 | 230 |
|
0 | 231 |
return $category->name; |
232 |
} |
|
233 |
||
234 |
/** |
|
16 | 235 |
* Checks if a category is an ancestor of another category. |
0 | 236 |
* |
16 | 237 |
* You can use either an ID or the category object for both parameters. |
238 |
* If you use an integer, the category will be retrieved. |
|
0 | 239 |
* |
240 |
* @since 2.1.0 |
|
241 |
* |
|
242 |
* @param int|object $cat1 ID or object to check if this is the parent category. |
|
243 |
* @param int|object $cat2 The child category. |
|
16 | 244 |
* @return bool Whether $cat2 is child of $cat1. |
0 | 245 |
*/ |
246 |
function cat_is_ancestor_of( $cat1, $cat2 ) { |
|
247 |
return term_is_ancestor_of( $cat1, $cat2, 'category' ); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Sanitizes category data based on context. |
|
252 |
* |
|
253 |
* @since 2.3.0 |
|
254 |
* |
|
16 | 255 |
* @param object|array $category Category data. |
256 |
* @param string $context Optional. Default 'display'. |
|
0 | 257 |
* @return object|array Same type as $category with sanitized data for safe use. |
258 |
*/ |
|
259 |
function sanitize_category( $category, $context = 'display' ) { |
|
260 |
return sanitize_term( $category, 'category', $context ); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Sanitizes data in single category key field. |
|
265 |
* |
|
266 |
* @since 2.3.0 |
|
267 |
* |
|
16 | 268 |
* @param string $field Category key to sanitize. |
269 |
* @param mixed $value Category value to sanitize. |
|
270 |
* @param int $cat_id Category ID. |
|
0 | 271 |
* @param string $context What filter to use, 'raw', 'display', etc. |
272 |
* @return mixed Same type as $value after $value has been sanitized. |
|
273 |
*/ |
|
274 |
function sanitize_category_field( $field, $value, $cat_id, $context ) { |
|
275 |
return sanitize_term_field( $field, $value, $cat_id, 'category', $context ); |
|
276 |
} |
|
277 |
||
278 |
/* Tags */ |
|
279 |
||
280 |
/** |
|
281 |
* Retrieves all post tags. |
|
282 |
* |
|
283 |
* @since 2.3.0 |
|
16 | 284 |
* |
285 |
* @param string|array $args { |
|
286 |
* Optional. Arguments to retrieve tags. See get_terms() for additional options. |
|
0 | 287 |
* |
16 | 288 |
* @type string $taxonomy Taxonomy to retrieve terms for. Default 'post_tag'. |
289 |
* } |
|
290 |
* @return WP_Term[]|int|WP_Error Array of 'post_tag' term objects, a count thereof, |
|
291 |
* or WP_Error if any of the taxonomies do not exist. |
|
0 | 292 |
*/ |
293 |
function get_tags( $args = '' ) { |
|
16 | 294 |
$defaults = array( 'taxonomy' => 'post_tag' ); |
295 |
$args = wp_parse_args( $args, $defaults ); |
|
296 |
||
297 |
$tags = get_terms( $args ); |
|
0 | 298 |
|
299 |
if ( empty( $tags ) ) { |
|
16 | 300 |
$tags = array(); |
301 |
} else { |
|
302 |
/** |
|
303 |
* Filters the array of term objects returned for the 'post_tag' taxonomy. |
|
304 |
* |
|
305 |
* @since 2.3.0 |
|
306 |
* |
|
307 |
* @param WP_Term[]|int|WP_Error $tags Array of 'post_tag' term objects, a count thereof, |
|
308 |
* or WP_Error if any of the taxonomies do not exist. |
|
309 |
* @param array $args An array of arguments. @see get_terms() |
|
310 |
*/ |
|
311 |
$tags = apply_filters( 'get_tags', $tags, $args ); |
|
0 | 312 |
} |
313 |
||
314 |
return $tags; |
|
315 |
} |
|
316 |
||
317 |
/** |
|
16 | 318 |
* Retrieves a post tag by tag ID or tag object. |
0 | 319 |
* |
320 |
* If you pass the $tag parameter an object, which is assumed to be the tag row |
|
16 | 321 |
* object retrieved from the database, it will cache the tag data. |
0 | 322 |
* |
16 | 323 |
* If you pass $tag an integer of the tag ID, then that tag will be retrieved |
324 |
* from the database, if it isn't already cached, and passed back. |
|
0 | 325 |
* |
16 | 326 |
* If you look at get_term(), both types will be passed through several filters |
327 |
* and finally sanitized based on the $filter parameter value. |
|
0 | 328 |
* |
329 |
* @since 2.3.0 |
|
330 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
* @param int|WP_Term|object $tag A tag ID or object. |
16 | 332 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
333 |
* correspond to a WP_Term object, an associative array, or a numeric array, |
|
334 |
* respectively. Default OBJECT. |
|
335 |
* @param string $filter Optional. How to sanitize tag fields. Default 'raw'. |
|
336 |
* @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter. |
|
337 |
* WP_Error if $tag is empty, null if it does not exist. |
|
0 | 338 |
*/ |
339 |
function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
|
340 |
return get_term( $tag, 'post_tag', $output, $filter ); |
|
341 |
} |
|
342 |
||
343 |
/* Cache */ |
|
344 |
||
345 |
/** |
|
16 | 346 |
* Removes the category cache data based on ID. |
0 | 347 |
* |
348 |
* @since 2.1.0 |
|
349 |
* |
|
350 |
* @param int $id Category ID |
|
351 |
*/ |
|
352 |
function clean_category_cache( $id ) { |
|
353 |
clean_term_cache( $id, 'category' ); |
|
354 |
} |
|
355 |
||
356 |
/** |
|
16 | 357 |
* Updates category structure to old pre-2.3 from new taxonomy structure. |
0 | 358 |
* |
359 |
* This function was added for the taxonomy support to update the new category |
|
360 |
* structure with the old category one. This will maintain compatibility with |
|
361 |
* plugins and themes which depend on the old key or property names. |
|
362 |
* |
|
363 |
* The parameter should only be passed a variable and not create the array or |
|
364 |
* object inline to the parameter. The reason for this is that parameter is |
|
365 |
* passed by reference and PHP will fail unless it has the variable. |
|
366 |
* |
|
367 |
* There is no return value, because everything is updated on the variable you |
|
368 |
* pass to it. This is one of the features with using pass by reference in PHP. |
|
369 |
* |
|
370 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
* @since 4.4.0 The `$category` parameter now also accepts a WP_Term object. |
0 | 372 |
* @access private |
373 |
* |
|
16 | 374 |
* @param array|object|WP_Term $category Category row object or array. |
0 | 375 |
*/ |
376 |
function _make_cat_compat( &$category ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
if ( is_object( $category ) && ! is_wp_error( $category ) ) { |
9 | 378 |
$category->cat_ID = $category->term_id; |
379 |
$category->category_count = $category->count; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
$category->category_description = $category->description; |
9 | 381 |
$category->cat_name = $category->name; |
382 |
$category->category_nicename = $category->slug; |
|
383 |
$category->category_parent = $category->parent; |
|
0 | 384 |
} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) { |
9 | 385 |
$category['cat_ID'] = &$category['term_id']; |
386 |
$category['category_count'] = &$category['count']; |
|
0 | 387 |
$category['category_description'] = &$category['description']; |
9 | 388 |
$category['cat_name'] = &$category['name']; |
389 |
$category['category_nicename'] = &$category['slug']; |
|
390 |
$category['category_parent'] = &$category['parent']; |
|
0 | 391 |
} |
392 |
} |