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