/bp-core/bp-core-taxonomy.php

  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.  
  14. // Exit if accessed directly. 
  15. defined( 'ABSPATH' ) || exit; 
  16.  
  17. /** 
  18. * Register our default taxonomies. 
  19. * 
  20. * @since 2.2.0 
  21. */ 
  22. function bp_register_default_taxonomies() { 
  23. // Member Type. 
  24. register_taxonomy( bp_get_member_type_tax_name(), 'user', array( 
  25. 'public' => false,  
  26. ) ); 
  27.  
  28. // Email type. 
  29. register_taxonomy( 
  30. bp_get_email_tax_type(),  
  31. bp_get_email_post_type(),  
  32. apply_filters( 'bp_register_email_tax_type', array( 
  33. 'description' => _x( 'BuddyPress email types', 'email type taxonomy description', 'buddypress' ),  
  34. 'labels' => bp_get_email_tax_type_labels(),  
  35. 'meta_box_cb' => 'bp_email_tax_type_metabox',  
  36. 'public' => false,  
  37. 'query_var' => false,  
  38. 'rewrite' => false,  
  39. 'show_in_menu' => false,  
  40. 'show_tagcloud' => false,  
  41. 'show_ui' => bp_is_root_blog() && bp_current_user_can( 'bp_moderate' ),  
  42. ) ) 
  43. ); 
  44. add_action( 'bp_register_taxonomies', 'bp_register_default_taxonomies' ); 
  45.  
  46. /** 
  47. * Gets the ID of the site that BP should use for taxonomy term storage. 
  48. * 
  49. * Defaults to the root blog ID. 
  50. * 
  51. * @since 2.6.0 
  52. * 
  53. * @param string $taxonomy Taxonomy slug to check for. 
  54. * @return int 
  55. */ 
  56. function bp_get_taxonomy_term_site_id( $taxonomy = '' ) { 
  57. $site_id = bp_get_root_blog_id(); 
  58.  
  59. /** 
  60. * Filters the ID of the site where BP should store taxonomy terms. 
  61. * 
  62. * @since 2.6.0 
  63. * 
  64. * @param int $site_id Site ID to cehck for. 
  65. * @param string $taxonomy Taxonomy slug to check for. 
  66. */ 
  67. return (int) apply_filters( 'bp_get_taxonomy_term_site_id', $site_id, $taxonomy ); 
  68.  
  69. /** 
  70. * Set taxonomy terms on a BuddyPress object. 
  71. * 
  72. * @since 2.2.0 
  73. * 
  74. * @see wp_set_object_terms() for a full description of function and parameters. 
  75. * 
  76. * @param int $object_id Object ID. 
  77. * @param string|array $terms Term or terms to set. 
  78. * @param string $taxonomy Taxonomy name. 
  79. * @param bool $append Optional. True to append terms to existing terms. Default: false. 
  80. * @return array Array of term taxonomy IDs. 
  81. */ 
  82. function bp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 
  83. $site_id = bp_get_taxonomy_term_site_id( $taxonomy ); 
  84.  
  85. $switched = false; 
  86. if ( $site_id !== get_current_blog_id() ) { 
  87. switch_to_blog( $site_id ); 
  88. bp_register_taxonomies(); 
  89. $switched = true; 
  90.  
  91. $tt_ids = wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); 
  92.  
  93. if ( $switched ) { 
  94. restore_current_blog(); 
  95.  
  96. /** 
  97. * Fires when taxonomy terms have been set on BuddyPress objects. 
  98. * 
  99. * @since 2.7.0 
  100. * 
  101. * @param int $object_id Object ID. 
  102. * @param array $terms Term or terms to remove. 
  103. * @param array $tt_ids Array of term taxonomy IDs. 
  104. * @param string $taxonomy Taxonomy name. 
  105. */ 
  106. do_action( 'bp_set_object_terms', $object_id, $terms, $tt_ids, $taxonomy ); 
  107.  
  108. return $tt_ids; 
  109.  
  110. /** 
  111. * Get taxonomy terms for a BuddyPress object. 
  112. * 
  113. * @since 2.2.0 
  114. * 
  115. * @see wp_get_object_terms() for a full description of function and parameters. 
  116. * 
  117. * @param int|array $object_ids ID or IDs of objects. 
  118. * @param string|array $taxonomies Name or names of taxonomies to match. 
  119. * @param array $args See {@see wp_get_object_terms()}. 
  120. * @return array 
  121. */ 
  122. function bp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { 
  123. // Different taxonomies must be stored on different sites. 
  124. $taxonomy_site_map = array(); 
  125. foreach ( (array) $taxonomies as $taxonomy ) { 
  126. $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy ); 
  127. $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy; 
  128.  
  129. $retval = array(); 
  130. foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) { 
  131. $switched = false; 
  132. if ( $taxonomy_site_id !== get_current_blog_id() ) { 
  133. switch_to_blog( $taxonomy_site_id ); 
  134. bp_register_taxonomies(); 
  135. $switched = true; 
  136.  
  137. $site_terms = wp_get_object_terms( $object_ids, $site_taxonomies, $args ); 
  138. $retval = array_merge( $retval, $site_terms ); 
  139.  
  140. if ( $switched ) { 
  141. restore_current_blog(); 
  142.  
  143. return $retval; 
  144.  
  145. /** 
  146. * Remove taxonomy terms on a BuddyPress object. 
  147. * 
  148. * @since 2.3.0 
  149. * 
  150. * @see wp_remove_object_terms() for a full description of function and parameters. 
  151. * 
  152. * @param int $object_id Object ID. 
  153. * @param string|array $terms Term or terms to remove. 
  154. * @param string $taxonomy Taxonomy name. 
  155. * @return bool|WP_Error True on success, false or WP_Error on failure. 
  156. */ 
  157. function bp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
  158. $site_id = bp_get_taxonomy_term_site_id( $taxonomy ); 
  159.  
  160. $switched = false; 
  161. if ( $site_id !== get_current_blog_id() ) { 
  162. switch_to_blog( $site_id ); 
  163. bp_register_taxonomies(); 
  164. $switched = true; 
  165.  
  166. $retval = wp_remove_object_terms( $object_id, $terms, $taxonomy ); 
  167.  
  168. if ( $switched ) { 
  169. restore_current_blog(); 
  170.  
  171. /** 
  172. * Fires when taxonomy terms have been removed from BuddyPress objects. 
  173. * 
  174. * @since 2.7.0 
  175. * 
  176. * @param int $object_id Object ID. 
  177. * @param array $terms Term or terms to remove. 
  178. * @param string $taxonomy Taxonomy name. 
  179. */ 
  180. do_action( 'bp_remove_object_terms', $object_id, $terms, $taxonomy ); 
  181.  
  182. return $retval; 
  183.  
  184. /** 
  185. * Retrieve IDs of objects in valid taxonomies and terms for BuddyPress-related taxonomies. 
  186. * 
  187. * Note that object IDs are from the `bp_get_taxonomy_term_site_id()`, which on some 
  188. * multisite configurations may not be the same as the current site. 
  189. * 
  190. * @since 2.7.0 
  191. * 
  192. * @see get_objects_in_term() for a full description of function and parameters. 
  193. * 
  194. * @param int|array $term_ids Term id or array of term ids of terms that will be used. 
  195. * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names. 
  196. * @param array|string $args Change the order of the object_ids, either ASC or DESC. 
  197. * 
  198. * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success,  
  199. * the array can be empty, meaning that there are no $object_ids found. When 
  200. * object IDs are found, an array of those IDs will be returned. 
  201. */ 
  202. function bp_get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { 
  203. // Different taxonomies may be stored on different sites. 
  204. $taxonomy_site_map = array(); 
  205. foreach ( (array) $taxonomies as $taxonomy ) { 
  206. $taxonomy_site_id = bp_get_taxonomy_term_site_id( $taxonomy ); 
  207. $taxonomy_site_map[ $taxonomy_site_id ][] = $taxonomy; 
  208.  
  209. $retval = array(); 
  210. foreach ( $taxonomy_site_map as $taxonomy_site_id => $site_taxonomies ) { 
  211. $switched = false; 
  212. if ( $taxonomy_site_id !== get_current_blog_id() ) { 
  213. switch_to_blog( $taxonomy_site_id ); 
  214. bp_register_taxonomies(); 
  215. $switched = true; 
  216.  
  217. $site_objects = get_objects_in_term( $term_ids, $site_taxonomies, $args ); 
  218. $retval = array_merge( $retval, $site_objects ); 
  219.  
  220. if ( $switched ) { 
  221. restore_current_blog(); 
  222.  
  223. return $retval; 
  224.  
  225. /** 
  226. * Get term data for terms in BuddyPress taxonomies. 
  227. * 
  228. * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some 
  229. * multisite configurations may not be the same as the current site. 
  230. * 
  231. * @since 2.7.0 
  232. * 
  233. * @see get_term_by() for a full description of function and parameters. 
  234. * 
  235. * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' 
  236. * @param string|int $value Search for this term value 
  237. * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'. 
  238. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 
  239. * @param string $filter Optional, default is raw or no WordPress defined filter will applied. 
  240. * 
  241. * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist 
  242. * or `$term` was not found. 
  243. */ 
  244. function bp_get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 
  245. $site_id = bp_get_taxonomy_term_site_id( $taxonomy ); 
  246.  
  247. $switched = false; 
  248. if ( $site_id !== get_current_blog_id() ) { 
  249. switch_to_blog( $site_id ); 
  250. bp_register_taxonomies(); 
  251. $switched = true; 
  252.  
  253. $term = get_term_by( $field, $value, $taxonomy, $output, $filter ); 
  254.  
  255. if ( $switched ) { 
  256. restore_current_blog(); 
  257.  
  258. return $term; 
.