PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/taxonomy.php

http://github.com/markjaquith/WordPress
PHP | 4674 lines | 2031 code | 568 blank | 2075 comment | 368 complexity | 724eb8ef38cf6c2b55603833ed5344e1 MD5 | raw file
Possible License(s): 0BSD

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

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