/wp-content/plugins/buddypress/bp-core/bp-core-taxonomy.php

https://github.com/livinglab/openlab · PHP · 502 lines · 212 code · 65 blank · 225 comment · 29 complexity · 98bc8005d33a0d68cb4d8498ed31f19b MD5 · raw file

  1. <?php
  2. /**
  3. * BuddyPress taxonomy functions.
  4. *
  5. * Most BuddyPress taxonomy functions are wrappers for their WordPress counterparts.
  6. * Because BuddyPress can be activated in various ways in a network environment, we
  7. * must switch to the root blog before using the WP functions.
  8. *
  9. * @package BuddyPress
  10. * @subpackage Core
  11. * @since 2.2.0
  12. */
  13. // Exit if accessed directly.
  14. defined( 'ABSPATH' ) || exit;
  15. /**
  16. * Returns default BuddyPress taxonomies.
  17. *
  18. * @since 7.0.0
  19. *
  20. * @return array The BuddyPress default taxonomies.
  21. */
  22. function bp_get_default_taxonomies() {
  23. $taxonomies = array(
  24. // Member Type.
  25. bp_get_member_type_tax_name() => array(
  26. 'object' => 'user',
  27. 'component' => 'members',
  28. 'args' => bp_get_member_type_tax_args(),
  29. ),
  30. // Email type.
  31. bp_get_email_tax_type() => array(
  32. 'object' => bp_get_email_post_type(),
  33. 'component' => 'core',
  34. 'args' => bp_get_email_tax_type_args(),
  35. ),
  36. );
  37. /**
  38. * This filter should only be used by built-in BuddyPress Components.
  39. *
  40. * @since 7.0.0
  41. *
  42. * @param array $taxonomies The taxonomy arguments used for WordPress registration.
  43. */
  44. return apply_filters( 'bp_get_default_taxonomies', $taxonomies );
  45. }
  46. /**
  47. * Register our default taxonomies.
  48. *
  49. * @since 2.2.0
  50. */
  51. function bp_register_default_taxonomies() {
  52. $taxonomies = bp_get_default_taxonomies();
  53. foreach ( $taxonomies as $taxonomy_name => $taxonomy_params ) {
  54. if ( ! isset( $taxonomy_params['object'] ) || ! isset( $taxonomy_params['args'] ) ) {
  55. continue;
  56. }
  57. register_taxonomy(
  58. $taxonomy_name,
  59. $taxonomy_params['object'],
  60. $taxonomy_params['args']
  61. );
  62. }
  63. }
  64. add_action( 'bp_register_taxonomies', 'bp_register_default_taxonomies' );
  65. /**
  66. * Gets the ID of the site that BP should use for taxonomy term storage.
  67. *
  68. * Defaults to the root blog ID.
  69. *
  70. * @since 2.6.0
  71. *
  72. * @param string $taxonomy Taxonomy slug to check for.
  73. * @return int
  74. */
  75. function bp_get_taxonomy_term_site_id( $taxonomy = '' ) {
  76. $site_id = bp_get_root_blog_id();
  77. /**
  78. * Filters the ID of the site where BP should store taxonomy terms.
  79. *
  80. * @since 2.6.0
  81. *
  82. * @param int $site_id Site ID to cehck for.
  83. * @param string $taxonomy Taxonomy slug to check for.
  84. */
  85. return (int) apply_filters( 'bp_get_taxonomy_term_site_id', $site_id, $taxonomy );
  86. }
  87. /**
  88. * Set taxonomy terms on a BuddyPress object.
  89. *
  90. * @since 2.2.0
  91. *
  92. * @see wp_set_object_terms() for a full description of function and parameters.
  93. *
  94. * @param int $object_id Object ID.
  95. * @param string|array $terms Term or terms to set.
  96. * @param string $taxonomy Taxonomy name.
  97. * @param bool $append Optional. True to append terms to existing terms. Default: false.
  98. * @return array Array of term taxonomy IDs.
  99. */
  100. function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
  101. $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  102. $switched = false;
  103. if ( $site_id !== get_current_blog_id() ) {
  104. switch_to_blog( $site_id );
  105. bp_register_taxonomies();
  106. $switched = true;
  107. }
  108. $tt_ids = wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
  109. if ( $switched ) {
  110. restore_current_blog();
  111. }
  112. /**
  113. * Fires when taxonomy terms have been set on BuddyPress objects.
  114. *
  115. * @since 2.7.0
  116. *
  117. * @param int $object_id Object ID.
  118. * @param array $terms Term or terms to remove.
  119. * @param array $tt_ids Array of term taxonomy IDs.
  120. * @param string $taxonomy Taxonomy name.
  121. */
  122. do_action( 'bp_set_object_terms', $object_id, $terms, $tt_ids, $taxonomy );
  123. return $tt_ids;
  124. }
  125. /**
  126. * Get taxonomy terms for a BuddyPress object.
  127. *
  128. * @since 2.2.0
  129. *
  130. * @see wp_get_object_terms() for a full description of function and parameters.
  131. *
  132. * @param int|array $object_ids ID or IDs of objects.
  133. * @param string|array $taxonomies Name or names of taxonomies to match.
  134. * @param array $args See {@see wp_get_object_terms()}.
  135. * @return array
  136. */
  137. function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) {
  138. // Different taxonomies must be stored on different sites.
  139. $taxonomy_site_map = array();
  140. foreach ( (array) $taxonomies as $taxonomy ) {
  141. $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  142. $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
  143. }
  144. $retval = array();
  145. foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
  146. $switched = false;
  147. if ( $taxonomy_site_id !== get_current_blog_id() ) {
  148. switch_to_blog( $taxonomy_site_id );
  149. bp_register_taxonomies();
  150. $switched = true;
  151. }
  152. $site_terms = wp_get_object_terms( $object_ids, $site_taxonomies, $args );
  153. $retval = array_merge( $retval, $site_terms );
  154. if ( $switched ) {
  155. restore_current_blog();
  156. }
  157. }
  158. return $retval;
  159. }
  160. /**
  161. * Remove taxonomy terms on a BuddyPress object.
  162. *
  163. * @since 2.3.0
  164. *
  165. * @see wp_remove_object_terms() for a full description of function and parameters.
  166. *
  167. * @param int $object_id Object ID.
  168. * @param string|array $terms Term or terms to remove.
  169. * @param string $taxonomy Taxonomy name.
  170. * @return bool|WP_Error True on success, false or WP_Error on failure.
  171. */
  172. function bp_remove_object_terms( $object_id, $terms, $taxonomy ) {
  173. $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  174. $switched = false;
  175. if ( $site_id !== get_current_blog_id() ) {
  176. switch_to_blog( $site_id );
  177. bp_register_taxonomies();
  178. $switched = true;
  179. }
  180. $retval = wp_remove_object_terms( $object_id, $terms, $taxonomy );
  181. if ( $switched ) {
  182. restore_current_blog();
  183. }
  184. /**
  185. * Fires when taxonomy terms have been removed from BuddyPress objects.
  186. *
  187. * @since 2.7.0
  188. *
  189. * @param int $object_id Object ID.
  190. * @param array $terms Term or terms to remove.
  191. * @param string $taxonomy Taxonomy name.
  192. */
  193. do_action( 'bp_remove_object_terms', $object_id, $terms, $taxonomy );
  194. return $retval;
  195. }
  196. /**
  197. * Retrieve IDs of objects in valid taxonomies and terms for BuddyPress-related taxonomies.
  198. *
  199. * Note that object IDs are from the `bp_get_taxonomy_term_site_id()`, which on some
  200. * multisite configurations may not be the same as the current site.
  201. *
  202. * @since 2.7.0
  203. *
  204. * @see get_objects_in_term() for a full description of function and parameters.
  205. *
  206. * @param int|array $term_ids Term id or array of term ids of terms that will be used.
  207. * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
  208. * @param array|string $args Change the order of the object_ids, either ASC or DESC.
  209. *
  210. * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success,
  211. * the array can be empty, meaning that there are no $object_ids found. When
  212. * object IDs are found, an array of those IDs will be returned.
  213. */
  214. function bp_get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
  215. // Different taxonomies may be stored on different sites.
  216. $taxonomy_site_map = array();
  217. foreach ( (array) $taxonomies as $taxonomy ) {
  218. $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  219. $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy;
  220. }
  221. $retval = array();
  222. foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) {
  223. $switched = false;
  224. if ( $taxonomy_site_id !== get_current_blog_id() ) {
  225. switch_to_blog( $taxonomy_site_id );
  226. bp_register_taxonomies();
  227. $switched = true;
  228. }
  229. $site_objects = get_objects_in_term( $term_ids, $site_taxonomies, $args );
  230. $retval = array_merge( $retval, $site_objects );
  231. if ( $switched ) {
  232. restore_current_blog();
  233. }
  234. }
  235. return $retval;
  236. }
  237. /**
  238. * Get term data for terms in BuddyPress taxonomies.
  239. *
  240. * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some
  241. * multisite configurations may not be the same as the current site.
  242. *
  243. * @since 2.7.0
  244. *
  245. * @see get_term_by() for a full description of function and parameters.
  246. *
  247. * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
  248. * @param string|int $value Search for this term value
  249. * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
  250. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  251. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  252. *
  253. * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
  254. * or `$term` was not found.
  255. */
  256. function bp_get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
  257. $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  258. $switched = false;
  259. if ( $site_id !== get_current_blog_id() ) {
  260. switch_to_blog( $site_id );
  261. bp_register_taxonomies();
  262. $switched = true;
  263. }
  264. $term = get_term_by( $field, $value, $taxonomy, $output, $filter );
  265. if ( $switched ) {
  266. restore_current_blog();
  267. }
  268. return $term;
  269. }
  270. /**
  271. * Add a new taxonomy term to the database.
  272. *
  273. * @since 7.0.0
  274. *
  275. * @param string $term The BP term name to add.
  276. * @param string $taxonomy The BP taxonomy to which to add the BP term.
  277. * @param array $args {
  278. * Optional. Array of arguments for inserting a BP term.
  279. * @type string $description The term description. Default empty string.
  280. * @type string $slug The term slug to use. Default empty string.
  281. * @type array $metas The term metas to add. Default empty array.
  282. * }
  283. * @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
  284. * WP_Error otherwise.
  285. */
  286. function bp_insert_term( $term, $taxonomy = '', $args = array() ) {
  287. if ( ! taxonomy_exists( $taxonomy ) ) {
  288. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.', 'buddypress' ) );
  289. }
  290. $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  291. $switched = false;
  292. if ( $site_id !== get_current_blog_id() ) {
  293. switch_to_blog( $site_id );
  294. bp_register_taxonomies();
  295. $switched = true;
  296. }
  297. $term_metas = array();
  298. if ( isset( $args['metas'] ) ) {
  299. $term_metas = (array) $args['metas'];
  300. unset( $args['metas'] );
  301. }
  302. /**
  303. * Fires before a BP Term is added to the database.
  304. *
  305. * @since 7.0.0
  306. *
  307. * @param string $term The BP term name to add.
  308. * @param string $taxonomy The BP taxonomy to which to add the term.
  309. * @param array $args Array of arguments for inserting a BP term.
  310. */
  311. do_action( 'bp_before_insert_term', $term, $taxonomy, $args );
  312. $tt_id = wp_insert_term( $term, $taxonomy, $args );
  313. if ( is_wp_error( $tt_id ) ) {
  314. return $tt_id;
  315. }
  316. $term_id = reset( $tt_id );
  317. if ( $term_metas ) {
  318. bp_update_type_metadata( $term_id, $taxonomy, $term_metas );
  319. }
  320. if ( $switched ) {
  321. restore_current_blog();
  322. }
  323. /**
  324. * Fires when taxonomy terms have been set on BuddyPress objects.
  325. *
  326. * @since 7.0.0
  327. *
  328. * @param array $tt_ids An array containing the `term_id` and `term_taxonomy_id`.
  329. * @param string $taxonomy Taxonomy name.
  330. * @param array $term_metas The term metadata.
  331. */
  332. do_action( 'bp_insert_term', $tt_id, $taxonomy, $term_metas );
  333. return $tt_id;
  334. }
  335. /**
  336. * Get taxonomy BP Terms from the database.
  337. *
  338. * @since 7.0.0
  339. *
  340. * @param array $args {
  341. * Array of arguments to query BP Terms.
  342. * @see `get_terms()` for full description of arguments in case of a member type.
  343. * }
  344. * @return array The list of terms matching arguments.
  345. */
  346. function bp_get_terms( $args = array() ) {
  347. $args = bp_parse_args(
  348. $args,
  349. array(
  350. 'taxonomy' => '',
  351. 'number' => '',
  352. 'hide_empty' => false,
  353. ),
  354. 'get_terms'
  355. );
  356. if ( ! $args['taxonomy'] ) {
  357. return array();
  358. }
  359. $site_id = bp_get_taxonomy_term_site_id( $args['taxonomy'] );
  360. $switched = false;
  361. if ( $site_id !== get_current_blog_id() ) {
  362. switch_to_blog( $site_id );
  363. bp_register_taxonomies();
  364. $switched = true;
  365. }
  366. $terms = get_terms( $args );
  367. if ( $switched ) {
  368. restore_current_blog();
  369. }
  370. /**
  371. * Filter here to modify the BP Terms found into the database.
  372. *
  373. * @since 7.0.0
  374. *
  375. * @param array $terms The list of terms matching arguments.
  376. * @param array $args Array of arguments used to query BP Terms.
  377. */
  378. return apply_filters(
  379. 'bp_get_terms',
  380. $terms,
  381. $args
  382. );
  383. }
  384. /**
  385. * Deletes a BP Term.
  386. *
  387. * @since 7.0.0
  388. *
  389. * @param int $term_id The BP Term ID. Required.
  390. * @param string $taxonomy The BP Taxonomy Name. Required.
  391. * @return bool|WP_Error True on success, WP_Error on failure.
  392. */
  393. function bp_delete_term( $term_id = 0, $taxonomy = '' ) {
  394. if ( ! $term_id || ! $taxonomy ) {
  395. return new WP_Error( 'missing_arguments', __( 'Sorry, the term ID and the taxonomy are required arguments.', 'buddypress' ) );
  396. }
  397. $site_id = bp_get_taxonomy_term_site_id( $taxonomy );
  398. $switched = false;
  399. if ( $site_id !== get_current_blog_id() ) {
  400. switch_to_blog( $site_id );
  401. bp_register_taxonomies();
  402. $switched = true;
  403. }
  404. /**
  405. * Fires before a BP Term is deleted from the database.
  406. *
  407. * @since 7.0.0
  408. *
  409. * @param int $term_id The BP Term ID.
  410. * @param string $taxonomy The BP Taxonomy Name.
  411. */
  412. do_action( 'bp_before_delete_term', $term_id, $taxonomy );
  413. $deleted = wp_delete_term( $term_id, $taxonomy );
  414. if ( $switched ) {
  415. restore_current_blog();
  416. }
  417. if ( is_wp_error( $deleted ) ) {
  418. return $deleted;
  419. }
  420. if ( false === $deleted ) {
  421. return new WP_Error( 'inexistant_term', __( 'Sorry, the term does not exist.', 'buddypress' ) );
  422. }
  423. if ( 0 === $deleted ) {
  424. return new WP_Error( 'default_term', __( 'Sorry, the default term cannot be deleted.', 'buddypress' ) );
  425. }
  426. /**
  427. * Fires once a BP Term has been deleted from the database.
  428. *
  429. * @since 7.0.0
  430. *
  431. * @param boolean $deleted True.
  432. * @param int $term_id The deleted BP Term ID.
  433. * @param string $taxonomy The BP Taxonomy Name of the deleted BP Term ID.
  434. */
  435. do_action( 'bp_delete_term', $deleted, $term_id, $taxonomy );
  436. return $deleted;
  437. }