wp/wp-includes/taxonomy.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
first import
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Taxonomy API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
// Taxonomy Registration
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * Creates the initial taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * This function fires twice: in wp-settings.php before plugins are loaded (for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * backwards compatibility reasons), and again on the 'init' action. We must avoid
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * registering rewrite rules before the 'init' action.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
function create_initial_taxonomies() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
	global $wp_rewrite;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	if ( ! did_action( 'init' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		$rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		$post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		$rewrite = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			'category' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
				'hierarchical' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
				'slug' => get_option('category_base') ? get_option('category_base') : 'category',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
				'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
				'ep_mask' => EP_CATEGORIES,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
			'post_tag' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
				'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
				'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
				'ep_mask' => EP_TAGS,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	register_taxonomy( 'category', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		'hierarchical' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		'query_var' => 'category_name',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		'rewrite' => $rewrite['category'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		'show_ui' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		'show_admin_column' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	register_taxonomy( 'post_tag', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	 	'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		'query_var' => 'tag',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		'rewrite' => $rewrite['post_tag'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		'show_ui' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		'show_admin_column' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	register_taxonomy( 'nav_menu', 'nav_menu_item', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			'name' => __( 'Navigation Menus' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			'singular_name' => __( 'Navigation Menu' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		'show_in_nav_menus' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	register_taxonomy( 'link_category', 'link', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			'name' => __( 'Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
			'singular_name' => __( 'Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			'search_items' => __( 'Search Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			'popular_items' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			'all_items' => __( 'All Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			'edit_item' => __( 'Edit Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			'update_item' => __( 'Update Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			'add_new_item' => __( 'Add New Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			'new_item_name' => __( 'New Link Category Name' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			'separate_items_with_commas' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			'add_or_remove_items' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			'choose_from_most_used' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		'capabilities' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			'manage_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			'edit_terms'   => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			'delete_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			'assign_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	register_taxonomy( 'post_format', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
			'name' => _x( 'Format', 'post format' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
			'singular_name' => _x( 'Format', 'post format' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		'query_var' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		'rewrite' => $rewrite['post_format'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
 * Get a list of registered taxonomy objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 * @see register_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 * @param array $args An array of key => value arguments to match against the taxonomy objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 * @param string $operator The logical operation to perform. 'or' means only one element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 * @return array A list of taxonomy names or objects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
	$field = ('names' == $output) ? 'name' : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
	return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
 * Return all of the taxonomy names that are of $object_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 * It appears that this function can be used to find all of the names inside of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
 * $wp_taxonomies global variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
 * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
 * result in <code>Array('category', 'post_tag')</code>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
 * @return array The names of all taxonomy of $object_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
function get_object_taxonomies($object, $output = 'names') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	if ( is_object($object) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		if ( $object->post_type == 'attachment' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
			return get_attachment_taxonomies($object);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		$object = $object->post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	$object = (array) $object;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		if ( array_intersect($object, (array) $tax_obj->object_type) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
			if ( 'names' == $output )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
				$taxonomies[] = $tax_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
				$taxonomies[ $tax_name ] = $tax_obj;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
 * Retrieves the taxonomy object of $taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 * The get_taxonomy function will first check that the parameter string given
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 * is a taxonomy object and if it is, it will return it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
 * @uses taxonomy_exists() Checks whether taxonomy exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
 * @param string $taxonomy Name of taxonomy object to return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
 * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
function get_taxonomy( $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	if ( ! taxonomy_exists( $taxonomy ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
	return $wp_taxonomies[$taxonomy];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * Checks that the taxonomy name exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 * Formerly is_taxonomy(), introduced in 2.3.0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
 * @return bool Whether the taxonomy exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
function taxonomy_exists( $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	return isset( $wp_taxonomies[$taxonomy] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
 * Whether the taxonomy object is hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
 * Checks to make sure that the taxonomy is an object first. Then Gets the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
 * object, and finally returns the hierarchical value in the object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
 * A false return value might also mean that the taxonomy does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
 * @uses taxonomy_exists() Checks whether taxonomy exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
 * @uses get_taxonomy() Used to get the taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 * @return bool Whether the taxonomy is hierarchical
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
function is_taxonomy_hierarchical($taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
	$taxonomy = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	return $taxonomy->hierarchical;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
 * Create or modify a taxonomy object. Do not use before init.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
 * A simple function for creating or modifying a taxonomy object based on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
 * parameters given. The function will accept an array (third optional
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
 * parameter), along with strings for the taxonomy name and another string for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
 * the object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 * Nothing is returned, so expect error maybe or use taxonomy_exists() to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
 * whether taxonomy exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
 * Optional $args contents:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
 * - label - Name of the taxonomy shown in the menu. Usually plural. If not set, labels['name'] will be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
 * - labels - An array of labels for this taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
 *     * By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
 *     * You can see accepted values in {@link get_taxonomy_labels()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
 * - description - A short descriptive summary of what the taxonomy is for. Defaults to blank.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
 * - public - If the taxonomy should be publicly queryable; //@TODO not implemented.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
 *     * Defaults to true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
 * - hierarchical - Whether the taxonomy is hierarchical (e.g. category). Defaults to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
 * - show_ui -Whether to generate a default UI for managing this taxonomy in the admin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
 *     * If not set, the default is inherited from public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
 * - show_in_menu - Where to show the taxonomy in the admin menu.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 *     * If true, the taxonomy is shown as a submenu of the object type menu.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
 *     * If false, no menu is shown.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
 *     * show_ui must be true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
 *     * If not set, the default is inherited from show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
 * - show_in_nav_menus - Makes this taxonomy available for selection in navigation menus.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
 *     * If not set, the default is inherited from public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
 * - show_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
 *     * If not set, the default is inherited from show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
 * - meta_box_cb - Provide a callback function for the meta box display. Defaults to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
 *     post_categories_meta_box for hierarchical taxonomies and post_tags_meta_box for non-hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
 * - capabilities - Array of capabilities for this taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
 *     * You can see accepted values in this function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 * - rewrite - Triggers the handling of rewrites for this taxonomy. Defaults to true, using $taxonomy as slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 *     * To prevent rewrite, set to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 *     * To specify rewrite rules, an array can be passed with any of these keys
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 *         * 'slug' => string Customize the permastruct slug. Defaults to $taxonomy key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
 *         * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 *         * 'hierarchical' => bool Either hierarchical rewrite tag or not. Defaults to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 *         * 'ep_mask' => const Assign an endpoint mask.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 *             * If not specified, defaults to EP_NONE.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 * - query_var - Sets the query_var key for this taxonomy. Defaults to $taxonomy key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
 *     * If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
 *     * If specified as a string, the query ?{query_var_string}={term_slug} will be valid.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
 * - update_count_callback - Works much like a hook, in that it will be called when the count is updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
 *     * Defaults to _update_post_term_count() for taxonomies attached to post types, which then confirms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
 *       that the objects are published before counting them.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
 *     * Defaults to _update_generic_term_count() for taxonomies attached to other object types, such as links.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
 * - _builtin - true if this taxonomy is a native or "built-in" taxonomy. THIS IS FOR INTERNAL USE ONLY!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
 * @uses $wp_taxonomies Inserts new taxonomy object into the list
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
 * @uses $wp Adds query vars
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
 * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
 * @param array|string $object_type Name of the object type for the taxonomy object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
 * @param array|string $args See optional args description above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
 * @return null|WP_Error WP_Error if errors, otherwise null.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
	global $wp_taxonomies, $wp;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	if ( ! is_array( $wp_taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		$wp_taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
		'labels'                => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		'description'           => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
		'public'                => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		'hierarchical'          => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		'show_ui'               => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		'show_in_menu'          => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		'show_in_nav_menus'     => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
		'show_tagcloud'         => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		'meta_box_cb'           => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		'capabilities'          => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
		'rewrite'               => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		'query_var'             => $taxonomy,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		'update_count_callback' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		'_builtin'              => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	if ( strlen( $taxonomy ) > 32 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
		return new WP_Error( 'taxonomy_too_long', __( 'Taxonomies cannot exceed 32 characters in length' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
	if ( false !== $args['query_var'] && ! empty( $wp ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		if ( true === $args['query_var'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
			$args['query_var'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
			$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
		$wp->add_query_var( $args['query_var'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		$args['rewrite'] = wp_parse_args( $args['rewrite'], array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
			'with_front' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
			'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
			'ep_mask' => EP_NONE,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		if ( empty( $args['rewrite']['slug'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
			$args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
			$tag = '(.+?)';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
			$tag = '([^/]+)';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	if ( null === $args['show_ui'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
		$args['show_ui'] = $args['public'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	// If not set, default to the setting for show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	if ( null === $args['show_in_menu' ] || ! $args['show_ui'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
		$args['show_in_menu' ] = $args['show_ui'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	if ( null === $args['show_in_nav_menus'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		$args['show_in_nav_menus'] = $args['public'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	// If not set, default to the setting for show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	if ( null === $args['show_tagcloud'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		$args['show_tagcloud'] = $args['show_ui'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	$default_caps = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		'manage_terms' => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		'edit_terms'   => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		'delete_terms' => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
		'assign_terms' => 'edit_posts',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
	$args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
	unset( $args['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
	$args['name'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	$args['object_type'] = array_unique( (array) $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
	$args['labels'] = get_taxonomy_labels( (object) $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
	$args['label'] = $args['labels']->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	// If not set, use the default meta box
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	if ( null === $args['meta_box_cb'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
		if ( $args['hierarchical'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
			$args['meta_box_cb'] = 'post_categories_meta_box';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
			$args['meta_box_cb'] = 'post_tags_meta_box';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
	$wp_taxonomies[ $taxonomy ] = (object) $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
	// register callback handling for metabox
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 	add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
	do_action( 'registered_taxonomy', $taxonomy, $object_type, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
 * Builds an object with all taxonomy labels out of a taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
 * Accepted keys of the label array in the taxonomy object:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
 * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 * - singular_name - name for one object of this taxonomy. Default is Tag/Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
 * - search_items - Default is Search Tags/Search Categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
 * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 * - all_items - Default is All Tags/All Categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
 * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
 * - parent_item_colon - The same as <code>parent_item</code>, but with colon <code>:</code> in the end
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
 * - edit_item - Default is Edit Tag/Edit Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
 * - view_item - Default is View Tag/View Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
 * - update_item - Default is Update Tag/Update Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
 * - add_new_item - Default is Add New Tag/Add New Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
 * - new_item_name - Default is New Tag Name/New Category Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
 * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
 * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags", used in the meta box when JavaScript is disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
 * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags", used in the meta box.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
 * - not_found - This string isn't used on hierarchical taxonomies. Default is "No tags found", used in the meta box.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
 * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
 * @param object $tax Taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
 * @return object object with all the labels as member variables
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
function get_taxonomy_labels( $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	$tax->labels = (array) $tax->labels;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
		$tax->labels['separate_items_with_commas'] = $tax->helps;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
		$tax->labels['not_found'] = $tax->no_tagcloud;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	$nohier_vs_hier_defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
		'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
		'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		'popular_items' => array( __( 'Popular Tags' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
		'parent_item' => array( null, __( 'Parent Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
		'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
		'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
		'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
		'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
		'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		'not_found' => array( __( 'No tags found.' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
 * Add an already registered taxonomy to an object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
 * @uses $wp_taxonomies Modifies taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
 * @param string $object_type Name of the object type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
 * @return bool True if successful, false if not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
function register_taxonomy_for_object_type( $taxonomy, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	if ( !isset($wp_taxonomies[$taxonomy]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	if ( ! get_post_type_object($object_type) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
		$wp_taxonomies[$taxonomy]->object_type[] = $object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
 * Remove an already registered taxonomy from an object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
 * @param string $taxonomy    Name of taxonomy object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
 * @param string $object_type Name of the object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
 * @return bool True if successful, false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
	if ( ! get_post_type_object( $object_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
	$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	if ( false === $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
	unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
// Term API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
 * Retrieve object_ids of valid taxonomy and term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
 * The strings of $taxonomies must exist before this function will continue. On
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
 * failure of finding a valid taxonomy, it will return an WP_Error class, kind
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
 * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
 * still test for the WP_Error class and get the error message.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
 * The $terms aren't checked the same as $taxonomies, but still need to exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
 * for $object_ids to be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
 * It is possible to change the order that object_ids is returned by either
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
 * using PHP sort family functions or using the database by using $args with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
 * either ASC or DESC array. The value should be in the key named 'order'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
 * @uses wp_parse_args() Creates an array from string $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
 * @param int|array $term_ids Term id or array of term ids of terms that will be used
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
 * @param array|string $args Change the order of the object_ids, either ASC or DESC
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
 *	the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	if ( ! is_array( $term_ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
		$term_ids = array( $term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	if ( ! is_array( $taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
		$taxonomies = array( $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	foreach ( (array) $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
		if ( ! taxonomy_exists( $taxonomy ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
	$defaults = array( 'order' => 'ASC' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	extract( $args, EXTR_SKIP );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	$order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
	$term_ids = array_map('intval', $term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	$taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
	$term_ids = "'" . implode( "', '", $term_ids ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	$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 ($term_ids) ORDER BY tr.object_id $order");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
	if ( ! $object_ids )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	return $object_ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
 * Given a taxonomy query, generates SQL to be appended to a main query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
 * @see WP_Tax_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
 * @param array $tax_query A compact tax query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
 * @param string $primary_table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
 * @param string $primary_id_column
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	$tax_query_obj = new WP_Tax_Query( $tax_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
	return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
 * Container class for a multiple taxonomy query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
class WP_Tax_Query {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
	 * List of taxonomy queries. A single taxonomy query is an associative array:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
	 * - 'taxonomy' string The taxonomy being queried
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
	 * - 'terms' string|array The list of terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	 * - 'field' string (optional) Which term field is being used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
	 *		Possible values: 'term_id', 'slug' or 'name'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
	 *		Default: 'term_id'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	 * - 'operator' string (optional)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
	 *		Possible values: 'AND', 'IN' or 'NOT IN'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
	 *		Default: 'IN'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
	 * - 'include_children' bool (optional) Whether to include child terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
	 *		Default: true
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
	public $queries = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
	 * The relation between the queries. Can be one of 'AND' or 'OR'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
	public $relation;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
	 * Standard response when the query should not return any rows.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
	 * Constructor.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
	 * Parses a compact tax query and sets defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
	 * @param array $tax_query A compact tax query:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
	 *  array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
	 *    'relation' => 'OR',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
	 *    array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
	 *      'taxonomy' => 'tax1',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
	 *      'terms' => array( 'term1', 'term2' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
	 *      'field' => 'slug',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
	 *    ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
	 *    array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	 *      'taxonomy' => 'tax2',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
	 *      'terms' => array( 'term-a', 'term-b' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
	 *      'field' => 'slug',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
	 *    ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
	 *  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
	public function __construct( $tax_query ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
		if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
			$this->relation = 'OR';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
			$this->relation = 'AND';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
			'taxonomy' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
			'terms' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
			'include_children' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
			'field' => 'term_id',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
			'operator' => 'IN',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
		foreach ( $tax_query as $query ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
			if ( ! is_array( $query ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
			$query = array_merge( $defaults, $query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
			$query['terms'] = (array) $query['terms'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
			$this->queries[] = $query;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	 * Generates SQL clauses to be appended to a main query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
	 * @param string $primary_table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
	 * @param string $primary_id_column
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
	 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
	public function get_sql( $primary_table, $primary_id_column ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
		$join = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		$where = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
		$count = count( $this->queries );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		foreach ( $this->queries as $index => $query ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
			$this->clean_query( $query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
			if ( is_wp_error( $query ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
				return self::$no_results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
			extract( $query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
			if ( 'IN' == $operator ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
				if ( empty( $terms ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
					if ( 'OR' == $this->relation ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
						if ( ( $index + 1 === $count ) && empty( $where ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
							return self::$no_results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
						continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
					} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
						return self::$no_results;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
				$terms = implode( ',', $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
				$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
				$join .= " INNER JOIN $wpdb->term_relationships";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
				$join .= $i ? " AS $alias" : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
				$join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
				$where[] = "$alias.term_taxonomy_id $operator ($terms)";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
			} elseif ( 'NOT IN' == $operator ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
				if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
				$terms = implode( ',', $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
				$where[] = "$primary_table.$primary_id_column NOT IN (
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
					SELECT object_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
					FROM $wpdb->term_relationships
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
					WHERE term_taxonomy_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
				)";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
			} elseif ( 'AND' == $operator ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
				if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
				$num_terms = count( $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
				$terms = implode( ',', $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
				$where[] = "(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
					SELECT COUNT(1)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
					FROM $wpdb->term_relationships
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
					WHERE term_taxonomy_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
					AND object_id = $primary_table.$primary_id_column
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
				) = $num_terms";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
			$i++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
		if ( ! empty( $where ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
			$where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
			$where = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
		return compact( 'join', 'where' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	 * Validates a single query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
	 * @param array &$query The single query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
	private function clean_query( &$query ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
		if ( ! taxonomy_exists( $query['taxonomy'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
			$query = new WP_Error( 'Invalid taxonomy' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
		$query['terms'] = array_unique( (array) $query['terms'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
		if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
			$this->transform_query( $query, 'term_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
			if ( is_wp_error( $query ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
			$children = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
			foreach ( $query['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
				$children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
				$children[] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
			$query['terms'] = $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
		$this->transform_query( $query, 'term_taxonomy_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	 * Transforms a single query, from one field to another.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
	 * @param array &$query The single query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
	 * @param string $resulting_field The resulting field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
	public function transform_query( &$query, $resulting_field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
		if ( empty( $query['terms'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
		if ( $query['field'] == $resulting_field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
		$resulting_field = sanitize_key( $resulting_field );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
		switch ( $query['field'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
			case 'slug':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
			case 'name':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
				$terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
					SELECT $wpdb->term_taxonomy.$resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
					INNER JOIN $wpdb->terms USING (term_id)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
					WHERE taxonomy = '{$query['taxonomy']}'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
					AND $wpdb->terms.{$query['field']} IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
			case 'term_taxonomy_id':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
					SELECT $resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
					WHERE term_taxonomy_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
					SELECT $resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
					WHERE taxonomy = '{$query['taxonomy']}'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
					AND term_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
		if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
			$query = new WP_Error( 'Inexistent terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
		$query['terms'] = $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
		$query['field'] = $resulting_field;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
 * Get all Term data from database by Term ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
 * The usage of the get_term function is to apply filters to a term object. It
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
 * is possible to get a term object from the database before applying the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
 * filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
 * $term ID must be part of $taxonomy, to get from the database. Failure, might
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
 * be able to be captured by the hooks. Failure would be the same value as $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
 * returns for the get_row method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
 * There are two hooks, one is specifically for each term, named 'get_term', and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
 * term object, and the taxonomy name as parameters. Both hooks are expected to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
 * return a Term object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
 * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
 * Must return term object. Used in get_term() as a catch-all filter for every
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
 * $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
 * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
 * name. Must return term object. $taxonomy will be the taxonomy name, so for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
 * example, if 'category', it would be 'get_category' as the filter name. Useful
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
 * for custom taxonomies or plugging into default taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
 * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
 * @param string $taxonomy Taxonomy name that $term is part of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
 * exist then WP_Error will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
	$null = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
	if ( empty($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
		$error = new WP_Error('invalid_term', __('Empty Term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
		return $error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
	if ( ! taxonomy_exists($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
		return $error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
	if ( is_object($term) && empty($term->filter) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		wp_cache_add($term->term_id, $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
		$_term = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
		if ( is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
			$term = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
		if ( !$term = (int) $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
			return $null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
		if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
			$_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 = %d LIMIT 1", $taxonomy, $term) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
			if ( ! $_term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
				return $null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
			wp_cache_add($term, $_term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
	$_term = apply_filters('get_term', $_term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
	$_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
	$_term = sanitize_term($_term, $taxonomy, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
		return $_term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
		$__term = get_object_vars($_term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
		return $__term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
		$__term = array_values(get_object_vars($_term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
		return $__term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
		return $_term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
 * Get all Term data from database by Term field and data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
 * required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
 * The default $field is 'id', therefore it is possible to also use null for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
 * field, but not recommended that you do so.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
 * If $value does not exist, the return value will be false. If $taxonomy exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
 * and $field and $value combinations exist, the Term will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
 * @param string|int $value Search for this term value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
	if ( 'slug' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		$field = 't.slug';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		$value = sanitize_title($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		if ( empty($value) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
	} else if ( 'name' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
		// Assume already escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
		$value = wp_unslash($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		$field = 't.name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
	} else if ( 'term_taxonomy_id' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		$field = 'tt.term_taxonomy_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
		$term = get_term( (int) $value, $taxonomy, $output, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
			$term = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	$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) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	if ( !$term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	wp_cache_add($term->term_id, $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	$term = apply_filters('get_term', $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	$term = apply_filters("get_$taxonomy", $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
	$term = sanitize_term($term, $taxonomy, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
		return get_object_vars($term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
		return array_values(get_object_vars($term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
 * Merge all term children into a single array of their IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
 * This recursive function will merge all of the children of $term into the same
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
 * array of term IDs. Only useful for taxonomies which are hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
 * Will return an empty array if $term does not exist in $taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
 * @uses _get_term_hierarchy()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
 * @param string $term_id ID of Term to get children
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
function get_term_children( $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
	$term_id = intval( $term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	$terms = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
	if ( ! isset($terms[$term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
	$children = $terms[$term_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
	foreach ( (array) $terms[$term_id] as $child ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		if ( isset($terms[$child]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
			$children = array_merge($children, get_term_children($child, $taxonomy));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
	return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
 * Get sanitized Term field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
 * Does checks for $term, based on the $taxonomy. The function is for contextual
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
 * reasons and for simplicity of usage. See sanitize_term_field() for more
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
 * information.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
 * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
 * @param string $field Term field to fetch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
 * @param int $term Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
 * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
	$term = (int) $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
	$term = get_term( $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
	if ( is_wp_error($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
	if ( !isset($term->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
	return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
 * Sanitizes Term for editing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
 * Return value is sanitize_term() and usage is for sanitizing the term for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
 * editing. Function is for contextual and simplicity.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
 * @uses sanitize_term() Passes the return value on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
 * @param int|object $id Term ID or Object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
 * @return mixed|null|WP_Error Will return empty string if $term is not an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
function get_term_to_edit( $id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
	$term = get_term( $id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
	if ( is_wp_error($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
	return sanitize_term($term, $taxonomy, 'edit');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
 * Retrieve the terms in a given taxonomy or list of taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
 * You can fully inject any customizations to the query before it is sent, as
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
 * well as control the output with a filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
 * The 'get_terms' filter will be called when the cache has the term and will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
 * pass the found term along with the array of $taxonomies and array of $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
 * This filter is also called before the array of terms is passed and will pass
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
 * the array of terms, along with the $taxonomies and $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
 * The 'list_terms_exclusions' filter passes the compiled exclusions along with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
 * the $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
 * The 'get_terms_orderby' filter passes the ORDER BY clause for the query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
 * along with the $args array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
 * The 'get_terms_fields' filter passes the fields for the SELECT query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
 * along with the $args array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
 * The list of arguments that $args can contain, which will overwrite the defaults:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
 * orderby - Default is 'name'. Can be name, count, term_group, slug or nothing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
 * (will use term_id), Passing a custom value other than these will cause it to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
 * order based on the custom value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
 * order - Default is ASC. Can use DESC.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
 * hide_empty - Default is true. Will not return empty terms, which means
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
 * terms whose count is 0 according to the given taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
 * exclude - Default is an empty array. An array, comma- or space-delimited string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
 * of term ids to exclude from the return array. If 'include' is non-empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
 * 'exclude' is ignored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
 * exclude_tree - Default is an empty array. An array, comma- or space-delimited
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
 * string of term ids to exclude from the return array, along with all of their
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
 * descendant terms according to the primary taxonomy. If 'include' is non-empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
 * 'exclude_tree' is ignored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
 * include - Default is an empty array. An array, comma- or space-delimited string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
 * of term ids to include in the return array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
 * number - The maximum number of terms to return. Default is to return them all.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
 * offset - The number by which to offset the terms query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
 * fields - Default is 'all', which returns an array of term objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
 * If 'fields' is 'ids' or 'names', returns an array of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
 * integers or strings, respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
 * slug - Returns terms whose "slug" matches this value. Default is empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
 * hierarchical - Whether to include terms that have non-empty descendants
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
 * (even if 'hide_empty' is set to true).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
 * search - Returned terms' names will contain the value of 'search',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
 * case-insensitive. Default is an empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
 * name__like - Returned terms' names will contain the value of 'name__like',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
 * case-insensitive. Default is empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
 * description__like - Returned terms' descriptions will contain the value of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
 *  'description__like', case-insensitive. Default is empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
 * The argument 'pad_counts', if set to true will include the quantity of a term's
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
 * children in the quantity of each term's "count" object variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
 * The 'get' argument, if set to 'all' instead of its default empty string,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
 * returns terms regardless of ancestry or whether the terms are empty.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
 * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
 * is 0. If set to a non-zero value, all returned terms will be descendants
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
 * of that term according to the given taxonomy. Hence 'child_of' is set to 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
 * make term ancestry ambiguous.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
 * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
 * the empty string '', which has a different meaning from the integer 0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
 * If set to an integer value, all returned terms will have as an immediate
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
 * ancestor the term whose ID is specified by that integer according to the given taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
 * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
 * The 'cache_domain' argument enables a unique cache key to be produced when this query is stored
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
 * in object cache. For instance, if you are using one of this function's filters to modify the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
 * query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
 * the cache for similar queries. Default value is 'core'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
 * @param string|array $args The values of what to search for when returning terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
function get_terms($taxonomies, $args = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
	$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
	$single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
	if ( ! is_array( $taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
		$taxonomies = array( $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
	foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
		if ( ! taxonomy_exists($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
			$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
			return $error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
	$defaults = array('orderby' => 'name', 'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
		'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
		'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
		'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
		'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
	$args['number'] = absint( $args['number'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
	$args['offset'] = absint( $args['offset'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
	if ( !$single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ||
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
		'' !== $args['parent'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
		$args['child_of'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
		$args['hierarchical'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
		$args['pad_counts'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
	if ( 'all' == $args['get'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
		$args['child_of'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
		$args['hide_empty'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
		$args['hierarchical'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
		$args['pad_counts'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
	$args = apply_filters( 'get_terms_args', $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
	extract($args, EXTR_SKIP);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
	if ( $child_of ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
		$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
		if ( ! isset( $hierarchy[ $child_of ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
			return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
	if ( $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
		$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
		if ( ! isset( $hierarchy[ $parent ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
			return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
	// $args can be whatever, only use the args defined in defaults to compute the key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
	$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
	$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
	$last_changed = wp_cache_get( 'last_changed', 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
	if ( ! $last_changed ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
		$last_changed = microtime();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
		wp_cache_set( 'last_changed', $last_changed, 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
	$cache_key = "get_terms:$key:$last_changed";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
	$cache = wp_cache_get( $cache_key, 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
	if ( false !== $cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
		$cache = apply_filters('get_terms', $cache, $taxonomies, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
		return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
	$_orderby = strtolower($orderby);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
	if ( 'count' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
		$orderby = 'tt.count';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
	else if ( 'name' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
		$orderby = 't.name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
	else if ( 'slug' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
		$orderby = 't.slug';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
	else if ( 'term_group' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
		$orderby = 't.term_group';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
	else if ( 'none' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
		$orderby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
	elseif ( empty($_orderby) || 'id' == $_orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
		$orderby = 't.term_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
		$orderby = 't.name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
	$orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
	if ( !empty($orderby) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
		$orderby = "ORDER BY $orderby";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
		$order = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
	$order = strtoupper( $order );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
	if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
		$order = 'ASC';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1349
	$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
	$inclusions = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
	if ( ! empty( $include ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
		$exclude = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
		$exclude_tree = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
		$inclusions = implode( ',', wp_parse_id_list( $include ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
	if ( ! empty( $inclusions ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
		$inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
		$where .= $inclusions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
	$exclusions = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
	if ( ! empty( $exclude_tree ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
		$exclude_tree = wp_parse_id_list( $exclude_tree );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
		$excluded_children = $exclude_tree;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
		foreach ( $exclude_tree as $extrunk ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
			$excluded_children = array_merge(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
				$excluded_children,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
				(array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		$exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
	if ( ! empty( $exclude ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		$exterms = wp_parse_id_list( $exclude );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
		if ( empty( $exclusions ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
			$exclusions = implode( ',', $exterms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
			$exclusions .= ', ' . implode( ',', $exterms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
	if ( ! empty( $exclusions ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
		$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
	$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
	if ( ! empty( $exclusions ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
		$where .= $exclusions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
	if ( !empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
		$slug = sanitize_title($slug);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
		$where .= " AND t.slug = '$slug'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
	if ( !empty($name__like) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
		$name__like = like_escape( $name__like );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
		$where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
	if ( ! empty( $description__like ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
		$description__like = like_escape( $description__like );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
		$where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
	if ( '' !== $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
		$parent = (int) $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
		$where .= " AND tt.parent = '$parent'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
	if ( 'count' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
		$hierarchical = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
	if ( $hide_empty && !$hierarchical )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
		$where .= ' AND tt.count > 0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
	// don't limit the query results when we have to descend the family tree
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
	if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
		if ( $offset )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
			$limits = 'LIMIT ' . $offset . ',' . $number;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
			$limits = 'LIMIT ' . $number;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		$limits = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
	if ( ! empty( $search ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
		$search = like_escape( $search );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
		$where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
	$selects = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
	switch ( $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
		case 'all':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
			$selects = array( 't.*', 'tt.*' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
		case 'ids':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
		case 'id=>parent':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
			$selects = array( 't.term_id', 'tt.parent', 'tt.count' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
		case 'names':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
			$selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
		case 'count':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
			$orderby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
			$order = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
			$selects = array( 'COUNT(*)' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
		case 'id=>name':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
			$selects = array( 't.term_id', 't.name' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
		case 'id=>slug':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
			$selects = array( 't.term_id', 't.slug' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
	$_fields = $fields;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
	$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
	$join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
	$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
	$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
	foreach ( $pieces as $piece )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
		$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
	$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
	$fields = $_fields;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
	if ( 'count' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
		$term_count = $wpdb->get_var($query);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
		return $term_count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
	$terms = $wpdb->get_results($query);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
	if ( 'all' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
		update_term_cache($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
	if ( empty($terms) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
		wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
		$terms = apply_filters('get_terms', array(), $taxonomies, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
		return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
	if ( $child_of ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
		$children = _get_term_hierarchy( reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
		if ( ! empty( $children ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
			$terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
	// Update term counts to include children.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
	if ( $pad_counts && 'all' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
		_pad_term_counts( $terms, reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
	// Make sure we show empty categories that have children.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
	if ( $hierarchical && $hide_empty && is_array( $terms ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
		foreach ( $terms as $k => $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
			if ( ! $term->count ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
				$children = _get_term_children( $term->term_id, $terms, reset( $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
				if ( is_array( $children ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
					foreach ( $children as $child )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
						if ( $child->count )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
							continue 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
				// It really is empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
				unset($terms[$k]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
	reset( $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
	$_terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
	if ( 'id=>parent' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
		while ( $term = array_shift( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
			$_terms[$term->term_id] = $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
	} elseif ( 'ids' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
		while ( $term = array_shift( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
			$_terms[] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
	} elseif ( 'names' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
		while ( $term = array_shift( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
			$_terms[] = $term->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
	} elseif ( 'id=>name' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
		while ( $term = array_shift( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
			$_terms[$term->term_id] = $term->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
	} elseif ( 'id=>slug' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
		while ( $term = array_shift( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
			$_terms[$term->term_id] = $term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
	if ( ! empty( $_terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
		$terms = $_terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
	if ( $number && is_array( $terms ) && count( $terms ) > $number )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
		$terms = array_slice( $terms, $offset, $number );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
	wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
	$terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
	return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
 * Check if Term exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
 * Formerly is_term(), introduced in 2.3.0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
 * @param int|string $term The term to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
 * @param string $taxonomy The taxonomy name to use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
 * @param int $parent ID of parent term under which to confine the exists search.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
 * 	and the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
function term_exists($term, $taxonomy = '', $parent = 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
	$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
	$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 ";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
	if ( is_int($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
		if ( 0 == $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
			return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
		$where = 't.term_id = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
		if ( !empty($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
			return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
			return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
	$term = trim( wp_unslash( $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
	if ( '' === $slug = sanitize_title($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
	$where = 't.slug = %s';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
	$else_where = 't.name = %s';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
	$where_fields = array($slug);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
	$else_where_fields = array($term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
	if ( !empty($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
		$parent = (int) $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
		if ( $parent > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
			$where_fields[] = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
			$else_where_fields[] = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
			$where .= ' AND tt.parent = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
			$else_where .= ' AND tt.parent = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
		$where_fields[] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
		$else_where_fields[] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
		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) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
		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);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
	if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1606
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
	return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
 * Check if a term is an ancestor of another term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
 * You can use either an id or the term object for both parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
 * @param int|object $term1 ID or object to check if this is the parent term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
 * @param int|object $term2 The child term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
 * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
 * @return bool Whether $term2 is child of $term1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
	if ( ! isset( $term1->term_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
		$term1 = get_term( $term1, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
	if ( ! isset( $term2->parent ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
		$term2 = get_term( $term2, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
	if ( empty( $term1->term_id ) || empty( $term2->parent ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
	if ( $term2->parent == $term1->term_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
	return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
 * Sanitize Term all fields.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
 * this function will sanitize <strong>all</strong> fields. The context is based
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
 * on sanitize_term_field().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
 * The $term is expected to be either an array or an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
 * @uses sanitize_term_field Used to sanitize all fields in a term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
 * @param array|object $term The term to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
 * @param string $taxonomy The taxonomy name to use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
 * @param string $context Default is 'display'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
 * @return array|object Term with all fields sanitized
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
function sanitize_term($term, $taxonomy, $context = 'display') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
	$fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
	$do_object = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
	if ( is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
		$do_object = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
	$term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
	foreach ( (array) $fields as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
		if ( $do_object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
			if ( isset($term->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
				$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
			if ( isset($term[$field]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
				$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
	if ( $do_object )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
		$term->filter = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
		$term['filter'] = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
	return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
 * Cleanse the field value in the term based on the context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
 * Passing a term field value through the function should be assumed to have
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
 * cleansed the value for whatever context the term field is going to be used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
 * If no context or an unsupported context is given, then default filters will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
 * be applied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
 * There are enough filters for each context to support a custom filtering
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
 * without creating your own filter function. Simply create a function that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
 * hooks into the filter you need.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
 * @param string $field Term field to sanitize
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
 * @param string $value Search for this term value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
 * @param int $term_id Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
 * @param string $context Either edit, db, display, attribute, or js.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
 * @return mixed sanitized field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
	if ( 'parent' == $field  || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
		if ( $value < 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
			$value = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
	if ( 'edit' == $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
		$value = apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
		$value = apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
		if ( 'description' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
			$value = esc_html($value); // textarea_escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
			$value = esc_attr($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
	} else if ( 'db' == $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
		$value = apply_filters("pre_term_{$field}", $value, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1733
		$value = apply_filters("pre_{$taxonomy}_{$field}", $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
		// Back compat filters
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
		if ( 'slug' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
			$value = apply_filters('pre_category_nicename', $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
	} else if ( 'rss' == $context ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
		$value = apply_filters("term_{$field}_rss", $value, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
		$value = apply_filters("{$taxonomy}_{$field}_rss", $value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
		// Use display filters by default.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
		$value = apply_filters("term_{$field}", $value, $term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
		$value = apply_filters("{$taxonomy}_{$field}", $value, $term_id, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
	if ( 'attribute' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
		$value = esc_attr($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
	else if ( 'js' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
		$value = esc_js($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
	return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
 * Count how many terms are in Taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1757
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
 * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1761
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
 * @uses get_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
 * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
 * @param string $taxonomy Taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
 * @param array|string $args Overwrite defaults. See get_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
 * @return int|WP_Error How many terms are in $taxonomy. WP_Error if $taxonomy does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
function wp_count_terms( $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
	$defaults = array('hide_empty' => false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
	// backwards compatibility
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
	if ( isset($args['ignore_empty']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
		$args['hide_empty'] = $args['ignore_empty'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
		unset($args['ignore_empty']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
	$args['fields'] = 'count';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1782
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
	return get_terms($taxonomy, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
 * Will unlink the object from the taxonomy or taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
 * Will remove all relationships between the object and any terms in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
 * a particular taxonomy or taxonomies. Does not remove the term or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
 * taxonomy itself.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
 * @uses wp_remove_object_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
 * @param int $object_id The term Object Id that refers to the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
	if ( !is_array($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1805
		$taxonomies = array($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1806
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
	foreach ( (array) $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
		$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
		$term_ids = array_map( 'intval', $term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
		wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
 * Removes a term from the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
 * If the term is a parent of other terms, then the children will be updated to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
 * that term's parent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
 * The $args 'default' will only override the terms found, if there is only one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
 * term found. Any other and the found terms are used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
 * The $args 'force_default' will force the term supplied as default to be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
 * assigned even if the object was not going to be termless
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1827
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
 * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
 *	hooks, passing term ID, term taxonomy ID, and deleted term object. 'delete_term'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1832
 *	also gets taxonomy as the third parameter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1833
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1834
 * @param int $term Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1835
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1836
 * @param array|string $args Optional. Change 'default' term id and override found term ids.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1837
 * @return bool|WP_Error Returns false if not term; true if completes delete action.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
function wp_delete_term( $term, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
	$term = (int) $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
	if ( ! $ids = term_exists($term, $taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
	if ( is_wp_error( $ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
		return $ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1849
	$tt_id = $ids['term_taxonomy_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1851
	$defaults = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
	if ( 'category' == $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
		$defaults['default'] = get_option( 'default_category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1855
		if ( $defaults['default'] == $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1856
			return 0; // Don't delete the default category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
	extract($args, EXTR_SKIP);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
	if ( isset( $default ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
		$default = (int) $default;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
		if ( ! term_exists($default, $taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
			unset($default);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
	// Update children to point to new parent
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
	if ( is_taxonomy_hierarchical($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
		$term_obj = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
		if ( is_wp_error( $term_obj ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
			return $term_obj;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
		$parent = $term_obj->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
		$edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
		do_action( 'edit_term_taxonomies', $edit_tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
		$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1878
		do_action( 'edited_term_taxonomies', $edit_tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
	$objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
	foreach ( (array) $objects as $object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
		$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
		if ( 1 == count($terms) && isset($default) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1886
			$terms = array($default);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
			$terms = array_diff($terms, array($term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1889
			if (isset($default) && isset($force_default) && $force_default)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1890
				$terms = array_merge($terms, array($default));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1891
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1892
		$terms = array_map('intval', $terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
		wp_set_object_terms($object, $terms, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1894
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
	// Clean the relationship caches for all object types using this term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
	$tax_object = get_taxonomy( $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
	foreach ( $tax_object->object_type as $object_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
		clean_object_term_cache( $objects, $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
	// Get the object before deletion so we can pass to actions below
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
	$deleted_term = get_term( $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
	do_action( 'delete_term_taxonomy', $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
	$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
	do_action( 'deleted_term_taxonomy', $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
	// Delete the term if no taxonomies use it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
	if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
		$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
	clean_term_cache($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1914
	do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1915
	do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1916
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1917
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1918
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
 * Deletes one existing category.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1922
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
 * @uses wp_delete_term()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
 * @param int $cat_ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1927
 * @return mixed Returns true if completes delete action; false if term doesn't exist;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1928
 * 	Zero on attempted deletion of default Category; WP_Error object is also a possibility.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1929
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1930
function wp_delete_category( $cat_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1931
	return wp_delete_term( $cat_ID, 'category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1932
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1933
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1934
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1937
 * The following information has to do the $args parameter and for what can be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
 * contained in the string or array of that parameter, if it exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
 * The first argument is called, 'orderby' and has the default value of 'name'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
 * The other value that is supported is 'count'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
 * The second argument is called, 'order' and has the default value of 'ASC'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1944
 * The only other value that will be acceptable is 'DESC'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1946
 * The final argument supported is called, 'fields' and has the default value of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
 * 'all'. There are multiple other options that can be used instead. Supported
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
 * values are as follows: 'all', 'ids', 'names', and finally
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
 * 'all_with_object_id'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
 * The fields argument also decides what will be returned. If 'all' or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
 * 'all_with_object_id' is chosen or the default kept intact, then all matching
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1953
 * terms objects will be returned. If either 'ids' or 'names' is used, then an
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1954
 * array of all matching term ids or term names will be returned respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1955
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1956
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1957
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1958
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1959
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1960
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1961
 * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
 * @param string|array $taxonomies The taxonomies to retrieve terms from.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1963
 * @param array|string $args Change what is returned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1964
 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
	if ( empty( $object_ids ) || empty( $taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1972
	if ( !is_array($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
		$taxonomies = array($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
	foreach ( (array) $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
		if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
			return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
		$object_ids = array($object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
	$object_ids = array_map('intval', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
	$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
	$terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
	if ( count($taxonomies) > 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
		foreach ( $taxonomies as $index => $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
			$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
			if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
				unset($taxonomies[$index]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
				$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
		$t = get_taxonomy($taxonomies[0]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
		if ( isset($t->args) && is_array($t->args) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
			$args = array_merge($args, $t->args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
	extract($args, EXTR_SKIP);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
	if ( 'count' == $orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
		$orderby = 'tt.count';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
	else if ( 'name' == $orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
		$orderby = 't.name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
	else if ( 'slug' == $orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
		$orderby = 't.slug';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
	else if ( 'term_group' == $orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2011
		$orderby = 't.term_group';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
	else if ( 'term_order' == $orderby )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
		$orderby = 'tr.term_order';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
	else if ( 'none' == $orderby ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
		$orderby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
		$order = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
		$orderby = 't.term_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
	// tt_ids queries can only be none or tr.term_taxonomy_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
	if ( ('tt_ids' == $fields) && !empty($orderby) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
		$orderby = 'tr.term_taxonomy_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
	if ( !empty($orderby) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
		$orderby = "ORDER BY $orderby";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
	$order = strtoupper( $order );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
	if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2030
		$order = 'ASC';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2031
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2032
	$taxonomies = "'" . implode("', '", $taxonomies) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
	$object_ids = implode(', ', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
	$select_this = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2036
	if ( 'all' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
		$select_this = 't.*, tt.*';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
	else if ( 'ids' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
		$select_this = 't.term_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
	else if ( 'names' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2041
		$select_this = 't.name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2042
	else if ( 'slugs' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
		$select_this = 't.slug';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
	else if ( 'all_with_object_id' == $fields )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
		$select_this = 't.*, tt.*, tr.object_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
	$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";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
	if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
		$terms = array_merge($terms, $wpdb->get_results($query));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2051
		update_term_cache($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2052
	} else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
		$terms = array_merge($terms, $wpdb->get_col($query));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
	} else if ( 'tt_ids' == $fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
		$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");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
	if ( ! $terms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
		$terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
 * Add a new term to the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
 * A non-existent term is inserted in the following sequence:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
 * 1. The term is added to the term table, then related to the taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
 * 2. If everything is correct, several actions are fired.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
 * 3. The 'term_id_filter' is evaluated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
 * 4. The term cache is cleaned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
 * 5. Several more actions are fired.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
 * 6. An array is returned containing the term_id and term_taxonomy_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2075
 * If the 'slug' argument is not empty, then it is checked to see if the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
 * is invalid. If it is not a valid, existing term, it is added and the term_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
 * is given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2079
 * If the taxonomy is hierarchical, and the 'parent' argument is not empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
 * the term is inserted and the term_id will be given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
 * Error handling:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
 * If $taxonomy does not exist or $term is empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
 * a WP_Error object will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2086
 * If the term already exists on the same hierarchical level,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
 * or the term slug and name are not unique, a WP_Error object will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
 * @global wpdb $wpdb The WordPress database object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2091
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
 * @param string       $term     The term to add or update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
 * @param string       $taxonomy The taxonomy to which to add the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
 * @param array|string $args {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
 *     Arguments to change values of the inserted term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
 *     @type string 'alias_of'    Slug of the term to make this term an alias of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
 *                                Default empty string. Accepts a term slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
 *     @type string 'description' The term description.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
 *                                Default empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
 *     @type int    'parent'      The id of the parent term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
 *                                Default 0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2104
 *     @type string 'slug'        The term slug to use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2105
 *                                Default empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
 * }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2107
 * @return array|WP_Error An array containing the term_id and term_taxonomy_id, WP_Error otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2108
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
function wp_insert_term( $term, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
	$term = apply_filters( 'pre_insert_term', $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
		if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
			return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
	if ( is_int($term) && 0 == $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
		return new WP_Error('invalid_term_id', __('Invalid term ID'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
	if ( '' == trim($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
		return new WP_Error('empty_term_name', __('A name is required for this term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2127
	$args['name'] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
	$args['taxonomy'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
	$args = sanitize_term($args, $taxonomy, 'db');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
	extract($args, EXTR_SKIP);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
	// expected_slashed ($name)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
	$name = wp_unslash($name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
	$description = wp_unslash($description);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
	if ( empty($slug) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
		$slug = sanitize_title($name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2138
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2139
	$term_group = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
	if ( $alias_of ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
		$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
		if ( $alias->term_group ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
			// The alias we want is already in a group, so let's use that one.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
			$term_group = $alias->term_group;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
			// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
			do_action( 'edit_terms', $alias->term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
			$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
			do_action( 'edited_terms', $alias->term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
	if ( $term_id = term_exists($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
		$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
		// We've got an existing term in the same taxonomy, which matches the name of the new term:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
		if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
			// Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
			$siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
			if ( in_array($name, $siblings) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
				return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
				$slug = wp_unique_term_slug($slug, (object) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
				if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
					return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
				$term_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
		} elseif ( $existing_term['name'] != $name ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
			// We've got an existing term, with a different name, Create the new term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
			$slug = wp_unique_term_slug($slug, (object) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
			if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
				return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
			$term_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2174
		} elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2175
			// Same name, same slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2176
			return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
		// This term does not exist at all in the database, Create it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
		$slug = wp_unique_term_slug($slug, (object) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
		if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
			return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
		$term_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
	// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
	if ( empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
		$slug = sanitize_title($slug, $term_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
		do_action( 'edit_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2190
		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2191
		do_action( 'edited_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2192
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2193
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2194
	$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 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
	if ( !empty($tt_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2197
		return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
	$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
	$tt_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
	do_action("create_term", $term_id, $tt_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2203
	do_action("create_$taxonomy", $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2205
	$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
	clean_term_cache($term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
	do_action("created_term", $term_id, $tt_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
	do_action("created_$taxonomy", $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
 * Create Term and Taxonomy Relationships.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2218
 * Relates an object (post, link etc) to a term and taxonomy type. Creates the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2219
 * term and taxonomy relationship if it doesn't already exist. Creates a term if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2220
 * it doesn't exist (using the slug).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2221
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2222
 * A relationship means that the term is grouped in or belongs to the taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2223
 * A term has no meaning until it is given context by defining which taxonomy it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2224
 * exists under.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2225
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2226
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
 * @uses wp_remove_object_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
 * @param int $object_id The object to relate to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
 * @param array|int|string $terms The slug or id of the term, will replace all existing
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
 * related terms in this taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
 * @param array|string $taxonomy The context in which to relate the term to the object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2235
 * @param bool $append If false will delete difference of terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
 * @return array|WP_Error Affected Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2238
function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2244
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2246
	if ( !is_array($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2247
		$terms = array($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2248
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2249
	if ( ! $append )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2250
		$old_tt_ids =  wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2251
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2252
		$old_tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2254
	$tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2255
	$term_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2256
	$new_tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2258
	foreach ( (array) $terms as $term) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2259
		if ( !strlen(trim($term)) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
		if ( !$term_info = term_exists($term, $taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
			// Skip if a non-existent term ID is passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
			if ( is_int($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2265
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
			$term_info = wp_insert_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2267
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
		if ( is_wp_error($term_info) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2269
			return $term_info;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
		$term_ids[] = $term_info['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
		$tt_id = $term_info['term_taxonomy_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
		$tt_ids[] = $tt_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2274
		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 ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2276
		do_action( 'add_term_relationship', $object_id, $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2277
		$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2278
		do_action( 'added_term_relationship', $object_id, $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2279
		$new_tt_ids[] = $tt_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2281
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2282
	if ( $new_tt_ids )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
		wp_update_term_count( $new_tt_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
	if ( ! $append ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
		$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
		if ( $delete_tt_ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
			$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2290
			$delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2291
			$delete_term_ids = array_map( 'intval', $delete_term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2293
			$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2294
			if ( is_wp_error( $remove ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2295
				return $remove;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2296
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2297
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2298
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2299
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2300
	$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2301
	if ( ! $append && isset($t->sort) && $t->sort ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2302
		$values = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2303
		$term_order = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2304
		$final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2305
		foreach ( $tt_ids as $tt_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2306
			if ( in_array($tt_id, $final_tt_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2307
				$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2308
		if ( $values )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2309
			if ( false === $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)" ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2310
				return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database' ), $wpdb->last_error );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2311
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2313
	wp_cache_delete( $object_id, $taxonomy . '_relationships' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2314
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2315
	do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2316
	return $tt_ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2317
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2319
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2320
 * Add term(s) associated with a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2321
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2322
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2323
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2324
 * @since 3.6
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
 * @uses wp_set_object_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2326
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2327
 * @param int $object_id The ID of the object to which the terms will be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to add.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
 * @param array|string $taxonomy Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2330
 * @return array|WP_Error Affected Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2331
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2332
function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2333
	return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2336
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
 * Remove term(s) associated with a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2338
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2339
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2341
 * @since 3.6
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2342
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
 * @uses apply_filters() Calls 'delete_term_relationships' hook with object_id and tt_ids as parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
 * @uses apply_filters() Calls 'deleted_term_relationships' hook with object_id and tt_ids as parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2346
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2347
 * @param int $object_id The ID of the object from which the terms will be removed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2348
 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
 * @param array|string $taxonomy Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
 * @return bool|WP_Error True on success, false or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2356
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2357
	if ( ! taxonomy_exists( $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2358
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2359
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2360
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
	if ( ! is_array( $terms ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2362
		$terms = array( $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
	$tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
		if ( ! strlen( trim( $term ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2370
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2372
		if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
			// Skip if a non-existent term ID is passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
			if ( is_int( $term ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2375
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2376
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2377
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2379
		if ( is_wp_error( $term_info ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
			return $term_info;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
		$tt_ids[] = $term_info['term_taxonomy_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2384
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
	if ( $tt_ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
		$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
		do_action( 'delete_term_relationships', $object_id, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
		$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
		do_action( 'deleted_term_relationships', $object_id, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
		wp_update_term_count( $tt_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
		return (bool) $deleted;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2394
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
 * Will make slug unique, if it isn't already.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
 * The $slug has to be unique global to every taxonomy, meaning that one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
 * taxonomy term can't have a matching slug with another taxonomy term. Each
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
 * slug has to be globally unique for every taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
 * The way this works is that if the taxonomy that the term belongs to is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2407
 * hierarchical and has a parent, it will append that parent to the $slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2408
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2409
 * If that still doesn't return an unique slug, then it try to append a number
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2410
 * until it finds a number that is truly unique.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2411
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2412
 * The only purpose for $term is for appending a parent, if one exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2413
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2414
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2415
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2416
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2417
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2418
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2419
 * @param string $slug The string that will be tried for a unique slug
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2420
 * @param object $term The term object that the $slug will belong too
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2421
 * @return string Will return a true unique slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2422
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2423
function wp_unique_term_slug($slug, $term) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2424
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2425
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2426
	if ( ! term_exists( $slug ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2427
		return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2428
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2429
	// If the taxonomy supports hierarchy and the term has a parent, make the slug unique
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2430
	// by incorporating parent slugs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2431
	if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2432
		$the_parent = $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2433
		while ( ! empty($the_parent) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
			$parent_term = get_term($the_parent, $term->taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
			if ( is_wp_error($parent_term) || empty($parent_term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
			$slug .= '-' . $parent_term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2438
			if ( ! term_exists( $slug ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2439
				return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2441
			if ( empty($parent_term->parent) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2442
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2443
			$the_parent = $parent_term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2444
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2445
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2447
	// If we didn't get a unique slug, try appending a number to make it unique.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2448
	if ( ! empty( $term->term_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2449
		$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2450
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2451
		$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2452
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2453
	if ( $wpdb->get_var( $query ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2454
		$num = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2455
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2456
			$alt_slug = $slug . "-$num";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2457
			$num++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2458
			$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2459
		} while ( $slug_check );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2460
		$slug = $alt_slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2461
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2462
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2463
	return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2464
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2466
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2467
 * Update term based on arguments provided.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2468
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2469
 * The $args will indiscriminately override all values with the same field name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2470
 * Care must be taken to not override important information need to update or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2471
 * update will fail (or perhaps create a new term, neither would be acceptable).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2472
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2473
 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2474
 * defined in $args already.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2475
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2476
 * 'alias_of' will create a term group, if it doesn't already exist, and update
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2477
 * it for the $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2478
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2479
 * If the 'slug' argument in $args is missing, then the 'name' in $args will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2480
 * used. It should also be noted that if you set 'slug' and it isn't unique then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2481
 * a WP_Error will be passed back. If you don't pass any slug, then a unique one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2482
 * will be created for you.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2483
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2484
 * For what can be overrode in $args, check the term scheme can contain and stay
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2485
 * away from the term keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2486
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2487
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2488
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2489
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2490
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2491
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2492
 * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2493
 * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2494
 *	id and taxonomy id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2495
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2496
 * @param int $term_id The ID of the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2497
 * @param string $taxonomy The context in which to relate the term to the object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2498
 * @param array|string $args Overwrite term field values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2499
 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2500
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2501
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2502
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2504
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2505
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2507
	$term_id = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2508
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2509
	// First, get all of the original args
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2510
	$term = get_term ($term_id, $taxonomy, ARRAY_A);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2512
	if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2513
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2515
	// Escape data pulled from DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2516
	$term = wp_slash($term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2517
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2518
	// Merge old and new args with new args overwriting old ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2519
	$args = array_merge($term, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2520
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2521
	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2522
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2523
	$args = sanitize_term($args, $taxonomy, 'db');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2524
	extract($args, EXTR_SKIP);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2526
	// expected_slashed ($name)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2527
	$name = wp_unslash($name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2528
	$description = wp_unslash($description);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2530
	if ( '' == trim($name) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2531
		return new WP_Error('empty_term_name', __('A name is required for this term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2533
	$empty_slug = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2534
	if ( empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2535
		$empty_slug = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2536
		$slug = sanitize_title($name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2537
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2539
	if ( $alias_of ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2540
		$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2541
		if ( $alias->term_group ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2542
			// The alias we want is already in a group, so let's use that one.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2543
			$term_group = $alias->term_group;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2544
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2545
			// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2546
			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2547
			do_action( 'edit_terms', $alias->term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2548
			$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2549
			do_action( 'edited_terms', $alias->term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2550
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2551
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2552
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2553
	// Check $parent to see if it will cause a hierarchy loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2554
	$parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2556
	// Check for duplicate slug
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2557
	$id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2558
	if ( $id && ($id != $term_id) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2559
		// If an empty slug was passed or the parent changed, reset the slug to something unique.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2560
		// Otherwise, bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2561
		if ( $empty_slug || ( $parent != $term['parent']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2562
			$slug = wp_unique_term_slug($slug, (object) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2563
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2564
			return new WP_Error('duplicate_term_slug', sprintf(__('The slug &#8220;%s&#8221; is already in use by another term'), $slug));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2565
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2566
	do_action( 'edit_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2567
	$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2568
	if ( empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2569
		$slug = sanitize_title($name, $term_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2570
		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2571
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2572
	do_action( 'edited_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2574
	$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) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2575
	do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2576
	$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2577
	do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2578
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2579
	do_action("edit_term", $term_id, $tt_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2580
	do_action("edit_$taxonomy", $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2582
	$term_id = apply_filters('term_id_filter', $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2583
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2584
	clean_term_cache($term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2586
	do_action("edited_term", $term_id, $tt_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2587
	do_action("edited_$taxonomy", $term_id, $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2588
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2589
	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2590
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2591
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2592
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2593
 * Enable or disable term counting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2594
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2595
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2596
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2597
 * @param bool $defer Optional. Enable if true, disable if false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2598
 * @return bool Whether term counting is enabled or disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2599
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2600
function wp_defer_term_counting($defer=null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2601
	static $_defer = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2603
	if ( is_bool($defer) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2604
		$_defer = $defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2605
		// flush any deferred counts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2606
		if ( !$defer )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2607
			wp_update_term_count( null, null, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2608
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2610
	return $_defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2611
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2612
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2613
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2614
 * Updates the amount of terms in taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2615
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2616
 * If there is a taxonomy callback applied, then it will be called for updating
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2617
 * the count.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2618
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2619
 * The default action is to count what the amount of terms have the relationship
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2620
 * of term ID. Once that is done, then update the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2621
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2622
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2623
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2624
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2625
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2626
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2627
 * @param int|array $terms The term_taxonomy_id of the terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2628
 * @param string $taxonomy The context of the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2629
 * @return bool If no terms will return false, and if successful will return true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2630
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2631
function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2632
	static $_deferred = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2633
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2634
	if ( $do_deferred ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2635
		foreach ( (array) array_keys($_deferred) as $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2636
			wp_update_term_count_now( $_deferred[$tax], $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2637
			unset( $_deferred[$tax] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2638
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2639
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2640
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2641
	if ( empty($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2642
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2644
	if ( !is_array($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2645
		$terms = array($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2646
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2647
	if ( wp_defer_term_counting() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2648
		if ( !isset($_deferred[$taxonomy]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2649
			$_deferred[$taxonomy] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2650
		$_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2651
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2652
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2653
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2654
	return wp_update_term_count_now( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2655
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2657
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2658
 * Perform term count update immediately.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2659
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2660
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2661
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2662
 * @param array $terms The term_taxonomy_id of terms to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2663
 * @param string $taxonomy The context of the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2664
 * @return bool Always true when complete.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2665
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2666
function wp_update_term_count_now( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2667
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2669
	$terms = array_map('intval', $terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2670
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2671
	$taxonomy = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2672
	if ( !empty($taxonomy->update_count_callback) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2673
		call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2674
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2675
		$object_types = (array) $taxonomy->object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2676
		foreach ( $object_types as &$object_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2677
			if ( 0 === strpos( $object_type, 'attachment:' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2678
				list( $object_type ) = explode( ':', $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2679
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2681
		if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2682
			// Only post types are attached to this taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2683
			_update_post_term_count( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2684
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2685
			// Default count updater
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2686
			_update_generic_term_count( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2687
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2688
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2690
	clean_term_cache($terms, '', false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2692
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2693
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2694
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2695
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2696
// Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2697
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2698
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2699
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2700
 * Removes the taxonomy relationship to terms from the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2701
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2702
 * Will remove the entire taxonomy relationship containing term $object_id. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2703
 * term IDs have to exist within the taxonomy $object_type for the deletion to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2704
 * take place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2705
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2706
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2707
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2708
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2709
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2710
 * @see get_object_taxonomies() for more on $object_type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2711
 * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2712
 *	Passes, function params in same order.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2713
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2714
 * @param int|array $object_ids Single or list of term object ID(s)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2715
 * @param array|string $object_type The taxonomy object type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2716
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2717
function clean_object_term_cache($object_ids, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2718
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2719
		$object_ids = array($object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2721
	$taxonomies = get_object_taxonomies( $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2722
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2723
	foreach ( $object_ids as $id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2724
		foreach ( $taxonomies as $taxonomy )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2725
			wp_cache_delete($id, "{$taxonomy}_relationships");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2727
	do_action('clean_object_term_cache', $object_ids, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2728
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2729
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2730
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2731
 * Will remove all of the term ids from the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2732
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2733
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2734
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2735
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2736
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2737
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2738
 * @param int|array $ids Single or list of Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2739
 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2740
 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2741
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2742
function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2743
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2744
	static $cleaned = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2745
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2746
	if ( !is_array($ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2747
		$ids = array($ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2748
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2749
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2750
	// If no taxonomy, assume tt_ids.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2751
	if ( empty($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2752
		$tt_ids = array_map('intval', $ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2753
		$tt_ids = implode(', ', $tt_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2754
		$terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2755
		$ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2756
		foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2757
			$taxonomies[] = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2758
			$ids[] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2759
			wp_cache_delete($term->term_id, $term->taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2760
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2761
		$taxonomies = array_unique($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2762
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2763
		$taxonomies = array($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2764
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2765
			foreach ( $ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2766
				wp_cache_delete($id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2767
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2768
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2769
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2770
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2771
	foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2772
		if ( isset($cleaned[$taxonomy]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2773
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2774
		$cleaned[$taxonomy] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2775
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2776
		if ( $clean_taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2777
			wp_cache_delete('all_ids', $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2778
			wp_cache_delete('get', $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2779
			delete_option("{$taxonomy}_children");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2780
			// Regenerate {$taxonomy}_children
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2781
			_get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2782
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2783
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2784
		do_action('clean_term_cache', $ids, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2785
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2786
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2787
	wp_cache_set( 'last_changed', microtime(), 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2788
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2790
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2791
 * Retrieves the taxonomy relationship to the term object id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2792
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2793
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2794
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2795
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2796
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2797
 * @uses wp_cache_get() Retrieves taxonomy relationship from cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2798
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2799
 * @param int|array $id Term object ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2800
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2801
 * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2802
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2803
function get_object_term_cache($id, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2804
	$cache = wp_cache_get($id, "{$taxonomy}_relationships");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2805
	return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2806
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2807
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2808
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2809
 * Updates the cache for Term ID(s).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2810
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2811
 * Will only update the cache for terms not already cached.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2812
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2813
 * The $object_ids expects that the ids be separated by commas, if it is a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2814
 * string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2815
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2816
 * It should be noted that update_object_term_cache() is very time extensive. It
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2817
 * is advised that the function is not called very often or at least not for a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2818
 * lot of terms that exist in a lot of taxonomies. The amount of time increases
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2819
 * for each term and it also increases for each taxonomy the term belongs to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2820
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2821
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2822
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2823
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2824
 * @uses wp_get_object_terms() Used to get terms from the database to update
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2825
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2826
 * @param string|array $object_ids Single or list of term object ID(s)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2827
 * @param array|string $object_type The taxonomy object type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2828
 * @return null|bool Null value is given with empty $object_ids. False if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2829
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2830
function update_object_term_cache($object_ids, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2831
	if ( empty($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2832
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2833
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2834
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2835
		$object_ids = explode(',', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2837
	$object_ids = array_map('intval', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2839
	$taxonomies = get_object_taxonomies($object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2840
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2841
	$ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2842
	foreach ( (array) $object_ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2843
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2844
			if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2845
				$ids[] = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2846
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2847
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2848
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2849
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2851
	if ( empty( $ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2852
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2853
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2854
	$terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2855
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2856
	$object_terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2857
	foreach ( (array) $terms as $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2858
		$object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2859
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2860
	foreach ( $ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2861
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2862
			if ( ! isset($object_terms[$id][$taxonomy]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2863
				if ( !isset($object_terms[$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2864
					$object_terms[$id] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2865
				$object_terms[$id][$taxonomy] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2866
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2867
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2868
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2870
	foreach ( $object_terms as $id => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2871
		foreach ( $value as $taxonomy => $terms ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2872
			wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2873
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2874
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2875
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2876
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2877
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2878
 * Updates Terms to Taxonomy in cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2879
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2880
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2881
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2882
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2883
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2884
 * @param array $terms List of Term objects to change
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2885
 * @param string $taxonomy Optional. Update Term to this taxonomy in cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2886
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2887
function update_term_cache($terms, $taxonomy = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2888
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2889
		$term_taxonomy = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2890
		if ( empty($term_taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2891
			$term_taxonomy = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2892
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2893
		wp_cache_add($term->term_id, $term, $term_taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2894
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2895
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2896
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2897
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2898
// Private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2899
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2900
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2901
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2902
 * Retrieves children of taxonomy as Term IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2903
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2904
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2905
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2906
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2907
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2908
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2909
 * @uses update_option() Stores all of the children in "$taxonomy_children"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2910
 *	 option. That is the name of the taxonomy, immediately followed by '_children'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2911
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2912
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2913
 * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2914
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2915
function _get_term_hierarchy($taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2916
	if ( !is_taxonomy_hierarchical($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2917
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2918
	$children = get_option("{$taxonomy}_children");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2919
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2920
	if ( is_array($children) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2921
		return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2922
	$children = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2923
	$terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2924
	foreach ( $terms as $term_id => $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2925
		if ( $parent > 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2926
			$children[$parent][] = $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2927
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2928
	update_option("{$taxonomy}_children", $children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2930
	return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2931
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2932
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2933
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2934
 * Get the subset of $terms that are descendants of $term_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2935
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2936
 * If $terms is an array of objects, then _get_term_children returns an array of objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2937
 * If $terms is an array of IDs, then _get_term_children returns an array of IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2938
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2939
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2940
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2941
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2942
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2943
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2944
 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2945
 * @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.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2946
 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2947
 * @return array The subset of $terms that are descendants of $term_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2948
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2949
function _get_term_children($term_id, $terms, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2950
	$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2951
	if ( empty($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2952
		return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2953
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2954
	$term_list = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2955
	$has_children = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2956
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2957
	if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2958
		return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2959
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2960
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2961
		$use_id = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2962
		if ( !is_object($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2963
			$term = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2964
			if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2965
				return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2966
			$use_id = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2967
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2969
		if ( $term->term_id == $term_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2970
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2971
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2972
		if ( $term->parent == $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2973
			if ( $use_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2974
				$term_list[] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2975
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2976
				$term_list[] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2978
			if ( !isset($has_children[$term->term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2979
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2980
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2981
			if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2982
				$term_list = array_merge($term_list, $children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2983
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2984
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2985
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2986
	return $term_list;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2987
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2989
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2990
 * Add count of children to parent count.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2991
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2992
 * Recalculates term counts by including items from child terms. Assumes all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2993
 * relevant children are already in the $terms argument.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2994
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2995
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2996
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2997
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2998
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2999
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3000
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3001
 * @param array $terms List of Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3002
 * @param string $taxonomy Term Context
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3003
 * @return null Will break from function if conditions are not met.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3004
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3005
function _pad_term_counts(&$terms, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3006
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3008
	// This function only works for hierarchical taxonomies like post categories.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3009
	if ( !is_taxonomy_hierarchical( $taxonomy ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3010
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3011
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3012
	$term_hier = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3013
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3014
	if ( empty($term_hier) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3015
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3016
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3017
	$term_items = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3019
	foreach ( (array) $terms as $key => $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3020
		$terms_by_id[$term->term_id] = & $terms[$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3021
		$term_ids[$term->term_taxonomy_id] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3022
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3023
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3024
	// Get the object and term ids and stick them in a lookup table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3025
	$tax_obj = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3026
	$object_types = esc_sql($tax_obj->object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3027
	$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 (" . implode(',', array_keys($term_ids)) . ") AND post_type IN ('" . implode("', '", $object_types) . "') AND post_status = 'publish'");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3028
	foreach ( $results as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3029
		$id = $term_ids[$row->term_taxonomy_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3030
		$term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3031
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3032
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3033
	// Touch every ancestor's lookup row for each post in each term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3034
	foreach ( $term_ids as $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3035
		$child = $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3036
		while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3037
			if ( !empty( $term_items[$term_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3038
				foreach ( $term_items[$term_id] as $item_id => $touches ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3039
					$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3040
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3041
			$child = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3042
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3043
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3045
	// Transfer the touched cells
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3046
	foreach ( (array) $term_items as $id => $items )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3047
		if ( isset($terms_by_id[$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3048
			$terms_by_id[$id]->count = count($items);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3049
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3050
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3051
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3052
// Default callbacks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3053
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3054
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3055
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3056
 * Will update term count based on object types of the current taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3057
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3058
 * Private function for the default callback for post_tag and category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3059
 * taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3060
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3061
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3062
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3063
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3064
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3065
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3066
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3067
 * @param array $terms List of Term taxonomy IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3068
 * @param object $taxonomy Current taxonomy object of terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3069
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3070
function _update_post_term_count( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3071
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3072
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3073
	$object_types = (array) $taxonomy->object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3074
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3075
	foreach ( $object_types as &$object_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3076
		list( $object_type ) = explode( ':', $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3077
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3078
	$object_types = array_unique( $object_types );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3079
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3080
	if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3081
		unset( $object_types[ $check_attachments ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3082
		$check_attachments = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3083
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3084
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3085
	if ( $object_types )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3086
		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3087
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3088
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3089
		$count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3090
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3091
		// Attachments can be 'inherit' status, we need to base count off the parent's status if so
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3092
		if ( $check_attachments )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3093
			$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3094
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3095
		if ( $object_types )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3096
			$count += (int) $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 IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3097
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3098
		do_action( 'edit_term_taxonomy', $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3099
		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3100
		do_action( 'edited_term_taxonomy', $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3101
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3102
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3104
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3105
 * Will update term count based on number of objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3106
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3107
 * Default callback for the link_category taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3108
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3109
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3110
 * @subpackage Taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3111
 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3112
 * @uses $wpdb
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3113
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3114
 * @param array $terms List of Term taxonomy IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3115
 * @param object $taxonomy Current taxonomy object of terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3116
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3117
function _update_generic_term_count( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3118
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3120
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3121
		$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3123
		do_action( 'edit_term_taxonomy', $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3124
		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3125
		do_action( 'edited_term_taxonomy', $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3126
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3127
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3128
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3129
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3130
 * Generates a permalink for a taxonomy term archive.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3132
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3133
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3134
 * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3135
 * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3136
 * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3137
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3138
 * @param object|int|string $term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3139
 * @param string $taxonomy (optional if $term is object)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3140
 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3141
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3142
function get_term_link( $term, $taxonomy = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3143
	global $wp_rewrite;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3145
	if ( !is_object($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3146
		if ( is_int($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3147
			$term = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3148
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3149
			$term = get_term_by('slug', $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3150
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3151
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3153
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3154
		$term = new WP_Error('invalid_term', __('Empty Term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3156
	if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3157
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3158
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3159
	$taxonomy = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3161
	$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3163
	$slug = $term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3164
	$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3166
	if ( empty($termlink) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3167
		if ( 'category' == $taxonomy )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3168
			$termlink = '?cat=' . $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3169
		elseif ( $t->query_var )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3170
			$termlink = "?$t->query_var=$slug";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3171
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3172
			$termlink = "?taxonomy=$taxonomy&term=$slug";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3173
		$termlink = home_url($termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3174
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3175
		if ( $t->rewrite['hierarchical'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3176
			$hierarchical_slugs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3177
			$ancestors = get_ancestors($term->term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3178
			foreach ( (array)$ancestors as $ancestor ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3179
				$ancestor_term = get_term($ancestor, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3180
				$hierarchical_slugs[] = $ancestor_term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3181
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3182
			$hierarchical_slugs = array_reverse($hierarchical_slugs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3183
			$hierarchical_slugs[] = $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3184
			$termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3185
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3186
			$termlink = str_replace("%$taxonomy%", $slug, $termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3187
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3188
		$termlink = home_url( user_trailingslashit($termlink, 'category') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3189
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3190
	// Back Compat filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3191
	if ( 'post_tag' == $taxonomy )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3192
		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3193
	elseif ( 'category' == $taxonomy )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3194
		$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3196
	return apply_filters('term_link', $termlink, $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3197
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3199
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3200
 * Display the taxonomies of a post with available options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3201
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3202
 * This function can be used within the loop to display the taxonomies for a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3203
 * post without specifying the Post ID. You can also use it outside the Loop to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3204
 * display the taxonomies for a specific post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3205
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3206
 * The available defaults are:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3207
 * 'post' : default is 0. The post ID to get taxonomies of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3208
 * 'before' : default is empty string. Display before taxonomies list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3209
 * 'sep' : default is empty string. Separate every taxonomy with value in this.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3210
 * 'after' : default is empty string. Display this after the taxonomies list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3211
 * 'template' : The template to use for displaying the taxonomy terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3212
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3213
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3214
 * @uses get_the_taxonomies()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3215
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3216
 * @param array $args Override the defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3217
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3218
function the_taxonomies($args = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3219
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3220
		'post' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3221
		'before' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3222
		'sep' => ' ',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3223
		'after' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3224
		'template' => '%s: %l.'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3225
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3227
	$r = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3228
	extract( $r, EXTR_SKIP );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3230
	echo $before . join($sep, get_the_taxonomies($post, $r)) . $after;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3231
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3233
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3234
 * Retrieve all taxonomies associated with a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3235
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3236
 * This function can be used within the loop. It will also return an array of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3237
 * the taxonomies with links to the taxonomy and name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3238
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3239
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3240
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3241
 * @param int $post Optional. Post ID or will use Global Post ID (in loop).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3242
 * @param array $args Override the defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3243
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3244
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3245
function get_the_taxonomies($post = 0, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3246
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3248
	$args = wp_parse_args( $args, array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3249
		'template' => '%s: %l.',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3250
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3251
	extract( $args, EXTR_SKIP );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3252
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3253
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3254
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3255
	if ( !$post )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3256
		return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3258
	foreach ( get_object_taxonomies($post) as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3259
		$t = (array) get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3260
		if ( empty($t['label']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3261
			$t['label'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3262
		if ( empty($t['args']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3263
			$t['args'] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3264
		if ( empty($t['template']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3265
			$t['template'] = $template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3267
		$terms = get_object_term_cache($post->ID, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3268
		if ( false === $terms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3269
			$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3271
		$links = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3272
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3273
		foreach ( $terms as $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3274
			$links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3276
		if ( $links )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3277
			$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3278
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3279
	return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3280
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3281
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3282
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3283
 * Retrieve all taxonomies of a post with just the names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3284
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3285
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3286
 * @uses get_object_taxonomies()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3287
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3288
 * @param int $post Optional. Post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3289
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3290
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3291
function get_post_taxonomies($post = 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3292
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3294
	return get_object_taxonomies($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3295
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3296
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3297
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3298
 * Determine if the given object is associated with any of the given terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3299
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3300
 * The given terms are checked against the object's terms' term_ids, names and slugs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3301
 * Terms given as integers will only be checked against the object's terms' term_ids.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3302
 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3303
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3304
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3305
 * @uses get_object_term_cache()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3306
 * @uses wp_get_object_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3307
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3308
 * @param int $object_id ID of the object (post ID, link ID, ...)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3309
 * @param string $taxonomy Single taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3310
 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3311
 * @return bool|WP_Error. WP_Error on input error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3312
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3313
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3314
	if ( !$object_id = (int) $object_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3315
		return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3317
	$object_terms = get_object_term_cache( $object_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3318
	if ( false === $object_terms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3319
		 $object_terms = wp_get_object_terms( $object_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3321
	if ( is_wp_error( $object_terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3322
		return $object_terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3323
	if ( empty( $object_terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3324
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3325
	if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3326
		return ( !empty( $object_terms ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3328
	$terms = (array) $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3330
	if ( $ints = array_filter( $terms, 'is_int' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3331
		$strs = array_diff( $terms, $ints );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3332
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3333
		$strs =& $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3334
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3335
	foreach ( $object_terms as $object_term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3336
		if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3337
		if ( $strs ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3338
			if ( in_array( $object_term->term_id, $strs ) ) return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3339
			if ( in_array( $object_term->name, $strs ) )    return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3340
			if ( in_array( $object_term->slug, $strs ) )    return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3341
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3342
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3343
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3344
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3345
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3346
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3347
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3348
 * Determine if the given object type is associated with the given taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3349
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3350
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3351
 * @uses get_object_taxonomies()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3352
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3353
 * @param string $object_type Object type string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3354
 * @param string $taxonomy Single taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3355
 * @return bool True if object is associated with the taxonomy, otherwise false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3356
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3357
function is_object_in_taxonomy($object_type, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3358
	$taxonomies = get_object_taxonomies($object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3359
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3360
	if ( empty($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3361
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3362
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3363
	if ( in_array($taxonomy, $taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3364
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3365
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3366
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3367
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3369
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3370
 * Get an array of ancestor IDs for a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3371
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3372
 * @param int $object_id The ID of the object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3373
 * @param string $object_type The type of object for which we'll be retrieving ancestors.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3374
 * @return array of ancestors from lowest to highest in the hierarchy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3375
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3376
function get_ancestors($object_id = 0, $object_type = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3377
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3379
	$ancestors = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3381
	if ( empty( $object_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3382
		return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3383
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3385
	if ( is_taxonomy_hierarchical( $object_type ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3386
		$term = get_term($object_id, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3387
		while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3388
			$ancestors[] = (int) $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3389
			$term = get_term($term->parent, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3390
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3391
	} elseif ( post_type_exists( $object_type ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3392
		$ancestors = get_post_ancestors($object_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3393
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3394
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3395
	return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3396
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3398
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3399
 * Returns the term's parent's term_ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3400
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3401
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3402
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3403
 * @param int $term_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3404
 * @param string $taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3405
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3406
 * @return int|bool false on error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3407
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3408
function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3409
	$term = get_term( $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3410
	if ( !$term || is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3411
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3412
	return (int) $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3413
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3415
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3416
 * Checks the given subset of the term hierarchy for hierarchy loops.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3417
 * Prevents loops from forming and breaks those that it finds.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3418
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3419
 * Attached to the wp_update_term_parent filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3420
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3421
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3422
 * @uses wp_find_hierarchy_loop()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3423
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3424
 * @param int $parent term_id of the parent for the term we're checking.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3425
 * @param int $term_id The term we're checking.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3426
 * @param string $taxonomy The taxonomy of the term we're checking.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3427
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3428
 * @return int The new parent for the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3429
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3430
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3431
	// Nothing fancy here - bail
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3432
	if ( !$parent )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3433
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3434
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3435
	// Can't be its own parent
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3436
	if ( $parent == $term_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3437
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3438
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3439
	// Now look for larger loops
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3441
	if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3442
		return $parent; // No loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3444
	// Setting $parent to the given value causes a loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3445
	if ( isset( $loop[$term_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3446
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3448
	// There's a loop, but it doesn't contain $term_id. Break the loop.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3449
	foreach ( array_keys( $loop ) as $loop_member )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3450
		wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3451
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3452
	return $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3453
}