5 * @package WordPress |
5 * @package WordPress |
6 * @subpackage Taxonomy |
6 * @subpackage Taxonomy |
7 */ |
7 */ |
8 |
8 |
9 /** |
9 /** |
10 * Retrieve list of category objects. |
10 * Retrieves a list of category objects. |
11 * |
11 * |
12 * If you change the type to 'link' in the arguments, then the link categories |
12 * If you set the 'taxonomy' argument to 'link_category', the link categories |
13 * will be returned instead. Also all categories will be updated to be backward |
13 * will be returned instead. |
14 * compatible with pre-2.3 plugins and themes. |
|
15 * |
14 * |
16 * @since 2.1.0 |
15 * @since 2.1.0 |
|
16 * |
17 * @see get_terms() Type of arguments that can be changed. |
17 * @see get_terms() Type of arguments that can be changed. |
18 * |
18 * |
19 * @param string|array $args { |
19 * @param string|array $args { |
20 * Optional. Arguments to retrieve categories. See get_terms() for additional options. |
20 * Optional. Arguments to retrieve categories. See get_terms() for additional options. |
21 * |
21 * |
22 * @type string $taxonomy Taxonomy to retrieve terms for. In this case, default 'category'. |
22 * @type string $taxonomy Taxonomy to retrieve terms for. Default 'category'. |
23 * } |
23 * } |
24 * @return array List of categories. |
24 * @return array List of category objects. |
25 */ |
25 */ |
26 function get_categories( $args = '' ) { |
26 function get_categories( $args = '' ) { |
27 $defaults = array( 'taxonomy' => 'category' ); |
27 $defaults = array( 'taxonomy' => 'category' ); |
28 $args = wp_parse_args( $args, $defaults ); |
28 $args = wp_parse_args( $args, $defaults ); |
29 |
|
30 $taxonomy = $args['taxonomy']; |
|
31 |
29 |
32 /** |
30 /** |
33 * Filters the taxonomy used to retrieve terms when calling get_categories(). |
31 * Filters the taxonomy used to retrieve terms when calling get_categories(). |
34 * |
32 * |
35 * @since 2.7.0 |
33 * @since 2.7.0 |
36 * |
34 * |
37 * @param string $taxonomy Taxonomy to retrieve terms from. |
35 * @param string $taxonomy Taxonomy to retrieve terms from. |
38 * @param array $args An array of arguments. See get_terms(). |
36 * @param array $args An array of arguments. See get_terms(). |
39 */ |
37 */ |
40 $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args ); |
38 $args['taxonomy'] = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args ); |
41 |
39 |
42 // Back compat |
40 // Back compat. |
43 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
41 if ( isset( $args['type'] ) && 'link' === $args['type'] ) { |
44 _deprecated_argument( |
42 _deprecated_argument( |
45 __FUNCTION__, |
43 __FUNCTION__, |
46 '3.0.0', |
44 '3.0.0', |
47 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
|
48 sprintf( |
45 sprintf( |
|
46 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
49 __( '%1$s is deprecated. Use %2$s instead.' ), |
47 __( '%1$s is deprecated. Use %2$s instead.' ), |
50 '<code>type => link</code>', |
48 '<code>type => link</code>', |
51 '<code>taxonomy => link_category</code>' |
49 '<code>taxonomy => link_category</code>' |
52 ) |
50 ) |
53 ); |
51 ); |
54 $taxonomy = $args['taxonomy'] = 'link_category'; |
52 $args['taxonomy'] = 'link_category'; |
55 } |
53 } |
56 |
54 |
57 $categories = get_terms( $taxonomy, $args ); |
55 $categories = get_terms( $args ); |
58 |
56 |
59 if ( is_wp_error( $categories ) ) { |
57 if ( is_wp_error( $categories ) ) { |
60 $categories = array(); |
58 $categories = array(); |
61 } else { |
59 } else { |
62 $categories = (array) $categories; |
60 $categories = (array) $categories; |
78 * be retrieved from the database, if it isn't already cached, and pass it back. |
76 * be retrieved from the database, if it isn't already cached, and pass it back. |
79 * |
77 * |
80 * If you look at get_term(), then both types will be passed through several |
78 * 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. |
79 * filters and finally sanitized based on the $filter parameter value. |
82 * |
80 * |
83 * The category will converted to maintain backward compatibility. |
|
84 * |
|
85 * @since 1.5.1 |
81 * @since 1.5.1 |
86 * |
82 * |
87 * @param int|object $category Category ID or Category row object |
83 * @param int|object $category Category ID or category row object. |
88 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a |
84 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
89 * WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. |
85 * correspond to a WP_Term object, an associative array, or a numeric array, |
90 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. |
86 * respectively. Default OBJECT. |
|
87 * @param string $filter Optional. How to sanitize category fields. Default 'raw'. |
91 * @return object|array|WP_Error|null Category data in type defined by $output parameter. |
88 * @return object|array|WP_Error|null Category data in type defined by $output parameter. |
92 * WP_Error if $category is empty, null if it does not exist. |
89 * WP_Error if $category is empty, null if it does not exist. |
93 */ |
90 */ |
94 function get_category( $category, $output = OBJECT, $filter = 'raw' ) { |
91 function get_category( $category, $output = OBJECT, $filter = 'raw' ) { |
95 $category = get_term( $category, 'category', $output, $filter ); |
92 $category = get_term( $category, 'category', $output, $filter ); |
117 * |
114 * |
118 * @since 2.1.0 |
115 * @since 2.1.0 |
119 * |
116 * |
120 * @param string $category_path URL containing category slugs. |
117 * @param string $category_path URL containing category slugs. |
121 * @param bool $full_match Optional. Whether full path should be matched. |
118 * @param bool $full_match Optional. Whether full path should be matched. |
122 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
119 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
123 * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. |
120 * correspond to a WP_Term object, an associative array, or a numeric array, |
|
121 * respectively. Default OBJECT. |
124 * @return WP_Term|array|WP_Error|null Type is based on $output value. |
122 * @return WP_Term|array|WP_Error|null Type is based on $output value. |
125 */ |
123 */ |
126 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
124 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
127 $category_path = rawurlencode( urldecode( $category_path ) ); |
125 $category_path = rawurlencode( urldecode( $category_path ) ); |
128 $category_path = str_replace( '%2F', '/', $category_path ); |
126 $category_path = str_replace( '%2F', '/', $category_path ); |
129 $category_path = str_replace( '%20', ' ', $category_path ); |
127 $category_path = str_replace( '%20', ' ', $category_path ); |
130 $category_paths = '/' . trim( $category_path, '/' ); |
128 $category_paths = '/' . trim( $category_path, '/' ); |
131 $leaf_path = sanitize_title( basename( $category_paths ) ); |
129 $leaf_path = sanitize_title( basename( $category_paths ) ); |
132 $category_paths = explode( '/', $category_paths ); |
130 $category_paths = explode( '/', $category_paths ); |
133 $full_path = ''; |
131 $full_path = ''; |
|
132 |
134 foreach ( (array) $category_paths as $pathdir ) { |
133 foreach ( (array) $category_paths as $pathdir ) { |
135 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); |
134 $full_path .= ( '' !== $pathdir ? '/' : '' ) . sanitize_title( $pathdir ); |
136 } |
135 } |
|
136 |
137 $categories = get_terms( |
137 $categories = get_terms( |
138 'category', |
|
139 array( |
138 array( |
140 'get' => 'all', |
139 'taxonomy' => 'category', |
141 'slug' => $leaf_path, |
140 'get' => 'all', |
|
141 'slug' => $leaf_path, |
142 ) |
142 ) |
143 ); |
143 ); |
144 |
144 |
145 if ( empty( $categories ) ) { |
145 if ( empty( $categories ) ) { |
146 return; |
146 return; |
147 } |
147 } |
148 |
148 |
149 foreach ( $categories as $category ) { |
149 foreach ( $categories as $category ) { |
150 $path = '/' . $leaf_path; |
150 $path = '/' . $leaf_path; |
151 $curcategory = $category; |
151 $curcategory = $category; |
152 while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) { |
152 while ( ( 0 != $curcategory->parent ) && ( $curcategory->parent != $curcategory->term_id ) ) { |
153 $curcategory = get_term( $curcategory->parent, 'category' ); |
153 $curcategory = get_term( $curcategory->parent, 'category' ); |
|
154 |
154 if ( is_wp_error( $curcategory ) ) { |
155 if ( is_wp_error( $curcategory ) ) { |
155 return $curcategory; |
156 return $curcategory; |
156 } |
157 } |
|
158 |
157 $path = '/' . $curcategory->slug . $path; |
159 $path = '/' . $curcategory->slug . $path; |
158 } |
160 } |
159 |
161 |
160 if ( $path == $full_path ) { |
162 if ( $path == $full_path ) { |
161 $category = get_term( $category->term_id, 'category', $output ); |
163 $category = get_term( $category->term_id, 'category', $output ); |
162 _make_cat_compat( $category ); |
164 _make_cat_compat( $category ); |
|
165 |
163 return $category; |
166 return $category; |
164 } |
167 } |
165 } |
168 } |
166 |
169 |
167 // If full matching is not required, return the first cat that matches the leaf. |
170 // If full matching is not required, return the first cat that matches the leaf. |
168 if ( ! $full_match ) { |
171 if ( ! $full_match ) { |
169 $category = get_term( reset( $categories )->term_id, 'category', $output ); |
172 $category = get_term( reset( $categories )->term_id, 'category', $output ); |
170 _make_cat_compat( $category ); |
173 _make_cat_compat( $category ); |
|
174 |
171 return $category; |
175 return $category; |
172 } |
176 } |
173 } |
177 } |
174 |
178 |
175 /** |
179 /** |
176 * Retrieve category object by category slug. |
180 * Retrieves a category object by category slug. |
177 * |
181 * |
178 * @since 2.3.0 |
182 * @since 2.3.0 |
179 * |
183 * |
180 * @param string $slug The category slug. |
184 * @param string $slug The category slug. |
181 * @return object Category data object |
185 * @return object|false Category data object on success, false if not found. |
182 */ |
186 */ |
183 function get_category_by_slug( $slug ) { |
187 function get_category_by_slug( $slug ) { |
184 $category = get_term_by( 'slug', $slug, 'category' ); |
188 $category = get_term_by( 'slug', $slug, 'category' ); |
|
189 |
185 if ( $category ) { |
190 if ( $category ) { |
186 _make_cat_compat( $category ); |
191 _make_cat_compat( $category ); |
187 } |
192 } |
188 |
193 |
189 return $category; |
194 return $category; |
190 } |
195 } |
191 |
196 |
192 /** |
197 /** |
193 * Retrieve the ID of a category from its name. |
198 * Retrieves the ID of a category from its name. |
194 * |
199 * |
195 * @since 1.0.0 |
200 * @since 1.0.0 |
196 * |
201 * |
197 * @param string $cat_name Category name. |
202 * @param string $cat_name Category name. |
198 * @return int 0, if failure and ID of category on success. |
203 * @return int Category ID on success, 0 if the category doesn't exist. |
199 */ |
204 */ |
200 function get_cat_ID( $cat_name ) { |
205 function get_cat_ID( $cat_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
201 $cat = get_term_by( 'name', $cat_name, 'category' ); |
206 $cat = get_term_by( 'name', $cat_name, 'category' ); |
|
207 |
202 if ( $cat ) { |
208 if ( $cat ) { |
203 return $cat->term_id; |
209 return $cat->term_id; |
204 } |
210 } |
|
211 |
205 return 0; |
212 return 0; |
206 } |
213 } |
207 |
214 |
208 /** |
215 /** |
209 * Retrieve the name of a category from its ID. |
216 * Retrieves the name of a category from its ID. |
210 * |
217 * |
211 * @since 1.0.0 |
218 * @since 1.0.0 |
212 * |
219 * |
213 * @param int $cat_id Category ID |
220 * @param int $cat_id Category ID. |
214 * @return string Category name, or an empty string if category doesn't exist. |
221 * @return string Category name, or an empty string if the category doesn't exist. |
215 */ |
222 */ |
216 function get_cat_name( $cat_id ) { |
223 function get_cat_name( $cat_id ) { |
217 $cat_id = (int) $cat_id; |
224 $cat_id = (int) $cat_id; |
218 $category = get_term( $cat_id, 'category' ); |
225 $category = get_term( $cat_id, 'category' ); |
|
226 |
219 if ( ! $category || is_wp_error( $category ) ) { |
227 if ( ! $category || is_wp_error( $category ) ) { |
220 return ''; |
228 return ''; |
221 } |
229 } |
|
230 |
222 return $category->name; |
231 return $category->name; |
223 } |
232 } |
224 |
233 |
225 /** |
234 /** |
226 * Check if a category is an ancestor of another category. |
235 * Checks if a category is an ancestor of another category. |
227 * |
236 * |
228 * You can use either an id or the category object for both parameters. If you |
237 * You can use either an ID or the category object for both parameters. |
229 * use an integer the category will be retrieved. |
238 * If you use an integer, the category will be retrieved. |
230 * |
239 * |
231 * @since 2.1.0 |
240 * @since 2.1.0 |
232 * |
241 * |
233 * @param int|object $cat1 ID or object to check if this is the parent category. |
242 * @param int|object $cat1 ID or object to check if this is the parent category. |
234 * @param int|object $cat2 The child category. |
243 * @param int|object $cat2 The child category. |
235 * @return bool Whether $cat2 is child of $cat1 |
244 * @return bool Whether $cat2 is child of $cat1. |
236 */ |
245 */ |
237 function cat_is_ancestor_of( $cat1, $cat2 ) { |
246 function cat_is_ancestor_of( $cat1, $cat2 ) { |
238 return term_is_ancestor_of( $cat1, $cat2, 'category' ); |
247 return term_is_ancestor_of( $cat1, $cat2, 'category' ); |
239 } |
248 } |
240 |
249 |
241 /** |
250 /** |
242 * Sanitizes category data based on context. |
251 * Sanitizes category data based on context. |
243 * |
252 * |
244 * @since 2.3.0 |
253 * @since 2.3.0 |
245 * |
254 * |
246 * @param object|array $category Category data |
255 * @param object|array $category Category data. |
247 * @param string $context Optional. Default is 'display'. |
256 * @param string $context Optional. Default 'display'. |
248 * @return object|array Same type as $category with sanitized data for safe use. |
257 * @return object|array Same type as $category with sanitized data for safe use. |
249 */ |
258 */ |
250 function sanitize_category( $category, $context = 'display' ) { |
259 function sanitize_category( $category, $context = 'display' ) { |
251 return sanitize_term( $category, 'category', $context ); |
260 return sanitize_term( $category, 'category', $context ); |
252 } |
261 } |
270 |
279 |
271 /** |
280 /** |
272 * Retrieves all post tags. |
281 * Retrieves all post tags. |
273 * |
282 * |
274 * @since 2.3.0 |
283 * @since 2.3.0 |
275 * @see get_terms() For list of arguments to pass. |
284 * |
276 * |
285 * @param string|array $args { |
277 * @param string|array $args Tag arguments to use when retrieving tags. |
286 * Optional. Arguments to retrieve tags. See get_terms() for additional options. |
278 * @return WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof. |
287 * |
|
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. |
279 */ |
292 */ |
280 function get_tags( $args = '' ) { |
293 function get_tags( $args = '' ) { |
281 $tags = get_terms( 'post_tag', $args ); |
294 $defaults = array( 'taxonomy' => 'post_tag' ); |
|
295 $args = wp_parse_args( $args, $defaults ); |
|
296 |
|
297 $tags = get_terms( $args ); |
282 |
298 |
283 if ( empty( $tags ) ) { |
299 if ( empty( $tags ) ) { |
284 $return = array(); |
300 $tags = array(); |
285 return $return; |
301 } else { |
286 } |
302 /** |
287 |
303 * Filters the array of term objects returned for the 'post_tag' taxonomy. |
288 /** |
304 * |
289 * Filters the array of term objects returned for the 'post_tag' taxonomy. |
305 * @since 2.3.0 |
290 * |
306 * |
291 * @since 2.3.0 |
307 * @param WP_Term[]|int|WP_Error $tags Array of 'post_tag' term objects, a count thereof, |
292 * |
308 * or WP_Error if any of the taxonomies do not exist. |
293 * @param WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof. |
309 * @param array $args An array of arguments. @see get_terms() |
294 * @param array $args An array of arguments. @see get_terms() |
310 */ |
295 */ |
311 $tags = apply_filters( 'get_tags', $tags, $args ); |
296 $tags = apply_filters( 'get_tags', $tags, $args ); |
312 } |
|
313 |
297 return $tags; |
314 return $tags; |
298 } |
315 } |
299 |
316 |
300 /** |
317 /** |
301 * Retrieve post tag by tag ID or tag object. |
318 * Retrieves a post tag by tag ID or tag object. |
302 * |
319 * |
303 * If you pass the $tag parameter an object, which is assumed to be the tag row |
320 * 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. |
321 * object retrieved from the database, it will cache the tag data. |
305 * |
322 * |
306 * If you pass $tag an integer of the tag ID, then that tag will |
323 * If you pass $tag an integer of the tag ID, then that tag will be retrieved |
307 * be retrieved from the database, if it isn't already cached, and pass it back. |
324 * from the database, if it isn't already cached, and passed back. |
308 * |
325 * |
309 * If you look at get_term(), then both types will be passed through several |
326 * If you look at get_term(), both types will be passed through several filters |
310 * filters and finally sanitized based on the $filter parameter value. |
327 * and finally sanitized based on the $filter parameter value. |
311 * |
328 * |
312 * @since 2.3.0 |
329 * @since 2.3.0 |
313 * |
330 * |
314 * @param int|WP_Term|object $tag A tag ID or object. |
331 * @param int|WP_Term|object $tag A tag ID or object. |
315 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
332 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
316 * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. |
333 * correspond to a WP_Term object, an associative array, or a numeric array, |
317 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied. |
334 * respectively. Default OBJECT. |
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. |
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. |
319 */ |
338 */ |
320 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
339 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
321 return get_term( $tag, 'post_tag', $output, $filter ); |
340 return get_term( $tag, 'post_tag', $output, $filter ); |
322 } |
341 } |
323 |
342 |
324 /* Cache */ |
343 /* Cache */ |
325 |
344 |
326 /** |
345 /** |
327 * Remove the category cache data based on ID. |
346 * Removes the category cache data based on ID. |
328 * |
347 * |
329 * @since 2.1.0 |
348 * @since 2.1.0 |
330 * |
349 * |
331 * @param int $id Category ID |
350 * @param int $id Category ID |
332 */ |
351 */ |
333 function clean_category_cache( $id ) { |
352 function clean_category_cache( $id ) { |
334 clean_term_cache( $id, 'category' ); |
353 clean_term_cache( $id, 'category' ); |
335 } |
354 } |
336 |
355 |
337 /** |
356 /** |
338 * Update category structure to old pre 2.3 from new taxonomy structure. |
357 * Updates category structure to old pre-2.3 from new taxonomy structure. |
339 * |
358 * |
340 * This function was added for the taxonomy support to update the new category |
359 * 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 |
360 * structure with the old category one. This will maintain compatibility with |
342 * plugins and themes which depend on the old key or property names. |
361 * plugins and themes which depend on the old key or property names. |
343 * |
362 * |