wp/wp-includes/taxonomy.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
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 {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
		 * Filter the post formats rewrite base.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
		 * @param string $context Context of the rewrite base. Default 'type'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		$post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		$rewrite = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			'category' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
				'hierarchical' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
				'slug' => get_option('category_base') ? get_option('category_base') : 'category',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
				'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
				'ep_mask' => EP_CATEGORIES,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			'post_tag' => array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
				'hierarchical' => false,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
				'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
				'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
				'ep_mask' => EP_TAGS,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
			),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
			'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	register_taxonomy( 'category', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		'hierarchical' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		'query_var' => 'category_name',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		'rewrite' => $rewrite['category'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		'show_ui' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		'show_admin_column' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	register_taxonomy( 'post_tag', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	 	'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		'query_var' => 'tag',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		'rewrite' => $rewrite['post_tag'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		'show_ui' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		'show_admin_column' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	register_taxonomy( 'nav_menu', 'nav_menu_item', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
			'name' => __( 'Navigation Menus' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			'singular_name' => __( 'Navigation Menu' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		'show_in_nav_menus' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	register_taxonomy( 'link_category', 'link', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			'name' => __( 'Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			'singular_name' => __( 'Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			'search_items' => __( 'Search Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
			'popular_items' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			'all_items' => __( 'All Link Categories' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			'edit_item' => __( 'Edit Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			'update_item' => __( 'Update Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			'add_new_item' => __( 'Add New Link Category' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			'new_item_name' => __( 'New Link Category Name' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
			'separate_items_with_commas' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			'add_or_remove_items' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			'choose_from_most_used' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		'capabilities' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			'manage_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
			'edit_terms'   => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
			'delete_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
			'assign_terms' => 'manage_links',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		'query_var' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		'rewrite' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		'public' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	register_taxonomy( 'post_format', 'post', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		'public' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		'labels' => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
			'name' => _x( 'Format', 'post format' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
			'singular_name' => _x( 'Format', 'post format' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		'query_var' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		'rewrite' => $rewrite['post_format'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		'show_ui' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		'_builtin' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 * Get a list of registered taxonomy objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
 * @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
   138
 * @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
   139
 * @param string $operator The logical operation to perform. 'or' means only one element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
 *  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
   141
 * @return array A list of taxonomy names or objects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	$field = ('names' == $output) ? 'name' : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
 * Return all of the taxonomy names that are of $object_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
 * 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
   155
 * $wp_taxonomies global variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
 * `<?php $taxonomies = get_object_taxonomies('post'); ?>` Should
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
 * result in `Array( 'category', 'post_tag' )`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
 * @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
   165
 * @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
   166
 * @return array The names of all taxonomy of $object_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
function get_object_taxonomies($object, $output = 'names') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	if ( is_object($object) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
		if ( $object->post_type == 'attachment' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
			return get_attachment_taxonomies($object);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		$object = $object->post_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	$object = (array) $object;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
		if ( array_intersect($object, (array) $tax_obj->object_type) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
			if ( 'names' == $output )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
				$taxonomies[] = $tax_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
				$taxonomies[ $tax_name ] = $tax_obj;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		}
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
	return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 * Retrieves the taxonomy object of $taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 * The get_taxonomy function will first check that the parameter string given
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 * is a taxonomy object and if it is, it will return it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
 * @uses $wp_taxonomies
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
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 * @uses $wp_taxonomies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
 * @return bool Whether the taxonomy exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
function taxonomy_exists( $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
	return isset( $wp_taxonomies[$taxonomy] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
 * Whether the taxonomy object is hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
 * 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
   236
 * object, and finally returns the hierarchical value in the object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
 * A false return value might also mean that the taxonomy does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
 * @return bool Whether the taxonomy is hierarchical
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
function is_taxonomy_hierarchical($taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
	$taxonomy = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
	return $taxonomy->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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
 * Create or modify a taxonomy object. Do not use before init.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
 * A simple function for creating or modifying a taxonomy object based on the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
 * parameters given. The function will accept an array (third optional
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
 * parameter), along with strings for the taxonomy name and another string for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
 * the object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
 * Nothing is returned, so expect error maybe or use taxonomy_exists() to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
 * whether taxonomy exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
 * Optional $args contents:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
 * - 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
   267
 * - labels - An array of labels for this taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 *     * 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
   269
 *     * You can see accepted values in {@link get_taxonomy_labels()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 * - 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
   271
 * - public - If the taxonomy should be publicly queryable; //@TODO not implemented.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 *     * Defaults to true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
 * - hierarchical - Whether the taxonomy is hierarchical (e.g. category). Defaults to false.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
 * - show_ui - Whether to generate a default UI for managing this taxonomy in the admin.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
 *     * If not set, the default is inherited from public.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
 * - show_in_menu - Whether to show the taxonomy in the admin menu.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
 *     * 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
   278
 *     * If false, no menu is shown.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
 *     * show_ui must be true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
 *     * If not set, the default is inherited from show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
 * - show_in_nav_menus - Makes this taxonomy available for selection in navigation menus.
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_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 *     * If not set, the default is inherited from show_ui.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
 * - show_in_quick_edit - Whether to show the taxonomy in the quick/bulk edit panel.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
 *     * It not set, the default is inherited from show_ui.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
 * - show_admin_column - Whether to display a column for the taxonomy on its post type listing screens.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
 *     * Defaults to false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
 * - meta_box_cb - Provide a callback function for the meta box display.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
 *     * If not set, defaults to post_categories_meta_box for hierarchical taxonomies
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
 *     and post_tags_meta_box for non-hierarchical.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
 *     * If false, no meta box is shown.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
 * - capabilities - Array of capabilities for this taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
 *     * You can see accepted values in this function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
 * - 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
   296
 *     * To prevent rewrite, set to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 *     * 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
   298
 *         * 'slug' => string Customize the permastruct slug. Defaults to $taxonomy key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 *         * '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
   300
 *         * 'hierarchical' => bool Either hierarchical rewrite tag or not. Defaults to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 *         * 'ep_mask' => const Assign an endpoint mask.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 *             * If not specified, defaults to EP_NONE.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * - 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
   304
 *     * If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
 *     * 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
   306
 * - 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
   307
 *     * 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
   308
 *       that the objects are published before counting them.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
 *     * 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
   310
 * - _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
   311
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
 * @since 4.2.0 Introduced `show_in_quick_edit` argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
 * @global array $wp_taxonomies Registered taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
 * @global WP    $wp            WP instance.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
 * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
 * @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
   320
 * @param array|string $args See optional args description above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
 * @return null|WP_Error WP_Error if errors, otherwise null.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	global $wp_taxonomies, $wp;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	if ( ! is_array( $wp_taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
		$wp_taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		'labels'                => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
		'description'           => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		'public'                => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		'hierarchical'          => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		'show_ui'               => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		'show_in_menu'          => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
		'show_in_nav_menus'     => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		'show_tagcloud'         => null,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
		'show_in_quick_edit'	=> null,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
		'show_admin_column'     => false,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		'meta_box_cb'           => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		'capabilities'          => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		'rewrite'               => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
		'query_var'             => $taxonomy,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
		'update_count_callback' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
		'_builtin'              => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
		_doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
		return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
	if ( false !== $args['query_var'] && ! empty( $wp ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		if ( true === $args['query_var'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			$args['query_var'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
			$args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		$wp->add_query_var( $args['query_var'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
		$args['rewrite'] = wp_parse_args( $args['rewrite'], array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
			'with_front' => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
			'hierarchical' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
			'ep_mask' => EP_NONE,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
		if ( empty( $args['rewrite']['slug'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
			$args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
			$tag = '(.+?)';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
			$tag = '([^/]+)';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
		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
   378
		add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	if ( null === $args['show_ui'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		$args['show_ui'] = $args['public'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	// If not set, default to the setting for show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	if ( null === $args['show_in_menu' ] || ! $args['show_ui'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
		$args['show_in_menu' ] = $args['show_ui'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	// If not set, default to the setting for public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	if ( null === $args['show_in_nav_menus'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		$args['show_in_nav_menus'] = $args['public'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	// If not set, default to the setting for show_ui.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	if ( null === $args['show_tagcloud'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		$args['show_tagcloud'] = $args['show_ui'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
	// If not set, default to the setting for show_ui.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
	if ( null === $args['show_in_quick_edit'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
		$args['show_in_quick_edit'] = $args['show_ui'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	$default_caps = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
		'manage_terms' => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
		'edit_terms'   => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
		'delete_terms' => 'manage_categories',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
		'assign_terms' => 'edit_posts',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	$args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	unset( $args['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	$args['name'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	$args['object_type'] = array_unique( (array) $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	$args['labels'] = get_taxonomy_labels( (object) $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
	$args['label'] = $args['labels']->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
	// If not set, use the default meta box
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	if ( null === $args['meta_box_cb'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
		if ( $args['hierarchical'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			$args['meta_box_cb'] = 'post_categories_meta_box';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
			$args['meta_box_cb'] = 'post_tags_meta_box';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
	$wp_taxonomies[ $taxonomy ] = (object) $args;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
	// register callback handling for metabox
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 	add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
	 * Fires after a taxonomy is registered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
	 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
	 * @param string       $taxonomy    Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
	 * @param array|string $object_type Object type or array of object types.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
	 * @param array        $args        Array of taxonomy registration arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   438
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	do_action( 'registered_taxonomy', $taxonomy, $object_type, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 * Builds an object with all taxonomy labels out of a taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
 * Accepted keys of the label array in the taxonomy object:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
 * - 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
   448
 * - singular_name - name for one object of this taxonomy. Default is Tag/Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 * - search_items - Default is Search Tags/Search Categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
 * - 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
   451
 * - all_items - Default is All Tags/All Categories
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
 * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   453
 * - parent_item_colon - The same as `parent_item`, but with colon `:` in the end
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
 * - edit_item - Default is Edit Tag/Edit Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
 * - view_item - Default is View Tag/View Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
 * - update_item - Default is Update Tag/Update Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
 * - add_new_item - Default is Add New Tag/Add New Category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
 * - new_item_name - Default is New Tag Name/New Category Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
 * - 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
   460
 * - 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
   461
 * - 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.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
 * - not_found - Default is "No tags found"/"No categories found", used in the meta box and taxonomy list table.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
 * 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
   465
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
 * @param object $tax Taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
 * @return object object with all the labels as member variables
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
function get_taxonomy_labels( $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	$tax->labels = (array) $tax->labels;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		$tax->labels['separate_items_with_commas'] = $tax->helps;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		$tax->labels['not_found'] = $tax->no_tagcloud;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	$nohier_vs_hier_defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
		'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
		'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
		'popular_items' => array( __( 'Popular Tags' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
		'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
		'parent_item' => array( null, __( 'Parent Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
		'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
		'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
		'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
		'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
		'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   496
		'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
 * Add an already registered taxonomy to an object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
 * @uses $wp_taxonomies Modifies taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
 * @param string $taxonomy Name of taxonomy object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
 * @param string $object_type Name of the object type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
 * @return bool True if successful, false if not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
function register_taxonomy_for_object_type( $taxonomy, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
	if ( !isset($wp_taxonomies[$taxonomy]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
	if ( ! get_post_type_object($object_type) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
		return false;
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 ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		$wp_taxonomies[$taxonomy]->object_type[] = $object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
 * Remove an already registered taxonomy from an object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
 * @param string $taxonomy    Name of taxonomy object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
 * @param string $object_type Name of the object type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
 * @return bool True if successful, false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
	global $wp_taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
	if ( ! get_post_type_object( $object_type ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
	$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
	if ( false === $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
	unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
// Term API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
 * Retrieve object_ids of valid taxonomy and term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
 * The strings of $taxonomies must exist before this function will continue. On
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
 * 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
   563
 * 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
   564
 * still test for the WP_Error class and get the error message.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
 * 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
   567
 * for $object_ids to be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
 * 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
   570
 * using PHP sort family functions or using the database by using $args with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
 * 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
   572
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
 * @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
   578
 * @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
   579
 * @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
   580
 * @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
   581
 *	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
   582
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
	if ( ! is_array( $term_ids ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
		$term_ids = array( $term_ids );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
	if ( ! is_array( $taxonomies ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
		$taxonomies = array( $taxonomies );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
	foreach ( (array) $taxonomies as $taxonomy ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
		if ( ! taxonomy_exists( $taxonomy ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   595
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	$defaults = array( 'order' => 'ASC' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	$args = wp_parse_args( $args, $defaults );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   600
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   601
	$order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	$term_ids = array_map('intval', $term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	$taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
	$term_ids = "'" . implode( "', '", $term_ids ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
	$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
   609
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   610
	if ( ! $object_ids ){
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
		return array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   612
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	return $object_ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
 * Given a taxonomy query, generates SQL to be appended to a main query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
 * @see WP_Tax_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
 * @param array $tax_query A compact tax query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
 * @param string $primary_table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
 * @param string $primary_id_column
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	$tax_query_obj = new WP_Tax_Query( $tax_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
	return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   634
 * Class for generating SQL clauses that filter a primary query according to object taxonomy terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
 * `WP_Tax_Query` is a helper that allows primary query classes, such as {@see WP_Query}, to filter
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   637
 * their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
 * to the primary SQL query string.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
class WP_Tax_Query {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
	 * Array of taxonomy queries.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
	 * See {@see WP_Tax_Query::__construct()} for information on tax query arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	public $queries = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	 * The relation between the queries. Can be one of 'AND' or 'OR'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
	public $relation;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
	 * Standard response when the query should not return any rows.
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.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   671
	private static $no_results = array( 'join' => array( '' ), 'where' => array( '0 = 1' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   672
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   673
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   674
	 * A flat list of table aliases used in the JOIN clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   675
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   676
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   678
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
	protected $table_aliases = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   681
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   683
	 * Terms and taxonomies fetched by this query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
	 * We store this data in a flat array because they are referenced in a
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
	 * number of places by {@see WP_Query}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   689
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   690
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
	public $queried_terms = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
	 * Database table that where the metadata's objects are stored (eg $wpdb->users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   696
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   699
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
	public $primary_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
	 * Column in 'primary_table' that represents the ID of the object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
	public $primary_id_column;
0
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
	 * Constructor.
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
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
	 * @since 4.1.0 Added support for `$operator` 'NOT EXISTS' and 'EXISTS' values.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
	 * @param array $tax_query {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
	 *     Array of taxonomy query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
	 *     @type string $relation Optional. The MySQL keyword used to join
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   723
	 *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
	 *     @type array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
	 *         Optional. An array of first-order clause parameters, or another fully-formed tax query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   727
	 *         @type string           $taxonomy         Taxonomy being queried. Optional when field=term_taxonomy_id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   728
	 *         @type string|int|array $terms            Term or terms to filter by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   729
	 *         @type string           $field            Field to match $terms against. Accepts 'term_id', 'slug',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   730
	 *                                                 'name', or 'term_taxonomy_id'. Default: 'term_id'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   731
	 *         @type string           $operator         MySQL operator to be used with $terms in the WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   732
	 *                                                  Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   733
	 *                                                  Default: 'IN'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   734
	 *         @type bool             $include_children Optional. Whether to include child terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   735
	 *                                                  Requires a $taxonomy. Default: true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   736
	 *     }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   737
	 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
	public function __construct( $tax_query ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   740
		if ( isset( $tax_query['relation'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   741
			$this->relation = $this->sanitize_relation( $tax_query['relation'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
			$this->relation = 'AND';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
		$this->queries = $this->sanitize_query( $tax_query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   747
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   749
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
	 * Ensure the 'tax_query' argument passed to the class constructor is well-formed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   751
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   752
	 * Ensures that each query-level clause has a 'relation' key, and that
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   753
	 * each first-order clause contains all the necessary keys from `$defaults`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   754
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   755
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   756
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   757
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   758
	 * @param array $queries Array of queries clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   759
	 * @return array Sanitized array of query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   760
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   761
	public function sanitize_query( $queries ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   762
		$cleaned_query = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   763
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
		$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
			'taxonomy' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			'terms' => array(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
			'field' => 'term_id',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
			'operator' => 'IN',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
			'include_children' => true,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   772
		foreach ( $queries as $key => $query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   773
			if ( 'relation' === $key ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   774
				$cleaned_query['relation'] = $this->sanitize_relation( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   775
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   776
			// First-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   777
			} elseif ( self::is_first_order_clause( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   778
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   779
				$cleaned_clause = array_merge( $defaults, $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   780
				$cleaned_clause['terms'] = (array) $cleaned_clause['terms'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   781
				$cleaned_query[] = $cleaned_clause;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   782
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   783
				/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   784
				 * Keep a copy of the clause in the flate
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   785
				 * $queried_terms array, for use in WP_Query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   786
				 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   787
				if ( ! empty( $cleaned_clause['taxonomy'] ) && 'NOT IN' !== $cleaned_clause['operator'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   788
					$taxonomy = $cleaned_clause['taxonomy'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   789
					if ( ! isset( $this->queried_terms[ $taxonomy ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   790
						$this->queried_terms[ $taxonomy ] = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   791
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   792
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   793
					/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   794
					 * Backward compatibility: Only store the first
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   795
					 * 'terms' and 'field' found for a given taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   796
					 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   797
					if ( ! empty( $cleaned_clause['terms'] ) && ! isset( $this->queried_terms[ $taxonomy ]['terms'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   798
						$this->queried_terms[ $taxonomy ]['terms'] = $cleaned_clause['terms'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   799
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   800
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   801
					if ( ! empty( $cleaned_clause['field'] ) && ! isset( $this->queried_terms[ $taxonomy ]['field'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   802
						$this->queried_terms[ $taxonomy ]['field'] = $cleaned_clause['field'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   803
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   804
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   805
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   806
			// Otherwise, it's a nested query, so we recurse.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   807
			} elseif ( is_array( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   808
				$cleaned_subquery = $this->sanitize_query( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   809
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   810
				if ( ! empty( $cleaned_subquery ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   811
					// All queries with children must have a relation.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   812
					if ( ! isset( $cleaned_subquery['relation'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   813
						$cleaned_subquery['relation'] = 'AND';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   814
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   815
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   816
					$cleaned_query[] = $cleaned_subquery;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   817
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   818
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   820
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   821
		return $cleaned_query;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   823
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
	 * Sanitize a 'relation' operator.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   830
	 * @param string $relation Raw relation key from the query argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   831
	 * @return string Sanitized relation ('AND' or 'OR').
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   832
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   833
	public function sanitize_relation( $relation ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   834
		if ( 'OR' === strtoupper( $relation ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   835
			return 'OR';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   836
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   837
			return 'AND';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   838
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   839
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   840
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   841
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   842
	 * Determine whether a clause is first-order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   843
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   844
	 * A "first-order" clause is one that contains any of the first-order
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   845
	 * clause keys ('terms', 'taxonomy', 'include_children', 'field',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   846
	 * 'operator'). An empty clause also counts as a first-order clause,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   847
	 * for backward compatibility. Any clause that doesn't meet this is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   848
	 * determined, by process of elimination, to be a higher-order query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   849
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   850
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   851
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   852
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   853
	 * @param array $query Tax query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   854
	 * @return bool Whether the query clause is a first-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   855
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   856
	protected static function is_first_order_clause( $query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   857
		return is_array( $query ) && ( empty( $query ) || array_key_exists( 'terms', $query ) || array_key_exists( 'taxonomy', $query ) || array_key_exists( 'include_children', $query ) || array_key_exists( 'field', $query ) || array_key_exists( 'operator', $query ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
	 * Generates SQL clauses to be appended to a main query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   866
	 * @param string $primary_table     Database table where the object being filtered is stored (eg wp_users).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   867
	 * @param string $primary_id_column ID column for the filtered object in $primary_table.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   868
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   869
	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   870
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   871
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   872
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   873
	 * }
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
	public function get_sql( $primary_table, $primary_id_column ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
		$this->primary_table = $primary_table;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   877
		$this->primary_id_column = $primary_id_column;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   878
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   879
		return $this->get_sql_clauses();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   880
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   881
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   882
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   883
	 * Generate SQL clauses to be appended to a main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   884
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   885
	 * Called by the public {@see WP_Tax_Query::get_sql()}, this method
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   886
	 * is abstracted out to maintain parity with the other Query classes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   887
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   888
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   889
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   890
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   891
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   892
	 *     Array containing JOIN and WHERE SQL clauses to append to the main query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   893
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   894
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   895
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   896
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   897
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
	protected function get_sql_clauses() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   899
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   900
		 * $queries are passed by reference to get_sql_for_query() for recursion.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   901
		 * To keep $this->queries unaltered, pass a copy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   902
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   903
		$queries = $this->queries;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   904
		$sql = $this->get_sql_for_query( $queries );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   905
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
		if ( ! empty( $sql['where'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   907
			$sql['where'] = ' AND ' . $sql['where'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   908
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   909
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   910
		return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   911
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   912
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   914
	 * Generate SQL clauses for a single query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
	 * If nested subqueries are found, this method recurses the tree to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
	 * produce the properly nested SQL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   920
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   921
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   922
	 * @param array $query Query to parse, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   923
	 * @param int   $depth Optional. Number of tree levels deep we currently are.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
	 *                     Used to calculate indentation. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   925
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
	 *     Array containing JOIN and WHERE SQL clauses to append to a single query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   928
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   929
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   930
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   932
	protected function get_sql_for_query( &$query, $depth = 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
		$sql_chunks = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   934
			'join'  => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
			'where' => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
		$sql = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
			'join'  => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   940
			'where' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   941
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   942
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
		$indent = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   944
		for ( $i = 0; $i < $depth; $i++ ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   945
			$indent .= "  ";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   947
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
		foreach ( $query as $key => &$clause ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
			if ( 'relation' === $key ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
				$relation = $query['relation'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   951
			} elseif ( is_array( $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
				// This is a first-order clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
				if ( $this->is_first_order_clause( $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
					$clause_sql = $this->get_sql_for_clause( $clause, $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
					$where_count = count( $clause_sql['where'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
					if ( ! $where_count ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
						$sql_chunks['where'][] = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
					} elseif ( 1 === $where_count ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
						$sql_chunks['where'][] = $clause_sql['where'][0];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
					} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
						$sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
					$sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
				// This is a subquery, so we recurse.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
					$clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
					$sql_chunks['where'][] = $clause_sql['where'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
					$sql_chunks['join'][]  = $clause_sql['join'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
		// Filter to remove empties.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
		$sql_chunks['join']  = array_filter( $sql_chunks['join'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
		$sql_chunks['where'] = array_filter( $sql_chunks['where'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
		if ( empty( $relation ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
			$relation = 'AND';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   984
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
		// Filter duplicate JOIN clauses and combine into a single string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
		if ( ! empty( $sql_chunks['join'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
			$sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   990
		// Generate a single WHERE clause with proper brackets and indentation.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
		if ( ! empty( $sql_chunks['where'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
			$sql['where'] = '( ' . "\n  " . $indent . implode( ' ' . "\n  " . $indent . $relation . ' ' . "\n  " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
		return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   999
	 * Generate SQL JOIN and WHERE clauses for a "first-order" query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1000
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1001
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1002
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1003
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1004
	 * @param array $clause       Query clause, passed by reference
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1005
	 * @param array $parent_query Parent query array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1006
	 * @return array {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1007
	 *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1008
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
	 *     @type string $join  SQL fragment to append to the main JOIN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1010
	 *     @type string $where SQL fragment to append to the main WHERE clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1012
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
	public function get_sql_for_clause( &$clause, $parent_query ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
		$sql = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
			'where' => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1018
			'join'  => array(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1019
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1020
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		$join = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
		$this->clean_query( $clause );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1024
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
		if ( is_wp_error( $clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1026
			return self::$no_results;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1027
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1028
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1029
		$terms = $clause['terms'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1030
		$operator = strtoupper( $clause['operator'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1031
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1032
		if ( 'IN' == $operator ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1033
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
			if ( empty( $terms ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
				return self::$no_results;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1036
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1037
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1038
			$terms = implode( ',', $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1039
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1040
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1041
			 * Before creating another table join, see if this clause has a
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
			 * sibling with an existing join that can be shared.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1043
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1044
			$alias = $this->find_compatible_table_alias( $clause, $parent_query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1045
			if ( false === $alias ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1046
				$i = count( $this->table_aliases );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
				$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1049
				// Store the alias as part of a flat array to build future iterators.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1050
				$this->table_aliases[] = $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1051
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
				// Store the alias with this clause, so later siblings can use it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
				$clause['alias'] = $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
				$join .= " INNER JOIN $wpdb->term_relationships";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
				$join .= $i ? " AS $alias" : '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
				$join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1058
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1061
			$where = "$alias.term_taxonomy_id $operator ($terms)";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1062
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1063
		} elseif ( 'NOT IN' == $operator ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1064
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1065
			if ( empty( $terms ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1066
				return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1067
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1068
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1069
			$terms = implode( ',', $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1070
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1071
			$where = "$this->primary_table.$this->primary_id_column NOT IN (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1072
				SELECT object_id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1073
				FROM $wpdb->term_relationships
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1074
				WHERE term_taxonomy_id IN ($terms)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1075
			)";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1076
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1077
		} elseif ( 'AND' == $operator ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1078
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
			if ( empty( $terms ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1080
				return $sql;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1083
			$num_terms = count( $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1084
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1085
			$terms = implode( ',', $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1086
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1087
			$where = "(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1088
				SELECT COUNT(1)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1089
				FROM $wpdb->term_relationships
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1090
				WHERE term_taxonomy_id IN ($terms)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1091
				AND object_id = $this->primary_table.$this->primary_id_column
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1092
			) = $num_terms";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1093
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1094
		} elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1095
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1096
			$where = $wpdb->prepare( "$operator (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1097
				SELECT 1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1098
				FROM $wpdb->term_relationships
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1099
				INNER JOIN $wpdb->term_taxonomy
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1100
				ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1101
				WHERE $wpdb->term_taxonomy.taxonomy = %s
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
				AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
			)", $clause['taxonomy'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
		$sql['join'][]  = $join;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
		$sql['where'][] = $where;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
		return $sql;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1112
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1113
	 * Identify an existing table alias that is compatible with the current query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1114
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1115
	 * We avoid unnecessary table joins by allowing each clause to look for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1116
	 * an existing table alias that is compatible with the query that it
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1117
	 * needs to perform.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1118
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1119
	 * An existing alias is compatible if (a) it is a sibling of `$clause`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1120
	 * (ie, it's under the scope of the same relation), and (b) the combination
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1121
	 * of operator and relation between the clauses allows for a shared table
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1122
	 * join. In the case of {@see WP_Tax_Query}, this only applies to 'IN'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1123
	 * clauses that are connected by the relation 'OR'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1124
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1125
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1126
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1127
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1128
	 * @param array       $clause       Query clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1129
	 * @param array       $parent_query Parent query of $clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1130
	 * @return string|bool Table alias if found, otherwise false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1131
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1132
	protected function find_compatible_table_alias( $clause, $parent_query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1133
		$alias = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1134
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1135
		// Sanity check. Only IN queries use the JOIN syntax .
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1136
		if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1137
			return $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1138
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1139
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1140
		// Since we're only checking IN queries, we're only concerned with OR relations.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1141
		if ( ! isset( $parent_query['relation'] ) || 'OR' !== $parent_query['relation'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1142
			return $alias;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1143
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1144
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1145
		$compatible_operators = array( 'IN' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1146
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1147
		foreach ( $parent_query as $sibling ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1148
			if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1149
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1150
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1151
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1152
			if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1153
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1155
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1156
			// The sibling must both have compatible operator to share its alias.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1157
			if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1158
				$alias = $sibling['alias'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1159
				break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1160
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1161
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1162
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1163
		return $alias;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
	 * Validates a single query.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1172
	 * @param array &$query The single query.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
	private function clean_query( &$query ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1175
		if ( empty( $query['taxonomy'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1176
			if ( 'term_taxonomy_id' !== $query['field'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1177
				$query = new WP_Error( 'Invalid taxonomy' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1178
				return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1179
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1180
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1181
			// so long as there are shared terms, include_children requires that a taxonomy is set
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1182
			$query['include_children'] = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1183
		} elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
			$query = new WP_Error( 'Invalid taxonomy' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
		$query['terms'] = array_unique( (array) $query['terms'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
		if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
			$this->transform_query( $query, 'term_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
			if ( is_wp_error( $query ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
			$children = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
			foreach ( $query['terms'] as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
				$children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
				$children[] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
			$query['terms'] = $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
		$this->transform_query( $query, 'term_taxonomy_id' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
	 * Transforms a single query, from one field to another.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
	 * @since 3.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
	 * @param array  &$query          The single query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
	 * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
	 *                                or 'term_id'. Default: 'term_id'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
	public function transform_query( &$query, $resulting_field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
		if ( empty( $query['terms'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
		if ( $query['field'] == $resulting_field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
		$resulting_field = sanitize_key( $resulting_field );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
		switch ( $query['field'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
			case 'slug':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
			case 'name':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1230
				foreach ( $query['terms'] as &$term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1231
					/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1232
					 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1233
					 * matter because `sanitize_term_field()` ignores the $term_id param when the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1234
					 * context is 'db'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1235
					 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1236
					$term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1237
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
				$terms = implode( ",", $query['terms'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
					SELECT $wpdb->term_taxonomy.$resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
					INNER JOIN $wpdb->terms USING (term_id)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
					WHERE taxonomy = '{$query['taxonomy']}'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
					AND $wpdb->terms.{$query['field']} IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
			case 'term_taxonomy_id':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
					SELECT $resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
					WHERE term_taxonomy_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
				$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
				$terms = $wpdb->get_col( "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
					SELECT $resulting_field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
					FROM $wpdb->term_taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
					WHERE taxonomy = '{$query['taxonomy']}'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
					AND term_id IN ($terms)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
				" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
		if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
			$query = new WP_Error( 'Inexistent terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
		$query['terms'] = $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
		$query['field'] = $resulting_field;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
 * Get all Term data from database by Term ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
 * 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
  1281
 * is possible to get a term object from the database before applying the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
 * filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
 * $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
  1285
 * 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
  1286
 * returns for the get_row method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
 * 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
  1289
 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
 * term object, and the taxonomy name as parameters. Both hooks are expected to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
 * return a Term object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
 * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
 * 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
  1295
 * $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
 * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
 * name. Must return term object. $taxonomy will be the taxonomy name, so for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
 * example, if 'category', it would be 'get_category' as the filter name. Useful
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
 * for custom taxonomies or plugging into default taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1304
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
 * @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
  1306
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
 * @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
  1308
 * @param string $taxonomy Taxonomy name that $term is part of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
 * @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
  1311
 * @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
  1312
 * exist then WP_Error will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
	if ( empty($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
		$error = new WP_Error('invalid_term', __('Empty Term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
		return $error;
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
	if ( ! taxonomy_exists($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
		$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
		return $error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
	if ( is_object($term) && empty($term->filter) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1328
		wp_cache_add( $term->term_id, $term, $taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
		$_term = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
		if ( is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
			$term = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
		if ( !$term = (int) $term )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1334
			return null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1335
		if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
			$_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
  1337
			if ( ! $_term )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1338
				return null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1339
			wp_cache_add( $term, $_term, $taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1343
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1344
	 * Filter a term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
	 * @param int|object $_term    Term object or ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
	 * @param string     $taxonomy The taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
	$_term = apply_filters( 'get_term', $_term, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1352
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1353
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1354
	 * Filter a taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1355
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1356
	 * The dynamic portion of the filter name, `$taxonomy`, refers
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1357
	 * to the taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1358
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1359
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1360
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1361
	 * @param int|object $_term    Term object or ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1362
	 * @param string     $taxonomy The taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1363
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1364
	$_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
	$_term = sanitize_term($_term, $taxonomy, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
		return $_term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
		$__term = get_object_vars($_term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
		return $__term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
		$__term = array_values(get_object_vars($_term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
		return $__term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
		return $_term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
 * Get all Term data from database by Term field and data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
 * 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
  1384
 * required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
 * 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
  1387
 * field, but not recommended that you do so.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
 * 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
  1390
 * and $field and $value combinations exist, the Term will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1394
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
 * @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
  1396
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
 * @param string|int $value Search for this term value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
 * @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
  1402
 * @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
  1403
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
	if ( 'slug' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
		$field = 't.slug';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
		$value = sanitize_title($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
		if ( empty($value) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1415
	} elseif ( 'name' == $field ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
		// Assume already escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
		$value = wp_unslash($value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
		$field = 't.name';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1419
	} elseif ( 'term_taxonomy_id' == $field ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
		$field = 'tt.term_taxonomy_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
	} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1423
		$term = get_term( (int) $value, $taxonomy, $output, $filter );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
			$term = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1429
	$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 ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1430
	if ( ! $term )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1433
	wp_cache_add( $term->term_id, $term, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1434
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1435
	/** This filter is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1436
	$term = apply_filters( 'get_term', $term, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1437
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1438
	/** This filter is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1439
	$term = apply_filters( "get_$taxonomy", $term, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1440
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
	$term = sanitize_term($term, $taxonomy, $filter);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
		return get_object_vars($term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
		return array_values(get_object_vars($term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
 * Merge all term children into a single array of their IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
 * 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
  1458
 * array of term IDs. Only useful for taxonomies which are hierarchical.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
 * Will return an empty array if $term does not exist in $taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1464
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
 * @param string $term_id ID of Term to get children
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
 * @param string $taxonomy Taxonomy Name
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1468
 * @return array|WP_Error List of Term IDs. WP_Error returned if $taxonomy does not exist
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
function get_term_children( $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
	$term_id = intval( $term_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
	$terms = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
	if ( ! isset($terms[$term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
	$children = $terms[$term_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
	foreach ( (array) $terms[$term_id] as $child ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1484
		if ( $term_id == $child ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1485
			continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1486
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1487
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
		if ( isset($terms[$child]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
			$children = array_merge($children, get_term_children($child, $taxonomy));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
	return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
 * Get sanitized Term field.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
 * Does checks for $term, based on the $taxonomy. The function is for contextual
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
 * reasons and for simplicity of usage. See sanitize_term_field() for more
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
 * information.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
 * @param string $field Term field to fetch
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
 * @param int $term Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
 * @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
  1508
 * @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
  1509
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
	$term = (int) $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
	$term = get_term( $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
	if ( is_wp_error($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
	if ( !isset($term->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
	return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
 * Sanitizes Term for editing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
 * Return value is sanitize_term() and usage is for sanitizing the term for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
 * editing. Function is for contextual and simplicity.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
 * @param int|object $id Term ID or Object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
 * @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
  1536
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
function get_term_to_edit( $id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
	$term = get_term( $id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
	if ( is_wp_error($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
	return sanitize_term($term, $taxonomy, 'edit');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
 * Retrieve the terms in a given taxonomy or list of taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
 * 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
  1553
 * well as control the output with a filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
 * 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
  1556
 * 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
  1557
 * 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
  1558
 * the array of terms, along with the $taxonomies and $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
 * The 'list_terms_exclusions' filter passes the compiled exclusions along with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
 * the $args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
 * The 'get_terms_orderby' filter passes the ORDER BY clause for the query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
 * along with the $args array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1567
 * @since 4.2.0 Introduced 'name' and 'childless' parameters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1568
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1569
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1570
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1571
 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1572
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1573
 *     Optional. Array or string of arguments to get terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1574
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1575
 *     @type string       $orderby           Field(s) to order terms by. Accepts term fields ('name', 'slug',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1576
 *                                           'term_group', 'term_id', 'id', 'description'), 'count' for term
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1577
 *                                           taxonomy count, 'include' to match the 'order' of the $include param,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1578
 *                                           or 'none' to skip ORDER BY. Defaults to 'name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1579
 *     @type string       $order             Whether to order terms in ascending or descending order.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1580
 *                                           Accepts 'ASC' (ascending) or 'DESC' (descending).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1581
 *                                           Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1582
 *     @type bool|int     $hide_empty        Whether to hide terms not assigned to any posts. Accepts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1583
 *                                           1|true or 0|false. Default 1|true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1584
 *     @type array|string $include           Array or comma/space-separated string of term ids to include.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1585
 *                                           Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1586
 *     @type array|string $exclude           Array or comma/space-separated string of term ids to exclude.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1587
 *                                           If $include is non-empty, $exclude is ignored.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1588
 *                                           Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1589
 *     @type array|string $exclude_tree      Array or comma/space-separated string of term ids to exclude
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1590
 *                                           along with all of their descendant terms. If $include is
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1591
 *                                           non-empty, $exclude_tree is ignored. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1592
 *     @type int|string   $number            Maximum number of terms to return. Accepts ''|0 (all) or any
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1593
 *                                           positive number. Default ''|0 (all).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1594
 *     @type int          $offset            The number by which to offset the terms query. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1595
 *     @type string       $fields            Term fields to query for. Accepts 'all' (returns an array of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1596
 *                                           term objects), 'ids' or 'names' (returns an array of integers
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1597
 *                                           or strings, respectively. Default 'all'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1598
 *     @type string|array $name              Optional. Name or array of names to return term(s) for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1599
 *     @type string|array $slug              Optional. Slug or array of slugs to return term(s) for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1600
 *     @type bool         $hierarchical      Whether to include terms that have non-empty descendants (even
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1601
 *                                           if $hide_empty is set to true). Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1602
 *     @type string       $search            Search criteria to match terms. Will be SQL-formatted with
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1603
 *                                           wildcards before and after. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1604
 *     @type string       $name__like        Retrieve terms with criteria by which a term is LIKE $name__like.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1605
 *                                           Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1606
 *     @type string       $description__like Retrieve terms where the description is LIKE $description__like.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1607
 *                                           Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1608
 *     @type bool         $pad_counts        Whether to pad the quantity of a term's children in the quantity
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1609
 *                                           of each term's "count" object variable. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1610
 *     @type string       $get               Whether to return terms regardless of ancestry or whether the terms
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1611
 *                                           are empty. Accepts 'all' or empty (disabled). Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1612
 *     @type int          $child_of          Term ID to retrieve child terms of. If multiple taxonomies
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1613
 *                                           are passed, $child_of is ignored. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1614
 *     @type int|string   $parent            Parent term ID to retrieve direct-child terms of. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1615
 *     @type bool         $childless         True to limit results to terms that have no children. This parameter has
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1616
 *                                           no effect on non-hierarchical taxonomies. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1617
 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1618
 *                                           object cache. Default is 'core'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1619
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1620
 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1621
 *                        do not exist.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1623
function get_terms( $taxonomies, $args = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
	$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
	$single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1628
	if ( ! is_array( $taxonomies ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
		$taxonomies = array( $taxonomies );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1630
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
	foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
		if ( ! taxonomy_exists($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
			$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
			return $error;
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
	$defaults = array('orderby' => 'name', 'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
		'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1641
		'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
		'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
		'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
	$args['number'] = absint( $args['number'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
	$args['offset'] = absint( $args['offset'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1647
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1648
	// Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1649
	$has_hierarchical_tax = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1650
	foreach ( $taxonomies as $_tax ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1651
		if ( is_taxonomy_hierarchical( $_tax ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1652
			$has_hierarchical_tax = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1653
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1654
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1655
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1656
	if ( ! $has_hierarchical_tax ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
		$args['hierarchical'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
		$args['pad_counts'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1661
	// 'parent' overrides 'child_of'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1662
	if ( 0 < intval( $args['parent'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1663
		$args['child_of'] = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1664
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1665
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
	if ( 'all' == $args['get'] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1667
		$args['childless'] = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
		$args['child_of'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
		$args['hide_empty'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
		$args['hierarchical'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
		$args['pad_counts'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1674
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1675
	 * Filter the terms query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1676
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1677
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1678
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1679
	 * @param array $args       An array of arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1680
	 * @param array $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1681
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
	$args = apply_filters( 'get_terms_args', $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1684
	// Avoid the query if the queried parent/child_of term has no descendants.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1685
	$child_of = $args['child_of'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1686
	$parent   = $args['parent'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
	if ( $child_of ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1689
		$_parent = $child_of;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1690
	} elseif ( $parent ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1691
		$_parent = $parent;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1692
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1693
		$_parent = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1696
	if ( $_parent ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1697
		$in_hierarchy = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1698
		foreach ( $taxonomies as $_tax ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1699
			$hierarchy = _get_term_hierarchy( $_tax );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1700
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1701
			if ( isset( $hierarchy[ $_parent ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1702
				$in_hierarchy = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1703
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1704
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1705
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1706
		if ( ! $in_hierarchy ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
			return $empty_array;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1708
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
	// $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
  1712
	$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1713
	$key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
	$last_changed = wp_cache_get( 'last_changed', 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
	if ( ! $last_changed ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
		$last_changed = microtime();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
		wp_cache_set( 'last_changed', $last_changed, 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
	$cache_key = "get_terms:$key:$last_changed";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
	$cache = wp_cache_get( $cache_key, 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
	if ( false !== $cache ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1722
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1723
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1724
		 * Filter the given taxonomy's terms cache.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1725
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1726
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1727
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1728
		 * @param array $cache      Cached array of terms for the given taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1729
		 * @param array $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1730
		 * @param array $args       An array of arguments to get terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1731
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1732
		$cache = apply_filters( 'get_terms', $cache, $taxonomies, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1733
		return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1736
	$_orderby = strtolower( $args['orderby'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1737
	if ( 'count' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
		$orderby = 'tt.count';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1739
	} elseif ( 'name' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
		$orderby = 't.name';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1741
	} elseif ( 'slug' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
		$orderby = 't.slug';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1743
	} elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1744
		$include = implode( ',', array_map( 'absint', $args['include'] ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1745
		$orderby = "FIELD( t.term_id, $include )";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1746
	} elseif ( 'term_group' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
		$orderby = 't.term_group';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1748
	} elseif ( 'description' == $_orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1749
		$orderby = 'tt.description';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1750
	} elseif ( 'none' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
		$orderby = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1752
	} elseif ( empty($_orderby) || 'id' == $_orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
		$orderby = 't.term_id';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1754
	} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
		$orderby = 't.name';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1756
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1757
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1758
	 * Filter the ORDERBY clause of the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1759
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1760
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1761
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1762
	 * @param string $orderby    ORDERBY clause of the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1763
	 * @param array  $args       An array of terms query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1764
	 * @param array  $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1765
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
	$orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1768
	$order = strtoupper( $args['order'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1769
	if ( ! empty( $orderby ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
		$orderby = "ORDER BY $orderby";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1771
	} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
		$order = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1773
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1774
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1775
	if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
		$order = 'ASC';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1777
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
	$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1780
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1781
	$exclude = $args['exclude'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1782
	$exclude_tree = $args['exclude_tree'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1783
	$include = $args['include'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1784
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
	$inclusions = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
	if ( ! empty( $include ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
		$exclude = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
		$exclude_tree = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
		$inclusions = implode( ',', wp_parse_id_list( $include ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
	if ( ! empty( $inclusions ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
		$inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
		$where .= $inclusions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1797
	$exclusions = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
	if ( ! empty( $exclude_tree ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
		$exclude_tree = wp_parse_id_list( $exclude_tree );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
		$excluded_children = $exclude_tree;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
		foreach ( $exclude_tree as $extrunk ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
			$excluded_children = array_merge(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
				$excluded_children,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
				(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
  1805
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1806
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1807
		$exclusions = array_merge( $excluded_children, $exclusions );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
	if ( ! empty( $exclude ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1811
		$exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1812
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1813
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1814
	// 'childless' terms are those without an entry in the flattened term hierarchy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1815
	$childless = (bool) $args['childless'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1816
	if ( $childless ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1817
		foreach ( $taxonomies as $_tax ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1818
			$term_hierarchy = _get_term_hierarchy( $_tax );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1819
			$exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1820
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1823
	if ( ! empty( $exclusions ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1824
		$exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1825
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1826
		$exclusions = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1827
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1828
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1829
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1830
	 * Filter the terms to exclude from the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1831
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1832
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1833
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1834
	 * @param string $exclusions NOT IN clause of the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1835
	 * @param array  $args       An array of terms query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1836
	 * @param array  $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1837
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
	$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1840
	if ( ! empty( $exclusions ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
		$where .= $exclusions;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1842
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1843
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1844
	if ( ! empty( $args['name'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1845
		if ( is_array( $args['name'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1846
			$name = array_map( 'sanitize_text_field', $args['name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1847
			$where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $name ) ) . "')";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1848
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1849
			$name = sanitize_text_field( $args['name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1850
			$where .= $wpdb->prepare( " AND t.name = %s", $name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1851
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1854
	if ( ! empty( $args['slug'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1855
		if ( is_array( $args['slug'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1856
			$slug = array_map( 'sanitize_title', $args['slug'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1857
			$where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1858
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1859
			$slug = sanitize_title( $args['slug'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1860
			$where .= " AND t.slug = '$slug'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1861
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1864
	if ( ! empty( $args['name__like'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1865
		$where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1866
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1867
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1868
	if ( ! empty( $args['description__like'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1869
		$where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
	if ( '' !== $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
		$parent = (int) $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
		$where .= " AND tt.parent = '$parent'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1877
	$hierarchical = $args['hierarchical'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1878
	if ( 'count' == $args['fields'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
		$hierarchical = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1880
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1881
	if ( $args['hide_empty'] && !$hierarchical ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
		$where .= ' AND tt.count > 0';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1883
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1884
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1885
	$number = $args['number'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1886
	$offset = $args['offset'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
	// 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
  1889
	if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1890
		if ( $offset ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1891
			$limits = 'LIMIT ' . $offset . ',' . $number;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1892
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
			$limits = 'LIMIT ' . $number;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1894
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
		$limits = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1899
	if ( ! empty( $args['search'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1900
		$like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1901
		$where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
	$selects = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1905
	switch ( $args['fields'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
		case 'all':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
			$selects = array( 't.*', 'tt.*' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
		case 'ids':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
		case 'id=>parent':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1911
			$selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
		case 'names':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1914
			$selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1915
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1916
		case 'count':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1917
			$orderby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1918
			$order = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
			$selects = array( 'COUNT(*)' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
		case 'id=>name':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1922
			$selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
		case 'id=>slug':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1925
			$selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1927
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1928
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1929
	$_fields = $args['fields'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1930
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1931
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1932
	 * Filter the fields to select in the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1933
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1934
	 * Field lists modified using this filter will only modify the term fields returned
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1935
	 * by the function when the `$fields` parameter set to 'count' or 'all'. In all other
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1936
	 * cases, the term fields in the results array will be determined by the `$fields`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1937
	 * parameter alone.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1938
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1939
	 * Use of this filter can result in unpredictable behavior, and is not recommended.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1940
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1941
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1942
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1943
	 * @param array $selects    An array of fields to select for the terms query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1944
	 * @param array $args       An array of term query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1945
	 * @param array $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1946
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
	$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
	$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
  1950
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
	$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1952
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1953
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1954
	 * Filter the terms query SQL clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1955
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1956
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1957
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1958
	 * @param array $pieces     Terms query SQL clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1959
	 * @param array $taxonomies An array of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1960
	 * @param array $args       An array of terms query arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1961
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
	$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1963
	$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1964
	$join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1965
	$where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1966
	$orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1967
	$order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1968
	$limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
	$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
  1971
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1972
	if ( 'count' == $_fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
		$term_count = $wpdb->get_var($query);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
		return $term_count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
	$terms = $wpdb->get_results($query);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1978
	if ( 'all' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1979
		update_term_cache( $terms );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
	if ( empty($terms) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
		wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1984
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1985
		/** This filter is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1986
		$terms = apply_filters( 'get_terms', array(), $taxonomies, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
		return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
	if ( $child_of ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1991
		foreach ( $taxonomies as $_tax ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1992
			$children = _get_term_hierarchy( $_tax );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1993
			if ( ! empty( $children ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1994
				$terms = _get_term_children( $child_of, $terms, $_tax );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1995
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1996
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
	// Update term counts to include children.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2000
	if ( $args['pad_counts'] && 'all' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2001
		foreach ( $taxonomies as $_tax ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2002
			_pad_term_counts( $terms, $_tax );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2003
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2004
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
	// Make sure we show empty categories that have children.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2007
	if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
		foreach ( $terms as $k => $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
			if ( ! $term->count ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2010
				$children = get_term_children( $term->term_id, $term->taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2011
				if ( is_array( $children ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2012
					foreach ( $children as $child_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2013
						$child = get_term( $child_id, $term->taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2014
						if ( $child->count ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
							continue 2;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2016
						}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2017
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2018
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
				// It really is empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
				unset($terms[$k]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
	$_terms = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2027
	if ( 'id=>parent' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2028
		foreach ( $terms as $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2029
			$_terms[ $term->term_id ] = $term->parent;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2030
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2031
	} elseif ( 'ids' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2032
		foreach ( $terms as $term ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
			$_terms[] = $term->term_id;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2034
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2035
	} elseif ( 'names' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2036
		foreach ( $terms as $term ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
			$_terms[] = $term->name;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2038
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2039
	} elseif ( 'id=>name' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2040
		foreach ( $terms as $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2041
			$_terms[ $term->term_id ] = $term->name;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2042
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2043
	} elseif ( 'id=>slug' == $_fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2044
		foreach ( $terms as $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2045
			$_terms[ $term->term_id ] = $term->slug;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2046
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2049
	if ( ! empty( $_terms ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
		$terms = $_terms;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2051
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2052
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2053
	if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
		$terms = array_slice( $terms, $offset, $number );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2055
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
	wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2059
	/** This filter is documented in wp-includes/taxonomy */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
	$terms = apply_filters( 'get_terms', $terms, $taxonomies, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	return $terms;
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
 * Check if Term exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
 * Formerly is_term(), introduced in 2.3.0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2071
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
 * @param int|string $term The term to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 * @param string $taxonomy The taxonomy name to use
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2075
 * @param int $parent Optional. ID of parent term under which to confine the exists search.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2076
 * @return mixed Returns null if the term does not exist. Returns the term ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2077
 *               if no taxonomy is specified and the term ID exists. Returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2078
 *               an array of the term ID and the term taxonomy ID the taxonomy
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2079
 *               is specified and the pairing exists.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2081
function term_exists( $term, $taxonomy = '', $parent = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
	$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
	$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
  2086
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
	if ( is_int($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
		if ( 0 == $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
			return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
		$where = 't.term_id = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2091
		if ( !empty($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2092
			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
  2093
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
			return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2095
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2097
	$term = trim( wp_unslash( $term ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2098
	$slug = sanitize_title( $term );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2099
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2100
	$where = 't.slug = %s';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2101
	$else_where = 't.name = %s';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2102
	$where_fields = array($slug);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2103
	$else_where_fields = array($term);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2104
	$orderby = 'ORDER BY t.term_id ASC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2105
	$limit = 'LIMIT 1';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2106
	if ( !empty($taxonomy) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2107
		if ( is_numeric( $parent ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2108
			$parent = (int) $parent;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2109
			$where_fields[] = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2110
			$else_where_fields[] = $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2111
			$where .= ' AND tt.parent = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2112
			$else_where .= ' AND tt.parent = %d';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2113
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2115
		$where_fields[] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2116
		$else_where_fields[] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2117
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2118
		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 $orderby $limit", $where_fields), ARRAY_A) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2119
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2120
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2121
		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 $orderby $limit", $else_where_fields), ARRAY_A);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2122
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2124
	if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
		return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2126
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2127
	return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2128
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2130
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2131
 * Check if a term is an ancestor of another term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2132
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2133
 * You can use either an id or the term object for both parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2134
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2135
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2136
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2137
 * @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
  2138
 * @param int|object $term2 The child term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2139
 * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
 * @return bool Whether $term2 is child of $term1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
	if ( ! isset( $term1->term_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
		$term1 = get_term( $term1, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
	if ( ! isset( $term2->parent ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
		$term2 = get_term( $term2, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
	if ( empty( $term1->term_id ) || empty( $term2->parent ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
	if ( $term2->parent == $term1->term_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
	return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
 * Sanitize Term all fields.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2159
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2160
 * this function will sanitize <strong>all</strong> fields. The context is based
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2161
 * on sanitize_term_field().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2162
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
 * The $term is expected to be either an array or an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2165
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2166
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2167
 * @param array|object $term The term to check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2168
 * @param string $taxonomy The taxonomy name to use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2169
 * @param string $context Default is 'display'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
 * @return array|object Term with all fields sanitized
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2171
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2172
function sanitize_term($term, $taxonomy, $context = 'display') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2173
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2174
	$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2175
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2176
	$do_object = is_object( $term );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
	$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
  2179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
	foreach ( (array) $fields as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
		if ( $do_object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
			if ( isset($term->$field) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2183
				$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
			if ( isset($term[$field]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
				$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2188
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2190
	if ( $do_object )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2191
		$term->filter = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2192
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2193
		$term['filter'] = $context;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2195
	return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2196
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
 * Cleanse the field value in the term based on the context.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2201
 * Passing a term field value through the function should be assumed to have
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
 * 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
  2203
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2204
 * If no context or an unsupported context is given, then default filters will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2205
 * be applied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
 * There are enough filters for each context to support a custom filtering
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2208
 * without creating your own filter function. Simply create a function that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
 * hooks into the filter you need.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2211
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2213
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
 * @param string $field Term field to sanitize
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
 * @param string $value Search for this term value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
 * @param int $term_id Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2218
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2219
 * @param string $context Either edit, db, display, attribute, or js.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2220
 * @return mixed sanitized field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2221
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2222
function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2223
	$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2224
	if ( in_array( $field, $int_fields ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2225
		$value = (int) $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2226
		if ( $value < 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
			$value = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
	if ( 'raw' == $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
	if ( 'edit' == $context ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2234
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2235
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2236
		 * Filter a term field to edit before it is sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2237
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2238
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2239
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2240
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2241
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2242
		 * @param mixed $value     Value of the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2243
		 * @param int   $term_id   Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2244
		 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2245
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2246
		$value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2247
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2248
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2249
		 * Filter the taxonomy field to edit before it is sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2250
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2251
		 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2252
		 * to the taxonomy slug and taxonomy field, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2253
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2254
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2255
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2256
		 * @param mixed $value   Value of the taxonomy field to edit.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2257
		 * @param int   $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2258
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2259
		$value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2260
		if ( 'description' == $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
			$value = esc_html($value); // textarea_escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2263
			$value = esc_attr($value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2264
	} elseif ( 'db' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2265
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2266
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2267
		 * Filter a term field value before it is sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2268
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2269
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2270
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2271
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2272
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2273
		 * @param mixed  $value    Value of the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2274
		 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2275
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2276
		$value = apply_filters( "pre_term_{$field}", $value, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2277
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2278
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2279
		 * Filter a taxonomy field before it is sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2280
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2281
		 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2282
		 * to the taxonomy slug and field name, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2283
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2284
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2285
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2286
		 * @param mixed $value Value of the taxonomy field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2287
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2288
		$value = apply_filters( "pre_{$taxonomy}_{$field}", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2289
		// Back compat filters
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2290
		if ( 'slug' == $field ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2291
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2292
			 * Filter the category nicename before it is sanitized.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2293
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2294
			 * Use the pre_{$taxonomy}_{$field} hook instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2295
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2296
			 * @since 2.0.3
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2297
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2298
			 * @param string $value The category nicename.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2299
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2300
			$value = apply_filters( 'pre_category_nicename', $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2301
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2302
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2303
	} elseif ( 'rss' == $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2304
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2305
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2306
		 * Filter the term field for use in RSS.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2307
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2308
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2309
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2310
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2311
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2312
		 * @param mixed  $value    Value of the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2313
		 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2314
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2315
		$value = apply_filters( "term_{$field}_rss", $value, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2316
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2317
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2318
		 * Filter the taxonomy field for use in RSS.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2319
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2320
		 * The dynamic portions of the hook name, `$taxonomy`, and $field, refer
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2321
		 * to the taxonomy slug and field name, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2322
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2323
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2324
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2325
		 * @param mixed $value Value of the taxonomy field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2326
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2327
		$value = apply_filters( "{$taxonomy}_{$field}_rss", $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
		// Use display filters by default.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2330
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2331
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2332
		 * Filter the term field sanitized for display.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2333
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2334
		 * The dynamic portion of the filter name, `$field`, refers to the term field name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2335
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2336
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2337
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2338
		 * @param mixed  $value    Value of the term field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2339
		 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2340
		 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2341
		 * @param string $context  Context to retrieve the term field value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2342
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2343
		$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2344
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2345
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2346
		 * Filter the taxonomy field sanitized for display.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2347
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2348
		 * The dynamic portions of the filter name, `$taxonomy`, and $field, refer
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2349
		 * to the taxonomy slug and taxonomy field, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2350
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2351
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2352
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2353
		 * @param mixed  $value   Value of the taxonomy field.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2354
		 * @param int    $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2355
		 * @param string $context Context to retrieve the taxonomy field value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2356
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2357
		$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2358
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2359
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2360
	if ( 'attribute' == $context ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
		$value = esc_attr($value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2362
	} elseif ( 'js' == $context ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
		$value = esc_js($value);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2364
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
	return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
 * Count how many terms are in Taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2370
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
 * 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
  2372
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2375
 * @param string $taxonomy Taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2376
 * @param array|string $args Overwrite defaults. See get_terms()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2377
 * @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
  2378
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2379
function wp_count_terms( $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
	$defaults = array('hide_empty' => false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
	// backwards compatibility
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2384
	if ( isset($args['ignore_empty']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2385
		$args['hide_empty'] = $args['ignore_empty'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2386
		unset($args['ignore_empty']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2387
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2388
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2389
	$args['fields'] = 'count';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2391
	return get_terms($taxonomy, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2392
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2393
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2394
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2395
 * Will unlink the object from the taxonomy or taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
 * Will remove all relationships between the object and any terms in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
 * a particular taxonomy or taxonomies. Does not remove the term or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
 * taxonomy itself.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
 * @param int $object_id The term Object Id that refers to the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2407
	$object_id = (int) $object_id;
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 ( !is_array($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2410
		$taxonomies = array($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2411
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2412
	foreach ( (array) $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2413
		$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2414
		$term_ids = array_map( 'intval', $term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2415
		wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2416
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2417
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2418
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2419
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2420
 * Removes a term from the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2421
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2422
 * 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
  2423
 * that term's parent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2424
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2425
 * 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
  2426
 * term found. Any other and the found terms are used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2427
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2428
 * The $args 'force_default' will force the term supplied as default to be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2429
 * assigned even if the object was not going to be termless
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2430
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2431
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2432
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2433
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
 * @param int $term Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
 * @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
  2438
 * @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
  2439
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2440
function wp_delete_term( $term, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2441
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2442
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2443
	$term = (int) $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2445
	if ( ! $ids = term_exists($term, $taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2446
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2447
	if ( is_wp_error( $ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2448
		return $ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2450
	$tt_id = $ids['term_taxonomy_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2451
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2452
	$defaults = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2453
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2454
	if ( 'category' == $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2455
		$defaults['default'] = get_option( 'default_category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2456
		if ( $defaults['default'] == $term )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2457
			return 0; // Don't delete the default category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2458
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2459
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2460
	$args = wp_parse_args($args, $defaults);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2461
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2462
	if ( isset( $args['default'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2463
		$default = (int) $args['default'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2464
		if ( ! term_exists( $default, $taxonomy ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2465
			unset( $default );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2466
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2467
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2468
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2469
	if ( isset( $args['force_default'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2470
		$force_default = $args['force_default'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2471
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2472
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2473
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2474
	 * Fires when deleting a term, before any modifications are made to posts or terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2475
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2476
	 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2477
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2478
	 * @param int    $term     Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2479
	 * @param string $taxonomy Taxonomy Name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2480
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2481
	do_action( 'pre_delete_term', $term, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2482
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2483
	// Update children to point to new parent
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2484
	if ( is_taxonomy_hierarchical($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2485
		$term_obj = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2486
		if ( is_wp_error( $term_obj ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2487
			return $term_obj;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2488
		$parent = $term_obj->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2489
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2490
		$edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2491
		$edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2492
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2493
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2494
		 * Fires immediately before a term to delete's children are reassigned a parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2495
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2496
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2497
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2498
		 * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2499
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2500
		do_action( 'edit_term_taxonomies', $edit_tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2501
		$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2502
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2503
		// Clean the cache for all child terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2504
		$edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2505
		clean_term_cache( $edit_term_ids, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2506
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2507
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2508
		 * Fires immediately after a term to delete's children are reassigned a parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2509
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2510
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2511
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2512
		 * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2513
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2514
		do_action( 'edited_term_taxonomies', $edit_tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2515
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2517
	$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
  2518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2519
	foreach ( (array) $objects as $object ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2520
		$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2521
		if ( 1 == count($terms) && isset($default) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2522
			$terms = array($default);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2523
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2524
			$terms = array_diff($terms, array($term));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2525
			if (isset($default) && isset($force_default) && $force_default)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2526
				$terms = array_merge($terms, array($default));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2527
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2528
		$terms = array_map('intval', $terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2529
		wp_set_object_terms($object, $terms, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2530
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2531
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2532
	// Clean the relationship caches for all object types using this term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2533
	$tax_object = get_taxonomy( $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2534
	foreach ( $tax_object->object_type as $object_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2535
		clean_object_term_cache( $objects, $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2536
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2537
	// Get the object before deletion so we can pass to actions below
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2538
	$deleted_term = get_term( $term, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2539
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2540
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2541
	 * Fires immediately before a term taxonomy ID is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2542
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2543
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2544
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2545
	 * @param int $tt_id Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2546
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2547
	do_action( 'delete_term_taxonomy', $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2548
	$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2549
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2550
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2551
	 * Fires immediately after a term taxonomy ID is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2552
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2553
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2554
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2555
	 * @param int $tt_id Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2556
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2557
	do_action( 'deleted_term_taxonomy', $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2559
	// Delete the term if no taxonomies use it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2560
	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
  2561
		$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2563
	clean_term_cache($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2564
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2565
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2566
	 * Fires after a term is deleted from the database and the cache is cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2567
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2568
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2569
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2570
	 * @param int     $term         Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2571
	 * @param int     $tt_id        Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2572
	 * @param string  $taxonomy     Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2573
	 * @param mixed   $deleted_term Copy of the already-deleted term, in the form specified
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2574
	 *                              by the parent function. WP_Error otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2575
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2576
	do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2577
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2578
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2579
	 * Fires after a term in a specific taxonomy is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2580
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2581
	 * The dynamic portion of the hook name, `$taxonomy`, refers to the specific
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2582
	 * taxonomy the term belonged to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2583
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2584
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2585
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2586
	 * @param int     $term         Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2587
	 * @param int     $tt_id        Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2588
	 * @param mixed   $deleted_term Copy of the already-deleted term, in the form specified
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2589
	 *                              by the parent function. {@see WP_Error} otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2590
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2591
	do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2592
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2593
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2594
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2596
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2597
 * Deletes one existing category.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2598
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2599
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2600
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2601
 * @param int $cat_ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2602
 * @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
  2603
 * 	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
  2604
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2605
function wp_delete_category( $cat_ID ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2606
	return wp_delete_term( $cat_ID, 'category' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2607
}
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
 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2611
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2612
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2613
 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2614
 *              Introduced `$parent` argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2615
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2616
 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2617
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2618
 * @param int|array    $object_ids The ID(s) of the object(s) to retrieve.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2619
 * @param string|array $taxonomies The taxonomies to retrieve terms from.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2620
 * @param array|string $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2621
 *     Array of arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2622
 *     @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2623
 *                           'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2624
 *     @type string $order   Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2625
 *     @type string $fields  Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2626
 *                           'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2627
 *                           term objects being returned, 'ids' will return an array of integers, and 'names' an array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2628
 *                           of strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2629
 *     @type int    $parent  Optional. Limit results to the direct children of a given term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2630
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2631
 * @return array|WP_Error The requested term data or empty array if no terms found.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2632
 *                        WP_Error if any of the $taxonomies don't exist.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2633
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2634
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2635
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2637
	if ( empty( $object_ids ) || empty( $taxonomies ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2638
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2639
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2640
	if ( !is_array($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2641
		$taxonomies = array($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2642
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2643
	foreach ( $taxonomies as $taxonomy ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2644
		if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2645
			return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2646
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2647
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2648
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2649
		$object_ids = array($object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2650
	$object_ids = array_map('intval', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2651
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2652
	$defaults = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2653
		'orderby' => 'name',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2654
		'order'   => 'ASC',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2655
		'fields'  => 'all',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2656
		'parent'  => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2657
	);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2658
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2659
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2660
	$terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2661
	if ( count($taxonomies) > 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2662
		foreach ( $taxonomies as $index => $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2663
			$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2664
			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
  2665
				unset($taxonomies[$index]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2666
				$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
  2667
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2669
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2670
		$t = get_taxonomy($taxonomies[0]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2671
		if ( isset($t->args) && is_array($t->args) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2672
			$args = array_merge($args, $t->args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2673
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2674
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2675
	$orderby = $args['orderby'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2676
	$order = $args['order'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2677
	$fields = $args['fields'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2678
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2679
	if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2680
		$orderby = "t.$orderby";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2681
	} else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2682
		$orderby = "tt.$orderby";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2683
	} else if ( 'term_order' === $orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2684
		$orderby = 'tr.term_order';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2685
	} else if ( 'none' === $orderby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2686
		$orderby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2687
		$order = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2688
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2689
		$orderby = 't.term_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2690
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2692
	// tt_ids queries can only be none or tr.term_taxonomy_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2693
	if ( ('tt_ids' == $fields) && !empty($orderby) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2694
		$orderby = 'tr.term_taxonomy_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2696
	if ( !empty($orderby) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2697
		$orderby = "ORDER BY $orderby";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2698
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2699
	$order = strtoupper( $order );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2700
	if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2701
		$order = 'ASC';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2702
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2703
	$taxonomy_array = $taxonomies;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2704
	$object_id_array = $object_ids;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2705
	$taxonomies = "'" . implode("', '", $taxonomies) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2706
	$object_ids = implode(', ', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2708
	$select_this = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2709
	if ( 'all' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2710
		$select_this = 't.*, tt.*';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2711
	} elseif ( 'ids' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2712
		$select_this = 't.term_id';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2713
	} elseif ( 'names' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2714
		$select_this = 't.name';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2715
	} elseif ( 'slugs' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2716
		$select_this = 't.slug';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2717
	} elseif ( 'all_with_object_id' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2718
		$select_this = 't.*, tt.*, tr.object_id';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2719
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2720
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2721
	$where = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2722
		"tt.taxonomy IN ($taxonomies)",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2723
		"tr.object_id IN ($object_ids)",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2724
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2725
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2726
	if ( '' !== $args['parent'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2727
		$where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2728
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2729
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2730
	$where = implode( ' AND ', $where );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2731
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2732
	$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 $where $orderby $order";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2733
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2734
	$objects = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2735
	if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2736
		$_terms = $wpdb->get_results( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2737
		foreach ( $_terms as $key => $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2738
			$_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2739
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2740
		$terms = array_merge( $terms, $_terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2741
		update_term_cache( $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2742
		$objects = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2743
	} elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2744
		$_terms = $wpdb->get_col( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2745
		$_field = ( 'ids' == $fields ) ? 'term_id' : 'name';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2746
		foreach ( $_terms as $key => $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2747
			$_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2748
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2749
		$terms = array_merge( $terms, $_terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2750
	} elseif ( 'tt_ids' == $fields ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2751
		$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");
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2752
		foreach ( $terms as $key => $tt_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2753
			$terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2754
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2755
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2756
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2757
	if ( ! $terms ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2758
		$terms = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2759
	} elseif ( $objects && 'all_with_object_id' !== $fields ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2760
		$_tt_ids = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2761
		$_terms = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2762
		foreach ( $terms as $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2763
			if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2764
				continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2765
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2766
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2767
			$_tt_ids[] = $term->term_taxonomy_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2768
			$_terms[] = $term;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2769
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2770
		$terms = $_terms;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2771
	} elseif ( ! $objects ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2772
		$terms = array_values( array_unique( $terms ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2773
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2774
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2775
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2776
	 * Filter the terms for a given object or objects.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2777
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2778
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2779
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2780
	 * @param array $terms           An array of terms for the given object or objects.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2781
	 * @param array $object_id_array Array of object IDs for which `$terms` were retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2782
	 * @param array $taxonomy_array  Array of taxonomies from which `$terms` were retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2783
	 * @param array $args            An array of arguments for retrieving terms for the given
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2784
	 *                               object(s). See wp_get_object_terms() for details.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2785
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2786
	$terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2787
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2788
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2789
	 * Filter the terms for a given object or objects.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2790
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2791
	 * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2792
	 * {@see 'get_object_terms'} filter is recommended as an alternative.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2793
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2794
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2795
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2796
	 * @param array     $terms      An array of terms for the given object or objects.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2797
	 * @param int|array $object_ids Object ID or array of IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2798
	 * @param string    $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2799
	 * @param array     $args       An array of arguments for retrieving terms for the given object(s).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2800
	 *                              See {@see wp_get_object_terms()} for details.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2801
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2802
	return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2803
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2805
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2806
 * Add a new term to the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2807
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2808
 * A non-existent term is inserted in the following sequence:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2809
 * 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
  2810
 * 2. If everything is correct, several actions are fired.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2811
 * 3. The 'term_id_filter' is evaluated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2812
 * 4. The term cache is cleaned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2813
 * 5. Several more actions are fired.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2814
 * 6. An array is returned containing the term_id and term_taxonomy_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2815
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2816
 * 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
  2817
 * 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
  2818
 * is given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2819
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2820
 * If the taxonomy is hierarchical, and the 'parent' argument is not empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2821
 * the term is inserted and the term_id will be given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2822
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2823
 * Error handling:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2824
 * If $taxonomy does not exist or $term is empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2825
 * a WP_Error object will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2826
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2827
 * If the term already exists on the same hierarchical level,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2828
 * 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
  2829
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2830
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2831
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2832
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2833
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2834
 * @param string       $term     The term to add or update.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2835
 * @param string       $taxonomy The taxonomy to which to add the term.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2836
 * @param array|string $args {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2837
 *     Optional. Array or string of arguments for inserting a term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2838
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2839
 *     @type string $alias_of    Slug of the term to make this term an alias of.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2840
 *                               Default empty string. Accepts a term slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2841
 *     @type string $description The term description. Default empty string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2842
 *     @type int    $parent      The id of the parent term. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2843
 *     @type string $slug        The term slug to use. Default empty string.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2844
 * }
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2845
 * @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2846
 *                        {@see WP_Error} otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2847
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2848
function wp_insert_term( $term, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2849
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2850
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2851
	if ( ! taxonomy_exists($taxonomy) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2852
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2853
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2854
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2855
	 * Filter a term before it is sanitized and inserted into the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2856
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2857
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2858
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2859
	 * @param string $term     The term to add or update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2860
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2861
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2862
	$term = apply_filters( 'pre_insert_term', $term, $taxonomy );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2863
	if ( is_wp_error( $term ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2864
		return $term;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2865
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2866
	if ( is_int($term) && 0 == $term ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2867
		return new WP_Error('invalid_term_id', __('Invalid term ID'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2868
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2869
	if ( '' == trim($term) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2870
		return new WP_Error('empty_term_name', __('A name is required for this term'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2871
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2872
	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2873
	$args = wp_parse_args( $args, $defaults );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2874
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2875
	if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2876
		return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2877
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2878
	$args['name'] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2879
	$args['taxonomy'] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2880
	$args = sanitize_term($args, $taxonomy, 'db');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2881
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2882
	// expected_slashed ($name)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2883
	$name = wp_unslash( $args['name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2884
	$description = wp_unslash( $args['description'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2885
	$parent = (int) $args['parent'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2886
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2887
	$slug_provided = ! empty( $args['slug'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2888
	if ( ! $slug_provided ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2889
		$slug = sanitize_title( $name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2890
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2891
		$slug = $args['slug'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2892
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2893
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2894
	$term_group = 0;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2895
	if ( $args['alias_of'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2896
		$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2897
		if ( ! empty( $alias->term_group ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2898
			// 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
  2899
			$term_group = $alias->term_group;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2900
		} elseif ( ! empty( $alias->term_id ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2901
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2902
			 * The alias is not in a group, so we create a new one
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2903
			 * and add the alias to it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2904
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2905
			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2906
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2907
			wp_update_term( $alias->term_id, $taxonomy, array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2908
				'term_group' => $term_group,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2909
			) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2910
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2911
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2912
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2913
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2914
	 * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2915
	 * unless a unique slug has been explicitly provided.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2916
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2917
	if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2918
		$slug_match = get_term_by( 'slug', $slug, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2919
		if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2920
			if ( is_taxonomy_hierarchical( $taxonomy ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2921
				$siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2922
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2923
				$existing_term = null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2924
				if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2925
					$existing_term = $name_match;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2926
				} elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2927
					$existing_term = $slug_match;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2928
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2929
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2930
				if ( $existing_term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2931
					return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2932
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2933
			} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2934
				return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2935
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2936
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2937
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2938
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2939
	$slug = wp_unique_term_slug( $slug, (object) $args );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2940
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2941
	if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2942
		return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2943
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2944
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2945
	$term_id = (int) $wpdb->insert_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2946
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2947
	// 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
  2948
	if ( empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2949
		$slug = sanitize_title($slug, $term_id);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2950
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2951
		/** This action is documented in wp-includes/taxonomy.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2952
		do_action( 'edit_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2953
		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2954
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2955
		/** This action is documented in wp-includes/taxonomy.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2956
		do_action( 'edited_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2957
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2958
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2959
	$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
  2960
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2961
	if ( !empty($tt_id) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2962
		return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2963
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2964
	$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
  2965
	$tt_id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2966
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2967
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2968
	 * Sanity check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2969
	 * an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2970
	 * and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2971
	 * are not fired.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2972
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2973
	$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, tt.term_taxonomy_id FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2974
	if ( $duplicate_term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2975
		$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2976
		$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2977
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2978
		$term_id = (int) $duplicate_term->term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2979
		$tt_id   = (int) $duplicate_term->term_taxonomy_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2980
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2981
		clean_term_cache( $term_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2982
		return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2983
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2984
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2985
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2986
	 * Fires immediately after a new term is created, before the term cache is cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2987
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2988
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2989
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2990
	 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2991
	 * @param int    $tt_id    Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2992
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2993
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2994
	do_action( "create_term", $term_id, $tt_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2995
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2996
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2997
	 * Fires after a new term is created for a specific taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2998
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2999
	 * The dynamic portion of the hook name, `$taxonomy`, refers
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3000
	 * to the slug of the taxonomy the term was created for.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3001
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3002
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3003
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3004
	 * @param int $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3005
	 * @param int $tt_id   Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3006
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3007
	do_action( "create_$taxonomy", $term_id, $tt_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3008
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3009
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3010
	 * Filter the term ID after a new term is created.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3011
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3012
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3013
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3014
	 * @param int $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3015
	 * @param int $tt_id   Taxonomy term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3016
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3017
	$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3019
	clean_term_cache($term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3020
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3021
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3022
	 * Fires after a new term is created, and after the term cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3023
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3024
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3025
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3026
	do_action( "created_term", $term_id, $tt_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3027
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3028
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3029
	 * Fires after a new term in a specific taxonomy is created, and after the term
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3030
	 * cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3031
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3032
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3033
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3034
	 * @param int $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3035
	 * @param int $tt_id   Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3036
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3037
	do_action( "created_$taxonomy", $term_id, $tt_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3039
	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3040
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3041
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3042
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3043
 * Create Term and Taxonomy Relationships.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3044
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3045
 * 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
  3046
 * 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
  3047
 * it doesn't exist (using the slug).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3048
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3049
 * 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
  3050
 * 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
  3051
 * exists under.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3052
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3053
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3054
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3055
 * @param int              $object_id The object to relate to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3056
 * @param array|int|string $terms     A single term slug, single term id, or array of either term slugs or ids.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3057
 *                                    Will replace all existing related terms in this taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3058
 * @param string           $taxonomy  The context in which to relate the term to the object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3059
 * @param bool             $append    Optional. If false will delete difference of terms. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3060
 * @return array|WP_Error Affected Term IDs.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3061
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3062
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3063
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3064
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3065
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3066
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3067
	if ( ! taxonomy_exists($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3068
		return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3069
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3070
	if ( !is_array($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3071
		$terms = array($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3072
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3073
	if ( ! $append )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3074
		$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
  3075
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3076
		$old_tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3077
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3078
	$tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3079
	$term_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3080
	$new_tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3082
	foreach ( (array) $terms as $term) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3083
		if ( !strlen(trim($term)) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3084
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3085
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3086
		if ( !$term_info = term_exists($term, $taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3087
			// Skip if a non-existent term ID is passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3088
			if ( is_int($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3089
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3090
			$term_info = wp_insert_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3091
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3092
		if ( is_wp_error($term_info) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3093
			return $term_info;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3094
		$term_ids[] = $term_info['term_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3095
		$tt_id = $term_info['term_taxonomy_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3096
		$tt_ids[] = $tt_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3097
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3098
		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
  3099
			continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3100
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3101
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3102
		 * Fires immediately before an object-term relationship is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3103
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3104
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3105
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3106
		 * @param int $object_id Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3107
		 * @param int $tt_id     Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3108
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3109
		do_action( 'add_term_relationship', $object_id, $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3110
		$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3111
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3112
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3113
		 * Fires immediately after an object-term relationship is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3114
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3115
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3116
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3117
		 * @param int $object_id Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3118
		 * @param int $tt_id     Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3119
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3120
		do_action( 'added_term_relationship', $object_id, $tt_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3121
		$new_tt_ids[] = $tt_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3122
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3124
	if ( $new_tt_ids )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3125
		wp_update_term_count( $new_tt_ids, $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
	if ( ! $append ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3128
		$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3130
		if ( $delete_tt_ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3131
			$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3132
			$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
  3133
			$delete_term_ids = array_map( 'intval', $delete_term_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3135
			$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3136
			if ( is_wp_error( $remove ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3137
				return $remove;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3138
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3139
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3140
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3142
	$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3143
	if ( ! $append && isset($t->sort) && $t->sort ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3144
		$values = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3145
		$term_order = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3146
		$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
  3147
		foreach ( $tt_ids as $tt_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3148
			if ( in_array($tt_id, $final_tt_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3149
				$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3150
		if ( $values )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3151
			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
  3152
				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
  3153
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3155
	wp_cache_delete( $object_id, $taxonomy . '_relationships' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3156
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3157
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3158
	 * Fires after an object's terms have been set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3159
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3160
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3161
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3162
	 * @param int    $object_id  Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3163
	 * @param array  $terms      An array of object terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3164
	 * @param array  $tt_ids     An array of term taxonomy IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3165
	 * @param string $taxonomy   Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3166
	 * @param bool   $append     Whether to append new terms to the old terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3167
	 * @param array  $old_tt_ids Old array of term taxonomy IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3168
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3169
	do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3170
	return $tt_ids;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3171
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3173
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3174
 * Add term(s) associated with a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3175
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3176
 * @since 3.6.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3177
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3178
 * @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
  3179
 * @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
  3180
 * @param array|string $taxonomy Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3181
 * @return array|WP_Error Affected Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3182
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3183
function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3184
	return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3185
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3186
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3187
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3188
 * Remove term(s) associated with a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3189
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3190
 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3191
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3192
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3193
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3194
 * @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
  3195
 * @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
  3196
 * @param array|string $taxonomy Taxonomy name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3197
 * @return bool|WP_Error True on success, false or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3198
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3199
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3200
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3202
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3204
	if ( ! taxonomy_exists( $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3205
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3206
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3208
	if ( ! is_array( $terms ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3209
		$terms = array( $terms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3210
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3212
	$tt_ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3213
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3214
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3215
		if ( ! strlen( trim( $term ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3216
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3217
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3218
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3219
		if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3220
			// Skip if a non-existent term ID is passed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3221
			if ( is_int( $term ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3222
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3223
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3224
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3226
		if ( is_wp_error( $term_info ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3227
			return $term_info;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3228
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3230
		$tt_ids[] = $term_info['term_taxonomy_id'];
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
	if ( $tt_ids ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3234
		$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3235
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3236
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3237
		 * Fires immediately before an object-term relationship is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3238
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3239
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3240
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3241
		 * @param int   $object_id Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3242
		 * @param array $tt_ids    An array of term taxonomy IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3243
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3244
		do_action( 'delete_term_relationships', $object_id, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3245
		$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3246
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3247
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3248
		 * Fires immediately after an object-term relationship is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3249
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3250
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3251
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3252
		 * @param int   $object_id Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3253
		 * @param array $tt_ids    An array of term taxonomy IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3254
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3255
		do_action( 'deleted_term_relationships', $object_id, $tt_ids );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3256
		wp_update_term_count( $tt_ids, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3257
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3258
		return (bool) $deleted;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3259
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3260
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3261
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3262
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3263
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3264
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3265
 * Will make slug unique, if it isn't already.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3266
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3267
 * The $slug has to be unique global to every taxonomy, meaning that one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3268
 * taxonomy term can't have a matching slug with another taxonomy term. Each
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3269
 * slug has to be globally unique for every taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3270
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3271
 * 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
  3272
 * hierarchical and has a parent, it will append that parent to the $slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3273
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3274
 * 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
  3275
 * until it finds a number that is truly unique.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3276
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3277
 * The only purpose for $term is for appending a parent, if one exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3278
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3279
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3280
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3281
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3282
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3283
 * @param string $slug The string that will be tried for a unique slug
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3284
 * @param object $term The term object that the $slug will belong too
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3285
 * @return string Will return a true unique slug.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3286
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3287
function wp_unique_term_slug($slug, $term) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3288
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3289
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3290
	if ( ! term_exists( $slug ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3291
		return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3292
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3293
	// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3294
	if ( get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3295
		return $slug;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3296
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3297
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3298
	// 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
  3299
	// by incorporating parent slugs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3300
	if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3301
		$the_parent = $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3302
		while ( ! empty($the_parent) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3303
			$parent_term = get_term($the_parent, $term->taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3304
			if ( is_wp_error($parent_term) || empty($parent_term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3305
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3306
			$slug .= '-' . $parent_term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3307
			if ( ! term_exists( $slug ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3308
				return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3309
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3310
			if ( empty($parent_term->parent) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3311
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3312
			$the_parent = $parent_term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3313
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3314
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3315
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3316
	// 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
  3317
	if ( ! empty( $term->term_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3318
		$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
  3319
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3320
		$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3321
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3322
	if ( $wpdb->get_var( $query ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3323
		$num = 2;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3324
		do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3325
			$alt_slug = $slug . "-$num";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3326
			$num++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3327
			$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
  3328
		} while ( $slug_check );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3329
		$slug = $alt_slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3330
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3331
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3332
	return $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3333
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3334
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3335
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3336
 * Update term based on arguments provided.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3337
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3338
 * The $args will indiscriminately override all values with the same field name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3339
 * Care must be taken to not override important information need to update or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3340
 * update will fail (or perhaps create a new term, neither would be acceptable).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3341
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3342
 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3343
 * defined in $args already.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3344
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3345
 * '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
  3346
 * it for the $term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3347
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3348
 * 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
  3349
 * 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
  3350
 * 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
  3351
 * will be created for you.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3352
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3353
 * 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
  3354
 * away from the term keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3355
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3356
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3357
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3358
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3359
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3360
 * @param int $term_id The ID of the term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3361
 * @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
  3362
 * @param array|string $args Overwrite term field values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3363
 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3364
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3365
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3366
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3367
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3368
	if ( ! taxonomy_exists( $taxonomy ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3369
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3370
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3372
	$term_id = (int) $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3373
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3374
	// First, get all of the original args
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3375
	$term = get_term( $term_id, $taxonomy, ARRAY_A );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3376
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3377
	if ( is_wp_error( $term ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3378
		return $term;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3379
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3380
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3381
	if ( ! $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3382
		return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3383
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3385
	// Escape data pulled from DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3386
	$term = wp_slash($term);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3388
	// Merge old and new args with new args overwriting old ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3389
	$args = array_merge($term, $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3390
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3391
	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3392
	$args = wp_parse_args($args, $defaults);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3393
	$args = sanitize_term($args, $taxonomy, 'db');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3394
	$parsed_args = $args;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3396
	// expected_slashed ($name)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3397
	$name = wp_unslash( $args['name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3398
	$description = wp_unslash( $args['description'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3399
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3400
	$parsed_args['name'] = $name;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3401
	$parsed_args['description'] = $description;
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
	if ( '' == trim($name) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3404
		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
  3405
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3406
	if ( $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3407
		return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3408
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3409
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3410
	$empty_slug = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3411
	if ( empty( $args['slug'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3412
		$empty_slug = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3413
		$slug = sanitize_title($name);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3414
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3415
		$slug = $args['slug'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3416
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3417
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3418
	$parsed_args['slug'] = $slug;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3419
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3420
	$term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3421
	if ( $args['alias_of'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3422
		$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3423
		if ( ! empty( $alias->term_group ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3424
			// 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
  3425
			$term_group = $alias->term_group;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3426
		} elseif ( ! empty( $alias->term_id ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3427
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3428
			 * The alias is not in a group, so we create a new one
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3429
			 * and add the alias to it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3430
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3431
			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3432
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3433
			wp_update_term( $alias->term_id, $taxonomy, array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3434
				'term_group' => $term_group,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3435
			) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3436
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3437
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3438
		$parsed_args['term_group'] = $term_group;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3439
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3440
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3441
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3442
	 * Filter the term parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3443
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3444
	 * Hook to this filter to see if it will cause a hierarchy loop.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3445
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3446
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3447
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3448
	 * @param int    $parent      ID of the parent term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3449
	 * @param int    $term_id     Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3450
	 * @param string $taxonomy    Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3451
	 * @param array  $parsed_args An array of potentially altered update arguments for the given term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3452
	 * @param array  $args        An array of update arguments for the given term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3453
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3454
	$parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3456
	// Check for duplicate slug
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3457
	$duplicate = get_term_by( 'slug', $slug, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3458
	if ( $duplicate && $duplicate->term_id != $term_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3459
		// 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
  3460
		// Otherwise, bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3461
		if ( $empty_slug || ( $parent != $term['parent']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3462
			$slug = wp_unique_term_slug($slug, (object) $args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3463
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3464
			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
  3465
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3466
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3467
	$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) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3468
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3469
	// Check whether this is a shared term that needs splitting.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3470
	$_term_id = _split_shared_term( $term_id, $tt_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3471
	if ( ! is_wp_error( $_term_id ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3472
		$term_id = $_term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3473
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3474
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3475
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3476
	 * Fires immediately before the given terms are edited.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3477
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3478
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3479
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3480
	 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3481
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3482
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3483
	do_action( 'edit_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3484
	$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3485
	if ( empty($slug) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3486
		$slug = sanitize_title($name, $term_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3487
		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3488
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3489
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3490
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3491
	 * Fires immediately after the given terms are edited.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3492
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3493
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3494
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3495
	 * @param int    $term_id  Term ID
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3496
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3497
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3498
	do_action( 'edited_terms', $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3499
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3500
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3501
	 * Fires immediate before a term-taxonomy relationship is updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3502
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3503
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3504
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3505
	 * @param int    $tt_id    Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3506
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3507
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3508
	do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3509
	$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3510
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3511
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3512
	 * Fires immediately after a term-taxonomy relationship is updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3513
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3514
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3515
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3516
	 * @param int    $tt_id    Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3517
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3518
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3519
	do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3520
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3521
	// Clean the relationship caches for all object types using this term
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3522
	$objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3523
	$tax_object = get_taxonomy( $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3524
	foreach ( $tax_object->object_type as $object_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3525
		clean_object_term_cache( $objects, $object_type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3526
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3527
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3528
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3529
	 * Fires after a term has been updated, but before the term cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3530
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3531
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3532
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3533
	 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3534
	 * @param int    $tt_id    Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3535
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3536
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3537
	do_action( "edit_term", $term_id, $tt_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3538
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3539
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3540
	 * Fires after a term in a specific taxonomy has been updated, but before the term
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3541
	 * cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3542
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3543
	 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3544
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3545
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3546
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3547
	 * @param int $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3548
	 * @param int $tt_id   Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3549
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3550
	do_action( "edit_$taxonomy", $term_id, $tt_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3551
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3552
	/** This filter is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3553
	$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3554
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3555
	clean_term_cache($term_id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3556
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3557
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3558
	 * Fires after a term has been updated, and the term cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3559
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3560
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3561
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3562
	 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3563
	 * @param int    $tt_id    Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3564
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3565
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3566
	do_action( "edited_term", $term_id, $tt_id, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3567
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3568
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3569
	 * Fires after a term for a specific taxonomy has been updated, and the term
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3570
	 * cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3571
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3572
	 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3573
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3574
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3575
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3576
	 * @param int $term_id Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3577
	 * @param int $tt_id   Term taxonomy ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3578
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3579
	do_action( "edited_$taxonomy", $term_id, $tt_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3580
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3581
	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3582
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3583
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3584
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3585
 * Enable or disable term counting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3586
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3587
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3588
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3589
 * @param bool $defer Optional. Enable if true, disable if false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3590
 * @return bool Whether term counting is enabled or disabled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3591
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3592
function wp_defer_term_counting($defer=null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3593
	static $_defer = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3595
	if ( is_bool($defer) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3596
		$_defer = $defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3597
		// flush any deferred counts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3598
		if ( !$defer )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3599
			wp_update_term_count( null, null, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3600
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3602
	return $_defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3603
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3604
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3605
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3606
 * Updates the amount of terms in taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3607
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3608
 * 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
  3609
 * the count.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3610
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3611
 * 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
  3612
 * of term ID. Once that is done, then update the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3613
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3614
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3615
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3616
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3617
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3618
 * @param int|array $terms The term_taxonomy_id of the terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3619
 * @param string $taxonomy The context of the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3620
 * @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
  3621
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3622
function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3623
	static $_deferred = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3625
	if ( $do_deferred ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3626
		foreach ( (array) array_keys($_deferred) as $tax ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3627
			wp_update_term_count_now( $_deferred[$tax], $tax );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3628
			unset( $_deferred[$tax] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3629
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3630
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3632
	if ( empty($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3633
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3635
	if ( !is_array($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3636
		$terms = array($terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3637
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3638
	if ( wp_defer_term_counting() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3639
		if ( !isset($_deferred[$taxonomy]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3640
			$_deferred[$taxonomy] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3641
		$_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3642
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3643
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3644
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3645
	return wp_update_term_count_now( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3646
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3647
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3648
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3649
 * Perform term count update immediately.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3650
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3651
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3652
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3653
 * @param array $terms The term_taxonomy_id of terms to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3654
 * @param string $taxonomy The context of the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3655
 * @return bool Always true when complete.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3656
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3657
function wp_update_term_count_now( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3658
	$terms = array_map('intval', $terms);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3659
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3660
	$taxonomy = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3661
	if ( !empty($taxonomy->update_count_callback) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3662
		call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3663
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3664
		$object_types = (array) $taxonomy->object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3665
		foreach ( $object_types as &$object_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3666
			if ( 0 === strpos( $object_type, 'attachment:' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3667
				list( $object_type ) = explode( ':', $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3668
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3670
		if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3671
			// Only post types are attached to this taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3672
			_update_post_term_count( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3673
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3674
			// Default count updater
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3675
			_update_generic_term_count( $terms, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3676
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3677
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3678
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3679
	clean_term_cache($terms, '', false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3681
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3682
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3683
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3684
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3685
// Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3686
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3687
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3688
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3689
 * Removes the taxonomy relationship to terms from the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3690
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3691
 * Will remove the entire taxonomy relationship containing term $object_id. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3692
 * 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
  3693
 * take place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3694
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3695
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3696
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3697
 * @see get_object_taxonomies() for more on $object_type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3698
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3699
 * @param int|array $object_ids Single or list of term object ID(s)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3700
 * @param array|string $object_type The taxonomy object type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3701
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3702
function clean_object_term_cache($object_ids, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3703
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3704
		$object_ids = array($object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3705
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3706
	$taxonomies = get_object_taxonomies( $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3707
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3708
	foreach ( $object_ids as $id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3709
		foreach ( $taxonomies as $taxonomy ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3710
			wp_cache_delete($id, "{$taxonomy}_relationships");
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3711
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3712
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3713
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3714
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3715
	 * Fires after the object term cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3716
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3717
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3718
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3719
	 * @param array  $object_ids An array of object IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3720
	 * @param string $objet_type Object type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3721
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3722
	do_action( 'clean_object_term_cache', $object_ids, $object_type );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3723
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3724
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3725
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3726
 * Will remove all of the term ids from the cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3727
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3728
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3729
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3730
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3731
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3732
 * @param int|array $ids Single or list of Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3733
 * @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
  3734
 * @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
  3735
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3736
function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3737
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3738
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3739
	if ( !is_array($ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3740
		$ids = array($ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3741
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3742
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3743
	// If no taxonomy, assume tt_ids.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3744
	if ( empty($taxonomy) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3745
		$tt_ids = array_map('intval', $ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3746
		$tt_ids = implode(', ', $tt_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3747
		$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
  3748
		$ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3749
		foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3750
			$taxonomies[] = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3751
			$ids[] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3752
			wp_cache_delete($term->term_id, $term->taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3753
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3754
		$taxonomies = array_unique($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3755
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3756
		$taxonomies = array($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3757
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3758
			foreach ( $ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3759
				wp_cache_delete($id, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3760
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3761
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3762
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3763
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3764
	foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3765
		if ( $clean_taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3766
			wp_cache_delete('all_ids', $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3767
			wp_cache_delete('get', $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3768
			delete_option("{$taxonomy}_children");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3769
			// Regenerate {$taxonomy}_children
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3770
			_get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3771
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3772
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3773
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3774
		 * Fires once after each taxonomy's term cache has been cleaned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3775
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3776
		 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3777
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3778
		 * @param array  $ids      An array of term IDs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3779
		 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3780
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3781
		do_action( 'clean_term_cache', $ids, $taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3782
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3783
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3784
	wp_cache_set( 'last_changed', microtime(), 'terms' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3785
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3786
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3787
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3788
 * Retrieves the taxonomy relationship to the term object id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3789
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3790
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3791
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3792
 * @param int    $id       Term object ID
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3793
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3794
 * @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
  3795
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3796
function get_object_term_cache($id, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3797
	$cache = wp_cache_get($id, "{$taxonomy}_relationships");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3798
	return $cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3799
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3800
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3801
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3802
 * Updates the cache for the given term object ID(s).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3803
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3804
 * Note: Due to performance concerns, great care should be taken to only update
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3805
 * term caches when necessary. Processing time can increase exponentially depending
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3806
 * on both the number of passed term IDs and the number of taxonomies those terms
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3807
 * belong to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3808
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3809
 * Caches will only be updated for terms not already cached.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3810
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3811
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3812
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3813
 * @param string|array $object_ids  Comma-separated list or array of term object IDs..
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3814
 * @param array|string $object_type The taxonomy object type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3815
 * @return null|false Null if `$object_ids` is empty, false if all of the terms in
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3816
 *                    `$object_ids` are already cached.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3817
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3818
function update_object_term_cache($object_ids, $object_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3819
	if ( empty($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3820
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3821
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3822
	if ( !is_array($object_ids) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3823
		$object_ids = explode(',', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3824
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3825
	$object_ids = array_map('intval', $object_ids);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3826
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3827
	$taxonomies = get_object_taxonomies($object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3828
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3829
	$ids = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3830
	foreach ( (array) $object_ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3831
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3832
			if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3833
				$ids[] = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3834
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3835
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3836
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3837
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3839
	if ( empty( $ids ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3840
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3841
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3842
	$terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3843
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3844
	$object_terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3845
	foreach ( (array) $terms as $term )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3846
		$object_terms[$term->object_id][$term->taxonomy][] = $term;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3847
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3848
	foreach ( $ids as $id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3849
		foreach ( $taxonomies as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3850
			if ( ! isset($object_terms[$id][$taxonomy]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3851
				if ( !isset($object_terms[$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3852
					$object_terms[$id] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3853
				$object_terms[$id][$taxonomy] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3854
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3855
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3856
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3857
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3858
	foreach ( $object_terms as $id => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3859
		foreach ( $value as $taxonomy => $terms ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3860
			wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3861
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3862
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3863
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3864
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3865
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3866
 * Updates Terms to Taxonomy in cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3867
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3868
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3869
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3870
 * @param array $terms List of Term objects to change
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3871
 * @param string $taxonomy Optional. Update Term to this taxonomy in cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3872
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3873
function update_term_cache($terms, $taxonomy = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3874
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3875
		$term_taxonomy = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3876
		if ( empty($term_taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3877
			$term_taxonomy = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3878
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3879
		wp_cache_add( $term->term_id, $term, $term_taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3880
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3881
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3882
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3883
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3884
// Private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3885
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3886
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3887
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3888
 * Retrieves children of taxonomy as Term IDs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3889
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3890
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3891
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3892
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3893
 * @param string $taxonomy Taxonomy Name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3894
 * @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
  3895
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3896
function _get_term_hierarchy($taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3897
	if ( !is_taxonomy_hierarchical($taxonomy) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3898
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3899
	$children = get_option("{$taxonomy}_children");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3900
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3901
	if ( is_array($children) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3902
		return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3903
	$children = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3904
	$terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3905
	foreach ( $terms as $term_id => $parent ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3906
		if ( $parent > 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3907
			$children[$parent][] = $term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3908
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3909
	update_option("{$taxonomy}_children", $children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3910
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3911
	return $children;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3912
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3914
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3915
 * Get the subset of $terms that are descendants of $term_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3916
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3917
 * 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
  3918
 * 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
  3919
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3920
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3921
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3922
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3923
 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3924
 * @param array  $terms     The set of terms - either an array of term objects or term IDs - from which those that
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3925
 *                          are descendants of $term_id will be chosen.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3926
 * @param string $taxonomy  The taxonomy which determines the hierarchy of the terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3927
 * @param array  $ancestors Term ancestors that have already been identified. Passed by reference, to keep track of
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3928
 *                          found terms when recursing the hierarchy. The array of located ancestors is used to prevent
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3929
 *                          infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3930
 * @return array The subset of $terms that are descendants of $term_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3931
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3932
function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3933
	$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3934
	if ( empty($terms) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3935
		return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3936
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3937
	$term_list = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3938
	$has_children = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3940
	if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3941
		return $empty_array;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3942
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3943
	// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3944
	if ( empty( $ancestors ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3945
		$ancestors[ $term_id ] = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3946
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3947
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3948
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3949
		$use_id = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3950
		if ( !is_object($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3951
			$term = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3952
			if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3953
				return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3954
			$use_id = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3955
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3956
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3957
		// Don't recurse if we've already identified the term as a child - this indicates a loop.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3958
		if ( isset( $ancestors[ $term->term_id ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3959
			continue;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3960
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3961
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3962
		if ( $term->parent == $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3963
			if ( $use_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3964
				$term_list[] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3965
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3966
				$term_list[] = $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3968
			if ( !isset($has_children[$term->term_id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3969
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3970
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3971
			$ancestors[ $term->term_id ] = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3972
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3973
			if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3974
				$term_list = array_merge($term_list, $children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3975
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3976
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3978
	return $term_list;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3979
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3980
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3981
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3982
 * Add count of children to parent count.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3983
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3984
 * Recalculates term counts by including items from child terms. Assumes all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3985
 * relevant children are already in the $terms argument.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3986
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3987
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3988
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3989
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3990
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3991
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3992
 * @param array $terms List of Term IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3993
 * @param string $taxonomy Term Context
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3994
 * @return null Will break from function if conditions are not met.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3995
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3996
function _pad_term_counts(&$terms, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3997
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3998
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3999
	// This function only works for hierarchical taxonomies like post categories.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4000
	if ( !is_taxonomy_hierarchical( $taxonomy ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4001
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4002
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4003
	$term_hier = _get_term_hierarchy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4004
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4005
	if ( empty($term_hier) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4006
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4008
	$term_items = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4009
	$terms_by_id = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4010
	$term_ids = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4011
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4012
	foreach ( (array) $terms as $key => $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4013
		$terms_by_id[$term->term_id] = & $terms[$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4014
		$term_ids[$term->term_taxonomy_id] = $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4015
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4016
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4017
	// Get the object and term ids and stick them in a lookup table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4018
	$tax_obj = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4019
	$object_types = esc_sql($tax_obj->object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4020
	$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
  4021
	foreach ( $results as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4022
		$id = $term_ids[$row->term_taxonomy_id];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4023
		$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
  4024
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4026
	// Touch every ancestor's lookup row for each post in each term
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4027
	foreach ( $term_ids as $term_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4028
		$child = $term_id;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4029
		$ancestors = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4030
		while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4031
			$ancestors[] = $child;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4032
			if ( !empty( $term_items[$term_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4033
				foreach ( $term_items[$term_id] as $item_id => $touches ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4034
					$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
  4035
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4036
			$child = $parent;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4037
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4038
			if ( in_array( $parent, $ancestors ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4039
				break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4040
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4041
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4042
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4043
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4044
	// Transfer the touched cells
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4045
	foreach ( (array) $term_items as $id => $items )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4046
		if ( isset($terms_by_id[$id]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4047
			$terms_by_id[$id]->count = count($items);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4048
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4050
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4051
// Default callbacks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4052
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4053
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4054
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4055
 * Will update term count based on object types of the current taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4056
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4057
 * Private function for the default callback for post_tag and category
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4058
 * taxonomies.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4059
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4060
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4061
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4062
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4063
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4064
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4065
 * @param array $terms List of Term taxonomy IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4066
 * @param object $taxonomy Current taxonomy object of terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4067
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4068
function _update_post_term_count( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4069
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4071
	$object_types = (array) $taxonomy->object_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4072
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4073
	foreach ( $object_types as &$object_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4074
		list( $object_type ) = explode( ':', $object_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4075
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4076
	$object_types = array_unique( $object_types );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4077
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4078
	if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4079
		unset( $object_types[ $check_attachments ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4080
		$check_attachments = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4081
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4082
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4083
	if ( $object_types )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4084
		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4085
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4086
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4087
		$count = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4088
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4089
		// 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
  4090
		if ( $check_attachments )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4091
			$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
  4092
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4093
		if ( $object_types )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4094
			$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
  4095
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4096
		/** This action is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4097
		do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4098
		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4099
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4100
		/** This action is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4101
		do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4102
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4103
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4105
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4106
 * Will update term count based on number of objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4107
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4108
 * Default callback for the link_category taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4109
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4110
 * @since 3.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4111
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4112
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4113
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4114
 * @param array $terms List of Term taxonomy IDs
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4115
 * @param object $taxonomy Current taxonomy object of terms
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4116
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4117
function _update_generic_term_count( $terms, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4118
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4120
	foreach ( (array) $terms as $term ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4121
		$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
  4122
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4123
		/** This action is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4124
		do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4125
		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4126
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4127
		/** This action is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4128
		do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4129
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4130
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4131
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4132
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4133
 * Create a new term for a term_taxonomy item that currently shares its term with another term_taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4134
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4135
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4136
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4137
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4138
 * @param int  $term_id          ID of the shared term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4139
 * @param int  $term_taxonomy_id ID of the term_taxonomy item to receive a new term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4140
 * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4141
 *                      database schema), `$term_id` is returned. When the term is successfully split, the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4142
 *                      new term_id is returned. A WP_Error is returned for miscellaneous errors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4143
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4144
function _split_shared_term( $term_id, $term_taxonomy_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4145
	global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4146
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4147
	// Don't try to split terms if database schema does not support shared slugs.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4148
	$current_db_version = get_option( 'db_version' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4149
	if ( $current_db_version < 30133 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4150
		return $term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4151
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4152
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4153
	// If there are no shared term_taxonomy rows, there's nothing to do here.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4154
	$shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4155
	if ( ! $shared_tt_count ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4156
		return $term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4157
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4158
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4159
	// Pull up data about the currently shared slug, which we'll use to populate the new one.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4160
	$shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4161
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4162
	$new_term_data = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4163
		'name' => $shared_term->name,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4164
		'slug' => $shared_term->slug,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4165
		'term_group' => $shared_term->term_group,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4166
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4167
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4168
	if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4169
		return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4170
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4171
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4172
	$new_term_id = (int) $wpdb->insert_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4173
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4174
	// Update the existing term_taxonomy to point to the newly created term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4175
	$wpdb->update( $wpdb->term_taxonomy,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4176
		array( 'term_id' => $new_term_id ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4177
		array( 'term_taxonomy_id' => $term_taxonomy_id )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4178
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4179
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4180
	// Reassign child terms to the new parent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4181
	$term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4182
	$children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4183
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4184
	if ( ! empty( $children_tt_ids ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4185
		foreach ( $children_tt_ids as $child_tt_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4186
			$wpdb->update( $wpdb->term_taxonomy,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4187
				array( 'parent' => $new_term_id ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4188
				array( 'term_taxonomy_id' => $child_tt_id )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4189
			);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4190
			clean_term_cache( $term_id, $term_taxonomy->taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4191
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4192
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4193
		// If the term has no children, we must force its taxonomy cache to be rebuilt separately.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4194
		clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4195
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4196
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4197
	// Clean the cache for term taxonomies formerly shared with the current term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4198
	$shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4199
	if ( $shared_term_taxonomies ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4200
		foreach ( $shared_term_taxonomies as $shared_term_taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4201
			clean_term_cache( $term_id, $shared_term_taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4202
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4203
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4204
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4205
	// Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4206
	$split_term_data = get_option( '_split_terms', array() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4207
	if ( ! isset( $split_term_data[ $term_id ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4208
		$split_term_data[ $term_id ] = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4209
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4210
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4211
	$split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4212
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4213
	update_option( '_split_terms', $split_term_data );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4214
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4215
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4216
	 * Fires after a previously shared taxonomy term is split into two separate terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4217
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4218
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4219
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4220
	 * @param int    $term_id          ID of the formerly shared term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4221
	 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4222
	 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4223
	 * @param string $taxonomy         Taxonomy for the split term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4224
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4225
	do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4226
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4227
	return $new_term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4228
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4229
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4230
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4231
 * Check default categories when a term gets split to see if any of them need to be updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4232
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4233
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4234
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4235
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4236
 * @param int    $term_id          ID of the formerly shared term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4237
 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4238
 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4239
 * @param string $taxonomy         Taxonomy for the split term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4240
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4241
function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4242
	if ( 'category' != $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4243
		return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4244
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4245
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4246
	foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4247
		if ( $term_id == get_option( $option, -1 ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4248
			update_option( $option, $new_term_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4249
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4250
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4251
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4252
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4253
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4254
 * Check menu items when a term gets split to see if any of them need to be updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4255
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4256
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4257
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4258
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4259
 * @param int    $term_id          ID of the formerly shared term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4260
 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4261
 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4262
 * @param string $taxonomy         Taxonomy for the split term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4263
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4264
function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4265
	global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4266
	$post_ids = $wpdb->get_col( $wpdb->prepare(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4267
		"SELECT m1.post_id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4268
		FROM {$wpdb->postmeta} AS m1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4269
			INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4270
			INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4271
		WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4272
			AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4273
			AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4274
		$taxonomy,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4275
		$term_id
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4276
	) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4277
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4278
	if ( $post_ids ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4279
		foreach ( $post_ids as $post_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4280
			update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4281
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4282
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4283
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4284
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4285
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4286
 * Get data about terms that previously shared a single term_id, but have since been split.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4287
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4288
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4289
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4290
 * @param int $old_term_id Term ID. This is the old, pre-split term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4291
 * @return array Array of new term IDs, keyed by taxonomy.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4292
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4293
function wp_get_split_terms( $old_term_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4294
	$split_terms = get_option( '_split_terms', array() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4295
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4296
	$terms = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4297
	if ( isset( $split_terms[ $old_term_id ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4298
		$terms = $split_terms[ $old_term_id ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4299
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4300
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4301
	return $terms;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4302
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4303
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4304
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4305
 * Get the new term ID corresponding to a previously split term.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4306
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4307
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4308
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4309
 * @param int    $old_term_id Term ID. This is the old, pre-split term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4310
 * @param string $taxonomy    Taxonomy that the term belongs to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4311
 * @return bool|int If a previously split term is found corresponding to the old term_id and taxonomy,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4312
 *                  the new term_id will be returned. If no previously split term is found matching
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4313
 *                  the parameters, returns false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4314
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4315
function wp_get_split_term( $old_term_id, $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4316
	$split_terms = wp_get_split_terms( $old_term_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4317
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4318
	$term_id = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4319
	if ( isset( $split_terms[ $taxonomy ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4320
		$term_id = (int) $split_terms[ $taxonomy ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4321
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4322
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4323
	return $term_id;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4324
}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4325
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4326
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4327
 * Generate a permalink for a taxonomy term archive.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4328
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4329
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4330
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4331
 * @param object|int|string $term     The term object, ID, or slug whose link will be retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4332
 * @param string            $taxonomy Optional. Taxonomy. Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4333
 * @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
  4334
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4335
function get_term_link( $term, $taxonomy = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4336
	global $wp_rewrite;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4338
	if ( !is_object($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4339
		if ( is_int($term) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4340
			$term = get_term($term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4341
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4342
			$term = get_term_by('slug', $term, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4343
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4344
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4346
	if ( !is_object($term) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4347
		$term = new WP_Error('invalid_term', __('Empty Term'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4349
	if ( is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4350
		return $term;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4352
	$taxonomy = $term->taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4354
	$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4355
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4356
	$slug = $term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4357
	$t = get_taxonomy($taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4359
	if ( empty($termlink) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4360
		if ( 'category' == $taxonomy )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4361
			$termlink = '?cat=' . $term->term_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4362
		elseif ( $t->query_var )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4363
			$termlink = "?$t->query_var=$slug";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4364
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4365
			$termlink = "?taxonomy=$taxonomy&term=$slug";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4366
		$termlink = home_url($termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4367
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4368
		if ( $t->rewrite['hierarchical'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4369
			$hierarchical_slugs = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4370
			$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4371
			foreach ( (array)$ancestors as $ancestor ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4372
				$ancestor_term = get_term($ancestor, $taxonomy);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4373
				$hierarchical_slugs[] = $ancestor_term->slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4374
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4375
			$hierarchical_slugs = array_reverse($hierarchical_slugs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4376
			$hierarchical_slugs[] = $slug;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4377
			$termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4378
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4379
			$termlink = str_replace("%$taxonomy%", $slug, $termlink);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4380
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4381
		$termlink = home_url( user_trailingslashit($termlink, 'category') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4382
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4383
	// Back Compat filters.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4384
	if ( 'post_tag' == $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4385
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4386
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4387
		 * Filter the tag link.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4388
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4389
		 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4390
		 * @deprecated 2.5.0 Use 'term_link' instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4391
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4392
		 * @param string $termlink Tag link URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4393
		 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4394
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4395
		$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4396
	} elseif ( 'category' == $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4397
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4398
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4399
		 * Filter the category link.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4400
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4401
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4402
		 * @deprecated 2.5.0 Use 'term_link' instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4403
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4404
		 * @param string $termlink Category link URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4405
		 * @param int    $term_id  Term ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4406
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4407
		$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4408
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4409
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4410
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4411
	 * Filter the term link.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4412
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4413
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4414
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4415
	 * @param string $termlink Term link URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4416
	 * @param object $term     Term object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4417
	 * @param string $taxonomy Taxonomy slug.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4418
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4419
	return apply_filters( 'term_link', $termlink, $term, $taxonomy );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4420
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4422
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4423
 * Display the taxonomies of a post with available options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4424
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4425
 * 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
  4426
 * 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
  4427
 * display the taxonomies for a specific post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4428
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4429
 * @since 2.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4430
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4431
 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4432
 *     Arguments about which post to use and how to format the output. Shares all of the arguments supported by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4433
 *     {@link get_the_taxonomies()}, in addition to the following.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4434
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4435
 *     @type  int|WP_Post $post   Post ID or object to get taxonomies of. Default current post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4436
 *     @type  string      $before Displays before the taxonomies. Default empty string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4437
 *     @type  string      $sep    Separates each taxonomy. Default is a space.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4438
 *     @type  string      $after  Displays after the taxonomies. Default empty string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4439
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4440
 * @param array $args See {@link get_the_taxonomies()} for a description of arguments and their defaults.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4441
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4442
function the_taxonomies( $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4443
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4444
		'post' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4445
		'before' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4446
		'sep' => ' ',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4447
		'after' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4448
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4450
	$r = wp_parse_args( $args, $defaults );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4451
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4452
	echo $r['before'] . join( $r['sep'], get_the_taxonomies( $r['post'], $r ) ) . $r['after'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4453
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4454
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4455
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4456
 * Retrieve all taxonomies associated with a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4457
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4458
 * 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
  4459
 * the taxonomies with links to the taxonomy and name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4460
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4461
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4462
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4463
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4464
 * @param array $args {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4465
 *     Arguments about how to format the list of taxonomies.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4466
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4467
 *     @type string $template      Template for displaying a taxonomy label and list of terms.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4468
 *                                 Default is "Label: Terms."
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4469
 *     @type string $term_template Template for displaying a single term in the list. Default is the term name
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4470
 *                                 linked to its archive.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4471
 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4472
 * @return array List of taxonomies.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4473
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4474
function get_the_taxonomies( $post = 0, $args = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4475
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4476
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4477
	$args = wp_parse_args( $args, array(
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4478
		/* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4479
		'template' => __( '%s: %l.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4480
		'term_template' => '<a href="%1$s">%2$s</a>',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4481
	) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4483
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4484
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4485
	if ( ! $post ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4486
		return $taxonomies;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4487
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4488
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4489
	foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4490
		$t = (array) get_taxonomy( $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4491
		if ( empty( $t['label'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4492
			$t['label'] = $taxonomy;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4493
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4494
		if ( empty( $t['args'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4495
			$t['args'] = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4496
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4497
		if ( empty( $t['template'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4498
			$t['template'] = $args['template'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4499
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4500
		if ( empty( $t['term_template'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4501
			$t['term_template'] = $args['term_template'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4502
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4503
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4504
		$terms = get_object_term_cache( $post->ID, $taxonomy );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4505
		if ( false === $terms ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4506
			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4507
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4508
		$links = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4509
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4510
		foreach ( $terms as $term ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4511
			$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4512
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4513
		if ( $links ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4514
			$taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4515
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4516
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4517
	return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4518
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4519
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4520
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4521
 * Retrieve all taxonomies of a post with just the names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4522
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4523
 * @since 2.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4524
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4525
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4526
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4527
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4528
function get_post_taxonomies( $post = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4529
	$post = get_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4530
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4531
	return get_object_taxonomies($post);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4532
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4533
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4534
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4535
 * Determine if the given object is associated with any of the given terms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4536
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4537
 * 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
  4538
 * 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
  4539
 * 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
  4540
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4541
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4542
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4543
 * @param int $object_id ID of the object (post ID, link ID, ...)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4544
 * @param string $taxonomy Single taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4545
 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4546
 * @return bool|WP_Error WP_Error on input error.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4547
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4548
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4549
	if ( !$object_id = (int) $object_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4550
		return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4551
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4552
	$object_terms = get_object_term_cache( $object_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4553
	if ( false === $object_terms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4554
		 $object_terms = wp_get_object_terms( $object_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4555
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4556
	if ( is_wp_error( $object_terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4557
		return $object_terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4558
	if ( empty( $object_terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4559
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4560
	if ( empty( $terms ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4561
		return ( !empty( $object_terms ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4563
	$terms = (array) $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4565
	if ( $ints = array_filter( $terms, 'is_int' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4566
		$strs = array_diff( $terms, $ints );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4567
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4568
		$strs =& $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4569
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4570
	foreach ( $object_terms as $object_term ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4571
		// If term is an int, check against term_ids only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4572
		if ( $ints && in_array( $object_term->term_id, $ints ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4573
			return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4574
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4575
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4576
		if ( $strs ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4577
			// Only check numeric strings against term_id, to avoid false matches due to type juggling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4578
			$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4579
			if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4580
				return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4581
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4582
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4583
			if ( in_array( $object_term->name, $strs ) ) return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4584
			if ( in_array( $object_term->slug, $strs ) ) return true;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4585
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4586
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4587
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4588
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4589
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4591
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4592
 * Determine if the given object type is associated with the given taxonomy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4593
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4594
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4595
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4596
 * @param string $object_type Object type string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4597
 * @param string $taxonomy Single taxonomy name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4598
 * @return bool True if object is associated with the taxonomy, otherwise false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4599
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4600
function is_object_in_taxonomy($object_type, $taxonomy) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4601
	$taxonomies = get_object_taxonomies($object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4603
	if ( empty($taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4604
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4605
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4606
	if ( in_array($taxonomy, $taxonomies) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4607
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4608
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4609
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4610
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4611
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4612
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4613
 * Get an array of ancestor IDs for a given object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4614
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4615
 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4616
 * @since 4.1.0 Introduced the `$resource_type` argument.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4617
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4618
 * @param int    $object_id     Optional. The ID of the object. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4619
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4620
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4621
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4622
 *                              or 'taxonomy'. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4623
 * @return array An array of ancestors from lowest to highest in the hierarchy.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4624
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4625
function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4626
	$object_id = (int) $object_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4627
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4628
	$ancestors = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4629
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4630
	if ( empty( $object_id ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4631
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4632
		/** This filter is documented in wp-includes/taxonomy.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4633
		return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4634
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4635
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4636
	if ( ! $resource_type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4637
		if ( is_taxonomy_hierarchical( $object_type ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4638
			$resource_type = 'taxonomy';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4639
		} elseif ( post_type_exists( $object_type ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4640
			$resource_type = 'post_type';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4641
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4642
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4643
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4644
	if ( 'taxonomy' === $resource_type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4645
		$term = get_term($object_id, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4646
		while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4647
			$ancestors[] = (int) $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4648
			$term = get_term($term->parent, $object_type);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4649
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4650
	} elseif ( 'post_type' === $resource_type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4651
		$ancestors = get_post_ancestors($object_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4652
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4653
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4654
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4655
	 * Filter a given object's ancestors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4656
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4657
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4658
	 * @since 4.1.0 Introduced the `$resource_type` parameter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4659
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4660
	 * @param array  $ancestors     An array of object ancestors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4661
	 * @param int    $object_id     Object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4662
	 * @param string $object_type   Type of object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4663
	 * @param string $resource_type Type of resource $object_type is.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4664
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  4665
	return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4666
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4667
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4668
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4669
 * Returns the term's parent's term_ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4670
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4671
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4672
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4673
 * @param int $term_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4674
 * @param string $taxonomy
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4675
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4676
 * @return int|bool false on error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4677
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4678
function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4679
	$term = get_term( $term_id, $taxonomy );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4680
	if ( !$term || is_wp_error( $term ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4681
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4682
	return (int) $term->parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4683
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4684
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4685
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4686
 * Checks the given subset of the term hierarchy for hierarchy loops.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4687
 * Prevents loops from forming and breaks those that it finds.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4688
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4689
 * Attached to the wp_update_term_parent filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4690
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4691
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4692
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4693
 * @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
  4694
 * @param int $term_id The term we're checking.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4695
 * @param string $taxonomy The taxonomy of the term we're checking.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4696
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4697
 * @return int The new parent for the term.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4698
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4699
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4700
	// Nothing fancy here - bail
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4701
	if ( !$parent )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4702
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4703
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4704
	// Can't be its own parent
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4705
	if ( $parent == $term_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4706
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4708
	// Now look for larger loops
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4709
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4710
	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
  4711
		return $parent; // No loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4712
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4713
	// Setting $parent to the given value causes a loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4714
	if ( isset( $loop[$term_id] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4715
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4717
	// 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
  4718
	foreach ( array_keys( $loop ) as $loop_member )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4719
		wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4721
	return $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  4722
}