raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Taxonomy API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Taxonomy
mp-wp_genesis 7 * @since 2.3.0
mp-wp_genesis 8 */
mp-wp_genesis 9
mp-wp_genesis 10 //
mp-wp_genesis 11 // Taxonomy Registration
mp-wp_genesis 12 //
mp-wp_genesis 13
mp-wp_genesis 14 /**
mp-wp_genesis 15 * Default Taxonomy Objects
mp-wp_genesis 16 * @since 2.3.0
mp-wp_genesis 17 * @global array $wp_taxonomies
mp-wp_genesis 18 */
mp-wp_genesis 19 $wp_taxonomies = array();
mp-wp_genesis 20 $wp_taxonomies['category'] = (object) array('name' => 'category', 'object_type' => 'post', 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count');
mp-wp_genesis 21 $wp_taxonomies['post_tag'] = (object) array('name' => 'post_tag', 'object_type' => 'post', 'hierarchical' => false, 'update_count_callback' => '_update_post_term_count');
mp-wp_genesis 22 $wp_taxonomies['link_category'] = (object) array('name' => 'link_category', 'object_type' => 'link', 'hierarchical' => false);
mp-wp_genesis 23
mp-wp_genesis 24 /**
mp-wp_genesis 25 * Return all of the taxonomy names that are of $object_type.
mp-wp_genesis 26 *
mp-wp_genesis 27 * It appears that this function can be used to find all of the names inside of
mp-wp_genesis 28 * $wp_taxonomies global variable.
mp-wp_genesis 29 *
mp-wp_genesis 30 * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should
mp-wp_genesis 31 * result in <code>Array('category', 'post_tag')</code>
mp-wp_genesis 32 *
mp-wp_genesis 33 * @package WordPress
mp-wp_genesis 34 * @subpackage Taxonomy
mp-wp_genesis 35 * @since 2.3.0
mp-wp_genesis 36 *
mp-wp_genesis 37 * @uses $wp_taxonomies
mp-wp_genesis 38 *
mp-wp_genesis 39 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
mp-wp_genesis 40 * @return array The names of all taxonomy of $object_type.
mp-wp_genesis 41 */
mp-wp_genesis 42 function get_object_taxonomies($object) {
mp-wp_genesis 43 global $wp_taxonomies;
mp-wp_genesis 44
mp-wp_genesis 45 if ( is_object($object) ) {
mp-wp_genesis 46 if ( $object->post_type == 'attachment' )
mp-wp_genesis 47 return get_attachment_taxonomies($object);
mp-wp_genesis 48 $object = $object->post_type;
mp-wp_genesis 49 }
mp-wp_genesis 50
mp-wp_genesis 51 $object = (array) $object;
mp-wp_genesis 52
mp-wp_genesis 53 $taxonomies = array();
mp-wp_genesis 54 foreach ( (array) $wp_taxonomies as $taxonomy ) {
mp-wp_genesis 55 if ( array_intersect($object, (array) $taxonomy->object_type) )
mp-wp_genesis 56 $taxonomies[] = $taxonomy->name;
mp-wp_genesis 57 }
mp-wp_genesis 58
mp-wp_genesis 59 return $taxonomies;
mp-wp_genesis 60 }
mp-wp_genesis 61
mp-wp_genesis 62 /**
mp-wp_genesis 63 * Retrieves the taxonomy object of $taxonomy.
mp-wp_genesis 64 *
mp-wp_genesis 65 * The get_taxonomy function will first check that the parameter string given
mp-wp_genesis 66 * is a taxonomy object and if it is, it will return it.
mp-wp_genesis 67 *
mp-wp_genesis 68 * @package WordPress
mp-wp_genesis 69 * @subpackage Taxonomy
mp-wp_genesis 70 * @since 2.3.0
mp-wp_genesis 71 *
mp-wp_genesis 72 * @uses $wp_taxonomies
mp-wp_genesis 73 * @uses is_taxonomy() Checks whether taxonomy exists
mp-wp_genesis 74 *
mp-wp_genesis 75 * @param string $taxonomy Name of taxonomy object to return
mp-wp_genesis 76 * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist
mp-wp_genesis 77 */
mp-wp_genesis 78 function get_taxonomy( $taxonomy ) {
mp-wp_genesis 79 global $wp_taxonomies;
mp-wp_genesis 80
mp-wp_genesis 81 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 82 return false;
mp-wp_genesis 83
mp-wp_genesis 84 return $wp_taxonomies[$taxonomy];
mp-wp_genesis 85 }
mp-wp_genesis 86
mp-wp_genesis 87 /**
mp-wp_genesis 88 * Checks that the taxonomy name exists.
mp-wp_genesis 89 *
mp-wp_genesis 90 * @package WordPress
mp-wp_genesis 91 * @subpackage Taxonomy
mp-wp_genesis 92 * @since 2.3.0
mp-wp_genesis 93 *
mp-wp_genesis 94 * @uses $wp_taxonomies
mp-wp_genesis 95 *
mp-wp_genesis 96 * @param string $taxonomy Name of taxonomy object
mp-wp_genesis 97 * @return bool Whether the taxonomy exists or not.
mp-wp_genesis 98 */
mp-wp_genesis 99 function is_taxonomy( $taxonomy ) {
mp-wp_genesis 100 global $wp_taxonomies;
mp-wp_genesis 101
mp-wp_genesis 102 return isset($wp_taxonomies[$taxonomy]);
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 /**
mp-wp_genesis 106 * Whether the taxonomy object is hierarchical.
mp-wp_genesis 107 *
mp-wp_genesis 108 * Checks to make sure that the taxonomy is an object first. Then Gets the
mp-wp_genesis 109 * object, and finally returns the hierarchical value in the object.
mp-wp_genesis 110 *
mp-wp_genesis 111 * A false return value might also mean that the taxonomy does not exist.
mp-wp_genesis 112 *
mp-wp_genesis 113 * @package WordPress
mp-wp_genesis 114 * @subpackage Taxonomy
mp-wp_genesis 115 * @since 2.3.0
mp-wp_genesis 116 *
mp-wp_genesis 117 * @uses is_taxonomy() Checks whether taxonomy exists
mp-wp_genesis 118 * @uses get_taxonomy() Used to get the taxonomy object
mp-wp_genesis 119 *
mp-wp_genesis 120 * @param string $taxonomy Name of taxonomy object
mp-wp_genesis 121 * @return bool Whether the taxonomy is hierarchical
mp-wp_genesis 122 */
mp-wp_genesis 123 function is_taxonomy_hierarchical($taxonomy) {
mp-wp_genesis 124 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 125 return false;
mp-wp_genesis 126
mp-wp_genesis 127 $taxonomy = get_taxonomy($taxonomy);
mp-wp_genesis 128 return $taxonomy->hierarchical;
mp-wp_genesis 129 }
mp-wp_genesis 130
mp-wp_genesis 131 /**
mp-wp_genesis 132 * Create or modify a taxonomy object. Do not use before init.
mp-wp_genesis 133 *
mp-wp_genesis 134 * A simple function for creating or modifying a taxonomy object based on the
mp-wp_genesis 135 * parameters given. The function will accept an array (third optional
mp-wp_genesis 136 * parameter), along with strings for the taxonomy name and another string for
mp-wp_genesis 137 * the object type.
mp-wp_genesis 138 *
mp-wp_genesis 139 * Nothing is returned, so expect error maybe or use is_taxonomy() to check
mp-wp_genesis 140 * whether taxonomy exists.
mp-wp_genesis 141 *
mp-wp_genesis 142 * Optional $args contents:
mp-wp_genesis 143 *
mp-wp_genesis 144 * hierarachical - has some defined purpose at other parts of the API and is a
mp-wp_genesis 145 * boolean value.
mp-wp_genesis 146 *
mp-wp_genesis 147 * update_count_callback - works much like a hook, in that it will be called
mp-wp_genesis 148 * when the count is updated.
mp-wp_genesis 149 *
mp-wp_genesis 150 * rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize
mp-wp_genesis 151 * permastruct; default will use $taxonomy as slug.
mp-wp_genesis 152 *
mp-wp_genesis 153 * query_var - false to prevent queries, or string to customize query var
mp-wp_genesis 154 * (?$query_var=$term); default will use $taxonomy as query var.
mp-wp_genesis 155 *
mp-wp_genesis 156 * @package WordPress
mp-wp_genesis 157 * @subpackage Taxonomy
mp-wp_genesis 158 * @since 2.3.0
mp-wp_genesis 159 * @uses $wp_taxonomies Inserts new taxonomy object into the list
mp-wp_genesis 160 * @uses $wp_rewrite Adds rewrite tags and permastructs
mp-wp_genesis 161 * @uses $wp Adds query vars
mp-wp_genesis 162 *
mp-wp_genesis 163 * @param string $taxonomy Name of taxonomy object
mp-wp_genesis 164 * @param array|string $object_type Name of the object type for the taxonomy object.
mp-wp_genesis 165 * @param array|string $args See above description for the two keys values.
mp-wp_genesis 166 */
mp-wp_genesis 167 function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
mp-wp_genesis 168 global $wp_taxonomies, $wp_rewrite, $wp;
mp-wp_genesis 169
mp-wp_genesis 170 $defaults = array('hierarchical' => false, 'update_count_callback' => '', 'rewrite' => true, 'query_var' => true);
mp-wp_genesis 171 $args = wp_parse_args($args, $defaults);
mp-wp_genesis 172
mp-wp_genesis 173 if ( false !== $args['query_var'] && !empty($wp) ) {
mp-wp_genesis 174 if ( true === $args['query_var'] )
mp-wp_genesis 175 $args['query_var'] = $taxonomy;
mp-wp_genesis 176 $args['query_var'] = sanitize_title_with_dashes($args['query_var']);
mp-wp_genesis 177 $wp->add_query_var($args['query_var']);
mp-wp_genesis 178 }
mp-wp_genesis 179
mp-wp_genesis 180 if ( false !== $args['rewrite'] && !empty($wp_rewrite) ) {
mp-wp_genesis 181 if ( !is_array($args['rewrite']) )
mp-wp_genesis 182 $args['rewrite'] = array();
mp-wp_genesis 183 if ( !isset($args['rewrite']['slug']) )
mp-wp_genesis 184 $args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy);
mp-wp_genesis 185 $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term");
mp-wp_genesis 186 $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%");
mp-wp_genesis 187 }
mp-wp_genesis 188
mp-wp_genesis 189 $args['name'] = $taxonomy;
mp-wp_genesis 190 $args['object_type'] = $object_type;
mp-wp_genesis 191 $wp_taxonomies[$taxonomy] = (object) $args;
mp-wp_genesis 192 }
mp-wp_genesis 193
mp-wp_genesis 194 //
mp-wp_genesis 195 // Term API
mp-wp_genesis 196 //
mp-wp_genesis 197
mp-wp_genesis 198 /**
mp-wp_genesis 199 * Retrieve object_ids of valid taxonomy and term.
mp-wp_genesis 200 *
mp-wp_genesis 201 * The strings of $taxonomies must exist before this function will continue. On
mp-wp_genesis 202 * failure of finding a valid taxonomy, it will return an WP_Error class, kind
mp-wp_genesis 203 * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
mp-wp_genesis 204 * still test for the WP_Error class and get the error message.
mp-wp_genesis 205 *
mp-wp_genesis 206 * The $terms aren't checked the same as $taxonomies, but still need to exist
mp-wp_genesis 207 * for $object_ids to be returned.
mp-wp_genesis 208 *
mp-wp_genesis 209 * It is possible to change the order that object_ids is returned by either
mp-wp_genesis 210 * using PHP sort family functions or using the database by using $args with
mp-wp_genesis 211 * either ASC or DESC array. The value should be in the key named 'order'.
mp-wp_genesis 212 *
mp-wp_genesis 213 * @package WordPress
mp-wp_genesis 214 * @subpackage Taxonomy
mp-wp_genesis 215 * @since 2.3.0
mp-wp_genesis 216 *
mp-wp_genesis 217 * @uses $wpdb
mp-wp_genesis 218 * @uses wp_parse_args() Creates an array from string $args.
mp-wp_genesis 219 *
mp-wp_genesis 220 * @param string|array $terms String of term or array of string values of terms that will be used
mp-wp_genesis 221 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names
mp-wp_genesis 222 * @param array|string $args Change the order of the object_ids, either ASC or DESC
mp-wp_genesis 223 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success
mp-wp_genesis 224 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
mp-wp_genesis 225 */
mp-wp_genesis 226 function get_objects_in_term( $terms, $taxonomies, $args = array() ) {
mp-wp_genesis 227 global $wpdb;
mp-wp_genesis 228
mp-wp_genesis 229 if ( !is_array( $terms) )
mp-wp_genesis 230 $terms = array($terms);
mp-wp_genesis 231
mp-wp_genesis 232 if ( !is_array($taxonomies) )
mp-wp_genesis 233 $taxonomies = array($taxonomies);
mp-wp_genesis 234
mp-wp_genesis 235 foreach ( (array) $taxonomies as $taxonomy ) {
mp-wp_genesis 236 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 237 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 238 }
mp-wp_genesis 239
mp-wp_genesis 240 $defaults = array('order' => 'ASC');
mp-wp_genesis 241 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 242 extract($args, EXTR_SKIP);
mp-wp_genesis 243
mp-wp_genesis 244 $order = ( 'desc' == strtolower($order) ) ? 'DESC' : 'ASC';
mp-wp_genesis 245
mp-wp_genesis 246 $terms = array_map('intval', $terms);
mp-wp_genesis 247
mp-wp_genesis 248 $taxonomies = "'" . implode("', '", $taxonomies) . "'";
mp-wp_genesis 249 $terms = "'" . implode("', '", $terms) . "'";
mp-wp_genesis 250
mp-wp_genesis 251 $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($terms) ORDER BY tr.object_id $order");
mp-wp_genesis 252
mp-wp_genesis 253 if ( ! $object_ids )
mp-wp_genesis 254 return array();
mp-wp_genesis 255
mp-wp_genesis 256 return $object_ids;
mp-wp_genesis 257 }
mp-wp_genesis 258
mp-wp_genesis 259 /**
mp-wp_genesis 260 * Get all Term data from database by Term ID.
mp-wp_genesis 261 *
mp-wp_genesis 262 * The usage of the get_term function is to apply filters to a term object. It
mp-wp_genesis 263 * is possible to get a term object from the database before applying the
mp-wp_genesis 264 * filters.
mp-wp_genesis 265 *
mp-wp_genesis 266 * $term ID must be part of $taxonomy, to get from the database. Failure, might
mp-wp_genesis 267 * be able to be captured by the hooks. Failure would be the same value as $wpdb
mp-wp_genesis 268 * returns for the get_row method.
mp-wp_genesis 269 *
mp-wp_genesis 270 * There are two hooks, one is specifically for each term, named 'get_term', and
mp-wp_genesis 271 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
mp-wp_genesis 272 * term object, and the taxonomy name as parameters. Both hooks are expected to
mp-wp_genesis 273 * return a Term object.
mp-wp_genesis 274 *
mp-wp_genesis 275 * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
mp-wp_genesis 276 * Must return term object. Used in get_term() as a catch-all filter for every
mp-wp_genesis 277 * $term.
mp-wp_genesis 278 *
mp-wp_genesis 279 * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
mp-wp_genesis 280 * name. Must return term object. $taxonomy will be the taxonomy name, so for
mp-wp_genesis 281 * example, if 'category', it would be 'get_category' as the filter name. Useful
mp-wp_genesis 282 * for custom taxonomies or plugging into default taxonomies.
mp-wp_genesis 283 *
mp-wp_genesis 284 * @package WordPress
mp-wp_genesis 285 * @subpackage Taxonomy
mp-wp_genesis 286 * @since 2.3.0
mp-wp_genesis 287 *
mp-wp_genesis 288 * @uses $wpdb
mp-wp_genesis 289 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
mp-wp_genesis 290 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
mp-wp_genesis 291 *
mp-wp_genesis 292 * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
mp-wp_genesis 293 * @param string $taxonomy Taxonomy name that $term is part of.
mp-wp_genesis 294 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
mp-wp_genesis 295 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
mp-wp_genesis 296 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
mp-wp_genesis 297 * exist then WP_Error will be returned.
mp-wp_genesis 298 */
mp-wp_genesis 299 function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 300 global $wpdb;
mp-wp_genesis 301
mp-wp_genesis 302 if ( empty($term) ) {
mp-wp_genesis 303 $error = new WP_Error('invalid_term', __('Empty Term'));
mp-wp_genesis 304 return $error;
mp-wp_genesis 305 }
mp-wp_genesis 306
mp-wp_genesis 307 if ( ! is_taxonomy($taxonomy) ) {
mp-wp_genesis 308 $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 309 return $error;
mp-wp_genesis 310 }
mp-wp_genesis 311
mp-wp_genesis 312 if ( is_object($term) && empty($term->filter) ) {
mp-wp_genesis 313 wp_cache_add($term->term_id, $term, $taxonomy);
mp-wp_genesis 314 $_term = $term;
mp-wp_genesis 315 } else {
mp-wp_genesis 316 if ( is_object($term) )
mp-wp_genesis 317 $term = $term->term_id;
mp-wp_genesis 318 $term = (int) $term;
mp-wp_genesis 319 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
mp-wp_genesis 320 $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %s LIMIT 1", $taxonomy, $term) );
mp-wp_genesis 321 wp_cache_add($term, $_term, $taxonomy);
mp-wp_genesis 322 }
mp-wp_genesis 323 }
mp-wp_genesis 324
mp-wp_genesis 325 $_term = apply_filters('get_term', $_term, $taxonomy);
mp-wp_genesis 326 $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
mp-wp_genesis 327 $_term = sanitize_term($_term, $taxonomy, $filter);
mp-wp_genesis 328
mp-wp_genesis 329 if ( $output == OBJECT ) {
mp-wp_genesis 330 return $_term;
mp-wp_genesis 331 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 332 $__term = get_object_vars($_term);
mp-wp_genesis 333 return $__term;
mp-wp_genesis 334 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 335 $__term = array_values(get_object_vars($_term));
mp-wp_genesis 336 return $__term;
mp-wp_genesis 337 } else {
mp-wp_genesis 338 return $_term;
mp-wp_genesis 339 }
mp-wp_genesis 340 }
mp-wp_genesis 341
mp-wp_genesis 342 /**
mp-wp_genesis 343 * Get all Term data from database by Term field and data.
mp-wp_genesis 344 *
mp-wp_genesis 345 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
mp-wp_genesis 346 * required.
mp-wp_genesis 347 *
mp-wp_genesis 348 * The default $field is 'id', therefore it is possible to also use null for
mp-wp_genesis 349 * field, but not recommended that you do so.
mp-wp_genesis 350 *
mp-wp_genesis 351 * If $value does not exist, the return value will be false. If $taxonomy exists
mp-wp_genesis 352 * and $field and $value combinations exist, the Term will be returned.
mp-wp_genesis 353 *
mp-wp_genesis 354 * @package WordPress
mp-wp_genesis 355 * @subpackage Taxonomy
mp-wp_genesis 356 * @since 2.3.0
mp-wp_genesis 357 *
mp-wp_genesis 358 * @uses $wpdb
mp-wp_genesis 359 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
mp-wp_genesis 360 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
mp-wp_genesis 361 *
mp-wp_genesis 362 * @param string $field Either 'slug', 'name', or 'id'
mp-wp_genesis 363 * @param string|int $value Search for this term value
mp-wp_genesis 364 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 365 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
mp-wp_genesis 366 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
mp-wp_genesis 367 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
mp-wp_genesis 368 */
mp-wp_genesis 369 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
mp-wp_genesis 370 global $wpdb;
mp-wp_genesis 371
mp-wp_genesis 372 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 373 return false;
mp-wp_genesis 374
mp-wp_genesis 375 if ( 'slug' == $field ) {
mp-wp_genesis 376 $field = 't.slug';
mp-wp_genesis 377 $value = sanitize_title($value);
mp-wp_genesis 378 if ( empty($value) )
mp-wp_genesis 379 return false;
mp-wp_genesis 380 } else if ( 'name' == $field ) {
mp-wp_genesis 381 // Assume already escaped
mp-wp_genesis 382 $value = stripslashes($value);
mp-wp_genesis 383 $field = 't.name';
mp-wp_genesis 384 } else {
mp-wp_genesis 385 $field = 't.term_id';
mp-wp_genesis 386 $value = (int) $value;
mp-wp_genesis 387 }
mp-wp_genesis 388
mp-wp_genesis 389 $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
mp-wp_genesis 390 if ( !$term )
mp-wp_genesis 391 return false;
mp-wp_genesis 392
mp-wp_genesis 393 wp_cache_add($term->term_id, $term, $taxonomy);
mp-wp_genesis 394
mp-wp_genesis 395 $term = sanitize_term($term, $taxonomy, $filter);
mp-wp_genesis 396
mp-wp_genesis 397 if ( $output == OBJECT ) {
mp-wp_genesis 398 return $term;
mp-wp_genesis 399 } elseif ( $output == ARRAY_A ) {
mp-wp_genesis 400 return get_object_vars($term);
mp-wp_genesis 401 } elseif ( $output == ARRAY_N ) {
mp-wp_genesis 402 return array_values(get_object_vars($term));
mp-wp_genesis 403 } else {
mp-wp_genesis 404 return $term;
mp-wp_genesis 405 }
mp-wp_genesis 406 }
mp-wp_genesis 407
mp-wp_genesis 408 /**
mp-wp_genesis 409 * Merge all term children into a single array.
mp-wp_genesis 410 *
mp-wp_genesis 411 * This recursive function will merge all of the children of $term into the same
mp-wp_genesis 412 * array. Only useful for taxonomies which are hierarchical.
mp-wp_genesis 413 *
mp-wp_genesis 414 * Will return an empty array if $term does not exist in $taxonomy.
mp-wp_genesis 415 *
mp-wp_genesis 416 * @package WordPress
mp-wp_genesis 417 * @subpackage Taxonomy
mp-wp_genesis 418 * @since 2.3.0
mp-wp_genesis 419 *
mp-wp_genesis 420 * @uses $wpdb
mp-wp_genesis 421 * @uses _get_term_hierarchy()
mp-wp_genesis 422 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
mp-wp_genesis 423 *
mp-wp_genesis 424 * @param string $term Name of Term to get children
mp-wp_genesis 425 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 426 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
mp-wp_genesis 427 */
mp-wp_genesis 428 function get_term_children( $term, $taxonomy ) {
mp-wp_genesis 429 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 430 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 431
mp-wp_genesis 432 $terms = _get_term_hierarchy($taxonomy);
mp-wp_genesis 433
mp-wp_genesis 434 if ( ! isset($terms[$term]) )
mp-wp_genesis 435 return array();
mp-wp_genesis 436
mp-wp_genesis 437 $children = $terms[$term];
mp-wp_genesis 438
mp-wp_genesis 439 foreach ( (array) $terms[$term] as $child ) {
mp-wp_genesis 440 if ( isset($terms[$child]) )
mp-wp_genesis 441 $children = array_merge($children, get_term_children($child, $taxonomy));
mp-wp_genesis 442 }
mp-wp_genesis 443
mp-wp_genesis 444 return $children;
mp-wp_genesis 445 }
mp-wp_genesis 446
mp-wp_genesis 447 /**
mp-wp_genesis 448 * Get sanitized Term field.
mp-wp_genesis 449 *
mp-wp_genesis 450 * Does checks for $term, based on the $taxonomy. The function is for contextual
mp-wp_genesis 451 * reasons and for simplicity of usage. See sanitize_term_field() for more
mp-wp_genesis 452 * information.
mp-wp_genesis 453 *
mp-wp_genesis 454 * @package WordPress
mp-wp_genesis 455 * @subpackage Taxonomy
mp-wp_genesis 456 * @since 2.3.0
mp-wp_genesis 457 *
mp-wp_genesis 458 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
mp-wp_genesis 459 *
mp-wp_genesis 460 * @param string $field Term field to fetch
mp-wp_genesis 461 * @param int $term Term ID
mp-wp_genesis 462 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 463 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
mp-wp_genesis 464 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
mp-wp_genesis 465 */
mp-wp_genesis 466 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
mp-wp_genesis 467 $term = (int) $term;
mp-wp_genesis 468 $term = get_term( $term, $taxonomy );
mp-wp_genesis 469 if ( is_wp_error($term) )
mp-wp_genesis 470 return $term;
mp-wp_genesis 471
mp-wp_genesis 472 if ( !is_object($term) )
mp-wp_genesis 473 return '';
mp-wp_genesis 474
mp-wp_genesis 475 if ( !isset($term->$field) )
mp-wp_genesis 476 return '';
mp-wp_genesis 477
mp-wp_genesis 478 return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
mp-wp_genesis 479 }
mp-wp_genesis 480
mp-wp_genesis 481 /**
mp-wp_genesis 482 * Sanitizes Term for editing.
mp-wp_genesis 483 *
mp-wp_genesis 484 * Return value is sanitize_term() and usage is for sanitizing the term for
mp-wp_genesis 485 * editing. Function is for contextual and simplicity.
mp-wp_genesis 486 *
mp-wp_genesis 487 * @package WordPress
mp-wp_genesis 488 * @subpackage Taxonomy
mp-wp_genesis 489 * @since 2.3.0
mp-wp_genesis 490 *
mp-wp_genesis 491 * @uses sanitize_term() Passes the return value on success
mp-wp_genesis 492 *
mp-wp_genesis 493 * @param int|object $id Term ID or Object
mp-wp_genesis 494 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 495 * @return mixed|null|WP_Error Will return empty string if $term is not an object.
mp-wp_genesis 496 */
mp-wp_genesis 497 function get_term_to_edit( $id, $taxonomy ) {
mp-wp_genesis 498 $term = get_term( $id, $taxonomy );
mp-wp_genesis 499
mp-wp_genesis 500 if ( is_wp_error($term) )
mp-wp_genesis 501 return $term;
mp-wp_genesis 502
mp-wp_genesis 503 if ( !is_object($term) )
mp-wp_genesis 504 return '';
mp-wp_genesis 505
mp-wp_genesis 506 return sanitize_term($term, $taxonomy, 'edit');
mp-wp_genesis 507 }
mp-wp_genesis 508
mp-wp_genesis 509 /**
mp-wp_genesis 510 * Retrieve the terms in a given taxonomy or list of taxonomies.
mp-wp_genesis 511 *
mp-wp_genesis 512 * You can fully inject any customizations to the query before it is sent, as
mp-wp_genesis 513 * well as control the output with a filter.
mp-wp_genesis 514 *
mp-wp_genesis 515 * The 'get_terms' filter will be called when the cache has the term and will
mp-wp_genesis 516 * pass the found term along with the array of $taxonomies and array of $args.
mp-wp_genesis 517 * This filter is also called before the array of terms is passed and will pass
mp-wp_genesis 518 * the array of terms, along with the $taxonomies and $args.
mp-wp_genesis 519 *
mp-wp_genesis 520 * The 'list_terms_exclusions' filter passes the compiled exclusions along with
mp-wp_genesis 521 * the $args.
mp-wp_genesis 522 *
mp-wp_genesis 523 * The list of arguments that $args can contain, which will overwrite the defaults:
mp-wp_genesis 524 *
mp-wp_genesis 525 * orderby - Default is 'name'. Can be name, count, or nothing (will use
mp-wp_genesis 526 * term_id).
mp-wp_genesis 527 *
mp-wp_genesis 528 * order - Default is ASC. Can use DESC.
mp-wp_genesis 529 *
mp-wp_genesis 530 * hide_empty - Default is true. Will not return empty terms, which means
mp-wp_genesis 531 * terms whose count is 0 according to the given taxonomy.
mp-wp_genesis 532 *
mp-wp_genesis 533 * exclude - Default is an empty string. A comma- or space-delimited string
mp-wp_genesis 534 * of term ids to exclude from the return array. If 'include' is non-empty,
mp-wp_genesis 535 * 'exclude' is ignored.
mp-wp_genesis 536 *
mp-wp_genesis 537 * include - Default is an empty string. A comma- or space-delimited string
mp-wp_genesis 538 * of term ids to include in the return array.
mp-wp_genesis 539 *
mp-wp_genesis 540 * number - The maximum number of terms to return. Default is empty.
mp-wp_genesis 541 *
mp-wp_genesis 542 * offset - The number by which to offset the terms query.
mp-wp_genesis 543 *
mp-wp_genesis 544 * fields - Default is 'all', which returns an array of term objects.
mp-wp_genesis 545 * If 'fields' is 'ids' or 'names', returns an array of
mp-wp_genesis 546 * integers or strings, respectively.
mp-wp_genesis 547 *
mp-wp_genesis 548 * slug - Returns terms whose "slug" matches this value. Default is empty string.
mp-wp_genesis 549 *
mp-wp_genesis 550 * hierarchical - Whether to include terms that have non-empty descendants
mp-wp_genesis 551 * (even if 'hide_empty' is set to true).
mp-wp_genesis 552 *
mp-wp_genesis 553 * search - Returned terms' names will contain the value of 'search',
mp-wp_genesis 554 * case-insensitive. Default is an empty string.
mp-wp_genesis 555 *
mp-wp_genesis 556 * name__like - Returned terms' names will begin with the value of 'name__like',
mp-wp_genesis 557 * case-insensitive. Default is empty string.
mp-wp_genesis 558 *
mp-wp_genesis 559 * The argument 'pad_counts', if set to true will include the quantity of a term's
mp-wp_genesis 560 * children in the quantity of each term's "count" object variable.
mp-wp_genesis 561 *
mp-wp_genesis 562 * The 'get' argument, if set to 'all' instead of its default empty string,
mp-wp_genesis 563 * returns terms regardless of ancestry or whether the terms are empty.
mp-wp_genesis 564 *
mp-wp_genesis 565 * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
mp-wp_genesis 566 * is 0. If set to a non-zero value, all returned terms will be descendants
mp-wp_genesis 567 * of that term according to the given taxonomy. Hence 'child_of' is set to 0
mp-wp_genesis 568 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
mp-wp_genesis 569 * make term ancestry ambiguous.
mp-wp_genesis 570 *
mp-wp_genesis 571 * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
mp-wp_genesis 572 * the empty string '', which has a different meaning from the integer 0.
mp-wp_genesis 573 * If set to an integer value, all returned terms will have as an immediate
mp-wp_genesis 574 * ancestor the term whose ID is specified by that integer according to the given taxonomy.
mp-wp_genesis 575 * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'
mp-wp_genesis 576 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
mp-wp_genesis 577 *
mp-wp_genesis 578 * @package WordPress
mp-wp_genesis 579 * @subpackage Taxonomy
mp-wp_genesis 580 * @since 2.3.0
mp-wp_genesis 581 *
mp-wp_genesis 582 * @uses $wpdb
mp-wp_genesis 583 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
mp-wp_genesis 584 *
mp-wp_genesis 585 * @param string|array Taxonomy name or list of Taxonomy names
mp-wp_genesis 586 * @param string|array $args The values of what to search for when returning terms
mp-wp_genesis 587 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
mp-wp_genesis 588 */
mp-wp_genesis 589 function &get_terms($taxonomies, $args = '') {
mp-wp_genesis 590 global $wpdb;
mp-wp_genesis 591 $empty_array = array();
mp-wp_genesis 592
mp-wp_genesis 593 $single_taxonomy = false;
mp-wp_genesis 594 if ( !is_array($taxonomies) ) {
mp-wp_genesis 595 $single_taxonomy = true;
mp-wp_genesis 596 $taxonomies = array($taxonomies);
mp-wp_genesis 597 }
mp-wp_genesis 598
mp-wp_genesis 599 foreach ( (array) $taxonomies as $taxonomy ) {
mp-wp_genesis 600 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 601 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 602 }
mp-wp_genesis 603
mp-wp_genesis 604 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
mp-wp_genesis 605
mp-wp_genesis 606 $defaults = array('orderby' => 'name', 'order' => 'ASC',
mp-wp_genesis 607 'hide_empty' => true, 'exclude' => '', 'include' => '',
mp-wp_genesis 608 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
mp-wp_genesis 609 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
mp-wp_genesis 610 'pad_counts' => false, 'offset' => '', 'search' => '');
mp-wp_genesis 611 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 612 $args['number'] = absint( $args['number'] );
mp-wp_genesis 613 $args['offset'] = absint( $args['offset'] );
mp-wp_genesis 614 if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
mp-wp_genesis 615 '' != $args['parent'] ) {
mp-wp_genesis 616 $args['child_of'] = 0;
mp-wp_genesis 617 $args['hierarchical'] = false;
mp-wp_genesis 618 $args['pad_counts'] = false;
mp-wp_genesis 619 }
mp-wp_genesis 620
mp-wp_genesis 621 if ( 'all' == $args['get'] ) {
mp-wp_genesis 622 $args['child_of'] = 0;
mp-wp_genesis 623 $args['hide_empty'] = 0;
mp-wp_genesis 624 $args['hierarchical'] = false;
mp-wp_genesis 625 $args['pad_counts'] = false;
mp-wp_genesis 626 }
mp-wp_genesis 627 extract($args, EXTR_SKIP);
mp-wp_genesis 628
mp-wp_genesis 629 if ( $child_of ) {
mp-wp_genesis 630 $hierarchy = _get_term_hierarchy($taxonomies[0]);
mp-wp_genesis 631 if ( !isset($hierarchy[$child_of]) )
mp-wp_genesis 632 return $empty_array;
mp-wp_genesis 633 }
mp-wp_genesis 634
mp-wp_genesis 635 if ( $parent ) {
mp-wp_genesis 636 $hierarchy = _get_term_hierarchy($taxonomies[0]);
mp-wp_genesis 637 if ( !isset($hierarchy[$parent]) )
mp-wp_genesis 638 return $empty_array;
mp-wp_genesis 639 }
mp-wp_genesis 640
mp-wp_genesis 641 // $args can be whatever, only use the args defined in defaults to compute the key
mp-wp_genesis 642 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
mp-wp_genesis 643 $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
mp-wp_genesis 644 $last_changed = wp_cache_get('last_changed', 'terms');
mp-wp_genesis 645 if ( !$last_changed ) {
mp-wp_genesis 646 $last_changed = time();
mp-wp_genesis 647 wp_cache_set('last_changed', $last_changed, 'terms');
mp-wp_genesis 648 }
mp-wp_genesis 649 $cache_key = "get_terms:$key:$last_changed";
mp-wp_genesis 650
mp-wp_genesis 651 if ( $cache = wp_cache_get( $cache_key, 'terms' ) ) {
mp-wp_genesis 652 $terms = apply_filters('get_terms', $cache, $taxonomies, $args);
mp-wp_genesis 653 return $terms;
mp-wp_genesis 654 }
mp-wp_genesis 655
mp-wp_genesis 656 if ( 'count' == $orderby )
mp-wp_genesis 657 $orderby = 'tt.count';
mp-wp_genesis 658 else if ( 'name' == $orderby )
mp-wp_genesis 659 $orderby = 't.name';
mp-wp_genesis 660 else if ( 'slug' == $orderby )
mp-wp_genesis 661 $orderby = 't.slug';
mp-wp_genesis 662 else if ( 'term_group' == $orderby )
mp-wp_genesis 663 $orderby = 't.term_group';
mp-wp_genesis 664 else
mp-wp_genesis 665 $orderby = 't.term_id';
mp-wp_genesis 666
mp-wp_genesis 667 $where = '';
mp-wp_genesis 668 $inclusions = '';
mp-wp_genesis 669 if ( !empty($include) ) {
mp-wp_genesis 670 $exclude = '';
mp-wp_genesis 671 $interms = preg_split('/[\s,]+/',$include);
mp-wp_genesis 672 if ( count($interms) ) {
mp-wp_genesis 673 foreach ( (array) $interms as $interm ) {
mp-wp_genesis 674 if (empty($inclusions))
mp-wp_genesis 675 $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
mp-wp_genesis 676 else
mp-wp_genesis 677 $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
mp-wp_genesis 678 }
mp-wp_genesis 679 }
mp-wp_genesis 680 }
mp-wp_genesis 681
mp-wp_genesis 682 if ( !empty($inclusions) )
mp-wp_genesis 683 $inclusions .= ')';
mp-wp_genesis 684 $where .= $inclusions;
mp-wp_genesis 685
mp-wp_genesis 686 $exclusions = '';
mp-wp_genesis 687 if ( !empty($exclude) ) {
mp-wp_genesis 688 $exterms = preg_split('/[\s,]+/',$exclude);
mp-wp_genesis 689 if ( count($exterms) ) {
mp-wp_genesis 690 foreach ( (array) $exterms as $exterm ) {
mp-wp_genesis 691 if (empty($exclusions))
mp-wp_genesis 692 $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
mp-wp_genesis 693 else
mp-wp_genesis 694 $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
mp-wp_genesis 695 }
mp-wp_genesis 696 }
mp-wp_genesis 697 }
mp-wp_genesis 698
mp-wp_genesis 699 if ( !empty($exclusions) )
mp-wp_genesis 700 $exclusions .= ')';
mp-wp_genesis 701 $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
mp-wp_genesis 702 $where .= $exclusions;
mp-wp_genesis 703
mp-wp_genesis 704 if ( !empty($slug) ) {
mp-wp_genesis 705 $slug = sanitize_title($slug);
mp-wp_genesis 706 $where .= " AND t.slug = '$slug'";
mp-wp_genesis 707 }
mp-wp_genesis 708
mp-wp_genesis 709 if ( !empty($name__like) )
mp-wp_genesis 710 $where .= " AND t.name LIKE '{$name__like}%'";
mp-wp_genesis 711
mp-wp_genesis 712 if ( '' != $parent ) {
mp-wp_genesis 713 $parent = (int) $parent;
mp-wp_genesis 714 $where .= " AND tt.parent = '$parent'";
mp-wp_genesis 715 }
mp-wp_genesis 716
mp-wp_genesis 717 if ( $hide_empty && !$hierarchical )
mp-wp_genesis 718 $where .= ' AND tt.count > 0';
mp-wp_genesis 719
mp-wp_genesis 720 if ( !empty($number) ) {
mp-wp_genesis 721 if( $offset )
mp-wp_genesis 722 $number = 'LIMIT ' . $offset . ',' . $number;
mp-wp_genesis 723 else
mp-wp_genesis 724 $number = 'LIMIT ' . $number;
mp-wp_genesis 725
mp-wp_genesis 726 } else
mp-wp_genesis 727 $number = '';
mp-wp_genesis 728
mp-wp_genesis 729 if ( !empty($search) ) {
mp-wp_genesis 730 $search = like_escape($search);
mp-wp_genesis 731 $where .= " AND (t.name LIKE '%$search%')";
mp-wp_genesis 732 }
mp-wp_genesis 733
mp-wp_genesis 734 $select_this = '';
mp-wp_genesis 735 if ( 'all' == $fields )
mp-wp_genesis 736 $select_this = 't.*, tt.*';
mp-wp_genesis 737 else if ( 'ids' == $fields )
mp-wp_genesis 738 $select_this = 't.term_id, tt.parent, tt.count';
mp-wp_genesis 739 else if ( 'names' == $fields )
mp-wp_genesis 740 $select_this = 't.term_id, tt.parent, tt.count, t.name';
mp-wp_genesis 741
mp-wp_genesis 742 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number";
mp-wp_genesis 743
mp-wp_genesis 744 if ( 'all' == $fields ) {
mp-wp_genesis 745 $terms = $wpdb->get_results($query);
mp-wp_genesis 746 update_term_cache($terms);
mp-wp_genesis 747 } else if ( ('ids' == $fields) || ('names' == $fields) ) {
mp-wp_genesis 748 $terms = $wpdb->get_results($query);
mp-wp_genesis 749 }
mp-wp_genesis 750
mp-wp_genesis 751 if ( empty($terms) ) {
mp-wp_genesis 752 $cache[ $key ] = array();
mp-wp_genesis 753 wp_cache_set( 'get_terms', $cache, 'terms' );
mp-wp_genesis 754 $terms = apply_filters('get_terms', array(), $taxonomies, $args);
mp-wp_genesis 755 return $terms;
mp-wp_genesis 756 }
mp-wp_genesis 757
mp-wp_genesis 758 if ( $child_of ) {
mp-wp_genesis 759 $children = _get_term_hierarchy($taxonomies[0]);
mp-wp_genesis 760 if ( ! empty($children) )
mp-wp_genesis 761 $terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
mp-wp_genesis 762 }
mp-wp_genesis 763
mp-wp_genesis 764 // Update term counts to include children.
mp-wp_genesis 765 if ( $pad_counts && 'all' == $fields )
mp-wp_genesis 766 _pad_term_counts($terms, $taxonomies[0]);
mp-wp_genesis 767
mp-wp_genesis 768 // Make sure we show empty categories that have children.
mp-wp_genesis 769 if ( $hierarchical && $hide_empty && is_array($terms) ) {
mp-wp_genesis 770 foreach ( $terms as $k => $term ) {
mp-wp_genesis 771 if ( ! $term->count ) {
mp-wp_genesis 772 $children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
mp-wp_genesis 773 if( is_array($children) )
mp-wp_genesis 774 foreach ( $children as $child )
mp-wp_genesis 775 if ( $child->count )
mp-wp_genesis 776 continue 2;
mp-wp_genesis 777
mp-wp_genesis 778 // It really is empty
mp-wp_genesis 779 unset($terms[$k]);
mp-wp_genesis 780 }
mp-wp_genesis 781 }
mp-wp_genesis 782 }
mp-wp_genesis 783 reset ( $terms );
mp-wp_genesis 784
mp-wp_genesis 785 $_terms = array();
mp-wp_genesis 786 if ( 'ids' == $fields ) {
mp-wp_genesis 787 while ( $term = array_shift($terms) )
mp-wp_genesis 788 $_terms[] = $term->term_id;
mp-wp_genesis 789 $terms = $_terms;
mp-wp_genesis 790 } elseif ( 'names' == $fields ) {
mp-wp_genesis 791 while ( $term = array_shift($terms) )
mp-wp_genesis 792 $_terms[] = $term->name;
mp-wp_genesis 793 $terms = $_terms;
mp-wp_genesis 794 }
mp-wp_genesis 795
mp-wp_genesis 796 wp_cache_add( $cache_key, $terms, 'terms' );
mp-wp_genesis 797
mp-wp_genesis 798 $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
mp-wp_genesis 799 return $terms;
mp-wp_genesis 800 }
mp-wp_genesis 801
mp-wp_genesis 802 /**
mp-wp_genesis 803 * Check if Term exists.
mp-wp_genesis 804 *
mp-wp_genesis 805 * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
mp-wp_genesis 806 *
mp-wp_genesis 807 * @package WordPress
mp-wp_genesis 808 * @subpackage Taxonomy
mp-wp_genesis 809 * @since 2.3.0
mp-wp_genesis 810 *
mp-wp_genesis 811 * @uses $wpdb
mp-wp_genesis 812 *
mp-wp_genesis 813 * @param int|string $term The term to check
mp-wp_genesis 814 * @param string $taxonomy The taxonomy name to use
mp-wp_genesis 815 * @return mixed Get the term id or Term Object, if exists.
mp-wp_genesis 816 */
mp-wp_genesis 817 function is_term($term, $taxonomy = '') {
mp-wp_genesis 818 global $wpdb;
mp-wp_genesis 819
mp-wp_genesis 820 $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
mp-wp_genesis 821 $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
mp-wp_genesis 822
mp-wp_genesis 823 if ( is_int($term) ) {
mp-wp_genesis 824 if ( 0 == $term )
mp-wp_genesis 825 return 0;
mp-wp_genesis 826 $where = 't.term_id = %d';
mp-wp_genesis 827 if ( !empty($taxonomy) )
mp-wp_genesis 828 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
mp-wp_genesis 829 else
mp-wp_genesis 830 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
mp-wp_genesis 831 }
mp-wp_genesis 832
mp-wp_genesis 833 if ( '' === $slug = sanitize_title($term) )
mp-wp_genesis 834 return 0;
mp-wp_genesis 835
mp-wp_genesis 836 $where = 't.slug = %s';
mp-wp_genesis 837 $else_where = 't.name = %s';
mp-wp_genesis 838
mp-wp_genesis 839 if ( !empty($taxonomy) ) {
mp-wp_genesis 840 if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $slug, $taxonomy), ARRAY_A) )
mp-wp_genesis 841 return $result;
mp-wp_genesis 842
mp-wp_genesis 843 return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $term, $taxonomy), ARRAY_A);
mp-wp_genesis 844 }
mp-wp_genesis 845
mp-wp_genesis 846 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $slug) ) )
mp-wp_genesis 847 return $result;
mp-wp_genesis 848
mp-wp_genesis 849 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $term) );
mp-wp_genesis 850 }
mp-wp_genesis 851
mp-wp_genesis 852 /**
mp-wp_genesis 853 * Sanitize Term all fields.
mp-wp_genesis 854 *
mp-wp_genesis 855 * Relys on sanitize_term_field() to sanitize the term. The difference is that
mp-wp_genesis 856 * this function will sanitize <strong>all</strong> fields. The context is based
mp-wp_genesis 857 * on sanitize_term_field().
mp-wp_genesis 858 *
mp-wp_genesis 859 * The $term is expected to be either an array or an object.
mp-wp_genesis 860 *
mp-wp_genesis 861 * @package WordPress
mp-wp_genesis 862 * @subpackage Taxonomy
mp-wp_genesis 863 * @since 2.3.0
mp-wp_genesis 864 *
mp-wp_genesis 865 * @uses sanitize_term_field Used to sanitize all fields in a term
mp-wp_genesis 866 *
mp-wp_genesis 867 * @param array|object $term The term to check
mp-wp_genesis 868 * @param string $taxonomy The taxonomy name to use
mp-wp_genesis 869 * @param string $context Default is 'display'.
mp-wp_genesis 870 * @return array|object Term with all fields sanitized
mp-wp_genesis 871 */
mp-wp_genesis 872 function sanitize_term($term, $taxonomy, $context = 'display') {
mp-wp_genesis 873
mp-wp_genesis 874 if ( 'raw' == $context )
mp-wp_genesis 875 return $term;
mp-wp_genesis 876
mp-wp_genesis 877 $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
mp-wp_genesis 878
mp-wp_genesis 879 $do_object = false;
mp-wp_genesis 880 if ( is_object($term) )
mp-wp_genesis 881 $do_object = true;
mp-wp_genesis 882
mp-wp_genesis 883 $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
mp-wp_genesis 884
mp-wp_genesis 885 foreach ( (array) $fields as $field ) {
mp-wp_genesis 886 if ( $do_object ) {
mp-wp_genesis 887 if ( isset($term->$field) )
mp-wp_genesis 888 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
mp-wp_genesis 889 } else {
mp-wp_genesis 890 if ( isset($term[$field]) )
mp-wp_genesis 891 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
mp-wp_genesis 892 }
mp-wp_genesis 893 }
mp-wp_genesis 894
mp-wp_genesis 895 if ( $do_object )
mp-wp_genesis 896 $term->filter = $context;
mp-wp_genesis 897 else
mp-wp_genesis 898 $term['filter'] = $context;
mp-wp_genesis 899
mp-wp_genesis 900 return $term;
mp-wp_genesis 901 }
mp-wp_genesis 902
mp-wp_genesis 903 /**
mp-wp_genesis 904 * Cleanse the field value in the term based on the context.
mp-wp_genesis 905 *
mp-wp_genesis 906 * Passing a term field value through the function should be assumed to have
mp-wp_genesis 907 * cleansed the value for whatever context the term field is going to be used.
mp-wp_genesis 908 *
mp-wp_genesis 909 * If no context or an unsupported context is given, then default filters will
mp-wp_genesis 910 * be applied.
mp-wp_genesis 911 *
mp-wp_genesis 912 * There are enough filters for each context to support a custom filtering
mp-wp_genesis 913 * without creating your own filter function. Simply create a function that
mp-wp_genesis 914 * hooks into the filter you need.
mp-wp_genesis 915 *
mp-wp_genesis 916 * @package WordPress
mp-wp_genesis 917 * @subpackage Taxonomy
mp-wp_genesis 918 * @since 2.3.0
mp-wp_genesis 919 *
mp-wp_genesis 920 * @uses $wpdb
mp-wp_genesis 921 *
mp-wp_genesis 922 * @param string $field Term field to sanitize
mp-wp_genesis 923 * @param string $value Search for this term value
mp-wp_genesis 924 * @param int $term_id Term ID
mp-wp_genesis 925 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 926 * @param string $context Either edit, db, display, attribute, or js.
mp-wp_genesis 927 * @return mixed sanitized field
mp-wp_genesis 928 */
mp-wp_genesis 929 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
mp-wp_genesis 930 if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
mp-wp_genesis 931 $value = (int) $value;
mp-wp_genesis 932 if ( $value < 0 )
mp-wp_genesis 933 $value = 0;
mp-wp_genesis 934 }
mp-wp_genesis 935
mp-wp_genesis 936 if ( 'raw' == $context )
mp-wp_genesis 937 return $value;
mp-wp_genesis 938
mp-wp_genesis 939 if ( 'edit' == $context ) {
mp-wp_genesis 940 $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
mp-wp_genesis 941 $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
mp-wp_genesis 942 if ( 'description' == $field )
mp-wp_genesis 943 $value = format_to_edit($value);
mp-wp_genesis 944 else
mp-wp_genesis 945 $value = attribute_escape($value);
mp-wp_genesis 946 } else if ( 'db' == $context ) {
mp-wp_genesis 947 $value = apply_filters("pre_term_$field", $value, $taxonomy);
mp-wp_genesis 948 $value = apply_filters("pre_${taxonomy}_$field", $value);
mp-wp_genesis 949 // Back compat filters
mp-wp_genesis 950 if ( 'slug' == $field )
mp-wp_genesis 951 $value = apply_filters('pre_category_nicename', $value);
mp-wp_genesis 952
mp-wp_genesis 953 } else if ( 'rss' == $context ) {
mp-wp_genesis 954 $value = apply_filters("term_${field}_rss", $value, $taxonomy);
mp-wp_genesis 955 $value = apply_filters("${taxonomy}_${field}_rss", $value);
mp-wp_genesis 956 } else {
mp-wp_genesis 957 // Use display filters by default.
mp-wp_genesis 958 $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
mp-wp_genesis 959 $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
mp-wp_genesis 960 }
mp-wp_genesis 961
mp-wp_genesis 962 if ( 'attribute' == $context )
mp-wp_genesis 963 $value = attribute_escape($value);
mp-wp_genesis 964 else if ( 'js' == $context )
mp-wp_genesis 965 $value = js_escape($value);
mp-wp_genesis 966
mp-wp_genesis 967 return $value;
mp-wp_genesis 968 }
mp-wp_genesis 969
mp-wp_genesis 970 /**
mp-wp_genesis 971 * Count how many terms are in Taxonomy.
mp-wp_genesis 972 *
mp-wp_genesis 973 * Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code>
mp-wp_genesis 974 * or <code>array('ignore_empty' => true);</code>.
mp-wp_genesis 975 *
mp-wp_genesis 976 * @package WordPress
mp-wp_genesis 977 * @subpackage Taxonomy
mp-wp_genesis 978 * @since 2.3.0
mp-wp_genesis 979 *
mp-wp_genesis 980 * @uses $wpdb
mp-wp_genesis 981 * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
mp-wp_genesis 982 *
mp-wp_genesis 983 * @param string $taxonomy Taxonomy name
mp-wp_genesis 984 * @param array|string $args Overwrite defaults
mp-wp_genesis 985 * @return int How many terms are in $taxonomy
mp-wp_genesis 986 */
mp-wp_genesis 987 function wp_count_terms( $taxonomy, $args = array() ) {
mp-wp_genesis 988 global $wpdb;
mp-wp_genesis 989
mp-wp_genesis 990 $defaults = array('ignore_empty' => false);
mp-wp_genesis 991 $args = wp_parse_args($args, $defaults);
mp-wp_genesis 992 extract($args, EXTR_SKIP);
mp-wp_genesis 993
mp-wp_genesis 994 $where = '';
mp-wp_genesis 995 if ( $ignore_empty )
mp-wp_genesis 996 $where = 'AND count > 0';
mp-wp_genesis 997
mp-wp_genesis 998 return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) );
mp-wp_genesis 999 }
mp-wp_genesis 1000
mp-wp_genesis 1001 /**
mp-wp_genesis 1002 * Will unlink the term from the taxonomy.
mp-wp_genesis 1003 *
mp-wp_genesis 1004 * Will remove the term's relationship to the taxonomy, not the term or taxonomy
mp-wp_genesis 1005 * itself. The term and taxonomy will still exist. Will require the term's
mp-wp_genesis 1006 * object ID to perform the operation.
mp-wp_genesis 1007 *
mp-wp_genesis 1008 * @package WordPress
mp-wp_genesis 1009 * @subpackage Taxonomy
mp-wp_genesis 1010 * @since 2.3.0
mp-wp_genesis 1011 * @uses $wpdb
mp-wp_genesis 1012 *
mp-wp_genesis 1013 * @param int $object_id The term Object Id that refers to the term
mp-wp_genesis 1014 * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name.
mp-wp_genesis 1015 */
mp-wp_genesis 1016 function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
mp-wp_genesis 1017 global $wpdb;
mp-wp_genesis 1018
mp-wp_genesis 1019 $object_id = (int) $object_id;
mp-wp_genesis 1020
mp-wp_genesis 1021 if ( !is_array($taxonomies) )
mp-wp_genesis 1022 $taxonomies = array($taxonomies);
mp-wp_genesis 1023
mp-wp_genesis 1024 foreach ( (array) $taxonomies as $taxonomy ) {
mp-wp_genesis 1025 $tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
mp-wp_genesis 1026 $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
mp-wp_genesis 1027 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
mp-wp_genesis 1028 wp_update_term_count($tt_ids, $taxonomy);
mp-wp_genesis 1029 }
mp-wp_genesis 1030 }
mp-wp_genesis 1031
mp-wp_genesis 1032 /**
mp-wp_genesis 1033 * Removes a term from the database.
mp-wp_genesis 1034 *
mp-wp_genesis 1035 * If the term is a parent of other terms, then the children will be updated to
mp-wp_genesis 1036 * that term's parent.
mp-wp_genesis 1037 *
mp-wp_genesis 1038 * The $args 'default' will only override the terms found, if there is only one
mp-wp_genesis 1039 * term found. Any other and the found terms are used.
mp-wp_genesis 1040 *
mp-wp_genesis 1041 * @package WordPress
mp-wp_genesis 1042 * @subpackage Taxonomy
mp-wp_genesis 1043 * @since 2.3.0
mp-wp_genesis 1044 *
mp-wp_genesis 1045 * @uses $wpdb
mp-wp_genesis 1046 * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action
mp-wp_genesis 1047 * hooks, passing term object, term id. 'delete_term' gets an additional
mp-wp_genesis 1048 * parameter with the $taxonomy parameter.
mp-wp_genesis 1049 *
mp-wp_genesis 1050 * @param int $term Term ID
mp-wp_genesis 1051 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 1052 * @param array|string $args Optional. Change 'default' term id and override found term ids.
mp-wp_genesis 1053 * @return bool|WP_Error Returns false if not term; true if completes delete action.
mp-wp_genesis 1054 */
mp-wp_genesis 1055 function wp_delete_term( $term, $taxonomy, $args = array() ) {
mp-wp_genesis 1056 global $wpdb;
mp-wp_genesis 1057
mp-wp_genesis 1058 $term = (int) $term;
mp-wp_genesis 1059
mp-wp_genesis 1060 if ( ! $ids = is_term($term, $taxonomy) )
mp-wp_genesis 1061 return false;
mp-wp_genesis 1062 if ( is_wp_error( $ids ) )
mp-wp_genesis 1063 return $ids;
mp-wp_genesis 1064
mp-wp_genesis 1065 $tt_id = $ids['term_taxonomy_id'];
mp-wp_genesis 1066
mp-wp_genesis 1067 $defaults = array();
mp-wp_genesis 1068 $args = wp_parse_args($args, $defaults);
mp-wp_genesis 1069 extract($args, EXTR_SKIP);
mp-wp_genesis 1070
mp-wp_genesis 1071 if ( isset($default) ) {
mp-wp_genesis 1072 $default = (int) $default;
mp-wp_genesis 1073 if ( ! is_term($default, $taxonomy) )
mp-wp_genesis 1074 unset($default);
mp-wp_genesis 1075 }
mp-wp_genesis 1076
mp-wp_genesis 1077 // Update children to point to new parent
mp-wp_genesis 1078 if ( is_taxonomy_hierarchical($taxonomy) ) {
mp-wp_genesis 1079 $term_obj = get_term($term, $taxonomy);
mp-wp_genesis 1080 if ( is_wp_error( $term_obj ) )
mp-wp_genesis 1081 return $term_obj;
mp-wp_genesis 1082 $parent = $term_obj->parent;
mp-wp_genesis 1083
mp-wp_genesis 1084 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
mp-wp_genesis 1085 }
mp-wp_genesis 1086
mp-wp_genesis 1087 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
mp-wp_genesis 1088
mp-wp_genesis 1089 foreach ( (array) $objects as $object ) {
mp-wp_genesis 1090 $terms = wp_get_object_terms($object, $taxonomy, 'fields=ids');
mp-wp_genesis 1091 if ( 1 == count($terms) && isset($default) )
mp-wp_genesis 1092 $terms = array($default);
mp-wp_genesis 1093 else
mp-wp_genesis 1094 $terms = array_diff($terms, array($term));
mp-wp_genesis 1095 $terms = array_map('intval', $terms);
mp-wp_genesis 1096 wp_set_object_terms($object, $terms, $taxonomy);
mp-wp_genesis 1097 }
mp-wp_genesis 1098
mp-wp_genesis 1099 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
mp-wp_genesis 1100
mp-wp_genesis 1101 // Delete the term if no taxonomies use it.
mp-wp_genesis 1102 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
mp-wp_genesis 1103 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
mp-wp_genesis 1104
mp-wp_genesis 1105 clean_term_cache($term, $taxonomy);
mp-wp_genesis 1106
mp-wp_genesis 1107 do_action('delete_term', $term, $tt_id, $taxonomy);
mp-wp_genesis 1108 do_action("delete_$taxonomy", $term, $tt_id);
mp-wp_genesis 1109
mp-wp_genesis 1110 return true;
mp-wp_genesis 1111 }
mp-wp_genesis 1112
mp-wp_genesis 1113 /**
mp-wp_genesis 1114 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
mp-wp_genesis 1115 *
mp-wp_genesis 1116 * The following information has to do the $args parameter and for what can be
mp-wp_genesis 1117 * contained in the string or array of that parameter, if it exists.
mp-wp_genesis 1118 *
mp-wp_genesis 1119 * The first argument is called, 'orderby' and has the default value of 'name'.
mp-wp_genesis 1120 * The other value that is supported is 'count'.
mp-wp_genesis 1121 *
mp-wp_genesis 1122 * The second argument is called, 'order' and has the default value of 'ASC'.
mp-wp_genesis 1123 * The only other value that will be acceptable is 'DESC'.
mp-wp_genesis 1124 *
mp-wp_genesis 1125 * The final argument supported is called, 'fields' and has the default value of
mp-wp_genesis 1126 * 'all'. There are multiple other options that can be used instead. Supported
mp-wp_genesis 1127 * values are as follows: 'all', 'ids', 'names', and finally
mp-wp_genesis 1128 * 'all_with_object_id'.
mp-wp_genesis 1129 *
mp-wp_genesis 1130 * The fields argument also decides what will be returned. If 'all' or
mp-wp_genesis 1131 * 'all_with_object_id' is choosen or the default kept intact, then all matching
mp-wp_genesis 1132 * terms objects will be returned. If either 'ids' or 'names' is used, then an
mp-wp_genesis 1133 * array of all matching term ids or term names will be returned respectively.
mp-wp_genesis 1134 *
mp-wp_genesis 1135 * @package WordPress
mp-wp_genesis 1136 * @subpackage Taxonomy
mp-wp_genesis 1137 * @since 2.3.0
mp-wp_genesis 1138 * @uses $wpdb
mp-wp_genesis 1139 *
mp-wp_genesis 1140 * @param int|array $object_id The id of the object(s) to retrieve.
mp-wp_genesis 1141 * @param string|array $taxonomies The taxonomies to retrieve terms from.
mp-wp_genesis 1142 * @param array|string $args Change what is returned
mp-wp_genesis 1143 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
mp-wp_genesis 1144 */
mp-wp_genesis 1145 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
mp-wp_genesis 1146 global $wpdb;
mp-wp_genesis 1147
mp-wp_genesis 1148 if ( !is_array($taxonomies) )
mp-wp_genesis 1149 $taxonomies = array($taxonomies);
mp-wp_genesis 1150
mp-wp_genesis 1151 foreach ( (array) $taxonomies as $taxonomy ) {
mp-wp_genesis 1152 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 1153 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 1154 }
mp-wp_genesis 1155
mp-wp_genesis 1156 if ( !is_array($object_ids) )
mp-wp_genesis 1157 $object_ids = array($object_ids);
mp-wp_genesis 1158 $object_ids = array_map('intval', $object_ids);
mp-wp_genesis 1159
mp-wp_genesis 1160 $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
mp-wp_genesis 1161 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 1162
mp-wp_genesis 1163 $terms = array();
mp-wp_genesis 1164 if ( count($taxonomies) > 1 ) {
mp-wp_genesis 1165 foreach ( $taxonomies as $index => $taxonomy ) {
mp-wp_genesis 1166 $t = get_taxonomy($taxonomy);
mp-wp_genesis 1167 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
mp-wp_genesis 1168 unset($taxonomies[$index]);
mp-wp_genesis 1169 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
mp-wp_genesis 1170 }
mp-wp_genesis 1171 }
mp-wp_genesis 1172 } else {
mp-wp_genesis 1173 $t = get_taxonomy($taxonomies[0]);
mp-wp_genesis 1174 if ( isset($t->args) && is_array($t->args) )
mp-wp_genesis 1175 $args = array_merge($args, $t->args);
mp-wp_genesis 1176 }
mp-wp_genesis 1177
mp-wp_genesis 1178 extract($args, EXTR_SKIP);
mp-wp_genesis 1179
mp-wp_genesis 1180 if ( 'count' == $orderby )
mp-wp_genesis 1181 $orderby = 'tt.count';
mp-wp_genesis 1182 else if ( 'name' == $orderby )
mp-wp_genesis 1183 $orderby = 't.name';
mp-wp_genesis 1184 else if ( 'slug' == $orderby )
mp-wp_genesis 1185 $orderby = 't.slug';
mp-wp_genesis 1186 else if ( 'term_group' == $orderby )
mp-wp_genesis 1187 $orderby = 't.term_group';
mp-wp_genesis 1188 else if ( 'term_order' == $orderby )
mp-wp_genesis 1189 $orderby = 'tr.term_order';
mp-wp_genesis 1190 else
mp-wp_genesis 1191 $orderby = 't.term_id';
mp-wp_genesis 1192
mp-wp_genesis 1193 $taxonomies = "'" . implode("', '", $taxonomies) . "'";
mp-wp_genesis 1194 $object_ids = implode(', ', $object_ids);
mp-wp_genesis 1195
mp-wp_genesis 1196 $select_this = '';
mp-wp_genesis 1197 if ( 'all' == $fields )
mp-wp_genesis 1198 $select_this = 't.*, tt.*';
mp-wp_genesis 1199 else if ( 'ids' == $fields )
mp-wp_genesis 1200 $select_this = 't.term_id';
mp-wp_genesis 1201 else if ( 'names' == $fields )
mp-wp_genesis 1202 $select_this = 't.name';
mp-wp_genesis 1203 else if ( 'all_with_object_id' == $fields )
mp-wp_genesis 1204 $select_this = 't.*, tt.*, tr.object_id';
mp-wp_genesis 1205
mp-wp_genesis 1206 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order";
mp-wp_genesis 1207
mp-wp_genesis 1208 if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
mp-wp_genesis 1209 $terms = array_merge($terms, $wpdb->get_results($query));
mp-wp_genesis 1210 update_term_cache($terms);
mp-wp_genesis 1211 } else if ( 'ids' == $fields || 'names' == $fields ) {
mp-wp_genesis 1212 $terms = array_merge($terms, $wpdb->get_col($query));
mp-wp_genesis 1213 } else if ( 'tt_ids' == $fields ) {
mp-wp_genesis 1214 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order");
mp-wp_genesis 1215 }
mp-wp_genesis 1216
mp-wp_genesis 1217 if ( ! $terms )
mp-wp_genesis 1218 return array();
mp-wp_genesis 1219
mp-wp_genesis 1220 return $terms;
mp-wp_genesis 1221 }
mp-wp_genesis 1222
mp-wp_genesis 1223 /**
mp-wp_genesis 1224 * Adds a new term to the database. Optionally marks it as an alias of an existing term.
mp-wp_genesis 1225 *
mp-wp_genesis 1226 * Error handling is assigned for the nonexistance of the $taxonomy and $term
mp-wp_genesis 1227 * parameters before inserting. If both the term id and taxonomy exist
mp-wp_genesis 1228 * previously, then an array will be returned that contains the term id and the
mp-wp_genesis 1229 * contents of what is returned. The keys of the array are 'term_id' and
mp-wp_genesis 1230 * 'term_taxonomy_id' containing numeric values.
mp-wp_genesis 1231 *
mp-wp_genesis 1232 * It is assumed that the term does not yet exist or the above will apply. The
mp-wp_genesis 1233 * term will be first added to the term table and then related to the taxonomy
mp-wp_genesis 1234 * if everything is well. If everything is correct, then several actions will be
mp-wp_genesis 1235 * run prior to a filter and then several actions will be run after the filter
mp-wp_genesis 1236 * is run.
mp-wp_genesis 1237 *
mp-wp_genesis 1238 * The arguments decide how the term is handled based on the $args parameter.
mp-wp_genesis 1239 * The following is a list of the available overrides and the defaults.
mp-wp_genesis 1240 *
mp-wp_genesis 1241 * 'alias_of'. There is no default, but if added, expected is the slug that the
mp-wp_genesis 1242 * term will be an alias of. Expected to be a string.
mp-wp_genesis 1243 *
mp-wp_genesis 1244 * 'description'. There is no default. If exists, will be added to the database
mp-wp_genesis 1245 * along with the term. Expected to be a string.
mp-wp_genesis 1246 *
mp-wp_genesis 1247 * 'parent'. Expected to be numeric and default is 0 (zero). Will assign value
mp-wp_genesis 1248 * of 'parent' to the term.
mp-wp_genesis 1249 *
mp-wp_genesis 1250 * 'slug'. Expected to be a string. There is no default.
mp-wp_genesis 1251 *
mp-wp_genesis 1252 * If 'slug' argument exists then the slug will be checked to see if it is not
mp-wp_genesis 1253 * a valid term. If that check succeeds (it is not a valid term), then it is
mp-wp_genesis 1254 * added and the term id is given. If it fails, then a check is made to whether
mp-wp_genesis 1255 * the taxonomy is hierarchical and the parent argument is not empty. If the
mp-wp_genesis 1256 * second check succeeds, the term will be inserted and the term id will be
mp-wp_genesis 1257 * given.
mp-wp_genesis 1258 *
mp-wp_genesis 1259 * @package WordPress
mp-wp_genesis 1260 * @subpackage Taxonomy
mp-wp_genesis 1261 * @since 2.3.0
mp-wp_genesis 1262 * @uses $wpdb
mp-wp_genesis 1263 *
mp-wp_genesis 1264 * @uses do_action() Calls 'create_term' hook with the term id and taxonomy id as parameters.
mp-wp_genesis 1265 * @uses do_action() Calls 'create_$taxonomy' hook with term id and taxonomy id as parameters.
mp-wp_genesis 1266 * @uses apply_filters() Calls 'term_id_filter' hook with term id and taxonomy id as parameters.
mp-wp_genesis 1267 * @uses do_action() Calls 'created_term' hook with the term id and taxonomy id as parameters.
mp-wp_genesis 1268 * @uses do_action() Calls 'created_$taxonomy' hook with term id and taxonomy id as parameters.
mp-wp_genesis 1269 *
mp-wp_genesis 1270 * @param int|string $term The term to add or update.
mp-wp_genesis 1271 * @param string $taxonomy The taxonomy to which to add the term
mp-wp_genesis 1272 * @param array|string $args Change the values of the inserted term
mp-wp_genesis 1273 * @return array|WP_Error The Term ID and Term Taxonomy ID
mp-wp_genesis 1274 */
mp-wp_genesis 1275 function wp_insert_term( $term, $taxonomy, $args = array() ) {
mp-wp_genesis 1276 global $wpdb;
mp-wp_genesis 1277
mp-wp_genesis 1278 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 1279 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
mp-wp_genesis 1280
mp-wp_genesis 1281 if ( is_int($term) && 0 == $term )
mp-wp_genesis 1282 return new WP_Error('invalid_term_id', __('Invalid term ID'));
mp-wp_genesis 1283
mp-wp_genesis 1284 if ( '' == trim($term) )
mp-wp_genesis 1285 return new WP_Error('empty_term_name', __('A name is required for this term'));
mp-wp_genesis 1286
mp-wp_genesis 1287 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
mp-wp_genesis 1288 $args = wp_parse_args($args, $defaults);
mp-wp_genesis 1289 $args['name'] = $term;
mp-wp_genesis 1290 $args['taxonomy'] = $taxonomy;
mp-wp_genesis 1291 $args = sanitize_term($args, $taxonomy, 'db');
mp-wp_genesis 1292 extract($args, EXTR_SKIP);
mp-wp_genesis 1293
mp-wp_genesis 1294 // expected_slashed ($name)
mp-wp_genesis 1295 $name = stripslashes($name);
mp-wp_genesis 1296 $description = stripslashes($description);
mp-wp_genesis 1297
mp-wp_genesis 1298 if ( empty($slug) )
mp-wp_genesis 1299 $slug = sanitize_title($name);
mp-wp_genesis 1300
mp-wp_genesis 1301 $term_group = 0;
mp-wp_genesis 1302 if ( $alias_of ) {
mp-wp_genesis 1303 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
mp-wp_genesis 1304 if ( $alias->term_group ) {
mp-wp_genesis 1305 // The alias we want is already in a group, so let's use that one.
mp-wp_genesis 1306 $term_group = $alias->term_group;
mp-wp_genesis 1307 } else {
mp-wp_genesis 1308 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
mp-wp_genesis 1309 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
mp-wp_genesis 1310 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $alias->term_id ) );
mp-wp_genesis 1311 }
mp-wp_genesis 1312 }
mp-wp_genesis 1313
mp-wp_genesis 1314 if ( ! $term_id = is_term($slug) ) {
mp-wp_genesis 1315 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
mp-wp_genesis 1316 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
mp-wp_genesis 1317 $term_id = (int) $wpdb->insert_id;
mp-wp_genesis 1318 } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) {
mp-wp_genesis 1319 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
mp-wp_genesis 1320 // by incorporating parent slugs.
mp-wp_genesis 1321 $slug = wp_unique_term_slug($slug, (object) $args);
mp-wp_genesis 1322 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
mp-wp_genesis 1323 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
mp-wp_genesis 1324 $term_id = (int) $wpdb->insert_id;
mp-wp_genesis 1325 }
mp-wp_genesis 1326
mp-wp_genesis 1327 if ( empty($slug) ) {
mp-wp_genesis 1328 $slug = sanitize_title($slug, $term_id);
mp-wp_genesis 1329 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
mp-wp_genesis 1330 }
mp-wp_genesis 1331
mp-wp_genesis 1332 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );
mp-wp_genesis 1333
mp-wp_genesis 1334 if ( !empty($tt_id) )
mp-wp_genesis 1335 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
mp-wp_genesis 1336
mp-wp_genesis 1337 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
mp-wp_genesis 1338 $tt_id = (int) $wpdb->insert_id;
mp-wp_genesis 1339
mp-wp_genesis 1340 do_action("create_term", $term_id, $tt_id);
mp-wp_genesis 1341 do_action("create_$taxonomy", $term_id, $tt_id);
mp-wp_genesis 1342
mp-wp_genesis 1343 $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
mp-wp_genesis 1344
mp-wp_genesis 1345 clean_term_cache($term_id, $taxonomy);
mp-wp_genesis 1346
mp-wp_genesis 1347 do_action("created_term", $term_id, $tt_id);
mp-wp_genesis 1348 do_action("created_$taxonomy", $term_id, $tt_id);
mp-wp_genesis 1349
mp-wp_genesis 1350 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
mp-wp_genesis 1351 }
mp-wp_genesis 1352
mp-wp_genesis 1353 /**
mp-wp_genesis 1354 * Create Term and Taxonomy Relationships.
mp-wp_genesis 1355 *
mp-wp_genesis 1356 * Relates an object (post, link etc) to a term and taxonomy type. Creates the
mp-wp_genesis 1357 * term and taxonomy relationship if it doesn't already exist. Creates a term if
mp-wp_genesis 1358 * it doesn't exist (using the slug).
mp-wp_genesis 1359 *
mp-wp_genesis 1360 * A relationship means that the term is grouped in or belongs to the taxonomy.
mp-wp_genesis 1361 * A term has no meaning until it is given context by defining which taxonomy it
mp-wp_genesis 1362 * exists under.
mp-wp_genesis 1363 *
mp-wp_genesis 1364 * @package WordPress
mp-wp_genesis 1365 * @subpackage Taxonomy
mp-wp_genesis 1366 * @since 2.3.0
mp-wp_genesis 1367 * @uses $wpdb
mp-wp_genesis 1368 *
mp-wp_genesis 1369 * @param int $object_id The object to relate to.
mp-wp_genesis 1370 * @param array|int|string $term The slug or id of the term.
mp-wp_genesis 1371 * @param array|string $taxonomy The context in which to relate the term to the object.
mp-wp_genesis 1372 * @param bool $append If false will delete difference of terms.
mp-wp_genesis 1373 * @return array|WP_Error Affected Term IDs
mp-wp_genesis 1374 */
mp-wp_genesis 1375 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
mp-wp_genesis 1376 global $wpdb;
mp-wp_genesis 1377
mp-wp_genesis 1378 $object_id = (int) $object_id;
mp-wp_genesis 1379
mp-wp_genesis 1380 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 1381 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
mp-wp_genesis 1382
mp-wp_genesis 1383 if ( !is_array($terms) )
mp-wp_genesis 1384 $terms = array($terms);
mp-wp_genesis 1385
mp-wp_genesis 1386 if ( ! $append )
mp-wp_genesis 1387 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
mp-wp_genesis 1388
mp-wp_genesis 1389 $tt_ids = array();
mp-wp_genesis 1390 $term_ids = array();
mp-wp_genesis 1391
mp-wp_genesis 1392 foreach ( (array) $terms as $term) {
mp-wp_genesis 1393 if ( !strlen(trim($term)) )
mp-wp_genesis 1394 continue;
mp-wp_genesis 1395
mp-wp_genesis 1396 if ( !$term_info = is_term($term, $taxonomy) )
mp-wp_genesis 1397 $term_info = wp_insert_term($term, $taxonomy);
mp-wp_genesis 1398 if ( is_wp_error($term_info) )
mp-wp_genesis 1399 return $term_info;
mp-wp_genesis 1400 $term_ids[] = $term_info['term_id'];
mp-wp_genesis 1401 $tt_id = $term_info['term_taxonomy_id'];
mp-wp_genesis 1402 $tt_ids[] = $tt_id;
mp-wp_genesis 1403
mp-wp_genesis 1404 if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
mp-wp_genesis 1405 continue;
mp-wp_genesis 1406 $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
mp-wp_genesis 1407 }
mp-wp_genesis 1408
mp-wp_genesis 1409 wp_update_term_count($tt_ids, $taxonomy);
mp-wp_genesis 1410
mp-wp_genesis 1411 if ( ! $append ) {
mp-wp_genesis 1412 $delete_terms = array_diff($old_tt_ids, $tt_ids);
mp-wp_genesis 1413 if ( $delete_terms ) {
mp-wp_genesis 1414 $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
mp-wp_genesis 1415 $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
mp-wp_genesis 1416 wp_update_term_count($delete_terms, $taxonomy);
mp-wp_genesis 1417 }
mp-wp_genesis 1418 }
mp-wp_genesis 1419
mp-wp_genesis 1420 $t = get_taxonomy($taxonomy);
mp-wp_genesis 1421 if ( ! $append && isset($t->sort) && $t->sort ) {
mp-wp_genesis 1422 $values = array();
mp-wp_genesis 1423 $term_order = 0;
mp-wp_genesis 1424 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
mp-wp_genesis 1425 foreach ( $tt_ids as $tt_id )
mp-wp_genesis 1426 if ( in_array($tt_id, $final_tt_ids) )
mp-wp_genesis 1427 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
mp-wp_genesis 1428 if ( $values )
mp-wp_genesis 1429 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
mp-wp_genesis 1430 }
mp-wp_genesis 1431
mp-wp_genesis 1432 return $tt_ids;
mp-wp_genesis 1433 }
mp-wp_genesis 1434
mp-wp_genesis 1435 /**
mp-wp_genesis 1436 * Will make slug unique, if it isn't already.
mp-wp_genesis 1437 *
mp-wp_genesis 1438 * The $slug has to be unique global to every taxonomy, meaning that one
mp-wp_genesis 1439 * taxonomy term can't have a matching slug with another taxonomy term. Each
mp-wp_genesis 1440 * slug has to be globally unique for every taxonomy.
mp-wp_genesis 1441 *
mp-wp_genesis 1442 * The way this works is that if the taxonomy that the term belongs to is
mp-wp_genesis 1443 * heirarchical and has a parent, it will append that parent to the $slug.
mp-wp_genesis 1444 *
mp-wp_genesis 1445 * If that still doesn't return an unique slug, then it try to append a number
mp-wp_genesis 1446 * until it finds a number that is truely unique.
mp-wp_genesis 1447 *
mp-wp_genesis 1448 * The only purpose for $term is for appending a parent, if one exists.
mp-wp_genesis 1449 *
mp-wp_genesis 1450 * @package WordPress
mp-wp_genesis 1451 * @subpackage Taxonomy
mp-wp_genesis 1452 * @since 2.3.0
mp-wp_genesis 1453 * @uses $wpdb
mp-wp_genesis 1454 *
mp-wp_genesis 1455 * @param string $slug The string that will be tried for a unique slug
mp-wp_genesis 1456 * @param object $term The term object that the $slug will belong too
mp-wp_genesis 1457 * @return string Will return a true unique slug.
mp-wp_genesis 1458 */
mp-wp_genesis 1459 function wp_unique_term_slug($slug, $term) {
mp-wp_genesis 1460 global $wpdb;
mp-wp_genesis 1461
mp-wp_genesis 1462 // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
mp-wp_genesis 1463 // by incorporating parent slugs.
mp-wp_genesis 1464 if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
mp-wp_genesis 1465 $the_parent = $term->parent;
mp-wp_genesis 1466 while ( ! empty($the_parent) ) {
mp-wp_genesis 1467 $parent_term = get_term($the_parent, $term->taxonomy);
mp-wp_genesis 1468 if ( is_wp_error($parent_term) || empty($parent_term) )
mp-wp_genesis 1469 break;
mp-wp_genesis 1470 $slug .= '-' . $parent_term->slug;
mp-wp_genesis 1471 if ( empty($parent_term->parent) )
mp-wp_genesis 1472 break;
mp-wp_genesis 1473 $the_parent = $parent_term->parent;
mp-wp_genesis 1474 }
mp-wp_genesis 1475 }
mp-wp_genesis 1476
mp-wp_genesis 1477 // If we didn't get a unique slug, try appending a number to make it unique.
mp-wp_genesis 1478 if ( !empty($args['term_id']) )
mp-wp_genesis 1479 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );
mp-wp_genesis 1480 else
mp-wp_genesis 1481 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
mp-wp_genesis 1482
mp-wp_genesis 1483 if ( $wpdb->get_var( $query ) ) {
mp-wp_genesis 1484 $num = 2;
mp-wp_genesis 1485 do {
mp-wp_genesis 1486 $alt_slug = $slug . "-$num";
mp-wp_genesis 1487 $num++;
mp-wp_genesis 1488 $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
mp-wp_genesis 1489 } while ( $slug_check );
mp-wp_genesis 1490 $slug = $alt_slug;
mp-wp_genesis 1491 }
mp-wp_genesis 1492
mp-wp_genesis 1493 return $slug;
mp-wp_genesis 1494 }
mp-wp_genesis 1495
mp-wp_genesis 1496 /**
mp-wp_genesis 1497 * Update term based on arguments provided.
mp-wp_genesis 1498 *
mp-wp_genesis 1499 * The $args will indiscriminately override all values with the same field name.
mp-wp_genesis 1500 * Care must be taken to not override important information need to update or
mp-wp_genesis 1501 * update will fail (or perhaps create a new term, neither would be acceptable).
mp-wp_genesis 1502 *
mp-wp_genesis 1503 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
mp-wp_genesis 1504 * defined in $args already.
mp-wp_genesis 1505 *
mp-wp_genesis 1506 * 'alias_of' will create a term group, if it doesn't already exist, and update
mp-wp_genesis 1507 * it for the $term.
mp-wp_genesis 1508 *
mp-wp_genesis 1509 * If the 'slug' argument in $args is missing, then the 'name' in $args will be
mp-wp_genesis 1510 * used. It should also be noted that if you set 'slug' and it isn't unique then
mp-wp_genesis 1511 * a WP_Error will be passed back. If you don't pass any slug, then a unique one
mp-wp_genesis 1512 * will be created for you.
mp-wp_genesis 1513 *
mp-wp_genesis 1514 * For what can be overrode in $args, check the term scheme can contain and stay
mp-wp_genesis 1515 * away from the term keys.
mp-wp_genesis 1516 *
mp-wp_genesis 1517 * @package WordPress
mp-wp_genesis 1518 * @subpackage Taxonomy
mp-wp_genesis 1519 * @since 2.3.0
mp-wp_genesis 1520 *
mp-wp_genesis 1521 * @uses $wpdb
mp-wp_genesis 1522 * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice.
mp-wp_genesis 1523 * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term
mp-wp_genesis 1524 * id and taxonomy id.
mp-wp_genesis 1525 *
mp-wp_genesis 1526 * @param int $term The ID of the term
mp-wp_genesis 1527 * @param string $taxonomy The context in which to relate the term to the object.
mp-wp_genesis 1528 * @param array|string $args Overwrite term field values
mp-wp_genesis 1529 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
mp-wp_genesis 1530 */
mp-wp_genesis 1531 function wp_update_term( $term, $taxonomy, $args = array() ) {
mp-wp_genesis 1532 global $wpdb;
mp-wp_genesis 1533
mp-wp_genesis 1534 if ( ! is_taxonomy($taxonomy) )
mp-wp_genesis 1535 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
mp-wp_genesis 1536
mp-wp_genesis 1537 $term_id = (int) $term;
mp-wp_genesis 1538
mp-wp_genesis 1539 // First, get all of the original args
mp-wp_genesis 1540 $term = get_term ($term_id, $taxonomy, ARRAY_A);
mp-wp_genesis 1541
mp-wp_genesis 1542 // Escape data pulled from DB.
mp-wp_genesis 1543 $term = add_magic_quotes($term);
mp-wp_genesis 1544
mp-wp_genesis 1545 // Merge old and new args with new args overwriting old ones.
mp-wp_genesis 1546 $args = array_merge($term, $args);
mp-wp_genesis 1547
mp-wp_genesis 1548 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
mp-wp_genesis 1549 $args = wp_parse_args($args, $defaults);
mp-wp_genesis 1550 $args = sanitize_term($args, $taxonomy, 'db');
mp-wp_genesis 1551 extract($args, EXTR_SKIP);
mp-wp_genesis 1552
mp-wp_genesis 1553 // expected_slashed ($name)
mp-wp_genesis 1554 $name = stripslashes($name);
mp-wp_genesis 1555 $description = stripslashes($description);
mp-wp_genesis 1556
mp-wp_genesis 1557 if ( '' == trim($name) )
mp-wp_genesis 1558 return new WP_Error('empty_term_name', __('A name is required for this term'));
mp-wp_genesis 1559
mp-wp_genesis 1560 $empty_slug = false;
mp-wp_genesis 1561 if ( empty($slug) ) {
mp-wp_genesis 1562 $empty_slug = true;
mp-wp_genesis 1563 $slug = sanitize_title($name);
mp-wp_genesis 1564 }
mp-wp_genesis 1565
mp-wp_genesis 1566 if ( $alias_of ) {
mp-wp_genesis 1567 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
mp-wp_genesis 1568 if ( $alias->term_group ) {
mp-wp_genesis 1569 // The alias we want is already in a group, so let's use that one.
mp-wp_genesis 1570 $term_group = $alias->term_group;
mp-wp_genesis 1571 } else {
mp-wp_genesis 1572 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
mp-wp_genesis 1573 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
mp-wp_genesis 1574 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
mp-wp_genesis 1575 }
mp-wp_genesis 1576 }
mp-wp_genesis 1577
mp-wp_genesis 1578 // Check for duplicate slug
mp-wp_genesis 1579 $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
mp-wp_genesis 1580 if ( $id && ($id != $term_id) ) {
mp-wp_genesis 1581 // If an empty slug was passed or the parent changed, reset the slug to something unique.
mp-wp_genesis 1582 // Otherwise, bail.
mp-wp_genesis 1583 if ( $empty_slug || ( $parent != $term->parent) )
mp-wp_genesis 1584 $slug = wp_unique_term_slug($slug, (object) $args);
mp-wp_genesis 1585 else
mp-wp_genesis 1586 return new WP_Error('duplicate_term_slug', sprintf(__('The slug "%s" is already in use by another term'), $slug));
mp-wp_genesis 1587 }
mp-wp_genesis 1588
mp-wp_genesis 1589 $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
mp-wp_genesis 1590
mp-wp_genesis 1591 if ( empty($slug) ) {
mp-wp_genesis 1592 $slug = sanitize_title($name, $term_id);
mp-wp_genesis 1593 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
mp-wp_genesis 1594 }
mp-wp_genesis 1595
mp-wp_genesis 1596 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
mp-wp_genesis 1597
mp-wp_genesis 1598 $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
mp-wp_genesis 1599
mp-wp_genesis 1600 do_action("edit_term", $term_id, $tt_id);
mp-wp_genesis 1601 do_action("edit_$taxonomy", $term_id, $tt_id);
mp-wp_genesis 1602
mp-wp_genesis 1603 $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
mp-wp_genesis 1604
mp-wp_genesis 1605 clean_term_cache($term_id, $taxonomy);
mp-wp_genesis 1606
mp-wp_genesis 1607 do_action("edited_term", $term_id, $tt_id);
mp-wp_genesis 1608 do_action("edited_$taxonomy", $term_id, $tt_id);
mp-wp_genesis 1609
mp-wp_genesis 1610 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
mp-wp_genesis 1611 }
mp-wp_genesis 1612
mp-wp_genesis 1613 /**
mp-wp_genesis 1614 * Enable or disable term counting.
mp-wp_genesis 1615 *
mp-wp_genesis 1616 * @since 2.5.0
mp-wp_genesis 1617 *
mp-wp_genesis 1618 * @param bool $defer Optional. Enable if true, disable if false.
mp-wp_genesis 1619 * @return bool Whether term counting is enabled or disabled.
mp-wp_genesis 1620 */
mp-wp_genesis 1621 function wp_defer_term_counting($defer=null) {
mp-wp_genesis 1622 static $_defer = false;
mp-wp_genesis 1623
mp-wp_genesis 1624 if ( is_bool($defer) ) {
mp-wp_genesis 1625 $_defer = $defer;
mp-wp_genesis 1626 // flush any deferred counts
mp-wp_genesis 1627 if ( !$defer )
mp-wp_genesis 1628 wp_update_term_count( null, null, true );
mp-wp_genesis 1629 }
mp-wp_genesis 1630
mp-wp_genesis 1631 return $_defer;
mp-wp_genesis 1632 }
mp-wp_genesis 1633
mp-wp_genesis 1634 /**
mp-wp_genesis 1635 * Updates the amount of terms in taxonomy.
mp-wp_genesis 1636 *
mp-wp_genesis 1637 * If there is a taxonomy callback applyed, then it will be called for updating
mp-wp_genesis 1638 * the count.
mp-wp_genesis 1639 *
mp-wp_genesis 1640 * The default action is to count what the amount of terms have the relationship
mp-wp_genesis 1641 * of term ID. Once that is done, then update the database.
mp-wp_genesis 1642 *
mp-wp_genesis 1643 * @package WordPress
mp-wp_genesis 1644 * @subpackage Taxonomy
mp-wp_genesis 1645 * @since 2.3.0
mp-wp_genesis 1646 * @uses $wpdb
mp-wp_genesis 1647 *
mp-wp_genesis 1648 * @param int|array $terms The term_taxonomy_id of the terms
mp-wp_genesis 1649 * @param string $taxonomy The context of the term.
mp-wp_genesis 1650 * @return bool If no terms will return false, and if successful will return true.
mp-wp_genesis 1651 */
mp-wp_genesis 1652 function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {
mp-wp_genesis 1653 static $_deferred = array();
mp-wp_genesis 1654
mp-wp_genesis 1655 if ( $do_deferred ) {
mp-wp_genesis 1656 foreach ( (array) array_keys($_deferred) as $tax ) {
mp-wp_genesis 1657 wp_update_term_count_now( $_deferred[$tax], $tax );
mp-wp_genesis 1658 unset( $_deferred[$tax] );
mp-wp_genesis 1659 }
mp-wp_genesis 1660 }
mp-wp_genesis 1661
mp-wp_genesis 1662 if ( empty($terms) )
mp-wp_genesis 1663 return false;
mp-wp_genesis 1664
mp-wp_genesis 1665 if ( !is_array($terms) )
mp-wp_genesis 1666 $terms = array($terms);
mp-wp_genesis 1667
mp-wp_genesis 1668 if ( wp_defer_term_counting() ) {
mp-wp_genesis 1669 if ( !isset($_deferred[$taxonomy]) )
mp-wp_genesis 1670 $_deferred[$taxonomy] = array();
mp-wp_genesis 1671 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
mp-wp_genesis 1672 return true;
mp-wp_genesis 1673 }
mp-wp_genesis 1674
mp-wp_genesis 1675 return wp_update_term_count_now( $terms, $taxonomy );
mp-wp_genesis 1676 }
mp-wp_genesis 1677
mp-wp_genesis 1678 /**
mp-wp_genesis 1679 * Perform term count update immediately.
mp-wp_genesis 1680 *
mp-wp_genesis 1681 * @since 2.5.0
mp-wp_genesis 1682 *
mp-wp_genesis 1683 * @param array $terms The term_taxonomy_id of terms to update.
mp-wp_genesis 1684 * @param string $taxonomy The context of the term.
mp-wp_genesis 1685 * @return bool Always true when complete.
mp-wp_genesis 1686 */
mp-wp_genesis 1687 function wp_update_term_count_now( $terms, $taxonomy ) {
mp-wp_genesis 1688 global $wpdb;
mp-wp_genesis 1689
mp-wp_genesis 1690 $terms = array_map('intval', $terms);
mp-wp_genesis 1691
mp-wp_genesis 1692 $taxonomy = get_taxonomy($taxonomy);
mp-wp_genesis 1693 if ( !empty($taxonomy->update_count_callback) ) {
mp-wp_genesis 1694 call_user_func($taxonomy->update_count_callback, $terms);
mp-wp_genesis 1695 } else {
mp-wp_genesis 1696 // Default count updater
mp-wp_genesis 1697 foreach ( (array) $terms as $term) {
mp-wp_genesis 1698 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) );
mp-wp_genesis 1699 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
mp-wp_genesis 1700 }
mp-wp_genesis 1701
mp-wp_genesis 1702 }
mp-wp_genesis 1703
mp-wp_genesis 1704 clean_term_cache($terms);
mp-wp_genesis 1705
mp-wp_genesis 1706 return true;
mp-wp_genesis 1707 }
mp-wp_genesis 1708
mp-wp_genesis 1709 //
mp-wp_genesis 1710 // Cache
mp-wp_genesis 1711 //
mp-wp_genesis 1712
mp-wp_genesis 1713
mp-wp_genesis 1714 /**
mp-wp_genesis 1715 * Removes the taxonomy relationship to terms from the cache.
mp-wp_genesis 1716 *
mp-wp_genesis 1717 * Will remove the entire taxonomy relationship containing term $object_id. The
mp-wp_genesis 1718 * term IDs have to exist within the taxonomy $object_type for the deletion to
mp-wp_genesis 1719 * take place.
mp-wp_genesis 1720 *
mp-wp_genesis 1721 * @package WordPress
mp-wp_genesis 1722 * @subpackage Taxonomy
mp-wp_genesis 1723 * @since 2.3.0
mp-wp_genesis 1724 *
mp-wp_genesis 1725 * @see get_object_taxonomies() for more on $object_type
mp-wp_genesis 1726 * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion.
mp-wp_genesis 1727 * Passes, function params in same order.
mp-wp_genesis 1728 *
mp-wp_genesis 1729 * @param int|array $object_ids Single or list of term object ID(s)
mp-wp_genesis 1730 * @param array|string $object_type The taxonomy object type
mp-wp_genesis 1731 */
mp-wp_genesis 1732 function clean_object_term_cache($object_ids, $object_type) {
mp-wp_genesis 1733 if ( !is_array($object_ids) )
mp-wp_genesis 1734 $object_ids = array($object_ids);
mp-wp_genesis 1735
mp-wp_genesis 1736 foreach ( $object_ids as $id )
mp-wp_genesis 1737 foreach ( get_object_taxonomies($object_type) as $taxonomy )
mp-wp_genesis 1738 wp_cache_delete($id, "{$taxonomy}_relationships");
mp-wp_genesis 1739
mp-wp_genesis 1740 do_action('clean_object_term_cache', $object_ids, $object_type);
mp-wp_genesis 1741 }
mp-wp_genesis 1742
mp-wp_genesis 1743
mp-wp_genesis 1744 /**
mp-wp_genesis 1745 * Will remove all of the term ids from the cache.
mp-wp_genesis 1746 *
mp-wp_genesis 1747 * @package WordPress
mp-wp_genesis 1748 * @subpackage Taxonomy
mp-wp_genesis 1749 * @since 2.3.0
mp-wp_genesis 1750 * @uses $wpdb
mp-wp_genesis 1751 *
mp-wp_genesis 1752 * @param int|array $ids Single or list of Term IDs
mp-wp_genesis 1753 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
mp-wp_genesis 1754 */
mp-wp_genesis 1755 function clean_term_cache($ids, $taxonomy = '') {
mp-wp_genesis 1756 global $wpdb;
mp-wp_genesis 1757 static $cleaned = array();
mp-wp_genesis 1758
mp-wp_genesis 1759 if ( !is_array($ids) )
mp-wp_genesis 1760 $ids = array($ids);
mp-wp_genesis 1761
mp-wp_genesis 1762 $taxonomies = array();
mp-wp_genesis 1763 // If no taxonomy, assume tt_ids.
mp-wp_genesis 1764 if ( empty($taxonomy) ) {
mp-wp_genesis 1765 $tt_ids = implode(', ', $ids);
mp-wp_genesis 1766 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
mp-wp_genesis 1767 foreach ( (array) $terms as $term ) {
mp-wp_genesis 1768 $taxonomies[] = $term->taxonomy;
mp-wp_genesis 1769 wp_cache_delete($term->term_id, $term->taxonomy);
mp-wp_genesis 1770 }
mp-wp_genesis 1771 $taxonomies = array_unique($taxonomies);
mp-wp_genesis 1772 } else {
mp-wp_genesis 1773 foreach ( $ids as $id ) {
mp-wp_genesis 1774 wp_cache_delete($id, $taxonomy);
mp-wp_genesis 1775 }
mp-wp_genesis 1776 $taxonomies = array($taxonomy);
mp-wp_genesis 1777 }
mp-wp_genesis 1778
mp-wp_genesis 1779 foreach ( $taxonomies as $taxonomy ) {
mp-wp_genesis 1780 if ( isset($cleaned[$taxonomy]) )
mp-wp_genesis 1781 continue;
mp-wp_genesis 1782 $cleaned[$taxonomy] = true;
mp-wp_genesis 1783 wp_cache_delete('all_ids', $taxonomy);
mp-wp_genesis 1784 wp_cache_delete('get', $taxonomy);
mp-wp_genesis 1785 delete_option("{$taxonomy}_children");
mp-wp_genesis 1786 }
mp-wp_genesis 1787
mp-wp_genesis 1788 wp_cache_set('last_changed', time(), 'terms');
mp-wp_genesis 1789
mp-wp_genesis 1790 do_action('clean_term_cache', $ids, $taxonomy);
mp-wp_genesis 1791 }
mp-wp_genesis 1792
mp-wp_genesis 1793
mp-wp_genesis 1794 /**
mp-wp_genesis 1795 * Retrieves the taxonomy relationship to the term object id.
mp-wp_genesis 1796 *
mp-wp_genesis 1797 * @package WordPress
mp-wp_genesis 1798 * @subpackage Taxonomy
mp-wp_genesis 1799 * @since 2.3.0
mp-wp_genesis 1800 *
mp-wp_genesis 1801 * @uses wp_cache_get() Retrieves taxonomy relationship from cache
mp-wp_genesis 1802 *
mp-wp_genesis 1803 * @param int|array $id Term object ID
mp-wp_genesis 1804 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 1805 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
mp-wp_genesis 1806 */
mp-wp_genesis 1807 function &get_object_term_cache($id, $taxonomy) {
mp-wp_genesis 1808 $cache = wp_cache_get($id, "{$taxonomy}_relationships");
mp-wp_genesis 1809 return $cache;
mp-wp_genesis 1810 }
mp-wp_genesis 1811
mp-wp_genesis 1812
mp-wp_genesis 1813 /**
mp-wp_genesis 1814 * Updates the cache for Term ID(s).
mp-wp_genesis 1815 *
mp-wp_genesis 1816 * Will only update the cache for terms not already cached.
mp-wp_genesis 1817 *
mp-wp_genesis 1818 * The $object_ids expects that the ids be separated by commas, if it is a
mp-wp_genesis 1819 * string.
mp-wp_genesis 1820 *
mp-wp_genesis 1821 * It should be noted that update_object_term_cache() is very time extensive. It
mp-wp_genesis 1822 * is advised that the function is not called very often or at least not for a
mp-wp_genesis 1823 * lot of terms that exist in a lot of taxonomies. The amount of time increases
mp-wp_genesis 1824 * for each term and it also increases for each taxonomy the term belongs to.
mp-wp_genesis 1825 *
mp-wp_genesis 1826 * @package WordPress
mp-wp_genesis 1827 * @subpackage Taxonomy
mp-wp_genesis 1828 * @since 2.3.0
mp-wp_genesis 1829 * @uses wp_get_object_terms() Used to get terms from the database to update
mp-wp_genesis 1830 *
mp-wp_genesis 1831 * @param string|array $object_ids Single or list of term object ID(s)
mp-wp_genesis 1832 * @param array|string $object_type The taxonomy object type
mp-wp_genesis 1833 * @return null|bool Null value is given with empty $object_ids. False if
mp-wp_genesis 1834 */
mp-wp_genesis 1835 function update_object_term_cache($object_ids, $object_type) {
mp-wp_genesis 1836 if ( empty($object_ids) )
mp-wp_genesis 1837 return;
mp-wp_genesis 1838
mp-wp_genesis 1839 if ( !is_array($object_ids) )
mp-wp_genesis 1840 $object_ids = explode(',', $object_ids);
mp-wp_genesis 1841
mp-wp_genesis 1842 $object_ids = array_map('intval', $object_ids);
mp-wp_genesis 1843
mp-wp_genesis 1844 $taxonomies = get_object_taxonomies($object_type);
mp-wp_genesis 1845
mp-wp_genesis 1846 $ids = array();
mp-wp_genesis 1847 foreach ( (array) $object_ids as $id ) {
mp-wp_genesis 1848 foreach ( $taxonomies as $taxonomy ) {
mp-wp_genesis 1849 if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
mp-wp_genesis 1850 $ids[] = $id;
mp-wp_genesis 1851 break;
mp-wp_genesis 1852 }
mp-wp_genesis 1853 }
mp-wp_genesis 1854 }
mp-wp_genesis 1855
mp-wp_genesis 1856 if ( empty( $ids ) )
mp-wp_genesis 1857 return false;
mp-wp_genesis 1858
mp-wp_genesis 1859 $terms = wp_get_object_terms($ids, $taxonomies, 'fields=all_with_object_id');
mp-wp_genesis 1860
mp-wp_genesis 1861 $object_terms = array();
mp-wp_genesis 1862 foreach ( (array) $terms as $term )
mp-wp_genesis 1863 $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
mp-wp_genesis 1864
mp-wp_genesis 1865 foreach ( $ids as $id ) {
mp-wp_genesis 1866 foreach ( $taxonomies as $taxonomy ) {
mp-wp_genesis 1867 if ( ! isset($object_terms[$id][$taxonomy]) ) {
mp-wp_genesis 1868 if ( !isset($object_terms[$id]) )
mp-wp_genesis 1869 $object_terms[$id] = array();
mp-wp_genesis 1870 $object_terms[$id][$taxonomy] = array();
mp-wp_genesis 1871 }
mp-wp_genesis 1872 }
mp-wp_genesis 1873 }
mp-wp_genesis 1874
mp-wp_genesis 1875 foreach ( $object_terms as $id => $value ) {
mp-wp_genesis 1876 foreach ( $value as $taxonomy => $terms ) {
mp-wp_genesis 1877 wp_cache_set($id, $terms, "{$taxonomy}_relationships");
mp-wp_genesis 1878 }
mp-wp_genesis 1879 }
mp-wp_genesis 1880 }
mp-wp_genesis 1881
mp-wp_genesis 1882
mp-wp_genesis 1883 /**
mp-wp_genesis 1884 * Updates Terms to Taxonomy in cache.
mp-wp_genesis 1885 *
mp-wp_genesis 1886 * @package WordPress
mp-wp_genesis 1887 * @subpackage Taxonomy
mp-wp_genesis 1888 * @since 2.3.0
mp-wp_genesis 1889 *
mp-wp_genesis 1890 * @param array $terms List of Term objects to change
mp-wp_genesis 1891 * @param string $taxonomy Optional. Update Term to this taxonomy in cache
mp-wp_genesis 1892 */
mp-wp_genesis 1893 function update_term_cache($terms, $taxonomy = '') {
mp-wp_genesis 1894 foreach ( (array) $terms as $term ) {
mp-wp_genesis 1895 $term_taxonomy = $taxonomy;
mp-wp_genesis 1896 if ( empty($term_taxonomy) )
mp-wp_genesis 1897 $term_taxonomy = $term->taxonomy;
mp-wp_genesis 1898
mp-wp_genesis 1899 wp_cache_add($term->term_id, $term, $term_taxonomy);
mp-wp_genesis 1900 }
mp-wp_genesis 1901 }
mp-wp_genesis 1902
mp-wp_genesis 1903 //
mp-wp_genesis 1904 // Private
mp-wp_genesis 1905 //
mp-wp_genesis 1906
mp-wp_genesis 1907
mp-wp_genesis 1908 /**
mp-wp_genesis 1909 * Retrieves children of taxonomy.
mp-wp_genesis 1910 *
mp-wp_genesis 1911 * @package WordPress
mp-wp_genesis 1912 * @subpackage Taxonomy
mp-wp_genesis 1913 * @access private
mp-wp_genesis 1914 * @since 2.3.0
mp-wp_genesis 1915 *
mp-wp_genesis 1916 * @uses update_option() Stores all of the children in "$taxonomy_children"
mp-wp_genesis 1917 * option. That is the name of the taxonomy, immediately followed by '_children'.
mp-wp_genesis 1918 *
mp-wp_genesis 1919 * @param string $taxonomy Taxonomy Name
mp-wp_genesis 1920 * @return array Empty if $taxonomy isn't hierarachical or returns children.
mp-wp_genesis 1921 */
mp-wp_genesis 1922 function _get_term_hierarchy($taxonomy) {
mp-wp_genesis 1923 if ( !is_taxonomy_hierarchical($taxonomy) )
mp-wp_genesis 1924 return array();
mp-wp_genesis 1925 $children = get_option("{$taxonomy}_children");
mp-wp_genesis 1926 if ( is_array($children) )
mp-wp_genesis 1927 return $children;
mp-wp_genesis 1928
mp-wp_genesis 1929 $children = array();
mp-wp_genesis 1930 $terms = get_terms($taxonomy, 'get=all');
mp-wp_genesis 1931 foreach ( $terms as $term ) {
mp-wp_genesis 1932 if ( $term->parent > 0 )
mp-wp_genesis 1933 $children[$term->parent][] = $term->term_id;
mp-wp_genesis 1934 }
mp-wp_genesis 1935 update_option("{$taxonomy}_children", $children);
mp-wp_genesis 1936
mp-wp_genesis 1937 return $children;
mp-wp_genesis 1938 }
mp-wp_genesis 1939
mp-wp_genesis 1940
mp-wp_genesis 1941 /**
mp-wp_genesis 1942 * Get the subset of $terms that are descendants of $term_id.
mp-wp_genesis 1943 *
mp-wp_genesis 1944 * If $terms is an array of objects, then _get_term_children returns an array of objects.
mp-wp_genesis 1945 * If $terms is an array of IDs, then _get_term_children returns an array of IDs.
mp-wp_genesis 1946 *
mp-wp_genesis 1947 * @package WordPress
mp-wp_genesis 1948 * @subpackage Taxonomy
mp-wp_genesis 1949 * @access private
mp-wp_genesis 1950 * @since 2.3.0
mp-wp_genesis 1951 *
mp-wp_genesis 1952 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.
mp-wp_genesis 1953 * @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen.
mp-wp_genesis 1954 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
mp-wp_genesis 1955 * @return array The subset of $terms that are descendants of $term_id.
mp-wp_genesis 1956 */
mp-wp_genesis 1957 function &_get_term_children($term_id, $terms, $taxonomy) {
mp-wp_genesis 1958 $empty_array = array();
mp-wp_genesis 1959 if ( empty($terms) )
mp-wp_genesis 1960 return $empty_array;
mp-wp_genesis 1961
mp-wp_genesis 1962 $term_list = array();
mp-wp_genesis 1963 $has_children = _get_term_hierarchy($taxonomy);
mp-wp_genesis 1964
mp-wp_genesis 1965 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
mp-wp_genesis 1966 return $empty_array;
mp-wp_genesis 1967
mp-wp_genesis 1968 foreach ( (array) $terms as $term ) {
mp-wp_genesis 1969 $use_id = false;
mp-wp_genesis 1970 if ( !is_object($term) ) {
mp-wp_genesis 1971 $term = get_term($term, $taxonomy);
mp-wp_genesis 1972 if ( is_wp_error( $term ) )
mp-wp_genesis 1973 return $term;
mp-wp_genesis 1974 $use_id = true;
mp-wp_genesis 1975 }
mp-wp_genesis 1976
mp-wp_genesis 1977 if ( $term->term_id == $term_id )
mp-wp_genesis 1978 continue;
mp-wp_genesis 1979
mp-wp_genesis 1980 if ( $term->parent == $term_id ) {
mp-wp_genesis 1981 if ( $use_id )
mp-wp_genesis 1982 $term_list[] = $term->term_id;
mp-wp_genesis 1983 else
mp-wp_genesis 1984 $term_list[] = $term;
mp-wp_genesis 1985
mp-wp_genesis 1986 if ( !isset($has_children[$term->term_id]) )
mp-wp_genesis 1987 continue;
mp-wp_genesis 1988
mp-wp_genesis 1989 if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
mp-wp_genesis 1990 $term_list = array_merge($term_list, $children);
mp-wp_genesis 1991 }
mp-wp_genesis 1992 }
mp-wp_genesis 1993
mp-wp_genesis 1994 return $term_list;
mp-wp_genesis 1995 }
mp-wp_genesis 1996
mp-wp_genesis 1997
mp-wp_genesis 1998 /**
mp-wp_genesis 1999 * Add count of children to parent count.
mp-wp_genesis 2000 *
mp-wp_genesis 2001 * Recalculates term counts by including items from child terms. Assumes all
mp-wp_genesis 2002 * relevant children are already in the $terms argument.
mp-wp_genesis 2003 *
mp-wp_genesis 2004 * @package WordPress
mp-wp_genesis 2005 * @subpackage Taxonomy
mp-wp_genesis 2006 * @access private
mp-wp_genesis 2007 * @since 2.3.0
mp-wp_genesis 2008 * @uses $wpdb
mp-wp_genesis 2009 *
mp-wp_genesis 2010 * @param array $terms List of Term IDs
mp-wp_genesis 2011 * @param string $taxonomy Term Context
mp-wp_genesis 2012 * @return null Will break from function if conditions are not met.
mp-wp_genesis 2013 */
mp-wp_genesis 2014 function _pad_term_counts(&$terms, $taxonomy) {
mp-wp_genesis 2015 global $wpdb;
mp-wp_genesis 2016
mp-wp_genesis 2017 // This function only works for post categories.
mp-wp_genesis 2018 if ( 'category' != $taxonomy )
mp-wp_genesis 2019 return;
mp-wp_genesis 2020
mp-wp_genesis 2021 $term_hier = _get_term_hierarchy($taxonomy);
mp-wp_genesis 2022
mp-wp_genesis 2023 if ( empty($term_hier) )
mp-wp_genesis 2024 return;
mp-wp_genesis 2025
mp-wp_genesis 2026 $term_items = array();
mp-wp_genesis 2027
mp-wp_genesis 2028 foreach ( (array) $terms as $key => $term ) {
mp-wp_genesis 2029 $terms_by_id[$term->term_id] = & $terms[$key];
mp-wp_genesis 2030 $term_ids[$term->term_taxonomy_id] = $term->term_id;
mp-wp_genesis 2031 }
mp-wp_genesis 2032
mp-wp_genesis 2033 // Get the object and term ids and stick them in a lookup table
mp-wp_genesis 2034 $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") AND post_type = 'post' AND post_status = 'publish'");
mp-wp_genesis 2035 foreach ( $results as $row ) {
mp-wp_genesis 2036 $id = $term_ids[$row->term_taxonomy_id];
mp-wp_genesis 2037 $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
mp-wp_genesis 2038 }
mp-wp_genesis 2039
mp-wp_genesis 2040 // Touch every ancestor's lookup row for each post in each term
mp-wp_genesis 2041 foreach ( $term_ids as $term_id ) {
mp-wp_genesis 2042 $child = $term_id;
mp-wp_genesis 2043 while ( $parent = $terms_by_id[$child]->parent ) {
mp-wp_genesis 2044 if ( !empty($term_items[$term_id]) )
mp-wp_genesis 2045 foreach ( $term_items[$term_id] as $item_id => $touches ) {
mp-wp_genesis 2046 $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
mp-wp_genesis 2047 }
mp-wp_genesis 2048 $child = $parent;
mp-wp_genesis 2049 }
mp-wp_genesis 2050 }
mp-wp_genesis 2051
mp-wp_genesis 2052 // Transfer the touched cells
mp-wp_genesis 2053 foreach ( (array) $term_items as $id => $items )
mp-wp_genesis 2054 if ( isset($terms_by_id[$id]) )
mp-wp_genesis 2055 $terms_by_id[$id]->count = count($items);
mp-wp_genesis 2056 }
mp-wp_genesis 2057
mp-wp_genesis 2058 //
mp-wp_genesis 2059 // Default callbacks
mp-wp_genesis 2060 //
mp-wp_genesis 2061
mp-wp_genesis 2062 /**
mp-wp_genesis 2063 * Will update term count based on posts.
mp-wp_genesis 2064 *
mp-wp_genesis 2065 * Private function for the default callback for post_tag and category
mp-wp_genesis 2066 * taxonomies.
mp-wp_genesis 2067 *
mp-wp_genesis 2068 * @package WordPress
mp-wp_genesis 2069 * @subpackage Taxonomy
mp-wp_genesis 2070 * @access private
mp-wp_genesis 2071 * @since 2.3.0
mp-wp_genesis 2072 * @uses $wpdb
mp-wp_genesis 2073 *
mp-wp_genesis 2074 * @param array $terms List of Term taxonomy IDs
mp-wp_genesis 2075 */
mp-wp_genesis 2076 function _update_post_term_count( $terms ) {
mp-wp_genesis 2077 global $wpdb;
mp-wp_genesis 2078
mp-wp_genesis 2079 foreach ( (array) $terms as $term ) {
mp-wp_genesis 2080 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term ) );
mp-wp_genesis 2081 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
mp-wp_genesis 2082 }
mp-wp_genesis 2083 }
mp-wp_genesis 2084
mp-wp_genesis 2085
mp-wp_genesis 2086 /**
mp-wp_genesis 2087 * Generates a permalink for a taxonomy term archive.
mp-wp_genesis 2088 *
mp-wp_genesis 2089 * @since 2.5.0
mp-wp_genesis 2090 *
mp-wp_genesis 2091 * @param object|int|string $term
mp-wp_genesis 2092 * @param string $taxonomy
mp-wp_genesis 2093 * @return string HTML link to taxonomy term archive
mp-wp_genesis 2094 */
mp-wp_genesis 2095 function get_term_link( $term, $taxonomy ) {
mp-wp_genesis 2096 global $wp_rewrite;
mp-wp_genesis 2097
mp-wp_genesis 2098 // use legacy functions for core taxonomies until they are fully plugged in
mp-wp_genesis 2099 if ( $taxonomy == 'category' )
mp-wp_genesis 2100 return get_category_link($term);
mp-wp_genesis 2101 if ( $taxonomy == 'post_tag' )
mp-wp_genesis 2102 return get_tag_link($term);
mp-wp_genesis 2103
mp-wp_genesis 2104 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
mp-wp_genesis 2105
mp-wp_genesis 2106 if ( !is_object($term) ) {
mp-wp_genesis 2107 if ( is_int($term) ) {
mp-wp_genesis 2108 $term = &get_term($term, $taxonomy);
mp-wp_genesis 2109 } else {
mp-wp_genesis 2110 $term = &get_term_by('slug', $term, $taxonomy);
mp-wp_genesis 2111 }
mp-wp_genesis 2112 }
mp-wp_genesis 2113 if ( is_wp_error( $term ) )
mp-wp_genesis 2114 return $term;
mp-wp_genesis 2115
mp-wp_genesis 2116 $slug = $term->slug;
mp-wp_genesis 2117
mp-wp_genesis 2118 if ( empty($termlink) ) {
mp-wp_genesis 2119 $file = get_option('home') . '/';
mp-wp_genesis 2120 $t = get_taxonomy($taxonomy);
mp-wp_genesis 2121 if ( $t->query_var )
mp-wp_genesis 2122 $termlink = "$file?$t->query_var=$slug";
mp-wp_genesis 2123 else
mp-wp_genesis 2124 $termlink = "$file?taxonomy=$taxonomy&term=$slug";
mp-wp_genesis 2125 } else {
mp-wp_genesis 2126 $termlink = str_replace("%$taxonomy%", $slug, $termlink);
mp-wp_genesis 2127 $termlink = get_option('home') . user_trailingslashit($termlink, 'category');
mp-wp_genesis 2128 }
mp-wp_genesis 2129 return apply_filters('term_link', $termlink, $term, $taxonomy);
mp-wp_genesis 2130 }
mp-wp_genesis 2131
mp-wp_genesis 2132 /**
mp-wp_genesis 2133 * Display the taxonomies of a post with available options.
mp-wp_genesis 2134 *
mp-wp_genesis 2135 * This function can be used within the loop to display the taxonomies for a
mp-wp_genesis 2136 * post without specifying the Post ID. You can also use it outside the Loop to
mp-wp_genesis 2137 * display the taxonomies for a specific post.
mp-wp_genesis 2138 *
mp-wp_genesis 2139 * The available defaults are:
mp-wp_genesis 2140 * 'post' : default is 0. The post ID to get taxonomies of.
mp-wp_genesis 2141 * 'before' : default is empty string. Display before taxonomies list.
mp-wp_genesis 2142 * 'sep' : default is empty string. Separate every taxonomy with value in this.
mp-wp_genesis 2143 * 'after' : default is empty string. Display this after the taxonomies list.
mp-wp_genesis 2144 *
mp-wp_genesis 2145 * @since 2.5.0
mp-wp_genesis 2146 * @uses get_the_taxonomies()
mp-wp_genesis 2147 *
mp-wp_genesis 2148 * @param array $args Override the defaults.
mp-wp_genesis 2149 */
mp-wp_genesis 2150 function the_taxonomies($args = array()) {
mp-wp_genesis 2151 $defaults = array(
mp-wp_genesis 2152 'post' => 0,
mp-wp_genesis 2153 'before' => '',
mp-wp_genesis 2154 'sep' => ' ',
mp-wp_genesis 2155 'after' => '',
mp-wp_genesis 2156 );
mp-wp_genesis 2157
mp-wp_genesis 2158 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 2159 extract( $r, EXTR_SKIP );
mp-wp_genesis 2160
mp-wp_genesis 2161 echo $before . join($sep, get_the_taxonomies($post)) . $after;
mp-wp_genesis 2162 }
mp-wp_genesis 2163
mp-wp_genesis 2164 /**
mp-wp_genesis 2165 * Retrieve all taxonomies associated with a post.
mp-wp_genesis 2166 *
mp-wp_genesis 2167 * This function can be used within the loop. It will also return an array of
mp-wp_genesis 2168 * the taxonomies with links to the taxonomy and name.
mp-wp_genesis 2169 *
mp-wp_genesis 2170 * @since 2.5.0
mp-wp_genesis 2171 *
mp-wp_genesis 2172 * @param int $post Optional. Post ID or will use Global Post ID (in loop).
mp-wp_genesis 2173 * @return array
mp-wp_genesis 2174 */
mp-wp_genesis 2175 function get_the_taxonomies($post = 0) {
mp-wp_genesis 2176 if ( is_int($post) )
mp-wp_genesis 2177 $post =& get_post($post);
mp-wp_genesis 2178 elseif ( !is_object($post) )
mp-wp_genesis 2179 $post =& $GLOBALS['post'];
mp-wp_genesis 2180
mp-wp_genesis 2181 $taxonomies = array();
mp-wp_genesis 2182
mp-wp_genesis 2183 if ( !$post )
mp-wp_genesis 2184 return $taxonomies;
mp-wp_genesis 2185
mp-wp_genesis 2186 $template = apply_filters('taxonomy_template', '%s: %l.');
mp-wp_genesis 2187
mp-wp_genesis 2188 foreach ( get_object_taxonomies($post) as $taxonomy ) {
mp-wp_genesis 2189 $t = (array) get_taxonomy($taxonomy);
mp-wp_genesis 2190 if ( empty($t['label']) )
mp-wp_genesis 2191 $t['label'] = $taxonomy;
mp-wp_genesis 2192 if ( empty($t['args']) )
mp-wp_genesis 2193 $t['args'] = array();
mp-wp_genesis 2194 if ( empty($t['template']) )
mp-wp_genesis 2195 $t['template'] = $template;
mp-wp_genesis 2196
mp-wp_genesis 2197 $terms = get_object_term_cache($post->ID, $taxonomy);
mp-wp_genesis 2198 if ( empty($terms) )
mp-wp_genesis 2199 $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
mp-wp_genesis 2200
mp-wp_genesis 2201 $links = array();
mp-wp_genesis 2202
mp-wp_genesis 2203 foreach ( $terms as $term )
mp-wp_genesis 2204 $links[] = "<a href='" . attribute_escape(get_term_link($term, $taxonomy)) . "'>$term->name</a>";
mp-wp_genesis 2205
mp-wp_genesis 2206 if ( $links )
mp-wp_genesis 2207 $taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);
mp-wp_genesis 2208 }
mp-wp_genesis 2209 return $taxonomies;
mp-wp_genesis 2210 }
mp-wp_genesis 2211
mp-wp_genesis 2212 /**
mp-wp_genesis 2213 * Retrieve all taxonomies of a post with just the names.
mp-wp_genesis 2214 *
mp-wp_genesis 2215 * @since 2.5.0
mp-wp_genesis 2216 * @uses get_object_taxonomies()
mp-wp_genesis 2217 *
mp-wp_genesis 2218 * @param int $post Optional. Post ID
mp-wp_genesis 2219 * @return array
mp-wp_genesis 2220 */
mp-wp_genesis 2221 function get_post_taxonomies($post = 0) {
mp-wp_genesis 2222 $post =& get_post($post);
mp-wp_genesis 2223
mp-wp_genesis 2224 return get_object_taxonomies($post);
mp-wp_genesis 2225 }
mp-wp_genesis 2226
mp-wp_genesis 2227 /**
mp-wp_genesis 2228 * Determine if the given object is associated with any of the given terms.
mp-wp_genesis 2229 *
mp-wp_genesis 2230 * The given terms are checked against the object's terms' term_ids, names and slugs.
mp-wp_genesis 2231 * Terms given as integers will only be checked against the object's terms' term_ids.
mp-wp_genesis 2232 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
mp-wp_genesis 2233 *
mp-wp_genesis 2234 * @since 2.7.0
mp-wp_genesis 2235 * @uses get_object_term_cache()
mp-wp_genesis 2236 * @uses wp_get_object_terms()
mp-wp_genesis 2237 *
mp-wp_genesis 2238 * @param int $object_id. ID of the object (post ID, link ID, ...)
mp-wp_genesis 2239 * @param string $taxonomy. Single taxonomy name
mp-wp_genesis 2240 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said
mp-wp_genesis 2241 * @return bool|WP_Error. WP_Error on input error.
mp-wp_genesis 2242 */
mp-wp_genesis 2243 function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
mp-wp_genesis 2244 if ( !$object_id = (int) $object_id )
mp-wp_genesis 2245 return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
mp-wp_genesis 2246
mp-wp_genesis 2247 $object_terms = get_object_term_cache( $object_id, $taxonomy );
mp-wp_genesis 2248 if ( empty( $object_terms ) )
mp-wp_genesis 2249 $object_terms = wp_get_object_terms( $object_id, $taxonomy );
mp-wp_genesis 2250
mp-wp_genesis 2251 if ( is_wp_error( $object_terms ) )
mp-wp_genesis 2252 return $object_terms;
mp-wp_genesis 2253 if ( empty( $object_terms ) )
mp-wp_genesis 2254 return false;
mp-wp_genesis 2255 if ( empty( $terms ) )
mp-wp_genesis 2256 return ( !empty( $object_terms ) );
mp-wp_genesis 2257
mp-wp_genesis 2258 $terms = (array) $terms;
mp-wp_genesis 2259
mp-wp_genesis 2260 if ( $ints = array_filter( $terms, 'is_int' ) )
mp-wp_genesis 2261 $strs = array_diff( $terms, $ints );
mp-wp_genesis 2262 else
mp-wp_genesis 2263 $strs =& $terms;
mp-wp_genesis 2264
mp-wp_genesis 2265 foreach ( $object_terms as $object_term ) {
mp-wp_genesis 2266 if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
mp-wp_genesis 2267 if ( $strs ) {
mp-wp_genesis 2268 if ( in_array( $object_term->term_id, $strs ) ) return true;
mp-wp_genesis 2269 if ( in_array( $object_term->name, $strs ) ) return true;
mp-wp_genesis 2270 if ( in_array( $object_term->slug, $strs ) ) return true;
mp-wp_genesis 2271 }
mp-wp_genesis 2272 }
mp-wp_genesis 2273
mp-wp_genesis 2274 return false;
mp-wp_genesis 2275 }
mp-wp_genesis 2276
mp-wp_genesis 2277 ?>