PageRenderTime 327ms CodeModel.GetById 89ms app.highlight 103ms RepoModel.GetById 80ms app.codeStats 0ms

/wp-includes/category.php

https://bitbucket.org/aqge/deptandashboard
PHP | 359 lines | 126 code | 47 blank | 186 comment | 28 complexity | e06c4cf2ad3ebda3e8023c0414e2e6d1 MD5 | raw file
  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 */
 16function 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 */
 39function &get_categories( $args = '' ) {
 40	$defaults = array( 'taxonomy' => 'category' );
 41	$args = wp_parse_args( $args, $defaults );
 42
 43	$taxonomy = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args );
 44
 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
 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 * @uses get_term() Used to get the category data from the taxonomy.
 75 *
 76 * @param int|object $category Category ID or Category row object
 77 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
 78 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
 79 * @return mixed Category data in type defined by $output parameter.
 80 */
 81function &get_category( $category, $output = OBJECT, $filter = 'raw' ) {
 82	$category = get_term( $category, 'category', $output, $filter );
 83	if ( is_wp_error( $category ) )
 84		return $category;
 85
 86	_make_cat_compat( $category );
 87
 88	return $category;
 89}
 90
 91/**
 92 * Retrieve category based on URL containing the category slug.
 93 *
 94 * Breaks the $category_path parameter up to get the category slug.
 95 *
 96 * Tries to find the child path and will return it. If it doesn't find a
 97 * match, then it will return the first category matching slug, if $full_match,
 98 * is set to false. If it does not, then it will return null.
 99 *
100 * It is also possible that it will return a WP_Error object on failure. Check
101 * for it when using this function.
102 *
103 * @since 2.1.0
104 *
105 * @param string $category_path URL containing category slugs.
106 * @param bool $full_match Optional. Whether full path should be matched.
107 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
108 * @return null|object|array Null on failure. Type is based on $output value.
109 */
110function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
111	$category_path = rawurlencode( urldecode( $category_path ) );
112	$category_path = str_replace( '%2F', '/', $category_path );
113	$category_path = str_replace( '%20', ' ', $category_path );
114	$category_paths = '/' . trim( $category_path, '/' );
115	$leaf_path  = sanitize_title( basename( $category_paths ) );
116	$category_paths = explode( '/', $category_paths );
117	$full_path = '';
118	foreach ( (array) $category_paths as $pathdir )
119		$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
120
121	$categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
122
123	if ( empty( $categories ) )
124		return null;
125
126	foreach ( $categories as $category ) {
127		$path = '/' . $leaf_path;
128		$curcategory = $category;
129		while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
130			$curcategory = get_term( $curcategory->parent, 'category' );
131			if ( is_wp_error( $curcategory ) )
132				return $curcategory;
133			$path = '/' . $curcategory->slug . $path;
134		}
135
136		if ( $path == $full_path )
137			return get_category( $category->term_id, $output );
138	}
139
140	// If full matching is not required, return the first cat that matches the leaf.
141	if ( ! $full_match )
142		return get_category( $categories[0]->term_id, $output );
143
144	return null;
145}
146
147/**
148 * Retrieve category object by category slug.
149 *
150 * @since 2.3.0
151 *
152 * @param string $slug The category slug.
153 * @return object Category data object
154 */
155function get_category_by_slug( $slug  ) {
156	$category = get_term_by( 'slug', $slug, 'category' );
157	if ( $category )
158		_make_cat_compat( $category );
159
160	return $category;
161}
162
163
164/**
165 * Retrieve the ID of a category from its name.
166 *
167 * @since 1.0.0
168 *
169 * @param string $cat_name Optional. Default is 'General' and can be any category name.
170 * @return int 0, if failure and ID of category on success.
171 */
172function get_cat_ID( $cat_name='General' ) {
173	$cat = get_term_by( 'name', $cat_name, 'category' );
174	if ( $cat )
175		return $cat->term_id;
176	return 0;
177}
178
179
180/**
181 * Retrieve the name of a category from its ID.
182 *
183 * @since 1.0.0
184 *
185 * @param int $cat_id Category ID
186 * @return string Category name, or an empty string if category doesn't exist.
187 */
188function get_cat_name( $cat_id ) {
189	$cat_id = (int) $cat_id;
190	$category = &get_category( $cat_id );
191	if ( ! $category || is_wp_error( $category ) )
192		return '';
193	return $category->name;
194}
195
196
197/**
198 * Check if a category is an ancestor of another category.
199 *
200 * You can use either an id or the category object for both parameters. If you
201 * use an integer the category will be retrieved.
202 *
203 * @since 2.1.0
204 *
205 * @param int|object $cat1 ID or object to check if this is the parent category.
206 * @param int|object $cat2 The child category.
207 * @return bool Whether $cat2 is child of $cat1
208 */
209function cat_is_ancestor_of( $cat1, $cat2 ) {
210	if ( ! isset($cat1->term_id) )
211		$cat1 = &get_category( $cat1 );
212	if ( ! isset($cat2->parent) )
213		$cat2 = &get_category( $cat2 );
214
215	if ( empty($cat1->term_id) || empty($cat2->parent) )
216		return false;
217	if ( $cat2->parent == $cat1->term_id )
218		return true;
219
220	return cat_is_ancestor_of( $cat1, get_category( $cat2->parent ) );
221}
222
223
224/**
225 * Sanitizes category data based on context.
226 *
227 * @since 2.3.0
228 * @uses sanitize_term() See this function for what context are supported.
229 *
230 * @param object|array $category Category data
231 * @param string $context Optional. Default is 'display'.
232 * @return object|array Same type as $category with sanitized data for safe use.
233 */
234function sanitize_category( $category, $context = 'display' ) {
235	return sanitize_term( $category, 'category', $context );
236}
237
238
239/**
240 * Sanitizes data in single category key field.
241 *
242 * @since 2.3.0
243 * @uses sanitize_term_field() See function for more details.
244 *
245 * @param string $field Category key to sanitize
246 * @param mixed $value Category value to sanitize
247 * @param int $cat_id Category ID
248 * @param string $context What filter to use, 'raw', 'display', etc.
249 * @return mixed Same type as $value after $value has been sanitized.
250 */
251function sanitize_category_field( $field, $value, $cat_id, $context ) {
252	return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
253}
254
255/* Tags */
256
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 */
268function &get_tags( $args = '' ) {
269	$tags = get_terms( 'post_tag', $args );
270
271	if ( empty( $tags ) ) {
272		$return = array();
273		return $return;
274	}
275
276	$tags = apply_filters( 'get_tags', $tags, $args );
277	return $tags;
278}
279
280
281/**
282 * Retrieve post tag by tag ID or tag object.
283 *
284 * If you pass the $tag parameter an object, which is assumed to be the tag row
285 * object retrieved the database. It will cache the tag data.
286 *
287 * If you pass $tag an integer of the tag ID, then that tag will
288 * be retrieved from the database, if it isn't already cached, and pass it back.
289 *
290 * If you look at get_term(), then both types will be passed through several
291 * filters and finally sanitized based on the $filter parameter value.
292 *
293 * @since 2.3.0
294 *
295 * @param int|object $tag
296 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
297 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
298 * @return object|array Return type based on $output value.
299 */
300function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
301	return get_term( $tag, 'post_tag', $output, $filter );
302}
303
304
305/* Cache */
306
307
308/**
309 * Remove the category cache data based on ID.
310 *
311 * @since 2.1.0
312 * @uses clean_term_cache() Clears the cache for the category based on ID
313 *
314 * @param int $id Category ID
315 */
316function clean_category_cache( $id ) {
317	clean_term_cache( $id, 'category' );
318}
319
320
321/**
322 * Update category structure to old pre 2.3 from new taxonomy structure.
323 *
324 * This function was added for the taxonomy support to update the new category
325 * structure with the old category one. This will maintain compatibility with
326 * plugins and themes which depend on the old key or property names.
327 *
328 * The parameter should only be passed a variable and not create the array or
329 * object inline to the parameter. The reason for this is that parameter is
330 * passed by reference and PHP will fail unless it has the variable.
331 *
332 * There is no return value, because everything is updated on the variable you
333 * pass to it. This is one of the features with using pass by reference in PHP.
334 *
335 * @since 2.3.0
336 * @access private
337 *
338 * @param array|object $category Category Row object or array
339 */
340function _make_cat_compat( &$category ) {
341	if ( is_object( $category ) ) {
342		$category->cat_ID = &$category->term_id;
343		$category->category_count = &$category->count;
344		$category->category_description = &$category->description;
345		$category->cat_name = &$category->name;
346		$category->category_nicename = &$category->slug;
347		$category->category_parent = &$category->parent;
348	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
349		$category['cat_ID'] = &$category['term_id'];
350		$category['category_count'] = &$category['count'];
351		$category['category_description'] = &$category['description'];
352		$category['cat_name'] = &$category['name'];
353		$category['category_nicename'] = &$category['slug'];
354		$category['category_parent'] = &$category['parent'];
355	}
356}
357
358
359?>