/bp-groups/bp-groups-filters.php

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Filters. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage GroupsFilters 
  7. * @since 1.0.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. // Filter bbPress template locations. 
  14. add_filter( 'bp_groups_get_directory_template', 'bp_add_template_locations' ); 
  15. add_filter( 'bp_get_single_group_template', 'bp_add_template_locations' ); 
  16.  
  17. /** Apply WordPress defined filters */ 
  18. add_filter( 'bp_get_group_description', 'wptexturize' ); 
  19. add_filter( 'bp_get_group_description_excerpt', 'wptexturize' ); 
  20. add_filter( 'bp_get_group_name', 'wptexturize' ); 
  21.  
  22. add_filter( 'bp_get_group_description', 'convert_smilies' ); 
  23. add_filter( 'bp_get_group_description_excerpt', 'convert_smilies' ); 
  24.  
  25. add_filter( 'bp_get_group_description', 'convert_chars' ); 
  26. add_filter( 'bp_get_group_description_excerpt', 'convert_chars' ); 
  27. add_filter( 'bp_get_group_name', 'convert_chars' ); 
  28.  
  29. add_filter( 'bp_get_group_description', 'wpautop' ); 
  30. add_filter( 'bp_get_group_description_excerpt', 'wpautop' ); 
  31.  
  32. add_filter( 'bp_get_group_description', 'make_clickable', 9 ); 
  33. add_filter( 'bp_get_group_description_excerpt', 'make_clickable', 9 ); 
  34.  
  35. add_filter( 'bp_get_group_name', 'wp_filter_kses', 1 ); 
  36. add_filter( 'bp_get_group_permalink', 'wp_filter_kses', 1 ); 
  37. add_filter( 'bp_get_group_description', 'bp_groups_filter_kses', 1 ); 
  38. add_filter( 'bp_get_group_description_excerpt', 'wp_filter_kses', 1 ); 
  39. add_filter( 'groups_group_name_before_save', 'wp_filter_kses', 1 ); 
  40. add_filter( 'groups_group_description_before_save', 'wp_filter_kses', 1 ); 
  41.  
  42. add_filter( 'bp_get_group_description', 'stripslashes' ); 
  43. add_filter( 'bp_get_group_description_excerpt', 'stripslashes' ); 
  44. add_filter( 'bp_get_group_name', 'stripslashes' ); 
  45. add_filter( 'bp_get_group_member_name', 'stripslashes' ); 
  46. add_filter( 'bp_get_group_member_link', 'stripslashes' ); 
  47.  
  48. add_filter( 'groups_new_group_forum_desc', 'bp_create_excerpt' ); 
  49.  
  50. add_filter( 'groups_group_name_before_save', 'force_balance_tags' ); 
  51. add_filter( 'groups_group_description_before_save', 'force_balance_tags' ); 
  52.  
  53. // Trim trailing spaces from name and description when saving. 
  54. add_filter( 'groups_group_name_before_save', 'trim' ); 
  55. add_filter( 'groups_group_description_before_save', 'trim' ); 
  56.  
  57. // Support emoji. 
  58. if ( function_exists( 'wp_encode_emoji' ) ) { 
  59. add_filter( 'groups_group_description_before_save', 'wp_encode_emoji' ); 
  60.  
  61. // Escape output of new group creation details. 
  62. add_filter( 'bp_get_new_group_name', 'esc_attr' ); 
  63. add_filter( 'bp_get_new_group_description', 'esc_textarea' ); 
  64.  
  65. // Format numerical output. 
  66. add_filter( 'bp_get_total_group_count', 'bp_core_number_format' ); 
  67. add_filter( 'bp_get_group_total_for_member', 'bp_core_number_format' ); 
  68. add_filter( 'bp_get_group_total_members', 'bp_core_number_format' ); 
  69. add_filter( 'bp_get_total_group_count_for_user', 'bp_core_number_format' ); 
  70.  
  71. // Activity component integration. 
  72. add_filter( 'bp_activity_at_name_do_notifications', 'bp_groups_disable_at_mention_notification_for_non_public_groups', 10, 4 ); 
  73.  
  74. // Default group avatar. 
  75. add_filter( 'bp_core_avatar_default', 'bp_groups_default_avatar', 10, 3 ); 
  76. add_filter( 'bp_core_avatar_default_thumb', 'bp_groups_default_avatar', 10, 3 ); 
  77.  
  78. /** 
  79. * Filter output of Group Description through WordPress's KSES API. 
  80. * 
  81. * @since 1.1.0 
  82. * 
  83. * @param string $content Content to filter. 
  84. * @return string 
  85. */ 
  86. function bp_groups_filter_kses( $content = '' ) { 
  87.  
  88. /** 
  89. * Note that we don't immediately bail if $content is empty. This is because 
  90. * WordPress's KSES API calls several other filters that might be relevant 
  91. * to someone's workflow (like `pre_kses`) 
  92. */ 
  93.  
  94. // Get allowed tags using core WordPress API allowing third party plugins 
  95. // to target the specific `buddypress-groups` context. 
  96. $allowed_tags = wp_kses_allowed_html( 'buddypress-groups' ); 
  97.  
  98. // Add our own tags allowed in group descriptions. 
  99. $allowed_tags['a']['class'] = array(); 
  100. $allowed_tags['img'] = array(); 
  101. $allowed_tags['img']['src'] = array(); 
  102. $allowed_tags['img']['alt'] = array(); 
  103. $allowed_tags['img']['width'] = array(); 
  104. $allowed_tags['img']['height'] = array(); 
  105. $allowed_tags['img']['class'] = array(); 
  106. $allowed_tags['img']['id'] = array(); 
  107. $allowed_tags['code'] = array(); 
  108.  
  109. /** 
  110. * Filters the HTML elements allowed for a given context. 
  111. * 
  112. * @since 1.2.0 
  113. * 
  114. * @param string $allowed_tags Allowed tags, attributes, and/or entities. 
  115. */ 
  116. $tags = apply_filters( 'bp_groups_filter_kses', $allowed_tags ); 
  117.  
  118. // Return KSES'ed content, allowing the above tags. 
  119. return wp_kses( $content, $tags ); 
  120.  
  121. /** Legacy group forums (bbPress 1.x) *****************************************/ 
  122.  
  123. /** 
  124. * Filter bbPress query SQL when on group pages or on forums directory. 
  125. * 
  126. * @since 1.1.0 
  127. */ 
  128. function groups_add_forum_privacy_sql() { 
  129. add_filter( 'get_topics_fields', 'groups_add_forum_fields_sql' ); 
  130. add_filter( 'get_topics_join', 'groups_add_forum_tables_sql' ); 
  131. add_filter( 'get_topics_where', 'groups_add_forum_where_sql' ); 
  132. add_filter( 'bbpress_init', 'groups_add_forum_privacy_sql' ); 
  133.  
  134. /** 
  135. * Add fields to bbPress query for group-specific data. 
  136. * 
  137. * @since 1.1.0 
  138. * 
  139. * @param string $sql SQL statement to amend. 
  140. * @return string 
  141. */ 
  142. function groups_add_forum_fields_sql( $sql = '' ) { 
  143. $sql = 't.*, g.id as object_id, g.name as object_name, g.slug as object_slug'; 
  144. return $sql; 
  145.  
  146. /** 
  147. * Add JOINed tables to bbPress query for group-specific data. 
  148. * 
  149. * @since 1.1.0 
  150. * 
  151. * @param string $sql SQL statement to amend. 
  152. * @return string 
  153. */ 
  154. function groups_add_forum_tables_sql( $sql = '' ) { 
  155. $bp = buddypress(); 
  156.  
  157. $sql .= 'JOIN ' . $bp->groups->table_name . ' AS g LEFT JOIN ' . $bp->groups->table_name_groupmeta . ' AS gm ON g.id = gm.group_id '; 
  158.  
  159. return $sql; 
  160.  
  161. /** 
  162. * Add WHERE clauses to bbPress query for group-specific data and access protection. 
  163. * 
  164. * @since 1.1.0 
  165. * 
  166. * @param string $sql SQL Statement to amend. 
  167. * @return string 
  168. */ 
  169. function groups_add_forum_where_sql( $sql = '' ) { 
  170.  
  171. // Define locale variable. 
  172. $parts = array(); 
  173.  
  174. // Set this for groups. 
  175. $parts['groups'] = "(gm.meta_key = 'forum_id' AND gm.meta_value = t.forum_id)"; 
  176.  
  177. // Restrict to public... 
  178. $parts['private'] = "g.status = 'public'"; 
  179.  
  180. /** 
  181. * ...but do some checks to possibly remove public restriction. 
  182. * 
  183. * Decide if private are visible 
  184. */ 
  185.  
  186. // Are we in our own profile? 
  187. if ( bp_is_my_profile() ) 
  188. unset( $parts['private'] ); 
  189.  
  190. // Are we a super admin? 
  191. elseif ( bp_current_user_can( 'bp_moderate' ) ) 
  192. unset( $parts['private'] ); 
  193.  
  194. // No need to filter on a single item. 
  195. elseif ( bp_is_single_item() ) 
  196. unset( $parts['private'] ); 
  197.  
  198. // Check the SQL filter that was passed. 
  199. if ( !empty( $sql ) ) 
  200. $parts['passed'] = $sql; 
  201.  
  202. // Assemble Voltron. 
  203. $parts_string = implode( ' AND ', $parts ); 
  204.  
  205. $bp = buddypress(); 
  206.  
  207. // Set it to the global filter. 
  208. $bp->groups->filter_sql = $parts_string; 
  209.  
  210. // Return the global filter. 
  211. return $bp->groups->filter_sql; 
  212.  
  213. /** 
  214. * Modify bbPress caps for bp-forums. 
  215. * 
  216. * @since 1.1.0 
  217. * 
  218. * @param bool $value Original value for current_user_can check. 
  219. * @param string $cap Capability checked. 
  220. * @param array $args Arguments for the caps. 
  221. * @return bool 
  222. */ 
  223. function groups_filter_bbpress_caps( $value, $cap, $args ) { 
  224.  
  225. if ( bp_current_user_can( 'bp_moderate' ) ) 
  226. return true; 
  227.  
  228. if ( 'add_tag_to' === $cap ) { 
  229. $bp = buddypress(); 
  230.  
  231. if ( $bp->groups->current_group->user_has_access ) { 
  232. return true; 
  233.  
  234. if ( 'manage_forums' == $cap && is_user_logged_in() ) 
  235. return true; 
  236.  
  237. return $value; 
  238. add_filter( 'bb_current_user_can', 'groups_filter_bbpress_caps', 10, 3 ); 
  239.  
  240. /** 
  241. * Amends the forum directory's "last active" bbPress SQL query to stop it fetching information we aren't going to use. 
  242. * 
  243. * This speeds up the query. 
  244. * 
  245. * @since 1.5.0 
  246. * 
  247. * @see BB_Query::_filter_sql() 
  248. * 
  249. * @param string $sql SQL statement. 
  250. * @return string 
  251. */ 
  252. function groups_filter_forums_root_page_sql( $sql ) { 
  253.  
  254. /** 
  255. * Filters the forum directory's "last active" bbPress SQL query. 
  256. * 
  257. * This filter is used to prevent fetching information that is not used. 
  258. * 
  259. * @since 1.5.0 
  260. * 
  261. * @param string $value SQL string to specify fetching just topic_id. 
  262. */ 
  263. return apply_filters( 'groups_filter_bbpress_root_page_sql', 't.topic_id' ); 
  264. add_filter( 'get_latest_topics_fields', 'groups_filter_forums_root_page_sql' ); 
  265.  
  266. /** 
  267. * Should BuddyPress load the mentions scripts and related assets, including results to prime the 
  268. * mentions suggestions? 
  269. * 
  270. * @since 2.2.0 
  271. * 
  272. * @param bool $load_mentions True to load mentions assets, false otherwise. 
  273. * @param bool $mentions_enabled True if mentions are enabled. 
  274. * @return bool True if mentions scripts should be loaded. 
  275. */ 
  276. function bp_groups_maybe_load_mentions_scripts( $load_mentions, $mentions_enabled ) { 
  277. if ( ! $mentions_enabled ) { 
  278. return $load_mentions; 
  279.  
  280. if ( $load_mentions || bp_is_group_activity() ) { 
  281. return true; 
  282.  
  283. return $load_mentions; 
  284. add_filter( 'bp_activity_maybe_load_mentions_scripts', 'bp_groups_maybe_load_mentions_scripts', 10, 2 ); 
  285.  
  286. /** 
  287. * Disable at-mention notifications for users who are not a member of the non-public group where the activity appears. 
  288. * 
  289. * @since 2.5.0 
  290. * 
  291. * @param bool $send Whether to send the notification. 
  292. * @param array $usernames Array of all usernames being notified. 
  293. * @param int $user_id ID of the user to be notified. 
  294. * @param BP_Activity_Activity $activity Activity object. 
  295. * @return bool 
  296. */ 
  297. function bp_groups_disable_at_mention_notification_for_non_public_groups( $send, $usernames, $user_id, BP_Activity_Activity $activity ) { 
  298. // Skip the check for administrators, who can get notifications from non-public groups. 
  299. if ( user_can( $user_id, 'bp_moderate' ) ) { 
  300. return $send; 
  301.  
  302. if ( 'groups' === $activity->component ) { 
  303. $group = groups_get_group( $activity->item_id ); 
  304. if ( 'public' !== $group->status && ! groups_is_user_member( $user_id, $group->id ) ) { 
  305. $send = false; 
  306.  
  307. return $send; 
  308.  
  309. /** 
  310. * Use the mystery group avatar for groups. 
  311. * 
  312. * @since 2.6.0 
  313. * 
  314. * @param string $avatar Current avatar src. 
  315. * @param array $params Avatar params. 
  316. * @return string 
  317. */ 
  318. function bp_groups_default_avatar( $avatar, $params ) { 
  319. if ( isset( $params['object'] ) && 'group' === $params['object'] ) { 
  320. if ( isset( $params['type'] ) && 'thumb' === $params['type'] ) { 
  321. $file = 'mystery-group-50.png'; 
  322. } else { 
  323. $file = 'mystery-group.png'; 
  324.  
  325. $avatar = buddypress()->plugin_url . "bp-core/images/$file"; 
  326.  
  327. return $avatar; 
.