web/wp-includes/category.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    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 ) {
   153 		_make_cat_compat( $category );
   158 		_make_cat_compat( $category );
   154 
   159 
   155 	return $category;
   160 	return $category;
   156 }
   161 }
   157 
   162 
   158 
       
   159 /**
   163 /**
   160  * Retrieve the ID of a category from its name.
   164  * Retrieve the ID of a category from its name.
   161  *
   165  *
   162  * @since 1.0.0
   166  * @since 1.0.0
   163  *
   167  *
   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.
   267 	}
   258 	}
   268 
   259 
   269 	$tags = apply_filters( 'get_tags', $tags, $args );
   260 	$tags = apply_filters( 'get_tags', $tags, $args );
   270 	return $tags;
   261 	return $tags;
   271 }
   262 }
   272 
       
   273 
   263 
   274 /**
   264 /**
   275  * Retrieve post tag by tag ID or tag object.
   265  * Retrieve post tag by tag ID or tag object.
   276  *
   266  *
   277  * If you pass the $tag parameter an object, which is assumed to be the tag row
   267  * If you pass the $tag parameter an object, which is assumed to be the tag row
   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
   361 		$category['cat_name'] = &$category['name'];
   332 		$category['cat_name'] = &$category['name'];
   362 		$category['category_nicename'] = &$category['slug'];
   333 		$category['category_nicename'] = &$category['slug'];
   363 		$category['category_parent'] = &$category['parent'];
   334 		$category['category_parent'] = &$category['parent'];
   364 	}
   335 	}
   365 }
   336 }
   366 
       
   367 
       
   368 ?>