13 * |
13 * |
14 * @return object List of all of the category IDs. |
14 * @return object List of all of the category IDs. |
15 */ |
15 */ |
16 function get_all_category_ids() { |
16 function get_all_category_ids() { |
17 if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) { |
17 if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) { |
18 $cat_ids = get_terms( 'category', 'fields=ids&get=all' ); |
18 $cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') ); |
19 wp_cache_add( 'all_category_ids', $cat_ids, 'category' ); |
19 wp_cache_add( 'all_category_ids', $cat_ids, 'category' ); |
20 } |
20 } |
21 |
21 |
22 return $cat_ids; |
22 return $cat_ids; |
23 } |
23 } |
35 * |
35 * |
36 * @param string|array $args Optional. Change the defaults retrieving categories. |
36 * @param string|array $args Optional. Change the defaults retrieving categories. |
37 * @return array List of categories. |
37 * @return array List of categories. |
38 */ |
38 */ |
39 function &get_categories( $args = '' ) { |
39 function &get_categories( $args = '' ) { |
40 $defaults = array( 'type' => 'category' ); |
40 $defaults = array( 'taxonomy' => 'category' ); |
41 $args = wp_parse_args( $args, $defaults ); |
41 $args = wp_parse_args( $args, $defaults ); |
42 |
42 |
43 $taxonomy = apply_filters( 'get_categories_taxonomy', 'category', $args ); |
43 $taxonomy = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args ); |
44 if ( 'link' == $args['type'] ) |
44 |
45 $taxonomy = 'link_category'; |
45 // Back compat |
|
46 if ( isset($args['type']) && 'link' == $args['type'] ) { |
|
47 _deprecated_argument( __FUNCTION__, '3.0', '' ); |
|
48 $taxonomy = $args['taxonomy'] = 'link_category'; |
|
49 } |
|
50 |
46 $categories = (array) get_terms( $taxonomy, $args ); |
51 $categories = (array) get_terms( $taxonomy, $args ); |
47 |
52 |
48 foreach ( array_keys( $categories ) as $k ) |
53 foreach ( array_keys( $categories ) as $k ) |
49 _make_cat_compat( $categories[$k] ); |
54 _make_cat_compat( $categories[$k] ); |
50 |
55 |
96 * for it when using this function. |
101 * for it when using this function. |
97 * |
102 * |
98 * @since 2.1.0 |
103 * @since 2.1.0 |
99 * |
104 * |
100 * @param string $category_path URL containing category slugs. |
105 * @param string $category_path URL containing category slugs. |
101 * @param bool $full_match Optional. Whether should match full path or not. |
106 * @param bool $full_match Optional. Whether full path should be matched. |
102 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N |
107 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N |
103 * @return null|object|array Null on failure. Type is based on $output value. |
108 * @return null|object|array Null on failure. Type is based on $output value. |
104 */ |
109 */ |
105 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
110 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { |
106 $category_path = rawurlencode( urldecode( $category_path ) ); |
111 $category_path = rawurlencode( urldecode( $category_path ) ); |
111 $category_paths = explode( '/', $category_paths ); |
116 $category_paths = explode( '/', $category_paths ); |
112 $full_path = ''; |
117 $full_path = ''; |
113 foreach ( (array) $category_paths as $pathdir ) |
118 foreach ( (array) $category_paths as $pathdir ) |
114 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); |
119 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); |
115 |
120 |
116 $categories = get_terms( 'category', "get=all&slug=$leaf_path" ); |
121 $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) ); |
117 |
122 |
118 if ( empty( $categories ) ) |
123 if ( empty( $categories ) ) |
119 return null; |
124 return null; |
120 |
125 |
121 foreach ( $categories as $category ) { |
126 foreach ( $categories as $category ) { |
169 if ( $cat ) |
173 if ( $cat ) |
170 return $cat->term_id; |
174 return $cat->term_id; |
171 return 0; |
175 return 0; |
172 } |
176 } |
173 |
177 |
174 |
|
175 /** |
178 /** |
176 * Retrieve the name of a category from its ID. |
179 * Retrieve the name of a category from its ID. |
177 * |
180 * |
178 * @since 1.0.0 |
181 * @since 1.0.0 |
179 * |
182 * |
180 * @param int $cat_id Category ID |
183 * @param int $cat_id Category ID |
181 * @return string Category name |
184 * @return string Category name, or an empty string if category doesn't exist. |
182 */ |
185 */ |
183 function get_cat_name( $cat_id ) { |
186 function get_cat_name( $cat_id ) { |
184 $cat_id = (int) $cat_id; |
187 $cat_id = (int) $cat_id; |
185 $category = &get_category( $cat_id ); |
188 $category = &get_category( $cat_id ); |
|
189 if ( ! $category || is_wp_error( $category ) ) |
|
190 return ''; |
186 return $category->name; |
191 return $category->name; |
187 } |
192 } |
188 |
|
189 |
193 |
190 /** |
194 /** |
191 * Check if a category is an ancestor of another category. |
195 * Check if a category is an ancestor of another category. |
192 * |
196 * |
193 * You can use either an id or the category object for both parameters. If you |
197 * You can use either an id or the category object for both parameters. If you |
198 * @param int|object $cat1 ID or object to check if this is the parent category. |
202 * @param int|object $cat1 ID or object to check if this is the parent category. |
199 * @param int|object $cat2 The child category. |
203 * @param int|object $cat2 The child category. |
200 * @return bool Whether $cat2 is child of $cat1 |
204 * @return bool Whether $cat2 is child of $cat1 |
201 */ |
205 */ |
202 function cat_is_ancestor_of( $cat1, $cat2 ) { |
206 function cat_is_ancestor_of( $cat1, $cat2 ) { |
203 if ( ! isset($cat1->term_id) ) |
207 return term_is_ancestor_of( $cat1, $cat2, 'category' ); |
204 $cat1 = &get_category( $cat1 ); |
208 } |
205 if ( ! isset($cat2->parent) ) |
|
206 $cat2 = &get_category( $cat2 ); |
|
207 |
|
208 if ( empty($cat1->term_id) || empty($cat2->parent) ) |
|
209 return false; |
|
210 if ( $cat2->parent == $cat1->term_id ) |
|
211 return true; |
|
212 |
|
213 return cat_is_ancestor_of( $cat1, get_category( $cat2->parent ) ); |
|
214 } |
|
215 |
|
216 |
209 |
217 /** |
210 /** |
218 * Sanitizes category data based on context. |
211 * Sanitizes category data based on context. |
219 * |
212 * |
220 * @since 2.3.0 |
213 * @since 2.3.0 |
225 * @return object|array Same type as $category with sanitized data for safe use. |
218 * @return object|array Same type as $category with sanitized data for safe use. |
226 */ |
219 */ |
227 function sanitize_category( $category, $context = 'display' ) { |
220 function sanitize_category( $category, $context = 'display' ) { |
228 return sanitize_term( $category, 'category', $context ); |
221 return sanitize_term( $category, 'category', $context ); |
229 } |
222 } |
230 |
|
231 |
223 |
232 /** |
224 /** |
233 * Sanitizes data in single category key field. |
225 * Sanitizes data in single category key field. |
234 * |
226 * |
235 * @since 2.3.0 |
227 * @since 2.3.0 |
245 return sanitize_term_field( $field, $value, $cat_id, 'category', $context ); |
237 return sanitize_term_field( $field, $value, $cat_id, 'category', $context ); |
246 } |
238 } |
247 |
239 |
248 /* Tags */ |
240 /* Tags */ |
249 |
241 |
250 |
|
251 /** |
242 /** |
252 * Retrieves all post tags. |
243 * Retrieves all post tags. |
253 * |
244 * |
254 * @since 2.3.0 |
245 * @since 2.3.0 |
255 * @see get_terms() For list of arguments to pass. |
246 * @see get_terms() For list of arguments to pass. |
292 */ |
282 */ |
293 function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
283 function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) { |
294 return get_term( $tag, 'post_tag', $output, $filter ); |
284 return get_term( $tag, 'post_tag', $output, $filter ); |
295 } |
285 } |
296 |
286 |
297 |
|
298 /* Cache */ |
287 /* Cache */ |
299 |
|
300 |
|
301 /** |
|
302 * Update the categories cache. |
|
303 * |
|
304 * This function does not appear to be used anymore or does not appear to be |
|
305 * needed. It might be a legacy function left over from when there was a need |
|
306 * for updating the category cache. |
|
307 * |
|
308 * @since 1.5.0 |
|
309 * |
|
310 * @return bool Always return True |
|
311 */ |
|
312 function update_category_cache() { |
|
313 return true; |
|
314 } |
|
315 |
|
316 |
288 |
317 /** |
289 /** |
318 * Remove the category cache data based on ID. |
290 * Remove the category cache data based on ID. |
319 * |
291 * |
320 * @since 2.1.0 |
292 * @since 2.1.0 |
323 * @param int $id Category ID |
295 * @param int $id Category ID |
324 */ |
296 */ |
325 function clean_category_cache( $id ) { |
297 function clean_category_cache( $id ) { |
326 clean_term_cache( $id, 'category' ); |
298 clean_term_cache( $id, 'category' ); |
327 } |
299 } |
328 |
|
329 |
300 |
330 /** |
301 /** |
331 * Update category structure to old pre 2.3 from new taxonomy structure. |
302 * Update category structure to old pre 2.3 from new taxonomy structure. |
332 * |
303 * |
333 * This function was added for the taxonomy support to update the new category |
304 * This function was added for the taxonomy support to update the new category |