-
+ ABC9224B4A497BB631EED64922858F8C6854D2FF061832A104638C87A37F225AD34380485E0281F0BCE639684F56479462A93991614D8E65E14EC88E9B38B11Bmp-wp/wp-includes/category.php(0 . 0)(1 . 384)
75116 <?php
75117 /**
75118 * WordPress Category API
75119 *
75120 * @package WordPress
75121 */
75122
75123 /**
75124 * Retrieves all category IDs.
75125 *
75126 * @since 2.0.0
75127 * @link http://codex.wordpress.org/Function_Reference/get_all_category_ids
75128 *
75129 * @return object List of all of the category IDs.
75130 */
75131 function get_all_category_ids() {
75132 if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) {
75133 $cat_ids = get_terms( 'category', 'fields=ids&get=all' );
75134 wp_cache_add( 'all_category_ids', $cat_ids, 'category' );
75135 }
75136
75137 return $cat_ids;
75138 }
75139
75140 /**
75141 * Retrieve list of category objects.
75142 *
75143 * If you change the type to 'link' in the arguments, then the link categories
75144 * will be returned instead. Also all categories will be updated to be backwards
75145 * compatible with pre-2.3 plugins and themes.
75146 *
75147 * @since 2.1.0
75148 * @see get_terms() Type of arguments that can be changed.
75149 * @link http://codex.wordpress.org/Function_Reference/get_categories
75150 *
75151 * @param string|array $args Optional. Change the defaults retrieving categories.
75152 * @return array List of categories.
75153 */
75154 function &get_categories( $args = '' ) {
75155 $defaults = array( 'type' => 'category' );
75156 $args = wp_parse_args( $args, $defaults );
75157
75158 $taxonomy = apply_filters( 'get_categories_taxonomy', 'category', $args );
75159 if ( 'link' == $args['type'] )
75160 $taxonomy = 'link_category';
75161 $categories = (array) get_terms( $taxonomy, $args );
75162
75163 foreach ( array_keys( $categories ) as $k )
75164 _make_cat_compat( $categories[$k] );
75165
75166 return $categories;
75167 }
75168
75169 /**
75170 * Retrieves category data given a category ID or category object.
75171 *
75172 * If you pass the $category parameter an object, which is assumed to be the
75173 * category row object retrieved the database. It will cache the category data.
75174 *
75175 * If you pass $category an integer of the category ID, then that category will
75176 * be retrieved from the database, if it isn't already cached, and pass it back.
75177 *
75178 * If you look at get_term(), then both types will be passed through several
75179 * filters and finally sanitized based on the $filter parameter value.
75180 *
75181 * The category will converted to maintain backwards compatibility.
75182 *
75183 * @since 1.5.1
75184 * @uses get_term() Used to get the category data from the taxonomy.
75185 *
75186 * @param int|object $category Category ID or Category row object
75187 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
75188 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
75189 * @return mixed Category data in type defined by $output parameter.
75190 */
75191 function &get_category( $category, $output = OBJECT, $filter = 'raw' ) {
75192 $category = get_term( $category, 'category', $output, $filter );
75193 if ( is_wp_error( $category ) )
75194 return $category;
75195
75196 _make_cat_compat( $category );
75197
75198 return $category;
75199 }
75200
75201 /**
75202 * Retrieve category based on URL containing the category slug.
75203 *
75204 * Breaks the $category_path parameter up to get the category slug.
75205 *
75206 * Tries to find the child path and will return it. If it doesn't find a
75207 * match, then it will return the first category matching slug, if $full_match,
75208 * is set to false. If it does not, then it will return null.
75209 *
75210 * It is also possible that it will return a WP_Error object on failure. Check
75211 * for it when using this function.
75212 *
75213 * @since 2.1.0
75214 *
75215 * @param string $category_path URL containing category slugs.
75216 * @param bool $full_match Optional. Whether should match full path or not.
75217 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
75218 * @return null|object|array Null on failure. Type is based on $output value.
75219 */
75220 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
75221 $category_path = rawurlencode( urldecode( $category_path ) );
75222 $category_path = str_replace( '%2F', '/', $category_path );
75223 $category_path = str_replace( '%20', ' ', $category_path );
75224 $category_paths = '/' . trim( $category_path, '/' );
75225 $leaf_path = sanitize_title( basename( $category_paths ) );
75226 $category_paths = explode( '/', $category_paths );
75227 $full_path = '';
75228 foreach ( (array) $category_paths as $pathdir )
75229 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
75230
75231 $categories = get_terms( 'category', "get=all&slug=$leaf_path" );
75232
75233 if ( empty( $categories ) )
75234 return null;
75235
75236 foreach ( $categories as $category ) {
75237 $path = '/' . $leaf_path;
75238 $curcategory = $category;
75239 while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
75240 $curcategory = get_term( $curcategory->parent, 'category' );
75241 if ( is_wp_error( $curcategory ) )
75242 return $curcategory;
75243 $path = '/' . $curcategory->slug . $path;
75244 }
75245
75246 if ( $path == $full_path )
75247 return get_category( $category->term_id, $output );
75248 }
75249
75250 // If full matching is not required, return the first cat that matches the leaf.
75251 if ( ! $full_match )
75252 return get_category( $categories[0]->term_id, $output );
75253
75254 return null;
75255 }
75256
75257 /**
75258 * Retrieve category object by category slug.
75259 *
75260 * @since 2.3.0
75261 *
75262 * @param string $slug The category slug.
75263 * @return object Category data object
75264 */
75265 function get_category_by_slug( $slug ) {
75266 $category = get_term_by( 'slug', $slug, 'category' );
75267 if ( $category )
75268 _make_cat_compat( $category );
75269
75270 return $category;
75271 }
75272
75273
75274 /**
75275 * Retrieve the ID of a category from its name.
75276 *
75277 * @since 1.0.0
75278 *
75279 * @param string $cat_name Optional. Default is 'General' and can be any category name.
75280 * @return int 0, if failure and ID of category on success.
75281 */
75282 function get_cat_ID( $cat_name='General' ) {
75283 $cat = get_term_by( 'name', $cat_name, 'category' );
75284 if ( $cat )
75285 return $cat->term_id;
75286 return 0;
75287 }
75288
75289
75290 /**
75291 * Retrieve the category name by the category ID.
75292 *
75293 * @since 0.71
75294 * @deprecated Use get_cat_name()
75295 * @see get_cat_name() get_catname() is deprecated in favor of get_cat_name().
75296 *
75297 * @param int $cat_ID Category ID
75298 * @return string category name
75299 */
75300 function get_catname( $cat_ID ) {
75301 return get_cat_name( $cat_ID );
75302 }
75303
75304
75305 /**
75306 * Retrieve the name of a category from its ID.
75307 *
75308 * @since 1.0.0
75309 *
75310 * @param int $cat_id Category ID
75311 * @return string Category name
75312 */
75313 function get_cat_name( $cat_id ) {
75314 $cat_id = (int) $cat_id;
75315 $category = &get_category( $cat_id );
75316 return $category->name;
75317 }
75318
75319
75320 /**
75321 * Check if a category is an ancestor of another category.
75322 *
75323 * You can use either an id or the category object for both parameters. If you
75324 * use an integer the category will be retrieved.
75325 *
75326 * @since 2.1.0
75327 *
75328 * @param int|object $cat1 ID or object to check if this is the parent category.
75329 * @param int|object $cat2 The child category.
75330 * @return bool Whether $cat2 is child of $cat1
75331 */
75332 function cat_is_ancestor_of( $cat1, $cat2 ) {
75333 if ( is_int( $cat1 ) )
75334 $cat1 = &get_category( $cat1 );
75335 if ( is_int( $cat2 ) )
75336 $cat2 = &get_category( $cat2 );
75337
75338 if ( !$cat1->term_id || !$cat2->parent )
75339 return false;
75340
75341 if ( $cat2->parent == $cat1->term_id )
75342 return true;
75343
75344 return cat_is_ancestor_of( $cat1, get_category( $cat2->parent ) );
75345 }
75346
75347
75348 /**
75349 * Sanitizes category data based on context.
75350 *
75351 * @since 2.3.0
75352 * @uses sanitize_term() See this function for what context are supported.
75353 *
75354 * @param object|array $category Category data
75355 * @param string $context Optional. Default is 'display'.
75356 * @return object|array Same type as $category with sanitized data for safe use.
75357 */
75358 function sanitize_category( $category, $context = 'display' ) {
75359 return sanitize_term( $category, 'category', $context );
75360 }
75361
75362
75363 /**
75364 * Sanitizes data in single category key field.
75365 *
75366 * @since 2.3.0
75367 * @uses sanitize_term_field() See function for more details.
75368 *
75369 * @param string $field Category key to sanitize
75370 * @param mixed $value Category value to sanitize
75371 * @param int $cat_id Category ID
75372 * @param string $context What filter to use, 'raw', 'display', etc.
75373 * @return mixed Same type as $value after $value has been sanitized.
75374 */
75375 function sanitize_category_field( $field, $value, $cat_id, $context ) {
75376 return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
75377 }
75378
75379 /* Tags */
75380
75381
75382 /**
75383 * Retrieves all post tags.
75384 *
75385 * @since 2.3.0
75386 * @see get_terms() For list of arguments to pass.
75387 * @uses apply_filters() Calls 'get_tags' hook on array of tags and with $args.
75388 *
75389 * @param string|array $args Tag arguments to use when retrieving tags.
75390 * @return array List of tags.
75391 */
75392 function &get_tags( $args = '' ) {
75393 $tags = get_terms( 'post_tag', $args );
75394
75395 if ( empty( $tags ) ) {
75396 $return = array();
75397 return $return;
75398 }
75399
75400 $tags = apply_filters( 'get_tags', $tags, $args );
75401 return $tags;
75402 }
75403
75404
75405 /**
75406 * Retrieve post tag by tag ID or tag object.
75407 *
75408 * If you pass the $tag parameter an object, which is assumed to be the tag row
75409 * object retrieved the database. It will cache the tag data.
75410 *
75411 * If you pass $tag an integer of the tag ID, then that tag will
75412 * be retrieved from the database, if it isn't already cached, and pass it back.
75413 *
75414 * If you look at get_term(), then both types will be passed through several
75415 * filters and finally sanitized based on the $filter parameter value.
75416 *
75417 * @since 2.3.0
75418 *
75419 * @param int|object $tag
75420 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
75421 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
75422 * @return object|array Return type based on $output value.
75423 */
75424 function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
75425 return get_term( $tag, 'post_tag', $output, $filter );
75426 }
75427
75428
75429 /* Cache */
75430
75431
75432 /**
75433 * Update the categories cache.
75434 *
75435 * This function does not appear to be used anymore or does not appear to be
75436 * needed. It might be a legacy function left over from when there was a need
75437 * for updating the category cache.
75438 *
75439 * @since 1.5.0
75440 *
75441 * @return bool Always return True
75442 */
75443 function update_category_cache() {
75444 return true;
75445 }
75446
75447
75448 /**
75449 * Remove the category cache data based on ID.
75450 *
75451 * @since 2.1.0
75452 * @uses clean_term_cache() Clears the cache for the category based on ID
75453 *
75454 * @param int $id Category ID
75455 */
75456 function clean_category_cache( $id ) {
75457 clean_term_cache( $id, 'category' );
75458 }
75459
75460
75461 /**
75462 * Update category structure to old pre 2.3 from new taxonomy structure.
75463 *
75464 * This function was added for the taxonomy support to update the new category
75465 * structure with the old category one. This will maintain compatibility with
75466 * plugins and themes which depend on the old key or property names.
75467 *
75468 * The parameter should only be passed a variable and not create the array or
75469 * object inline to the parameter. The reason for this is that parameter is
75470 * passed by reference and PHP will fail unless it has the variable.
75471 *
75472 * There is no return value, because everything is updated on the variable you
75473 * pass to it. This is one of the features with using pass by reference in PHP.
75474 *
75475 * @since 2.3.0
75476 * @access private
75477 *
75478 * @param array|object $category Category Row object or array
75479 */
75480 function _make_cat_compat( &$category ) {
75481 if ( is_object( $category ) ) {
75482 $category->cat_ID = &$category->term_id;
75483 $category->category_count = &$category->count;
75484 $category->category_description = &$category->description;
75485 $category->cat_name = &$category->name;
75486 $category->category_nicename = &$category->slug;
75487 $category->category_parent = &$category->parent;
75488 } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
75489 $category['cat_ID'] = &$category['term_id'];
75490 $category['category_count'] = &$category['count'];
75491 $category['category_description'] = &$category['description'];
75492 $category['cat_name'] = &$category['name'];
75493 $category['category_nicename'] = &$category['slug'];
75494 $category['category_parent'] = &$category['parent'];
75495 }
75496 }
75497
75498
75499 ?>