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