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