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