IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
<+><?php\n/**\n * Taxonomy API\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n */\n\n//\n// Taxonomy Registration\n//\n\n/**\n * Creates the initial taxonomies.\n *\n * This function fires twice: in wp-settings.php before plugins are loaded (for\n * backwards compatibility reasons), and again on the 'init' action. We must avoid\n * registering rewrite rules before the 'init' action.\n */\nfunction create_initial_taxonomies() {\n\tglobal $wp_rewrite;\n\n\tif ( ! did_action( 'init' ) ) {\n\t\t$rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );\n\t} else {\n\t\t$post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );\n\t\t$rewrite = array(\n\t\t\t'category' => array(\n\t\t\t\t'hierarchical' => true,\n\t\t\t\t'slug' => get_option('category_base') ? get_option('category_base') : 'category',\n\t\t\t\t'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),\n\t\t\t\t'ep_mask' => EP_CATEGORIES,\n\t\t\t),\n\t\t\t'post_tag' => array(\n\t\t\t\t'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',\n\t\t\t\t'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),\n\t\t\t\t'ep_mask' => EP_TAGS,\n\t\t\t),\n\t\t\t'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,\n\t\t);\n\t}\n\n\tregister_taxonomy( 'category', 'post', array(\n\t\t'hierarchical' => true,\n\t\t'query_var' => 'category_name',\n\t\t'rewrite' => $rewrite['category'],\n\t\t'public' => true,\n\t\t'show_ui' => true,\n\t\t'_builtin' => true,\n\t) );\n\n\tregister_taxonomy( 'post_tag', 'post', array(\n\t \t'hierarchical' => false,\n\t\t'query_var' => 'tag',\n\t\t'rewrite' => $rewrite['post_tag'],\n\t\t'public' => true,\n\t\t'show_ui' => true,\n\t\t'_builtin' => true,\n\t) );\n\n\tregister_taxonomy( 'nav_menu', 'nav_menu_item', array(\n\t\t'public' => false,\n\t\t'hierarchical' => false,\n\t\t'labels' => array(\n\t\t\t'name' => __( 'Navigation Menus' ),\n\t\t\t'singular_name' => __( 'Navigation Menu' ),\n\t\t),\n\t\t'query_var' => false,\n\t\t'rewrite' => false,\n\t\t'show_ui' => false,\n\t\t'_builtin' => true,\n\t\t'show_in_nav_menus' => false,\n\t) );\n\n\tregister_taxonomy( 'link_category', 'link', array(\n\t\t'hierarchical' => false,\n\t\t'labels' => array(\n\t\t\t'name' => __( 'Link Categories' ),\n\t\t\t'singular_name' => __( 'Link Category' ),\n\t\t\t'search_items' => __( 'Search Link Categories' ),\n\t\t\t'popular_items' => null,\n\t\t\t'all_items' => __( 'All Link Categories' ),\n\t\t\t'edit_item' => __( 'Edit Link Category' ),\n\t\t\t'update_item' => __( 'Update Link Category' ),\n\t\t\t'add_new_item' => __( 'Add New Link Category' ),\n\t\t\t'new_item_name' => __( 'New Link Category Name' ),\n\t\t\t'separate_items_with_commas' => null,\n\t\t\t'add_or_remove_items' => null,\n\t\t\t'choose_from_most_used' => null,\n\t\t),\n\t\t'query_var' => false,\n\t\t'rewrite' => false,\n\t\t'public' => false,\n\t\t'show_ui' => false,\n\t\t'_builtin' => true,\n\t) );\n\n\tregister_taxonomy( 'post_format', 'post', array(\n\t\t'public' => true,\n\t\t'hierarchical' => false,\n\t\t'labels' => array(\n\t\t\t'name' => _x( 'Format', 'post format' ),\n\t\t\t'singular_name' => _x( 'Format', 'post format' ),\n\t\t),\n\t\t'query_var' => true,\n\t\t'rewrite' => $rewrite['post_format'],\n\t\t'show_ui' => false,\n\t\t'_builtin' => true,\n\t\t'show_in_nav_menus' => current_theme_supports( 'post-formats' ),\n\t) );\n}\nadd_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority\n\n/**\n * Get a list of registered taxonomy objects.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 3.0.0\n * @uses $wp_taxonomies\n * @see register_taxonomy\n *\n * @param array $args An array of key => value arguments to match against the taxonomy objects.\n * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.\n * @param string $operator The logical operation to perform. 'or' means only one element\n * from the array needs to match; 'and' means all elements must match. The default is 'and'.\n * @return array A list of taxonomy names or objects\n */\nfunction get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {\n\tglobal $wp_taxonomies;\n\n\t$field = ('names' == $output) ? 'name' : false;\n\n\treturn wp_filter_object_list($wp_taxonomies, $args, $operator, $field);\n}\n\n/**\n * Return all of the taxonomy names that are of $object_type.\n *\n * It appears that this function can be used to find all of the names inside of\n * $wp_taxonomies global variable.\n *\n * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should\n * result in <code>Array('category', 'post_tag')</code>\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wp_taxonomies\n *\n * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)\n * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.\n * @return array The names of all taxonomy of $object_type.\n */\nfunction get_object_taxonomies($object, $output = 'names') {\n\tglobal $wp_taxonomies;\n\n\tif ( is_object($object) ) {\n\t\tif ( $object->post_type == 'attachment' )\n\t\t\treturn get_attachment_taxonomies($object);\n\t\t$object = $object->post_type;\n\t}\n\n\t$object = (array) $object;\n\n\t$taxonomies = array();\n\tforeach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {\n\t\tif ( array_intersect($object, (array) $tax_obj->object_type) ) {\n\t\t\tif ( 'names' == $output )\n\t\t\t\t$taxonomies[] = $tax_name;\n\t\t\telse\n\t\t\t\t$taxonomies[ $tax_name ] = $tax_obj;\n\t\t}\n\t}\n\n\treturn $taxonomies;\n}\n\n/**\n * Retrieves the taxonomy object of $taxonomy.\n *\n * The get_taxonomy function will first check that the parameter string given\n * is a taxonomy object and if it is, it will return it.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wp_taxonomies\n * @uses taxonomy_exists() Checks whether taxonomy exists\n *\n * @param string $taxonomy Name of taxonomy object to return\n * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist\n */\nfunction get_taxonomy( $taxonomy ) {\n\tglobal $wp_taxonomies;\n\n\tif ( ! taxonomy_exists( $taxonomy ) )\n\t\treturn false;\n\n\treturn $wp_taxonomies[$taxonomy];\n}\n\n/**\n * Checks that the taxonomy name exists.\n *\n * Formerly is_taxonomy(), introduced in 2.3.0.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 3.0.0\n *\n * @uses $wp_taxonomies\n *\n * @param string $taxonomy Name of taxonomy object\n * @return bool Whether the taxonomy exists.\n */\nfunction taxonomy_exists( $taxonomy ) {\n\tglobal $wp_taxonomies;\n\n\treturn isset( $wp_taxonomies[$taxonomy] );\n}\n\n/**\n * Whether the taxonomy object is hierarchical.\n *\n * Checks to make sure that the taxonomy is an object first. Then Gets the\n * object, and finally returns the hierarchical value in the object.\n *\n * A false return value might also mean that the taxonomy does not exist.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses taxonomy_exists() Checks whether taxonomy exists\n * @uses get_taxonomy() Used to get the taxonomy object\n *\n * @param string $taxonomy Name of taxonomy object\n * @return bool Whether the taxonomy is hierarchical\n */\nfunction is_taxonomy_hierarchical($taxonomy) {\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn false;\n\n\t$taxonomy = get_taxonomy($taxonomy);\n\treturn $taxonomy->hierarchical;\n}\n\n/**\n * Create or modify a taxonomy object. Do not use before init.\n *\n * A simple function for creating or modifying a taxonomy object based on the\n * parameters given. The function will accept an array (third optional\n * parameter), along with strings for the taxonomy name and another string for\n * the object type.\n *\n * Nothing is returned, so expect error maybe or use taxonomy_exists() to check\n * whether taxonomy exists.\n *\n * Optional $args contents:\n *\n * label - Name of the taxonomy shown in the menu. Usually plural. If not set, labels['name'] will be used.\n *\n * hierarchical - has some defined purpose at other parts of the API and is a\n * boolean value.\n *\n * update_count_callback - works much like a hook, in that it will be called when the count is updated.\n * \tDefaults to _update_post_term_count() for taxonomies attached to post types, which then confirms\n * \tthat the objects are published before counting them.\n * \tDefaults to _update_generic_term_count() for taxonomies attached to other object types, such as links.\n *\n * rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize\n * permastruct; default will use $taxonomy as slug.\n *\n * query_var - false to prevent queries, or string to customize query var\n * (?$query_var=$term); default will use $taxonomy as query var.\n *\n * public - If the taxonomy should be publicly queryable; //@TODO not implemented.\n * defaults to true.\n *\n * show_ui - If the WordPress UI admin tags UI should apply to this taxonomy;\n * defaults to public.\n *\n * show_in_nav_menus - true makes this taxonomy available for selection in navigation menus.\n * Defaults to public.\n *\n * show_tagcloud - false to prevent the taxonomy being listed in the Tag Cloud Widget;\n * defaults to show_ui which defaults to public.\n *\n * labels - An array of labels for this taxonomy. You can see accepted values in {@link get_taxonomy_labels()}. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wp_taxonomies Inserts new taxonomy object into the list\n * @uses $wp Adds query vars\n *\n * @param string $taxonomy Name of taxonomy object\n * @param array|string $object_type Name of the object type for the taxonomy object.\n * @param array|string $args See above description for the two keys values.\n */\nfunction register_taxonomy( $taxonomy, $object_type, $args = array() ) {\n\tglobal $wp_taxonomies, $wp;\n\n\tif ( ! is_array($wp_taxonomies) )\n\t\t$wp_taxonomies = array();\n\n\t$defaults = array(\t'hierarchical' => false,\n\t\t\t\t\t\t'update_count_callback' => '',\n\t\t\t\t\t\t'rewrite' => true,\n\t\t\t\t\t\t'query_var' => $taxonomy,\n\t\t\t\t\t\t'public' => true,\n\t\t\t\t\t\t'show_ui' => null,\n\t\t\t\t\t\t'show_tagcloud' => null,\n\t\t\t\t\t\t'_builtin' => false,\n\t\t\t\t\t\t'labels' => array(),\n\t\t\t\t\t\t'capabilities' => array(),\n\t\t\t\t\t\t'show_in_nav_menus' => null,\n\t\t\t\t\t);\n\t$args = wp_parse_args($args, $defaults);\n\n\tif ( false !== $args['query_var'] && !empty($wp) ) {\n\t\tif ( true === $args['query_var'] )\n\t\t\t$args['query_var'] = $taxonomy;\n\t\t$args['query_var'] = sanitize_title_with_dashes($args['query_var']);\n\t\t$wp->add_query_var($args['query_var']);\n\t}\n\n\tif ( false !== $args['rewrite'] && ( is_admin() || '' != get_option('permalink_structure') ) ) {\n\t\t$args['rewrite'] = wp_parse_args($args['rewrite'], array(\n\t\t\t'slug' => sanitize_title_with_dashes($taxonomy),\n\t\t\t'with_front' => true,\n\t\t\t'hierarchical' => false,\n\t\t\t'ep_mask' => EP_NONE,\n\t\t));\n\n\t\tif ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )\n\t\t\t$tag = '(.+?)';\n\t\telse\n\t\t\t$tag = '([^/]+)';\n\n\t\tadd_rewrite_tag( \"%$taxonomy%\", $tag, $args['query_var'] ? \"{$args['query_var']}=\" : \"taxonomy=$taxonomy&term=\" );\n\t\tadd_permastruct( $taxonomy, \"{$args['rewrite']['slug']}/%$taxonomy%\", $args['rewrite'] );\n\t}\n\n\tif ( is_null($args['show_ui']) )\n\t\t$args['show_ui'] = $args['public'];\n\n\t// Whether to show this type in nav-menus.php. Defaults to the setting for public.\n\tif ( null === $args['show_in_nav_menus'] )\n\t\t$args['show_in_nav_menus'] = $args['public'];\n\n\tif ( is_null($args['show_tagcloud']) )\n\t\t$args['show_tagcloud'] = $args['show_ui'];\n\n\t$default_caps = array(\n\t\t'manage_terms' => 'manage_categories',\n\t\t'edit_terms' => 'manage_categories',\n\t\t'delete_terms' => 'manage_categories',\n\t\t'assign_terms' => 'edit_posts',\n\t);\n\t$args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );\n\tunset( $args['capabilities'] );\n\n\t$args['name'] = $taxonomy;\n\t$args['object_type'] = array_unique( (array)$object_type );\n\n\t$args['labels'] = get_taxonomy_labels( (object) $args );\n\t$args['label'] = $args['labels']->name;\n\n\t$wp_taxonomies[$taxonomy] = (object) $args;\n\n\t// register callback handling for metabox\n \tadd_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term');\n\n\tdo_action( 'registered_taxonomy', $taxonomy, $object_type, $args );\n}\n\n/**\n * Builds an object with all taxonomy labels out of a taxonomy object\n *\n * Accepted keys of the label array in the taxonomy object:\n * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories\n * - singular_name - name for one object of this taxonomy. Default is Tag/Category\n * - search_items - Default is Search Tags/Search Categories\n * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags\n * - all_items - Default is All Tags/All Categories\n * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category\n * - parent_item_colon - The same as <code>parent_item</code>, but with colon <code>:</code> in the end\n * - edit_item - Default is Edit Tag/Edit Category\n * - view_item - Default is View Tag/View Category\n * - update_item - Default is Update Tag/Update Category\n * - add_new_item - Default is Add New Tag/Add New Category\n * - new_item_name - Default is New Tag Name/New Category Name\n * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is \"Separate tags with commas\", used in the meta box.\n * - 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.\n * - 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.\n *\n * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).\n *\n * @since 3.0.0\n * @param object $tax Taxonomy object\n * @return object object with all the labels as member variables\n */\n\nfunction get_taxonomy_labels( $tax ) {\n\tif ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )\n\t\t$tax->labels['separate_items_with_commas'] = $tax->helps;\n\n\t$nohier_vs_hier_defaults = array(\n\t\t'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),\n\t\t'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),\n\t\t'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),\n\t\t'popular_items' => array( __( 'Popular Tags' ), null ),\n\t\t'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),\n\t\t'parent_item' => array( null, __( 'Parent Category' ) ),\n\t\t'parent_item_colon' => array( null, __( 'Parent Category:' ) ),\n\t\t'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),\n\t\t'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),\n\t\t'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),\n\t\t'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),\n\t\t'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),\n\t\t'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),\n\t\t'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),\n\t\t'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),\n\t);\n\t$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];\n\n\treturn _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );\n}\n\n/**\n * Add an already registered taxonomy to an object type.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 3.0.0\n * @uses $wp_taxonomies Modifies taxonomy object\n *\n * @param string $taxonomy Name of taxonomy object\n * @param string $object_type Name of the object type\n * @return bool True if successful, false if not\n */\nfunction register_taxonomy_for_object_type( $taxonomy, $object_type) {\n\tglobal $wp_taxonomies;\n\n\tif ( !isset($wp_taxonomies[$taxonomy]) )\n\t\treturn false;\n\n\tif ( ! get_post_type_object($object_type) )\n\t\treturn false;\n\n\tif ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )\n\t\t$wp_taxonomies[$taxonomy]->object_type[] = $object_type;\n\n\treturn true;\n}\n\n//\n// Term API\n//\n\n/**\n * Retrieve object_ids of valid taxonomy and term.\n *\n * The strings of $taxonomies must exist before this function will continue. On\n * failure of finding a valid taxonomy, it will return an WP_Error class, kind\n * of like Exceptions in PHP 5, except you can't catch them. Even so, you can\n * still test for the WP_Error class and get the error message.\n *\n * The $terms aren't checked the same as $taxonomies, but still need to exist\n * for $object_ids to be returned.\n *\n * It is possible to change the order that object_ids is returned by either\n * using PHP sort family functions or using the database by using $args with\n * either ASC or DESC array. The value should be in the key named 'order'.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses wp_parse_args() Creates an array from string $args.\n *\n * @param int|array $term_ids Term id or array of term ids of terms that will be used\n * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names\n * @param array|string $args Change the order of the object_ids, either ASC or DESC\n * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success\n *\tthe array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.\n */\nfunction get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {\n\tglobal $wpdb;\n\n\tif ( ! is_array( $term_ids ) )\n\t\t$term_ids = array( $term_ids );\n\n\tif ( ! is_array( $taxonomies ) )\n\t\t$taxonomies = array( $taxonomies );\n\n\tforeach ( (array) $taxonomies as $taxonomy ) {\n\t\tif ( ! taxonomy_exists( $taxonomy ) )\n\t\t\treturn new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );\n\t}\n\n\t$defaults = array( 'order' => 'ASC' );\n\t$args = wp_parse_args( $args, $defaults );\n\textract( $args, EXTR_SKIP );\n\n\t$order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';\n\n\t$term_ids = array_map('intval', $term_ids );\n\n\t$taxonomies = \"'\" . implode( \"', '\", $taxonomies ) . \"'\";\n\t$term_ids = \"'\" . implode( \"', '\", $term_ids ) . \"'\";\n\n\t$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\");\n\n\tif ( ! $object_ids )\n\t\treturn array();\n\n\treturn $object_ids;\n}\n\n/**\n * Given a taxonomy query, generates SQL to be appended to a main query.\n *\n * @since 3.1.0\n *\n * @see WP_Tax_Query\n *\n * @param array $tax_query A compact tax query\n * @param string $primary_table\n * @param string $primary_id_column\n * @return array\n */\nfunction get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {\n\t$tax_query_obj = new WP_Tax_Query( $tax_query );\n\treturn $tax_query_obj->get_sql( $primary_table, $primary_id_column );\n}\n\n/**\n * Container class for a multiple taxonomy query.\n *\n * @since 3.1.0\n */\nclass WP_Tax_Query {\n\n\t/**\n\t * List of taxonomy queries. A single taxonomy query is an associative array:\n\t * - 'taxonomy' string The taxonomy being queried\n\t * - 'terms' string|array The list of terms\n\t * - 'field' string (optional) Which term field is being used.\n\t *\t\tPossible values: 'term_id', 'slug' or 'name'\n\t *\t\tDefault: 'term_id'\n\t * - 'operator' string (optional)\n\t *\t\tPossible values: 'AND', 'IN' or 'NOT IN'.\n\t *\t\tDefault: 'IN'\n\t * - 'include_children' bool (optional) Whether to include child terms.\n\t *\t\tDefault: true\n\t *\n\t * @since 3.1.0\n\t * @access public\n\t * @var array\n\t */\n\tpublic $queries = array();\n\n\t/**\n\t * The relation between the queries. Can be one of 'AND' or 'OR'.\n\t *\n\t * @since 3.1.0\n\t * @access public\n\t * @var string\n\t */\n\tpublic $relation;\n\n\t/**\n\t * Standard response when the query should not return any rows.\n\t *\n\t * @since 3.2.0\n\t * @access private\n\t * @var string\n\t */\n\tprivate static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' );\n\n\t/**\n\t * Constructor.\n\t *\n\t * Parses a compact tax query and sets defaults.\n\t *\n\t * @since 3.1.0\n\t * @access public\n\t *\n\t * @param array $tax_query A compact tax query:\n\t * array(\n\t * 'relation' => 'OR',\n\t * array(\n\t * 'taxonomy' => 'tax1',\n\t * 'terms' => array( 'term1', 'term2' ),\n\t * 'field' => 'slug',\n\t * ),\n\t * array(\n\t * 'taxonomy' => 'tax2',\n\t * 'terms' => array( 'term-a', 'term-b' ),\n\t * 'field' => 'slug',\n\t * ),\n\t * )\n\t */\n\tpublic function __construct( $tax_query ) {\n\t\tif ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {\n\t\t\t$this->relation = 'OR';\n\t\t} else {\n\t\t\t$this->relation = 'AND';\n\t\t}\n\n\t\t$defaults = array(\n\t\t\t'taxonomy' => '',\n\t\t\t'terms' => array(),\n\t\t\t'include_children' => true,\n\t\t\t'field' => 'term_id',\n\t\t\t'operator' => 'IN',\n\t\t);\n\n\t\tforeach ( $tax_query as $query ) {\n\t\t\tif ( ! is_array( $query ) )\n\t\t\t\tcontinue;\n\n\t\t\t$query = array_merge( $defaults, $query );\n\n\t\t\t$query['terms'] = (array) $query['terms'];\n\n\t\t\t$this->queries[] = $query;\n\t\t}\n\t}\n\n\t/**\n\t * Generates SQL clauses to be appended to a main query.\n\t *\n\t * @since 3.1.0\n\t * @access public\n\t *\n\t * @param string $primary_table\n\t * @param string $primary_id_column\n\t * @return array\n\t */\n\tpublic function get_sql( $primary_table, $primary_id_column ) {\n\t\tglobal $wpdb;\n\n\t\t$join = '';\n\t\t$where = array();\n\t\t$i = 0;\n\n\t\tforeach ( $this->queries as $query ) {\n\t\t\t$this->clean_query( $query );\n\n\t\t\tif ( is_wp_error( $query ) ) {\n\t\t\t\treturn self::$no_results;\n\t\t\t}\n\n\t\t\textract( $query );\n\n\t\t\tif ( 'IN' == $operator ) {\n\n\t\t\t\tif ( empty( $terms ) ) {\n\t\t\t\t\tif ( 'OR' == $this->relation )\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\telse\n\t\t\t\t\t\treturn self::$no_results;\n\t\t\t\t}\n\n\t\t\t\t$terms = implode( ',', $terms );\n\n\t\t\t\t$alias = $i ? 'tt' . $i : $wpdb->term_relationships;\n\n\t\t\t\t$join .= \" INNER JOIN $wpdb->term_relationships\";\n\t\t\t\t$join .= $i ? \" AS $alias\" : '';\n\t\t\t\t$join .= \" ON ($primary_table.$primary_id_column = $alias.object_id)\";\n\n\t\t\t\t$where[] = \"$alias.term_taxonomy_id $operator ($terms)\";\n\t\t\t} elseif ( 'NOT IN' == $operator ) {\n\n\t\t\t\tif ( empty( $terms ) )\n\t\t\t\t\tcontinue;\n\n\t\t\t\t$terms = implode( ',', $terms );\n\n\t\t\t\t$where[] = \"$primary_table.$primary_id_column NOT IN (\n\t\t\t\t\tSELECT object_id\n\t\t\t\t\tFROM $wpdb->term_relationships\n\t\t\t\t\tWHERE term_taxonomy_id IN ($terms)\n\t\t\t\t)\";\n\t\t\t} elseif ( 'AND' == $operator ) {\n\n\t\t\t\tif ( empty( $terms ) )\n\t\t\t\t\tcontinue;\n\n\t\t\t\t$num_terms = count( $terms );\n\n\t\t\t\t$terms = implode( ',', $terms );\n\n\t\t\t\t$where[] = \"(\n\t\t\t\t\tSELECT COUNT(1)\n\t\t\t\t\tFROM $wpdb->term_relationships\n\t\t\t\t\tWHERE term_taxonomy_id IN ($terms)\n\t\t\t\t\tAND object_id = $primary_table.$primary_id_column\n\t\t\t\t) = $num_terms\";\n\t\t\t}\n\n\t\t\t$i++;\n\t\t}\n\n\t\tif ( !empty( $where ) )\n\t\t\t$where = ' AND ( ' . implode( \" $this->relation \", $where ) . ' )';\n\t\telse\n\t\t\t$where = '';\n\n\t\treturn compact( 'join', 'where' );\n\t}\n\n\t/**\n\t * Validates a single query.\n\t *\n\t * @since 3.2.0\n\t * @access private\n\t *\n\t * @param array &$query The single query\n\t */\n\tprivate function clean_query( &$query ) {\n\t\tif ( ! taxonomy_exists( $query['taxonomy'] ) ) {\n\t\t\t$query = new WP_Error( 'Invalid taxonomy' );\n\t\t\treturn;\n\t\t}\n\n\t\t$query['terms'] = array_unique( (array) $query['terms'] );\n\n\t\tif ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {\n\t\t\t$this->transform_query( $query, 'term_id' );\n\n\t\t\tif ( is_wp_error( $query ) )\n\t\t\t\treturn;\n\n\t\t\t$children = array();\n\t\t\tforeach ( $query['terms'] as $term ) {\n\t\t\t\t$children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );\n\t\t\t\t$children[] = $term;\n\t\t\t}\n\t\t\t$query['terms'] = $children;\n\t\t}\n\n\t\t$this->transform_query( $query, 'term_taxonomy_id' );\n\t}\n\n\t/**\n\t * Transforms a single query, from one field to another.\n\t *\n\t * @since 3.2.0\n\t * @access private\n\t *\n\t * @param array &$query The single query\n\t * @param string $resulting_field The resulting field\n\t */\n\tprivate function transform_query( &$query, $resulting_field ) {\n\t\tglobal $wpdb;\n\n\t\tif ( empty( $query['terms'] ) )\n\t\t\treturn;\n\n\t\tif ( $query['field'] == $resulting_field )\n\t\t\treturn;\n\n\t\t$resulting_field = esc_sql( $resulting_field );\n\n\t\tswitch ( $query['field'] ) {\n\t\t\tcase 'slug':\n\t\t\tcase 'name':\n\t\t\t\t$terms = \"'\" . implode( \"','\", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . \"'\";\n\t\t\t\t$terms = $wpdb->get_col( \"\n\t\t\t\t\tSELECT $wpdb->term_taxonomy.$resulting_field\n\t\t\t\t\tFROM $wpdb->term_taxonomy\n\t\t\t\t\tINNER JOIN $wpdb->terms USING (term_id)\n\t\t\t\t\tWHERE taxonomy = '{$query['taxonomy']}'\n\t\t\t\t\tAND $wpdb->terms.{$query['field']} IN ($terms)\n\t\t\t\t\" );\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\t$terms = implode( ',', array_map( 'intval', $query['terms'] ) );\n\t\t\t\t$terms = $wpdb->get_col( \"\n\t\t\t\t\tSELECT $resulting_field\n\t\t\t\t\tFROM $wpdb->term_taxonomy\n\t\t\t\t\tWHERE taxonomy = '{$query['taxonomy']}'\n\t\t\t\t\tAND term_id IN ($terms)\n\t\t\t\t\" );\n\t\t}\n\n\t\tif ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {\n\t\t\t$query = new WP_Error( 'Inexistent terms' );\n\t\t\treturn;\n\t\t}\n\n\t\t$query['terms'] = $terms;\n\t\t$query['field'] = $resulting_field;\n\t}\n}\n\n/**\n * Get all Term data from database by Term ID.\n *\n * The usage of the get_term function is to apply filters to a term object. It\n * is possible to get a term object from the database before applying the\n * filters.\n *\n * $term ID must be part of $taxonomy, to get from the database. Failure, might\n * be able to be captured by the hooks. Failure would be the same value as $wpdb\n * returns for the get_row method.\n *\n * There are two hooks, one is specifically for each term, named 'get_term', and\n * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the\n * term object, and the taxonomy name as parameters. Both hooks are expected to\n * return a Term object.\n *\n * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.\n * Must return term object. Used in get_term() as a catch-all filter for every\n * $term.\n *\n * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy\n * name. Must return term object. $taxonomy will be the taxonomy name, so for\n * example, if 'category', it would be 'get_category' as the filter name. Useful\n * for custom taxonomies or plugging into default taxonomies.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses sanitize_term() Cleanses the term based on $filter context before returning.\n * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.\n *\n * @param int|object $term If integer, will get from database. If object will apply filters and return $term.\n * @param string $taxonomy Taxonomy name that $term is part of.\n * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N\n * @param string $filter Optional, default is raw or no WordPress defined filter will applied.\n * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not\n * exist then WP_Error will be returned.\n */\nfunction &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {\n\tglobal $wpdb;\n\t$null = null;\n\n\tif ( empty($term) ) {\n\t\t$error = new WP_Error('invalid_term', __('Empty Term'));\n\t\treturn $error;\n\t}\n\n\tif ( ! taxonomy_exists($taxonomy) ) {\n\t\t$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\t\treturn $error;\n\t}\n\n\tif ( is_object($term) && empty($term->filter) ) {\n\t\twp_cache_add($term->term_id, $term, $taxonomy);\n\t\t$_term = $term;\n\t} else {\n\t\tif ( is_object($term) )\n\t\t\t$term = $term->term_id;\n\t\tif ( !$term = (int) $term )\n\t\t\treturn $null;\n\t\tif ( ! $_term = wp_cache_get($term, $taxonomy) ) {\n\t\t\t$_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) );\n\t\t\tif ( ! $_term )\n\t\t\t\treturn $null;\n\t\t\twp_cache_add($term, $_term, $taxonomy);\n\t\t}\n\t}\n\n\t$_term = apply_filters('get_term', $_term, $taxonomy);\n\t$_term = apply_filters(\"get_$taxonomy\", $_term, $taxonomy);\n\t$_term = sanitize_term($_term, $taxonomy, $filter);\n\n\tif ( $output == OBJECT ) {\n\t\treturn $_term;\n\t} elseif ( $output == ARRAY_A ) {\n\t\t$__term = get_object_vars($_term);\n\t\treturn $__term;\n\t} elseif ( $output == ARRAY_N ) {\n\t\t$__term = array_values(get_object_vars($_term));\n\t\treturn $__term;\n\t} else {\n\t\treturn $_term;\n\t}\n}\n\n/**\n * Get all Term data from database by Term field and data.\n *\n * Warning: $value is not escaped for 'name' $field. You must do it yourself, if\n * required.\n *\n * The default $field is 'id', therefore it is possible to also use null for\n * field, but not recommended that you do so.\n *\n * If $value does not exist, the return value will be false. If $taxonomy exists\n * and $field and $value combinations exist, the Term will be returned.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses sanitize_term() Cleanses the term based on $filter context before returning.\n * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.\n *\n * @param string $field Either 'slug', 'name', or 'id'\n * @param string|int $value Search for this term value\n * @param string $taxonomy Taxonomy Name\n * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N\n * @param string $filter Optional, default is raw or no WordPress defined filter will applied.\n * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.\n */\nfunction get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {\n\tglobal $wpdb;\n\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn false;\n\n\tif ( 'slug' == $field ) {\n\t\t$field = 't.slug';\n\t\t$value = sanitize_title($value);\n\t\tif ( empty($value) )\n\t\t\treturn false;\n\t} else if ( 'name' == $field ) {\n\t\t// Assume already escaped\n\t\t$value = stripslashes($value);\n\t\t$field = 't.name';\n\t} else {\n\t\t$term = get_term( (int) $value, $taxonomy, $output, $filter);\n\t\tif ( is_wp_error( $term ) )\n\t\t\t$term = false;\n\t\treturn $term;\n\t}\n\n\t$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) );\n\tif ( !$term )\n\t\treturn false;\n\n\twp_cache_add($term->term_id, $term, $taxonomy);\n\n\t$term = apply_filters('get_term', $term, $taxonomy);\n\t$term = apply_filters(\"get_$taxonomy\", $term, $taxonomy);\n\t$term = sanitize_term($term, $taxonomy, $filter);\n\n\tif ( $output == OBJECT ) {\n\t\treturn $term;\n\t} elseif ( $output == ARRAY_A ) {\n\t\treturn get_object_vars($term);\n\t} elseif ( $output == ARRAY_N ) {\n\t\treturn array_values(get_object_vars($term));\n\t} else {\n\t\treturn $term;\n\t}\n}\n\n/**\n * Merge all term children into a single array of their IDs.\n *\n * This recursive function will merge all of the children of $term into the same\n * array of term IDs. Only useful for taxonomies which are hierarchical.\n *\n * Will return an empty array if $term does not exist in $taxonomy.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses _get_term_hierarchy()\n * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term\n *\n * @param string $term_id ID of Term to get children\n * @param string $taxonomy Taxonomy Name\n * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist\n */\nfunction get_term_children( $term_id, $taxonomy ) {\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\n\t$term_id = intval( $term_id );\n\n\t$terms = _get_term_hierarchy($taxonomy);\n\n\tif ( ! isset($terms[$term_id]) )\n\t\treturn array();\n\n\t$children = $terms[$term_id];\n\n\tforeach ( (array) $terms[$term_id] as $child ) {\n\t\tif ( isset($terms[$child]) )\n\t\t\t$children = array_merge($children, get_term_children($child, $taxonomy));\n\t}\n\n\treturn $children;\n}\n\n/**\n * Get sanitized Term field.\n *\n * Does checks for $term, based on the $taxonomy. The function is for contextual\n * reasons and for simplicity of usage. See sanitize_term_field() for more\n * information.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.\n *\n * @param string $field Term field to fetch\n * @param int $term Term ID\n * @param string $taxonomy Taxonomy Name\n * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.\n * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.\n */\nfunction get_term_field( $field, $term, $taxonomy, $context = 'display' ) {\n\t$term = (int) $term;\n\t$term = get_term( $term, $taxonomy );\n\tif ( is_wp_error($term) )\n\t\treturn $term;\n\n\tif ( !is_object($term) )\n\t\treturn '';\n\n\tif ( !isset($term->$field) )\n\t\treturn '';\n\n\treturn sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);\n}\n\n/**\n * Sanitizes Term for editing.\n *\n * Return value is sanitize_term() and usage is for sanitizing the term for\n * editing. Function is for contextual and simplicity.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses sanitize_term() Passes the return value on success\n *\n * @param int|object $id Term ID or Object\n * @param string $taxonomy Taxonomy Name\n * @return mixed|null|WP_Error Will return empty string if $term is not an object.\n */\nfunction get_term_to_edit( $id, $taxonomy ) {\n\t$term = get_term( $id, $taxonomy );\n\n\tif ( is_wp_error($term) )\n\t\treturn $term;\n\n\tif ( !is_object($term) )\n\t\treturn '';\n\n\treturn sanitize_term($term, $taxonomy, 'edit');\n}\n\n/**\n * Retrieve the terms in a given taxonomy or list of taxonomies.\n *\n * You can fully inject any customizations to the query before it is sent, as\n * well as control the output with a filter.\n *\n * The 'get_terms' filter will be called when the cache has the term and will\n * pass the found term along with the array of $taxonomies and array of $args.\n * This filter is also called before the array of terms is passed and will pass\n * the array of terms, along with the $taxonomies and $args.\n *\n * The 'list_terms_exclusions' filter passes the compiled exclusions along with\n * the $args.\n *\n * The 'get_terms_orderby' filter passes the ORDER BY clause for the query\n * along with the $args array.\n *\n * The 'get_terms_fields' filter passes the fields for the SELECT query\n * along with the $args array.\n *\n * The list of arguments that $args can contain, which will overwrite the defaults:\n *\n * orderby - Default is 'name'. Can be name, count, term_group, slug or nothing\n * (will use term_id), Passing a custom value other than these will cause it to\n * order based on the custom value.\n *\n * order - Default is ASC. Can use DESC.\n *\n * hide_empty - Default is true. Will not return empty terms, which means\n * terms whose count is 0 according to the given taxonomy.\n *\n * exclude - Default is an empty array. An array, comma- or space-delimited string\n * of term ids to exclude from the return array. If 'include' is non-empty,\n * 'exclude' is ignored.\n *\n * exclude_tree - Default is an empty array. An array, comma- or space-delimited\n * string of term ids to exclude from the return array, along with all of their\n * descendant terms according to the primary taxonomy. If 'include' is non-empty,\n * 'exclude_tree' is ignored.\n *\n * include - Default is an empty array. An array, comma- or space-delimited string\n * of term ids to include in the return array.\n *\n * number - The maximum number of terms to return. Default is to return them all.\n *\n * offset - The number by which to offset the terms query.\n *\n * fields - Default is 'all', which returns an array of term objects.\n * If 'fields' is 'ids' or 'names', returns an array of\n * integers or strings, respectively.\n *\n * slug - Returns terms whose \"slug\" matches this value. Default is empty string.\n *\n * hierarchical - Whether to include terms that have non-empty descendants\n * (even if 'hide_empty' is set to true).\n *\n * search - Returned terms' names will contain the value of 'search',\n * case-insensitive. Default is an empty string.\n *\n * name__like - Returned terms' names will begin with the value of 'name__like',\n * case-insensitive. Default is empty string.\n *\n * The argument 'pad_counts', if set to true will include the quantity of a term's\n * children in the quantity of each term's \"count\" object variable.\n *\n * The 'get' argument, if set to 'all' instead of its default empty string,\n * returns terms regardless of ancestry or whether the terms are empty.\n *\n * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default\n * is 0. If set to a non-zero value, all returned terms will be descendants\n * of that term according to the given taxonomy. Hence 'child_of' is set to 0\n * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies\n * make term ancestry ambiguous.\n *\n * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is\n * the empty string '', which has a different meaning from the integer 0.\n * If set to an integer value, all returned terms will have as an immediate\n * ancestor the term whose ID is specified by that integer according to the given taxonomy.\n * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'\n * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.\n *\n * The 'cache_domain' argument enables a unique cache key to be produced when this query is stored\n * in object cache. For instance, if you are using one of this function's filters to modify the\n * query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite\n * the cache for similar queries. Default value is 'core'.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.\n *\n * @param string|array $taxonomies Taxonomy name or list of Taxonomy names\n * @param string|array $args The values of what to search for when returning terms\n * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.\n */\nfunction &get_terms($taxonomies, $args = '') {\n\tglobal $wpdb;\n\t$empty_array = array();\n\n\t$single_taxonomy = false;\n\tif ( !is_array($taxonomies) ) {\n\t\t$single_taxonomy = true;\n\t\t$taxonomies = array($taxonomies);\n\t}\n\n\tforeach ( $taxonomies as $taxonomy ) {\n\t\tif ( ! taxonomy_exists($taxonomy) ) {\n\t\t\t$error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\t\t\treturn $error;\n\t\t}\n\t}\n\n\t$defaults = array('orderby' => 'name', 'order' => 'ASC',\n\t\t'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),\n\t\t'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',\n\t\t'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',\n\t\t'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );\n\t$args = wp_parse_args( $args, $defaults );\n\t$args['number'] = absint( $args['number'] );\n\t$args['offset'] = absint( $args['offset'] );\n\tif ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||\n\t\t'' !== $args['parent'] ) {\n\t\t$args['child_of'] = 0;\n\t\t$args['hierarchical'] = false;\n\t\t$args['pad_counts'] = false;\n\t}\n\n\tif ( 'all' == $args['get'] ) {\n\t\t$args['child_of'] = 0;\n\t\t$args['hide_empty'] = 0;\n\t\t$args['hierarchical'] = false;\n\t\t$args['pad_counts'] = false;\n\t}\n\n\t$args = apply_filters( 'get_terms_args', $args, $taxonomies );\n\n\textract($args, EXTR_SKIP);\n\n\tif ( $child_of ) {\n\t\t$hierarchy = _get_term_hierarchy($taxonomies[0]);\n\t\tif ( !isset($hierarchy[$child_of]) )\n\t\t\treturn $empty_array;\n\t}\n\n\tif ( $parent ) {\n\t\t$hierarchy = _get_term_hierarchy($taxonomies[0]);\n\t\tif ( !isset($hierarchy[$parent]) )\n\t\t\treturn $empty_array;\n\t}\n\n\t// $args can be whatever, only use the args defined in defaults to compute the key\n\t$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';\n\t$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );\n\t$last_changed = wp_cache_get('last_changed', 'terms');\n\tif ( !$last_changed ) {\n\t\t$last_changed = time();\n\t\twp_cache_set('last_changed', $last_changed, 'terms');\n\t}\n\t$cache_key = \"get_terms:$key:$last_changed\";\n\t$cache = wp_cache_get( $cache_key, 'terms' );\n\tif ( false !== $cache ) {\n\t\t$cache = apply_filters('get_terms', $cache, $taxonomies, $args);\n\t\treturn $cache;\n\t}\n\n\t$_orderby = strtolower($orderby);\n\tif ( 'count' == $_orderby )\n\t\t$orderby = 'tt.count';\n\telse if ( 'name' == $_orderby )\n\t\t$orderby = 't.name';\n\telse if ( 'slug' == $_orderby )\n\t\t$orderby = 't.slug';\n\telse if ( 'term_group' == $_orderby )\n\t\t$orderby = 't.term_group';\n\telse if ( 'none' == $_orderby )\n\t\t$orderby = '';\n\telseif ( empty($_orderby) || 'id' == $_orderby )\n\t\t$orderby = 't.term_id';\n\telse\n\t\t$orderby = 't.name';\n\n\t$orderby = apply_filters( 'get_terms_orderby', $orderby, $args );\n\n\tif ( !empty($orderby) )\n\t\t$orderby = \"ORDER BY $orderby\";\n\telse\n\t\t$order = '';\n\n\t$order = strtoupper( $order );\n\tif ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )\n\t\t$order = 'ASC';\n\n\t$where = \"tt.taxonomy IN ('\" . implode(\"', '\", $taxonomies) . \"')\";\n\t$inclusions = '';\n\tif ( !empty($include) ) {\n\t\t$exclude = '';\n\t\t$exclude_tree = '';\n\t\t$interms = wp_parse_id_list($include);\n\t\tforeach ( $interms as $interm ) {\n\t\t\tif ( empty($inclusions) )\n\t\t\t\t$inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';\n\t\t\telse\n\t\t\t\t$inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';\n\t\t}\n\t}\n\n\tif ( !empty($inclusions) )\n\t\t$inclusions .= ')';\n\t$where .= $inclusions;\n\n\t$exclusions = '';\n\tif ( !empty( $exclude_tree ) ) {\n\t\t$excluded_trunks = wp_parse_id_list($exclude_tree);\n\t\tforeach ( $excluded_trunks as $extrunk ) {\n\t\t\t$excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0));\n\t\t\t$excluded_children[] = $extrunk;\n\t\t\tforeach( $excluded_children as $exterm ) {\n\t\t\t\tif ( empty($exclusions) )\n\t\t\t\t\t$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';\n\t\t\t\telse\n\t\t\t\t\t$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( !empty($exclude) ) {\n\t\t$exterms = wp_parse_id_list($exclude);\n\t\tforeach ( $exterms as $exterm ) {\n\t\t\tif ( empty($exclusions) )\n\t\t\t\t$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';\n\t\t\telse\n\t\t\t\t$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';\n\t\t}\n\t}\n\n\tif ( !empty($exclusions) )\n\t\t$exclusions .= ')';\n\t$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );\n\t$where .= $exclusions;\n\n\tif ( !empty($slug) ) {\n\t\t$slug = sanitize_title($slug);\n\t\t$where .= \" AND t.slug = '$slug'\";\n\t}\n\n\tif ( !empty($name__like) ) {\n\t\t$name__like = like_escape( $name__like );\n\t\t$where .= $wpdb->prepare( \" AND t.name LIKE %s\", $name__like . '%' );\n\t}\n\n\tif ( '' !== $parent ) {\n\t\t$parent = (int) $parent;\n\t\t$where .= \" AND tt.parent = '$parent'\";\n\t}\n\n\tif ( $hide_empty && !$hierarchical )\n\t\t$where .= ' AND tt.count > 0';\n\n\t// don't limit the query results when we have to descend the family tree\n\tif ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {\n\t\tif ( $offset )\n\t\t\t$limits = 'LIMIT ' . $offset . ',' . $number;\n\t\telse\n\t\t\t$limits = 'LIMIT ' . $number;\n\t} else {\n\t\t$limits = '';\n\t}\n\n\tif ( !empty($search) ) {\n\t\t$search = like_escape($search);\n\t\t$where .= $wpdb->prepare( \" AND (t.name LIKE %s)\", '%' . $search . '%');\n\t}\n\n\t$selects = array();\n\tswitch ( $fields ) {\n\t\tcase 'all':\n\t\t\t$selects = array('t.*', 'tt.*');\n\t\t\tbreak;\n\t\tcase 'ids':\n\t\tcase 'id=>parent':\n\t\t\t$selects = array('t.term_id', 'tt.parent', 'tt.count');\n\t\t\tbreak;\n\t\tcase 'names':\n\t\t\t$selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');\n\t\t\tbreak;\n\t\tcase 'count':\n\t\t\t$orderby = '';\n\t\t\t$order = '';\n\t\t\t$selects = array('COUNT(*)');\n\t}\n\n\t$_fields = $fields;\n\n\t$fields = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));\n\n\t$join = \"INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id\";\n\n\t$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );\n\t$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );\n\tforeach ( $pieces as $piece )\n\t\t$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';\n\n\t$query = \"SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits\";\n\n\t$fields = $_fields;\n\n\tif ( 'count' == $fields ) {\n\t\t$term_count = $wpdb->get_var($query);\n\t\treturn $term_count;\n\t}\n\n\t$terms = $wpdb->get_results($query);\n\tif ( 'all' == $fields ) {\n\t\tupdate_term_cache($terms);\n\t}\n\n\tif ( empty($terms) ) {\n\t\twp_cache_add( $cache_key, array(), 'terms', 86400 ); // one day\n\t\t$terms = apply_filters('get_terms', array(), $taxonomies, $args);\n\t\treturn $terms;\n\t}\n\n\tif ( $child_of ) {\n\t\t$children = _get_term_hierarchy($taxonomies[0]);\n\t\tif ( ! empty($children) )\n\t\t\t$terms = & _get_term_children($child_of, $terms, $taxonomies[0]);\n\t}\n\n\t// Update term counts to include children.\n\tif ( $pad_counts && 'all' == $fields )\n\t\t_pad_term_counts($terms, $taxonomies[0]);\n\n\t// Make sure we show empty categories that have children.\n\tif ( $hierarchical && $hide_empty && is_array($terms) ) {\n\t\tforeach ( $terms as $k => $term ) {\n\t\t\tif ( ! $term->count ) {\n\t\t\t\t$children = _get_term_children($term->term_id, $terms, $taxonomies[0]);\n\t\t\t\tif ( is_array($children) )\n\t\t\t\t\tforeach ( $children as $child )\n\t\t\t\t\t\tif ( $child->count )\n\t\t\t\t\t\t\tcontinue 2;\n\n\t\t\t\t// It really is empty\n\t\t\t\tunset($terms[$k]);\n\t\t\t}\n\t\t}\n\t}\n\treset ( $terms );\n\n\t$_terms = array();\n\tif ( 'id=>parent' == $fields ) {\n\t\twhile ( $term = array_shift($terms) )\n\t\t\t$_terms[$term->term_id] = $term->parent;\n\t\t$terms = $_terms;\n\t} elseif ( 'ids' == $fields ) {\n\t\twhile ( $term = array_shift($terms) )\n\t\t\t$_terms[] = $term->term_id;\n\t\t$terms = $_terms;\n\t} elseif ( 'names' == $fields ) {\n\t\twhile ( $term = array_shift($terms) )\n\t\t\t$_terms[] = $term->name;\n\t\t$terms = $_terms;\n\t}\n\n\tif ( 0 < $number && intval(@count($terms)) > $number ) {\n\t\t$terms = array_slice($terms, $offset, $number);\n\t}\n\n\twp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day\n\n\t$terms = apply_filters('get_terms', $terms, $taxonomies, $args);\n\treturn $terms;\n}\n\n/**\n * Check if Term exists.\n *\n * Formerly is_term(), introduced in 2.3.0.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 3.0.0\n *\n * @uses $wpdb\n *\n * @param int|string $term The term to check\n * @param string $taxonomy The taxonomy name to use\n * @param int $parent ID of parent term under which to confine the exists search.\n * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified\n * \tand the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists.\n */\nfunction term_exists($term, $taxonomy = '', $parent = 0) {\n\tglobal $wpdb;\n\n\t$select = \"SELECT term_id FROM $wpdb->terms as t WHERE \";\n\t$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 \";\n\n\tif ( is_int($term) ) {\n\t\tif ( 0 == $term )\n\t\t\treturn 0;\n\t\t$where = 't.term_id = %d';\n\t\tif ( !empty($taxonomy) )\n\t\t\treturn $wpdb->get_row( $wpdb->prepare( $tax_select . $where . \" AND tt.taxonomy = %s\", $term, $taxonomy ), ARRAY_A );\n\t\telse\n\t\t\treturn $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );\n\t}\n\n\t$term = trim( stripslashes( $term ) );\n\n\tif ( '' === $slug = sanitize_title($term) )\n\t\treturn 0;\n\n\t$where = 't.slug = %s';\n\t$else_where = 't.name = %s';\n\t$where_fields = array($slug);\n\t$else_where_fields = array($term);\n\tif ( !empty($taxonomy) ) {\n\t\t$parent = (int) $parent;\n\t\tif ( $parent > 0 ) {\n\t\t\t$where_fields[] = $parent;\n\t\t\t$else_where_fields[] = $parent;\n\t\t\t$where .= ' AND tt.parent = %d';\n\t\t\t$else_where .= ' AND tt.parent = %d';\n\t\t}\n\n\t\t$where_fields[] = $taxonomy;\n\t\t$else_where_fields[] = $taxonomy;\n\n\t\tif ( $result = $wpdb->get_row( $wpdb->prepare(\"SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s\", $where_fields), ARRAY_A) )\n\t\t\treturn $result;\n\n\t\treturn $wpdb->get_row( $wpdb->prepare(\"SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s\", $else_where_fields), ARRAY_A);\n\t}\n\n\tif ( $result = $wpdb->get_var( $wpdb->prepare(\"SELECT term_id FROM $wpdb->terms as t WHERE $where\", $where_fields) ) )\n\t\treturn $result;\n\n\treturn $wpdb->get_var( $wpdb->prepare(\"SELECT term_id FROM $wpdb->terms as t WHERE $else_where\", $else_where_fields) );\n}\n\n/**\n * Check if a term is an ancestor of another term.\n *\n * You can use either an id or the term object for both parameters.\n *\n * @since 3.4.0\n *\n * @param int|object $term1 ID or object to check if this is the parent term.\n * @param int|object $term2 The child term.\n * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to.\n * @return bool Whether $term2 is child of $term1\n */\nfunction term_is_ancestor_of( $term1, $term2, $taxonomy ) {\n\tif ( ! isset( $term1->term_id ) )\n\t\t$term1 = get_term( $term1, $taxonomy );\n\tif ( ! isset( $term2->parent ) )\n\t\t$term2 = get_term( $term2, $taxonomy );\n\n\tif ( empty( $term1->term_id ) || empty( $term2->parent ) )\n\t\treturn false;\n\tif ( $term2->parent == $term1->term_id )\n\t\treturn true;\n\n\treturn term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );\n}\n\n/**\n * Sanitize Term all fields.\n *\n * Relies on sanitize_term_field() to sanitize the term. The difference is that\n * this function will sanitize <strong>all</strong> fields. The context is based\n * on sanitize_term_field().\n *\n * The $term is expected to be either an array or an object.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses sanitize_term_field Used to sanitize all fields in a term\n *\n * @param array|object $term The term to check\n * @param string $taxonomy The taxonomy name to use\n * @param string $context Default is 'display'.\n * @return array|object Term with all fields sanitized\n */\nfunction sanitize_term($term, $taxonomy, $context = 'display') {\n\n\tif ( 'raw' == $context )\n\t\treturn $term;\n\n\t$fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');\n\n\t$do_object = false;\n\tif ( is_object($term) )\n\t\t$do_object = true;\n\n\t$term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);\n\n\tforeach ( (array) $fields as $field ) {\n\t\tif ( $do_object ) {\n\t\t\tif ( isset($term->$field) )\n\t\t\t\t$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);\n\t\t} else {\n\t\t\tif ( isset($term[$field]) )\n\t\t\t\t$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);\n\t\t}\n\t}\n\n\tif ( $do_object )\n\t\t$term->filter = $context;\n\telse\n\t\t$term['filter'] = $context;\n\n\treturn $term;\n}\n\n/**\n * Cleanse the field value in the term based on the context.\n *\n * Passing a term field value through the function should be assumed to have\n * cleansed the value for whatever context the term field is going to be used.\n *\n * If no context or an unsupported context is given, then default filters will\n * be applied.\n *\n * There are enough filters for each context to support a custom filtering\n * without creating your own filter function. Simply create a function that\n * hooks into the filter you need.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n *\n * @param string $field Term field to sanitize\n * @param string $value Search for this term value\n * @param int $term_id Term ID\n * @param string $taxonomy Taxonomy Name\n * @param string $context Either edit, db, display, attribute, or js.\n * @return mixed sanitized field\n */\nfunction sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {\n\tif ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {\n\t\t$value = (int) $value;\n\t\tif ( $value < 0 )\n\t\t\t$value = 0;\n\t}\n\n\tif ( 'raw' == $context )\n\t\treturn $value;\n\n\tif ( 'edit' == $context ) {\n\t\t$value = apply_filters(\"edit_term_{$field}\", $value, $term_id, $taxonomy);\n\t\t$value = apply_filters(\"edit_{$taxonomy}_{$field}\", $value, $term_id);\n\t\tif ( 'description' == $field )\n\t\t\t$value = esc_html($value); // textarea_escaped\n\t\telse\n\t\t\t$value = esc_attr($value);\n\t} else if ( 'db' == $context ) {\n\t\t$value = apply_filters(\"pre_term_{$field}\", $value, $taxonomy);\n\t\t$value = apply_filters(\"pre_{$taxonomy}_{$field}\", $value);\n\t\t// Back compat filters\n\t\tif ( 'slug' == $field )\n\t\t\t$value = apply_filters('pre_category_nicename', $value);\n\n\t} else if ( 'rss' == $context ) {\n\t\t$value = apply_filters(\"term_{$field}_rss\", $value, $taxonomy);\n\t\t$value = apply_filters(\"{$taxonomy}_{$field}_rss\", $value);\n\t} else {\n\t\t// Use display filters by default.\n\t\t$value = apply_filters(\"term_{$field}\", $value, $term_id, $taxonomy, $context);\n\t\t$value = apply_filters(\"{$taxonomy}_{$field}\", $value, $term_id, $context);\n\t}\n\n\tif ( 'attribute' == $context )\n\t\t$value = esc_attr($value);\n\telse if ( 'js' == $context )\n\t\t$value = esc_js($value);\n\n\treturn $value;\n}\n\n/**\n * Count how many terms are in Taxonomy.\n *\n * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses get_terms()\n * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.\n *\n * @param string $taxonomy Taxonomy name\n * @param array|string $args Overwrite defaults. See get_terms()\n * @return int How many terms are in $taxonomy\n */\nfunction wp_count_terms( $taxonomy, $args = array() ) {\n\t$defaults = array('hide_empty' => false);\n\t$args = wp_parse_args($args, $defaults);\n\n\t// backwards compatibility\n\tif ( isset($args['ignore_empty']) ) {\n\t\t$args['hide_empty'] = $args['ignore_empty'];\n\t\tunset($args['ignore_empty']);\n\t}\n\n\t$args['fields'] = 'count';\n\n\treturn get_terms($taxonomy, $args);\n}\n\n/**\n * Will unlink the object from the taxonomy or taxonomies.\n *\n * Will remove all relationships between the object and any terms in\n * a particular taxonomy or taxonomies. Does not remove the term or\n * taxonomy itself.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param int $object_id The term Object Id that refers to the term\n * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.\n */\nfunction wp_delete_object_term_relationships( $object_id, $taxonomies ) {\n\tglobal $wpdb;\n\n\t$object_id = (int) $object_id;\n\n\tif ( !is_array($taxonomies) )\n\t\t$taxonomies = array($taxonomies);\n\n\tforeach ( (array) $taxonomies as $taxonomy ) {\n\t\t$tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));\n\t\t$in_tt_ids = \"'\" . implode(\"', '\", $tt_ids) . \"'\";\n\t\tdo_action( 'delete_term_relationships', $object_id, $tt_ids );\n\t\t$wpdb->query( $wpdb->prepare(\"DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)\", $object_id) );\n\t\tdo_action( 'deleted_term_relationships', $object_id, $tt_ids );\n\t\twp_update_term_count($tt_ids, $taxonomy);\n\t}\n}\n\n/**\n * Removes a term from the database.\n *\n * If the term is a parent of other terms, then the children will be updated to\n * that term's parent.\n *\n * The $args 'default' will only override the terms found, if there is only one\n * term found. Any other and the found terms are used.\n *\n * The $args 'force_default' will force the term supplied as default to be\n * assigned even if the object was not going to be termless\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action\n *\thooks, passing term object, term id. 'delete_term' gets an additional\n *\tparameter with the $taxonomy parameter.\n *\n * @param int $term Term ID\n * @param string $taxonomy Taxonomy Name\n * @param array|string $args Optional. Change 'default' term id and override found term ids.\n * @return bool|WP_Error Returns false if not term; true if completes delete action.\n */\nfunction wp_delete_term( $term, $taxonomy, $args = array() ) {\n\tglobal $wpdb;\n\n\t$term = (int) $term;\n\n\tif ( ! $ids = term_exists($term, $taxonomy) )\n\t\treturn false;\n\tif ( is_wp_error( $ids ) )\n\t\treturn $ids;\n\n\t$tt_id = $ids['term_taxonomy_id'];\n\n\t$defaults = array();\n\n\tif ( 'category' == $taxonomy ) {\n\t\t$defaults['default'] = get_option( 'default_category' );\n\t\tif ( $defaults['default'] == $term )\n\t\t\treturn 0; // Don't delete the default category\n\t}\n\n\t$args = wp_parse_args($args, $defaults);\n\textract($args, EXTR_SKIP);\n\n\tif ( isset( $default ) ) {\n\t\t$default = (int) $default;\n\t\tif ( ! term_exists($default, $taxonomy) )\n\t\t\tunset($default);\n\t}\n\n\t// Update children to point to new parent\n\tif ( is_taxonomy_hierarchical($taxonomy) ) {\n\t\t$term_obj = get_term($term, $taxonomy);\n\t\tif ( is_wp_error( $term_obj ) )\n\t\t\treturn $term_obj;\n\t\t$parent = $term_obj->parent;\n\n\t\t$edit_tt_ids = $wpdb->get_col( \"SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = \" . (int)$term_obj->term_id );\n\t\tdo_action( 'edit_term_taxonomies', $edit_tt_ids );\n\t\t$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );\n\t\tdo_action( 'edited_term_taxonomies', $edit_tt_ids );\n\t}\n\n\t$objects = $wpdb->get_col( $wpdb->prepare( \"SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d\", $tt_id ) );\n\n\tforeach ( (array) $objects as $object ) {\n\t\t$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));\n\t\tif ( 1 == count($terms) && isset($default) ) {\n\t\t\t$terms = array($default);\n\t\t} else {\n\t\t\t$terms = array_diff($terms, array($term));\n\t\t\tif (isset($default) && isset($force_default) && $force_default)\n\t\t\t\t$terms = array_merge($terms, array($default));\n\t\t}\n\t\t$terms = array_map('intval', $terms);\n\t\twp_set_object_terms($object, $terms, $taxonomy);\n\t}\n\n\t// Clean the relationship caches for all object types using this term\n\t$tax_object = get_taxonomy( $taxonomy );\n\tforeach ( $tax_object->object_type as $object_type )\n\t\tclean_object_term_cache( $objects, $object_type );\n\n\tdo_action( 'delete_term_taxonomy', $tt_id );\n\t$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );\n\tdo_action( 'deleted_term_taxonomy', $tt_id );\n\n\t// Delete the term if no taxonomies use it.\n\tif ( !$wpdb->get_var( $wpdb->prepare( \"SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d\", $term) ) )\n\t\t$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );\n\n\tclean_term_cache($term, $taxonomy);\n\n\tdo_action('delete_term', $term, $tt_id, $taxonomy);\n\tdo_action(\"delete_$taxonomy\", $term, $tt_id);\n\n\treturn true;\n}\n\n/**\n * Deletes one existing category.\n *\n * @since 2.0.0\n * @uses wp_delete_term()\n *\n * @param int $cat_ID\n * @return mixed Returns true if completes delete action; false if term doesn't exist;\n * \tZero on attempted deletion of default Category; WP_Error object is also a possibility.\n */\nfunction wp_delete_category( $cat_ID ) {\n\treturn wp_delete_term( $cat_ID, 'category' );\n}\n\n/**\n * Retrieves the terms associated with the given object(s), in the supplied taxonomies.\n *\n * The following information has to do the $args parameter and for what can be\n * contained in the string or array of that parameter, if it exists.\n *\n * The first argument is called, 'orderby' and has the default value of 'name'.\n * The other value that is supported is 'count'.\n *\n * The second argument is called, 'order' and has the default value of 'ASC'.\n * The only other value that will be acceptable is 'DESC'.\n *\n * The final argument supported is called, 'fields' and has the default value of\n * 'all'. There are multiple other options that can be used instead. Supported\n * values are as follows: 'all', 'ids', 'names', and finally\n * 'all_with_object_id'.\n *\n * The fields argument also decides what will be returned. If 'all' or\n * 'all_with_object_id' is chosen or the default kept intact, then all matching\n * terms objects will be returned. If either 'ids' or 'names' is used, then an\n * array of all matching term ids or term names will be returned respectively.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param int|array $object_ids The ID(s) of the object(s) to retrieve.\n * @param string|array $taxonomies The taxonomies to retrieve terms from.\n * @param array|string $args Change what is returned\n * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.\n */\nfunction wp_get_object_terms($object_ids, $taxonomies, $args = array()) {\n\tglobal $wpdb;\n\n\tif ( empty( $object_ids ) || empty( $taxonomies ) )\n\t\treturn array();\n\n\tif ( !is_array($taxonomies) )\n\t\t$taxonomies = array($taxonomies);\n\n\tforeach ( (array) $taxonomies as $taxonomy ) {\n\t\tif ( ! taxonomy_exists($taxonomy) )\n\t\t\treturn new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\t}\n\n\tif ( !is_array($object_ids) )\n\t\t$object_ids = array($object_ids);\n\t$object_ids = array_map('intval', $object_ids);\n\n\t$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');\n\t$args = wp_parse_args( $args, $defaults );\n\n\t$terms = array();\n\tif ( count($taxonomies) > 1 ) {\n\t\tforeach ( $taxonomies as $index => $taxonomy ) {\n\t\t\t$t = get_taxonomy($taxonomy);\n\t\t\tif ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {\n\t\t\t\tunset($taxonomies[$index]);\n\t\t\t\t$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));\n\t\t\t}\n\t\t}\n\t} else {\n\t\t$t = get_taxonomy($taxonomies[0]);\n\t\tif ( isset($t->args) && is_array($t->args) )\n\t\t\t$args = array_merge($args, $t->args);\n\t}\n\n\textract($args, EXTR_SKIP);\n\n\tif ( 'count' == $orderby )\n\t\t$orderby = 'tt.count';\n\telse if ( 'name' == $orderby )\n\t\t$orderby = 't.name';\n\telse if ( 'slug' == $orderby )\n\t\t$orderby = 't.slug';\n\telse if ( 'term_group' == $orderby )\n\t\t$orderby = 't.term_group';\n\telse if ( 'term_order' == $orderby )\n\t\t$orderby = 'tr.term_order';\n\telse if ( 'none' == $orderby ) {\n\t\t$orderby = '';\n\t\t$order = '';\n\t} else {\n\t\t$orderby = 't.term_id';\n\t}\n\n\t// tt_ids queries can only be none or tr.term_taxonomy_id\n\tif ( ('tt_ids' == $fields) && !empty($orderby) )\n\t\t$orderby = 'tr.term_taxonomy_id';\n\n\tif ( !empty($orderby) )\n\t\t$orderby = \"ORDER BY $orderby\";\n\n\t$taxonomies = \"'\" . implode(\"', '\", $taxonomies) . \"'\";\n\t$object_ids = implode(', ', $object_ids);\n\n\t$select_this = '';\n\tif ( 'all' == $fields )\n\t\t$select_this = 't.*, tt.*';\n\telse if ( 'ids' == $fields )\n\t\t$select_this = 't.term_id';\n\telse if ( 'names' == $fields )\n\t\t$select_this = 't.name';\n\telse if ( 'slugs' == $fields )\n\t\t$select_this = 't.slug';\n\telse if ( 'all_with_object_id' == $fields )\n\t\t$select_this = 't.*, tt.*, tr.object_id';\n\n\t$query = \"SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order\";\n\n\tif ( 'all' == $fields || 'all_with_object_id' == $fields ) {\n\t\t$terms = array_merge($terms, $wpdb->get_results($query));\n\t\tupdate_term_cache($terms);\n\t} else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {\n\t\t$terms = array_merge($terms, $wpdb->get_col($query));\n\t} else if ( 'tt_ids' == $fields ) {\n\t\t$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\");\n\t}\n\n\tif ( ! $terms )\n\t\t$terms = array();\n\n\treturn apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);\n}\n\n/**\n * Adds a new term to the database. Optionally marks it as an alias of an existing term.\n *\n * Error handling is assigned for the nonexistence of the $taxonomy and $term\n * parameters before inserting. If both the term id and taxonomy exist\n * previously, then an array will be returned that contains the term id and the\n * contents of what is returned. The keys of the array are 'term_id' and\n * 'term_taxonomy_id' containing numeric values.\n *\n * It is assumed that the term does not yet exist or the above will apply. The\n * term will be first added to the term table and then related to the taxonomy\n * if everything is well. If everything is correct, then several actions will be\n * run prior to a filter and then several actions will be run after the filter\n * is run.\n *\n * The arguments decide how the term is handled based on the $args parameter.\n * The following is a list of the available overrides and the defaults.\n *\n * 'alias_of'. There is no default, but if added, expected is the slug that the\n * term will be an alias of. Expected to be a string.\n *\n * 'description'. There is no default. If exists, will be added to the database\n * along with the term. Expected to be a string.\n *\n * 'parent'. Expected to be numeric and default is 0 (zero). Will assign value\n * of 'parent' to the term.\n *\n * 'slug'. Expected to be a string. There is no default.\n *\n * If 'slug' argument exists then the slug will be checked to see if it is not\n * a valid term. If that check succeeds (it is not a valid term), then it is\n * added and the term id is given. If it fails, then a check is made to whether\n * the taxonomy is hierarchical and the parent argument is not empty. If the\n * second check succeeds, the term will be inserted and the term id will be\n * given.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @uses apply_filters() Calls 'pre_insert_term' hook with term and taxonomy as parameters.\n * @uses do_action() Calls 'create_term' hook with the term id and taxonomy id as parameters.\n * @uses do_action() Calls 'create_$taxonomy' hook with term id and taxonomy id as parameters.\n * @uses apply_filters() Calls 'term_id_filter' hook with term id and taxonomy id as parameters.\n * @uses do_action() Calls 'created_term' hook with the term id and taxonomy id as parameters.\n * @uses do_action() Calls 'created_$taxonomy' hook with term id and taxonomy id as parameters.\n *\n * @param string $term The term to add or update.\n * @param string $taxonomy The taxonomy to which to add the term\n * @param array|string $args Change the values of the inserted term\n * @return array|WP_Error The Term ID and Term Taxonomy ID\n */\nfunction wp_insert_term( $term, $taxonomy, $args = array() ) {\n\tglobal $wpdb;\n\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\n\t$term = apply_filters( 'pre_insert_term', $term, $taxonomy );\n\t\tif ( is_wp_error( $term ) )\n\t\t\treturn $term;\n\n\tif ( is_int($term) && 0 == $term )\n\t\treturn new WP_Error('invalid_term_id', __('Invalid term ID'));\n\n\tif ( '' == trim($term) )\n\t\treturn new WP_Error('empty_term_name', __('A name is required for this term'));\n\n\t$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');\n\t$args = wp_parse_args($args, $defaults);\n\t$args['name'] = $term;\n\t$args['taxonomy'] = $taxonomy;\n\t$args = sanitize_term($args, $taxonomy, 'db');\n\textract($args, EXTR_SKIP);\n\n\t// expected_slashed ($name)\n\t$name = stripslashes($name);\n\t$description = stripslashes($description);\n\n\tif ( empty($slug) )\n\t\t$slug = sanitize_title($name);\n\n\t$term_group = 0;\n\tif ( $alias_of ) {\n\t\t$alias = $wpdb->get_row( $wpdb->prepare( \"SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s\", $alias_of) );\n\t\tif ( $alias->term_group ) {\n\t\t\t// The alias we want is already in a group, so let's use that one.\n\t\t\t$term_group = $alias->term_group;\n\t\t} else {\n\t\t\t// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.\n\t\t\t$term_group = $wpdb->get_var(\"SELECT MAX(term_group) FROM $wpdb->terms\") + 1;\n\t\t\tdo_action( 'edit_terms', $alias->term_id );\n\t\t\t$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );\n\t\t\tdo_action( 'edited_terms', $alias->term_id );\n\t\t}\n\t}\n\n\tif ( $term_id = term_exists($slug) ) {\n\t\t$existing_term = $wpdb->get_row( $wpdb->prepare( \"SELECT name FROM $wpdb->terms WHERE term_id = %d\", $term_id), ARRAY_A );\n\t\t// We've got an existing term in the same taxonomy, which matches the name of the new term:\n\t\tif ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {\n\t\t\t// Hierarchical, and it matches an existing term, Do not allow same \"name\" in the same level.\n\t\t\t$siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) );\n\t\t\tif ( in_array($name, $siblings) ) {\n\t\t\t\treturn new WP_Error('term_exists', __('A term with the name provided already exists with this parent.'), $exists['term_id']);\n\t\t\t} else {\n\t\t\t\t$slug = wp_unique_term_slug($slug, (object) $args);\n\t\t\t\tif ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )\n\t\t\t\t\treturn new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);\n\t\t\t\t$term_id = (int) $wpdb->insert_id;\n\t\t\t}\n\t\t} elseif ( $existing_term['name'] != $name ) {\n\t\t\t// We've got an existing term, with a different name, Create the new term.\n\t\t\t$slug = wp_unique_term_slug($slug, (object) $args);\n\t\t\tif ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )\n\t\t\t\treturn new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);\n\t\t\t$term_id = (int) $wpdb->insert_id;\n\t\t} elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) {\n\t\t\t// Same name, same slug.\n\t\t\treturn new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);\n\t\t}\n\t} else {\n\t\t// This term does not exist at all in the database, Create it.\n\t\t$slug = wp_unique_term_slug($slug, (object) $args);\n\t\tif ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )\n\t\t\treturn new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);\n\t\t$term_id = (int) $wpdb->insert_id;\n\t}\n\n\t// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.\n\tif ( empty($slug) ) {\n\t\t$slug = sanitize_title($slug, $term_id);\n\t\tdo_action( 'edit_terms', $term_id );\n\t\t$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );\n\t\tdo_action( 'edited_terms', $term_id );\n\t}\n\n\t$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 ) );\n\n\tif ( !empty($tt_id) )\n\t\treturn array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);\n\n\t$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );\n\t$tt_id = (int) $wpdb->insert_id;\n\n\tdo_action(\"create_term\", $term_id, $tt_id, $taxonomy);\n\tdo_action(\"create_$taxonomy\", $term_id, $tt_id);\n\n\t$term_id = apply_filters('term_id_filter', $term_id, $tt_id);\n\n\tclean_term_cache($term_id, $taxonomy);\n\n\tdo_action(\"created_term\", $term_id, $tt_id, $taxonomy);\n\tdo_action(\"created_$taxonomy\", $term_id, $tt_id);\n\n\treturn array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);\n}\n\n/**\n * Create Term and Taxonomy Relationships.\n *\n * Relates an object (post, link etc) to a term and taxonomy type. Creates the\n * term and taxonomy relationship if it doesn't already exist. Creates a term if\n * it doesn't exist (using the slug).\n *\n * A relationship means that the term is grouped in or belongs to the taxonomy.\n * A term has no meaning until it is given context by defining which taxonomy it\n * exists under.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param int $object_id The object to relate to.\n * @param array|int|string $terms The slug or id of the term, will replace all existing\n * related terms in this taxonomy.\n * @param array|string $taxonomy The context in which to relate the term to the object.\n * @param bool $append If false will delete difference of terms.\n * @return array|WP_Error Affected Term IDs\n */\nfunction wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {\n\tglobal $wpdb;\n\n\t$object_id = (int) $object_id;\n\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\n\tif ( !is_array($terms) )\n\t\t$terms = array($terms);\n\n\tif ( ! $append )\n\t\t$old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));\n\telse\n\t\t$old_tt_ids = array();\n\n\t$tt_ids = array();\n\t$term_ids = array();\n\t$new_tt_ids = array();\n\n\tforeach ( (array) $terms as $term) {\n\t\tif ( !strlen(trim($term)) )\n\t\t\tcontinue;\n\n\t\tif ( !$term_info = term_exists($term, $taxonomy) ) {\n\t\t\t// Skip if a non-existent term ID is passed.\n\t\t\tif ( is_int($term) )\n\t\t\t\tcontinue;\n\t\t\t$term_info = wp_insert_term($term, $taxonomy);\n\t\t}\n\t\tif ( is_wp_error($term_info) )\n\t\t\treturn $term_info;\n\t\t$term_ids[] = $term_info['term_id'];\n\t\t$tt_id = $term_info['term_taxonomy_id'];\n\t\t$tt_ids[] = $tt_id;\n\n\t\tif ( $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 ) ) )\n\t\t\tcontinue;\n\t\tdo_action( 'add_term_relationship', $object_id, $tt_id );\n\t\t$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );\n\t\tdo_action( 'added_term_relationship', $object_id, $tt_id );\n\t\t$new_tt_ids[] = $tt_id;\n\t}\n\n\tif ( $new_tt_ids )\n\t\twp_update_term_count( $new_tt_ids, $taxonomy );\n\n\tif ( ! $append ) {\n\t\t$delete_terms = array_diff($old_tt_ids, $tt_ids);\n\t\tif ( $delete_terms ) {\n\t\t\t$in_delete_terms = \"'\" . implode(\"', '\", $delete_terms) . \"'\";\n\t\t\tdo_action( 'delete_term_relationships', $object_id, $delete_terms );\n\t\t\t$wpdb->query( $wpdb->prepare(\"DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)\", $object_id) );\n\t\t\tdo_action( 'deleted_term_relationships', $object_id, $delete_terms );\n\t\t\twp_update_term_count($delete_terms, $taxonomy);\n\t\t}\n\t}\n\n\t$t = get_taxonomy($taxonomy);\n\tif ( ! $append && isset($t->sort) && $t->sort ) {\n\t\t$values = array();\n\t\t$term_order = 0;\n\t\t$final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));\n\t\tforeach ( $tt_ids as $tt_id )\n\t\t\tif ( in_array($tt_id, $final_tt_ids) )\n\t\t\t\t$values[] = $wpdb->prepare( \"(%d, %d, %d)\", $object_id, $tt_id, ++$term_order);\n\t\tif ( $values )\n\t\t\t$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)\");\n\t}\n\n\tdo_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);\n\treturn $tt_ids;\n}\n\n/**\n * Will make slug unique, if it isn't already.\n *\n * The $slug has to be unique global to every taxonomy, meaning that one\n * taxonomy term can't have a matching slug with another taxonomy term. Each\n * slug has to be globally unique for every taxonomy.\n *\n * The way this works is that if the taxonomy that the term belongs to is\n * hierarchical and has a parent, it will append that parent to the $slug.\n *\n * If that still doesn't return an unique slug, then it try to append a number\n * until it finds a number that is truly unique.\n *\n * The only purpose for $term is for appending a parent, if one exists.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param string $slug The string that will be tried for a unique slug\n * @param object $term The term object that the $slug will belong too\n * @return string Will return a true unique slug.\n */\nfunction wp_unique_term_slug($slug, $term) {\n\tglobal $wpdb;\n\n\tif ( ! term_exists( $slug ) )\n\t\treturn $slug;\n\n\t// If the taxonomy supports hierarchy and the term has a parent, make the slug unique\n\t// by incorporating parent slugs.\n\tif ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {\n\t\t$the_parent = $term->parent;\n\t\twhile ( ! empty($the_parent) ) {\n\t\t\t$parent_term = get_term($the_parent, $term->taxonomy);\n\t\t\tif ( is_wp_error($parent_term) || empty($parent_term) )\n\t\t\t\tbreak;\n\t\t\t$slug .= '-' . $parent_term->slug;\n\t\t\tif ( ! term_exists( $slug ) )\n\t\t\t\treturn $slug;\n\n\t\t\tif ( empty($parent_term->parent) )\n\t\t\t\tbreak;\n\t\t\t$the_parent = $parent_term->parent;\n\t\t}\n\t}\n\n\t// If we didn't get a unique slug, try appending a number to make it unique.\n\tif ( !empty($args['term_id']) )\n\t\t$query = $wpdb->prepare( \"SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d\", $slug, $args['term_id'] );\n\telse\n\t\t$query = $wpdb->prepare( \"SELECT slug FROM $wpdb->terms WHERE slug = %s\", $slug );\n\n\tif ( $wpdb->get_var( $query ) ) {\n\t\t$num = 2;\n\t\tdo {\n\t\t\t$alt_slug = $slug . \"-$num\";\n\t\t\t$num++;\n\t\t\t$slug_check = $wpdb->get_var( $wpdb->prepare( \"SELECT slug FROM $wpdb->terms WHERE slug = %s\", $alt_slug ) );\n\t\t} while ( $slug_check );\n\t\t$slug = $alt_slug;\n\t}\n\n\treturn $slug;\n}\n\n/**\n * Update term based on arguments provided.\n *\n * The $args will indiscriminately override all values with the same field name.\n * Care must be taken to not override important information need to update or\n * update will fail (or perhaps create a new term, neither would be acceptable).\n *\n * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not\n * defined in $args already.\n *\n * 'alias_of' will create a term group, if it doesn't already exist, and update\n * it for the $term.\n *\n * If the 'slug' argument in $args is missing, then the 'name' in $args will be\n * used. It should also be noted that if you set 'slug' and it isn't unique then\n * a WP_Error will be passed back. If you don't pass any slug, then a unique one\n * will be created for you.\n *\n * For what can be overrode in $args, check the term scheme can contain and stay\n * away from the term keys.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses $wpdb\n * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice.\n * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term\n *\tid and taxonomy id.\n *\n * @param int $term_id The ID of the term\n * @param string $taxonomy The context in which to relate the term to the object.\n * @param array|string $args Overwrite term field values\n * @return array|WP_Error Returns Term ID and Taxonomy Term ID\n */\nfunction wp_update_term( $term_id, $taxonomy, $args = array() ) {\n\tglobal $wpdb;\n\n\tif ( ! taxonomy_exists($taxonomy) )\n\t\treturn new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));\n\n\t$term_id = (int) $term_id;\n\n\t// First, get all of the original args\n\t$term = get_term ($term_id, $taxonomy, ARRAY_A);\n\n\tif ( is_wp_error( $term ) )\n\t\treturn $term;\n\n\t// Escape data pulled from DB.\n\t$term = add_magic_quotes($term);\n\n\t// Merge old and new args with new args overwriting old ones.\n\t$args = array_merge($term, $args);\n\n\t$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');\n\t$args = wp_parse_args($args, $defaults);\n\t$args = sanitize_term($args, $taxonomy, 'db');\n\textract($args, EXTR_SKIP);\n\n\t// expected_slashed ($name)\n\t$name = stripslashes($name);\n\t$description = stripslashes($description);\n\n\tif ( '' == trim($name) )\n\t\treturn new WP_Error('empty_term_name', __('A name is required for this term'));\n\n\t$empty_slug = false;\n\tif ( empty($slug) ) {\n\t\t$empty_slug = true;\n\t\t$slug = sanitize_title($name);\n\t}\n\n\tif ( $alias_of ) {\n\t\t$alias = $wpdb->get_row( $wpdb->prepare( \"SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s\", $alias_of) );\n\t\tif ( $alias->term_group ) {\n\t\t\t// The alias we want is already in a group, so let's use that one.\n\t\t\t$term_group = $alias->term_group;\n\t\t} else {\n\t\t\t// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.\n\t\t\t$term_group = $wpdb->get_var(\"SELECT MAX(term_group) FROM $wpdb->terms\") + 1;\n\t\t\tdo_action( 'edit_terms', $alias->term_id );\n\t\t\t$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );\n\t\t\tdo_action( 'edited_terms', $alias->term_id );\n\t\t}\n\t}\n\n\t// Check $parent to see if it will cause a hierarchy loop\n\t$parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );\n\n\t// Check for duplicate slug\n\t$id = $wpdb->get_var( $wpdb->prepare( \"SELECT term_id FROM $wpdb->terms WHERE slug = %s\", $slug ) );\n\tif ( $id && ($id != $term_id) ) {\n\t\t// If an empty slug was passed or the parent changed, reset the slug to something unique.\n\t\t// Otherwise, bail.\n\t\tif ( $empty_slug || ( $parent != $term['parent']) )\n\t\t\t$slug = wp_unique_term_slug($slug, (object) $args);\n\t\telse\n\t\t\treturn new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));\n\t}\n\tdo_action( 'edit_terms', $term_id );\n\t$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );\n\tif ( empty($slug) ) {\n\t\t$slug = sanitize_title($name, $term_id);\n\t\t$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );\n\t}\n\tdo_action( 'edited_terms', $term_id );\n\n\t$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) );\n\tdo_action( 'edit_term_taxonomy', $tt_id, $taxonomy );\n\t$wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );\n\tdo_action( 'edited_term_taxonomy', $tt_id, $taxonomy );\n\n\tdo_action(\"edit_term\", $term_id, $tt_id, $taxonomy);\n\tdo_action(\"edit_$taxonomy\", $term_id, $tt_id);\n\n\t$term_id = apply_filters('term_id_filter', $term_id, $tt_id);\n\n\tclean_term_cache($term_id, $taxonomy);\n\n\tdo_action(\"edited_term\", $term_id, $tt_id, $taxonomy);\n\tdo_action(\"edited_$taxonomy\", $term_id, $tt_id);\n\n\treturn array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);\n}\n\n/**\n * Enable or disable term counting.\n *\n * @since 2.5.0\n *\n * @param bool $defer Optional. Enable if true, disable if false.\n * @return bool Whether term counting is enabled or disabled.\n */\nfunction wp_defer_term_counting($defer=null) {\n\tstatic $_defer = false;\n\n\tif ( is_bool($defer) ) {\n\t\t$_defer = $defer;\n\t\t// flush any deferred counts\n\t\tif ( !$defer )\n\t\t\twp_update_term_count( null, null, true );\n\t}\n\n\treturn $_defer;\n}\n\n/**\n * Updates the amount of terms in taxonomy.\n *\n * If there is a taxonomy callback applied, then it will be called for updating\n * the count.\n *\n * The default action is to count what the amount of terms have the relationship\n * of term ID. Once that is done, then update the database.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param int|array $terms The term_taxonomy_id of the terms\n * @param string $taxonomy The context of the term.\n * @return bool If no terms will return false, and if successful will return true.\n */\nfunction wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {\n\tstatic $_deferred = array();\n\n\tif ( $do_deferred ) {\n\t\tforeach ( (array) array_keys($_deferred) as $tax ) {\n\t\t\twp_update_term_count_now( $_deferred[$tax], $tax );\n\t\t\tunset( $_deferred[$tax] );\n\t\t}\n\t}\n\n\tif ( empty($terms) )\n\t\treturn false;\n\n\tif ( !is_array($terms) )\n\t\t$terms = array($terms);\n\n\tif ( wp_defer_term_counting() ) {\n\t\tif ( !isset($_deferred[$taxonomy]) )\n\t\t\t$_deferred[$taxonomy] = array();\n\t\t$_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );\n\t\treturn true;\n\t}\n\n\treturn wp_update_term_count_now( $terms, $taxonomy );\n}\n\n/**\n * Perform term count update immediately.\n *\n * @since 2.5.0\n *\n * @param array $terms The term_taxonomy_id of terms to update.\n * @param string $taxonomy The context of the term.\n * @return bool Always true when complete.\n */\nfunction wp_update_term_count_now( $terms, $taxonomy ) {\n\tglobal $wpdb;\n\n\t$terms = array_map('intval', $terms);\n\n\t$taxonomy = get_taxonomy($taxonomy);\n\tif ( !empty($taxonomy->update_count_callback) ) {\n\t\tcall_user_func($taxonomy->update_count_callback, $terms, $taxonomy);\n\t} else {\n\t\t$object_types = (array) $taxonomy->object_type;\n\t\tforeach ( $object_types as &$object_type ) {\n\t\t\tif ( 0 === strpos( $object_type, 'attachment:' ) )\n\t\t\t\tlist( $object_type ) = explode( ':', $object_type );\n\t\t}\n\n\t\tif ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {\n\t\t\t// Only post types are attached to this taxonomy\n\t\t\t_update_post_term_count( $terms, $taxonomy );\n\t\t} else {\n\t\t\t// Default count updater\n\t\t\t_update_generic_term_count( $terms, $taxonomy );\n\t\t}\n\t}\n\n\tclean_term_cache($terms, '', false);\n\n\treturn true;\n}\n\n//\n// Cache\n//\n\n/**\n * Removes the taxonomy relationship to terms from the cache.\n *\n * Will remove the entire taxonomy relationship containing term $object_id. The\n * term IDs have to exist within the taxonomy $object_type for the deletion to\n * take place.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @see get_object_taxonomies() for more on $object_type\n * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion.\n *\tPasses, function params in same order.\n *\n * @param int|array $object_ids Single or list of term object ID(s)\n * @param array|string $object_type The taxonomy object type\n */\nfunction clean_object_term_cache($object_ids, $object_type) {\n\tif ( !is_array($object_ids) )\n\t\t$object_ids = array($object_ids);\n\n\tforeach ( $object_ids as $id )\n\t\tforeach ( get_object_taxonomies($object_type) as $taxonomy )\n\t\t\twp_cache_delete($id, \"{$taxonomy}_relationships\");\n\n\tdo_action('clean_object_term_cache', $object_ids, $object_type);\n}\n\n/**\n * Will remove all of the term ids from the cache.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param int|array $ids Single or list of Term IDs\n * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.\n * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.\n */\nfunction clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {\n\tglobal $wpdb;\n\tstatic $cleaned = array();\n\n\tif ( !is_array($ids) )\n\t\t$ids = array($ids);\n\n\t$taxonomies = array();\n\t// If no taxonomy, assume tt_ids.\n\tif ( empty($taxonomy) ) {\n\t\t$tt_ids = array_map('intval', $ids);\n\t\t$tt_ids = implode(', ', $tt_ids);\n\t\t$terms = $wpdb->get_results(\"SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)\");\n\t\t$ids = array();\n\t\tforeach ( (array) $terms as $term ) {\n\t\t\t$taxonomies[] = $term->taxonomy;\n\t\t\t$ids[] = $term->term_id;\n\t\t\twp_cache_delete($term->term_id, $term->taxonomy);\n\t\t}\n\t\t$taxonomies = array_unique($taxonomies);\n\t} else {\n\t\t$taxonomies = array($taxonomy);\n\t\tforeach ( $taxonomies as $taxonomy ) {\n\t\t\tforeach ( $ids as $id ) {\n\t\t\t\twp_cache_delete($id, $taxonomy);\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach ( $taxonomies as $taxonomy ) {\n\t\tif ( isset($cleaned[$taxonomy]) )\n\t\t\tcontinue;\n\t\t$cleaned[$taxonomy] = true;\n\n\t\tif ( $clean_taxonomy ) {\n\t\t\twp_cache_delete('all_ids', $taxonomy);\n\t\t\twp_cache_delete('get', $taxonomy);\n\t\t\tdelete_option(\"{$taxonomy}_children\");\n\t\t\t// Regenerate {$taxonomy}_children\n\t\t\t_get_term_hierarchy($taxonomy);\n\t\t}\n\n\t\tdo_action('clean_term_cache', $ids, $taxonomy);\n\t}\n\n\twp_cache_set('last_changed', time(), 'terms');\n}\n\n/**\n * Retrieves the taxonomy relationship to the term object id.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @uses wp_cache_get() Retrieves taxonomy relationship from cache\n *\n * @param int|array $id Term object ID\n * @param string $taxonomy Taxonomy Name\n * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.\n */\nfunction &get_object_term_cache($id, $taxonomy) {\n\t$cache = wp_cache_get($id, \"{$taxonomy}_relationships\");\n\treturn $cache;\n}\n\n/**\n * Updates the cache for Term ID(s).\n *\n * Will only update the cache for terms not already cached.\n *\n * The $object_ids expects that the ids be separated by commas, if it is a\n * string.\n *\n * It should be noted that update_object_term_cache() is very time extensive. It\n * is advised that the function is not called very often or at least not for a\n * lot of terms that exist in a lot of taxonomies. The amount of time increases\n * for each term and it also increases for each taxonomy the term belongs to.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n * @uses wp_get_object_terms() Used to get terms from the database to update\n *\n * @param string|array $object_ids Single or list of term object ID(s)\n * @param array|string $object_type The taxonomy object type\n * @return null|bool Null value is given with empty $object_ids. False if\n */\nfunction update_object_term_cache($object_ids, $object_type) {\n\tif ( empty($object_ids) )\n\t\treturn;\n\n\tif ( !is_array($object_ids) )\n\t\t$object_ids = explode(',', $object_ids);\n\n\t$object_ids = array_map('intval', $object_ids);\n\n\t$taxonomies = get_object_taxonomies($object_type);\n\n\t$ids = array();\n\tforeach ( (array) $object_ids as $id ) {\n\t\tforeach ( $taxonomies as $taxonomy ) {\n\t\t\tif ( false === wp_cache_get($id, \"{$taxonomy}_relationships\") ) {\n\t\t\t\t$ids[] = $id;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( empty( $ids ) )\n\t\treturn false;\n\n\t$terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id'));\n\n\t$object_terms = array();\n\tforeach ( (array) $terms as $term )\n\t\t$object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;\n\n\tforeach ( $ids as $id ) {\n\t\tforeach ( $taxonomies as $taxonomy ) {\n\t\t\tif ( ! isset($object_terms[$id][$taxonomy]) ) {\n\t\t\t\tif ( !isset($object_terms[$id]) )\n\t\t\t\t\t$object_terms[$id] = array();\n\t\t\t\t$object_terms[$id][$taxonomy] = array();\n\t\t\t}\n\t\t}\n\t}\n\n\tforeach ( $object_terms as $id => $value ) {\n\t\tforeach ( $value as $taxonomy => $terms ) {\n\t\t\twp_cache_add( $id, $terms, \"{$taxonomy}_relationships\" );\n\t\t}\n\t}\n}\n\n/**\n * Updates Terms to Taxonomy in cache.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 2.3.0\n *\n * @param array $terms List of Term objects to change\n * @param string $taxonomy Optional. Update Term to this taxonomy in cache\n */\nfunction update_term_cache($terms, $taxonomy = '') {\n\tforeach ( (array) $terms as $term ) {\n\t\t$term_taxonomy = $taxonomy;\n\t\tif ( empty($term_taxonomy) )\n\t\t\t$term_taxonomy = $term->taxonomy;\n\n\t\twp_cache_add($term->term_id, $term, $term_taxonomy);\n\t}\n}\n\n//\n// Private\n//\n\n/**\n * Retrieves children of taxonomy as Term IDs.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @access private\n * @since 2.3.0\n *\n * @uses update_option() Stores all of the children in \"$taxonomy_children\"\n *\t option. That is the name of the taxonomy, immediately followed by '_children'.\n *\n * @param string $taxonomy Taxonomy Name\n * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs.\n */\nfunction _get_term_hierarchy($taxonomy) {\n\tif ( !is_taxonomy_hierarchical($taxonomy) )\n\t\treturn array();\n\t$children = get_option(\"{$taxonomy}_children\");\n\n\tif ( is_array($children) )\n\t\treturn $children;\n\t$children = array();\n\t$terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));\n\tforeach ( $terms as $term_id => $parent ) {\n\t\tif ( $parent > 0 )\n\t\t\t$children[$parent][] = $term_id;\n\t}\n\tupdate_option(\"{$taxonomy}_children\", $children);\n\n\treturn $children;\n}\n\n/**\n * Get the subset of $terms that are descendants of $term_id.\n *\n * If $terms is an array of objects, then _get_term_children returns an array of objects.\n * If $terms is an array of IDs, then _get_term_children returns an array of IDs.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @access private\n * @since 2.3.0\n *\n * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.\n * @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen.\n * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.\n * @return array The subset of $terms that are descendants of $term_id.\n */\nfunction &_get_term_children($term_id, $terms, $taxonomy) {\n\t$empty_array = array();\n\tif ( empty($terms) )\n\t\treturn $empty_array;\n\n\t$term_list = array();\n\t$has_children = _get_term_hierarchy($taxonomy);\n\n\tif ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )\n\t\treturn $empty_array;\n\n\tforeach ( (array) $terms as $term ) {\n\t\t$use_id = false;\n\t\tif ( !is_object($term) ) {\n\t\t\t$term = get_term($term, $taxonomy);\n\t\t\tif ( is_wp_error( $term ) )\n\t\t\t\treturn $term;\n\t\t\t$use_id = true;\n\t\t}\n\n\t\tif ( $term->term_id == $term_id )\n\t\t\tcontinue;\n\n\t\tif ( $term->parent == $term_id ) {\n\t\t\tif ( $use_id )\n\t\t\t\t$term_list[] = $term->term_id;\n\t\t\telse\n\t\t\t\t$term_list[] = $term;\n\n\t\t\tif ( !isset($has_children[$term->term_id]) )\n\t\t\t\tcontinue;\n\n\t\t\tif ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )\n\t\t\t\t$term_list = array_merge($term_list, $children);\n\t\t}\n\t}\n\n\treturn $term_list;\n}\n\n/**\n * Add count of children to parent count.\n *\n * Recalculates term counts by including items from child terms. Assumes all\n * relevant children are already in the $terms argument.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @access private\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param array $terms List of Term IDs\n * @param string $taxonomy Term Context\n * @return null Will break from function if conditions are not met.\n */\nfunction _pad_term_counts(&$terms, $taxonomy) {\n\tglobal $wpdb;\n\n\t// This function only works for hierarchical taxonomies like post categories.\n\tif ( !is_taxonomy_hierarchical( $taxonomy ) )\n\t\treturn;\n\n\t$term_hier = _get_term_hierarchy($taxonomy);\n\n\tif ( empty($term_hier) )\n\t\treturn;\n\n\t$term_items = array();\n\n\tforeach ( (array) $terms as $key => $term ) {\n\t\t$terms_by_id[$term->term_id] = & $terms[$key];\n\t\t$term_ids[$term->term_taxonomy_id] = $term->term_id;\n\t}\n\n\t// Get the object and term ids and stick them in a lookup table\n\t$tax_obj = get_taxonomy($taxonomy);\n\t$object_types = esc_sql($tax_obj->object_type);\n\t$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'\");\n\tforeach ( $results as $row ) {\n\t\t$id = $term_ids[$row->term_taxonomy_id];\n\t\t$term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;\n\t}\n\n\t// Touch every ancestor's lookup row for each post in each term\n\tforeach ( $term_ids as $term_id ) {\n\t\t$child = $term_id;\n\t\twhile ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {\n\t\t\tif ( !empty( $term_items[$term_id] ) )\n\t\t\t\tforeach ( $term_items[$term_id] as $item_id => $touches ) {\n\t\t\t\t\t$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;\n\t\t\t\t}\n\t\t\t$child = $parent;\n\t\t}\n\t}\n\n\t// Transfer the touched cells\n\tforeach ( (array) $term_items as $id => $items )\n\t\tif ( isset($terms_by_id[$id]) )\n\t\t\t$terms_by_id[$id]->count = count($items);\n}\n\n//\n// Default callbacks\n//\n\n/**\n * Will update term count based on object types of the current taxonomy.\n *\n * Private function for the default callback for post_tag and category\n * taxonomies.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @access private\n * @since 2.3.0\n * @uses $wpdb\n *\n * @param array $terms List of Term taxonomy IDs\n * @param object $taxonomy Current taxonomy object of terms\n */\nfunction _update_post_term_count( $terms, $taxonomy ) {\n\tglobal $wpdb;\n\n\t$object_types = (array) $taxonomy->object_type;\n\n\tforeach ( $object_types as &$object_type )\n\t\tlist( $object_type ) = explode( ':', $object_type );\n\n\t$object_types = array_unique( $object_types );\n\n\tif ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) {\n\t\tunset( $object_types[ $check_attachments ] );\n\t\t$check_attachments = true;\n\t}\n\n\tif ( $object_types )\n\t\t$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );\n\n\tforeach ( (array) $terms as $term ) {\n\t\t$count = 0;\n\n\t\t// Attachments can be 'inherit' status, we need to base count off the parent's status if so\n\t\tif ( $check_attachments )\n\t\t\t$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 ) );\n\n\t\tif ( $object_types )\n\t\t\t$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 ) );\n\n\t\tdo_action( 'edit_term_taxonomy', $term, $taxonomy );\n\t\t$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );\n\t\tdo_action( 'edited_term_taxonomy', $term, $taxonomy );\n\t}\n}\n\n/**\n * Will update term count based on number of objects.\n *\n * Default callback for the link_category taxonomy.\n *\n * @package WordPress\n * @subpackage Taxonomy\n * @since 3.3.0\n * @uses $wpdb\n *\n * @param array $terms List of Term taxonomy IDs\n * @param object $taxonomy Current taxonomy object of terms\n */\nfunction _update_generic_term_count( $terms, $taxonomy ) {\n\tglobal $wpdb;\n\n\tforeach ( (array) $terms as $term ) {\n\t\t$count = $wpdb->get_var( $wpdb->prepare( \"SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d\", $term ) );\n\n\t\tdo_action( 'edit_term_taxonomy', $term, $taxonomy );\n\t\t$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );\n\t\tdo_action( 'edited_term_taxonomy', $term, $taxonomy );\n\t}\n}\n\n/**\n * Generates a permalink for a taxonomy term archive.\n *\n * @since 2.5.0\n *\n * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters.\n * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters.\n * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID.\n *\n * @param object|int|string $term\n * @param string $taxonomy (optional if $term is object)\n * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.\n */\nfunction get_term_link( $term, $taxonomy = '') {\n\tglobal $wp_rewrite;\n\n\tif ( !is_object($term) ) {\n\t\tif ( is_int($term) ) {\n\t\t\t$term = &get_term($term, $taxonomy);\n\t\t} else {\n\t\t\t$term = &get_term_by('slug', $term, $taxonomy);\n\t\t}\n\t}\n\n\tif ( !is_object($term) )\n\t\t$term = new WP_Error('invalid_term', __('Empty Term'));\n\n\tif ( is_wp_error( $term ) )\n\t\treturn $term;\n\n\t$taxonomy = $term->taxonomy;\n\n\t$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);\n\n\t$slug = $term->slug;\n\t$t = get_taxonomy($taxonomy);\n\n\tif ( empty($termlink) ) {\n\t\tif ( 'category' == $taxonomy )\n\t\t\t$termlink = '?cat=' . $term->term_id;\n\t\telseif ( $t->query_var )\n\t\t\t$termlink = \"?$t->query_var=$slug\";\n\t\telse\n\t\t\t$termlink = \"?taxonomy=$taxonomy&term=$slug\";\n\t\t$termlink = home_url($termlink);\n\t} else {\n\t\tif ( $t->rewrite['hierarchical'] ) {\n\t\t\t$hierarchical_slugs = array();\n\t\t\t$ancestors = get_ancestors($term->term_id, $taxonomy);\n\t\t\tforeach ( (array)$ancestors as $ancestor ) {\n\t\t\t\t$ancestor_term = get_term($ancestor, $taxonomy);\n\t\t\t\t$hierarchical_slugs[] = $ancestor_term->slug;\n\t\t\t}\n\t\t\t$hierarchical_slugs = array_reverse($hierarchical_slugs);\n\t\t\t$hierarchical_slugs[] = $slug;\n\t\t\t$termlink = str_replace(\"%$taxonomy%\", implode('/', $hierarchical_slugs), $termlink);\n\t\t} else {\n\t\t\t$termlink = str_replace(\"%$taxonomy%\", $slug, $termlink);\n\t\t}\n\t\t$termlink = home_url( user_trailingslashit($termlink, 'category') );\n\t}\n\t// Back Compat filters.\n\tif ( 'post_tag' == $taxonomy )\n\t\t$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );\n\telseif ( 'category' == $taxonomy )\n\t\t$termlink = apply_filters( 'category_link', $termlink, $term->term_id );\n\n\treturn apply_filters('term_link', $termlink, $term, $taxonomy);\n}\n\n/**\n * Display the taxonomies of a post with available options.\n *\n * This function can be used within the loop to display the taxonomies for a\n * post without specifying the Post ID. You can also use it outside the Loop to\n * display the taxonomies for a specific post.\n *\n * The available defaults are:\n * 'post' : default is 0. The post ID to get taxonomies of.\n * 'before' : default is empty string. Display before taxonomies list.\n * 'sep' : default is empty string. Separate every taxonomy with value in this.\n * 'after' : default is empty string. Display this after the taxonomies list.\n * 'template' : The template to use for displaying the taxonomy terms.\n *\n * @since 2.5.0\n * @uses get_the_taxonomies()\n *\n * @param array $args Override the defaults.\n */\nfunction the_taxonomies($args = array()) {\n\t$defaults = array(\n\t\t'post' => 0,\n\t\t'before' => '',\n\t\t'sep' => ' ',\n\t\t'after' => '',\n\t\t'template' => '%s: %l.'\n\t);\n\n\t$r = wp_parse_args( $args, $defaults );\n\textract( $r, EXTR_SKIP );\n\n\techo $before . join($sep, get_the_taxonomies($post, $r)) . $after;\n}\n\n/**\n * Retrieve all taxonomies associated with a post.\n *\n * This function can be used within the loop. It will also return an array of\n * the taxonomies with links to the taxonomy and name.\n *\n * @since 2.5.0\n *\n * @param int $post Optional. Post ID or will use Global Post ID (in loop).\n * @param array $args Override the defaults.\n * @return array\n */\nfunction get_the_taxonomies($post = 0, $args = array() ) {\n\tif ( is_int($post) )\n\t\t$post =& get_post($post);\n\telseif ( !is_object($post) )\n\t\t$post =& $GLOBALS['post'];\n\n\t$args = wp_parse_args( $args, array(\n\t\t'template' => '%s: %l.',\n\t) );\n\textract( $args, EXTR_SKIP );\n\n\t$taxonomies = array();\n\n\tif ( !$post )\n\t\treturn $taxonomies;\n\n\tforeach ( get_object_taxonomies($post) as $taxonomy ) {\n\t\t$t = (array) get_taxonomy($taxonomy);\n\t\tif ( empty($t['label']) )\n\t\t\t$t['label'] = $taxonomy;\n\t\tif ( empty($t['args']) )\n\t\t\t$t['args'] = array();\n\t\tif ( empty($t['template']) )\n\t\t\t$t['template'] = $template;\n\n\t\t$terms = get_object_term_cache($post->ID, $taxonomy);\n\t\tif ( empty($terms) )\n\t\t\t$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);\n\n\t\t$links = array();\n\n\t\tforeach ( $terms as $term )\n\t\t\t$links[] = \"<a href='\" . esc_attr( get_term_link($term) ) . \"'>$term->name</a>\";\n\n\t\tif ( $links )\n\t\t\t$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);\n\t}\n\treturn $taxonomies;\n}\n\n/**\n * Retrieve all taxonomies of a post with just the names.\n *\n * @since 2.5.0\n * @uses get_object_taxonomies()\n *\n * @param int $post Optional. Post ID\n * @return array\n */\nfunction get_post_taxonomies($post = 0) {\n\t$post =& get_post($post);\n\n\treturn get_object_taxonomies($post);\n}\n\n/**\n * Determine if the given object is associated with any of the given terms.\n *\n * The given terms are checked against the object's terms' term_ids, names and slugs.\n * Terms given as integers will only be checked against the object's terms' term_ids.\n * If no terms are given, determines if object is associated with any terms in the given taxonomy.\n *\n * @since 2.7.0\n * @uses get_object_term_cache()\n * @uses wp_get_object_terms()\n *\n * @param int $object_id ID of the object (post ID, link ID, ...)\n * @param string $taxonomy Single taxonomy name\n * @param int|string|array $terms Optional. Term term_id, name, slug or array of said\n * @return bool|WP_Error. WP_Error on input error.\n */\nfunction is_object_in_term( $object_id, $taxonomy, $terms = null ) {\n\tif ( !$object_id = (int) $object_id )\n\t\treturn new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );\n\n\t$object_terms = get_object_term_cache( $object_id, $taxonomy );\n\tif ( empty( $object_terms ) )\n\t\t $object_terms = wp_get_object_terms( $object_id, $taxonomy );\n\n\tif ( is_wp_error( $object_terms ) )\n\t\treturn $object_terms;\n\tif ( empty( $object_terms ) )\n\t\treturn false;\n\tif ( empty( $terms ) )\n\t\treturn ( !empty( $object_terms ) );\n\n\t$terms = (array) $terms;\n\n\tif ( $ints = array_filter( $terms, 'is_int' ) )\n\t\t$strs = array_diff( $terms, $ints );\n\telse\n\t\t$strs =& $terms;\n\n\tforeach ( $object_terms as $object_term ) {\n\t\tif ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id\n\t\tif ( $strs ) {\n\t\t\tif ( in_array( $object_term->term_id, $strs ) ) return true;\n\t\t\tif ( in_array( $object_term->name, $strs ) ) return true;\n\t\t\tif ( in_array( $object_term->slug, $strs ) ) return true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n/**\n * Determine if the given object type is associated with the given taxonomy.\n *\n * @since 3.0.0\n * @uses get_object_taxonomies()\n *\n * @param string $object_type Object type string\n * @param string $taxonomy Single taxonomy name\n * @return bool True if object is associated with the taxonomy, otherwise false.\n */\nfunction is_object_in_taxonomy($object_type, $taxonomy) {\n\t$taxonomies = get_object_taxonomies($object_type);\n\n\tif ( empty($taxonomies) )\n\t\treturn false;\n\n\tif ( in_array($taxonomy, $taxonomies) )\n\t\treturn true;\n\n\treturn false;\n}\n\n/**\n * Get an array of ancestor IDs for a given object.\n *\n * @param int $object_id The ID of the object\n * @param string $object_type The type of object for which we'll be retrieving ancestors.\n * @return array of ancestors from lowest to highest in the hierarchy.\n */\nfunction get_ancestors($object_id = 0, $object_type = '') {\n\t$object_id = (int) $object_id;\n\n\t$ancestors = array();\n\n\tif ( empty( $object_id ) ) {\n\t\treturn apply_filters('get_ancestors', $ancestors, $object_id, $object_type);\n\t}\n\n\tif ( is_taxonomy_hierarchical( $object_type ) ) {\n\t\t$term = get_term($object_id, $object_type);\n\t\twhile ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {\n\t\t\t$ancestors[] = (int) $term->parent;\n\t\t\t$term = get_term($term->parent, $object_type);\n\t\t}\n\t} elseif ( null !== get_post_type_object( $object_type ) ) {\n\t\t$object = get_post($object_id);\n\t\tif ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )\n\t\t\t$ancestors = $object->ancestors;\n\t\telse {\n\t\t\twhile ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {\n\t\t\t\t$ancestors[] = (int) $object->post_parent;\n\t\t\t\t$object = get_post($object->post_parent);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn apply_filters('get_ancestors', $ancestors, $object_id, $object_type);\n}\n\n/**\n * Returns the term's parent's term_ID\n *\n * @since 3.1.0\n *\n * @param int $term_id\n * @param string $taxonomy\n *\n * @return int|bool false on error\n */\nfunction wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {\n\t$term = get_term( $term_id, $taxonomy );\n\tif ( !$term || is_wp_error( $term ) )\n\t\treturn false;\n\treturn (int) $term->parent;\n}\n\n/**\n * Checks the given subset of the term hierarchy for hierarchy loops.\n * Prevents loops from forming and breaks those that it finds.\n *\n * Attached to the wp_update_term_parent filter.\n *\n * @since 3.1.0\n * @uses wp_find_hierarchy_loop()\n *\n * @param int $parent term_id of the parent for the term we're checking.\n * @param int $term_id The term we're checking.\n * @param string $taxonomy The taxonomy of the term we're checking.\n *\n * @return int The new parent for the term.\n */\nfunction wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {\n\t// Nothing fancy here - bail\n\tif ( !$parent )\n\t\treturn 0;\n\n\t// Can't be its own parent\n\tif ( $parent == $term_id )\n\t\treturn 0;\n\n\t// Now look for larger loops\n\n\tif ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )\n\t\treturn $parent; // No loop\n\n\t// Setting $parent to the given value causes a loop\n\tif ( isset( $loop[$term_id] ) )\n\t\treturn 0;\n\n\t// There's a loop, but it doesn't contain $term_id. Break the loop.\n\tforeach ( array_keys( $loop ) as $loop_member )\n\t\twp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );\n\n\treturn $parent;\n}\n
|
|
|
934 | 934 | // Assume already escaped |
935 | 935 | $value = stripslashes($value); |
936 | 936 | $field = 't.name'; |
| 937 | } else if ( 'term_taxonomy_id' == $field ) { |
| 938 | $value = (int) $value; |
| 939 | $field = 'tt.term_taxonomy_id'; |
937 | 940 | } else { |
938 | 941 | $term = get_term( (int) $value, $taxonomy, $output, $filter); |
939 | 942 | if ( is_wp_error( $term ) ) |