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