~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/category.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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