[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> taxonomy.php (source)

   1  <?php
   2  /**
   3   * Core Taxonomy API
   4   *
   5   * @package WordPress
   6   * @subpackage Taxonomy
   7   */
   8  
   9  //
  10  // Taxonomy registration.
  11  //
  12  
  13  /**
  14   * Creates the initial taxonomies.
  15   *
  16   * This function fires twice: in wp-settings.php before plugins are loaded (for
  17   * backward compatibility reasons), and again on the {@see 'init'} action. We must
  18   * avoid registering rewrite rules before the {@see 'init'} action.
  19   *
  20   * @since 2.8.0
  21   *
  22   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  23   */
  24  function create_initial_taxonomies() {
  25      global $wp_rewrite;
  26  
  27      if ( ! did_action( 'init' ) ) {
  28          $rewrite = array(
  29              'category'    => false,
  30              'post_tag'    => false,
  31              'post_format' => false,
  32          );
  33      } else {
  34  
  35          /**
  36           * Filters the post formats rewrite base.
  37           *
  38           * @since 3.1.0
  39           *
  40           * @param string $context Context of the rewrite base. Default 'type'.
  41           */
  42          $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
  43          $rewrite          = array(
  44              'category'    => array(
  45                  'hierarchical' => true,
  46                  'slug'         => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category',
  47                  'with_front'   => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(),
  48                  'ep_mask'      => EP_CATEGORIES,
  49              ),
  50              'post_tag'    => array(
  51                  'hierarchical' => false,
  52                  'slug'         => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag',
  53                  'with_front'   => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(),
  54                  'ep_mask'      => EP_TAGS,
  55              ),
  56              'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
  57          );
  58      }
  59  
  60      register_taxonomy(
  61          'category',
  62          'post',
  63          array(
  64              'hierarchical'          => true,
  65              'query_var'             => 'category_name',
  66              'rewrite'               => $rewrite['category'],
  67              'public'                => true,
  68              'show_ui'               => true,
  69              'show_admin_column'     => true,
  70              '_builtin'              => true,
  71              'capabilities'          => array(
  72                  'manage_terms' => 'manage_categories',
  73                  'edit_terms'   => 'edit_categories',
  74                  'delete_terms' => 'delete_categories',
  75                  'assign_terms' => 'assign_categories',
  76              ),
  77              'show_in_rest'          => true,
  78              'rest_base'             => 'categories',
  79              'rest_controller_class' => 'WP_REST_Terms_Controller',
  80          )
  81      );
  82  
  83      register_taxonomy(
  84          'post_tag',
  85          'post',
  86          array(
  87              'hierarchical'          => false,
  88              'query_var'             => 'tag',
  89              'rewrite'               => $rewrite['post_tag'],
  90              'public'                => true,
  91              'show_ui'               => true,
  92              'show_admin_column'     => true,
  93              '_builtin'              => true,
  94              'capabilities'          => array(
  95                  'manage_terms' => 'manage_post_tags',
  96                  'edit_terms'   => 'edit_post_tags',
  97                  'delete_terms' => 'delete_post_tags',
  98                  'assign_terms' => 'assign_post_tags',
  99              ),
 100              'show_in_rest'          => true,
 101              'rest_base'             => 'tags',
 102              'rest_controller_class' => 'WP_REST_Terms_Controller',
 103          )
 104      );
 105  
 106      register_taxonomy(
 107          'nav_menu',
 108          'nav_menu_item',
 109          array(
 110              'public'            => false,
 111              'hierarchical'      => false,
 112              'labels'            => array(
 113                  'name'          => __( 'Navigation Menus' ),
 114                  'singular_name' => __( 'Navigation Menu' ),
 115              ),
 116              'query_var'         => false,
 117              'rewrite'           => false,
 118              'show_ui'           => false,
 119              '_builtin'          => true,
 120              'show_in_nav_menus' => false,
 121          )
 122      );
 123  
 124      register_taxonomy(
 125          'link_category',
 126          'link',
 127          array(
 128              'hierarchical' => false,
 129              'labels'       => array(
 130                  'name'                       => __( 'Link Categories' ),
 131                  'singular_name'              => __( 'Link Category' ),
 132                  'search_items'               => __( 'Search Link Categories' ),
 133                  'popular_items'              => null,
 134                  'all_items'                  => __( 'All Link Categories' ),
 135                  'edit_item'                  => __( 'Edit Link Category' ),
 136                  'update_item'                => __( 'Update Link Category' ),
 137                  'add_new_item'               => __( 'Add New Link Category' ),
 138                  'new_item_name'              => __( 'New Link Category Name' ),
 139                  'separate_items_with_commas' => null,
 140                  'add_or_remove_items'        => null,
 141                  'choose_from_most_used'      => null,
 142                  'back_to_items'              => __( '&larr; Go to Link Categories' ),
 143              ),
 144              'capabilities' => array(
 145                  'manage_terms' => 'manage_links',
 146                  'edit_terms'   => 'manage_links',
 147                  'delete_terms' => 'manage_links',
 148                  'assign_terms' => 'manage_links',
 149              ),
 150              'query_var'    => false,
 151              'rewrite'      => false,
 152              'public'       => false,
 153              'show_ui'      => true,
 154              '_builtin'     => true,
 155          )
 156      );
 157  
 158      register_taxonomy(
 159          'post_format',
 160          'post',
 161          array(
 162              'public'            => true,
 163              'hierarchical'      => false,
 164              'labels'            => array(
 165                  'name'          => _x( 'Formats', 'post format' ),
 166                  'singular_name' => _x( 'Format', 'post format' ),
 167              ),
 168              'query_var'         => true,
 169              'rewrite'           => $rewrite['post_format'],
 170              'show_ui'           => false,
 171              '_builtin'          => true,
 172              'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
 173          )
 174      );
 175  }
 176  
 177  /**
 178   * Retrieves a list of registered taxonomy names or objects.
 179   *
 180   * @since 3.0.0
 181   *
 182   * @global array $wp_taxonomies The registered taxonomies.
 183   *
 184   * @param array  $args     Optional. An array of `key => value` arguments to match against the taxonomy objects.
 185   *                         Default empty array.
 186   * @param string $output   Optional. The type of output to return in the array. Accepts either taxonomy 'names'
 187   *                         or 'objects'. Default 'names'.
 188   * @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only
 189   *                         one element from the array needs to match; 'and' means all elements must match.
 190   *                         Default 'and'.
 191   * @return string[]|WP_Taxonomy[] An array of taxonomy names or objects.
 192   */
 193  function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
 194      global $wp_taxonomies;
 195  
 196      $field = ( 'names' === $output ) ? 'name' : false;
 197  
 198      return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
 199  }
 200  
 201  /**
 202   * Return the names or objects of the taxonomies which are registered for the requested object or object type, such as
 203   * a post object or post type name.
 204   *
 205   * Example:
 206   *
 207   *     $taxonomies = get_object_taxonomies( 'post' );
 208   *
 209   * This results in:
 210   *
 211   *     Array( 'category', 'post_tag' )
 212   *
 213   * @since 2.3.0
 214   *
 215   * @global array $wp_taxonomies The registered taxonomies.
 216   *
 217   * @param string|string[]|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
 218   * @param string                  $output Optional. The type of output to return in the array. Accepts either
 219   *                                        'names' or 'objects'. Default 'names'.
 220   * @return string[]|WP_Taxonomy[] The names or objects of all taxonomies of `$object_type`.
 221   */
 222  function get_object_taxonomies( $object, $output = 'names' ) {
 223      global $wp_taxonomies;
 224  
 225      if ( is_object( $object ) ) {
 226          if ( 'attachment' === $object->post_type ) {
 227              return get_attachment_taxonomies( $object, $output );
 228          }
 229          $object = $object->post_type;
 230      }
 231  
 232      $object = (array) $object;
 233  
 234      $taxonomies = array();
 235      foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
 236          if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
 237              if ( 'names' === $output ) {
 238                  $taxonomies[] = $tax_name;
 239              } else {
 240                  $taxonomies[ $tax_name ] = $tax_obj;
 241              }
 242          }
 243      }
 244  
 245      return $taxonomies;
 246  }
 247  
 248  /**
 249   * Retrieves the taxonomy object of $taxonomy.
 250   *
 251   * The get_taxonomy function will first check that the parameter string given
 252   * is a taxonomy object and if it is, it will return it.
 253   *
 254   * @since 2.3.0
 255   *
 256   * @global array $wp_taxonomies The registered taxonomies.
 257   *
 258   * @param string $taxonomy Name of taxonomy object to return.
 259   * @return WP_Taxonomy|false The Taxonomy Object or false if $taxonomy doesn't exist.
 260   */
 261  function get_taxonomy( $taxonomy ) {
 262      global $wp_taxonomies;
 263  
 264      if ( ! taxonomy_exists( $taxonomy ) ) {
 265          return false;
 266      }
 267  
 268      return $wp_taxonomies[ $taxonomy ];
 269  }
 270  
 271  /**
 272   * Determines whether the taxonomy name exists.
 273   *
 274   * Formerly is_taxonomy(), introduced in 2.3.0.
 275   *
 276   * For more information on this and similar theme functions, check out
 277   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 278   * Conditional Tags} article in the Theme Developer Handbook.
 279   *
 280   * @since 3.0.0
 281   *
 282   * @global array $wp_taxonomies The registered taxonomies.
 283   *
 284   * @param string $taxonomy Name of taxonomy object.
 285   * @return bool Whether the taxonomy exists.
 286   */
 287  function taxonomy_exists( $taxonomy ) {
 288      global $wp_taxonomies;
 289  
 290      return isset( $wp_taxonomies[ $taxonomy ] );
 291  }
 292  
 293  /**
 294   * Determines whether the taxonomy object is hierarchical.
 295   *
 296   * Checks to make sure that the taxonomy is an object first. Then Gets the
 297   * object, and finally returns the hierarchical value in the object.
 298   *
 299   * A false return value might also mean that the taxonomy does not exist.
 300   *
 301   * For more information on this and similar theme functions, check out
 302   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 303   * Conditional Tags} article in the Theme Developer Handbook.
 304   *
 305   * @since 2.3.0
 306   *
 307   * @param string $taxonomy Name of taxonomy object.
 308   * @return bool Whether the taxonomy is hierarchical.
 309   */
 310  function is_taxonomy_hierarchical( $taxonomy ) {
 311      if ( ! taxonomy_exists( $taxonomy ) ) {
 312          return false;
 313      }
 314  
 315      $taxonomy = get_taxonomy( $taxonomy );
 316      return $taxonomy->hierarchical;
 317  }
 318  
 319  /**
 320   * Creates or modifies a taxonomy object.
 321   *
 322   * Note: Do not use before the {@see 'init'} hook.
 323   *
 324   * A simple function for creating or modifying a taxonomy object based on
 325   * the parameters given. If modifying an existing taxonomy object, note
 326   * that the `$object_type` value from the original registration will be
 327   * overwritten.
 328   *
 329   * @since 2.3.0
 330   * @since 4.2.0 Introduced `show_in_quick_edit` argument.
 331   * @since 4.4.0 The `show_ui` argument is now enforced on the term editing screen.
 332   * @since 4.4.0 The `public` argument now controls whether the taxonomy can be queried on the front end.
 333   * @since 4.5.0 Introduced `publicly_queryable` argument.
 334   * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
 335   *              arguments to register the Taxonomy in REST API.
 336   * @since 5.1.0 Introduced `meta_box_sanitize_cb` argument.
 337   * @since 5.4.0 Added the registered taxonomy object as a return value.
 338   * @since 5.5.0 Introduced `default_term` argument.
 339   *
 340   * @global array $wp_taxonomies Registered taxonomies.
 341   *
 342   * @param string       $taxonomy    Taxonomy key, must not exceed 32 characters.
 343   * @param array|string $object_type Object type or array of object types with which the taxonomy should be associated.
 344   * @param array|string $args        {
 345   *     Optional. Array or query string of arguments for registering a taxonomy.
 346   *
 347   *     @type string[]      $labels                An array of labels for this taxonomy. By default, Tag labels are
 348   *                                                used for non-hierarchical taxonomies, and Category labels are used
 349   *                                                for hierarchical taxonomies. See accepted values in
 350   *                                                get_taxonomy_labels(). Default empty array.
 351   *     @type string        $description           A short descriptive summary of what the taxonomy is for. Default empty.
 352   *     @type bool          $public                Whether a taxonomy is intended for use publicly either via
 353   *                                                the admin interface or by front-end users. The default settings
 354   *                                                of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus`
 355   *                                                are inherited from `$public`.
 356   *     @type bool          $publicly_queryable    Whether the taxonomy is publicly queryable.
 357   *                                                If not set, the default is inherited from `$public`
 358   *     @type bool          $hierarchical          Whether the taxonomy is hierarchical. Default false.
 359   *     @type bool          $show_ui               Whether to generate and allow a UI for managing terms in this taxonomy in
 360   *                                                the admin. If not set, the default is inherited from `$public`
 361   *                                                (default true).
 362   *     @type bool          $show_in_menu          Whether to show the taxonomy in the admin menu. If true, the taxonomy is
 363   *                                                shown as a submenu of the object type menu. If false, no menu is shown.
 364   *                                                `$show_ui` must be true. If not set, default is inherited from `$show_ui`
 365   *                                                (default true).
 366   *     @type bool          $show_in_nav_menus     Makes this taxonomy available for selection in navigation menus. If not
 367   *                                                set, the default is inherited from `$public` (default true).
 368   *     @type bool          $show_in_rest          Whether to include the taxonomy in the REST API. Set this to true
 369   *                                                for the taxonomy to be available in the block editor.
 370   *     @type string        $rest_base             To change the base url of REST API route. Default is $taxonomy.
 371   *     @type string        $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
 372   *     @type bool          $show_tagcloud         Whether to list the taxonomy in the Tag Cloud Widget controls. If not set,
 373   *                                                the default is inherited from `$show_ui` (default true).
 374   *     @type bool          $show_in_quick_edit    Whether to show the taxonomy in the quick/bulk edit panel. It not set,
 375   *                                                the default is inherited from `$show_ui` (default true).
 376   *     @type bool          $show_admin_column     Whether to display a column for the taxonomy on its post type listing
 377   *                                                screens. Default false.
 378   *     @type bool|callable $meta_box_cb           Provide a callback function for the meta box display. If not set,
 379   *                                                post_categories_meta_box() is used for hierarchical taxonomies, and
 380   *                                                post_tags_meta_box() is used for non-hierarchical. If false, no meta
 381   *                                                box is shown.
 382   *     @type callable      $meta_box_sanitize_cb  Callback function for sanitizing taxonomy data saved from a meta
 383   *                                                box. If no callback is defined, an appropriate one is determined
 384   *                                                based on the value of `$meta_box_cb`.
 385   *     @type string[]      $capabilities {
 386   *         Array of capabilities for this taxonomy.
 387   *
 388   *         @type string $manage_terms Default 'manage_categories'.
 389   *         @type string $edit_terms   Default 'manage_categories'.
 390   *         @type string $delete_terms Default 'manage_categories'.
 391   *         @type string $assign_terms Default 'edit_posts'.
 392   *     }
 393   *     @type bool|array    $rewrite {
 394   *         Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent
 395   *         rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
 396   *
 397   *         @type string $slug         Customize the permastruct slug. Default `$taxonomy` key.
 398   *         @type bool   $with_front   Should the permastruct be prepended with WP_Rewrite::$front. Default true.
 399   *         @type bool   $hierarchical Either hierarchical rewrite tag or not. Default false.
 400   *         @type int    $ep_mask      Assign an endpoint mask. Default `EP_NONE`.
 401   *     }
 402   *     @type string|bool   $query_var             Sets the query var key for this taxonomy. Default `$taxonomy` key. If
 403   *                                                false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a
 404   *                                                string, the query `?{query_var}={term_slug}` will be valid.
 405   *     @type callable      $update_count_callback Works much like a hook, in that it will be called when the count is
 406   *                                                updated. Default _update_post_term_count() for taxonomies attached
 407   *                                                to post types, which confirms that the objects are published before
 408   *                                                counting them. Default _update_generic_term_count() for taxonomies
 409   *                                                attached to other object types, such as users.
 410   *     @type string|array  $default_term {
 411   *         Default term to be used for the taxonomy.
 412   *
 413   *         @type string $name         Name of default term.
 414   *         @type string $slug         Slug for default term. Default empty.
 415   *         @type string $description  Description for default term. Default empty.
 416   *     }
 417   *     @type bool          $sort                  Whether terms in this taxonomy should be sorted in the order they are
 418   *                                                provided to `wp_set_object_terms()`. Default null which equates to false.
 419   *     @type array         $args                  Array of arguments to automatically use inside `wp_get_object_terms()`
 420   *                                                for this taxonomy.
 421   *     @type bool          $_builtin              This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!
 422   *                                                Default false.
 423   * }
 424   * @return WP_Taxonomy|WP_Error The registered taxonomy object on success, WP_Error object on failure.
 425   */
 426  function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
 427      global $wp_taxonomies;
 428  
 429      if ( ! is_array( $wp_taxonomies ) ) {
 430          $wp_taxonomies = array();
 431      }
 432  
 433      $args = wp_parse_args( $args );
 434  
 435      if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
 436          _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2.0' );
 437          return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
 438      }
 439  
 440      $taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args );
 441      $taxonomy_object->add_rewrite_rules();
 442  
 443      $wp_taxonomies[ $taxonomy ] = $taxonomy_object;
 444  
 445      $taxonomy_object->add_hooks();
 446  
 447      // Add default term.
 448      if ( ! empty( $taxonomy_object->default_term ) ) {
 449          $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy );
 450          if ( $term ) {
 451              update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
 452          } else {
 453              $term = wp_insert_term(
 454                  $taxonomy_object->default_term['name'],
 455                  $taxonomy,
 456                  array(
 457                      'slug'        => sanitize_title( $taxonomy_object->default_term['slug'] ),
 458                      'description' => $taxonomy_object->default_term['description'],
 459                  )
 460              );
 461  
 462              // Update `term_id` in options.
 463              if ( ! is_wp_error( $term ) ) {
 464                  update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
 465              }
 466          }
 467      }
 468  
 469      /**
 470       * Fires after a taxonomy is registered.
 471       *
 472       * @since 3.3.0
 473       *
 474       * @param string       $taxonomy    Taxonomy slug.
 475       * @param array|string $object_type Object type or array of object types.
 476       * @param array        $args        Array of taxonomy registration arguments.
 477       */
 478      do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object );
 479  
 480      return $taxonomy_object;
 481  }
 482  
 483  /**
 484   * Unregisters a taxonomy.
 485   *
 486   * Can not be used to unregister built-in taxonomies.
 487   *
 488   * @since 4.5.0
 489   *
 490   * @global WP    $wp            Current WordPress environment instance.
 491   * @global array $wp_taxonomies List of taxonomies.
 492   *
 493   * @param string $taxonomy Taxonomy name.
 494   * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
 495   */
 496  function unregister_taxonomy( $taxonomy ) {
 497      if ( ! taxonomy_exists( $taxonomy ) ) {
 498          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 499      }
 500  
 501      $taxonomy_object = get_taxonomy( $taxonomy );
 502  
 503      // Do not allow unregistering internal taxonomies.
 504      if ( $taxonomy_object->_builtin ) {
 505          return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed.' ) );
 506      }
 507  
 508      global $wp_taxonomies;
 509  
 510      $taxonomy_object->remove_rewrite_rules();
 511      $taxonomy_object->remove_hooks();
 512  
 513      // Remove custom taxonomy default term option.
 514      if ( ! empty( $taxonomy_object->default_term ) ) {
 515          delete_option( 'default_term_' . $taxonomy_object->name );
 516      }
 517  
 518      // Remove the taxonomy.
 519      unset( $wp_taxonomies[ $taxonomy ] );
 520  
 521      /**
 522       * Fires after a taxonomy is unregistered.
 523       *
 524       * @since 4.5.0
 525       *
 526       * @param string $taxonomy Taxonomy name.
 527       */
 528      do_action( 'unregistered_taxonomy', $taxonomy );
 529  
 530      return true;
 531  }
 532  
 533  /**
 534   * Builds an object with all taxonomy labels out of a taxonomy object.
 535   *
 536   * @since 3.0.0
 537   * @since 4.3.0 Added the `no_terms` label.
 538   * @since 4.4.0 Added the `items_list_navigation` and `items_list` labels.
 539   * @since 4.9.0 Added the `most_used` and `back_to_items` labels.
 540   * @since 5.7.0 Added the `filter_by_item` label.
 541   *
 542   * @param WP_Taxonomy $tax Taxonomy object.
 543   * @return object {
 544   *     Taxonomy labels object. The first default value is for non-hierarchical taxonomies
 545   *     (like tags) and the second one is for hierarchical taxonomies (like categories).
 546   *
 547   *     @type string $name                       General name for the taxonomy, usually plural. The same
 548   *                                              as and overridden by `$tax->label`. Default 'Tags'/'Categories'.
 549   *     @type string $singular_name              Name for one object of this taxonomy. Default 'Tag'/'Category'.
 550   *     @type string $search_items               Default 'Search Tags'/'Search Categories'.
 551   *     @type string $popular_items              This label is only used for non-hierarchical taxonomies.
 552   *                                              Default 'Popular Tags'.
 553   *     @type string $all_items                  Default 'All Tags'/'All Categories'.
 554   *     @type string $parent_item                This label is only used for hierarchical taxonomies. Default
 555   *                                              'Parent Category'.
 556   *     @type string $parent_item_colon          The same as `parent_item`, but with colon `:` in the end.
 557   *     @type string $edit_item                  Default 'Edit Tag'/'Edit Category'.
 558   *     @type string $view_item                  Default 'View Tag'/'View Category'.
 559   *     @type string $update_item                Default 'Update Tag'/'Update Category'.
 560   *     @type string $add_new_item               Default 'Add New Tag'/'Add New Category'.
 561   *     @type string $new_item_name              Default 'New Tag Name'/'New Category Name'.
 562   *     @type string $separate_items_with_commas This label is only used for non-hierarchical taxonomies. Default
 563   *                                              'Separate tags with commas', used in the meta box.
 564   *     @type string $add_or_remove_items        This label is only used for non-hierarchical taxonomies. Default
 565   *                                              'Add or remove tags', used in the meta box when JavaScript
 566   *                                              is disabled.
 567   *     @type string $choose_from_most_used      This label is only used on non-hierarchical taxonomies. Default
 568   *                                              'Choose from the most used tags', used in the meta box.
 569   *     @type string $not_found                  Default 'No tags found'/'No categories found', used in
 570   *                                              the meta box and taxonomy list table.
 571   *     @type string $no_terms                   Default 'No tags'/'No categories', used in the posts and media
 572   *                                              list tables.
 573   *     @type string $filter_by_item             This label is only used for hierarchical taxonomies. Default
 574   *                                              'Filter by category', used in the posts list table.
 575   *     @type string $items_list_navigation      Label for the table pagination hidden heading.
 576   *     @type string $items_list                 Label for the table hidden heading.
 577   *     @type string $most_used                  Title for the Most Used tab. Default 'Most Used'.
 578   *     @type string $back_to_items              Label displayed after a term has been updated.
 579   * }
 580   */
 581  function get_taxonomy_labels( $tax ) {
 582      $tax->labels = (array) $tax->labels;
 583  
 584      if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
 585          $tax->labels['separate_items_with_commas'] = $tax->helps;
 586      }
 587  
 588      if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
 589          $tax->labels['not_found'] = $tax->no_tagcloud;
 590      }
 591  
 592      $nohier_vs_hier_defaults = array(
 593          'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
 594          'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
 595          'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
 596          'popular_items'              => array( __( 'Popular Tags' ), null ),
 597          'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
 598          'parent_item'                => array( null, __( 'Parent Category' ) ),
 599          'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
 600          'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
 601          'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
 602          'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
 603          'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
 604          'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
 605          'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
 606          'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
 607          'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
 608          'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
 609          'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
 610          'filter_by_item'             => array( null, __( 'Filter by category' ) ),
 611          'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
 612          'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
 613          /* translators: Tab heading when selecting from the most used terms. */
 614          'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
 615          'back_to_items'              => array( __( '&larr; Go to Tags' ), __( '&larr; Go to Categories' ) ),
 616      );
 617      $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
 618  
 619      $labels = _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
 620  
 621      $taxonomy = $tax->name;
 622  
 623      $default_labels = clone $labels;
 624  
 625      /**
 626       * Filters the labels of a specific taxonomy.
 627       *
 628       * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
 629       *
 630       * @since 4.4.0
 631       *
 632       * @see get_taxonomy_labels() for the full list of taxonomy labels.
 633       *
 634       * @param object $labels Object with labels for the taxonomy as member variables.
 635       */
 636      $labels = apply_filters( "taxonomy_labels_{$taxonomy}", $labels );
 637  
 638      // Ensure that the filtered labels contain all required default values.
 639      $labels = (object) array_merge( (array) $default_labels, (array) $labels );
 640  
 641      return $labels;
 642  }
 643  
 644  /**
 645   * Add an already registered taxonomy to an object type.
 646   *
 647   * @since 3.0.0
 648   *
 649   * @global array $wp_taxonomies The registered taxonomies.
 650   *
 651   * @param string $taxonomy    Name of taxonomy object.
 652   * @param string $object_type Name of the object type.
 653   * @return bool True if successful, false if not.
 654   */
 655  function register_taxonomy_for_object_type( $taxonomy, $object_type ) {
 656      global $wp_taxonomies;
 657  
 658      if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
 659          return false;
 660      }
 661  
 662      if ( ! get_post_type_object( $object_type ) ) {
 663          return false;
 664      }
 665  
 666      if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ) ) {
 667          $wp_taxonomies[ $taxonomy ]->object_type[] = $object_type;
 668      }
 669  
 670      // Filter out empties.
 671      $wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type );
 672  
 673      /**
 674       * Fires after a taxonomy is registered for an object type.
 675       *
 676       * @since 5.1.0
 677       *
 678       * @param string $taxonomy    Taxonomy name.
 679       * @param string $object_type Name of the object type.
 680       */
 681      do_action( 'registered_taxonomy_for_object_type', $taxonomy, $object_type );
 682  
 683      return true;
 684  }
 685  
 686  /**
 687   * Remove an already registered taxonomy from an object type.
 688   *
 689   * @since 3.7.0
 690   *
 691   * @global array $wp_taxonomies The registered taxonomies.
 692   *
 693   * @param string $taxonomy    Name of taxonomy object.
 694   * @param string $object_type Name of the object type.
 695   * @return bool True if successful, false if not.
 696   */
 697  function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
 698      global $wp_taxonomies;
 699  
 700      if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
 701          return false;
 702      }
 703  
 704      if ( ! get_post_type_object( $object_type ) ) {
 705          return false;
 706      }
 707  
 708      $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
 709      if ( false === $key ) {
 710          return false;
 711      }
 712  
 713      unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
 714  
 715      /**
 716       * Fires after a taxonomy is unregistered for an object type.
 717       *
 718       * @since 5.1.0
 719       *
 720       * @param string $taxonomy    Taxonomy name.
 721       * @param string $object_type Name of the object type.
 722       */
 723      do_action( 'unregistered_taxonomy_for_object_type', $taxonomy, $object_type );
 724  
 725      return true;
 726  }
 727  
 728  //
 729  // Term API.
 730  //
 731  
 732  /**
 733   * Retrieve object_ids of valid taxonomy and term.
 734   *
 735   * The strings of $taxonomies must exist before this function will continue.
 736   * On failure of finding a valid taxonomy, it will return a WP_Error class,
 737   * kind of like Exceptions in PHP 5, except you can't catch them. Even so,
 738   * you can still test for the WP_Error class and get the error message.
 739   *
 740   * The $terms aren't checked the same as $taxonomies, but still need to exist
 741   * for $object_ids to be returned.
 742   *
 743   * It is possible to change the order that object_ids is returned by either
 744   * using PHP sort family functions or using the database by using $args with
 745   * either ASC or DESC array. The value should be in the key named 'order'.
 746   *
 747   * @since 2.3.0
 748   *
 749   * @global wpdb $wpdb WordPress database abstraction object.
 750   *
 751   * @param int|array    $term_ids   Term ID or array of term IDs of terms that will be used.
 752   * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
 753   * @param array|string $args       Change the order of the object_ids, either ASC or DESC.
 754   * @return array|WP_Error An array of $object_ids on success, WP_Error if the taxonomy does not exist.
 755   */
 756  function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
 757      global $wpdb;
 758  
 759      if ( ! is_array( $term_ids ) ) {
 760          $term_ids = array( $term_ids );
 761      }
 762      if ( ! is_array( $taxonomies ) ) {
 763          $taxonomies = array( $taxonomies );
 764      }
 765      foreach ( (array) $taxonomies as $taxonomy ) {
 766          if ( ! taxonomy_exists( $taxonomy ) ) {
 767              return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 768          }
 769      }
 770  
 771      $defaults = array( 'order' => 'ASC' );
 772      $args     = wp_parse_args( $args, $defaults );
 773  
 774      $order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
 775  
 776      $term_ids = array_map( 'intval', $term_ids );
 777  
 778      $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
 779      $term_ids   = "'" . implode( "', '", $term_ids ) . "'";
 780  
 781      $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";
 782  
 783      $last_changed = wp_cache_get_last_changed( 'terms' );
 784      $cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
 785      $cache        = wp_cache_get( $cache_key, 'terms' );
 786      if ( false === $cache ) {
 787          $object_ids = $wpdb->get_col( $sql );
 788          wp_cache_set( $cache_key, $object_ids, 'terms' );
 789      } else {
 790          $object_ids = (array) $cache;
 791      }
 792  
 793      if ( ! $object_ids ) {
 794          return array();
 795      }
 796      return $object_ids;
 797  }
 798  
 799  /**
 800   * Given a taxonomy query, generates SQL to be appended to a main query.
 801   *
 802   * @since 3.1.0
 803   *
 804   * @see WP_Tax_Query
 805   *
 806   * @param array  $tax_query         A compact tax query
 807   * @param string $primary_table
 808   * @param string $primary_id_column
 809   * @return array
 810   */
 811  function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
 812      $tax_query_obj = new WP_Tax_Query( $tax_query );
 813      return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
 814  }
 815  
 816  /**
 817   * Get all Term data from database by Term ID.
 818   *
 819   * The usage of the get_term function is to apply filters to a term object. It
 820   * is possible to get a term object from the database before applying the
 821   * filters.
 822   *
 823   * $term ID must be part of $taxonomy, to get from the database. Failure, might
 824   * be able to be captured by the hooks. Failure would be the same value as $wpdb
 825   * returns for the get_row method.
 826   *
 827   * There are two hooks, one is specifically for each term, named 'get_term', and
 828   * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
 829   * term object, and the taxonomy name as parameters. Both hooks are expected to
 830   * return a Term object.
 831   *
 832   * {@see 'get_term'} hook - Takes two parameters the term Object and the taxonomy name.
 833   * Must return term object. Used in get_term() as a catch-all filter for every
 834   * $term.
 835   *
 836   * {@see 'get_$taxonomy'} hook - Takes two parameters the term Object and the taxonomy
 837   * name. Must return term object. $taxonomy will be the taxonomy name, so for
 838   * example, if 'category', it would be 'get_category' as the filter name. Useful
 839   * for custom taxonomies or plugging into default taxonomies.
 840   *
 841   * @todo Better formatting for DocBlock
 842   *
 843   * @since 2.3.0
 844   * @since 4.4.0 Converted to return a WP_Term object if `$output` is `OBJECT`.
 845   *              The `$taxonomy` parameter was made optional.
 846   *
 847   * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 848   *
 849   * @param int|WP_Term|object $term     If integer, term data will be fetched from the database,
 850   *                                     or from the cache if available.
 851   *                                     If stdClass object (as in the results of a database query),
 852   *                                     will apply filters and return a `WP_Term` object with the `$term` data.
 853   *                                     If `WP_Term`, will return `$term`.
 854   * @param string             $taxonomy Optional. Taxonomy name that `$term` is part of.
 855   * @param string             $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 856   *                                     correspond to a WP_Term object, an associative array, or a numeric array,
 857   *                                     respectively. Default OBJECT.
 858   * @param string             $filter   Optional. How to sanitize term fields. Default 'raw'.
 859   * @return WP_Term|array|WP_Error|null WP_Term instance (or array) on success, depending on the `$output` value.
 860   *                                     WP_Error if `$taxonomy` does not exist. Null for miscellaneous failure.
 861   */
 862  function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
 863      if ( empty( $term ) ) {
 864          return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
 865      }
 866  
 867      if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
 868          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
 869      }
 870  
 871      if ( $term instanceof WP_Term ) {
 872          $_term = $term;
 873      } elseif ( is_object( $term ) ) {
 874          if ( empty( $term->filter ) || 'raw' === $term->filter ) {
 875              $_term = sanitize_term( $term, $taxonomy, 'raw' );
 876              $_term = new WP_Term( $_term );
 877          } else {
 878              $_term = WP_Term::get_instance( $term->term_id );
 879          }
 880      } else {
 881          $_term = WP_Term::get_instance( $term, $taxonomy );
 882      }
 883  
 884      if ( is_wp_error( $_term ) ) {
 885          return $_term;
 886      } elseif ( ! $_term ) {
 887          return null;
 888      }
 889  
 890      // Ensure for filters that this is not empty.
 891      $taxonomy = $_term->taxonomy;
 892  
 893      /**
 894       * Filters a taxonomy term object.
 895       *
 896       * The {@see 'get_$taxonomy'} hook is also available for targeting a specific
 897       * taxonomy.
 898       *
 899       * @since 2.3.0
 900       * @since 4.4.0 `$_term` is now a `WP_Term` object.
 901       *
 902       * @param WP_Term $_term    Term object.
 903       * @param string  $taxonomy The taxonomy slug.
 904       */
 905      $_term = apply_filters( 'get_term', $_term, $taxonomy );
 906  
 907      /**
 908       * Filters a taxonomy term object.
 909       *
 910       * The dynamic portion of the filter name, `$taxonomy`, refers
 911       * to the slug of the term's taxonomy.
 912       *
 913       * @since 2.3.0
 914       * @since 4.4.0 `$_term` is now a `WP_Term` object.
 915       *
 916       * @param WP_Term $_term    Term object.
 917       * @param string  $taxonomy The taxonomy slug.
 918       */
 919      $_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );
 920  
 921      // Bail if a filter callback has changed the type of the `$_term` object.
 922      if ( ! ( $_term instanceof WP_Term ) ) {
 923          return $_term;
 924      }
 925  
 926      // Sanitize term, according to the specified filter.
 927      $_term->filter( $filter );
 928  
 929      if ( ARRAY_A === $output ) {
 930          return $_term->to_array();
 931      } elseif ( ARRAY_N === $output ) {
 932          return array_values( $_term->to_array() );
 933      }
 934  
 935      return $_term;
 936  }
 937  
 938  /**
 939   * Get all Term data from database by Term field and data.
 940   *
 941   * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
 942   * required.
 943   *
 944   * The default $field is 'id', therefore it is possible to also use null for
 945   * field, but not recommended that you do so.
 946   *
 947   * If $value does not exist, the return value will be false. If $taxonomy exists
 948   * and $field and $value combinations exist, the Term will be returned.
 949   *
 950   * This function will always return the first term that matches the `$field`-
 951   * `$value`-`$taxonomy` combination specified in the parameters. If your query
 952   * is likely to match more than one term (as is likely to be the case when
 953   * `$field` is 'name', for example), consider using get_terms() instead; that
 954   * way, you will get all matching terms, and can provide your own logic for
 955   * deciding which one was intended.
 956   *
 957   * @todo Better formatting for DocBlock.
 958   *
 959   * @since 2.3.0
 960   * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
 961   *              a WP_Term object if `$output` is `OBJECT`.
 962   * @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
 963   *
 964   * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
 965   *
 966   * @param string     $field    Either 'slug', 'name', 'id' or 'ID' (term_id), or 'term_taxonomy_id'.
 967   * @param string|int $value    Search for this term value.
 968   * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 969   * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 970   *                             correspond to a WP_Term object, an associative array, or a numeric array,
 971   *                             respectively. Default OBJECT.
 972   * @param string     $filter   Optional. How to sanitize term fields. Default 'raw'.
 973   * @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
 974   *                             False if `$taxonomy` does not exist or `$term` was not found.
 975   */
 976  function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
 977  
 978      // 'term_taxonomy_id' lookups don't require taxonomy checks.
 979      if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
 980          return false;
 981      }
 982  
 983      // No need to perform a query for empty 'slug' or 'name'.
 984      if ( 'slug' === $field || 'name' === $field ) {
 985          $value = (string) $value;
 986  
 987          if ( 0 === strlen( $value ) ) {
 988              return false;
 989          }
 990      }
 991  
 992      if ( 'id' === $field || 'ID' === $field || 'term_id' === $field ) {
 993          $term = get_term( (int) $value, $taxonomy, $output, $filter );
 994          if ( is_wp_error( $term ) || null === $term ) {
 995              $term = false;
 996          }
 997          return $term;
 998      }
 999  
1000      $args = array(
1001          'get'                    => 'all',
1002          'number'                 => 1,
1003          'taxonomy'               => $taxonomy,
1004          'update_term_meta_cache' => false,
1005          'orderby'                => 'none',
1006          'suppress_filter'        => true,
1007      );
1008  
1009      switch ( $field ) {
1010          case 'slug':
1011              $args['slug'] = $value;
1012              break;
1013          case 'name':
1014              $args['name'] = $value;
1015              break;
1016          case 'term_taxonomy_id':
1017              $args['term_taxonomy_id'] = $value;
1018              unset( $args['taxonomy'] );
1019              break;
1020          default:
1021              return false;
1022      }
1023  
1024      $terms = get_terms( $args );
1025      if ( is_wp_error( $terms ) || empty( $terms ) ) {
1026          return false;
1027      }
1028  
1029      $term = array_shift( $terms );
1030  
1031      // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
1032      if ( 'term_taxonomy_id' === $field ) {
1033          $taxonomy = $term->taxonomy;
1034      }
1035  
1036      return get_term( $term, $taxonomy, $output, $filter );
1037  }
1038  
1039  /**
1040   * Merge all term children into a single array of their IDs.
1041   *
1042   * This recursive function will merge all of the children of $term into the same
1043   * array of term IDs. Only useful for taxonomies which are hierarchical.
1044   *
1045   * Will return an empty array if $term does not exist in $taxonomy.
1046   *
1047   * @since 2.3.0
1048   *
1049   * @param int    $term_id  ID of Term to get children.
1050   * @param string $taxonomy Taxonomy Name.
1051   * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
1052   */
1053  function get_term_children( $term_id, $taxonomy ) {
1054      if ( ! taxonomy_exists( $taxonomy ) ) {
1055          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
1056      }
1057  
1058      $term_id = (int) $term_id;
1059  
1060      $terms = _get_term_hierarchy( $taxonomy );
1061  
1062      if ( ! isset( $terms[ $term_id ] ) ) {
1063          return array();
1064      }
1065  
1066      $children = $terms[ $term_id ];
1067  
1068      foreach ( (array) $terms[ $term_id ] as $child ) {
1069          if ( $term_id === $child ) {
1070              continue;
1071          }
1072  
1073          if ( isset( $terms[ $child ] ) ) {
1074              $children = array_merge( $children, get_term_children( $child, $taxonomy ) );
1075          }
1076      }
1077  
1078      return $children;
1079  }
1080  
1081  /**
1082   * Get sanitized Term field.
1083   *
1084   * The function is for contextual reasons and for simplicity of usage.
1085   *
1086   * @since 2.3.0
1087   * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
1088   *
1089   * @see sanitize_term_field()
1090   *
1091   * @param string      $field    Term field to fetch.
1092   * @param int|WP_Term $term     Term ID or object.
1093   * @param string      $taxonomy Optional. Taxonomy Name. Default empty.
1094   * @param string      $context  Optional. How to sanitize term fields. Look at sanitize_term_field() for available options.
1095   *                              Default 'display'.
1096   * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
1097   */
1098  function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
1099      $term = get_term( $term, $taxonomy );
1100      if ( is_wp_error( $term ) ) {
1101          return $term;
1102      }
1103  
1104      if ( ! is_object( $term ) ) {
1105          return '';
1106      }
1107  
1108      if ( ! isset( $term->$field ) ) {
1109          return '';
1110      }
1111  
1112      return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
1113  }
1114  
1115  /**
1116   * Sanitizes Term for editing.
1117   *
1118   * Return value is sanitize_term() and usage is for sanitizing the term for
1119   * editing. Function is for contextual and simplicity.
1120   *
1121   * @since 2.3.0
1122   *
1123   * @param int|object $id       Term ID or object.
1124   * @param string     $taxonomy Taxonomy name.
1125   * @return string|int|null|WP_Error Will return empty string if $term is not an object.
1126   */
1127  function get_term_to_edit( $id, $taxonomy ) {
1128      $term = get_term( $id, $taxonomy );
1129  
1130      if ( is_wp_error( $term ) ) {
1131          return $term;
1132      }
1133  
1134      if ( ! is_object( $term ) ) {
1135          return '';
1136      }
1137  
1138      return sanitize_term( $term, $taxonomy, 'edit' );
1139  }
1140  
1141  /**
1142   * Retrieves the terms in a given taxonomy or list of taxonomies.
1143   *
1144   * You can fully inject any customizations to the query before it is sent, as
1145   * well as control the output with a filter.
1146   *
1147   * The return type varies depending on the value passed to `$args['fields']`. See
1148   * WP_Term_Query::get_terms() for details. In all cases, a `WP_Error` object will
1149   * be returned if an invalid taxonomy is requested.
1150   *
1151   * The {@see 'get_terms'} filter will be called when the cache has the term and will
1152   * pass the found term along with the array of $taxonomies and array of $args.
1153   * This filter is also called before the array of terms is passed and will pass
1154   * the array of terms, along with the $taxonomies and $args.
1155   *
1156   * The {@see 'list_terms_exclusions'} filter passes the compiled exclusions along with
1157   * the $args.
1158   *
1159   * The {@see 'get_terms_orderby'} filter passes the `ORDER BY` clause for the query
1160   * along with the $args array.
1161   *
1162   * Prior to 4.5.0, the first parameter of `get_terms()` was a taxonomy or list of taxonomies:
1163   *
1164   *     $terms = get_terms( 'post_tag', array(
1165   *         'hide_empty' => false,
1166   *     ) );
1167   *
1168   * Since 4.5.0, taxonomies should be passed via the 'taxonomy' argument in the `$args` array:
1169   *
1170   *     $terms = get_terms( array(
1171   *         'taxonomy' => 'post_tag',
1172   *         'hide_empty' => false,
1173   *     ) );
1174   *
1175   * @since 2.3.0
1176   * @since 4.2.0 Introduced 'name' and 'childless' parameters.
1177   * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter.
1178   *              Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return
1179   *              a list of WP_Term objects.
1180   * @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
1181   *              Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
1182   * @since 4.8.0 Introduced 'suppress_filter' parameter.
1183   *
1184   * @internal The `$deprecated` parameter is parsed for backward compatibility only.
1185   *
1186   * @param array|string $args       Optional. Array or string of arguments. See WP_Term_Query::__construct()
1187   *                                 for information on accepted arguments. Default empty array.
1188   * @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format.
1189   *                                 If present, this parameter will be interpreted as `$args`, and the first
1190   *                                 function parameter will be parsed as a taxonomy or array of taxonomies.
1191   *                                 Default empty.
1192   * @return WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string,
1193   *                                                  or WP_Error if any of the taxonomies do not exist.
1194   *                                                  See the function description for more information.
1195   */
1196  function get_terms( $args = array(), $deprecated = '' ) {
1197      $term_query = new WP_Term_Query();
1198  
1199      $defaults = array(
1200          'suppress_filter' => false,
1201      );
1202  
1203      /*
1204       * Legacy argument format ($taxonomy, $args) takes precedence.
1205       *
1206       * We detect legacy argument format by checking if
1207       * (a) a second non-empty parameter is passed, or
1208       * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
1209       */
1210      $_args          = wp_parse_args( $args );
1211      $key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
1212      $do_legacy_args = $deprecated || empty( $key_intersect );
1213  
1214      if ( $do_legacy_args ) {
1215          $taxonomies       = (array) $args;
1216          $args             = wp_parse_args( $deprecated, $defaults );
1217          $args['taxonomy'] = $taxonomies;
1218      } else {
1219          $args = wp_parse_args( $args, $defaults );
1220          if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
1221              $args['taxonomy'] = (array) $args['taxonomy'];
1222          }
1223      }
1224  
1225      if ( ! empty( $args['taxonomy'] ) ) {
1226          foreach ( $args['taxonomy'] as $taxonomy ) {
1227              if ( ! taxonomy_exists( $taxonomy ) ) {
1228                  return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
1229              }
1230          }
1231      }
1232  
1233      // Don't pass suppress_filter to WP_Term_Query.
1234      $suppress_filter = $args['suppress_filter'];
1235      unset( $args['suppress_filter'] );
1236  
1237      $terms = $term_query->query( $args );
1238  
1239      // Count queries are not filtered, for legacy reasons.
1240      if ( ! is_array( $terms ) ) {
1241          return $terms;
1242      }
1243  
1244      if ( $suppress_filter ) {
1245          return $terms;
1246      }
1247  
1248      /**
1249       * Filters the found terms.
1250       *
1251       * @since 2.3.0
1252       * @since 4.6.0 Added the `$term_query` parameter.
1253       *
1254       * @param array         $terms      Array of found terms.
1255       * @param array         $taxonomies An array of taxonomies.
1256       * @param array         $args       An array of get_terms() arguments.
1257       * @param WP_Term_Query $term_query The WP_Term_Query object.
1258       */
1259      return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query );
1260  }
1261  
1262  /**
1263   * Adds metadata to a term.
1264   *
1265   * @since 4.4.0
1266   *
1267   * @param int    $term_id    Term ID.
1268   * @param string $meta_key   Metadata name.
1269   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1270   * @param bool   $unique     Optional. Whether the same key should not be added.
1271   *                           Default false.
1272   * @return int|false|WP_Error Meta ID on success, false on failure.
1273   *                            WP_Error when term_id is ambiguous between taxonomies.
1274   */
1275  function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
1276      if ( wp_term_is_shared( $term_id ) ) {
1277          return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
1278      }
1279  
1280      return add_metadata( 'term', $term_id, $meta_key, $meta_value, $unique );
1281  }
1282  
1283  /**
1284   * Removes metadata matching criteria from a term.
1285   *
1286   * @since 4.4.0
1287   *
1288   * @param int    $term_id    Term ID.
1289   * @param string $meta_key   Metadata name.
1290   * @param mixed  $meta_value Optional. Metadata value. If provided,
1291   *                           rows will only be removed that match the value.
1292   *                           Must be serializable if non-scalar. Default empty.
1293   * @return bool True on success, false on failure.
1294   */
1295  function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
1296      return delete_metadata( 'term', $term_id, $meta_key, $meta_value );
1297  }
1298  
1299  /**
1300   * Retrieves metadata for a term.
1301   *
1302   * @since 4.4.0
1303   *
1304   * @param int    $term_id Term ID.
1305   * @param string $key     Optional. The meta key to retrieve. By default,
1306   *                        returns data for all keys. Default empty.
1307   * @param bool   $single  Optional. Whether to return a single value.
1308   *                        This parameter has no effect if $key is not specified.
1309   *                        Default false.
1310   * @return mixed An array if $single is false. The value of the meta field
1311   *               if $single is true. False for an invalid $term_id.
1312   */
1313  function get_term_meta( $term_id, $key = '', $single = false ) {
1314      return get_metadata( 'term', $term_id, $key, $single );
1315  }
1316  
1317  /**
1318   * Updates term metadata.
1319   *
1320   * Use the `$prev_value` parameter to differentiate between meta fields with the same key and term ID.
1321   *
1322   * If the meta field for the term does not exist, it will be added.
1323   *
1324   * @since 4.4.0
1325   *
1326   * @param int    $term_id    Term ID.
1327   * @param string $meta_key   Metadata key.
1328   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1329   * @param mixed  $prev_value Optional. Previous value to check before updating.
1330   *                           If specified, only update existing metadata entries with
1331   *                           this value. Otherwise, update all entries. Default empty.
1332   * @return int|bool|WP_Error Meta ID if the key didn't exist. true on successful update,
1333   *                           false on failure or if the value passed to the function
1334   *                           is the same as the one that is already in the database.
1335   *                           WP_Error when term_id is ambiguous between taxonomies.
1336   */
1337  function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
1338      if ( wp_term_is_shared( $term_id ) ) {
1339          return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' ), $term_id );
1340      }
1341  
1342      return update_metadata( 'term', $term_id, $meta_key, $meta_value, $prev_value );
1343  }
1344  
1345  /**
1346   * Updates metadata cache for list of term IDs.
1347   *
1348   * Performs SQL query to retrieve all metadata for the terms matching `$term_ids` and stores them in the cache.
1349   * Subsequent calls to `get_term_meta()` will not need to query the database.
1350   *
1351   * @since 4.4.0
1352   *
1353   * @param array $term_ids List of term IDs.
1354   * @return array|false An array of metadata on success, false if there is nothing to update.
1355   */
1356  function update_termmeta_cache( $term_ids ) {
1357      return update_meta_cache( 'term', $term_ids );
1358  }
1359  
1360  /**
1361   * Get all meta data, including meta IDs, for the given term ID.
1362   *
1363   * @since 4.9.0
1364   *
1365   * @global wpdb $wpdb WordPress database abstraction object.
1366   *
1367   * @param int $term_id Term ID.
1368   * @return array|false Array with meta data, or false when the meta table is not installed.
1369   */
1370  function has_term_meta( $term_id ) {
1371      $check = wp_check_term_meta_support_prefilter( null );
1372      if ( null !== $check ) {
1373          return $check;
1374      }
1375  
1376      global $wpdb;
1377  
1378      return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, term_id FROM $wpdb->termmeta WHERE term_id = %d ORDER BY meta_key,meta_id", $term_id ), ARRAY_A );
1379  }
1380  
1381  /**
1382   * Registers a meta key for terms.
1383   *
1384   * @since 4.9.8
1385   *
1386   * @param string $taxonomy Taxonomy to register a meta key for. Pass an empty string
1387   *                         to register the meta key across all existing taxonomies.
1388   * @param string $meta_key The meta key to register.
1389   * @param array  $args     Data used to describe the meta key when registered. See
1390   *                         {@see register_meta()} for a list of supported arguments.
1391   * @return bool True if the meta key was successfully registered, false if not.
1392   */
1393  function register_term_meta( $taxonomy, $meta_key, array $args ) {
1394      $args['object_subtype'] = $taxonomy;
1395  
1396      return register_meta( 'term', $meta_key, $args );
1397  }
1398  
1399  /**
1400   * Unregisters a meta key for terms.
1401   *
1402   * @since 4.9.8
1403   *
1404   * @param string $taxonomy Taxonomy the meta key is currently registered for. Pass
1405   *                         an empty string if the meta key is registered across all
1406   *                         existing taxonomies.
1407   * @param string $meta_key The meta key to unregister.
1408   * @return bool True on success, false if the meta key was not previously registered.
1409   */
1410  function unregister_term_meta( $taxonomy, $meta_key ) {
1411      return unregister_meta_key( 'term', $meta_key, $taxonomy );
1412  }
1413  
1414  /**
1415   * Determines whether a taxonomy term exists.
1416   *
1417   * Formerly is_term(), introduced in 2.3.0.
1418   *
1419   * For more information on this and similar theme functions, check out
1420   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1421   * Conditional Tags} article in the Theme Developer Handbook.
1422   *
1423   * @since 3.0.0
1424   *
1425   * @global wpdb $wpdb WordPress database abstraction object.
1426   *
1427   * @param int|string $term     The term to check. Accepts term ID, slug, or name.
1428   * @param string     $taxonomy Optional. The taxonomy name to use.
1429   * @param int        $parent   Optional. ID of parent term under which to confine the exists search.
1430   * @return mixed Returns null if the term does not exist.
1431   *               Returns the term ID if no taxonomy is specified and the term ID exists.
1432   *               Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
1433   *               Returns 0 if term ID 0 is passed to the function.
1434   */
1435  function term_exists( $term, $taxonomy = '', $parent = null ) {
1436      global $wpdb;
1437  
1438      $select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
1439      $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 ";
1440  
1441      if ( is_int( $term ) ) {
1442          if ( 0 === $term ) {
1443              return 0;
1444          }
1445          $where = 't.term_id = %d';
1446          if ( ! empty( $taxonomy ) ) {
1447              // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
1448              return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
1449          } else {
1450              return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
1451          }
1452      }
1453  
1454      $term = trim( wp_unslash( $term ) );
1455      $slug = sanitize_title( $term );
1456  
1457      $where             = 't.slug = %s';
1458      $else_where        = 't.name = %s';
1459      $where_fields      = array( $slug );
1460      $else_where_fields = array( $term );
1461      $orderby           = 'ORDER BY t.term_id ASC';
1462      $limit             = 'LIMIT 1';
1463      if ( ! empty( $taxonomy ) ) {
1464          if ( is_numeric( $parent ) ) {
1465              $parent              = (int) $parent;
1466              $where_fields[]      = $parent;
1467              $else_where_fields[] = $parent;
1468              $where              .= ' AND tt.parent = %d';
1469              $else_where         .= ' AND tt.parent = %d';
1470          }
1471  
1472          $where_fields[]      = $taxonomy;
1473          $else_where_fields[] = $taxonomy;
1474  
1475          $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 );
1476          if ( $result ) {
1477              return $result;
1478          }
1479  
1480          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 );
1481      }
1482  
1483      // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
1484      $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) );
1485      if ( $result ) {
1486          return $result;
1487      }
1488  
1489      // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
1490      return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
1491  }
1492  
1493  /**
1494   * Check if a term is an ancestor of another term.
1495   *
1496   * You can use either an ID or the term object for both parameters.
1497   *
1498   * @since 3.4.0
1499   *
1500   * @param int|object $term1    ID or object to check if this is the parent term.
1501   * @param int|object $term2    The child term.
1502   * @param string     $taxonomy Taxonomy name that $term1 and `$term2` belong to.
1503   * @return bool Whether `$term2` is a child of `$term1`.
1504   */
1505  function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
1506      if ( ! isset( $term1->term_id ) ) {
1507          $term1 = get_term( $term1, $taxonomy );
1508      }
1509      if ( ! isset( $term2->parent ) ) {
1510          $term2 = get_term( $term2, $taxonomy );
1511      }
1512  
1513      if ( empty( $term1->term_id ) || empty( $term2->parent ) ) {
1514          return false;
1515      }
1516      if ( $term2->parent === $term1->term_id ) {
1517          return true;
1518      }
1519  
1520      return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
1521  }
1522  
1523  /**
1524   * Sanitize all term fields.
1525   *
1526   * Relies on sanitize_term_field() to sanitize the term. The difference is that
1527   * this function will sanitize **all** fields. The context is based
1528   * on sanitize_term_field().
1529   *
1530   * The `$term` is expected to be either an array or an object.
1531   *
1532   * @since 2.3.0
1533   *
1534   * @param array|object $term     The term to check.
1535   * @param string       $taxonomy The taxonomy name to use.
1536   * @param string       $context  Optional. Context in which to sanitize the term.
1537   *                               Accepts 'raw', 'edit', 'db', 'display', 'rss',
1538   *                               'attribute', or 'js'. Default 'display'.
1539   * @return array|object Term with all fields sanitized.
1540   */
1541  function sanitize_term( $term, $taxonomy, $context = 'display' ) {
1542      $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
1543  
1544      $do_object = is_object( $term );
1545  
1546      $term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );
1547  
1548      foreach ( (array) $fields as $field ) {
1549          if ( $do_object ) {
1550              if ( isset( $term->$field ) ) {
1551                  $term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
1552              }
1553          } else {
1554              if ( isset( $term[ $field ] ) ) {
1555                  $term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context );
1556              }
1557          }
1558      }
1559  
1560      if ( $do_object ) {
1561          $term->filter = $context;
1562      } else {
1563          $term['filter'] = $context;
1564      }
1565  
1566      return $term;
1567  }
1568  
1569  /**
1570   * Cleanse the field value in the term based on the context.
1571   *
1572   * Passing a term field value through the function should be assumed to have
1573   * cleansed the value for whatever context the term field is going to be used.
1574   *
1575   * If no context or an unsupported context is given, then default filters will
1576   * be applied.
1577   *
1578   * There are enough filters for each context to support a custom filtering
1579   * without creating your own filter function. Simply create a function that
1580   * hooks into the filter you need.
1581   *
1582   * @since 2.3.0
1583   *
1584   * @param string $field    Term field to sanitize.
1585   * @param string $value    Search for this term value.
1586   * @param int    $term_id  Term ID.
1587   * @param string $taxonomy Taxonomy Name.
1588   * @param string $context  Context in which to sanitize the term field.
1589   *                         Accepts 'raw', 'edit', 'db', 'display', 'rss',
1590   *                         'attribute', or 'js'. Default 'display'.
1591   * @return mixed Sanitized field.
1592   */
1593  function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
1594      $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
1595      if ( in_array( $field, $int_fields, true ) ) {
1596          $value = (int) $value;
1597          if ( $value < 0 ) {
1598              $value = 0;
1599          }
1600      }
1601  
1602      $context = strtolower( $context );
1603  
1604      if ( 'raw' === $context ) {
1605          return $value;
1606      }
1607  
1608      if ( 'edit' === $context ) {
1609  
1610          /**
1611           * Filters a term field to edit before it is sanitized.
1612           *
1613           * The dynamic portion of the filter name, `$field`, refers to the term field.
1614           *
1615           * @since 2.3.0
1616           *
1617           * @param mixed $value     Value of the term field.
1618           * @param int   $term_id   Term ID.
1619           * @param string $taxonomy Taxonomy slug.
1620           */
1621          $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );
1622  
1623          /**
1624           * Filters the taxonomy field to edit before it is sanitized.
1625           *
1626           * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
1627           * to the taxonomy slug and taxonomy field, respectively.
1628           *
1629           * @since 2.3.0
1630           *
1631           * @param mixed $value   Value of the taxonomy field to edit.
1632           * @param int   $term_id Term ID.
1633           */
1634          $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );
1635  
1636          if ( 'description' === $field ) {
1637              $value = esc_html( $value ); // textarea_escaped
1638          } else {
1639              $value = esc_attr( $value );
1640          }
1641      } elseif ( 'db' === $context ) {
1642  
1643          /**
1644           * Filters a term field value before it is sanitized.
1645           *
1646           * The dynamic portion of the filter name, `$field`, refers to the term field.
1647           *
1648           * @since 2.3.0
1649           *
1650           * @param mixed  $value    Value of the term field.
1651           * @param string $taxonomy Taxonomy slug.
1652           */
1653          $value = apply_filters( "pre_term_{$field}", $value, $taxonomy );
1654  
1655          /**
1656           * Filters a taxonomy field before it is sanitized.
1657           *
1658           * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
1659           * to the taxonomy slug and field name, respectively.
1660           *
1661           * @since 2.3.0
1662           *
1663           * @param mixed $value Value of the taxonomy field.
1664           */
1665          $value = apply_filters( "pre_{$taxonomy}_{$field}", $value );
1666  
1667          // Back compat filters.
1668          if ( 'slug' === $field ) {
1669              /**
1670               * Filters the category nicename before it is sanitized.
1671               *
1672               * Use the {@see 'pre_$taxonomy_$field'} hook instead.
1673               *
1674               * @since 2.0.3
1675               *
1676               * @param string $value The category nicename.
1677               */
1678              $value = apply_filters( 'pre_category_nicename', $value );
1679          }
1680      } elseif ( 'rss' === $context ) {
1681  
1682          /**
1683           * Filters the term field for use in RSS.
1684           *
1685           * The dynamic portion of the filter name, `$field`, refers to the term field.
1686           *
1687           * @since 2.3.0
1688           *
1689           * @param mixed  $value    Value of the term field.
1690           * @param string $taxonomy Taxonomy slug.
1691           */
1692          $value = apply_filters( "term_{$field}_rss", $value, $taxonomy );
1693  
1694          /**
1695           * Filters the taxonomy field for use in RSS.
1696           *
1697           * The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer
1698           * to the taxonomy slug and field name, respectively.
1699           *
1700           * @since 2.3.0
1701           *
1702           * @param mixed $value Value of the taxonomy field.
1703           */
1704          $value = apply_filters( "{$taxonomy}_{$field}_rss", $value );
1705      } else {
1706          // Use display filters by default.
1707  
1708          /**
1709           * Filters the term field sanitized for display.
1710           *
1711           * The dynamic portion of the filter name, `$field`, refers to the term field name.
1712           *
1713           * @since 2.3.0
1714           *
1715           * @param mixed  $value    Value of the term field.
1716           * @param int    $term_id  Term ID.
1717           * @param string $taxonomy Taxonomy slug.
1718           * @param string $context  Context to retrieve the term field value.
1719           */
1720          $value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );
1721  
1722          /**
1723           * Filters the taxonomy field sanitized for display.
1724           *
1725           * The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer
1726           * to the taxonomy slug and taxonomy field, respectively.
1727           *
1728           * @since 2.3.0
1729           *
1730           * @param mixed  $value   Value of the taxonomy field.
1731           * @param int    $term_id Term ID.
1732           * @param string $context Context to retrieve the taxonomy field value.
1733           */
1734          $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
1735      }
1736  
1737      if ( 'attribute' === $context ) {
1738          $value = esc_attr( $value );
1739      } elseif ( 'js' === $context ) {
1740          $value = esc_js( $value );
1741      }
1742      return $value;
1743  }
1744  
1745  /**
1746   * Count how many terms are in Taxonomy.
1747   *
1748   * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
1749   *
1750   * @since 2.3.0
1751   * @since 5.6.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
1752   *
1753   * @internal The `$deprecated` parameter is parsed for backward compatibility only.
1754   *
1755   * @param array|string $args       Optional. Array of arguments that get passed to get_terms().
1756   *                                 Default empty array.
1757   * @param array|string $deprecated Optional. Argument array, when using the legacy function parameter format.
1758   *                                 If present, this parameter will be interpreted as `$args`, and the first
1759   *                                 function parameter will be parsed as a taxonomy or array of taxonomies.
1760   *                                 Default empty.
1761   * @return string|WP_Error Numeric string containing the number of terms in that
1762   *                         taxonomy or WP_Error if the taxonomy does not exist.
1763   */
1764  function wp_count_terms( $args = array(), $deprecated = '' ) {
1765      $use_legacy_args = false;
1766  
1767      // Check whether function is used with legacy signature: `$taxonomy` and `$args`.
1768      if ( $args
1769          && ( is_string( $args ) && taxonomy_exists( $args )
1770              || is_array( $args ) && wp_is_numeric_array( $args ) )
1771      ) {
1772          $use_legacy_args = true;
1773      }
1774  
1775      $defaults = array( 'hide_empty' => false );
1776  
1777      if ( $use_legacy_args ) {
1778          $defaults['taxonomy'] = $args;
1779          $args                 = $deprecated;
1780      }
1781  
1782      $args = wp_parse_args( $args, $defaults );
1783  
1784      // Backward compatibility.
1785      if ( isset( $args['ignore_empty'] ) ) {
1786          $args['hide_empty'] = $args['ignore_empty'];
1787          unset( $args['ignore_empty'] );
1788      }
1789  
1790      $args['fields'] = 'count';
1791  
1792      return get_terms( $args );
1793  }
1794  
1795  /**
1796   * Will unlink the object from the taxonomy or taxonomies.
1797   *
1798   * Will remove all relationships between the object and any terms in
1799   * a particular taxonomy or taxonomies. Does not remove the term or
1800   * taxonomy itself.
1801   *
1802   * @since 2.3.0
1803   *
1804   * @param int          $object_id  The term Object Id that refers to the term.
1805   * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
1806   */
1807  function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
1808      $object_id = (int) $object_id;
1809  
1810      if ( ! is_array( $taxonomies ) ) {
1811          $taxonomies = array( $taxonomies );
1812      }
1813  
1814      foreach ( (array) $taxonomies as $taxonomy ) {
1815          $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
1816          $term_ids = array_map( 'intval', $term_ids );
1817          wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
1818      }
1819  }
1820  
1821  /**
1822   * Removes a term from the database.
1823   *
1824   * If the term is a parent of other terms, then the children will be updated to
1825   * that term's parent.
1826   *
1827   * Metadata associated with the term will be deleted.
1828   *
1829   * @since 2.3.0
1830   *
1831   * @global wpdb $wpdb WordPress database abstraction object.
1832   *
1833   * @param int          $term     Term ID.
1834   * @param string       $taxonomy Taxonomy Name.
1835   * @param array|string $args {
1836   *     Optional. Array of arguments to override the default term ID. Default empty array.
1837   *
1838   *     @type int  $default       The term ID to make the default term. This will only override
1839   *                               the terms found if there is only one term found. Any other and
1840   *                               the found terms are used.
1841   *     @type bool $force_default Optional. Whether to force the supplied term as default to be
1842   *                               assigned even if the object was not going to be term-less.
1843   *                               Default false.
1844   * }
1845   * @return bool|int|WP_Error True on success, false if term does not exist. Zero on attempted
1846   *                           deletion of default Category. WP_Error if the taxonomy does not exist.
1847   */
1848  function wp_delete_term( $term, $taxonomy, $args = array() ) {
1849      global $wpdb;
1850  
1851      $term = (int) $term;
1852  
1853      $ids = term_exists( $term, $taxonomy );
1854      if ( ! $ids ) {
1855          return false;
1856      }
1857      if ( is_wp_error( $ids ) ) {
1858          return $ids;
1859      }
1860  
1861      $tt_id = $ids['term_taxonomy_id'];
1862  
1863      $defaults = array();
1864  
1865      if ( 'category' === $taxonomy ) {
1866          $defaults['default'] = (int) get_option( 'default_category' );
1867          if ( $defaults['default'] === $term ) {
1868              return 0; // Don't delete the default category.
1869          }
1870      }
1871  
1872      // Don't delete the default custom taxonomy term.
1873      $taxonomy_object = get_taxonomy( $taxonomy );
1874      if ( ! empty( $taxonomy_object->default_term ) ) {
1875          $defaults['default'] = (int) get_option( 'default_term_' . $taxonomy );
1876          if ( $defaults['default'] === $term ) {
1877              return 0;
1878          }
1879      }
1880  
1881      $args = wp_parse_args( $args, $defaults );
1882  
1883      if ( isset( $args['default'] ) ) {
1884          $default = (int) $args['default'];
1885          if ( ! term_exists( $default, $taxonomy ) ) {
1886              unset( $default );
1887          }
1888      }
1889  
1890      if ( isset( $args['force_default'] ) ) {
1891          $force_default = $args['force_default'];
1892      }
1893  
1894      /**
1895       * Fires when deleting a term, before any modifications are made to posts or terms.
1896       *
1897       * @since 4.1.0
1898       *
1899       * @param int    $term     Term ID.
1900       * @param string $taxonomy Taxonomy Name.
1901       */
1902      do_action( 'pre_delete_term', $term, $taxonomy );
1903  
1904      // Update children to point to new parent.
1905      if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1906          $term_obj = get_term( $term, $taxonomy );
1907          if ( is_wp_error( $term_obj ) ) {
1908              return $term_obj;
1909          }
1910          $parent = $term_obj->parent;
1911  
1912          $edit_ids    = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int) $term_obj->term_id );
1913          $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
1914  
1915          /**
1916           * Fires immediately before a term to delete's children are reassigned a parent.
1917           *
1918           * @since 2.9.0
1919           *
1920           * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
1921           */
1922          do_action( 'edit_term_taxonomies', $edit_tt_ids );
1923  
1924          $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
1925  
1926          // Clean the cache for all child terms.
1927          $edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' );
1928          clean_term_cache( $edit_term_ids, $taxonomy );
1929  
1930          /**
1931           * Fires immediately after a term to delete's children are reassigned a parent.
1932           *
1933           * @since 2.9.0
1934           *
1935           * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
1936           */
1937          do_action( 'edited_term_taxonomies', $edit_tt_ids );
1938      }
1939  
1940      // Get the term before deleting it or its term relationships so we can pass to actions below.
1941      $deleted_term = get_term( $term, $taxonomy );
1942  
1943      $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
1944  
1945      foreach ( $object_ids as $object_id ) {
1946          if ( ! isset( $default ) ) {
1947              wp_remove_object_terms( $object_id, $term, $taxonomy );
1948              continue;
1949          }
1950  
1951          $terms = wp_get_object_terms(
1952              $object_id,
1953              $taxonomy,
1954              array(
1955                  'fields'  => 'ids',
1956                  'orderby' => 'none',
1957              )
1958          );
1959  
1960          if ( 1 === count( $terms ) && isset( $default ) ) {
1961              $terms = array( $default );
1962          } else {
1963              $terms = array_diff( $terms, array( $term ) );
1964              if ( isset( $default ) && isset( $force_default ) && $force_default ) {
1965                  $terms = array_merge( $terms, array( $default ) );
1966              }
1967          }
1968  
1969          $terms = array_map( 'intval', $terms );
1970          wp_set_object_terms( $object_id, $terms, $taxonomy );
1971      }
1972  
1973      // Clean the relationship caches for all object types using this term.
1974      $tax_object = get_taxonomy( $taxonomy );
1975      foreach ( $tax_object->object_type as $object_type ) {
1976          clean_object_term_cache( $object_ids, $object_type );
1977      }
1978  
1979      $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
1980      foreach ( $term_meta_ids as $mid ) {
1981          delete_metadata_by_mid( 'term', $mid );
1982      }
1983  
1984      /**
1985       * Fires immediately before a term taxonomy ID is deleted.
1986       *
1987       * @since 2.9.0
1988       *
1989       * @param int $tt_id Term taxonomy ID.
1990       */
1991      do_action( 'delete_term_taxonomy', $tt_id );
1992  
1993      $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
1994  
1995      /**
1996       * Fires immediately after a term taxonomy ID is deleted.
1997       *
1998       * @since 2.9.0
1999       *
2000       * @param int $tt_id Term taxonomy ID.
2001       */
2002      do_action( 'deleted_term_taxonomy', $tt_id );
2003  
2004      // Delete the term if no taxonomies use it.
2005      if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term ) ) ) {
2006          $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
2007      }
2008  
2009      clean_term_cache( $term, $taxonomy );
2010  
2011      /**
2012       * Fires after a term is deleted from the database and the cache is cleaned.
2013       *
2014       * The {@see 'delete_$taxonomy'} hook is also available for targeting a specific
2015       * taxonomy.
2016       *
2017       * @since 2.5.0
2018       * @since 4.5.0 Introduced the `$object_ids` argument.
2019       *
2020       * @param int     $term         Term ID.
2021       * @param int     $tt_id        Term taxonomy ID.
2022       * @param string  $taxonomy     Taxonomy slug.
2023       * @param WP_Term $deleted_term Copy of the already-deleted term.
2024       * @param array   $object_ids   List of term object IDs.
2025       */
2026      do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term, $object_ids );
2027  
2028      /**
2029       * Fires after a term in a specific taxonomy is deleted.
2030       *
2031       * The dynamic portion of the hook name, `$taxonomy`, refers to the specific
2032       * taxonomy the term belonged to.
2033       *
2034       * @since 2.3.0
2035       * @since 4.5.0 Introduced the `$object_ids` argument.
2036       *
2037       * @param int     $term         Term ID.
2038       * @param int     $tt_id        Term taxonomy ID.
2039       * @param WP_Term $deleted_term Copy of the already-deleted term.
2040       * @param array   $object_ids   List of term object IDs.
2041       */
2042      do_action( "delete_{$taxonomy}", $term, $tt_id, $deleted_term, $object_ids );
2043  
2044      return true;
2045  }
2046  
2047  /**
2048   * Deletes one existing category.
2049   *
2050   * @since 2.0.0
2051   *
2052   * @param int $cat_ID Category term ID.
2053   * @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist;
2054   *  Zero on attempted deletion of default Category; WP_Error object is also a possibility.
2055   */
2056  function wp_delete_category( $cat_ID ) {
2057      return wp_delete_term( $cat_ID, 'category' );
2058  }
2059  
2060  /**
2061   * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
2062   *
2063   * @since 2.3.0
2064   * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
2065   *              Introduced `$parent` argument.
2066   * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or
2067   *              'all_with_object_id', an array of `WP_Term` objects will be returned.
2068   * @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments.
2069   *
2070   * @param int|int[]       $object_ids The ID(s) of the object(s) to retrieve.
2071   * @param string|string[] $taxonomies The taxonomy names to retrieve terms from.
2072   * @param array|string    $args       See WP_Term_Query::__construct() for supported arguments.
2073   * @return WP_Term[]|WP_Error Array of terms or empty array if no terms found.
2074   *                            WP_Error if any of the taxonomies don't exist.
2075   */
2076  function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
2077      if ( empty( $object_ids ) || empty( $taxonomies ) ) {
2078          return array();
2079      }
2080  
2081      if ( ! is_array( $taxonomies ) ) {
2082          $taxonomies = array( $taxonomies );
2083      }
2084  
2085      foreach ( $taxonomies as $taxonomy ) {
2086          if ( ! taxonomy_exists( $taxonomy ) ) {
2087              return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
2088          }
2089      }
2090  
2091      if ( ! is_array( $object_ids ) ) {
2092          $object_ids = array( $object_ids );
2093      }
2094      $object_ids = array_map( 'intval', $object_ids );
2095  
2096      $args = wp_parse_args( $args );
2097  
2098      /**
2099       * Filters arguments for retrieving object terms.
2100       *
2101       * @since 4.9.0
2102       *
2103       * @param array    $args       An array of arguments for retrieving terms for the given object(s).
2104       *                             See {@see wp_get_object_terms()} for details.
2105       * @param int[]    $object_ids Array of object IDs.
2106       * @param string[] $taxonomies Array of taxonomy names to retrieve terms from.
2107       */
2108      $args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies );
2109  
2110      /*
2111       * When one or more queried taxonomies is registered with an 'args' array,
2112       * those params override the `$args` passed to this function.
2113       */
2114      $terms = array();
2115      if ( count( $taxonomies ) > 1 ) {
2116          foreach ( $taxonomies as $index => $taxonomy ) {
2117              $t = get_taxonomy( $taxonomy );
2118              if ( isset( $t->args ) && is_array( $t->args ) && array_merge( $args, $t->args ) != $args ) {
2119                  unset( $taxonomies[ $index ] );
2120                  $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
2121              }
2122          }
2123      } else {
2124          $t = get_taxonomy( $taxonomies[0] );
2125          if ( isset( $t->args ) && is_array( $t->args ) ) {
2126              $args = array_merge( $args, $t->args );
2127          }
2128      }
2129  
2130      $args['taxonomy']   = $taxonomies;
2131      $args['object_ids'] = $object_ids;
2132  
2133      // Taxonomies registered without an 'args' param are handled here.
2134      if ( ! empty( $taxonomies ) ) {
2135          $terms_from_remaining_taxonomies = get_terms( $args );
2136  
2137          // Array keys should be preserved for values of $fields that use term_id for keys.
2138          if ( ! empty( $args['fields'] ) && 0 === strpos( $args['fields'], 'id=>' ) ) {
2139              $terms = $terms + $terms_from_remaining_taxonomies;
2140          } else {
2141              $terms = array_merge( $terms, $terms_from_remaining_taxonomies );
2142          }
2143      }
2144  
2145      /**
2146       * Filters the terms for a given object or objects.
2147       *
2148       * @since 4.2.0
2149       *
2150       * @param WP_Term[] $terms      Array of terms for the given object or objects.
2151       * @param int[]     $object_ids Array of object IDs for which terms were retrieved.
2152       * @param string[]  $taxonomies Array of taxonomy names from which terms were retrieved.
2153       * @param array     $args       Array of arguments for retrieving terms for the given
2154       *                              object(s). See wp_get_object_terms() for details.
2155       */
2156      $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args );
2157  
2158      $object_ids = implode( ',', $object_ids );
2159      $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
2160  
2161      /**
2162       * Filters the terms for a given object or objects.
2163       *
2164       * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
2165       * {@see 'get_object_terms'} filter is recommended as an alternative.
2166       *
2167       * @since 2.8.0
2168       *
2169       * @param WP_Term[] $terms      Array of terms for the given object or objects.
2170       * @param string    $object_ids Comma separated list of object IDs for which terms were retrieved.
2171       * @param string    $taxonomies SQL fragment of taxonomy names from which terms were retrieved.
2172       * @param array     $args       Array of arguments for retrieving terms for the given
2173       *                              object(s). See wp_get_object_terms() for details.
2174       */
2175      return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
2176  }
2177  
2178  /**
2179   * Add a new term to the database.
2180   *
2181   * A non-existent term is inserted in the following sequence:
2182   * 1. The term is added to the term table, then related to the taxonomy.
2183   * 2. If everything is correct, several actions are fired.
2184   * 3. The 'term_id_filter' is evaluated.
2185   * 4. The term cache is cleaned.
2186   * 5. Several more actions are fired.
2187   * 6. An array is returned containing the `term_id` and `term_taxonomy_id`.
2188   *
2189   * If the 'slug' argument is not empty, then it is checked to see if the term
2190   * is invalid. If it is not a valid, existing term, it is added and the term_id
2191   * is given.
2192   *
2193   * If the taxonomy is hierarchical, and the 'parent' argument is not empty,
2194   * the term is inserted and the term_id will be given.
2195   *
2196   * Error handling:
2197   * If `$taxonomy` does not exist or `$term` is empty,
2198   * a WP_Error object will be returned.
2199   *
2200   * If the term already exists on the same hierarchical level,
2201   * or the term slug and name are not unique, a WP_Error object will be returned.
2202   *
2203   * @global wpdb $wpdb WordPress database abstraction object.
2204   *
2205   * @since 2.3.0
2206   *
2207   * @param string       $term     The term name to add.
2208   * @param string       $taxonomy The taxonomy to which to add the term.
2209   * @param array|string $args {
2210   *     Optional. Array or query string of arguments for inserting a term.
2211   *
2212   *     @type string $alias_of    Slug of the term to make this term an alias of.
2213   *                               Default empty string. Accepts a term slug.
2214   *     @type string $description The term description. Default empty string.
2215   *     @type int    $parent      The id of the parent term. Default 0.
2216   *     @type string $slug        The term slug to use. Default empty string.
2217   * }
2218   * @return array|WP_Error {
2219   *     An array of the new term data, WP_Error otherwise.
2220   *
2221   *     @type int        $term_id          The new term ID.
2222   *     @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
2223   * }
2224   */
2225  function wp_insert_term( $term, $taxonomy, $args = array() ) {
2226      global $wpdb;
2227  
2228      if ( ! taxonomy_exists( $taxonomy ) ) {
2229          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
2230      }
2231  
2232      /**
2233       * Filters a term before it is sanitized and inserted into the database.
2234       *
2235       * @since 3.0.0
2236       *
2237       * @param string|WP_Error $term     The term name to add, or a WP_Error object if there's an error.
2238       * @param string          $taxonomy Taxonomy slug.
2239       */
2240      $term = apply_filters( 'pre_insert_term', $term, $taxonomy );
2241  
2242      if ( is_wp_error( $term ) ) {
2243          return $term;
2244      }
2245  
2246      if ( is_int( $term ) && 0 === $term ) {
2247          return new WP_Error( 'invalid_term_id', __( 'Invalid term ID.' ) );
2248      }
2249  
2250      if ( '' === trim( $term ) ) {
2251          return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
2252      }
2253  
2254      $defaults = array(
2255          'alias_of'    => '',
2256          'description' => '',
2257          'parent'      => 0,
2258          'slug'        => '',
2259      );
2260      $args     = wp_parse_args( $args, $defaults );
2261  
2262      if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
2263          return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
2264      }
2265  
2266      $args['name']     = $term;
2267      $args['taxonomy'] = $taxonomy;
2268  
2269      // Coerce null description to strings, to avoid database errors.
2270      $args['description'] = (string) $args['description'];
2271  
2272      $args = sanitize_term( $args, $taxonomy, 'db' );
2273  
2274      // expected_slashed ($name)
2275      $name        = wp_unslash( $args['name'] );
2276      $description = wp_unslash( $args['description'] );
2277      $parent      = (int) $args['parent'];
2278  
2279      $slug_provided = ! empty( $args['slug'] );
2280      if ( ! $slug_provided ) {
2281          $slug = sanitize_title( $name );
2282      } else {
2283          $slug = $args['slug'];
2284      }
2285  
2286      $term_group = 0;
2287      if ( $args['alias_of'] ) {
2288          $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
2289          if ( ! empty( $alias->term_group ) ) {
2290              // The alias we want is already in a group, so let's use that one.
2291              $term_group = $alias->term_group;
2292          } elseif ( ! empty( $alias->term_id ) ) {
2293              /*
2294               * The alias is not in a group, so we create a new one
2295               * and add the alias to it.
2296               */
2297              $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
2298  
2299              wp_update_term(
2300                  $alias->term_id,
2301                  $taxonomy,
2302                  array(
2303                      'term_group' => $term_group,
2304                  )
2305              );
2306          }
2307      }
2308  
2309      /*
2310       * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
2311       * unless a unique slug has been explicitly provided.
2312       */
2313      $name_matches = get_terms(
2314          array(
2315              'taxonomy'               => $taxonomy,
2316              'name'                   => $name,
2317              'hide_empty'             => false,
2318              'parent'                 => $args['parent'],
2319              'update_term_meta_cache' => false,
2320          )
2321      );
2322  
2323      /*
2324       * The `name` match in `get_terms()` doesn't differentiate accented characters,
2325       * so we do a stricter comparison here.
2326       */
2327      $name_match = null;
2328      if ( $name_matches ) {
2329          foreach ( $name_matches as $_match ) {
2330              if ( strtolower( $name ) === strtolower( $_match->name ) ) {
2331                  $name_match = $_match;
2332                  break;
2333              }
2334          }
2335      }
2336  
2337      if ( $name_match ) {
2338          $slug_match = get_term_by( 'slug', $slug, $taxonomy );
2339          if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
2340              if ( is_taxonomy_hierarchical( $taxonomy ) ) {
2341                  $siblings = get_terms(
2342                      array(
2343                          'taxonomy'               => $taxonomy,
2344                          'get'                    => 'all',
2345                          'parent'                 => $parent,
2346                          'update_term_meta_cache' => false,
2347                      )
2348                  );
2349  
2350                  $existing_term = null;
2351                  $sibling_names = wp_list_pluck( $siblings, 'name' );
2352                  $sibling_slugs = wp_list_pluck( $siblings, 'slug' );
2353  
2354                  if ( ( ! $slug_provided || $name_match->slug === $slug ) && in_array( $name, $sibling_names, true ) ) {
2355                      $existing_term = $name_match;
2356                  } elseif ( $slug_match && in_array( $slug, $sibling_slugs, true ) ) {
2357                      $existing_term = $slug_match;
2358                  }
2359  
2360                  if ( $existing_term ) {
2361                      return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $existing_term->term_id );
2362                  }
2363              } else {
2364                  return new WP_Error( 'term_exists', __( 'A term with the name provided already exists in this taxonomy.' ), $name_match->term_id );
2365              }
2366          }
2367      }
2368  
2369      $slug = wp_unique_term_slug( $slug, (object) $args );
2370  
2371      $data = compact( 'name', 'slug', 'term_group' );
2372  
2373      /**
2374       * Filters term data before it is inserted into the database.
2375       *
2376       * @since 4.7.0
2377       *
2378       * @param array  $data     Term data to be inserted.
2379       * @param string $taxonomy Taxonomy slug.
2380       * @param array  $args     Arguments passed to wp_insert_term().
2381       */
2382      $data = apply_filters( 'wp_insert_term_data', $data, $taxonomy, $args );
2383  
2384      if ( false === $wpdb->insert( $wpdb->terms, $data ) ) {
2385          return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database.' ), $wpdb->last_error );
2386      }
2387  
2388      $term_id = (int) $wpdb->insert_id;
2389  
2390      // Seems unreachable. However, is used in the case that a term name is provided, which sanitizes to an empty string.
2391      if ( empty( $slug ) ) {
2392          $slug = sanitize_title( $slug, $term_id );
2393  
2394          /** This action is documented in wp-includes/taxonomy.php */
2395          do_action( 'edit_terms', $term_id, $taxonomy );
2396          $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
2397  
2398          /** This action is documented in wp-includes/taxonomy.php */
2399          do_action( 'edited_terms', $term_id, $taxonomy );
2400      }
2401  
2402      $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 ) );
2403  
2404      if ( ! empty( $tt_id ) ) {
2405          return array(
2406              'term_id'          => $term_id,
2407              'term_taxonomy_id' => $tt_id,
2408          );
2409      }
2410  
2411      if ( false === $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ) + array( 'count' => 0 ) ) ) {
2412          return new WP_Error( 'db_insert_error', __( 'Could not insert term taxonomy into the database.' ), $wpdb->last_error );
2413      }
2414  
2415      $tt_id = (int) $wpdb->insert_id;
2416  
2417      /*
2418       * Sanity check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than
2419       * an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id
2420       * and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
2421       * are not fired.
2422       */
2423      $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 ) );
2424  
2425      /**
2426       * Filters the duplicate term check that takes place during term creation.
2427       *
2428       * Term parent+taxonomy+slug combinations are meant to be unique, and wp_insert_term()
2429       * performs a last-minute confirmation of this uniqueness before allowing a new term
2430       * to be created. Plugins with different uniqueness requirements may use this filter
2431       * to bypass or modify the duplicate-term check.
2432       *
2433       * @since 5.1.0
2434       *
2435       * @param object $duplicate_term Duplicate term row from terms table, if found.
2436       * @param string $term           Term being inserted.
2437       * @param string $taxonomy       Taxonomy name.
2438       * @param array  $args           Term arguments passed to the function.
2439       * @param int    $tt_id          term_taxonomy_id for the newly created term.
2440       */
2441      $duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id );
2442  
2443      if ( $duplicate_term ) {
2444          $wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
2445          $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
2446  
2447          $term_id = (int) $duplicate_term->term_id;
2448          $tt_id   = (int) $duplicate_term->term_taxonomy_id;
2449  
2450          clean_term_cache( $term_id, $taxonomy );
2451          return array(
2452              'term_id'          => $term_id,
2453              'term_taxonomy_id' => $tt_id,
2454          );
2455      }
2456  
2457      /**
2458       * Fires immediately after a new term is created, before the term cache is cleaned.
2459       *
2460       * The {@see 'create_$taxonomy'} hook is also available for targeting a specific
2461       * taxonomy.
2462       *
2463       * @since 2.3.0
2464       *
2465       * @param int    $term_id  Term ID.
2466       * @param int    $tt_id    Term taxonomy ID.
2467       * @param string $taxonomy Taxonomy slug.
2468       */
2469      do_action( 'create_term', $term_id, $tt_id, $taxonomy );
2470  
2471      /**
2472       * Fires after a new term is created for a specific taxonomy.
2473       *
2474       * The dynamic portion of the hook name, `$taxonomy`, refers
2475       * to the slug of the taxonomy the term was created for.
2476       *
2477       * @since 2.3.0
2478       *
2479       * @param int $term_id Term ID.
2480       * @param int $tt_id   Term taxonomy ID.
2481       */
2482      do_action( "create_{$taxonomy}", $term_id, $tt_id );
2483  
2484      /**
2485       * Filters the term ID after a new term is created.
2486       *
2487       * @since 2.3.0
2488       *
2489       * @param int $term_id Term ID.
2490       * @param int $tt_id   Term taxonomy ID.
2491       */
2492      $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
2493  
2494      clean_term_cache( $term_id, $taxonomy );
2495  
2496      /**
2497       * Fires after a new term is created, and after the term cache has been cleaned.
2498       *
2499       * The {@see 'created_$taxonomy'} hook is also available for targeting a specific
2500       * taxonomy.
2501       *
2502       * @since 2.3.0
2503       *
2504       * @param int    $term_id  Term ID.
2505       * @param int    $tt_id    Term taxonomy ID.
2506       * @param string $taxonomy Taxonomy slug.
2507       */
2508      do_action( 'created_term', $term_id, $tt_id, $taxonomy );
2509  
2510      /**
2511       * Fires after a new term in a specific taxonomy is created, and after the term
2512       * cache has been cleaned.
2513       *
2514       * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
2515       *
2516       * @since 2.3.0
2517       *
2518       * @param int $term_id Term ID.
2519       * @param int $tt_id   Term taxonomy ID.
2520       */
2521      do_action( "created_{$taxonomy}", $term_id, $tt_id );
2522  
2523      /**
2524       * Fires after a term has been saved, and the term cache has been cleared.
2525       *
2526       * The {@see 'saved_$taxonomy'} hook is also available for targeting a specific
2527       * taxonomy.
2528       *
2529       * @since 5.5.0
2530       *
2531       * @param int    $term_id  Term ID.
2532       * @param int    $tt_id    Term taxonomy ID.
2533       * @param string $taxonomy Taxonomy slug.
2534       * @param bool   $update   Whether this is an existing term being updated.
2535       */
2536      do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false );
2537  
2538      /**
2539       * Fires after a term in a specific taxonomy has been saved, and the term
2540       * cache has been cleared.
2541       *
2542       * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
2543       *
2544       * @since 5.5.0
2545       *
2546       * @param int  $term_id Term ID.
2547       * @param int  $tt_id   Term taxonomy ID.
2548       * @param bool $update  Whether this is an existing term being updated.
2549       */
2550      do_action( "saved_{$taxonomy}", $term_id, $tt_id, false );
2551  
2552      return array(
2553          'term_id'          => $term_id,
2554          'term_taxonomy_id' => $tt_id,
2555      );
2556  }
2557  
2558  /**
2559   * Create Term and Taxonomy Relationships.
2560   *
2561   * Relates an object (post, link etc) to a term and taxonomy type. Creates the
2562   * term and taxonomy relationship if it doesn't already exist. Creates a term if
2563   * it doesn't exist (using the slug).
2564   *
2565   * A relationship means that the term is grouped in or belongs to the taxonomy.
2566   * A term has no meaning until it is given context by defining which taxonomy it
2567   * exists under.
2568   *
2569   * @since 2.3.0
2570   *
2571   * @global wpdb $wpdb WordPress database abstraction object.
2572   *
2573   * @param int              $object_id The object to relate to.
2574   * @param string|int|array $terms     A single term slug, single term ID, or array of either term slugs or IDs.
2575   *                                    Will replace all existing related terms in this taxonomy. Passing an
2576   *                                    empty value will remove all related terms.
2577   * @param string           $taxonomy  The context in which to relate the term to the object.
2578   * @param bool             $append    Optional. If false will delete difference of terms. Default false.
2579   * @return array|WP_Error Term taxonomy IDs of the affected terms or WP_Error on failure.
2580   */
2581  function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
2582      global $wpdb;
2583  
2584      $object_id = (int) $object_id;
2585  
2586      if ( ! taxonomy_exists( $taxonomy ) ) {
2587          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
2588      }
2589  
2590      if ( ! is_array( $terms ) ) {
2591          $terms = array( $terms );
2592      }
2593  
2594      if ( ! $append ) {
2595          $old_tt_ids = wp_get_object_terms(
2596              $object_id,
2597              $taxonomy,
2598              array(
2599                  'fields'                 => 'tt_ids',
2600                  'orderby'                => 'none',
2601                  'update_term_meta_cache' => false,
2602              )
2603          );
2604      } else {
2605          $old_tt_ids = array();
2606      }
2607  
2608      $tt_ids     = array();
2609      $term_ids   = array();
2610      $new_tt_ids = array();
2611  
2612      foreach ( (array) $terms as $term ) {
2613          if ( '' === trim( $term ) ) {
2614              continue;
2615          }
2616  
2617          $term_info = term_exists( $term, $taxonomy );
2618  
2619          if ( ! $term_info ) {
2620              // Skip if a non-existent term ID is passed.
2621              if ( is_int( $term ) ) {
2622                  continue;
2623              }
2624  
2625              $term_info = wp_insert_term( $term, $taxonomy );
2626          }
2627  
2628          if ( is_wp_error( $term_info ) ) {
2629              return $term_info;
2630          }
2631  
2632          $term_ids[] = $term_info['term_id'];
2633          $tt_id      = $term_info['term_taxonomy_id'];
2634          $tt_ids[]   = $tt_id;
2635  
2636          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 ) ) ) {
2637              continue;
2638          }
2639  
2640          /**
2641           * Fires immediately before an object-term relationship is added.
2642           *
2643           * @since 2.9.0
2644           * @since 4.7.0 Added the `$taxonomy` parameter.
2645           *
2646           * @param int    $object_id Object ID.
2647           * @param int    $tt_id     Term taxonomy ID.
2648           * @param string $taxonomy  Taxonomy slug.
2649           */
2650          do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy );
2651  
2652          $wpdb->insert(
2653              $wpdb->term_relationships,
2654              array(
2655                  'object_id'        => $object_id,
2656                  'term_taxonomy_id' => $tt_id,
2657              )
2658          );
2659  
2660          /**
2661           * Fires immediately after an object-term relationship is added.
2662           *
2663           * @since 2.9.0
2664           * @since 4.7.0 Added the `$taxonomy` parameter.
2665           *
2666           * @param int    $object_id Object ID.
2667           * @param int    $tt_id     Term taxonomy ID.
2668           * @param string $taxonomy  Taxonomy slug.
2669           */
2670          do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy );
2671  
2672          $new_tt_ids[] = $tt_id;
2673      }
2674  
2675      if ( $new_tt_ids ) {
2676          wp_update_term_count( $new_tt_ids, $taxonomy );
2677      }
2678  
2679      if ( ! $append ) {
2680          $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
2681  
2682          if ( $delete_tt_ids ) {
2683              $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
2684              $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 ) );
2685              $delete_term_ids  = array_map( 'intval', $delete_term_ids );
2686  
2687              $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
2688              if ( is_wp_error( $remove ) ) {
2689                  return $remove;
2690              }
2691          }
2692      }
2693  
2694      $t = get_taxonomy( $taxonomy );
2695  
2696      if ( ! $append && isset( $t->sort ) && $t->sort ) {
2697          $values     = array();
2698          $term_order = 0;
2699  
2700          $final_tt_ids = wp_get_object_terms(
2701              $object_id,
2702              $taxonomy,
2703              array(
2704                  'fields'                 => 'tt_ids',
2705                  'update_term_meta_cache' => false,
2706              )
2707          );
2708  
2709          foreach ( $tt_ids as $tt_id ) {
2710              if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) {
2711                  $values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order );
2712              }
2713          }
2714  
2715          if ( $values ) {
2716              if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . implode( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) {
2717                  return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error );
2718              }
2719          }
2720      }
2721  
2722      wp_cache_delete( $object_id, $taxonomy . '_relationships' );
2723      wp_cache_delete( 'last_changed', 'terms' );
2724  
2725      /**
2726       * Fires after an object's terms have been set.
2727       *
2728       * @since 2.8.0
2729       *
2730       * @param int    $object_id  Object ID.
2731       * @param array  $terms      An array of object terms.
2732       * @param array  $tt_ids     An array of term taxonomy IDs.
2733       * @param string $taxonomy   Taxonomy slug.
2734       * @param bool   $append     Whether to append new terms to the old terms.
2735       * @param array  $old_tt_ids Old array of term taxonomy IDs.
2736       */
2737      do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids );
2738  
2739      return $tt_ids;
2740  }
2741  
2742  /**
2743   * Add term(s) associated with a given object.
2744   *
2745   * @since 3.6.0
2746   *
2747   * @param int              $object_id The ID of the object to which the terms will be added.
2748   * @param string|int|array $terms     The slug(s) or ID(s) of the term(s) to add.
2749   * @param array|string     $taxonomy  Taxonomy name.
2750   * @return array|WP_Error Term taxonomy IDs of the affected terms.
2751   */
2752  function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
2753      return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
2754  }
2755  
2756  /**
2757   * Remove term(s) associated with a given object.
2758   *
2759   * @since 3.6.0
2760   *
2761   * @global wpdb $wpdb WordPress database abstraction object.
2762   *
2763   * @param int              $object_id The ID of the object from which the terms will be removed.
2764   * @param string|int|array $terms     The slug(s) or ID(s) of the term(s) to remove.
2765   * @param array|string     $taxonomy  Taxonomy name.
2766   * @return bool|WP_Error True on success, false or WP_Error on failure.
2767   */
2768  function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
2769      global $wpdb;
2770  
2771      $object_id = (int) $object_id;
2772  
2773      if ( ! taxonomy_exists( $taxonomy ) ) {
2774          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
2775      }
2776  
2777      if ( ! is_array( $terms ) ) {
2778          $terms = array( $terms );
2779      }
2780  
2781      $tt_ids = array();
2782  
2783      foreach ( (array) $terms as $term ) {
2784          if ( '' === trim( $term ) ) {
2785              continue;
2786          }
2787  
2788          $term_info = term_exists( $term, $taxonomy );
2789          if ( ! $term_info ) {
2790              // Skip if a non-existent term ID is passed.
2791              if ( is_int( $term ) ) {
2792                  continue;
2793              }
2794          }
2795  
2796          if ( is_wp_error( $term_info ) ) {
2797              return $term_info;
2798          }
2799  
2800          $tt_ids[] = $term_info['term_taxonomy_id'];
2801      }
2802  
2803      if ( $tt_ids ) {
2804          $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
2805  
2806          /**
2807           * Fires immediately before an object-term relationship is deleted.
2808           *
2809           * @since 2.9.0
2810           * @since 4.7.0 Added the `$taxonomy` parameter.
2811           *
2812           * @param int   $object_id Object ID.
2813           * @param array $tt_ids    An array of term taxonomy IDs.
2814           * @param string $taxonomy  Taxonomy slug.
2815           */
2816          do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy );
2817  
2818          $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
2819  
2820          wp_cache_delete( $object_id, $taxonomy . '_relationships' );
2821          wp_cache_delete( 'last_changed', 'terms' );
2822  
2823          /**
2824           * Fires immediately after an object-term relationship is deleted.
2825           *
2826           * @since 2.9.0
2827           * @since 4.7.0 Added the `$taxonomy` parameter.
2828           *
2829           * @param int    $object_id Object ID.
2830           * @param array  $tt_ids    An array of term taxonomy IDs.
2831           * @param string $taxonomy  Taxonomy slug.
2832           */
2833          do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy );
2834  
2835          wp_update_term_count( $tt_ids, $taxonomy );
2836  
2837          return (bool) $deleted;
2838      }
2839  
2840      return false;
2841  }
2842  
2843  /**
2844   * Will make slug unique, if it isn't already.
2845   *
2846   * The `$slug` has to be unique global to every taxonomy, meaning that one
2847   * taxonomy term can't have a matching slug with another taxonomy term. Each
2848   * slug has to be globally unique for every taxonomy.
2849   *
2850   * The way this works is that if the taxonomy that the term belongs to is
2851   * hierarchical and has a parent, it will append that parent to the $slug.
2852   *
2853   * If that still doesn't return a unique slug, then it tries to append a number
2854   * until it finds a number that is truly unique.
2855   *
2856   * The only purpose for `$term` is for appending a parent, if one exists.
2857   *
2858   * @since 2.3.0
2859   *
2860   * @global wpdb $wpdb WordPress database abstraction object.
2861   *
2862   * @param string $slug The string that will be tried for a unique slug.
2863   * @param object $term The term object that the `$slug` will belong to.
2864   * @return string Will return a true unique slug.
2865   */
2866  function wp_unique_term_slug( $slug, $term ) {
2867      global $wpdb;
2868  
2869      $needs_suffix  = true;
2870      $original_slug = $slug;
2871  
2872      // As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
2873      if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
2874          $needs_suffix = false;
2875      }
2876  
2877      /*
2878       * If the taxonomy supports hierarchy and the term has a parent, make the slug unique
2879       * by incorporating parent slugs.
2880       */
2881      $parent_suffix = '';
2882      if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
2883          $the_parent = $term->parent;
2884          while ( ! empty( $the_parent ) ) {
2885              $parent_term = get_term( $the_parent, $term->taxonomy );
2886              if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
2887                  break;
2888              }
2889              $parent_suffix .= '-' . $parent_term->slug;
2890              if ( ! term_exists( $slug . $parent_suffix ) ) {
2891                  break;
2892              }
2893  
2894              if ( empty( $parent_term->parent ) ) {
2895                  break;
2896              }
2897              $the_parent = $parent_term->parent;
2898          }
2899      }
2900  
2901      // If we didn't get a unique slug, try appending a number to make it unique.
2902  
2903      /**
2904       * Filters whether the proposed unique term slug is bad.
2905       *
2906       * @since 4.3.0
2907       *
2908       * @param bool   $needs_suffix Whether the slug needs to be made unique with a suffix.
2909       * @param string $slug         The slug.
2910       * @param object $term         Term object.
2911       */
2912      if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
2913          if ( $parent_suffix ) {
2914              $slug .= $parent_suffix;
2915          }
2916  
2917          if ( ! empty( $term->term_id ) ) {
2918              $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
2919          } else {
2920              $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
2921          }
2922  
2923          if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
2924              $num = 2;
2925              do {
2926                  $alt_slug = $slug . "-$num";
2927                  $num++;
2928                  $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
2929              } while ( $slug_check );
2930              $slug = $alt_slug;
2931          }
2932      }
2933  
2934      /**
2935       * Filters the unique term slug.
2936       *
2937       * @since 4.3.0
2938       *
2939       * @param string $slug          Unique term slug.
2940       * @param object $term          Term object.
2941       * @param string $original_slug Slug originally passed to the function for testing.
2942       */
2943      return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
2944  }
2945  
2946  /**
2947   * Update term based on arguments provided.
2948   *
2949   * The `$args` will indiscriminately override all values with the same field name.
2950   * Care must be taken to not override important information need to update or
2951   * update will fail (or perhaps create a new term, neither would be acceptable).
2952   *
2953   * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
2954   * defined in `$args` already.
2955   *
2956   * 'alias_of' will create a term group, if it doesn't already exist, and
2957   * update it for the `$term`.
2958   *
2959   * If the 'slug' argument in `$args` is missing, then the 'name' will be used.
2960   * If you set 'slug' and it isn't unique, then a WP_Error is returned.
2961   * If you don't pass any slug, then a unique one will be created.
2962   *
2963   * @since 2.3.0
2964   *
2965   * @global wpdb $wpdb WordPress database abstraction object.
2966   *
2967   * @param int          $term_id  The ID of the term.
2968   * @param string       $taxonomy The taxonomy of the term.
2969   * @param array|string $args {
2970   *     Optional. Array or string of arguments for updating a term.
2971   *
2972   *     @type string $alias_of    Slug of the term to make this term an alias of.
2973   *                               Default empty string. Accepts a term slug.
2974   *     @type string $description The term description. Default empty string.
2975   *     @type int    $parent      The id of the parent term. Default 0.
2976   *     @type string $slug        The term slug to use. Default empty string.
2977   * }
2978   * @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
2979   *                        WP_Error otherwise.
2980   */
2981  function wp_update_term( $term_id, $taxonomy, $args = array() ) {
2982      global $wpdb;
2983  
2984      if ( ! taxonomy_exists( $taxonomy ) ) {
2985          return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
2986      }
2987  
2988      $term_id = (int) $term_id;
2989  
2990      // First, get all of the original args.
2991      $term = get_term( $term_id, $taxonomy );
2992  
2993      if ( is_wp_error( $term ) ) {
2994          return $term;
2995      }
2996  
2997      if ( ! $term ) {
2998          return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
2999      }
3000  
3001      $term = (array) $term->data;
3002  
3003      // Escape data pulled from DB.
3004      $term = wp_slash( $term );
3005  
3006      // Merge old and new args with new args overwriting old ones.
3007      $args = array_merge( $term, $args );
3008  
3009      $defaults    = array(
3010          'alias_of'    => '',
3011          'description' => '',
3012          'parent'      => 0,
3013          'slug'        => '',
3014      );
3015      $args        = wp_parse_args( $args, $defaults );
3016      $args        = sanitize_term( $args, $taxonomy, 'db' );
3017      $parsed_args = $args;
3018  
3019      // expected_slashed ($name)
3020      $name        = wp_unslash( $args['name'] );
3021      $description = wp_unslash( $args['description'] );
3022  
3023      $parsed_args['name']        = $name;
3024      $parsed_args['description'] = $description;
3025  
3026      if ( '' === trim( $name ) ) {
3027          return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
3028      }
3029  
3030      if ( (int) $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
3031          return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
3032      }
3033  
3034      $empty_slug = false;
3035      if ( empty( $args['slug'] ) ) {
3036          $empty_slug = true;
3037          $slug       = sanitize_title( $name );
3038      } else {
3039          $slug = $args['slug'];
3040      }
3041  
3042      $parsed_args['slug'] = $slug;
3043  
3044      $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
3045      if ( $args['alias_of'] ) {
3046          $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
3047          if ( ! empty( $alias->term_group ) ) {
3048              // The alias we want is already in a group, so let's use that one.
3049              $term_group = $alias->term_group;
3050          } elseif ( ! empty( $alias->term_id ) ) {
3051              /*
3052               * The alias is not in a group, so we create a new one
3053               * and add the alias to it.
3054               */
3055              $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;
3056  
3057              wp_update_term(
3058                  $alias->term_id,
3059                  $taxonomy,
3060                  array(
3061                      'term_group' => $term_group,
3062                  )
3063              );
3064          }
3065  
3066          $parsed_args['term_group'] = $term_group;
3067      }
3068  
3069      /**
3070       * Filters the term parent.
3071       *
3072       * Hook to this filter to see if it will cause a hierarchy loop.
3073       *
3074       * @since 3.1.0
3075       *
3076       * @param int    $parent      ID of the parent term.
3077       * @param int    $term_id     Term ID.
3078       * @param string $taxonomy    Taxonomy slug.
3079       * @param array  $parsed_args An array of potentially altered update arguments for the given term.
3080       * @param array  $args        An array of update arguments for the given term.
3081       */
3082      $parent = (int) apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args );
3083  
3084      // Check for duplicate slug.
3085      $duplicate = get_term_by( 'slug', $slug, $taxonomy );
3086      if ( $duplicate && $duplicate->term_id !== $term_id ) {
3087          // If an empty slug was passed or the parent changed, reset the slug to something unique.
3088          // Otherwise, bail.
3089          if ( $empty_slug || ( $parent !== (int) $term['parent'] ) ) {
3090              $slug = wp_unique_term_slug( $slug, (object) $args );
3091          } else {
3092              /* translators: %s: Taxonomy term slug. */
3093              return new WP_Error( 'duplicate_term_slug', sprintf( __( 'The slug &#8220;%s&#8221; is already in use by another term.' ), $slug ) );
3094          }
3095      }
3096  
3097      $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 ) );
3098  
3099      // Check whether this is a shared term that needs splitting.
3100      $_term_id = _split_shared_term( $term_id, $tt_id );
3101      if ( ! is_wp_error( $_term_id ) ) {
3102          $term_id = $_term_id;
3103      }
3104  
3105      /**
3106       * Fires immediately before the given terms are edited.
3107       *
3108       * @since 2.9.0
3109       *
3110       * @param int    $term_id  Term ID.
3111       * @param string $taxonomy Taxonomy slug.
3112       */
3113      do_action( 'edit_terms', $term_id, $taxonomy );
3114  
3115      $data = compact( 'name', 'slug', 'term_group' );
3116  
3117      /**
3118       * Filters term data before it is updated in the database.
3119       *
3120       * @since 4.7.0
3121       *
3122       * @param array  $data     Term data to be updated.
3123       * @param int    $term_id  Term ID.
3124       * @param string $taxonomy Taxonomy slug.
3125       * @param array  $args     Arguments passed to wp_update_term().
3126       */
3127      $data = apply_filters( 'wp_update_term_data', $data, $term_id, $taxonomy, $args );
3128  
3129      $wpdb->update( $wpdb->terms, $data, compact( 'term_id' ) );
3130  
3131      if ( empty( $slug ) ) {
3132          $slug = sanitize_title( $name, $term_id );
3133          $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
3134      }
3135  
3136      /**
3137       * Fires immediately after a term is updated in the database, but before its
3138       * term-taxonomy relationship is updated.
3139       *
3140       * @since 2.9.0
3141       *
3142       * @param int    $term_id  Term ID
3143       * @param string $taxonomy Taxonomy slug.
3144       */
3145      do_action( 'edited_terms', $term_id, $taxonomy );
3146  
3147      /**
3148       * Fires immediate before a term-taxonomy relationship is updated.
3149       *
3150       * @since 2.9.0
3151       *
3152       * @param int    $tt_id    Term taxonomy ID.
3153       * @param string $taxonomy Taxonomy slug.
3154       */
3155      do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
3156  
3157      $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
3158  
3159      /**
3160       * Fires immediately after a term-taxonomy relationship is updated.
3161       *
3162       * @since 2.9.0
3163       *
3164       * @param int    $tt_id    Term taxonomy ID.
3165       * @param string $taxonomy Taxonomy slug.
3166       */
3167      do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
3168  
3169      /**
3170       * Fires after a term has been updated, but before the term cache has been cleaned.
3171       *
3172       * The {@see 'edit_$taxonomy'} hook is also available for targeting a specific
3173       * taxonomy.
3174       *
3175       * @since 2.3.0
3176       *
3177       * @param int    $term_id  Term ID.
3178       * @param int    $tt_id    Term taxonomy ID.
3179       * @param string $taxonomy Taxonomy slug.
3180       */
3181      do_action( 'edit_term', $term_id, $tt_id, $taxonomy );
3182  
3183      /**
3184       * Fires after a term in a specific taxonomy has been updated, but before the term
3185       * cache has been cleaned.
3186       *
3187       * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
3188       *
3189       * @since 2.3.0
3190       *
3191       * @param int $term_id Term ID.
3192       * @param int $tt_id   Term taxonomy ID.
3193       */
3194      do_action( "edit_{$taxonomy}", $term_id, $tt_id );
3195  
3196      /** This filter is documented in wp-includes/taxonomy.php */
3197      $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
3198  
3199      clean_term_cache( $term_id, $taxonomy );
3200  
3201      /**
3202       * Fires after a term has been updated, and the term cache has been cleaned.
3203       *
3204       * The {@see 'edited_$taxonomy'} hook is also available for targeting a specific
3205       * taxonomy.
3206       *
3207       * @since 2.3.0
3208       *
3209       * @param int    $term_id  Term ID.
3210       * @param int    $tt_id    Term taxonomy ID.
3211       * @param string $taxonomy Taxonomy slug.
3212       */
3213      do_action( 'edited_term', $term_id, $tt_id, $taxonomy );
3214  
3215      /**
3216       * Fires after a term for a specific taxonomy has been updated, and the term
3217       * cache has been cleaned.
3218       *
3219       * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
3220       *
3221       * @since 2.3.0
3222       *
3223       * @param int $term_id Term ID.
3224       * @param int $tt_id   Term taxonomy ID.
3225       */
3226      do_action( "edited_{$taxonomy}", $term_id, $tt_id );
3227  
3228      /** This action is documented in wp-includes/taxonomy.php */
3229      do_action( 'saved_term', $term_id, $tt_id, $taxonomy, true );
3230  
3231      /** This action is documented in wp-includes/taxonomy.php */
3232      do_action( "saved_{$taxonomy}", $term_id, $tt_id, true );
3233  
3234      return array(
3235          'term_id'          => $term_id,
3236          'term_taxonomy_id' => $tt_id,
3237      );
3238  }
3239  
3240  /**
3241   * Enable or disable term counting.
3242   *
3243   * @since 2.5.0
3244   *
3245   * @param bool $defer Optional. Enable if true, disable if false.
3246   * @return bool Whether term counting is enabled or disabled.
3247   */
3248  function wp_defer_term_counting( $defer = null ) {
3249      static $_defer = false;
3250  
3251      if ( is_bool( $defer ) ) {
3252          $_defer = $defer;
3253          // Flush any deferred counts.
3254          if ( ! $defer ) {
3255              wp_update_term_count( null, null, true );
3256          }
3257      }
3258  
3259      return $_defer;
3260  }
3261  
3262  /**
3263   * Updates the amount of terms in taxonomy.
3264   *
3265   * If there is a taxonomy callback applied, then it will be called for updating
3266   * the count.
3267   *
3268   * The default action is to count what the amount of terms have the relationship
3269   * of term ID. Once that is done, then update the database.
3270   *
3271   * @since 2.3.0
3272   *
3273   * @param int|array $terms       The term_taxonomy_id of the terms.
3274   * @param string    $taxonomy    The context of the term.
3275   * @param bool      $do_deferred Whether to flush the deferred term counts too. Default false.
3276   * @return bool If no terms will return false, and if successful will return true.
3277   */
3278  function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) {
3279      static $_deferred = array();
3280  
3281      if ( $do_deferred ) {
3282          foreach ( (array) array_keys( $_deferred ) as $tax ) {
3283              wp_update_term_count_now( $_deferred[ $tax ], $tax );
3284              unset( $_deferred[ $tax ] );
3285          }
3286      }
3287  
3288      if ( empty( $terms ) ) {
3289          return false;
3290      }
3291  
3292      if ( ! is_array( $terms ) ) {
3293          $terms = array( $terms );
3294      }
3295  
3296      if ( wp_defer_term_counting() ) {
3297          if ( ! isset( $_deferred[ $taxonomy ] ) ) {
3298              $_deferred[ $taxonomy ] = array();
3299          }
3300          $_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
3301          return true;
3302      }
3303  
3304      return wp_update_term_count_now( $terms, $taxonomy );
3305  }
3306  
3307  /**
3308   * Perform term count update immediately.
3309   *
3310   * @since 2.5.0
3311   *
3312   * @param array  $terms    The term_taxonomy_id of terms to update.
3313   * @param string $taxonomy The context of the term.
3314   * @return true Always true when complete.
3315   */
3316  function wp_update_term_count_now( $terms, $taxonomy ) {
3317      $terms = array_map( 'intval', $terms );
3318  
3319      $taxonomy = get_taxonomy( $taxonomy );
3320      if ( ! empty( $taxonomy->update_count_callback ) ) {
3321          call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
3322      } else {
3323          $object_types = (array) $taxonomy->object_type;
3324          foreach ( $object_types as &$object_type ) {
3325              if ( 0 === strpos( $object_type, 'attachment:' ) ) {
3326                  list( $object_type ) = explode( ':', $object_type );
3327              }
3328          }
3329  
3330          if ( array_filter( $object_types, 'post_type_exists' ) == $object_types ) {
3331              // Only post types are attached to this taxonomy.
3332              _update_post_term_count( $terms, $taxonomy );
3333          } else {
3334              // Default count updater.
3335              _update_generic_term_count( $terms, $taxonomy );
3336          }
3337      }
3338  
3339      clean_term_cache( $terms, '', false );
3340  
3341      return true;
3342  }
3343  
3344  //
3345  // Cache.
3346  //
3347  
3348  /**
3349   * Removes the taxonomy relationship to terms from the cache.
3350   *
3351   * Will remove the entire taxonomy relationship containing term `$object_id`. The
3352   * term IDs have to exist within the taxonomy `$object_type` for the deletion to
3353   * take place.
3354   *
3355   * @since 2.3.0
3356   *
3357   * @global bool $_wp_suspend_cache_invalidation
3358   *
3359   * @see get_object_taxonomies() for more on $object_type.
3360   *
3361   * @param int|array    $object_ids  Single or list of term object ID(s).
3362   * @param array|string $object_type The taxonomy object type.
3363   */
3364  function clean_object_term_cache( $object_ids, $object_type ) {
3365      global $_wp_suspend_cache_invalidation;
3366  
3367      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
3368          return;
3369      }
3370  
3371      if ( ! is_array( $object_ids ) ) {
3372          $object_ids = array( $object_ids );
3373      }
3374  
3375      $taxonomies = get_object_taxonomies( $object_type );
3376  
3377      foreach ( $object_ids as $id ) {
3378          foreach ( $taxonomies as $taxonomy ) {
3379              wp_cache_delete( $id, "{$taxonomy}_relationships" );
3380          }
3381      }
3382  
3383      /**
3384       * Fires after the object term cache has been cleaned.
3385       *
3386       * @since 2.5.0
3387       *
3388       * @param array  $object_ids An array of object IDs.
3389       * @param string $object_type Object type.
3390       */
3391      do_action( 'clean_object_term_cache', $object_ids, $object_type );
3392  }
3393  
3394  /**
3395   * Will remove all of the term IDs from the cache.
3396   *
3397   * @since 2.3.0
3398   *
3399   * @global wpdb $wpdb                           WordPress database abstraction object.
3400   * @global bool $_wp_suspend_cache_invalidation
3401   *
3402   * @param int|int[] $ids            Single or array of term IDs.
3403   * @param string    $taxonomy       Optional. Taxonomy slug. Can be empty, in which case the taxonomies of the passed
3404   *                                  term IDs will be used. Default empty.
3405   * @param bool      $clean_taxonomy Optional. Whether to clean taxonomy wide caches (true), or just individual
3406   *                                  term object caches (false). Default true.
3407   */
3408  function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
3409      global $wpdb, $_wp_suspend_cache_invalidation;
3410  
3411      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
3412          return;
3413      }
3414  
3415      if ( ! is_array( $ids ) ) {
3416          $ids = array( $ids );
3417      }
3418  
3419      $taxonomies = array();
3420      // If no taxonomy, assume tt_ids.
3421      if ( empty( $taxonomy ) ) {
3422          $tt_ids = array_map( 'intval', $ids );
3423          $tt_ids = implode( ', ', $tt_ids );
3424          $terms  = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
3425          $ids    = array();
3426  
3427          foreach ( (array) $terms as $term ) {
3428              $taxonomies[] = $term->taxonomy;
3429              $ids[]        = $term->term_id;
3430              wp_cache_delete( $term->term_id, 'terms' );
3431          }
3432  
3433          $taxonomies = array_unique( $taxonomies );
3434      } else {
3435          $taxonomies = array( $taxonomy );
3436  
3437          foreach ( $taxonomies as $taxonomy ) {
3438              foreach ( $ids as $id ) {
3439                  wp_cache_delete( $id, 'terms' );
3440              }
3441          }
3442      }
3443  
3444      foreach ( $taxonomies as $taxonomy ) {
3445          if ( $clean_taxonomy ) {
3446              clean_taxonomy_cache( $taxonomy );
3447          }
3448  
3449          /**
3450           * Fires once after each taxonomy's term cache has been cleaned.
3451           *
3452           * @since 2.5.0
3453           * @since 4.5.0 Added the `$clean_taxonomy` parameter.
3454           *
3455           * @param array  $ids            An array of term IDs.
3456           * @param string $taxonomy       Taxonomy slug.
3457           * @param bool   $clean_taxonomy Whether or not to clean taxonomy-wide caches
3458           */
3459          do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy );
3460      }
3461  
3462      wp_cache_set( 'last_changed', microtime(), 'terms' );
3463  }
3464  
3465  /**
3466   * Clean the caches for a taxonomy.
3467   *
3468   * @since 4.9.0
3469   *
3470   * @param string $taxonomy Taxonomy slug.
3471   */
3472  function clean_taxonomy_cache( $taxonomy ) {
3473      wp_cache_delete( 'all_ids', $taxonomy );
3474      wp_cache_delete( 'get', $taxonomy );
3475  
3476      // Regenerate cached hierarchy.
3477      delete_option( "{$taxonomy}_children" );
3478      _get_term_hierarchy( $taxonomy );
3479  
3480      /**
3481       * Fires after a taxonomy's caches have been cleaned.
3482       *
3483       * @since 4.9.0
3484       *
3485       * @param string $taxonomy Taxonomy slug.
3486       */
3487      do_action( 'clean_taxonomy_cache', $taxonomy );
3488  }
3489  
3490  /**
3491   * Retrieves the cached term objects for the given object ID.
3492   *
3493   * Upstream functions (like get_the_terms() and is_object_in_term()) are
3494   * responsible for populating the object-term relationship cache. The current
3495   * function only fetches relationship data that is already in the cache.
3496   *
3497   * @since 2.3.0
3498   * @since 4.7.0 Returns a `WP_Error` object if there's an error with
3499   *              any of the matched terms.
3500   *
3501   * @param int    $id       Term object ID, for example a post, comment, or user ID.
3502   * @param string $taxonomy Taxonomy name.
3503   * @return bool|WP_Term[]|WP_Error Array of `WP_Term` objects, if cached.
3504   *                                 False if cache is empty for `$taxonomy` and `$id`.
3505   *                                 WP_Error if get_term() returns an error object for any term.
3506   */
3507  function get_object_term_cache( $id, $taxonomy ) {
3508      $_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" );
3509  
3510      // We leave the priming of relationship caches to upstream functions.
3511      if ( false === $_term_ids ) {
3512          return false;
3513      }
3514  
3515      // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
3516      $term_ids = array();
3517      foreach ( $_term_ids as $term_id ) {
3518          if ( is_numeric( $term_id ) ) {
3519              $term_ids[] = (int) $term_id;
3520          } elseif ( isset( $term_id->term_id ) ) {
3521              $term_ids[] = (int) $term_id->term_id;
3522          }
3523      }
3524  
3525      // Fill the term objects.
3526      _prime_term_caches( $term_ids );
3527  
3528      $terms = array();
3529      foreach ( $term_ids as $term_id ) {
3530          $term = get_term( $term_id, $taxonomy );
3531          if ( is_wp_error( $term ) ) {
3532              return $term;
3533          }
3534  
3535          $terms[] = $term;
3536      }
3537  
3538      return $terms;
3539  }
3540  
3541  /**
3542   * Updates the cache for the given term object ID(s).
3543   *
3544   * Note: Due to performance concerns, great care should be taken to only update
3545   * term caches when necessary. Processing time can increase exponentially depending
3546   * on both the number of passed term IDs and the number of taxonomies those terms
3547   * belong to.
3548   *
3549   * Caches will only be updated for terms not already cached.
3550   *
3551   * @since 2.3.0
3552   *
3553   * @param string|int[]    $object_ids  Comma-separated list or array of term object IDs.
3554   * @param string|string[] $object_type The taxonomy object type or array of the same.
3555   * @return void|false Void on success or if the `$object_ids` parameter is empty,
3556   *                    false if all of the terms in `$object_ids` are already cached.
3557   */
3558  function update_object_term_cache( $object_ids, $object_type ) {
3559      if ( empty( $object_ids ) ) {
3560          return;
3561      }
3562  
3563      if ( ! is_array( $object_ids ) ) {
3564          $object_ids = explode( ',', $object_ids );
3565      }
3566  
3567      $object_ids     = array_map( 'intval', $object_ids );
3568      $non_cached_ids = array();
3569  
3570      $taxonomies = get_object_taxonomies( $object_type );
3571  
3572      foreach ( $taxonomies as $taxonomy ) {
3573          $cache_values = wp_cache_get_multiple( (array) $object_ids, "{$taxonomy}_relationships" );
3574  
3575          foreach ( $cache_values as $id => $value ) {
3576              if ( false === $value ) {
3577                  $non_cached_ids[] = $id;
3578              }
3579          }
3580      }
3581  
3582      if ( empty( $non_cached_ids ) ) {
3583          return false;
3584      }
3585  
3586      $non_cached_ids = array_unique( $non_cached_ids );
3587  
3588      $terms = wp_get_object_terms(
3589          $non_cached_ids,
3590          $taxonomies,
3591          array(
3592              'fields'                 => 'all_with_object_id',
3593              'orderby'                => 'name',
3594              'update_term_meta_cache' => false,
3595          )
3596      );
3597  
3598      $object_terms = array();
3599      foreach ( (array) $terms as $term ) {
3600          $object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
3601      }
3602  
3603      foreach ( $non_cached_ids as $id ) {
3604          foreach ( $taxonomies as $taxonomy ) {
3605              if ( ! isset( $object_terms[ $id ][ $taxonomy ] ) ) {
3606                  if ( ! isset( $object_terms[ $id ] ) ) {
3607                      $object_terms[ $id ] = array();
3608                  }
3609                  $object_terms[ $id ][ $taxonomy ] = array();
3610              }
3611          }
3612      }
3613  
3614      foreach ( $object_terms as $id => $value ) {
3615          foreach ( $value as $taxonomy => $terms ) {
3616              wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
3617          }
3618      }
3619  }
3620  
3621  /**
3622   * Updates Terms to Taxonomy in cache.
3623   *
3624   * @since 2.3.0
3625   *
3626   * @param WP_Term[] $terms    Array of term objects to change.
3627   * @param string    $taxonomy Not used.
3628   */
3629  function update_term_cache( $terms, $taxonomy = '' ) {
3630      foreach ( (array) $terms as $term ) {
3631          // Create a copy in case the array was passed by reference.
3632          $_term = clone $term;
3633  
3634          // Object ID should not be cached.
3635          unset( $_term->object_id );
3636  
3637          wp_cache_add( $term->term_id, $_term, 'terms' );
3638      }
3639  }
3640  
3641  //
3642  // Private.
3643  //
3644  
3645  /**
3646   * Retrieves children of taxonomy as Term IDs.
3647   *
3648   * @access private
3649   * @since 2.3.0
3650   *
3651   * @param string $taxonomy Taxonomy name.
3652   * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs.
3653   */
3654  function _get_term_hierarchy( $taxonomy ) {
3655      if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
3656          return array();
3657      }
3658      $children = get_option( "{$taxonomy}_children" );
3659  
3660      if ( is_array( $children ) ) {
3661          return $children;
3662      }
3663      $children = array();
3664      $terms    = get_terms(
3665          array(
3666              'taxonomy'               => $taxonomy,
3667              'get'                    => 'all',
3668              'orderby'                => 'id',
3669              'fields'                 => 'id=>parent',
3670              'update_term_meta_cache' => false,
3671          )
3672      );
3673      foreach ( $terms as $term_id => $parent ) {
3674          if ( $parent > 0 ) {
3675              $children[ $parent ][] = $term_id;
3676          }
3677      }
3678      update_option( "{$taxonomy}_children", $children );
3679  
3680      return $children;
3681  }
3682  
3683  /**
3684   * Get the subset of $terms that are descendants of $term_id.
3685   *
3686   * If `$terms` is an array of objects, then _get_term_children() returns an array of objects.
3687   * If `$terms` is an array of IDs, then _get_term_children() returns an array of IDs.
3688   *
3689   * @access private
3690   * @since 2.3.0
3691   *
3692   * @param int    $term_id   The ancestor term: all returned terms should be descendants of `$term_id`.
3693   * @param array  $terms     The set of terms - either an array of term objects or term IDs - from which those that
3694   *                          are descendants of $term_id will be chosen.
3695   * @param string $taxonomy  The taxonomy which determines the hierarchy of the terms.
3696   * @param array  $ancestors Optional. Term ancestors that have already been identified. Passed by reference, to keep
3697   *                          track of found terms when recursing the hierarchy. The array of located ancestors is used
3698   *                          to prevent infinite recursion loops. For performance, `term_ids` are used as array keys,
3699   *                          with 1 as value. Default empty array.
3700   * @return array|WP_Error The subset of $terms that are descendants of $term_id.
3701   */
3702  function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
3703      $empty_array = array();
3704      if ( empty( $terms ) ) {
3705          return $empty_array;
3706      }
3707  
3708      $term_id      = (int) $term_id;
3709      $term_list    = array();
3710      $has_children = _get_term_hierarchy( $taxonomy );
3711  
3712      if ( $term_id && ! isset( $has_children[ $term_id ] ) ) {
3713          return $empty_array;
3714      }
3715  
3716      // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
3717      if ( empty( $ancestors ) ) {
3718          $ancestors[ $term_id ] = 1;
3719      }
3720  
3721      foreach ( (array) $terms as $term ) {
3722          $use_id = false;
3723          if ( ! is_object( $term ) ) {
3724              $term = get_term( $term, $taxonomy );
3725              if ( is_wp_error( $term ) ) {
3726                  return $term;
3727              }
3728              $use_id = true;
3729          }
3730  
3731          // Don't recurse if we've already identified the term as a child - this indicates a loop.
3732          if ( isset( $ancestors[ $term->term_id ] ) ) {
3733              continue;
3734          }
3735  
3736          if ( (int) $term->parent === $term_id ) {
3737              if ( $use_id ) {
3738                  $term_list[] = $term->term_id;
3739              } else {
3740                  $term_list[] = $term;
3741              }
3742  
3743              if ( ! isset( $has_children[ $term->term_id ] ) ) {
3744                  continue;
3745              }
3746  
3747              $ancestors[ $term->term_id ] = 1;
3748  
3749              $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors );
3750              if ( $children ) {
3751                  $term_list = array_merge( $term_list, $children );
3752              }
3753          }
3754      }
3755  
3756      return $term_list;
3757  }
3758  
3759  /**
3760   * Add count of children to parent count.
3761   *
3762   * Recalculates term counts by including items from child terms. Assumes all
3763   * relevant children are already in the $terms argument.
3764   *
3765   * @access private
3766   * @since 2.3.0
3767   *
3768   * @global wpdb $wpdb WordPress database abstraction object.
3769   *
3770   * @param object[]|WP_Term[] $terms    List of term objects (passed by reference).
3771   * @param string             $taxonomy Term context.
3772   */
3773  function _pad_term_counts( &$terms, $taxonomy ) {
3774      global $wpdb;
3775  
3776      // This function only works for hierarchical taxonomies like post categories.
3777      if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
3778          return;
3779      }
3780  
3781      $term_hier = _get_term_hierarchy( $taxonomy );
3782  
3783      if ( empty( $term_hier ) ) {
3784          return;
3785      }
3786  
3787      $term_items  = array();
3788      $terms_by_id = array();
3789      $term_ids    = array();
3790  
3791      foreach ( (array) $terms as $key => $term ) {
3792          $terms_by_id[ $term->term_id ]       = & $terms[ $key ];
3793          $term_ids[ $term->term_taxonomy_id ] = $term->term_id;
3794      }
3795  
3796      // Get the object and term IDs and stick them in a lookup table.
3797      $tax_obj      = get_taxonomy( $taxonomy );
3798      $object_types = esc_sql( $tax_obj->object_type );
3799      $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'" );
3800  
3801      foreach ( $results as $row ) {
3802          $id = $term_ids[ $row->term_taxonomy_id ];
3803  
3804          $term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1;
3805      }
3806  
3807      // Touch every ancestor's lookup row for each post in each term.
3808      foreach ( $term_ids as $term_id ) {
3809          $child     = $term_id;
3810          $ancestors = array();
3811          while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) {
3812              $ancestors[] = $child;
3813  
3814              if ( ! empty( $term_items[ $term_id ] ) ) {
3815                  foreach ( $term_items[ $term_id ] as $item_id => $touches ) {
3816                      $term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1;
3817                  }
3818              }
3819  
3820              $child = $parent;
3821  
3822              if ( in_array( $parent, $ancestors, true ) ) {
3823                  break;
3824              }
3825          }
3826      }
3827  
3828      // Transfer the touched cells.
3829      foreach ( (array) $term_items as $id => $items ) {
3830          if ( isset( $terms_by_id[ $id ] ) ) {
3831              $terms_by_id[ $id ]->count = count( $items );
3832          }
3833      }
3834  }
3835  
3836  /**
3837   * Adds any terms from the given IDs to the cache that do not already exist in cache.
3838   *
3839   * @since 4.6.0
3840   * @access private
3841   *
3842   * @global wpdb $wpdb WordPress database abstraction object.
3843   *
3844   * @param array $term_ids          Array of term IDs.
3845   * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
3846   */
3847  function _prime_term_caches( $term_ids, $update_meta_cache = true ) {
3848      global $wpdb;
3849  
3850      $non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
3851      if ( ! empty( $non_cached_ids ) ) {
3852          $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)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
3853  
3854          update_term_cache( $fresh_terms, $update_meta_cache );
3855  
3856          if ( $update_meta_cache ) {
3857              update_termmeta_cache( $non_cached_ids );
3858          }
3859      }
3860  }
3861  
3862  //
3863  // Default callbacks.
3864  //
3865  
3866  /**
3867   * Will update term count based on object types of the current taxonomy.
3868   *
3869   * Private function for the default callback for post_tag and category
3870   * taxonomies.
3871   *
3872   * @access private
3873   * @since 2.3.0
3874   *
3875   * @global wpdb $wpdb WordPress database abstraction object.
3876   *
3877   * @param int[]       $terms    List of Term taxonomy IDs.
3878   * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
3879   */
3880  function _update_post_term_count( $terms, $taxonomy ) {
3881      global $wpdb;
3882  
3883      $object_types = (array) $taxonomy->object_type;
3884  
3885      foreach ( $object_types as &$object_type ) {
3886          list( $object_type ) = explode( ':', $object_type );
3887      }
3888  
3889      $object_types = array_unique( $object_types );
3890  
3891      $check_attachments = array_search( 'attachment', $object_types, true );
3892      if ( false !== $check_attachments ) {
3893          unset( $object_types[ $check_attachments ] );
3894          $check_attachments = true;
3895      }
3896  
3897      if ( $object_types ) {
3898          $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
3899      }
3900  
3901      $post_statuses = array( 'publish' );
3902  
3903      /**
3904       * Filters the post statuses for updating the term count.
3905       *
3906       * @since 5.7.0
3907       *
3908       * @param string[]    $post_statuses List of post statuses to include in the count. Default is 'publish'.
3909       * @param WP_Taxonomy $taxonomy      Current taxonomy object.
3910       */
3911      $post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );
3912  
3913      foreach ( (array) $terms as $term ) {
3914          $count = 0;
3915  
3916          // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
3917          if ( $check_attachments ) {
3918              // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
3919              $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 IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
3920          }
3921  
3922          if ( $object_types ) {
3923              // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
3924              $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 IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
3925          }
3926  
3927          /** This action is documented in wp-includes/taxonomy.php */
3928          do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
3929          $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
3930  
3931          /** This action is documented in wp-includes/taxonomy.php */
3932          do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
3933      }
3934  }
3935  
3936  /**
3937   * Will update term count based on number of objects.
3938   *
3939   * Default callback for the 'link_category' taxonomy.
3940   *
3941   * @since 3.3.0
3942   *
3943   * @global wpdb $wpdb WordPress database abstraction object.
3944   *
3945   * @param int[]       $terms    List of term taxonomy IDs.
3946   * @param WP_Taxonomy $taxonomy Current taxonomy object of terms.
3947   */
3948  function _update_generic_term_count( $terms, $taxonomy ) {
3949      global $wpdb;
3950  
3951      foreach ( (array) $terms as $term ) {
3952          $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
3953  
3954          /** This action is documented in wp-includes/taxonomy.php */
3955          do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
3956          $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
3957  
3958          /** This action is documented in wp-includes/taxonomy.php */
3959          do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
3960      }
3961  }
3962  
3963  /**
3964   * Create a new term for a term_taxonomy item that currently shares its term
3965   * with another term_taxonomy.
3966   *
3967   * @ignore
3968   * @since 4.2.0
3969   * @since 4.3.0 Introduced `$record` parameter. Also, `$term_id` and
3970   *              `$term_taxonomy_id` can now accept objects.
3971   *
3972   * @global wpdb $wpdb WordPress database abstraction object.
3973   *
3974   * @param int|object $term_id          ID of the shared term, or the shared term object.
3975   * @param int|object $term_taxonomy_id ID of the term_taxonomy item to receive a new term, or the term_taxonomy object
3976   *                                     (corresponding to a row from the term_taxonomy table).
3977   * @param bool       $record           Whether to record data about the split term in the options table. The recording
3978   *                                     process has the potential to be resource-intensive, so during batch operations
3979   *                                     it can be beneficial to skip inline recording and do it just once, after the
3980   *                                     batch is processed. Only set this to `false` if you know what you are doing.
3981   *                                     Default: true.
3982   * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
3983   *                      database schema), `$term_id` is returned. When the term is successfully split, the
3984   *                      new term_id is returned. A WP_Error is returned for miscellaneous errors.
3985   */
3986  function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) {
3987      global $wpdb;
3988  
3989      if ( is_object( $term_id ) ) {
3990          $shared_term = $term_id;
3991          $term_id     = (int) $shared_term->term_id;
3992      }
3993  
3994      if ( is_object( $term_taxonomy_id ) ) {
3995          $term_taxonomy    = $term_taxonomy_id;
3996          $term_taxonomy_id = (int) $term_taxonomy->term_taxonomy_id;
3997      }
3998  
3999      // If there are no shared term_taxonomy rows, there's nothing to do here.
4000      $shared_tt_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );
4001  
4002      if ( ! $shared_tt_count ) {
4003          return $term_id;
4004      }
4005  
4006      /*
4007       * Verify that the term_taxonomy_id passed to the function is actually associated with the term_id.
4008       * If there's a mismatch, it may mean that the term is already split. Return the actual term_id from the db.
4009       */
4010      $check_term_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
4011      if ( $check_term_id !== $term_id ) {
4012          return $check_term_id;
4013      }
4014  
4015      // Pull up data about the currently shared slug, which we'll use to populate the new one.
4016      if ( empty( $shared_term ) ) {
4017          $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
4018      }
4019  
4020      $new_term_data = array(
4021          'name'       => $shared_term->name,
4022          'slug'       => $shared_term->slug,
4023          'term_group' => $shared_term->term_group,
4024      );
4025  
4026      if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
4027          return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
4028      }
4029  
4030      $new_term_id = (int) $wpdb->insert_id;
4031  
4032      // Update the existing term_taxonomy to point to the newly created term.
4033      $wpdb->update(
4034          $wpdb->term_taxonomy,
4035          array( 'term_id' => $new_term_id ),
4036          array( 'term_taxonomy_id' => $term_taxonomy_id )
4037      );
4038  
4039      // Reassign child terms to the new parent.
4040      if ( empty( $term_taxonomy ) ) {
4041          $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
4042      }
4043  
4044      $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 ) );
4045      if ( ! empty( $children_tt_ids ) ) {
4046          foreach ( $children_tt_ids as $child_tt_id ) {
4047              $wpdb->update(
4048                  $wpdb->term_taxonomy,
4049                  array( 'parent' => $new_term_id ),
4050                  array( 'term_taxonomy_id' => $child_tt_id )
4051              );
4052              clean_term_cache( (int) $child_tt_id, '', false );
4053          }
4054      } else {
4055          // If the term has no children, we must force its taxonomy cache to be rebuilt separately.
4056          clean_term_cache( $new_term_id, $term_taxonomy->taxonomy, false );
4057      }
4058  
4059      clean_term_cache( $term_id, $term_taxonomy->taxonomy, false );
4060  
4061      /*
4062       * Taxonomy cache clearing is delayed to avoid race conditions that may occur when
4063       * regenerating the taxonomy's hierarchy tree.
4064       */
4065      $taxonomies_to_clean = array( $term_taxonomy->taxonomy );
4066  
4067      // Clean the cache for term taxonomies formerly shared with the current term.
4068      $shared_term_taxonomies = $wpdb->get_col( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
4069      $taxonomies_to_clean    = array_merge( $taxonomies_to_clean, $shared_term_taxonomies );
4070  
4071      foreach ( $taxonomies_to_clean as $taxonomy_to_clean ) {
4072          clean_taxonomy_cache( $taxonomy_to_clean );
4073      }
4074  
4075      // Keep a record of term_ids that have been split, keyed by old term_id. See wp_get_split_term().
4076      if ( $record ) {
4077          $split_term_data = get_option( '_split_terms', array() );
4078          if ( ! isset( $split_term_data[ $term_id ] ) ) {
4079              $split_term_data[ $term_id ] = array();
4080          }
4081  
4082          $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
4083          update_option( '_split_terms', $split_term_data );
4084      }
4085  
4086      // If we've just split the final shared term, set the "finished" flag.
4087      $shared_terms_exist = $wpdb->get_results(
4088          "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
4089           LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
4090           GROUP BY t.term_id
4091           HAVING term_tt_count > 1
4092           LIMIT 1"
4093      );
4094      if ( ! $shared_terms_exist ) {
4095          update_option( 'finished_splitting_shared_terms', true );
4096      }
4097  
4098      /**
4099       * Fires after a previously shared taxonomy term is split into two separate terms.
4100       *
4101       * @since 4.2.0
4102       *
4103       * @param int    $term_id          ID of the formerly shared term.
4104       * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
4105       * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4106       * @param string $taxonomy         Taxonomy for the split term.
4107       */
4108      do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy );
4109  
4110      return $new_term_id;
4111  }
4112  
4113  /**
4114   * Splits a batch of shared taxonomy terms.
4115   *
4116   * @since 4.3.0
4117   *
4118   * @global wpdb $wpdb WordPress database abstraction object.
4119   */
4120  function _wp_batch_split_terms() {
4121      global $wpdb;
4122  
4123      $lock_name = 'term_split.lock';
4124  
4125      // Try to lock.
4126      $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
4127  
4128      if ( ! $lock_result ) {
4129          $lock_result = get_option( $lock_name );
4130  
4131          // Bail if we were unable to create a lock, or if the existing lock is still valid.
4132          if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {
4133              wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
4134              return;
4135          }
4136      }
4137  
4138      // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
4139      update_option( $lock_name, time() );
4140  
4141      // Get a list of shared terms (those with more than one associated row in term_taxonomy).
4142      $shared_terms = $wpdb->get_results(
4143          "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
4144           LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
4145           GROUP BY t.term_id
4146           HAVING term_tt_count > 1
4147           LIMIT 10"
4148      );
4149  
4150      // No more terms, we're done here.
4151      if ( ! $shared_terms ) {
4152          update_option( 'finished_splitting_shared_terms', true );
4153          delete_option( $lock_name );
4154          return;
4155      }
4156  
4157      // Shared terms found? We'll need to run this script again.
4158      wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
4159  
4160      // Rekey shared term array for faster lookups.
4161      $_shared_terms = array();
4162      foreach ( $shared_terms as $shared_term ) {
4163          $term_id                   = (int) $shared_term->term_id;
4164          $_shared_terms[ $term_id ] = $shared_term;
4165      }
4166      $shared_terms = $_shared_terms;
4167  
4168      // Get term taxonomy data for all shared terms.
4169      $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
4170      $shared_tts      = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
4171  
4172      // Split term data recording is slow, so we do it just once, outside the loop.
4173      $split_term_data    = get_option( '_split_terms', array() );
4174      $skipped_first_term = array();
4175      $taxonomies         = array();
4176      foreach ( $shared_tts as $shared_tt ) {
4177          $term_id = (int) $shared_tt->term_id;
4178  
4179          // Don't split the first tt belonging to a given term_id.
4180          if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
4181              $skipped_first_term[ $term_id ] = 1;
4182              continue;
4183          }
4184  
4185          if ( ! isset( $split_term_data[ $term_id ] ) ) {
4186              $split_term_data[ $term_id ] = array();
4187          }
4188  
4189          // Keep track of taxonomies whose hierarchies need flushing.
4190          if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
4191              $taxonomies[ $shared_tt->taxonomy ] = 1;
4192          }
4193  
4194          // Split the term.
4195          $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
4196      }
4197  
4198      // Rebuild the cached hierarchy for each affected taxonomy.
4199      foreach ( array_keys( $taxonomies ) as $tax ) {
4200          delete_option( "{$tax}_children" );
4201          _get_term_hierarchy( $tax );
4202      }
4203  
4204      update_option( '_split_terms', $split_term_data );
4205  
4206      delete_option( $lock_name );
4207  }
4208  
4209  /**
4210   * In order to avoid the _wp_batch_split_terms() job being accidentally removed,
4211   * check that it's still scheduled while we haven't finished splitting terms.
4212   *
4213   * @ignore
4214   * @since 4.3.0
4215   */
4216  function _wp_check_for_scheduled_split_terms() {
4217      if ( ! get_option( 'finished_splitting_shared_terms' ) && ! wp_next_scheduled( 'wp_split_shared_term_batch' ) ) {
4218          wp_schedule_single_event( time() + MINUTE_IN_SECONDS, 'wp_split_shared_term_batch' );
4219      }
4220  }
4221  
4222  /**
4223   * Check default categories when a term gets split to see if any of them need to be updated.
4224   *
4225   * @ignore
4226   * @since 4.2.0
4227   *
4228   * @param int    $term_id          ID of the formerly shared term.
4229   * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
4230   * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4231   * @param string $taxonomy         Taxonomy for the split term.
4232   */
4233  function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4234      if ( 'category' !== $taxonomy ) {
4235          return;
4236      }
4237  
4238      foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
4239          if ( (int) get_option( $option, -1 ) === $term_id ) {
4240              update_option( $option, $new_term_id );
4241          }
4242      }
4243  }
4244  
4245  /**
4246   * Check menu items when a term gets split to see if any of them need to be updated.
4247   *
4248   * @ignore
4249   * @since 4.2.0
4250   *
4251   * @global wpdb $wpdb WordPress database abstraction object.
4252   *
4253   * @param int    $term_id          ID of the formerly shared term.
4254   * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
4255   * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4256   * @param string $taxonomy         Taxonomy for the split term.
4257   */
4258  function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4259      global $wpdb;
4260      $post_ids = $wpdb->get_col(
4261          $wpdb->prepare(
4262              "SELECT m1.post_id
4263          FROM {$wpdb->postmeta} AS m1
4264              INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
4265              INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id )
4266          WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
4267              AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = %s )
4268              AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
4269              $taxonomy,
4270              $term_id
4271          )
4272      );
4273  
4274      if ( $post_ids ) {
4275          foreach ( $post_ids as $post_id ) {
4276              update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
4277          }
4278      }
4279  }
4280  
4281  /**
4282   * If the term being split is a nav_menu, change associations.
4283   *
4284   * @ignore
4285   * @since 4.3.0
4286   *
4287   * @param int    $term_id          ID of the formerly shared term.
4288   * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
4289   * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4290   * @param string $taxonomy         Taxonomy for the split term.
4291   */
4292  function _wp_check_split_nav_menu_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4293      if ( 'nav_menu' !== $taxonomy ) {
4294          return;
4295      }
4296  
4297      // Update menu locations.
4298      $locations = get_nav_menu_locations();
4299      foreach ( $locations as $location => $menu_id ) {
4300          if ( $term_id === $menu_id ) {
4301              $locations[ $location ] = $new_term_id;
4302          }
4303      }
4304      set_theme_mod( 'nav_menu_locations', $locations );
4305  }
4306  
4307  /**
4308   * Get data about terms that previously shared a single term_id, but have since been split.
4309   *
4310   * @since 4.2.0
4311   *
4312   * @param int $old_term_id Term ID. This is the old, pre-split term ID.
4313   * @return array Array of new term IDs, keyed by taxonomy.
4314   */
4315  function wp_get_split_terms( $old_term_id ) {
4316      $split_terms = get_option( '_split_terms', array() );
4317  
4318      $terms = array();
4319      if ( isset( $split_terms[ $old_term_id ] ) ) {
4320          $terms = $split_terms[ $old_term_id ];
4321      }
4322  
4323      return $terms;
4324  }
4325  
4326  /**
4327   * Get the new term ID corresponding to a previously split term.
4328   *
4329   * @since 4.2.0
4330   *
4331   * @param int    $old_term_id Term ID. This is the old, pre-split term ID.
4332   * @param string $taxonomy    Taxonomy that the term belongs to.
4333   * @return int|false If a previously split term is found corresponding to the old term_id and taxonomy,
4334   *                   the new term_id will be returned. If no previously split term is found matching
4335   *                   the parameters, returns false.
4336   */
4337  function wp_get_split_term( $old_term_id, $taxonomy ) {
4338      $split_terms = wp_get_split_terms( $old_term_id );
4339  
4340      $term_id = false;
4341      if ( isset( $split_terms[ $taxonomy ] ) ) {
4342          $term_id = (int) $split_terms[ $taxonomy ];
4343      }
4344  
4345      return $term_id;
4346  }
4347  
4348  /**
4349   * Determine whether a term is shared between multiple taxonomies.
4350   *
4351   * Shared taxonomy terms began to be split in 4.3, but failed cron tasks or
4352   * other delays in upgrade routines may cause shared terms to remain.
4353   *
4354   * @since 4.4.0
4355   *
4356   * @param int $term_id Term ID.
4357   * @return bool Returns false if a term is not shared between multiple taxonomies or
4358   *              if splitting shared taxonomy terms is finished.
4359   */
4360  function wp_term_is_shared( $term_id ) {
4361      global $wpdb;
4362  
4363      if ( get_option( 'finished_splitting_shared_terms' ) ) {
4364          return false;
4365      }
4366  
4367      $tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
4368  
4369      return $tt_count > 1;
4370  }
4371  
4372  /**
4373   * Generate a permalink for a taxonomy term archive.
4374   *
4375   * @since 2.5.0
4376   *
4377   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
4378   *
4379   * @param WP_Term|int|string $term     The term object, ID, or slug whose link will be retrieved.
4380   * @param string             $taxonomy Optional. Taxonomy. Default empty.
4381   * @return string|WP_Error URL of the taxonomy term archive on success, WP_Error if term does not exist.
4382   */
4383  function get_term_link( $term, $taxonomy = '' ) {
4384      global $wp_rewrite;
4385  
4386      if ( ! is_object( $term ) ) {
4387          if ( is_int( $term ) ) {
4388              $term = get_term( $term, $taxonomy );
4389          } else {
4390              $term = get_term_by( 'slug', $term, $taxonomy );
4391          }
4392      }
4393  
4394      if ( ! is_object( $term ) ) {
4395          $term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
4396      }
4397  
4398      if ( is_wp_error( $term ) ) {
4399          return $term;
4400      }
4401  
4402      $taxonomy = $term->taxonomy;
4403  
4404      $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
4405  
4406      /**
4407       * Filters the permalink structure for a terms before token replacement occurs.
4408       *
4409       * @since 4.9.0
4410       *
4411       * @param string  $termlink The permalink structure for the term's taxonomy.
4412       * @param WP_Term $term     The term object.
4413       */
4414      $termlink = apply_filters( 'pre_term_link', $termlink, $term );
4415  
4416      $slug = $term->slug;
4417      $t    = get_taxonomy( $taxonomy );
4418  
4419      if ( empty( $termlink ) ) {
4420          if ( 'category' === $taxonomy ) {
4421              $termlink = '?cat=' . $term->term_id;
4422          } elseif ( $t->query_var ) {
4423              $termlink = "?$t->query_var=$slug";
4424          } else {
4425              $termlink = "?taxonomy=$taxonomy&term=$slug";
4426          }
4427          $termlink = home_url( $termlink );
4428      } else {
4429          if ( $t->rewrite['hierarchical'] ) {
4430              $hierarchical_slugs = array();
4431              $ancestors          = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
4432              foreach ( (array) $ancestors as $ancestor ) {
4433                  $ancestor_term        = get_term( $ancestor, $taxonomy );
4434                  $hierarchical_slugs[] = $ancestor_term->slug;
4435              }
4436              $hierarchical_slugs   = array_reverse( $hierarchical_slugs );
4437              $hierarchical_slugs[] = $slug;
4438              $termlink             = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
4439          } else {
4440              $termlink = str_replace( "%$taxonomy%", $slug, $termlink );
4441          }
4442          $termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
4443      }
4444  
4445      // Back compat filters.
4446      if ( 'post_tag' === $taxonomy ) {
4447  
4448          /**
4449           * Filters the tag link.
4450           *
4451           * @since 2.3.0
4452           * @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
4453           * @since 5.4.1 Restored (un-deprecated).
4454           *
4455           * @param string $termlink Tag link URL.
4456           * @param int    $term_id  Term ID.
4457           */
4458          $termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
4459      } elseif ( 'category' === $taxonomy ) {
4460  
4461          /**
4462           * Filters the category link.
4463           *
4464           * @since 1.5.0
4465           * @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
4466           * @since 5.4.1 Restored (un-deprecated).
4467           *
4468           * @param string $termlink Category link URL.
4469           * @param int    $term_id  Term ID.
4470           */
4471          $termlink = apply_filters( 'category_link', $termlink, $term->term_id );
4472      }
4473  
4474      /**
4475       * Filters the term link.
4476       *
4477       * @since 2.5.0
4478       *
4479       * @param string  $termlink Term link URL.
4480       * @param WP_Term $term     Term object.
4481       * @param string  $taxonomy Taxonomy slug.
4482       */
4483      return apply_filters( 'term_link', $termlink, $term, $taxonomy );
4484  }
4485  
4486  /**
4487   * Display the taxonomies of a post with available options.
4488   *
4489   * This function can be used within the loop to display the taxonomies for a
4490   * post without specifying the Post ID. You can also use it outside the Loop to
4491   * display the taxonomies for a specific post.
4492   *
4493   * @since 2.5.0
4494   *
4495   * @param array $args {
4496   *     Arguments about which post to use and how to format the output. Shares all of the arguments
4497   *     supported by get_the_taxonomies(), in addition to the following.
4498   *
4499   *     @type  int|WP_Post $post   Post ID or object to get taxonomies of. Default current post.
4500   *     @type  string      $before Displays before the taxonomies. Default empty string.
4501   *     @type  string      $sep    Separates each taxonomy. Default is a space.
4502   *     @type  string      $after  Displays after the taxonomies. Default empty string.
4503   * }
4504   */
4505  function the_taxonomies( $args = array() ) {
4506      $defaults = array(
4507          'post'   => 0,
4508          'before' => '',
4509          'sep'    => ' ',
4510          'after'  => '',
4511      );
4512  
4513      $parsed_args = wp_parse_args( $args, $defaults );
4514  
4515      echo $parsed_args['before'] . implode( $parsed_args['sep'], get_the_taxonomies( $parsed_args['post'], $parsed_args ) ) . $parsed_args['after'];
4516  }
4517  
4518  /**
4519   * Retrieve all taxonomies associated with a post.
4520   *
4521   * This function can be used within the loop. It will also return an array of
4522   * the taxonomies with links to the taxonomy and name.
4523   *
4524   * @since 2.5.0
4525   *
4526   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
4527   * @param array       $args {
4528   *           Optional. Arguments about how to format the list of taxonomies. Default empty array.
4529   *
4530   *     @type string $template      Template for displaying a taxonomy label and list of terms.
4531   *                                 Default is "Label: Terms."
4532   *     @type string $term_template Template for displaying a single term in the list. Default is the term name
4533   *                                 linked to its archive.
4534   * }
4535   * @return array List of taxonomies.
4536   */
4537  function get_the_taxonomies( $post = 0, $args = array() ) {
4538      $post = get_post( $post );
4539  
4540      $args = wp_parse_args(
4541          $args,
4542          array(
4543              /* translators: %s: Taxonomy label, %l: List of terms formatted as per $term_template. */
4544              'template'      => __( '%s: %l.' ),
4545              'term_template' => '<a href="%1$s">%2$s</a>',
4546          )
4547      );
4548  
4549      $taxonomies = array();
4550  
4551      if ( ! $post ) {
4552          return $taxonomies;
4553      }
4554  
4555      foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
4556          $t = (array) get_taxonomy( $taxonomy );
4557          if ( empty( $t['label'] ) ) {
4558              $t['label'] = $taxonomy;
4559          }
4560          if ( empty( $t['args'] ) ) {
4561              $t['args'] = array();
4562          }
4563          if ( empty( $t['template'] ) ) {
4564              $t['template'] = $args['template'];
4565          }
4566          if ( empty( $t['term_template'] ) ) {
4567              $t['term_template'] = $args['term_template'];
4568          }
4569  
4570          $terms = get_object_term_cache( $post->ID, $taxonomy );
4571          if ( false === $terms ) {
4572              $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
4573          }
4574          $links = array();
4575  
4576          foreach ( $terms as $term ) {
4577              $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
4578          }
4579          if ( $links ) {
4580              $taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
4581          }
4582      }
4583      return $taxonomies;
4584  }
4585  
4586  /**
4587   * Retrieve all taxonomy names for the given post.
4588   *
4589   * @since 2.5.0
4590   *
4591   * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
4592   * @return string[] An array of all taxonomy names for the given post.
4593   */
4594  function get_post_taxonomies( $post = 0 ) {
4595      $post = get_post( $post );
4596  
4597      return get_object_taxonomies( $post );
4598  }
4599  
4600  /**
4601   * Determine if the given object is associated with any of the given terms.
4602   *
4603   * The given terms are checked against the object's terms' term_ids, names and slugs.
4604   * Terms given as integers will only be checked against the object's terms' term_ids.
4605   * If no terms are given, determines if object is associated with any terms in the given taxonomy.
4606   *
4607   * @since 2.7.0
4608   *
4609   * @param int                       $object_id ID of the object (post ID, link ID, ...).
4610   * @param string                    $taxonomy  Single taxonomy name.
4611   * @param int|string|int[]|string[] $terms     Optional. Term ID, name, slug, or array of such
4612   *                                             to check against. Default null.
4613   * @return bool|WP_Error WP_Error on input error.
4614   */
4615  function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
4616      $object_id = (int) $object_id;
4617      if ( ! $object_id ) {
4618          return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
4619      }
4620  
4621      $object_terms = get_object_term_cache( $object_id, $taxonomy );
4622      if ( false === $object_terms ) {
4623          $object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
4624          if ( is_wp_error( $object_terms ) ) {
4625              return $object_terms;
4626          }
4627  
4628          wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
4629      }
4630  
4631      if ( is_wp_error( $object_terms ) ) {
4632          return $object_terms;
4633      }
4634      if ( empty( $object_terms ) ) {
4635          return false;
4636      }
4637      if ( empty( $terms ) ) {
4638          return ( ! empty( $object_terms ) );
4639      }
4640  
4641      $terms = (array) $terms;
4642  
4643      $ints = array_filter( $terms, 'is_int' );
4644      if ( $ints ) {
4645          $strs = array_diff( $terms, $ints );
4646      } else {
4647          $strs =& $terms;
4648      }
4649  
4650      foreach ( $object_terms as $object_term ) {
4651          // If term is an int, check against term_ids only.
4652          if ( $ints && in_array( $object_term->term_id, $ints, true ) ) {
4653              return true;
4654          }
4655  
4656          if ( $strs ) {
4657              // Only check numeric strings against term_id, to avoid false matches due to type juggling.
4658              $numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
4659              if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
4660                  return true;
4661              }
4662  
4663              if ( in_array( $object_term->name, $strs, true ) ) {
4664                  return true;
4665              }
4666              if ( in_array( $object_term->slug, $strs, true ) ) {
4667                  return true;
4668              }
4669          }
4670      }
4671  
4672      return false;
4673  }
4674  
4675  /**
4676   * Determine if the given object type is associated with the given taxonomy.
4677   *
4678   * @since 3.0.0
4679   *
4680   * @param string $object_type Object type string.
4681   * @param string $taxonomy    Single taxonomy name.
4682   * @return bool True if object is associated with the taxonomy, otherwise false.
4683   */
4684  function is_object_in_taxonomy( $object_type, $taxonomy ) {
4685      $taxonomies = get_object_taxonomies( $object_type );
4686      if ( empty( $taxonomies ) ) {
4687          return false;
4688      }
4689      return in_array( $taxonomy, $taxonomies, true );
4690  }
4691  
4692  /**
4693   * Get an array of ancestor IDs for a given object.
4694   *
4695   * @since 3.1.0
4696   * @since 4.1.0 Introduced the `$resource_type` argument.
4697   *
4698   * @param int    $object_id     Optional. The ID of the object. Default 0.
4699   * @param string $object_type   Optional. The type of object for which we'll be retrieving
4700   *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
4701   * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
4702   *                              or 'taxonomy'. Default empty.
4703   * @return int[] An array of IDs of ancestors from lowest to highest in the hierarchy.
4704   */
4705  function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
4706      $object_id = (int) $object_id;
4707  
4708      $ancestors = array();
4709  
4710      if ( empty( $object_id ) ) {
4711  
4712          /** This filter is documented in wp-includes/taxonomy.php */
4713          return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
4714      }
4715  
4716      if ( ! $resource_type ) {
4717          if ( is_taxonomy_hierarchical( $object_type ) ) {
4718              $resource_type = 'taxonomy';
4719          } elseif ( post_type_exists( $object_type ) ) {
4720              $resource_type = 'post_type';
4721          }
4722      }
4723  
4724      if ( 'taxonomy' === $resource_type ) {
4725          $term = get_term( $object_id, $object_type );
4726          while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors, true ) ) {
4727              $ancestors[] = (int) $term->parent;
4728              $term        = get_term( $term->parent, $object_type );
4729          }
4730      } elseif ( 'post_type' === $resource_type ) {
4731          $ancestors = get_post_ancestors( $object_id );
4732      }
4733  
4734      /**
4735       * Filters a given object's ancestors.
4736       *
4737       * @since 3.1.0
4738       * @since 4.1.1 Introduced the `$resource_type` parameter.
4739       *
4740       * @param int[]  $ancestors     An array of IDs of object ancestors.
4741       * @param int    $object_id     Object ID.
4742       * @param string $object_type   Type of object.
4743       * @param string $resource_type Type of resource $object_type is.
4744       */
4745      return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
4746  }
4747  
4748  /**
4749   * Returns the term's parent's term_ID.
4750   *
4751   * @since 3.1.0
4752   *
4753   * @param int    $term_id  Term ID.
4754   * @param string $taxonomy Taxonomy name.
4755   * @return int|false Parent term ID on success, false on failure.
4756   */
4757  function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
4758      $term = get_term( $term_id, $taxonomy );
4759      if ( ! $term || is_wp_error( $term ) ) {
4760          return false;
4761      }
4762      return (int) $term->parent;
4763  }
4764  
4765  /**
4766   * Checks the given subset of the term hierarchy for hierarchy loops.
4767   * Prevents loops from forming and breaks those that it finds.
4768   *
4769   * Attached to the {@see 'wp_update_term_parent'} filter.
4770   *
4771   * @since 3.1.0
4772   *
4773   * @param int    $parent   `term_id` of the parent for the term we're checking.
4774   * @param int    $term_id  The term we're checking.
4775   * @param string $taxonomy The taxonomy of the term we're checking.
4776   * @return int The new parent for the term.
4777   */
4778  function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
4779      // Nothing fancy here - bail.
4780      if ( ! $parent ) {
4781          return 0;
4782      }
4783  
4784      // Can't be its own parent.
4785      if ( $parent === $term_id ) {
4786          return 0;
4787      }
4788  
4789      // Now look for larger loops.
4790      $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) );
4791      if ( ! $loop ) {
4792          return $parent; // No loop.
4793      }
4794  
4795      // Setting $parent to the given value causes a loop.
4796      if ( isset( $loop[ $term_id ] ) ) {
4797          return 0;
4798      }
4799  
4800      // There's a loop, but it doesn't contain $term_id. Break the loop.
4801      foreach ( array_keys( $loop ) as $loop_member ) {
4802          wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
4803      }
4804  
4805      return $parent;
4806  }
4807  
4808  /**
4809   * Determines whether a taxonomy is considered "viewable".
4810   *
4811   * @since 5.1.0
4812   *
4813   * @param string|WP_Taxonomy $taxonomy Taxonomy name or object.
4814   * @return bool Whether the taxonomy should be considered viewable.
4815   */
4816  function is_taxonomy_viewable( $taxonomy ) {
4817      if ( is_scalar( $taxonomy ) ) {
4818          $taxonomy = get_taxonomy( $taxonomy );
4819          if ( ! $taxonomy ) {
4820              return false;
4821          }
4822      }
4823  
4824      return $taxonomy->publicly_queryable;
4825  }
4826  
4827  /**
4828   * Sets the last changed time for the 'terms' cache group.
4829   *
4830   * @since 5.0.0
4831   */
4832  function wp_cache_set_terms_last_changed() {
4833      wp_cache_set( 'last_changed', microtime(), 'terms' );
4834  }
4835  
4836  /**
4837   * Aborts calls to term meta if it is not supported.
4838   *
4839   * @since 5.0.0
4840   *
4841   * @param mixed $check Skip-value for whether to proceed term meta function execution.
4842   * @return mixed Original value of $check, or false if term meta is not supported.
4843   */
4844  function wp_check_term_meta_support_prefilter( $check ) {
4845      if ( get_option( 'db_version' ) < 34370 ) {
4846          return false;
4847      }
4848  
4849      return $check;
4850  }