wp/wp-includes/taxonomy.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-includes/taxonomy.php	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/taxonomy.php	Mon Oct 14 18:28:13 2019 +0200
@@ -25,7 +25,11 @@
 	global $wp_rewrite;
 
 	if ( ! did_action( 'init' ) ) {
-		$rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );
+		$rewrite = array(
+			'category'    => false,
+			'post_tag'    => false,
+			'post_format' => false,
+		);
 	} else {
 
 		/**
@@ -36,118 +40,138 @@
 		 * @param string $context Context of the rewrite base. Default 'type'.
 		 */
 		$post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
-		$rewrite = array(
-			'category' => array(
+		$rewrite          = array(
+			'category'    => array(
 				'hierarchical' => true,
-				'slug' => get_option('category_base') ? get_option('category_base') : 'category',
-				'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
-				'ep_mask' => EP_CATEGORIES,
+				'slug'         => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category',
+				'with_front'   => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(),
+				'ep_mask'      => EP_CATEGORIES,
 			),
-			'post_tag' => array(
+			'post_tag'    => array(
 				'hierarchical' => false,
-				'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
-				'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
-				'ep_mask' => EP_TAGS,
+				'slug'         => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag',
+				'with_front'   => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(),
+				'ep_mask'      => EP_TAGS,
 			),
 			'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
 		);
 	}
 
-	register_taxonomy( 'category', 'post', array(
-		'hierarchical' => true,
-		'query_var' => 'category_name',
-		'rewrite' => $rewrite['category'],
-		'public' => true,
-		'show_ui' => true,
-		'show_admin_column' => true,
-		'_builtin' => true,
-		'capabilities' => array(
-			'manage_terms' => 'manage_categories',
-			'edit_terms'   => 'edit_categories',
-			'delete_terms' => 'delete_categories',
-			'assign_terms' => 'assign_categories',
-		),
-		'show_in_rest' => true,
-		'rest_base' => 'categories',
-		'rest_controller_class' => 'WP_REST_Terms_Controller',
-	) );
-
-	register_taxonomy( 'post_tag', 'post', array(
-	 	'hierarchical' => false,
-		'query_var' => 'tag',
-		'rewrite' => $rewrite['post_tag'],
-		'public' => true,
-		'show_ui' => true,
-		'show_admin_column' => true,
-		'_builtin' => true,
-		'capabilities' => array(
-			'manage_terms' => 'manage_post_tags',
-			'edit_terms'   => 'edit_post_tags',
-			'delete_terms' => 'delete_post_tags',
-			'assign_terms' => 'assign_post_tags',
-		),
-		'show_in_rest' => true,
-		'rest_base' => 'tags',
-		'rest_controller_class' => 'WP_REST_Terms_Controller',
-	) );
-
-	register_taxonomy( 'nav_menu', 'nav_menu_item', array(
-		'public' => false,
-		'hierarchical' => false,
-		'labels' => array(
-			'name' => __( 'Navigation Menus' ),
-			'singular_name' => __( 'Navigation Menu' ),
-		),
-		'query_var' => false,
-		'rewrite' => false,
-		'show_ui' => false,
-		'_builtin' => true,
-		'show_in_nav_menus' => false,
-	) );
-
-	register_taxonomy( 'link_category', 'link', array(
-		'hierarchical' => false,
-		'labels' => array(
-			'name' => __( 'Link Categories' ),
-			'singular_name' => __( 'Link Category' ),
-			'search_items' => __( 'Search Link Categories' ),
-			'popular_items' => null,
-			'all_items' => __( 'All Link Categories' ),
-			'edit_item' => __( 'Edit Link Category' ),
-			'update_item' => __( 'Update Link Category' ),
-			'add_new_item' => __( 'Add New Link Category' ),
-			'new_item_name' => __( 'New Link Category Name' ),
-			'separate_items_with_commas' => null,
-			'add_or_remove_items' => null,
-			'choose_from_most_used' => null,
-			'back_to_items' => __( '← Back to Link Categories' ),
-		),
-		'capabilities' => array(
-			'manage_terms' => 'manage_links',
-			'edit_terms'   => 'manage_links',
-			'delete_terms' => 'manage_links',
-			'assign_terms' => 'manage_links',
-		),
-		'query_var' => false,
-		'rewrite' => false,
-		'public' => false,
-		'show_ui' => true,
-		'_builtin' => true,
-	) );
-
-	register_taxonomy( 'post_format', 'post', array(
-		'public' => true,
-		'hierarchical' => false,
-		'labels' => array(
-			'name' => _x( 'Format', 'post format' ),
-			'singular_name' => _x( 'Format', 'post format' ),
-		),
-		'query_var' => true,
-		'rewrite' => $rewrite['post_format'],
-		'show_ui' => false,
-		'_builtin' => true,
-		'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
-	) );
+	register_taxonomy(
+		'category',
+		'post',
+		array(
+			'hierarchical'          => true,
+			'query_var'             => 'category_name',
+			'rewrite'               => $rewrite['category'],
+			'public'                => true,
+			'show_ui'               => true,
+			'show_admin_column'     => true,
+			'_builtin'              => true,
+			'capabilities'          => array(
+				'manage_terms' => 'manage_categories',
+				'edit_terms'   => 'edit_categories',
+				'delete_terms' => 'delete_categories',
+				'assign_terms' => 'assign_categories',
+			),
+			'show_in_rest'          => true,
+			'rest_base'             => 'categories',
+			'rest_controller_class' => 'WP_REST_Terms_Controller',
+		)
+	);
+
+	register_taxonomy(
+		'post_tag',
+		'post',
+		array(
+			'hierarchical'          => false,
+			'query_var'             => 'tag',
+			'rewrite'               => $rewrite['post_tag'],
+			'public'                => true,
+			'show_ui'               => true,
+			'show_admin_column'     => true,
+			'_builtin'              => true,
+			'capabilities'          => array(
+				'manage_terms' => 'manage_post_tags',
+				'edit_terms'   => 'edit_post_tags',
+				'delete_terms' => 'delete_post_tags',
+				'assign_terms' => 'assign_post_tags',
+			),
+			'show_in_rest'          => true,
+			'rest_base'             => 'tags',
+			'rest_controller_class' => 'WP_REST_Terms_Controller',
+		)
+	);
+
+	register_taxonomy(
+		'nav_menu',
+		'nav_menu_item',
+		array(
+			'public'            => false,
+			'hierarchical'      => false,
+			'labels'            => array(
+				'name'          => __( 'Navigation Menus' ),
+				'singular_name' => __( 'Navigation Menu' ),
+			),
+			'query_var'         => false,
+			'rewrite'           => false,
+			'show_ui'           => false,
+			'_builtin'          => true,
+			'show_in_nav_menus' => false,
+		)
+	);
+
+	register_taxonomy(
+		'link_category',
+		'link',
+		array(
+			'hierarchical' => false,
+			'labels'       => array(
+				'name'                       => __( 'Link Categories' ),
+				'singular_name'              => __( 'Link Category' ),
+				'search_items'               => __( 'Search Link Categories' ),
+				'popular_items'              => null,
+				'all_items'                  => __( 'All Link Categories' ),
+				'edit_item'                  => __( 'Edit Link Category' ),
+				'update_item'                => __( 'Update Link Category' ),
+				'add_new_item'               => __( 'Add New Link Category' ),
+				'new_item_name'              => __( 'New Link Category Name' ),
+				'separate_items_with_commas' => null,
+				'add_or_remove_items'        => null,
+				'choose_from_most_used'      => null,
+				'back_to_items'              => __( '← Back to Link Categories' ),
+			),
+			'capabilities' => array(
+				'manage_terms' => 'manage_links',
+				'edit_terms'   => 'manage_links',
+				'delete_terms' => 'manage_links',
+				'assign_terms' => 'manage_links',
+			),
+			'query_var'    => false,
+			'rewrite'      => false,
+			'public'       => false,
+			'show_ui'      => true,
+			'_builtin'     => true,
+		)
+	);
+
+	register_taxonomy(
+		'post_format',
+		'post',
+		array(
+			'public'            => true,
+			'hierarchical'      => false,
+			'labels'            => array(
+				'name'          => _x( 'Formats', 'post format' ),
+				'singular_name' => _x( 'Format', 'post format' ),
+			),
+			'query_var'         => true,
+			'rewrite'           => $rewrite['post_format'],
+			'show_ui'           => false,
+			'_builtin'          => true,
+			'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
+		)
+	);
 }
 
 /**
@@ -164,14 +188,14 @@
  * @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only
  *                         one element from the array needs to match; 'and' means all elements must match.
  *                         Default 'and'.
- * @return array A list of taxonomy names or objects.
+ * @return string[]|WP_Taxonomy[] An array of taxonomy names or objects.
  */
 function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
 	global $wp_taxonomies;
 
-	$field = ('names' == $output) ? 'name' : false;
-
-	return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
+	$field = ( 'names' == $output ) ? 'name' : false;
+
+	return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
 }
 
 /**
@@ -198,9 +222,10 @@
 function get_object_taxonomies( $object, $output = 'names' ) {
 	global $wp_taxonomies;
 
-	if ( is_object($object) ) {
-		if ( $object->post_type == 'attachment' )
+	if ( is_object( $object ) ) {
+		if ( $object->post_type == 'attachment' ) {
 			return get_attachment_taxonomies( $object, $output );
+		}
 		$object = $object->post_type;
 	}
 
@@ -208,11 +233,12 @@
 
 	$taxonomies = array();
 	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
-		if ( array_intersect($object, (array) $tax_obj->object_type) ) {
-			if ( 'names' == $output )
+		if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
+			if ( 'names' == $output ) {
 				$taxonomies[] = $tax_name;
-			else
+			} else {
 				$taxonomies[ $tax_name ] = $tax_obj;
+			}
 		}
 	}
 
@@ -235,17 +261,22 @@
 function get_taxonomy( $taxonomy ) {
 	global $wp_taxonomies;
 
-	if ( ! taxonomy_exists( $taxonomy ) )
+	if ( ! taxonomy_exists( $taxonomy ) ) {
 		return false;
-
-	return $wp_taxonomies[$taxonomy];
+	}
+
+	return $wp_taxonomies[ $taxonomy ];
 }
 
 /**
- * Checks that the taxonomy name exists.
+ * Determines whether the taxonomy name exists.
  *
  * Formerly is_taxonomy(), introduced in 2.3.0.
  *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
  * @since 3.0.0
  *
  * @global array $wp_taxonomies The registered taxonomies.
@@ -256,27 +287,32 @@
 function taxonomy_exists( $taxonomy ) {
 	global $wp_taxonomies;
 
-	return isset( $wp_taxonomies[$taxonomy] );
+	return isset( $wp_taxonomies[ $taxonomy ] );
 }
 
 /**
- * Whether the taxonomy object is hierarchical.
+ * Determines whether the taxonomy object is hierarchical.
  *
  * Checks to make sure that the taxonomy is an object first. Then Gets the
  * object, and finally returns the hierarchical value in the object.
  *
  * A false return value might also mean that the taxonomy does not exist.
  *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
  * @since 2.3.0
  *
  * @param string $taxonomy Name of taxonomy object.
  * @return bool Whether the taxonomy is hierarchical.
  */
-function is_taxonomy_hierarchical($taxonomy) {
-	if ( ! taxonomy_exists($taxonomy) )
+function is_taxonomy_hierarchical( $taxonomy ) {
+	if ( ! taxonomy_exists( $taxonomy ) ) {
 		return false;
-
-	$taxonomy = get_taxonomy($taxonomy);
+	}
+
+	$taxonomy = get_taxonomy( $taxonomy );
 	return $taxonomy->hierarchical;
 }
 
@@ -297,6 +333,7 @@
  * @since 4.5.0 Introduced `publicly_queryable` argument.
  * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
  *              arguments to register the Taxonomy in REST API.
+ * @since 5.1.0 Introduced `meta_box_sanitize_cb` argument.
  *
  * @global array $wp_taxonomies Registered taxonomies.
  *
@@ -339,6 +376,9 @@
  *                                                post_categories_meta_box() is used for hierarchical taxonomies, and
  *                                                post_tags_meta_box() is used for non-hierarchical. If false, no meta
  *                                                box is shown.
+ *     @type callable      $meta_box_sanitize_cb  Callback function for sanitizing taxonomy data saved from a meta
+ *                                                box. If no callback is defined, an appropriate one is determined
+ *                                                based on the value of `$meta_box_cb`.
  *     @type array         $capabilities {
  *         Array of capabilities for this taxonomy.
  *
@@ -372,8 +412,9 @@
 function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
 	global $wp_taxonomies;
 
-	if ( ! is_array( $wp_taxonomies ) )
+	if ( ! is_array( $wp_taxonomies ) ) {
 		$wp_taxonomies = array();
+	}
 
 	$args = wp_parse_args( $args );
 
@@ -389,7 +430,6 @@
 
 	$taxonomy_object->add_hooks();
 
-
 	/**
 	 * Fires after a taxonomy is registered.
 	 *
@@ -495,35 +535,37 @@
 function get_taxonomy_labels( $tax ) {
 	$tax->labels = (array) $tax->labels;
 
-	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
+	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
 		$tax->labels['separate_items_with_commas'] = $tax->helps;
-
-	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) )
+	}
+
+	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
 		$tax->labels['not_found'] = $tax->no_tagcloud;
+	}
 
 	$nohier_vs_hier_defaults = array(
-		'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
-		'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
-		'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
-		'popular_items' => array( __( 'Popular Tags' ), null ),
-		'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
-		'parent_item' => array( null, __( 'Parent Category' ) ),
-		'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
-		'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
-		'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
-		'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
-		'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
-		'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
+		'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
+		'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
+		'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
+		'popular_items'              => array( __( 'Popular Tags' ), null ),
+		'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
+		'parent_item'                => array( null, __( 'Parent Category' ) ),
+		'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
+		'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
+		'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
+		'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
+		'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
+		'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
 		'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
-		'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
-		'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
-		'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
-		'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ),
-		'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
-		'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ),
+		'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
+		'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
+		'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
+		'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
+		'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
+		'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
 		/* translators: Tab heading when selecting from the most used terms */
-		'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
-		'back_to_items' => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ),
+		'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
+		'back_to_items'              => array( __( '← Back to Tags' ), __( '← Back to Categories' ) ),
 	);
 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
 
@@ -563,21 +605,34 @@
  * @param string $object_type Name of the object type.
  * @return bool True if successful, false if not.
  */
-function register_taxonomy_for_object_type( $taxonomy, $object_type) {
+function register_taxonomy_for_object_type( $taxonomy, $object_type ) {
 	global $wp_taxonomies;
 
-	if ( !isset($wp_taxonomies[$taxonomy]) )
+	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
 		return false;
-
-	if ( ! get_post_type_object($object_type) )
+	}
+
+	if ( ! get_post_type_object( $object_type ) ) {
 		return false;
-
-	if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
-		$wp_taxonomies[$taxonomy]->object_type[] = $object_type;
+	}
+
+	if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type ) ) {
+		$wp_taxonomies[ $taxonomy ]->object_type[] = $object_type;
+	}
 
 	// Filter out empties.
 	$wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type );
 
+	/**
+	 * Fires after a taxonomy is registered for an object type.
+	 *
+	 * @since 5.1.0
+	 *
+	 * @param string $taxonomy    Taxonomy name.
+	 * @param string $object_type Name of the object type.
+	 */
+	do_action( 'registered_taxonomy_for_object_type', $taxonomy, $object_type );
+
 	return true;
 }
 
@@ -595,17 +650,31 @@
 function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
 	global $wp_taxonomies;
 
-	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
+	if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
 		return false;
-
-	if ( ! get_post_type_object( $object_type ) )
+	}
+
+	if ( ! get_post_type_object( $object_type ) ) {
 		return false;
+	}
 
 	$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
-	if ( false === $key )
+	if ( false === $key ) {
 		return false;
+	}
 
 	unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
+
+	/**
+	 * Fires after a taxonomy is unregistered for an object type.
+	 *
+	 * @since 5.1.0
+	 *
+	 * @param string $taxonomy    Taxonomy name.
+	 * @param string $object_type Name of the object type.
+	 */
+	do_action( 'unregistered_taxonomy_for_object_type', $taxonomy, $object_type );
+
 	return true;
 }
 
@@ -636,7 +705,7 @@
  * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
  * @param array|string $args       Change the order of the object_ids, either ASC or DESC.
  * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success.
- *	the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
+ *  the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
  */
 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
 	global $wpdb;
@@ -654,20 +723,20 @@
 	}
 
 	$defaults = array( 'order' => 'ASC' );
-	$args = wp_parse_args( $args, $defaults );
+	$args     = wp_parse_args( $args, $defaults );
 
 	$order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
 
-	$term_ids = array_map('intval', $term_ids );
+	$term_ids = array_map( 'intval', $term_ids );
 
 	$taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
-	$term_ids = "'" . implode( "', '", $term_ids ) . "'";
+	$term_ids   = "'" . implode( "', '", $term_ids ) . "'";
 
 	$sql = "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";
 
 	$last_changed = wp_cache_get_last_changed( 'terms' );
-	$cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
-	$cache = wp_cache_get( $cache_key, 'terms' );
+	$cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
+	$cache        = wp_cache_get( $cache_key, 'terms' );
 	if ( false === $cache ) {
 		$object_ids = $wpdb->get_col( $sql );
 		wp_cache_set( $cache_key, $object_ids, 'terms' );
@@ -675,7 +744,7 @@
 		$object_ids = (array) $cache;
 	}
 
-	if ( ! $object_ids ){
+	if ( ! $object_ids ) {
 		return array();
 	}
 	return $object_ids;
@@ -771,28 +840,31 @@
 		return null;
 	}
 
+	// Ensure for filters that this is not empty.
+	$taxonomy = $_term->taxonomy;
+
 	/**
-	 * Filters a term.
+	 * Filters a taxonomy term object.
 	 *
 	 * @since 2.3.0
-	 * @since 4.4.0 `$_term` can now also be a WP_Term object.
+	 * @since 4.4.0 `$_term` is now a `WP_Term` object.
 	 *
-	 * @param int|WP_Term $_term    Term object or ID.
-	 * @param string      $taxonomy The taxonomy slug.
+	 * @param WP_Term $_term    Term object.
+	 * @param string  $taxonomy The taxonomy slug.
 	 */
 	$_term = apply_filters( 'get_term', $_term, $taxonomy );
 
 	/**
-	 * Filters a taxonomy.
+	 * Filters a taxonomy term object.
 	 *
 	 * The dynamic portion of the filter name, `$taxonomy`, refers
-	 * to the taxonomy slug.
+	 * to the slug of the term's taxonomy.
 	 *
 	 * @since 2.3.0
-	 * @since 4.4.0 `$_term` can now also be a WP_Term object.
+	 * @since 4.4.0 `$_term` is now a `WP_Term` object.
 	 *
-	 * @param int|WP_Term $_term    Term object or ID.
-	 * @param string      $taxonomy The taxonomy slug.
+	 * @param WP_Term $_term    Term object.
+	 * @param string  $taxonomy The taxonomy slug.
 	 */
 	$_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );
 
@@ -883,17 +955,17 @@
 	);
 
 	switch ( $field ) {
-		case 'slug' :
+		case 'slug':
 			$args['slug'] = $value;
 			break;
-		case 'name' :
+		case 'name':
 			$args['name'] = $value;
 			break;
-		case 'term_taxonomy_id' :
+		case 'term_taxonomy_id':
 			$args['term_taxonomy_id'] = $value;
-			unset( $args[ 'taxonomy' ] );
+			unset( $args['taxonomy'] );
 			break;
-		default :
+		default:
 			return false;
 	}
 
@@ -933,20 +1005,22 @@
 
 	$term_id = intval( $term_id );
 
-	$terms = _get_term_hierarchy($taxonomy);
-
-	if ( ! isset($terms[$term_id]) )
+	$terms = _get_term_hierarchy( $taxonomy );
+
+	if ( ! isset( $terms[ $term_id ] ) ) {
 		return array();
-
-	$children = $terms[$term_id];
-
-	foreach ( (array) $terms[$term_id] as $child ) {
+	}
+
+	$children = $terms[ $term_id ];
+
+	foreach ( (array) $terms[ $term_id ] as $child ) {
 		if ( $term_id == $child ) {
 			continue;
 		}
 
-		if ( isset($terms[$child]) )
-			$children = array_merge($children, get_term_children($child, $taxonomy));
+		if ( isset( $terms[ $child ] ) ) {
+			$children = array_merge( $children, get_term_children( $child, $taxonomy ) );
+		}
 	}
 
 	return $children;
@@ -970,14 +1044,17 @@
  */
 function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
 	$term = get_term( $term, $taxonomy );
-	if ( is_wp_error($term) )
+	if ( is_wp_error( $term ) ) {
 		return $term;
-
-	if ( !is_object($term) )
+	}
+
+	if ( ! is_object( $term ) ) {
 		return '';
-
-	if ( !isset($term->$field) )
+	}
+
+	if ( ! isset( $term->$field ) ) {
 		return '';
+	}
 
 	return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
 }
@@ -997,13 +1074,15 @@
 function get_term_to_edit( $id, $taxonomy ) {
 	$term = get_term( $id, $taxonomy );
 
-	if ( is_wp_error($term) )
+	if ( is_wp_error( $term ) ) {
 		return $term;
-
-	if ( !is_object($term) )
+	}
+
+	if ( ! is_object( $term ) ) {
 		return '';
-
-	return sanitize_term($term, $taxonomy, 'edit');
+	}
+
+	return sanitize_term( $term, $taxonomy, 'edit' );
 }
 
 /**
@@ -1069,13 +1148,13 @@
 	 * (a) a second non-empty parameter is passed, or
 	 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
 	 */
-	$_args = wp_parse_args( $args );
+	$_args          = wp_parse_args( $args );
 	$key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
 	$do_legacy_args = $deprecated || empty( $key_intersect );
 
 	if ( $do_legacy_args ) {
-		$taxonomies = (array) $args;
-		$args = wp_parse_args( $deprecated, $defaults );
+		$taxonomies       = (array) $args;
+		$args             = wp_parse_args( $deprecated, $defaults );
 		$args['taxonomy'] = $taxonomies;
 	} else {
 		$args = wp_parse_args( $args, $defaults );
@@ -1135,23 +1214,11 @@
  *                           False on failure.
  */
 function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return false;
-	}
-
 	if ( wp_term_is_shared( $term_id ) ) {
-		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
+		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
 	}
 
-	$added = add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
-
-	// Bust term query cache.
-	if ( $added ) {
-		wp_cache_set( 'last_changed', microtime(), 'terms' );
-	}
-
-	return $added;
+	return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
 }
 
 /**
@@ -1165,19 +1232,7 @@
  * @return bool True on success, false on failure.
  */
 function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return false;
-	}
-
-	$deleted = delete_metadata( 'term', $term_id, $meta_key, $meta_value );
-
-	// Bust term query cache.
-	if ( $deleted ) {
-		wp_cache_set( 'last_changed', microtime(), 'terms' );
-	}
-
-	return $deleted;
+	return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
 }
 
 /**
@@ -1192,11 +1247,6 @@
  * @return mixed If `$single` is false, an array of metadata values. If `$single` is true, a single metadata value.
  */
 function get_term_meta( $term_id, $key = '', $single = false ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return false;
-	}
-
 	return get_metadata( 'term', $term_id, $key, $single );
 }
 
@@ -1217,23 +1267,11 @@
  *                           WP_Error when term_id is ambiguous between taxonomies. False on failure.
  */
 function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return false;
-	}
-
 	if ( wp_term_is_shared( $term_id ) ) {
-		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.'), $term_id );
+		return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
 	}
 
-	$updated = update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
-
-	// Bust term query cache.
-	if ( $updated ) {
-		wp_cache_set( 'last_changed', microtime(), 'terms' );
-	}
-
-	return $updated;
+	return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
 }
 
 /**
@@ -1248,11 +1286,6 @@
  * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
  */
 function update_termmeta_cache( $term_ids ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return;
-	}
-
 	return update_meta_cache( 'term', $term_ids );
 }
 
@@ -1267,9 +1300,9 @@
  * @return array|false Array with meta data, or false when the meta table is not installed.
  */
 function has_term_meta( $term_id ) {
-	// Bail if term meta table is not installed.
-	if ( get_option( 'db_version' ) < 34370 ) {
-		return false;
+	$check = wp_check_term_meta_support_prefilter( null );
+	if ( null !== $check ) {
+		return $check;
 	}
 
 	global $wpdb;
@@ -1311,10 +1344,14 @@
 }
 
 /**
- * Check if Term exists.
+ * Determines whether a term exists.
  *
  * Formerly is_term(), introduced in 2.3.0.
  *
+ * For more information on this and similar theme functions, check out
+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
+ * Conditional Tags} article in the Theme Developer Handbook.
+ *
  * @since 3.0.0
  *
  * @global wpdb $wpdb WordPress database abstraction object.
@@ -1324,56 +1361,60 @@
  * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
  * @return mixed Returns null if the term does not exist. Returns the term ID
  *               if no taxonomy is specified and the term ID exists. Returns
- *               an array of the term ID and the term taxonomy ID the taxonomy
+ *               an array of the term ID and the term taxonomy ID if the taxonomy
  *               is specified and the pairing exists.
  */
 function term_exists( $term, $taxonomy = '', $parent = null ) {
 	global $wpdb;
 
-	$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
+	$select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
 	$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 ";
 
-	if ( is_int($term) ) {
-		if ( 0 == $term )
+	if ( is_int( $term ) ) {
+		if ( 0 == $term ) {
 			return 0;
+		}
 		$where = 't.term_id = %d';
-		if ( !empty($taxonomy) )
-			return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
-		else
+		if ( ! empty( $taxonomy ) ) {
+			return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
+		} else {
 			return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
+		}
 	}
 
 	$term = trim( wp_unslash( $term ) );
 	$slug = sanitize_title( $term );
 
-	$where = 't.slug = %s';
-	$else_where = 't.name = %s';
-	$where_fields = array($slug);
-	$else_where_fields = array($term);
-	$orderby = 'ORDER BY t.term_id ASC';
-	$limit = 'LIMIT 1';
-	if ( !empty($taxonomy) ) {
+	$where             = 't.slug = %s';
+	$else_where        = 't.name = %s';
+	$where_fields      = array( $slug );
+	$else_where_fields = array( $term );
+	$orderby           = 'ORDER BY t.term_id ASC';
+	$limit             = 'LIMIT 1';
+	if ( ! empty( $taxonomy ) ) {
 		if ( is_numeric( $parent ) ) {
-			$parent = (int) $parent;
-			$where_fields[] = $parent;
+			$parent              = (int) $parent;
+			$where_fields[]      = $parent;
 			$else_where_fields[] = $parent;
-			$where .= ' AND tt.parent = %d';
-			$else_where .= ' AND tt.parent = %d';
+			$where              .= ' AND tt.parent = %d';
+			$else_where         .= ' AND tt.parent = %d';
 		}
 
-		$where_fields[] = $taxonomy;
+		$where_fields[]      = $taxonomy;
 		$else_where_fields[] = $taxonomy;
 
-		if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields), ARRAY_A) )
+		if ( $result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields ), ARRAY_A ) ) {
 			return $result;
-
-		return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields), ARRAY_A);
+		}
+
+		return $wpdb->get_row( $wpdb->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields ), ARRAY_A );
 	}
 
-	if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
+	if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) {
 		return $result;
-
-	return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
+	}
+
+	return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
 }
 
 /**
@@ -1389,15 +1430,19 @@
  * @return bool Whether `$term2` is a child of `$term1`.
  */
 function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
-	if ( ! isset( $term1->term_id ) )
+	if ( ! isset( $term1->term_id ) ) {
 		$term1 = get_term( $term1, $taxonomy );
-	if ( ! isset( $term2->parent ) )
+	}
+	if ( ! isset( $term2->parent ) ) {
 		$term2 = get_term( $term2, $taxonomy );
-
-	if ( empty( $term1->term_id ) || empty( $term2->parent ) )
+	}
+
+	if ( empty( $term1->term_id ) || empty( $term2->parent ) ) {
 		return false;
-	if ( $term2->parent == $term1->term_id )
+	}
+	if ( $term2->parent == $term1->term_id ) {
 		return true;
+	}
 
 	return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
 }
@@ -1419,27 +1464,30 @@
  *                               'display', 'attribute', or 'js'. Default 'display'.
  * @return array|object Term with all fields sanitized.
  */
-function sanitize_term($term, $taxonomy, $context = 'display') {
+function sanitize_term( $term, $taxonomy, $context = 'display' ) {
 	$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
 
 	$do_object = is_object( $term );
 
-	$term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
+	$term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );
 
 	foreach ( (array) $fields as $field ) {
 		if ( $do_object ) {
-			if ( isset($term->$field) )
-				$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
+			if ( isset( $term->$field ) ) {
+				$term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
+			}
 		} else {
-			if ( isset($term[$field]) )
-				$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
+			if ( isset( $term[ $field ] ) ) {
+				$term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context );
+			}
 		}
 	}
 
-	if ( $do_object )
+	if ( $do_object ) {
 		$term->filter = $context;
-	else
+	} else {
 		$term['filter'] = $context;
+	}
 
 	return $term;
 }
@@ -1467,16 +1515,18 @@
  *                         'attribute', or 'js'.
  * @return mixed Sanitized field.
  */
-function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
+function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
 	$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
 	if ( in_array( $field, $int_fields ) ) {
 		$value = (int) $value;
-		if ( $value < 0 )
+		if ( $value < 0 ) {
 			$value = 0;
+		}
 	}
 
-	if ( 'raw' == $context )
+	if ( 'raw' == $context ) {
 		return $value;
+	}
 
 	if ( 'edit' == $context ) {
 
@@ -1506,10 +1556,11 @@
 		 */
 		$value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );
 
-		if ( 'description' == $field )
-			$value = esc_html($value); // textarea_escaped
-		else
-			$value = esc_attr($value);
+		if ( 'description' == $field ) {
+			$value = esc_html( $value ); // textarea_escaped
+		} else {
+			$value = esc_attr( $value );
+		}
 	} elseif ( 'db' == $context ) {
 
 		/**
@@ -1549,7 +1600,6 @@
 			 */
 			$value = apply_filters( 'pre_category_nicename', $value );
 		}
-
 	} elseif ( 'rss' == $context ) {
 
 		/**
@@ -1608,9 +1658,9 @@
 	}
 
 	if ( 'attribute' == $context ) {
-		$value = esc_attr($value);
+		$value = esc_attr( $value );
 	} elseif ( 'js' == $context ) {
-		$value = esc_js($value);
+		$value = esc_js( $value );
 	}
 	return $value;
 }
@@ -1628,18 +1678,18 @@
  * @return array|int|WP_Error Number of terms in that taxonomy or WP_Error if the taxonomy does not exist.
  */
 function wp_count_terms( $taxonomy, $args = array() ) {
-	$defaults = array('hide_empty' => false);
-	$args = wp_parse_args($args, $defaults);
+	$defaults = array( 'hide_empty' => false );
+	$args     = wp_parse_args( $args, $defaults );
 
 	// backward compatibility
-	if ( isset($args['ignore_empty']) ) {
+	if ( isset( $args['ignore_empty'] ) ) {
 		$args['hide_empty'] = $args['ignore_empty'];
-		unset($args['ignore_empty']);
+		unset( $args['ignore_empty'] );
 	}
 
 	$args['fields'] = 'count';
 
-	return get_terms($taxonomy, $args);
+	return get_terms( $taxonomy, $args );
 }
 
 /**
@@ -1657,8 +1707,9 @@
 function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
 	$object_id = (int) $object_id;
 
-	if ( !is_array($taxonomies) )
-		$taxonomies = array($taxonomies);
+	if ( ! is_array( $taxonomies ) ) {
+		$taxonomies = array( $taxonomies );
+	}
 
 	foreach ( (array) $taxonomies as $taxonomy ) {
 		$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
@@ -1699,10 +1750,12 @@
 
 	$term = (int) $term;
 
-	if ( ! $ids = term_exists($term, $taxonomy) )
+	if ( ! $ids = term_exists( $term, $taxonomy ) ) {
 		return false;
-	if ( is_wp_error( $ids ) )
+	}
+	if ( is_wp_error( $ids ) ) {
 		return $ids;
+	}
 
 	$tt_id = $ids['term_taxonomy_id'];
 
@@ -1710,11 +1763,12 @@
 
 	if ( 'category' == $taxonomy ) {
 		$defaults['default'] = get_option( 'default_category' );
-		if ( $defaults['default'] == $term )
+		if ( $defaults['default'] == $term ) {
 			return 0; // Don't delete the default category
+		}
 	}
 
-	$args = wp_parse_args($args, $defaults);
+	$args = wp_parse_args( $args, $defaults );
 
 	if ( isset( $args['default'] ) ) {
 		$default = (int) $args['default'];
@@ -1738,13 +1792,14 @@
 	do_action( 'pre_delete_term', $term, $taxonomy );
 
 	// Update children to point to new parent
-	if ( is_taxonomy_hierarchical($taxonomy) ) {
-		$term_obj = get_term($term, $taxonomy);
-		if ( is_wp_error( $term_obj ) )
+	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
+		$term_obj = get_term( $term, $taxonomy );
+		if ( is_wp_error( $term_obj ) ) {
 			return $term_obj;
+		}
 		$parent = $term_obj->parent;
 
-		$edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
+		$edit_ids    = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id );
 		$edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
 
 		/**
@@ -1756,7 +1811,7 @@
 		 */
 		do_action( 'edit_term_taxonomies', $edit_tt_ids );
 
-		$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
+		$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
 
 		// Clean the cache for all child terms.
 		$edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' );
@@ -1778,22 +1833,31 @@
 	$object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
 
 	foreach ( $object_ids as $object_id ) {
-		$terms = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids', 'orderby' => 'none' ) );
-		if ( 1 == count($terms) && isset($default) ) {
-			$terms = array($default);
+		$terms = wp_get_object_terms(
+			$object_id,
+			$taxonomy,
+			array(
+				'fields'  => 'ids',
+				'orderby' => 'none',
+			)
+		);
+		if ( 1 == count( $terms ) && isset( $default ) ) {
+			$terms = array( $default );
 		} else {
-			$terms = array_diff($terms, array($term));
-			if (isset($default) && isset($force_default) && $force_default)
-				$terms = array_merge($terms, array($default));
+			$terms = array_diff( $terms, array( $term ) );
+			if ( isset( $default ) && isset( $force_default ) && $force_default ) {
+				$terms = array_merge( $terms, array( $default ) );
+			}
 		}
-		$terms = array_map('intval', $terms);
+		$terms = array_map( 'intval', $terms );
 		wp_set_object_terms( $object_id, $terms, $taxonomy );
 	}
 
 	// Clean the relationship caches for all object types using this term.
 	$tax_object = get_taxonomy( $taxonomy );
-	foreach ( $tax_object->object_type as $object_type )
+	foreach ( $tax_object->object_type as $object_type ) {
 		clean_object_term_cache( $object_ids, $object_type );
+	}
 
 	$term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
 	foreach ( $term_meta_ids as $mid ) {
@@ -1820,10 +1884,11 @@
 	do_action( 'deleted_term_taxonomy', $tt_id );
 
 	// Delete the term if no taxonomies use it.
-	if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
+	if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) {
 		$wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
-
-	clean_term_cache($term, $taxonomy);
+	}
+
+	clean_term_cache( $term, $taxonomy );
 
 	/**
 	 * Fires after a term is deleted from the database and the cache is cleaned.
@@ -1867,7 +1932,7 @@
  *
  * @param int $cat_ID Category term ID.
  * @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist;
- * 	Zero on attempted deletion of default Category; WP_Error object is also a possibility.
+ *  Zero on attempted deletion of default Category; WP_Error object is also a possibility.
  */
 function wp_delete_category( $cat_ID ) {
 	return wp_delete_term( $cat_ID, 'category' );
@@ -1889,21 +1954,25 @@
  * @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.
  */
-function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
-	if ( empty( $object_ids ) || empty( $taxonomies ) )
+function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
+	if ( empty( $object_ids ) || empty( $taxonomies ) ) {
 		return array();
-
-	if ( !is_array($taxonomies) )
-		$taxonomies = array($taxonomies);
+	}
+
+	if ( ! is_array( $taxonomies ) ) {
+		$taxonomies = array( $taxonomies );
+	}
 
 	foreach ( $taxonomies as $taxonomy ) {
-		if ( ! taxonomy_exists($taxonomy) )
+		if ( ! taxonomy_exists( $taxonomy ) ) {
 			return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
+		}
 	}
 
-	if ( !is_array($object_ids) )
-		$object_ids = array($object_ids);
-	$object_ids = array_map('intval', $object_ids);
+	if ( ! is_array( $object_ids ) ) {
+		$object_ids = array( $object_ids );
+	}
+	$object_ids = array_map( 'intval', $object_ids );
 
 	$args = wp_parse_args( $args );
 
@@ -1939,7 +2008,7 @@
 		}
 	}
 
-	$args['taxonomy'] = $taxonomies;
+	$args['taxonomy']   = $taxonomies;
 	$args['object_ids'] = $object_ids;
 
 	// Taxonomies registered without an 'args' param are handled here.
@@ -2033,7 +2102,7 @@
 function wp_insert_term( $term, $taxonomy, $args = array() ) {
 	global $wpdb;
 
-	if ( ! taxonomy_exists($taxonomy) ) {
+	if ( ! taxonomy_exists( $taxonomy ) ) {
 		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 	}
 	/**
@@ -2054,25 +2123,30 @@
 	if ( '' == trim( $term ) ) {
 		return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
 	}
-	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
-	$args = wp_parse_args( $args, $defaults );
+	$defaults = array(
+		'alias_of'    => '',
+		'description' => '',
+		'parent'      => 0,
+		'slug'        => '',
+	);
+	$args     = wp_parse_args( $args, $defaults );
 
 	if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
 		return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
 	}
 
-	$args['name'] = $term;
+	$args['name']     = $term;
 	$args['taxonomy'] = $taxonomy;
 
 	// Coerce null description to strings, to avoid database errors.
 	$args['description'] = (string) $args['description'];
 
-	$args = sanitize_term($args, $taxonomy, 'db');
+	$args = sanitize_term( $args, $taxonomy, 'db' );
 
 	// expected_slashed ($name)
-	$name = wp_unslash( $args['name'] );
+	$name        = wp_unslash( $args['name'] );
 	$description = wp_unslash( $args['description'] );
-	$parent = (int) $args['parent'];
+	$parent      = (int) $args['parent'];
 
 	$slug_provided = ! empty( $args['slug'] );
 	if ( ! $slug_provided ) {
@@ -2092,11 +2166,15 @@
 			 * The alias is not in a group, so we create a new one
 			 * and add the alias to it.
 			 */
-			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
-
-			wp_update_term( $alias->term_id, $taxonomy, array(
-				'term_group' => $term_group,
-			) );
+			$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
+
+			wp_update_term(
+				$alias->term_id,
+				$taxonomy,
+				array(
+					'term_group' => $term_group,
+				)
+			);
 		}
 	}
 
@@ -2104,11 +2182,15 @@
 	 * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
 	 * unless a unique slug has been explicitly provided.
 	 */
-	$name_matches = get_terms( $taxonomy, array(
-		'name' => $name,
-		'hide_empty' => false,
-		'parent' => $args['parent'],
-	) );
+	$name_matches = get_terms(
+		$taxonomy,
+		array(
+			'name'                   => $name,
+			'hide_empty'             => false,
+			'parent'                 => $args['parent'],
+			'update_term_meta_cache' => false,
+		)
+	);
 
 	/*
 	 * The `name` match in `get_terms()` doesn't differentiate accented characters,
@@ -2128,7 +2210,14 @@
 		$slug_match = get_term_by( 'slug', $slug, $taxonomy );
 		if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
 			if ( is_taxonomy_hierarchical( $taxonomy ) ) {
-				$siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
+				$siblings = get_terms(
+					$taxonomy,
+					array(
+						'get'                    => 'all',
+						'parent'                 => $parent,
+						'update_term_meta_cache' => false,
+					)
+				);
 
 				$existing_term = null;
 				if ( ( ! $slug_provided || $name_match->slug === $slug ) && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
@@ -2168,8 +2257,8 @@
 	$term_id = (int) $wpdb->insert_id;
 
 	// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
-	if ( empty($slug) ) {
-		$slug = sanitize_title($slug, $term_id);
+	if ( empty( $slug ) ) {
+		$slug = sanitize_title( $slug, $term_id );
 
 		/** This action is documented in wp-includes/taxonomy.php */
 		do_action( 'edit_terms', $term_id, $taxonomy );
@@ -2181,10 +2270,13 @@
 
 	$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 ) );
 
-	if ( !empty($tt_id) ) {
-		return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
+	if ( ! empty( $tt_id ) ) {
+		return array(
+			'term_id'          => $term_id,
+			'term_taxonomy_id' => $tt_id,
+		);
 	}
-	$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
+	$wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) );
 	$tt_id = (int) $wpdb->insert_id;
 
 	/*
@@ -2193,7 +2285,26 @@
 	 * and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
 	 * are not fired.
 	 */
-	$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, tt.term_taxonomy_id FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
+	$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, t.slug, tt.term_taxonomy_id, tt.taxonomy FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
+
+	/**
+	 * Filters the duplicate term check that takes place during term creation.
+	 *
+	 * Term parent+taxonomy+slug combinations are meant to be unique, and wp_insert_term()
+	 * performs a last-minute confirmation of this uniqueness before allowing a new term
+	 * to be created. Plugins with different uniqueness requirements may use this filter
+	 * to bypass or modify the duplicate-term check.
+	 *
+	 * @since 5.1.0
+	 *
+	 * @param object $duplicate_term Duplicate term row from terms table, if found.
+	 * @param string $term           Term being inserted.
+	 * @param string $taxonomy       Taxonomy name.
+	 * @param array  $args           Term arguments passed to the function.
+	 * @param int    $tt_id          term_taxonomy_id for the newly created term.
+	 */
+	$duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id );
+
 	if ( $duplicate_term ) {
 		$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
 		$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
@@ -2202,7 +2313,10 @@
 		$tt_id   = (int) $duplicate_term->term_taxonomy_id;
 
 		clean_term_cache( $term_id, $taxonomy );
-		return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
+		return array(
+			'term_id'          => $term_id,
+			'term_taxonomy_id' => $tt_id,
+		);
 	}
 
 	/**
@@ -2214,7 +2328,7 @@
 	 * @param int    $tt_id    Term taxonomy ID.
 	 * @param string $taxonomy Taxonomy slug.
 	 */
-	do_action( "create_term", $term_id, $tt_id, $taxonomy );
+	do_action( 'create_term', $term_id, $tt_id, $taxonomy );
 
 	/**
 	 * Fires after a new term is created for a specific taxonomy.
@@ -2239,7 +2353,7 @@
 	 */
 	$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
 
-	clean_term_cache($term_id, $taxonomy);
+	clean_term_cache( $term_id, $taxonomy );
 
 	/**
 	 * Fires after a new term is created, and after the term cache has been cleaned.
@@ -2265,7 +2379,10 @@
 	 */
 	do_action( "created_{$taxonomy}", $term_id, $tt_id );
 
-	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
+	return array(
+		'term_id'          => $term_id,
+		'term_taxonomy_id' => $tt_id,
+	);
 }
 
 /**
@@ -2300,36 +2417,50 @@
 		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 	}
 
-	if ( !is_array($terms) )
-		$terms = array($terms);
-
-	if ( ! $append )
-		$old_tt_ids =  wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));
-	else
+	if ( ! is_array( $terms ) ) {
+		$terms = array( $terms );
+	}
+
+	if ( ! $append ) {
+		$old_tt_ids = wp_get_object_terms(
+			$object_id,
+			$taxonomy,
+			array(
+				'fields'                 => 'tt_ids',
+				'orderby'                => 'none',
+				'update_term_meta_cache' => false,
+			)
+		);
+	} else {
 		$old_tt_ids = array();
-
-	$tt_ids = array();
-	$term_ids = array();
+	}
+
+	$tt_ids     = array();
+	$term_ids   = array();
 	$new_tt_ids = array();
 
-	foreach ( (array) $terms as $term) {
-		if ( !strlen(trim($term)) )
+	foreach ( (array) $terms as $term ) {
+		if ( ! strlen( trim( $term ) ) ) {
 			continue;
-
-		if ( !$term_info = term_exists($term, $taxonomy) ) {
+		}
+
+		if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
 			// Skip if a non-existent term ID is passed.
-			if ( is_int($term) )
+			if ( is_int( $term ) ) {
 				continue;
-			$term_info = wp_insert_term($term, $taxonomy);
+			}
+			$term_info = wp_insert_term( $term, $taxonomy );
 		}
-		if ( is_wp_error($term_info) )
+		if ( is_wp_error( $term_info ) ) {
 			return $term_info;
+		}
 		$term_ids[] = $term_info['term_id'];
-		$tt_id = $term_info['term_taxonomy_id'];
-		$tt_ids[] = $tt_id;
-
-		if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
+		$tt_id      = $term_info['term_taxonomy_id'];
+		$tt_ids[]   = $tt_id;
+
+		if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) {
 			continue;
+		}
 
 		/**
 		 * Fires immediately before an object-term relationship is added.
@@ -2342,7 +2473,13 @@
 		 * @param string $taxonomy  Taxonomy slug.
 		 */
 		do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy );
-		$wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
+		$wpdb->insert(
+			$wpdb->term_relationships,
+			array(
+				'object_id'        => $object_id,
+				'term_taxonomy_id' => $tt_id,
+			)
+		);
 
 		/**
 		 * Fires immediately after an object-term relationship is added.
@@ -2358,16 +2495,17 @@
 		$new_tt_ids[] = $tt_id;
 	}
 
-	if ( $new_tt_ids )
+	if ( $new_tt_ids ) {
 		wp_update_term_count( $new_tt_ids, $taxonomy );
+	}
 
 	if ( ! $append ) {
 		$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
 
 		if ( $delete_tt_ids ) {
 			$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
-			$delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
-			$delete_term_ids = array_map( 'intval', $delete_term_ids );
+			$delete_term_ids  = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
+			$delete_term_ids  = array_map( 'intval', $delete_term_ids );
 
 			$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
 			if ( is_wp_error( $remove ) ) {
@@ -2376,17 +2514,28 @@
 		}
 	}
 
-	$t = get_taxonomy($taxonomy);
-	if ( ! $append && isset($t->sort) && $t->sort ) {
-		$values = array();
-		$term_order = 0;
-		$final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
-		foreach ( $tt_ids as $tt_id )
-			if ( in_array($tt_id, $final_tt_ids) )
-				$values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
-		if ( $values )
-			if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" ) )
+	$t = get_taxonomy( $taxonomy );
+	if ( ! $append && isset( $t->sort ) && $t->sort ) {
+		$values       = array();
+		$term_order   = 0;
+		$final_tt_ids = wp_get_object_terms(
+			$object_id,
+			$taxonomy,
+			array(
+				'fields'                 => 'tt_ids',
+				'update_term_meta_cache' => false,
+			)
+		);
+		foreach ( $tt_ids as $tt_id ) {
+			if ( in_array( $tt_id, $final_tt_ids ) ) {
+				$values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order );
+			}
+		}
+		if ( $values ) {
+			if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) {
 				return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error );
+			}
+		}
 	}
 
 	wp_cache_delete( $object_id, $taxonomy . '_relationships' );
@@ -2517,7 +2666,7 @@
  * The way this works is that if the taxonomy that the term belongs to is
  * hierarchical and has a parent, it will append that parent to the $slug.
  *
- * If that still doesn't return an unique slug, then it try to append a number
+ * If that still doesn't return a unique slug, then it tries to append a number
  * until it finds a number that is truly unique.
  *
  * The only purpose for `$term` is for appending a parent, if one exists.
@@ -2533,7 +2682,7 @@
 function wp_unique_term_slug( $slug, $term ) {
 	global $wpdb;
 
-	$needs_suffix = true;
+	$needs_suffix  = true;
 	$original_slug = $slug;
 
 	// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
@@ -2548,17 +2697,19 @@
 	$parent_suffix = '';
 	if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
 		$the_parent = $term->parent;
-		while ( ! empty($the_parent) ) {
-			$parent_term = get_term($the_parent, $term->taxonomy);
-			if ( is_wp_error($parent_term) || empty($parent_term) )
+		while ( ! empty( $the_parent ) ) {
+			$parent_term = get_term( $the_parent, $term->taxonomy );
+			if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
 				break;
+			}
 			$parent_suffix .= '-' . $parent_term->slug;
 			if ( ! term_exists( $slug . $parent_suffix ) ) {
 				break;
 			}
 
-			if ( empty($parent_term->parent) )
+			if ( empty( $parent_term->parent ) ) {
 				break;
+			}
 			$the_parent = $parent_term->parent;
 		}
 	}
@@ -2578,10 +2729,11 @@
 		if ( $parent_suffix ) {
 			$slug .= $parent_suffix;
 		} else {
-			if ( ! empty( $term->term_id ) )
+			if ( ! empty( $term->term_id ) ) {
 				$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
-			else
+			} else {
 				$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
+			}
 
 			if ( $wpdb->get_var( $query ) ) {
 				$num = 2;
@@ -2663,18 +2815,23 @@
 	$term = wp_slash( $term );
 
 	// Merge old and new args with new args overwriting old ones.
-	$args = array_merge($term, $args);
-
-	$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
-	$args = wp_parse_args($args, $defaults);
-	$args = sanitize_term($args, $taxonomy, 'db');
+	$args = array_merge( $term, $args );
+
+	$defaults    = array(
+		'alias_of'    => '',
+		'description' => '',
+		'parent'      => 0,
+		'slug'        => '',
+	);
+	$args        = wp_parse_args( $args, $defaults );
+	$args        = sanitize_term( $args, $taxonomy, 'db' );
 	$parsed_args = $args;
 
 	// expected_slashed ($name)
-	$name = wp_unslash( $args['name'] );
+	$name        = wp_unslash( $args['name'] );
 	$description = wp_unslash( $args['description'] );
 
-	$parsed_args['name'] = $name;
+	$parsed_args['name']        = $name;
 	$parsed_args['description'] = $description;
 
 	if ( '' == trim( $name ) ) {
@@ -2688,7 +2845,7 @@
 	$empty_slug = false;
 	if ( empty( $args['slug'] ) ) {
 		$empty_slug = true;
-		$slug = sanitize_title($name);
+		$slug       = sanitize_title( $name );
 	} else {
 		$slug = $args['slug'];
 	}
@@ -2706,11 +2863,15 @@
 			 * The alias is not in a group, so we create a new one
 			 * and add the alias to it.
 			 */
-			$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
-
-			wp_update_term( $alias->term_id, $taxonomy, array(
-				'term_group' => $term_group,
-			) );
+			$term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
+
+			wp_update_term(
+				$alias->term_id,
+				$taxonomy,
+				array(
+					'term_group' => $term_group,
+				)
+			);
 		}
 
 		$parsed_args['term_group'] = $term_group;
@@ -2736,15 +2897,15 @@
 	if ( $duplicate && $duplicate->term_id != $term_id ) {
 		// If an empty slug was passed or the parent changed, reset the slug to something unique.
 		// Otherwise, bail.
-		if ( $empty_slug || ( $parent != $term['parent']) ) {
-			$slug = wp_unique_term_slug($slug, (object) $args);
+		if ( $empty_slug || ( $parent != $term['parent'] ) ) {
+			$slug = wp_unique_term_slug( $slug, (object) $args );
 		} else {
-			/* translators: 1: Taxonomy term slug */
+			/* translators: %s: taxonomy term slug */
 			return new WP_Error( 'duplicate_term_slug', sprintf( __( 'The slug &#8220;%s&#8221; is already in use by another term.' ), $slug ) );
 		}
 	}
 
-	$tt_id = (int) $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) );
+	$tt_id = (int) $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 ) );
 
 	// Check whether this is a shared term that needs splitting.
 	$_term_id = _split_shared_term( $term_id, $tt_id );
@@ -2777,8 +2938,8 @@
 	$data = apply_filters( 'wp_update_term_data', $data, $term_id, $taxonomy, $args );
 
 	$wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) );
-	if ( empty($slug) ) {
-		$slug = sanitize_title($name, $term_id);
+	if ( empty( $slug ) ) {
+		$slug = sanitize_title( $name, $term_id );
 		$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
 	}
 
@@ -2823,7 +2984,7 @@
 	 * @param int    $tt_id    Term taxonomy ID.
 	 * @param string $taxonomy Taxonomy slug.
 	 */
-	do_action( "edit_term", $term_id, $tt_id, $taxonomy );
+	do_action( 'edit_term', $term_id, $tt_id, $taxonomy );
 
 	/**
 	 * Fires after a term in a specific taxonomy has been updated, but before the term
@@ -2841,7 +3002,7 @@
 	/** This filter is documented in wp-includes/taxonomy.php */
 	$term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
 
-	clean_term_cache($term_id, $taxonomy);
+	clean_term_cache( $term_id, $taxonomy );
 
 	/**
 	 * Fires after a term has been updated, and the term cache has been cleaned.
@@ -2852,7 +3013,7 @@
 	 * @param int    $tt_id    Term taxonomy ID.
 	 * @param string $taxonomy Taxonomy slug.
 	 */
-	do_action( "edited_term", $term_id, $tt_id, $taxonomy );
+	do_action( 'edited_term', $term_id, $tt_id, $taxonomy );
 
 	/**
 	 * Fires after a term for a specific taxonomy has been updated, and the term
@@ -2867,7 +3028,10 @@
 	 */
 	do_action( "edited_{$taxonomy}", $term_id, $tt_id );
 
-	return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
+	return array(
+		'term_id'          => $term_id,
+		'term_taxonomy_id' => $tt_id,
+	);
 }
 
 /**
@@ -2880,14 +3044,15 @@
  * @param bool $defer Optional. Enable if true, disable if false.
  * @return bool Whether term counting is enabled or disabled.
  */
-function wp_defer_term_counting($defer=null) {
+function wp_defer_term_counting( $defer = null ) {
 	static $_defer = false;
 
-	if ( is_bool($defer) ) {
+	if ( is_bool( $defer ) ) {
 		$_defer = $defer;
 		// flush any deferred counts
-		if ( !$defer )
+		if ( ! $defer ) {
 			wp_update_term_count( null, null, true );
+		}
 	}
 
 	return $_defer;
@@ -2915,22 +3080,25 @@
 	static $_deferred = array();
 
 	if ( $do_deferred ) {
-		foreach ( (array) array_keys($_deferred) as $tax ) {
-			wp_update_term_count_now( $_deferred[$tax], $tax );
-			unset( $_deferred[$tax] );
+		foreach ( (array) array_keys( $_deferred ) as $tax ) {
+			wp_update_term_count_now( $_deferred[ $tax ], $tax );
+			unset( $_deferred[ $tax ] );
 		}
 	}
 
-	if ( empty($terms) )
+	if ( empty( $terms ) ) {
 		return false;
-
-	if ( !is_array($terms) )
-		$terms = array($terms);
+	}
+
+	if ( ! is_array( $terms ) ) {
+		$terms = array( $terms );
+	}
 
 	if ( wp_defer_term_counting() ) {
-		if ( !isset($_deferred[$taxonomy]) )
-			$_deferred[$taxonomy] = array();
-		$_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
+		if ( ! isset( $_deferred[ $taxonomy ] ) ) {
+			$_deferred[ $taxonomy ] = array();
+		}
+		$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
 		return true;
 	}
 
@@ -2947,16 +3115,17 @@
  * @return true Always true when complete.
  */
 function wp_update_term_count_now( $terms, $taxonomy ) {
-	$terms = array_map('intval', $terms);
-
-	$taxonomy = get_taxonomy($taxonomy);
-	if ( !empty($taxonomy->update_count_callback) ) {
-		call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
+	$terms = array_map( 'intval', $terms );
+
+	$taxonomy = get_taxonomy( $taxonomy );
+	if ( ! empty( $taxonomy->update_count_callback ) ) {
+		call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
 	} else {
 		$object_types = (array) $taxonomy->object_type;
 		foreach ( $object_types as &$object_type ) {
-			if ( 0 === strpos( $object_type, 'attachment:' ) )
+			if ( 0 === strpos( $object_type, 'attachment:' ) ) {
 				list( $object_type ) = explode( ':', $object_type );
+			}
 		}
 
 		if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {
@@ -2968,7 +3137,7 @@
 		}
 	}
 
-	clean_term_cache($terms, '', false);
+	clean_term_cache( $terms, '', false );
 
 	return true;
 }
@@ -2993,21 +3162,22 @@
  * @param int|array    $object_ids  Single or list of term object ID(s).
  * @param array|string $object_type The taxonomy object type.
  */
-function clean_object_term_cache($object_ids, $object_type) {
+function clean_object_term_cache( $object_ids, $object_type ) {
 	global $_wp_suspend_cache_invalidation;
 
 	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
 		return;
 	}
 
-	if ( !is_array($object_ids) )
-		$object_ids = array($object_ids);
+	if ( ! is_array( $object_ids ) ) {
+		$object_ids = array( $object_ids );
+	}
 
 	$taxonomies = get_object_taxonomies( $object_type );
 
 	foreach ( $object_ids as $id ) {
 		foreach ( $taxonomies as $taxonomy ) {
-			wp_cache_delete($id, "{$taxonomy}_relationships");
+			wp_cache_delete( $id, "{$taxonomy}_relationships" );
 		}
 	}
 
@@ -3036,31 +3206,32 @@
  * @param bool      $clean_taxonomy Optional. Whether to clean taxonomy wide caches (true), or just individual
  *                                  term object caches (false). Default true.
  */
-function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
+function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
 	global $wpdb, $_wp_suspend_cache_invalidation;
 
 	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
 		return;
 	}
 
-	if ( !is_array($ids) )
-		$ids = array($ids);
+	if ( ! is_array( $ids ) ) {
+		$ids = array( $ids );
+	}
 
 	$taxonomies = array();
 	// If no taxonomy, assume tt_ids.
-	if ( empty($taxonomy) ) {
-		$tt_ids = array_map('intval', $ids);
-		$tt_ids = implode(', ', $tt_ids);
-		$terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
-		$ids = array();
+	if ( empty( $taxonomy ) ) {
+		$tt_ids = array_map( 'intval', $ids );
+		$tt_ids = implode( ', ', $tt_ids );
+		$terms  = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
+		$ids    = array();
 		foreach ( (array) $terms as $term ) {
 			$taxonomies[] = $term->taxonomy;
-			$ids[] = $term->term_id;
+			$ids[]        = $term->term_id;
 			wp_cache_delete( $term->term_id, 'terms' );
 		}
-		$taxonomies = array_unique($taxonomies);
+		$taxonomies = array_unique( $taxonomies );
 	} else {
-		$taxonomies = array($taxonomy);
+		$taxonomies = array( $taxonomy );
 		foreach ( $taxonomies as $taxonomy ) {
 			foreach ( $ids as $id ) {
 				wp_cache_delete( $id, 'terms' );
@@ -3122,7 +3293,7 @@
  * function only fetches relationship data that is already in the cache.
  *
  * @since 2.3.0
- * @since 4.7.0 Returns a WP_Error object if get_term() returns an error for
+ * @since 4.7.0 Returns a `WP_Error` object if `get_term()` returns an error for
  *              any of the matched terms.
  *
  * @param int    $id       Term object ID.
@@ -3181,35 +3352,42 @@
  * @param array|string $object_type The taxonomy object type.
  * @return void|false False if all of the terms in `$object_ids` are already cached.
  */
-function update_object_term_cache($object_ids, $object_type) {
-	if ( empty($object_ids) )
+function update_object_term_cache( $object_ids, $object_type ) {
+	if ( empty( $object_ids ) ) {
 		return;
-
-	if ( !is_array($object_ids) )
-		$object_ids = explode(',', $object_ids);
-
-	$object_ids = array_map('intval', $object_ids);
-
-	$taxonomies = get_object_taxonomies($object_type);
+	}
+
+	if ( ! is_array( $object_ids ) ) {
+		$object_ids = explode( ',', $object_ids );
+	}
+
+	$object_ids = array_map( 'intval', $object_ids );
+
+	$taxonomies = get_object_taxonomies( $object_type );
 
 	$ids = array();
 	foreach ( (array) $object_ids as $id ) {
 		foreach ( $taxonomies as $taxonomy ) {
-			if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
+			if ( false === wp_cache_get( $id, "{$taxonomy}_relationships" ) ) {
 				$ids[] = $id;
 				break;
 			}
 		}
 	}
 
-	if ( empty( $ids ) )
+	if ( empty( $ids ) ) {
 		return false;
-
-	$terms = wp_get_object_terms( $ids, $taxonomies, array(
-		'fields' => 'all_with_object_id',
-		'orderby' => 'name',
-		'update_term_meta_cache' => false,
-	) );
+	}
+
+	$terms = wp_get_object_terms(
+		$ids,
+		$taxonomies,
+		array(
+			'fields'                 => 'all_with_object_id',
+			'orderby'                => 'name',
+			'update_term_meta_cache' => false,
+		)
+	);
 
 	$object_terms = array();
 	foreach ( (array) $terms as $term ) {
@@ -3218,10 +3396,11 @@
 
 	foreach ( $ids as $id ) {
 		foreach ( $taxonomies as $taxonomy ) {
-			if ( ! isset($object_terms[$id][$taxonomy]) ) {
-				if ( !isset($object_terms[$id]) )
-					$object_terms[$id] = array();
-				$object_terms[$id][$taxonomy] = array();
+			if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
+				if ( ! isset( $object_terms[ $id ] ) ) {
+					$object_terms[ $id ] = array();
+				}
+				$object_terms[ $id ][ $taxonomy ] = array();
 			}
 		}
 	}
@@ -3260,26 +3439,37 @@
 /**
  * Retrieves children of taxonomy as Term IDs.
  *
- * @ignore
+ * @access private
  * @since 2.3.0
  *
  * @param string $taxonomy Taxonomy name.
  * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs.
  */
 function _get_term_hierarchy( $taxonomy ) {
-	if ( !is_taxonomy_hierarchical($taxonomy) )
+	if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
 		return array();
-	$children = get_option("{$taxonomy}_children");
-
-	if ( is_array($children) )
+	}
+	$children = get_option( "{$taxonomy}_children" );
+
+	if ( is_array( $children ) ) {
 		return $children;
+	}
 	$children = array();
-	$terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));
+	$terms    = get_terms(
+		$taxonomy,
+		array(
+			'get'                    => 'all',
+			'orderby'                => 'id',
+			'fields'                 => 'id=>parent',
+			'update_term_meta_cache' => false,
+		)
+	);
 	foreach ( $terms as $term_id => $parent ) {
-		if ( $parent > 0 )
-			$children[$parent][] = $term_id;
+		if ( $parent > 0 ) {
+			$children[ $parent ][] = $term_id;
+		}
 	}
-	update_option("{$taxonomy}_children", $children);
+	update_option( "{$taxonomy}_children", $children );
 
 	return $children;
 }
@@ -3305,14 +3495,16 @@
  */
 function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
 	$empty_array = array();
-	if ( empty($terms) )
+	if ( empty( $terms ) ) {
 		return $empty_array;
-
-	$term_list = array();
-	$has_children = _get_term_hierarchy($taxonomy);
-
-	if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
+	}
+
+	$term_list    = array();
+	$has_children = _get_term_hierarchy( $taxonomy );
+
+	if ( ( 0 != $term_id ) && ! isset( $has_children[ $term_id ] ) ) {
 		return $empty_array;
+	}
 
 	// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
 	if ( empty( $ancestors ) ) {
@@ -3321,10 +3513,11 @@
 
 	foreach ( (array) $terms as $term ) {
 		$use_id = false;
-		if ( !is_object($term) ) {
-			$term = get_term($term, $taxonomy);
-			if ( is_wp_error( $term ) )
+		if ( ! is_object( $term ) ) {
+			$term = get_term( $term, $taxonomy );
+			if ( is_wp_error( $term ) ) {
 				return $term;
+			}
 			$use_id = true;
 		}
 
@@ -3334,18 +3527,21 @@
 		}
 
 		if ( $term->parent == $term_id ) {
-			if ( $use_id )
+			if ( $use_id ) {
 				$term_list[] = $term->term_id;
-			else
+			} else {
 				$term_list[] = $term;
-
-			if ( !isset($has_children[$term->term_id]) )
+			}
+
+			if ( ! isset( $has_children[ $term->term_id ] ) ) {
 				continue;
+			}
 
 			$ancestors[ $term->term_id ] = 1;
 
-			if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
-				$term_list = array_merge($term_list, $children);
+			if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ) ) {
+				$term_list = array_merge( $term_list, $children );
+			}
 		}
 	}
 
@@ -3370,42 +3566,45 @@
 	global $wpdb;
 
 	// This function only works for hierarchical taxonomies like post categories.
-	if ( !is_taxonomy_hierarchical( $taxonomy ) )
+	if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
 		return;
-
-	$term_hier = _get_term_hierarchy($taxonomy);
-
-	if ( empty($term_hier) )
+	}
+
+	$term_hier = _get_term_hierarchy( $taxonomy );
+
+	if ( empty( $term_hier ) ) {
 		return;
-
-	$term_items = array();
+	}
+
+	$term_items  = array();
 	$terms_by_id = array();
-	$term_ids = array();
+	$term_ids    = array();
 
 	foreach ( (array) $terms as $key => $term ) {
-		$terms_by_id[$term->term_id] = & $terms[$key];
-		$term_ids[$term->term_taxonomy_id] = $term->term_id;
+		$terms_by_id[ $term->term_id ]       = & $terms[ $key ];
+		$term_ids[ $term->term_taxonomy_id ] = $term->term_id;
 	}
 
 	// Get the object and term ids and stick them in a lookup table.
-	$tax_obj = get_taxonomy($taxonomy);
-	$object_types = esc_sql($tax_obj->object_type);
-	$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'");
+	$tax_obj      = get_taxonomy( $taxonomy );
+	$object_types = esc_sql( $tax_obj->object_type );
+	$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'" );
 	foreach ( $results as $row ) {
-		$id = $term_ids[$row->term_taxonomy_id];
-		$term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
+		$id                                   = $term_ids[ $row->term_taxonomy_id ];
+		$term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1;
 	}
 
 	// Touch every ancestor's lookup row for each post in each term.
 	foreach ( $term_ids as $term_id ) {
-		$child = $term_id;
+		$child     = $term_id;
 		$ancestors = array();
-		while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
+		while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) {
 			$ancestors[] = $child;
-			if ( !empty( $term_items[$term_id] ) )
-				foreach ( $term_items[$term_id] as $item_id => $touches ) {
-					$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
+			if ( ! empty( $term_items[ $term_id ] ) ) {
+				foreach ( $term_items[ $term_id ] as $item_id => $touches ) {
+					$term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1;
 				}
+			}
 			$child = $parent;
 
 			if ( in_array( $parent, $ancestors ) ) {
@@ -3415,9 +3614,11 @@
 	}
 
 	// Transfer the touched cells.
-	foreach ( (array) $term_items as $id => $items )
-		if ( isset($terms_by_id[$id]) )
-			$terms_by_id[$id]->count = count($items);
+	foreach ( (array) $term_items as $id => $items ) {
+		if ( isset( $terms_by_id[ $id ] ) ) {
+			$terms_by_id[ $id ]->count = count( $items );
+		}
+	}
 }
 
 /**
@@ -3436,7 +3637,7 @@
 
 	$non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
 	if ( ! empty( $non_cached_ids ) ) {
-		$fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
+		$fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) );
 
 		update_term_cache( $fresh_terms, $update_meta_cache );
 
@@ -3469,8 +3670,9 @@
 
 	$object_types = (array) $taxonomy->object_type;
 
-	foreach ( $object_types as &$object_type )
+	foreach ( $object_types as &$object_type ) {
 		list( $object_type ) = explode( ':', $object_type );
+	}
 
 	$object_types = array_unique( $object_types );
 
@@ -3479,18 +3681,21 @@
 		$check_attachments = true;
 	}
 
-	if ( $object_types )
+	if ( $object_types ) {
 		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
+	}
 
 	foreach ( (array) $terms as $term ) {
 		$count = 0;
 
 		// Attachments can be 'inherit' status, we need to base count off the parent's status if so.
-		if ( $check_attachments )
+		if ( $check_attachments ) {
 			$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 ) );
-
-		if ( $object_types )
-			$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 ) );
+		}
+
+		if ( $object_types ) {
+			$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 ) );
+		}
 
 		/** This action is documented in wp-includes/taxonomy.php */
 		do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
@@ -3556,11 +3761,11 @@
 
 	if ( is_object( $term_id ) ) {
 		$shared_term = $term_id;
-		$term_id = intval( $shared_term->term_id );
+		$term_id     = intval( $shared_term->term_id );
 	}
 
 	if ( is_object( $term_taxonomy_id ) ) {
-		$term_taxonomy = $term_taxonomy_id;
+		$term_taxonomy    = $term_taxonomy_id;
 		$term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id );
 	}
 
@@ -3586,8 +3791,8 @@
 	}
 
 	$new_term_data = array(
-		'name' => $shared_term->name,
-		'slug' => $shared_term->slug,
+		'name'       => $shared_term->name,
+		'slug'       => $shared_term->slug,
 		'term_group' => $shared_term->term_group,
 	);
 
@@ -3598,7 +3803,8 @@
 	$new_term_id = (int) $wpdb->insert_id;
 
 	// Update the existing term_taxonomy to point to the newly created term.
-	$wpdb->update( $wpdb->term_taxonomy,
+	$wpdb->update(
+		$wpdb->term_taxonomy,
 		array( 'term_id' => $new_term_id ),
 		array( 'term_taxonomy_id' => $term_taxonomy_id )
 	);
@@ -3611,7 +3817,8 @@
 	$children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = %s", $term_id, $term_taxonomy->taxonomy ) );
 	if ( ! empty( $children_tt_ids ) ) {
 		foreach ( $children_tt_ids as $child_tt_id ) {
-			$wpdb->update( $wpdb->term_taxonomy,
+			$wpdb->update(
+				$wpdb->term_taxonomy,
 				array( 'parent' => $new_term_id ),
 				array( 'term_taxonomy_id' => $child_tt_id )
 			);
@@ -3632,7 +3839,7 @@
 
 	// Clean the cache for term taxonomies formerly shared with the current term.
 	$shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
-	$taxonomies_to_clean = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );
+	$taxonomies_to_clean    = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );
 
 	foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) {
 		clean_taxonomy_cache( $taxonomy_to_clean );
@@ -3726,17 +3933,17 @@
 	// Rekey shared term array for faster lookups.
 	$_shared_terms = array();
 	foreach ( $shared_terms as $shared_term ) {
-		$term_id = intval( $shared_term->term_id );
+		$term_id                   = intval( $shared_term->term_id );
 		$_shared_terms[ $term_id ] = $shared_term;
 	}
 	$shared_terms = $_shared_terms;
 
 	// Get term taxonomy data for all shared terms.
 	$shared_term_ids = implode( ',', array_keys( $shared_terms ) );
-	$shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
+	$shared_tts      = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
 
 	// Split term data recording is slow, so we do it just once, outside the loop.
-	$split_term_data = get_option( '_split_terms', array() );
+	$split_term_data    = get_option( '_split_terms', array() );
 	$skipped_first_term = $taxonomies = array();
 	foreach ( $shared_tts as $shared_tt ) {
 		$term_id = intval( $shared_tt->term_id );
@@ -3822,17 +4029,19 @@
  */
 function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
 	global $wpdb;
-	$post_ids = $wpdb->get_col( $wpdb->prepare(
-		"SELECT m1.post_id
+	$post_ids = $wpdb->get_col(
+		$wpdb->prepare(
+			"SELECT m1.post_id
 		FROM {$wpdb->postmeta} AS m1
 			INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
 			INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id )
 		WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
 			AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s )
 			AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
-		$taxonomy,
-		$term_id
-	) );
+			$taxonomy,
+			$term_id
+		)
+	);
 
 	if ( $post_ids ) {
 		foreach ( $post_ids as $post_id ) {
@@ -3918,7 +4127,7 @@
  *
  * @param int $term_id Term ID.
  * @return bool Returns false if a term is not shared between multiple taxonomies or
- *              if splittng shared taxonomy terms is finished.
+ *              if splitting shared taxonomy terms is finished.
  */
 function wp_term_is_shared( $term_id ) {
 	global $wpdb;
@@ -3946,7 +4155,7 @@
 function get_term_link( $term, $taxonomy = '' ) {
 	global $wp_rewrite;
 
-	if ( !is_object($term) ) {
+	if ( ! is_object( $term ) ) {
 		if ( is_int( $term ) ) {
 			$term = get_term( $term, $taxonomy );
 		} else {
@@ -3954,15 +4163,17 @@
 		}
 	}
 
-	if ( !is_object($term) )
+	if ( ! is_object( $term ) ) {
 		$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
-
-	if ( is_wp_error( $term ) )
+	}
+
+	if ( is_wp_error( $term ) ) {
 		return $term;
+	}
 
 	$taxonomy = $term->taxonomy;
 
-	$termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
+	$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
 
 	/**
 	 * Filters the permalink structure for a terms before token replacement occurs.
@@ -3975,31 +4186,32 @@
 	$termlink = apply_filters( 'pre_term_link', $termlink, $term );
 
 	$slug = $term->slug;
-	$t = get_taxonomy($taxonomy);
-
-	if ( empty($termlink) ) {
-		if ( 'category' == $taxonomy )
+	$t    = get_taxonomy( $taxonomy );
+
+	if ( empty( $termlink ) ) {
+		if ( 'category' == $taxonomy ) {
 			$termlink = '?cat=' . $term->term_id;
-		elseif ( $t->query_var )
+		} elseif ( $t->query_var ) {
 			$termlink = "?$t->query_var=$slug";
-		else
+		} else {
 			$termlink = "?taxonomy=$taxonomy&term=$slug";
-		$termlink = home_url($termlink);
+		}
+		$termlink = home_url( $termlink );
 	} else {
 		if ( $t->rewrite['hierarchical'] ) {
 			$hierarchical_slugs = array();
-			$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
-			foreach ( (array)$ancestors as $ancestor ) {
-				$ancestor_term = get_term($ancestor, $taxonomy);
+			$ancestors          = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
+			foreach ( (array) $ancestors as $ancestor ) {
+				$ancestor_term        = get_term( $ancestor, $taxonomy );
 				$hierarchical_slugs[] = $ancestor_term->slug;
 			}
-			$hierarchical_slugs = array_reverse($hierarchical_slugs);
+			$hierarchical_slugs   = array_reverse( $hierarchical_slugs );
 			$hierarchical_slugs[] = $slug;
-			$termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
+			$termlink             = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
 		} else {
-			$termlink = str_replace("%$taxonomy%", $slug, $termlink);
+			$termlink = str_replace( "%$taxonomy%", $slug, $termlink );
 		}
-		$termlink = home_url( user_trailingslashit($termlink, 'category') );
+		$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
 	}
 	// Back Compat filters.
 	if ( 'post_tag' == $taxonomy ) {
@@ -4061,10 +4273,10 @@
  */
 function the_taxonomies( $args = array() ) {
 	$defaults = array(
-		'post' => 0,
+		'post'   => 0,
 		'before' => '',
-		'sep' => ' ',
-		'after' => '',
+		'sep'    => ' ',
+		'after'  => '',
 	);
 
 	$r = wp_parse_args( $args, $defaults );
@@ -4094,11 +4306,14 @@
 function get_the_taxonomies( $post = 0, $args = array() ) {
 	$post = get_post( $post );
 
-	$args = wp_parse_args( $args, array(
-		/* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
-		'template' => __( '%s: %l.' ),
-		'term_template' => '<a href="%1$s">%2$s</a>',
-	) );
+	$args = wp_parse_args(
+		$args,
+		array(
+			/* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
+			'template'      => __( '%s: %l.' ),
+			'term_template' => '<a href="%1$s">%2$s</a>',
+		)
+	);
 
 	$taxonomies = array();
 
@@ -4131,7 +4346,7 @@
 			$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
 		}
 		if ( $links ) {
-			$taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
+			$taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
 		}
 	}
 	return $taxonomies;
@@ -4148,7 +4363,7 @@
 function get_post_taxonomies( $post = 0 ) {
 	$post = get_post( $post );
 
-	return get_object_taxonomies($post);
+	return get_object_taxonomies( $post );
 }
 
 /**
@@ -4166,8 +4381,9 @@
  * @return bool|WP_Error WP_Error on input error.
  */
 function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
-	if ( !$object_id = (int) $object_id )
+	if ( ! $object_id = (int) $object_id ) {
 		return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
+	}
 
 	$object_terms = get_object_term_cache( $object_id, $taxonomy );
 	if ( false === $object_terms ) {
@@ -4179,19 +4395,23 @@
 		wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
 	}
 
-	if ( is_wp_error( $object_terms ) )
+	if ( is_wp_error( $object_terms ) ) {
 		return $object_terms;
-	if ( empty( $object_terms ) )
+	}
+	if ( empty( $object_terms ) ) {
 		return false;
-	if ( empty( $terms ) )
-		return ( !empty( $object_terms ) );
+	}
+	if ( empty( $terms ) ) {
+		return ( ! empty( $object_terms ) );
+	}
 
 	$terms = (array) $terms;
 
-	if ( $ints = array_filter( $terms, 'is_int' ) )
+	if ( $ints = array_filter( $terms, 'is_int' ) ) {
 		$strs = array_diff( $terms, $ints );
-	else
+	} else {
 		$strs =& $terms;
+	}
 
 	foreach ( $object_terms as $object_term ) {
 		// If term is an int, check against term_ids only.
@@ -4206,8 +4426,12 @@
 				return true;
 			}
 
-			if ( in_array( $object_term->name, $strs ) ) return true;
-			if ( in_array( $object_term->slug, $strs ) ) return true;
+			if ( in_array( $object_term->name, $strs ) ) {
+				return true;
+			}
+			if ( in_array( $object_term->slug, $strs ) ) {
+				return true;
+			}
 		}
 	}
 
@@ -4264,13 +4488,13 @@
 	}
 
 	if ( 'taxonomy' === $resource_type ) {
-		$term = get_term($object_id, $object_type);
-		while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
+		$term = get_term( $object_id, $object_type );
+		while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
 			$ancestors[] = (int) $term->parent;
-			$term = get_term($term->parent, $object_type);
+			$term        = get_term( $term->parent, $object_type );
 		}
 	} elseif ( 'post_type' === $resource_type ) {
-		$ancestors = get_post_ancestors($object_id);
+		$ancestors = get_post_ancestors( $object_id );
 	}
 
 	/**
@@ -4320,24 +4544,73 @@
  */
 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
 	// Nothing fancy here - bail
-	if ( !$parent )
+	if ( ! $parent ) {
 		return 0;
+	}
 
 	// Can't be its own parent.
-	if ( $parent == $term_id )
+	if ( $parent == $term_id ) {
 		return 0;
+	}
 
 	// Now look for larger loops.
-	if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
+	if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) {
 		return $parent; // No loop
+	}
 
 	// Setting $parent to the given value causes a loop.
-	if ( isset( $loop[$term_id] ) )
+	if ( isset( $loop[ $term_id ] ) ) {
 		return 0;
+	}
 
 	// There's a loop, but it doesn't contain $term_id. Break the loop.
-	foreach ( array_keys( $loop ) as $loop_member )
+	foreach ( array_keys( $loop ) as $loop_member ) {
 		wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
+	}
 
 	return $parent;
 }
+
+/**
+ * Determines whether a taxonomy is considered "viewable".
+ *
+ * @since 5.1.0
+ *
+ * @param string|WP_Taxonomy $taxonomy Taxonomy name or object.
+ * @return bool Whether the taxonomy should be considered viewable.
+ */
+function is_taxonomy_viewable( $taxonomy ) {
+	if ( is_scalar( $taxonomy ) ) {
+		$taxonomy = get_taxonomy( $taxonomy );
+		if ( ! $taxonomy ) {
+			return false;
+		}
+	}
+
+	return $taxonomy->publicly_queryable;
+}
+
+/**
+ * Sets the last changed time for the 'terms' cache group.
+ *
+ * @since 5.0.0
+ */
+function wp_cache_set_terms_last_changed() {
+	wp_cache_set( 'last_changed', microtime(), 'terms' );
+}
+
+/**
+ * Aborts calls to term meta if it is not supported.
+ *
+ * @since 5.0.0
+ *
+ * @param mixed $check Skip-value for whether to proceed term meta function execution.
+ * @return mixed Original value of $check, or false if term meta is not supported.
+ */
+function wp_check_term_meta_support_prefilter( $check ) {
+	if ( get_option( 'db_version' ) < 34370 ) {
+		return false;
+	}
+
+	return $check;
+}