PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/taxonomy.php

https://github.com/markjaquith/WordPress
PHP | 4989 lines | 2148 code | 603 blank | 2238 comment | 385 complexity | 6b169e2a6f719e8ee02412b520fe345c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Core Taxonomy API
  4. *
  5. * @package WordPress
  6. * @subpackage Taxonomy
  7. */
  8. //
  9. // Taxonomy registration.
  10. //
  11. /**
  12. * Creates the initial taxonomies.
  13. *
  14. * This function fires twice: in wp-settings.php before plugins are loaded (for
  15. * backward compatibility reasons), and again on the {@see 'init'} action. We must
  16. * avoid registering rewrite rules before the {@see 'init'} action.
  17. *
  18. * @since 2.8.0
  19. * @since 5.9.0 Added `'wp_template_part_area'` taxonomy.
  20. *
  21. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  22. */
  23. function create_initial_taxonomies() {
  24. global $wp_rewrite;
  25. if ( ! did_action( 'init' ) ) {
  26. $rewrite = array(
  27. 'category' => false,
  28. 'post_tag' => false,
  29. 'post_format' => false,
  30. );
  31. } else {
  32. /**
  33. * Filters the post formats rewrite base.
  34. *
  35. * @since 3.1.0
  36. *
  37. * @param string $context Context of the rewrite base. Default 'type'.
  38. */
  39. $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
  40. $rewrite = array(
  41. 'category' => array(
  42. 'hierarchical' => true,
  43. 'slug' => get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category',
  44. 'with_front' => ! get_option( 'category_base' ) || $wp_rewrite->using_index_permalinks(),
  45. 'ep_mask' => EP_CATEGORIES,
  46. ),
  47. 'post_tag' => array(
  48. 'hierarchical' => false,
  49. 'slug' => get_option( 'tag_base' ) ? get_option( 'tag_base' ) : 'tag',
  50. 'with_front' => ! get_option( 'tag_base' ) || $wp_rewrite->using_index_permalinks(),
  51. 'ep_mask' => EP_TAGS,
  52. ),
  53. 'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
  54. );
  55. }
  56. register_taxonomy(
  57. 'category',
  58. 'post',
  59. array(
  60. 'hierarchical' => true,
  61. 'query_var' => 'category_name',
  62. 'rewrite' => $rewrite['category'],
  63. 'public' => true,
  64. 'show_ui' => true,
  65. 'show_admin_column' => true,
  66. '_builtin' => true,
  67. 'capabilities' => array(
  68. 'manage_terms' => 'manage_categories',
  69. 'edit_terms' => 'edit_categories',
  70. 'delete_terms' => 'delete_categories',
  71. 'assign_terms' => 'assign_categories',
  72. ),
  73. 'show_in_rest' => true,
  74. 'rest_base' => 'categories',
  75. 'rest_controller_class' => 'WP_REST_Terms_Controller',
  76. )
  77. );
  78. register_taxonomy(
  79. 'post_tag',
  80. 'post',
  81. array(
  82. 'hierarchical' => false,
  83. 'query_var' => 'tag',
  84. 'rewrite' => $rewrite['post_tag'],
  85. 'public' => true,
  86. 'show_ui' => true,
  87. 'show_admin_column' => true,
  88. '_builtin' => true,
  89. 'capabilities' => array(
  90. 'manage_terms' => 'manage_post_tags',
  91. 'edit_terms' => 'edit_post_tags',
  92. 'delete_terms' => 'delete_post_tags',
  93. 'assign_terms' => 'assign_post_tags',
  94. ),
  95. 'show_in_rest' => true,
  96. 'rest_base' => 'tags',
  97. 'rest_controller_class' => 'WP_REST_Terms_Controller',
  98. )
  99. );
  100. register_taxonomy(
  101. 'nav_menu',
  102. 'nav_menu_item',
  103. array(
  104. 'public' => false,
  105. 'hierarchical' => false,
  106. 'labels' => array(
  107. 'name' => __( 'Navigation Menus' ),
  108. 'singular_name' => __( 'Navigation Menu' ),
  109. ),
  110. 'query_var' => false,
  111. 'rewrite' => false,
  112. 'show_ui' => false,
  113. '_builtin' => true,
  114. 'show_in_nav_menus' => false,
  115. 'capabilities' => array(
  116. 'manage_terms' => 'edit_theme_options',
  117. 'edit_terms' => 'edit_theme_options',
  118. 'delete_terms' => 'edit_theme_options',
  119. 'assign_terms' => 'edit_theme_options',
  120. ),
  121. 'show_in_rest' => true,
  122. 'rest_base' => 'menus',
  123. 'rest_controller_class' => 'WP_REST_Menus_Controller',
  124. )
  125. );
  126. register_taxonomy(
  127. 'link_category',
  128. 'link',
  129. array(
  130. 'hierarchical' => false,
  131. 'labels' => array(
  132. 'name' => __( 'Link Categories' ),
  133. 'singular_name' => __( 'Link Category' ),
  134. 'search_items' => __( 'Search Link Categories' ),
  135. 'popular_items' => null,
  136. 'all_items' => __( 'All Link Categories' ),
  137. 'edit_item' => __( 'Edit Link Category' ),
  138. 'update_item' => __( 'Update Link Category' ),
  139. 'add_new_item' => __( 'Add New Link Category' ),
  140. 'new_item_name' => __( 'New Link Category Name' ),
  141. 'separate_items_with_commas' => null,
  142. 'add_or_remove_items' => null,
  143. 'choose_from_most_used' => null,
  144. 'back_to_items' => __( '&larr; Go to Link Categories' ),
  145. ),
  146. 'capabilities' => array(
  147. 'manage_terms' => 'manage_links',
  148. 'edit_terms' => 'manage_links',
  149. 'delete_terms' => 'manage_links',
  150. 'assign_terms' => 'manage_links',
  151. ),
  152. 'query_var' => false,
  153. 'rewrite' => false,
  154. 'public' => false,
  155. 'show_ui' => true,
  156. '_builtin' => true,
  157. )
  158. );
  159. register_taxonomy(
  160. 'post_format',
  161. 'post',
  162. array(
  163. 'public' => true,
  164. 'hierarchical' => false,
  165. 'labels' => array(
  166. 'name' => _x( 'Formats', 'post format' ),
  167. 'singular_name' => _x( 'Format', 'post format' ),
  168. ),
  169. 'query_var' => true,
  170. 'rewrite' => $rewrite['post_format'],
  171. 'show_ui' => false,
  172. '_builtin' => true,
  173. 'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
  174. )
  175. );
  176. register_taxonomy(
  177. 'wp_theme',
  178. array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
  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. register_taxonomy(
  195. 'wp_template_part_area',
  196. array( 'wp_template_part' ),
  197. array(
  198. 'public' => false,
  199. 'hierarchical' => false,
  200. 'labels' => array(
  201. 'name' => __( 'Template Part Areas' ),
  202. 'singular_name' => __( 'Template Part Area' ),
  203. ),
  204. 'query_var' => false,
  205. 'rewrite' => false,
  206. 'show_ui' => false,
  207. '_builtin' => true,
  208. 'show_in_nav_menus' => false,
  209. 'show_in_rest' => false,
  210. )
  211. );
  212. }
  213. /**
  214. * Retrieves a list of registered taxonomy names or objects.
  215. *
  216. * @since 3.0.0
  217. *
  218. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  219. *
  220. * @param array $args Optional. An array of `key => value` arguments to match against the taxonomy objects.
  221. * Default empty array.
  222. * @param string $output Optional. The type of output to return in the array. Accepts either taxonomy 'names'
  223. * or 'objects'. Default 'names'.
  224. * @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only
  225. * one element from the array needs to match; 'and' means all elements must match.
  226. * Default 'and'.
  227. * @return string[]|WP_Taxonomy[] An array of taxonomy names or objects.
  228. */
  229. function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
  230. global $wp_taxonomies;
  231. $field = ( 'names' === $output ) ? 'name' : false;
  232. return wp_filter_object_list( $wp_taxonomies, $args, $operator, $field );
  233. }
  234. /**
  235. * Return the names or objects of the taxonomies which are registered for the requested object or object type, such as
  236. * a post object or post type name.
  237. *
  238. * Example:
  239. *
  240. * $taxonomies = get_object_taxonomies( 'post' );
  241. *
  242. * This results in:
  243. *
  244. * Array( 'category', 'post_tag' )
  245. *
  246. * @since 2.3.0
  247. *
  248. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  249. *
  250. * @param string|string[]|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
  251. * @param string $output Optional. The type of output to return in the array. Accepts either
  252. * 'names' or 'objects'. Default 'names'.
  253. * @return string[]|WP_Taxonomy[] The names or objects of all taxonomies of `$object_type`.
  254. */
  255. function get_object_taxonomies( $object, $output = 'names' ) {
  256. global $wp_taxonomies;
  257. if ( is_object( $object ) ) {
  258. if ( 'attachment' === $object->post_type ) {
  259. return get_attachment_taxonomies( $object, $output );
  260. }
  261. $object = $object->post_type;
  262. }
  263. $object = (array) $object;
  264. $taxonomies = array();
  265. foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
  266. if ( array_intersect( $object, (array) $tax_obj->object_type ) ) {
  267. if ( 'names' === $output ) {
  268. $taxonomies[] = $tax_name;
  269. } else {
  270. $taxonomies[ $tax_name ] = $tax_obj;
  271. }
  272. }
  273. }
  274. return $taxonomies;
  275. }
  276. /**
  277. * Retrieves the taxonomy object of $taxonomy.
  278. *
  279. * The get_taxonomy function will first check that the parameter string given
  280. * is a taxonomy object and if it is, it will return it.
  281. *
  282. * @since 2.3.0
  283. *
  284. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  285. *
  286. * @param string $taxonomy Name of taxonomy object to return.
  287. * @return WP_Taxonomy|false The Taxonomy Object or false if $taxonomy doesn't exist.
  288. */
  289. function get_taxonomy( $taxonomy ) {
  290. global $wp_taxonomies;
  291. if ( ! taxonomy_exists( $taxonomy ) ) {
  292. return false;
  293. }
  294. return $wp_taxonomies[ $taxonomy ];
  295. }
  296. /**
  297. * Determines whether the taxonomy name exists.
  298. *
  299. * Formerly is_taxonomy(), introduced in 2.3.0.
  300. *
  301. * For more information on this and similar theme functions, check out
  302. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  303. * Conditional Tags} article in the Theme Developer Handbook.
  304. *
  305. * @since 3.0.0
  306. *
  307. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  308. *
  309. * @param string $taxonomy Name of taxonomy object.
  310. * @return bool Whether the taxonomy exists.
  311. */
  312. function taxonomy_exists( $taxonomy ) {
  313. global $wp_taxonomies;
  314. return isset( $wp_taxonomies[ $taxonomy ] );
  315. }
  316. /**
  317. * Determines whether the taxonomy object is hierarchical.
  318. *
  319. * Checks to make sure that the taxonomy is an object first. Then Gets the
  320. * object, and finally returns the hierarchical value in the object.
  321. *
  322. * A false return value might also mean that the taxonomy does not exist.
  323. *
  324. * For more information on this and similar theme functions, check out
  325. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  326. * Conditional Tags} article in the Theme Developer Handbook.
  327. *
  328. * @since 2.3.0
  329. *
  330. * @param string $taxonomy Name of taxonomy object.
  331. * @return bool Whether the taxonomy is hierarchical.
  332. */
  333. function is_taxonomy_hierarchical( $taxonomy ) {
  334. if ( ! taxonomy_exists( $taxonomy ) ) {
  335. return false;
  336. }
  337. $taxonomy = get_taxonomy( $taxonomy );
  338. return $taxonomy->hierarchical;
  339. }
  340. /**
  341. * Creates or modifies a taxonomy object.
  342. *
  343. * Note: Do not use before the {@see 'init'} hook.
  344. *
  345. * A simple function for creating or modifying a taxonomy object based on
  346. * the parameters given. If modifying an existing taxonomy object, note
  347. * that the `$object_type` value from the original registration will be
  348. * overwritten.
  349. *
  350. * @since 2.3.0
  351. * @since 4.2.0 Introduced `show_in_quick_edit` argument.
  352. * @since 4.4.0 The `show_ui` argument is now enforced on the term editing screen.
  353. * @since 4.4.0 The `public` argument now controls whether the taxonomy can be queried on the front end.
  354. * @since 4.5.0 Introduced `publicly_queryable` argument.
  355. * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
  356. * arguments to register the Taxonomy in REST API.
  357. * @since 5.1.0 Introduced `meta_box_sanitize_cb` argument.
  358. * @since 5.4.0 Added the registered taxonomy object as a return value.
  359. * @since 5.5.0 Introduced `default_term` argument.
  360. * @since 5.9.0 Introduced `rest_namespace` argument.
  361. *
  362. * @global WP_Taxonomy[] $wp_taxonomies Registered taxonomies.
  363. *
  364. * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
  365. * @param array|string $object_type Object type or array of object types with which the taxonomy should be associated.
  366. * @param array|string $args {
  367. * Optional. Array or query string of arguments for registering a taxonomy.
  368. *
  369. * @type string[] $labels An array of labels for this taxonomy. By default, Tag labels are
  370. * used for non-hierarchical taxonomies, and Category labels are used
  371. * for hierarchical taxonomies. See accepted values in
  372. * get_taxonomy_labels(). Default empty array.
  373. * @type string $description A short descriptive summary of what the taxonomy is for. Default empty.
  374. * @type bool $public Whether a taxonomy is intended for use publicly either via
  375. * the admin interface or by front-end users. The default settings
  376. * of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus`
  377. * are inherited from `$public`.
  378. * @type bool $publicly_queryable Whether the taxonomy is publicly queryable.
  379. * If not set, the default is inherited from `$public`
  380. * @type bool $hierarchical Whether the taxonomy is hierarchical. Default false.
  381. * @type bool $show_ui Whether to generate and allow a UI for managing terms in this taxonomy in
  382. * the admin. If not set, the default is inherited from `$public`
  383. * (default true).
  384. * @type bool $show_in_menu Whether to show the taxonomy in the admin menu. If true, the taxonomy is
  385. * shown as a submenu of the object type menu. If false, no menu is shown.
  386. * `$show_ui` must be true. If not set, default is inherited from `$show_ui`
  387. * (default true).
  388. * @type bool $show_in_nav_menus Makes this taxonomy available for selection in navigation menus. If not
  389. * set, the default is inherited from `$public` (default true).
  390. * @type bool $show_in_rest Whether to include the taxonomy in the REST API. Set this to true
  391. * for the taxonomy to be available in the block editor.
  392. * @type string $rest_base To change the base url of REST API route. Default is $taxonomy.
  393. * @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2.
  394. * @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
  395. * @type bool $show_tagcloud Whether to list the taxonomy in the Tag Cloud Widget controls. If not set,
  396. * the default is inherited from `$show_ui` (default true).
  397. * @type bool $show_in_quick_edit Whether to show the taxonomy in the quick/bulk edit panel. It not set,
  398. * the default is inherited from `$show_ui` (default true).
  399. * @type bool $show_admin_column Whether to display a column for the taxonomy on its post type listing
  400. * screens. Default false.
  401. * @type bool|callable $meta_box_cb Provide a callback function for the meta box display. If not set,
  402. * post_categories_meta_box() is used for hierarchical taxonomies, and
  403. * post_tags_meta_box() is used for non-hierarchical. If false, no meta
  404. * box is shown.
  405. * @type callable $meta_box_sanitize_cb Callback function for sanitizing taxonomy data saved from a meta
  406. * box. If no callback is defined, an appropriate one is determined
  407. * based on the value of `$meta_box_cb`.
  408. * @type string[] $capabilities {
  409. * Array of capabilities for this taxonomy.
  410. *
  411. * @type string $manage_terms Default 'manage_categories'.
  412. * @type string $edit_terms Default 'manage_categories'.
  413. * @type string $delete_terms Default 'manage_categories'.
  414. * @type string $assign_terms Default 'edit_posts'.
  415. * }
  416. * @type bool|array $rewrite {
  417. * Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent
  418. * rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
  419. *
  420. * @type string $slug Customize the permastruct slug. Default `$taxonomy` key.
  421. * @type bool $with_front Should the permastruct be prepended with WP_Rewrite::$front. Default true.
  422. * @type bool $hierarchical Either hierarchical rewrite tag or not. Default false.
  423. * @type int $ep_mask Assign an endpoint mask. Default `EP_NONE`.
  424. * }
  425. * @type string|bool $query_var Sets the query var key for this taxonomy. Default `$taxonomy` key. If
  426. * false, a taxonomy cannot be loaded at `?{query_var}={term_slug}`. If a
  427. * string, the query `?{query_var}={term_slug}` will be valid.
  428. * @type callable $update_count_callback Works much like a hook, in that it will be called when the count is
  429. * updated. Default _update_post_term_count() for taxonomies attached
  430. * to post types, which confirms that the objects are published before
  431. * counting them. Default _update_generic_term_count() for taxonomies
  432. * attached to other object types, such as users.
  433. * @type string|array $default_term {
  434. * Default term to be used for the taxonomy.
  435. *
  436. * @type string $name Name of default term.
  437. * @type string $slug Slug for default term. Default empty.
  438. * @type string $description Description for default term. Default empty.
  439. * }
  440. * @type bool $sort Whether terms in this taxonomy should be sorted in the order they are
  441. * provided to `wp_set_object_terms()`. Default null which equates to false.
  442. * @type array $args Array of arguments to automatically use inside `wp_get_object_terms()`
  443. * for this taxonomy.
  444. * @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!
  445. * Default false.
  446. * }
  447. * @return WP_Taxonomy|WP_Error The registered taxonomy object on success, WP_Error object on failure.
  448. */
  449. function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
  450. global $wp_taxonomies;
  451. if ( ! is_array( $wp_taxonomies ) ) {
  452. $wp_taxonomies = array();
  453. }
  454. $args = wp_parse_args( $args );
  455. if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
  456. _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2.0' );
  457. return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
  458. }
  459. $taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args );
  460. $taxonomy_object->add_rewrite_rules();
  461. $wp_taxonomies[ $taxonomy ] = $taxonomy_object;
  462. $taxonomy_object->add_hooks();
  463. // Add default term.
  464. if ( ! empty( $taxonomy_object->default_term ) ) {
  465. $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy );
  466. if ( $term ) {
  467. update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
  468. } else {
  469. $term = wp_insert_term(
  470. $taxonomy_object->default_term['name'],
  471. $taxonomy,
  472. array(
  473. 'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ),
  474. 'description' => $taxonomy_object->default_term['description'],
  475. )
  476. );
  477. // Update `term_id` in options.
  478. if ( ! is_wp_error( $term ) ) {
  479. update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
  480. }
  481. }
  482. }
  483. /**
  484. * Fires after a taxonomy is registered.
  485. *
  486. * @since 3.3.0
  487. *
  488. * @param string $taxonomy Taxonomy slug.
  489. * @param array|string $object_type Object type or array of object types.
  490. * @param array $args Array of taxonomy registration arguments.
  491. */
  492. do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object );
  493. return $taxonomy_object;
  494. }
  495. /**
  496. * Unregisters a taxonomy.
  497. *
  498. * Can not be used to unregister built-in taxonomies.
  499. *
  500. * @since 4.5.0
  501. *
  502. * @global WP $wp Current WordPress environment instance.
  503. * @global WP_Taxonomy[] $wp_taxonomies List of taxonomies.
  504. *
  505. * @param string $taxonomy Taxonomy name.
  506. * @return true|WP_Error True on success, WP_Error on failure or if the taxonomy doesn't exist.
  507. */
  508. function unregister_taxonomy( $taxonomy ) {
  509. if ( ! taxonomy_exists( $taxonomy ) ) {
  510. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  511. }
  512. $taxonomy_object = get_taxonomy( $taxonomy );
  513. // Do not allow unregistering internal taxonomies.
  514. if ( $taxonomy_object->_builtin ) {
  515. return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed.' ) );
  516. }
  517. global $wp_taxonomies;
  518. $taxonomy_object->remove_rewrite_rules();
  519. $taxonomy_object->remove_hooks();
  520. // Remove custom taxonomy default term option.
  521. if ( ! empty( $taxonomy_object->default_term ) ) {
  522. delete_option( 'default_term_' . $taxonomy_object->name );
  523. }
  524. // Remove the taxonomy.
  525. unset( $wp_taxonomies[ $taxonomy ] );
  526. /**
  527. * Fires after a taxonomy is unregistered.
  528. *
  529. * @since 4.5.0
  530. *
  531. * @param string $taxonomy Taxonomy name.
  532. */
  533. do_action( 'unregistered_taxonomy', $taxonomy );
  534. return true;
  535. }
  536. /**
  537. * Builds an object with all taxonomy labels out of a taxonomy object.
  538. *
  539. * @since 3.0.0
  540. * @since 4.3.0 Added the `no_terms` label.
  541. * @since 4.4.0 Added the `items_list_navigation` and `items_list` labels.
  542. * @since 4.9.0 Added the `most_used` and `back_to_items` labels.
  543. * @since 5.7.0 Added the `filter_by_item` label.
  544. * @since 5.8.0 Added the `item_link` and `item_link_description` labels.
  545. * @since 5.9.0 Added the `name_field_description`, `slug_field_description`,
  546. * `parent_field_description`, and `desc_field_description` labels.
  547. *
  548. * @param WP_Taxonomy $tax Taxonomy object.
  549. * @return object {
  550. * Taxonomy labels object. The first default value is for non-hierarchical taxonomies
  551. * (like tags) and the second one is for hierarchical taxonomies (like categories).
  552. *
  553. * @type string $name General name for the taxonomy, usually plural. The same
  554. * as and overridden by `$tax->label`. Default 'Tags'/'Categories'.
  555. * @type string $singular_name Name for one object of this taxonomy. Default 'Tag'/'Category'.
  556. * @type string $search_items Default 'Search Tags'/'Search Categories'.
  557. * @type string $popular_items This label is only used for non-hierarchical taxonomies.
  558. * Default 'Popular Tags'.
  559. * @type string $all_items Default 'All Tags'/'All Categories'.
  560. * @type string $parent_item This label is only used for hierarchical taxonomies. Default
  561. * 'Parent Category'.
  562. * @type string $parent_item_colon The same as `parent_item`, but with colon `:` in the end.
  563. * @type string $name_field_description Description for the Name field on Edit Tags screen.
  564. * Default 'The name is how it appears on your site'.
  565. * @type string $slug_field_description Description for the Slug field on Edit Tags screen.
  566. * Default 'The &#8220;slug&#8221; is the URL-friendly version
  567. * of the name. It is usually all lowercase and contains
  568. * only letters, numbers, and hyphens'.
  569. * @type string $parent_field_description Description for the Parent field on Edit Tags screen.
  570. * Default 'Assign a parent term to create a hierarchy.
  571. * The term Jazz, for example, would be the parent
  572. * of Bebop and Big Band'.
  573. * @type string $desc_field_description Description for the Description field on Edit Tags screen.
  574. * Default 'The description is not prominent by default;
  575. * however, some themes may show it'.
  576. * @type string $edit_item Default 'Edit Tag'/'Edit Category'.
  577. * @type string $view_item Default 'View Tag'/'View Category'.
  578. * @type string $update_item Default 'Update Tag'/'Update Category'.
  579. * @type string $add_new_item Default 'Add New Tag'/'Add New Category'.
  580. * @type string $new_item_name Default 'New Tag Name'/'New Category Name'.
  581. * @type string $separate_items_with_commas This label is only used for non-hierarchical taxonomies. Default
  582. * 'Separate tags with commas', used in the meta box.
  583. * @type string $add_or_remove_items This label is only used for non-hierarchical taxonomies. Default
  584. * 'Add or remove tags', used in the meta box when JavaScript
  585. * is disabled.
  586. * @type string $choose_from_most_used This label is only used on non-hierarchical taxonomies. Default
  587. * 'Choose from the most used tags', used in the meta box.
  588. * @type string $not_found Default 'No tags found'/'No categories found', used in
  589. * the meta box and taxonomy list table.
  590. * @type string $no_terms Default 'No tags'/'No categories', used in the posts and media
  591. * list tables.
  592. * @type string $filter_by_item This label is only used for hierarchical taxonomies. Default
  593. * 'Filter by category', used in the posts list table.
  594. * @type string $items_list_navigation Label for the table pagination hidden heading.
  595. * @type string $items_list Label for the table hidden heading.
  596. * @type string $most_used Title for the Most Used tab. Default 'Most Used'.
  597. * @type string $back_to_items Label displayed after a term has been updated.
  598. * @type string $item_link Used in the block editor. Title for a navigation link block variation.
  599. * Default 'Tag Link'/'Category Link'.
  600. * @type string $item_link_description Used in the block editor. Description for a navigation link block
  601. * variation. Default 'A link to a tag'/'A link to a category'.
  602. * }
  603. */
  604. function get_taxonomy_labels( $tax ) {
  605. $tax->labels = (array) $tax->labels;
  606. if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
  607. $tax->labels['separate_items_with_commas'] = $tax->helps;
  608. }
  609. if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
  610. $tax->labels['not_found'] = $tax->no_tagcloud;
  611. }
  612. $name_field_description = __( 'The name is how it appears on your site.' );
  613. $slug_field_description = __( 'The &#8220;slug&#8221; is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.' );
  614. $parent_field_description = __( 'Assign a parent term to create a hierarchy. The term Jazz, for example, would be the parent of Bebop and Big Band.' );
  615. $desc_field_description = __( 'The description is not prominent by default; however, some themes may show it.' );
  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. 'name_field_description' => array( $name_field_description, $name_field_description ),
  625. 'slug_field_description' => array( $slug_field_description, $slug_field_description ),
  626. 'parent_field_description' => array( null, $parent_field_description ),
  627. 'desc_field_description' => array( $desc_field_description, $desc_field_description ),
  628. 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
  629. 'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
  630. 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
  631. 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
  632. 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
  633. 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
  634. 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
  635. 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
  636. 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
  637. 'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ),
  638. 'filter_by_item' => array( null, __( 'Filter by category' ) ),
  639. 'items_list_navigation' => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
  640. 'items_list' => array( __( 'Tags list' ), __( 'Categories list' ) ),
  641. /* translators: Tab heading when selecting from the most used terms. */
  642. 'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
  643. 'back_to_items' => array( __( '&larr; Go to Tags' ), __( '&larr; Go to Categories' ) ),
  644. 'item_link' => array(
  645. _x( 'Tag Link', 'navigation link block title' ),
  646. _x( 'Category Link', 'navigation link block title' ),
  647. ),
  648. 'item_link_description' => array(
  649. _x( 'A link to a tag.', 'navigation link block description' ),
  650. _x( 'A link to a category.', 'navigation link block description' ),
  651. ),
  652. );
  653. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  654. $labels = _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
  655. $taxonomy = $tax->name;
  656. $default_labels = clone $labels;
  657. /**
  658. * Filters the labels of a specific taxonomy.
  659. *
  660. * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
  661. *
  662. * Possible hook names include:
  663. *
  664. * - `taxonomy_labels_category`
  665. * - `taxonomy_labels_post_tag`
  666. *
  667. * @since 4.4.0
  668. *
  669. * @see get_taxonomy_labels() for the full list of taxonomy labels.
  670. *
  671. * @param object $labels Object with labels for the taxonomy as member variables.
  672. */
  673. $labels = apply_filters( "taxonomy_labels_{$taxonomy}", $labels );
  674. // Ensure that the filtered labels contain all required default values.
  675. $labels = (object) array_merge( (array) $default_labels, (array) $labels );
  676. return $labels;
  677. }
  678. /**
  679. * Add an already registered taxonomy to an object type.
  680. *
  681. * @since 3.0.0
  682. *
  683. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  684. *
  685. * @param string $taxonomy Name of taxonomy object.
  686. * @param string $object_type Name of the object type.
  687. * @return bool True if successful, false if not.
  688. */
  689. function register_taxonomy_for_object_type( $taxonomy, $object_type ) {
  690. global $wp_taxonomies;
  691. if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
  692. return false;
  693. }
  694. if ( ! get_post_type_object( $object_type ) ) {
  695. return false;
  696. }
  697. if ( ! in_array( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ) ) {
  698. $wp_taxonomies[ $taxonomy ]->object_type[] = $object_type;
  699. }
  700. // Filter out empties.
  701. $wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type );
  702. /**
  703. * Fires after a taxonomy is registered for an object type.
  704. *
  705. * @since 5.1.0
  706. *
  707. * @param string $taxonomy Taxonomy name.
  708. * @param string $object_type Name of the object type.
  709. */
  710. do_action( 'registered_taxonomy_for_object_type', $taxonomy, $object_type );
  711. return true;
  712. }
  713. /**
  714. * Remove an already registered taxonomy from an object type.
  715. *
  716. * @since 3.7.0
  717. *
  718. * @global WP_Taxonomy[] $wp_taxonomies The registered taxonomies.
  719. *
  720. * @param string $taxonomy Name of taxonomy object.
  721. * @param string $object_type Name of the object type.
  722. * @return bool True if successful, false if not.
  723. */
  724. function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
  725. global $wp_taxonomies;
  726. if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
  727. return false;
  728. }
  729. if ( ! get_post_type_object( $object_type ) ) {
  730. return false;
  731. }
  732. $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
  733. if ( false === $key ) {
  734. return false;
  735. }
  736. unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
  737. /**
  738. * Fires after a taxonomy is unregistered for an object type.
  739. *
  740. * @since 5.1.0
  741. *
  742. * @param string $taxonomy Taxonomy name.
  743. * @param string $object_type Name of the object type.
  744. */
  745. do_action( 'unregistered_taxonomy_for_object_type', $taxonomy, $object_type );
  746. return true;
  747. }
  748. //
  749. // Term API.
  750. //
  751. /**
  752. * Retrieve object IDs of valid taxonomy and term.
  753. *
  754. * The strings of `$taxonomies` must exist before this function will continue.
  755. * On failure of finding a valid taxonomy, it will return a WP_Error.
  756. *
  757. * The `$terms` aren't checked the same as `$taxonomies`, but still need to exist
  758. * for object IDs to be returned.
  759. *
  760. * It is possible to change the order that object IDs are returned by using `$args`
  761. * with either ASC or DESC array. The value should be in the key named 'order'.
  762. *
  763. * @since 2.3.0
  764. *
  765. * @global wpdb $wpdb WordPress database abstraction object.
  766. *
  767. * @param int|int[] $term_ids Term ID or array of term IDs of terms that will be used.
  768. * @param string|string[] $taxonomies String of taxonomy name or Array of string values of taxonomy names.
  769. * @param array|string $args Change the order of the object IDs, either ASC or DESC.
  770. * @return string[]|WP_Error An array of object IDs as numeric strings on success,
  771. * WP_Error if the taxonomy does not exist.
  772. */
  773. function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
  774. global $wpdb;
  775. if ( ! is_array( $term_ids ) ) {
  776. $term_ids = array( $term_ids );
  777. }
  778. if ( ! is_array( $taxonomies ) ) {
  779. $taxonomies = array( $taxonomies );
  780. }
  781. foreach ( (array) $taxonomies as $taxonomy ) {
  782. if ( ! taxonomy_exists( $taxonomy ) ) {
  783. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  784. }
  785. }
  786. $defaults = array( 'order' => 'ASC' );
  787. $args = wp_parse_args( $args, $defaults );
  788. $order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
  789. $term_ids = array_map( 'intval', $term_ids );
  790. $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
  791. $term_ids = "'" . implode( "', '", $term_ids ) . "'";
  792. $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";
  793. $last_changed = wp_cache_get_last_changed( 'terms' );
  794. $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
  795. $cache = wp_cache_get( $cache_key, 'terms' );
  796. if ( false === $cache ) {
  797. $object_ids = $wpdb->get_col( $sql );
  798. wp_cache_set( $cache_key, $object_ids, 'terms' );
  799. } else {
  800. $object_ids = (array) $cache;
  801. }
  802. if ( ! $object_ids ) {
  803. return array();
  804. }
  805. return $object_ids;
  806. }
  807. /**
  808. * Given a taxonomy query, generates SQL to be appended to a main query.
  809. *
  810. * @since 3.1.0
  811. *
  812. * @see WP_Tax_Query
  813. *
  814. * @param array $tax_query A compact tax query
  815. * @param string $primary_table
  816. * @param string $primary_id_column
  817. * @return string[]
  818. */
  819. function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
  820. $tax_query_obj = new WP_Tax_Query( $tax_query );
  821. return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
  822. }
  823. /**
  824. * Get all Term data from database by Term ID.
  825. *
  826. * The usage of the get_term function is to apply filters to a term object. It
  827. * is possible to get a term object from the database before applying the
  828. * filters.
  829. *
  830. * $term ID must be part of $taxonomy, to get from the database. Failure, might
  831. * be able to be captured by the hooks. Failure would be the same value as $wpdb
  832. * returns for the get_row method.
  833. *
  834. * There are two hooks, one is specifically for each term, named 'get_term', and
  835. * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
  836. * term object, and the taxonomy name as parameters. Both hooks are expected to
  837. * return a Term object.
  838. *
  839. * {@see 'get_term'} hook - Takes two parameters the term Object and the taxonomy name.
  840. * Must return term object. Used in get_term() as a catch-all filter for every
  841. * $term.
  842. *
  843. * {@see 'get_$taxonomy'} hook - Takes two parameters the term Object and the taxonomy
  844. * name. Must return term object. $taxonomy will be the taxonomy name, so for
  845. * example, if 'category', it would be 'get_category' as the filter name. Useful
  846. * for custom taxonomies or plugging into default taxonomies.
  847. *
  848. * @todo Better formatting for DocBlock
  849. *
  850. * @since 2.3.0
  851. * @since 4.4.0 Converted to return a WP_Term object if `$output` is `OBJECT`.
  852. * The `$taxonomy` parameter was made optional.
  853. *
  854. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  855. *
  856. * @param int|WP_Term|object $term If integer, term data will be fetched from the database,
  857. * or from the cache if available.
  858. * If stdClass object (as in the results of a database query),
  859. * will apply filters and return a `WP_Term` object with the `$term` data.
  860. * If `WP_Term`, will return `$term`.
  861. * @param string $taxonomy Optional. Taxonomy name that `$term` is part of.
  862. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  863. * correspond to a WP_Term object, an associative array, or a numeric array,
  864. * respectively. Default OBJECT.
  865. * @param string $filter Optional. How to sanitize term fields. Default 'raw'.
  866. * @return WP_Term|array|WP_Error|null WP_Term instance (or array) on success, depending on the `$output` value.
  867. * WP_Error if `$taxonomy` does not exist. Null for miscellaneous failure.
  868. */
  869. function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
  870. if ( empty( $term ) ) {
  871. return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
  872. }
  873. if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
  874. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  875. }
  876. if ( $term instanceof WP_Term ) {
  877. $_term = $term;
  878. } elseif ( is_object( $term ) ) {
  879. if ( empty( $term->filter ) || 'raw' === $term->filter ) {
  880. $_term = sanitize_term( $term, $taxonomy, 'raw' );
  881. $_term = new WP_Term( $_term );
  882. } else {
  883. $_term = WP_Term::get_instance( $term->term_id );
  884. }
  885. } else {
  886. $_term = WP_Term::get_instance( $term, $taxonomy );
  887. }
  888. if ( is_wp_error( $_term ) ) {
  889. return $_term;
  890. } elseif ( ! $_term ) {
  891. return null;
  892. }
  893. // Ensure for filters that this is not empty.
  894. $taxonomy = $_term->taxonomy;
  895. /**
  896. * Filters a taxonomy term object.
  897. *
  898. * The {@see 'get_$taxonomy'} hook is also available for targeting a specific
  899. * taxonomy.
  900. *
  901. * @since 2.3.0
  902. * @since 4.4.0 `$_term` is now a `WP_Term` object.
  903. *
  904. * @param WP_Term $_term Term object.
  905. * @param string $taxonomy The taxonomy slug.
  906. */
  907. $_term = apply_filters( 'get_term', $_term, $taxonomy );
  908. /**
  909. * Filters a taxonomy term object.
  910. *
  911. * The dynamic portion of the hook name, `$taxonomy`, refers
  912. * to the slug of the term's taxonomy.
  913. *
  914. * Possible hook names include:
  915. *
  916. * - `get_category`
  917. * - `get_post_tag`
  918. *
  919. * @since 2.3.0
  920. * @since 4.4.0 `$_term` is now a `WP_Term` object.
  921. *
  922. * @param WP_Term $_term Term object.
  923. * @param string $taxonomy The taxonomy slug.
  924. */
  925. $_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );
  926. // Bail if a filter callback has changed the type of the `$_term` object.
  927. if ( ! ( $_term instanceof WP_Term ) ) {
  928. return $_term;
  929. }
  930. // Sanitize term, according to the specified filter.
  931. $_term->filter( $filter );
  932. if ( ARRAY_A === $output ) {
  933. return $_term->to_array();
  934. } elseif ( ARRAY_N === $output ) {
  935. return array_values( $_term->to_array() );
  936. }
  937. return $_term;
  938. }
  939. /**
  940. * Get all Term data from database by Term field and data.
  941. *
  942. * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
  943. * required.
  944. *
  945. * The default $field is 'id', therefore it is possible to also use null for
  946. * field, but not recommended that you do so.
  947. *
  948. * If $value does not exist, the return value will be false. If $taxonomy exists
  949. * and $field and $value combinations exist, the Term will be returned.
  950. *
  951. * This function will always return the first term that matches the `$field`-
  952. * `$value`-`$taxonomy` combination specified in the parameters. If your query
  953. * is likely to match more than one term (as is likely to be the case when
  954. * `$field` is 'name', for example), consider using get_terms() instead; that
  955. * way, you will get all matching terms, and can provide your own logic for
  956. * deciding which one was intended.
  957. *
  958. * @todo Better formatting for DocBlock.
  959. *
  960. * @since 2.3.0
  961. * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
  962. * a WP_Term object if `$output` is `OBJECT`.
  963. * @since 5.5.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  964. *
  965. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  966. *
  967. * @param string $field Either 'slug', 'name', 'term_id' (or 'id', 'ID'), or 'term_taxonomy_id'.
  968. * @param string|int $value Search for this term value.
  969. * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
  970. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  971. * correspond to a WP_Term object, an associative array, or a numeric array,
  972. * respectively. Default OBJECT.
  973. * @param string $filter Optional. How to sanitize term fields. Default 'raw'.
  974. * @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
  975. * False if `$taxonomy` does not exist or `$term` was not found.
  976. */
  977. function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
  978. // 'term_taxonomy_id' lookups don't require taxonomy checks.
  979. if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
  980. return false;
  981. }
  982. // No need to perform a query for empty 'slug' or 'name'.
  983. if ( 'slug' === $field || 'name' === $field ) {
  984. $value = (string) $value;
  985. if ( 0 === strlen( $value ) ) {
  986. return false;
  987. }
  988. }
  989. if ( 'id' === $field || 'ID' === $field || 'term_id' === $field ) {
  990. $term = get_term( (int) $value, $taxonomy, $output, $filter );
  991. if ( is_wp_error( $term ) || null === $term ) {
  992. $term = false;
  993. }
  994. return $term;
  995. }
  996. $args = array(
  997. 'get' => 'all',
  998. 'number' => 1,
  999. 'taxonomy' => $taxonomy,
  1000. 'update_term_meta_cache' => false,
  1001. 'orderby' => 'none',
  1002. 'suppress_filter' => true,
  1003. );
  1004. switch ( $field ) {
  1005. case 'slug':
  1006. $args['slug'] = $value;
  1007. break;
  1008. case 'name':
  1009. $args['name'] = $value;
  1010. break;
  1011. case 'term_taxonomy_id':
  1012. $args['term_taxonomy_id'] = $value;
  1013. unset( $args['taxonomy'] );
  1014. break;
  1015. default:
  1016. return false;
  1017. }
  1018. $terms = get_terms( $args );
  1019. if ( is_wp_error( $terms ) || empty( $terms ) ) {
  1020. return false;
  1021. }
  1022. $term = array_shift( $terms );
  1023. // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.
  1024. if ( 'term_taxonomy_id' === $field ) {
  1025. $taxonomy = $term->taxonomy;
  1026. }
  1027. return get_term( $term, $taxonomy, $output, $filter );
  1028. }
  1029. /**
  1030. * Merge all term children into a single array of their IDs.
  1031. *
  1032. * This recursive function will merge all of the children of $term into the same
  1033. * array of term IDs. Only useful for taxonomies which are hierarchical.
  1034. *
  1035. * Will return an empty array if $term does not exist in $taxonomy.
  1036. *
  1037. * @since 2.3.0
  1038. *
  1039. * @param int $term_id ID of term to get children.
  1040. * @param string $taxonomy Taxonomy name.
  1041. * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
  1042. */
  1043. function get_term_children( $term_id, $taxonomy ) {
  1044. if ( ! taxonomy_exists( $taxonomy ) ) {
  1045. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  1046. }
  1047. $term_id = (int) $term_id;
  1048. $terms = _get_term_hierarchy( $taxonomy );
  1049. if ( ! isset( $terms[ $term_id ] ) ) {
  1050. return array();
  1051. }
  1052. $children = $terms[ $term_id ];
  1053. foreach ( (array) $terms[ $term_id ] as $child ) {
  1054. if ( $term_id === $child ) {
  1055. continue;
  1056. }
  1057. if ( isset( $terms[ $child ] ) ) {
  1058. $children = array_merge( $children, get_term_children( $child, $taxonomy ) );
  1059. }
  1060. }
  1061. return $children;
  1062. }
  1063. /**
  1064. * Get sanitized Term field.
  1065. *
  1066. * The function is for contextual reasons and for simplicity of usage.
  1067. *
  1068. * @since 2.3.0
  1069. * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
  1070. *
  1071. * @see sanitize_term_field()
  1072. *
  1073. * @param string $field Term field to fetch.
  1074. * @param int|WP_Term $term Term ID or object.
  1075. * @param string $taxonomy Optional. Taxonomy name. Default empty.
  1076. * @param string $context Optional. How to sanitize term fields. Look at sanitize_term_field() for available options.
  1077. * Default 'display'.
  1078. * @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.
  1079. */
  1080. function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
  1081. $term = get_term( $term, $taxonomy );
  1082. if ( is_wp_error( $term ) ) {
  1083. return $term;
  1084. }
  1085. if ( ! is_object( $term ) ) {
  1086. return '';
  1087. }
  1088. if ( ! isset( $term->$field ) ) {
  1089. return '';
  1090. }
  1091. return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
  1092. }
  1093. /**
  1094. * Sanitizes Term for editing.
  1095. *
  1096. * Return value is sanitize_term() and usage is for sanitizing the term for
  1097. * editing. Function is for contextual and simplicity.
  1098. *
  1099. * @since 2.3.0
  1100. *
  1101. * @param int|object $id Term ID or object.
  1102. * @param string $taxonomy Taxonomy name.
  1103. * @return string|int|null|WP_Error Will return empty string if $term is not an object.
  1104. */
  1105. function get_term_to_edit( $id, $taxonomy ) {
  1106. $term = get_term( $id, $taxonomy );
  1107. if ( is_wp_error( $term ) ) {
  1108. return $term;
  1109. }
  1110. if ( ! is_object( $term ) ) {
  1111. return '';
  1112. }
  1113. return sanitize_term( $term, $taxonomy, 'edit' );
  1114. }
  1115. /**
  1116. * Retrieves the terms in a given taxonomy or list of taxonomies.
  1117. *
  1118. * You can fully inject any customizations to the query before it is sent, as
  1119. * well as control the output with a filter.
  1120. *
  1121. * The return type varies depending on the value passed to `$args['fields']`. See
  1122. * WP_Term_Query::get_terms() for details. In all cases, a `WP_Error` object will
  1123. * be returned if an invalid taxonomy is requested.
  1124. *
  1125. * The {@see 'get_terms'} filter will be called when the cache has the term and will
  1126. * pass the found term along with the array of $taxonomies and array of $args.
  1127. * This filter is also called before the array of terms is passed and will pass
  1128. * the array of terms, along with the $taxonomies and $args.
  1129. *
  1130. * The {@see 'list_terms_exclusions'} filter passes the compiled exclusions …

Large files files are truncated, but you can click here to view the full file