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