/bp-groups/bp-groups-notifications.php

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Notification Functions. 
  4. * 
  5. * These functions handle the recording, deleting and formatting of notifications 
  6. * for the user and for this specific component. 
  7. * 
  8. * @package BuddyPress 
  9. * @subpackage GroupsActivity 
  10. * @since 1.0.0 
  11. */ 
  12.  
  13. // Exit if accessed directly. 
  14. defined( 'ABSPATH' ) || exit; 
  15.  
  16. /** Emails ********************************************************************/ 
  17.  
  18. /** 
  19. * Notify all group members when a group is updated. 
  20. * 
  21. * @since 1.0.0 
  22. * 
  23. * @param int $group_id ID of the group. 
  24. * @param BP_Groups_Group|null $old_group Group before new details were saved. 
  25. */ 
  26. function groups_notification_group_updated( $group_id = 0, $old_group = null ) { 
  27. $group = groups_get_group( $group_id ); 
  28.  
  29. if ( $old_group instanceof BP_Groups_Group ) { 
  30. $changed = array(); 
  31.  
  32. if ( $group->name !== $old_group->name ) { 
  33. $changed[] = sprintf( 
  34. _x( '* Name changed from "%s" to "%s".', 'Group update email text', 'buddypress' ),  
  35. esc_html( $old_group->name ),  
  36. esc_html( $group->name ) 
  37. ); 
  38.  
  39. if ( $group->description !== $old_group->description ) { 
  40. $changed[] = sprintf( 
  41. _x( '* Description changed from "%s" to "%s".', 'Group update email text', 'buddypress' ),  
  42. esc_html( $old_group->description ),  
  43. esc_html( $group->description ) 
  44. ); 
  45.  
  46. /** 
  47. * Filters the bullet points listing updated items in the email notification after a group is updated. 
  48. * 
  49. * @since 2.2.0 
  50. * 
  51. * @param array $changed Array of bullet points. 
  52. */ 
  53. $changed = apply_filters( 'groups_notification_group_update_updated_items', $changed ); 
  54.  
  55. $changed_text = ''; 
  56. if ( ! empty( $changed ) ) { 
  57. $changed_text = implode( "\n", $changed ); 
  58.  
  59. $user_ids = BP_Groups_Member::get_group_member_ids( $group->id ); 
  60. foreach ( (array) $user_ids as $user_id ) { 
  61.  
  62. // Continue if member opted out of receiving this email. 
  63. if ( 'no' === bp_get_user_meta( $user_id, 'notification_groups_group_updated', true ) ) { 
  64. continue; 
  65.  
  66. $unsubscribe_args = array( 
  67. 'user_id' => $user_id,  
  68. 'notification_type' => 'groups-details-updated',  
  69. ); 
  70.  
  71. $args = array( 
  72. 'tokens' => array( 
  73. 'changed_text' => $changed_text,  
  74. 'group' => $group,  
  75. 'group.id' => $group_id,  
  76. 'group.url' => esc_url( bp_get_group_permalink( $group ) ),  
  77. 'group.name' => $group->name,  
  78. 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),  
  79. ),  
  80. ); 
  81. bp_send_email( 'groups-details-updated', (int) $user_id, $args ); 
  82.  
  83. /** 
  84. * Fires after the notification is sent that a group has been updated. 
  85. * 
  86. * See https://buddypress.trac.wordpress.org/ticket/3644 for blank message parameter. 
  87. * 
  88. * @since 1.5.0 
  89. * @since 2.5.0 $subject has been unset and is deprecated. 
  90. * 
  91. * @param array $user_ids Array of user IDs to notify about the update. 
  92. * @param string $subject Deprecated in 2.5; now an empty string. 
  93. * @param string $value Empty string preventing PHP error. 
  94. * @param int $group_id ID of the group that was updated. 
  95. */ 
  96. do_action( 'bp_groups_sent_updated_email', $user_ids, '', '', $group_id ); 
  97.  
  98. /** 
  99. * Notify group admin about new membership request. 
  100. * 
  101. * @since 1.0.0 
  102. * 
  103. * @param int $requesting_user_id ID of the user requesting group membership. 
  104. * @param int $admin_id ID of the group admin. 
  105. * @param int $group_id ID of the group. 
  106. * @param int $membership_id ID of the group membership object. 
  107. */ 
  108. function groups_notification_new_membership_request( $requesting_user_id = 0, $admin_id = 0, $group_id = 0, $membership_id = 0 ) { 
  109.  
  110. // Trigger a BuddyPress Notification. 
  111. if ( bp_is_active( 'notifications' ) ) { 
  112. bp_notifications_add_notification( array( 
  113. 'user_id' => $admin_id,  
  114. 'item_id' => $group_id,  
  115. 'secondary_item_id' => $requesting_user_id,  
  116. 'component_name' => buddypress()->groups->id,  
  117. 'component_action' => 'new_membership_request',  
  118. ) ); 
  119.  
  120. // Bail if member opted out of receiving this email. 
  121. if ( 'no' === bp_get_user_meta( $admin_id, 'notification_groups_membership_request', true ) ) { 
  122. return; 
  123.  
  124. $unsubscribe_args = array( 
  125. 'user_id' => $admin_id,  
  126. 'notification_type' => 'groups-membership-request',  
  127. ); 
  128.  
  129. $group = groups_get_group( $group_id ); 
  130. $args = array( 
  131. 'tokens' => array( 
  132. 'admin.id' => $admin_id,  
  133. 'group' => $group,  
  134. 'group.name' => $group->name,  
  135. 'group.id' => $group_id,  
  136. 'group-requests.url' => esc_url( bp_get_group_permalink( $group ) . 'admin/membership-requests' ),  
  137. 'membership.id' => $membership_id,  
  138. 'profile.url' => esc_url( bp_core_get_user_domain( $requesting_user_id ) ),  
  139. 'requesting-user.id' => $requesting_user_id,  
  140. 'requesting-user.name' => bp_core_get_user_displayname( $requesting_user_id ),  
  141. 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),  
  142. ),  
  143. ); 
  144. bp_send_email( 'groups-membership-request', (int) $admin_id, $args ); 
  145.  
  146. /** 
  147. * Notify member about their group membership request. 
  148. * 
  149. * @since 1.0.0 
  150. * 
  151. * @param int $requesting_user_id ID of the user requesting group membership. 
  152. * @param int $group_id ID of the group. 
  153. * @param bool $accepted Optional. Whether the membership request was accepted. 
  154. * Default: true. 
  155. */ 
  156. function groups_notification_membership_request_completed( $requesting_user_id = 0, $group_id = 0, $accepted = true ) { 
  157.  
  158. // Trigger a BuddyPress Notification. 
  159. if ( bp_is_active( 'notifications' ) ) { 
  160.  
  161. // What type of acknowledgement. 
  162. $type = ! empty( $accepted ) ? 'membership_request_accepted' : 'membership_request_rejected'; 
  163.  
  164. bp_notifications_add_notification( array( 
  165. 'user_id' => $requesting_user_id,  
  166. 'item_id' => $group_id,  
  167. 'component_name' => buddypress()->groups->id,  
  168. 'component_action' => $type,  
  169. ) ); 
  170.  
  171. // Bail if member opted out of receiving this email. 
  172. if ( 'no' === bp_get_user_meta( $requesting_user_id, 'notification_membership_request_completed', true ) ) { 
  173. return; 
  174.  
  175. $group = groups_get_group( $group_id ); 
  176. $args = array( 
  177. 'tokens' => array( 
  178. 'group' => $group,  
  179. 'group.id' => $group_id,  
  180. 'group.name' => $group->name,  
  181. 'group.url' => esc_url( bp_get_group_permalink( $group ) ),  
  182. 'requesting-user.id' => $requesting_user_id,  
  183. ),  
  184. ); 
  185.  
  186. if ( ! empty( $accepted ) ) { 
  187.  
  188. $unsubscribe_args = array( 
  189. 'user_id' => $requesting_user_id,  
  190. 'notification_type' => 'groups-membership-request-accepted',  
  191. ); 
  192.  
  193. $args['tokens']['unsubscribe'] = esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ); 
  194.  
  195. bp_send_email( 'groups-membership-request-accepted', (int) $requesting_user_id, $args ); 
  196.  
  197. } else { 
  198.  
  199. $unsubscribe_args = array( 
  200. 'user_id' => $requesting_user_id,  
  201. 'notification_type' => 'groups-membership-request-rejected',  
  202. ); 
  203.  
  204. $args['tokens']['unsubscribe'] = esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ); 
  205.  
  206. bp_send_email( 'groups-membership-request-rejected', (int) $requesting_user_id, $args ); 
  207. add_action( 'groups_membership_accepted', 'groups_notification_membership_request_completed', 10, 3 ); 
  208. add_action( 'groups_membership_rejected', 'groups_notification_membership_request_completed', 10, 3 ); 
  209.  
  210. /** 
  211. * Notify group member they have been promoted. 
  212. * 
  213. * @since 1.0.0 
  214. * 
  215. * @param int $user_id ID of the user. 
  216. * @param int $group_id ID of the group. 
  217. */ 
  218. function groups_notification_promoted_member( $user_id = 0, $group_id = 0 ) { 
  219.  
  220. // What type of promotion is this? 
  221. if ( groups_is_user_admin( $user_id, $group_id ) ) { 
  222. $promoted_to = __( 'an administrator', 'buddypress' ); 
  223. $type = 'member_promoted_to_admin'; 
  224. } else { 
  225. $promoted_to = __( 'a moderator', 'buddypress' ); 
  226. $type = 'member_promoted_to_mod'; 
  227.  
  228. // Trigger a BuddyPress Notification. 
  229. if ( bp_is_active( 'notifications' ) ) { 
  230. bp_notifications_add_notification( array( 
  231. 'user_id' => $user_id,  
  232. 'item_id' => $group_id,  
  233. 'component_name' => buddypress()->groups->id,  
  234. 'component_action' => $type,  
  235. ) ); 
  236.  
  237. // Bail if admin opted out of receiving this email. 
  238. if ( 'no' === bp_get_user_meta( $user_id, 'notification_groups_admin_promotion', true ) ) { 
  239. return; 
  240.  
  241. $unsubscribe_args = array( 
  242. 'user_id' => $user_id,  
  243. 'notification_type' => 'groups-member-promoted',  
  244. ); 
  245.  
  246. $group = groups_get_group( $group_id ); 
  247. $args = array( 
  248. 'tokens' => array( 
  249. 'group' => $group,  
  250. 'group.id' => $group_id,  
  251. 'group.url' => esc_url( bp_get_group_permalink( $group ) ),  
  252. 'group.name' => $group->name,  
  253. 'promoted_to' => $promoted_to,  
  254. 'user.id' => $user_id,  
  255. 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),  
  256. ),  
  257. ); 
  258. bp_send_email( 'groups-member-promoted', (int) $user_id, $args ); 
  259. add_action( 'groups_promoted_member', 'groups_notification_promoted_member', 10, 2 ); 
  260.  
  261. /** 
  262. * Notify a member they have been invited to a group. 
  263. * 
  264. * @since 1.0.0 
  265. * 
  266. * @param BP_Groups_Group $group Group object. 
  267. * @param BP_Groups_Member $member Member object. 
  268. * @param int $inviter_user_id ID of the user who sent the invite. 
  269. */ 
  270. function groups_notification_group_invites( &$group, &$member, $inviter_user_id ) { 
  271.  
  272. // Bail if member has already been invited. 
  273. if ( ! empty( $member->invite_sent ) ) { 
  274. return; 
  275.  
  276. // @todo $inviter_ud may be used for caching, test without it 
  277. $inviter_ud = bp_core_get_core_userdata( $inviter_user_id ); 
  278. $invited_user_id = $member->user_id; 
  279.  
  280. // Trigger a BuddyPress Notification. 
  281. if ( bp_is_active( 'notifications' ) ) { 
  282. bp_notifications_add_notification( array( 
  283. 'user_id' => $invited_user_id,  
  284. 'item_id' => $group->id,  
  285. 'component_name' => buddypress()->groups->id,  
  286. 'component_action' => 'group_invite',  
  287. ) ); 
  288.  
  289. // Bail if member opted out of receiving this email. 
  290. if ( 'no' === bp_get_user_meta( $invited_user_id, 'notification_groups_invite', true ) ) { 
  291. return; 
  292.  
  293. $invited_link = bp_core_get_user_domain( $invited_user_id ) . bp_get_groups_slug(); 
  294.  
  295. $unsubscribe_args = array( 
  296. 'user_id' => $invited_user_id,  
  297. 'notification_type' => 'groups-invitation',  
  298. ); 
  299.  
  300. $args = array( 
  301. 'tokens' => array( 
  302. 'group' => $group,  
  303. 'group.url' => bp_get_group_permalink( $group ),  
  304. 'group.name' => $group->name,  
  305. 'inviter.name' => bp_core_get_userlink( $inviter_user_id, true, false, true ),  
  306. 'inviter.url' => bp_core_get_user_domain( $inviter_user_id ),  
  307. 'inviter.id' => $inviter_user_id,  
  308. 'invites.url' => esc_url( $invited_link . '/invites/' ),  
  309. 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),  
  310. ),  
  311. ); 
  312. bp_send_email( 'groups-invitation', (int) $invited_user_id, $args ); 
  313.  
  314. /** Notifications *************************************************************/ 
  315.  
  316. /** 
  317. * Format notifications for the Groups component. 
  318. * 
  319. * @since 1.0.0 
  320. * 
  321. * @param string $action The kind of notification being rendered. 
  322. * @param int $item_id The primary item ID. 
  323. * @param int $secondary_item_id The secondary item ID. 
  324. * @param int $total_items The total number of messaging-related notifications 
  325. * waiting for the user. 
  326. * @param string $format 'string' for BuddyBar-compatible notifications; 'array' 
  327. * for WP Toolbar. Default: 'string'. 
  328. * @return string 
  329. */ 
  330. function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { 
  331.  
  332. switch ( $action ) { 
  333. case 'new_membership_request': 
  334. $group_id = $item_id; 
  335. $requesting_user_id = $secondary_item_id; 
  336.  
  337. $group = groups_get_group( $group_id ); 
  338. $group_link = bp_get_group_permalink( $group ); 
  339. $amount = 'single'; 
  340.  
  341. // Set up the string and the filter 
  342. // because different values are passed to the filters,  
  343. // we'll return values inline. 
  344. if ( (int) $total_items > 1 ) { 
  345. $text = sprintf( __( '%1$d new membership requests for the group "%2$s"', 'buddypress' ), (int) $total_items, $group->name ); 
  346. $amount = 'multiple'; 
  347. $notification_link = $group_link . 'admin/membership-requests/?n=1'; 
  348.  
  349. if ( 'string' == $format ) { 
  350.  
  351. /** 
  352. * Filters groups multiple new membership request notification for string format. 
  353. * 
  354. * This is a dynamic filter that is dependent on item count and action. 
  355. * Complete filter - bp_groups_multiple_new_membership_requests_notification. 
  356. * 
  357. * @since 1.0.0 
  358. * 
  359. * @param string $string HTML anchor tag for request. 
  360. * @param string $group_link The permalink for the group. 
  361. * @param int $total_items Total number of membership requests. 
  362. * @param string $group->name Name of the group. 
  363. * @param string $text Notification content. 
  364. * @param string $notification_link The permalink for notification. 
  365. */ 
  366. return apply_filters( 'bp_groups_' . $amount . '_' . $action . 's_notification', '<a href="' . $notification_link . '" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . $text . '</a>', $group_link, $total_items, $group->name, $text, $notification_link ); 
  367. } else { 
  368.  
  369. /** 
  370. * Filters groups multiple new membership request notification for any non-string format. 
  371. * 
  372. * This is a dynamic filter that is dependent on item count and action. 
  373. * Complete filter - bp_groups_multiple_new_membership_requests_notification. 
  374. * 
  375. * @since 1.0.0 
  376. * 
  377. * @param array $array Array holding permalink and content for notification. 
  378. * @param string $group_link The permalink for the group. 
  379. * @param int $total_items Total number of membership requests. 
  380. * @param string $group->name Name of the group. 
  381. * @param string $text Notification content. 
  382. * @param string $notification_link The permalink for notification. 
  383. */ 
  384. return apply_filters( 'bp_groups_' . $amount . '_' . $action . 's_notification', array( 
  385. 'link' => $notification_link,  
  386. 'text' => $text 
  387. ), $group_link, $total_items, $group->name, $text, $notification_link ); 
  388. } else { 
  389. $user_fullname = bp_core_get_user_displayname( $requesting_user_id ); 
  390. $text = sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname ); 
  391. $notification_link = $group_link . 'admin/membership-requests/?n=1'; 
  392.  
  393. if ( 'string' == $format ) { 
  394.  
  395. /** 
  396. * Filters groups single new membership request notification for string format. 
  397. * 
  398. * This is a dynamic filter that is dependent on item count and action. 
  399. * Complete filter - bp_groups_single_new_membership_request_notification. 
  400. * 
  401. * @since 1.0.0 
  402. * 
  403. * @param string $string HTML anchor tag for request. 
  404. * @param string $group_link The permalink for the group. 
  405. * @param string $user_fullname Full name of requesting user. 
  406. * @param string $group->name Name of the group. 
  407. * @param string $text Notification content. 
  408. * @param string $notification_link The permalink for notification. 
  409. */ 
  410. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $user_fullname, $group->name, $text, $notification_link ); 
  411. } else { 
  412.  
  413. /** 
  414. * Filters groups single new membership request notification for any non-string format. 
  415. * 
  416. * This is a dynamic filter that is dependent on item count and action. 
  417. * Complete filter - bp_groups_single_new_membership_request_notification. 
  418. * 
  419. * @since 1.0.0 
  420. * 
  421. * @param array $array Array holding permalink and content for notification. 
  422. * @param string $group_link The permalink for the group. 
  423. * @param string $user_fullname Full name of requesting user. 
  424. * @param string $group->name Name of the group. 
  425. * @param string $text Notification content. 
  426. * @param string $notification_link The permalink for notification. 
  427. */ 
  428. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  429. 'link' => $notification_link,  
  430. 'text' => $text 
  431. ), $group_link, $user_fullname, $group->name, $text, $notification_link ); 
  432.  
  433. break; 
  434.  
  435. case 'membership_request_accepted': 
  436. $group_id = $item_id; 
  437.  
  438. $group = groups_get_group( $group_id ); 
  439. $group_link = bp_get_group_permalink( $group ); 
  440. $amount = 'single'; 
  441.  
  442. if ( (int) $total_items > 1 ) { 
  443. $text = sprintf( __( '%d accepted group membership requests', 'buddypress' ), (int) $total_items, $group->name ); 
  444. $amount = 'multiple'; 
  445. $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1'; 
  446.  
  447. if ( 'string' == $format ) { 
  448.  
  449. /** 
  450. * Filters multiple accepted group membership requests notification for string format. 
  451. * Complete filter - bp_groups_multiple_membership_request_accepted_notification. 
  452. * 
  453. * @since 1.0.0 
  454. * 
  455. * @param string $string HTML anchor tag for notification. 
  456. * @param int $total_items Total number of accepted requests. 
  457. * @param string $group->name Name of the group. 
  458. * @param string $text Notification content. 
  459. * @param string $notification_link The permalink for notification. 
  460. */ 
  461. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name, $text, $notification_link ); 
  462. } else { 
  463.  
  464. /** 
  465. * Filters multiple accepted group membership requests notification for non-string format. 
  466. * Complete filter - bp_groups_multiple_membership_request_accepted_notification. 
  467. * 
  468. * @since 1.0.0 
  469. * 
  470. * @param array $array Array holding permalink and content for notification 
  471. * @param int $total_items Total number of accepted requests. 
  472. * @param string $group->name Name of the group. 
  473. * @param string $text Notification content. 
  474. * @param string $notification_link The permalink for notification. 
  475. */ 
  476. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  477. 'link' => $notification_link,  
  478. 'text' => $text 
  479. ), $total_items, $group->name, $text, $notification_link ); 
  480. } else { 
  481. $text = sprintf( __( 'Membership for group "%s" accepted', 'buddypress' ), $group->name ); 
  482. $filter = 'bp_groups_single_membership_request_accepted_notification'; 
  483. $notification_link = $group_link . '?n=1'; 
  484.  
  485. if ( 'string' == $format ) { 
  486.  
  487. /** 
  488. * Filters single accepted group membership request notification for string format. 
  489. * Complete filter - bp_groups_single_membership_request_accepted_notification. 
  490. * 
  491. * @since 1.0.0 
  492. * 
  493. * @param string $string HTML anchor tag for notification. 
  494. * @param string $group_link The permalink for the group. 
  495. * @param string $group->name Name of the group. 
  496. * @param string $text Notification content. 
  497. * @param string $notification_link The permalink for notification. 
  498. */ 
  499. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link ); 
  500. } else { 
  501.  
  502. /** 
  503. * Filters single accepted group membership request notification for non-string format. 
  504. * Complete filter - bp_groups_single_membership_request_accepted_notification. 
  505. * 
  506. * @since 1.0.0 
  507. * 
  508. * @param array $array Array holding permalink and content for notification. 
  509. * @param string $group_link The permalink for the group. 
  510. * @param string $group->name Name of the group. 
  511. * @param string $text Notification content. 
  512. * @param string $notification_link The permalink for notification. 
  513. */ 
  514. return apply_filters( $filter, array( 
  515. 'link' => $notification_link,  
  516. 'text' => $text 
  517. ), $group_link, $group->name, $text, $notification_link ); 
  518.  
  519. break; 
  520.  
  521. case 'membership_request_rejected': 
  522. $group_id = $item_id; 
  523.  
  524. $group = groups_get_group( $group_id ); 
  525. $group_link = bp_get_group_permalink( $group ); 
  526. $amount = 'single'; 
  527.  
  528. if ( (int) $total_items > 1 ) { 
  529. $text = sprintf( __( '%d rejected group membership requests', 'buddypress' ), (int) $total_items, $group->name ); 
  530. $amount = 'multiple'; 
  531. $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1'; 
  532.  
  533. if ( 'string' == $format ) { 
  534.  
  535. /** 
  536. * Filters multiple rejected group membership requests notification for string format. 
  537. * Complete filter - bp_groups_multiple_membership_request_rejected_notification. 
  538. * 
  539. * @since 1.0.0 
  540. * 
  541. * @param string $string HTML anchor tag for notification. 
  542. * @param int $total_items Total number of rejected requests. 
  543. * @param string $group->name Name of the group. 
  544. * @param string $text Notification content. 
  545. * @param string $notification_link The permalink for notification. 
  546. */ 
  547. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $group->name ); 
  548. } else { 
  549.  
  550. /** 
  551. * Filters multiple rejected group membership requests notification for non-string format. 
  552. * Complete filter - bp_groups_multiple_membership_request_rejected_notification. 
  553. * 
  554. * @since 1.0.0 
  555. * 
  556. * @param array $array Array holding permalink and content for notification. 
  557. * @param int $total_items Total number of rejected requests. 
  558. * @param string $group->name Name of the group. 
  559. * @param string $text Notification content. 
  560. * @param string $notification_link The permalink for notification. 
  561. */ 
  562. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  563. 'link' => $notification_link,  
  564. 'text' => $text 
  565. ), $total_items, $group->name, $text, $notification_link ); 
  566. } else { 
  567. $text = sprintf( __( 'Membership for group "%s" rejected', 'buddypress' ), $group->name ); 
  568. $notification_link = $group_link . '?n=1'; 
  569.  
  570. if ( 'string' == $format ) { 
  571.  
  572. /** 
  573. * Filters single rejected group membership requests notification for string format. 
  574. * Complete filter - bp_groups_single_membership_request_rejected_notification. 
  575. * 
  576. * @since 1.0.0 
  577. * 
  578. * @param string $string HTML anchor tag for notification. 
  579. * @param int $group_link The permalink for the group. 
  580. * @param string $group->name Name of the group. 
  581. * @param string $text Notification content. 
  582. * @param string $notification_link The permalink for notification. 
  583. */ 
  584. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link ); 
  585. } else { 
  586.  
  587. /** 
  588. * Filters single rejected group membership requests notification for non-string format. 
  589. * Complete filter - bp_groups_single_membership_request_rejected_notification. 
  590. * 
  591. * @since 1.0.0 
  592. * 
  593. * @param array $array Array holding permalink and content for notification. 
  594. * @param int $group_link The permalink for the group. 
  595. * @param string $group->name Name of the group. 
  596. * @param string $text Notification content. 
  597. * @param string $notification_link The permalink for notification. 
  598. */ 
  599. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  600. 'link' => $notification_link,  
  601. 'text' => $text 
  602. ), $group_link, $group->name, $text, $notification_link ); 
  603.  
  604. break; 
  605.  
  606. case 'member_promoted_to_admin': 
  607. $group_id = $item_id; 
  608.  
  609. $group = groups_get_group( $group_id ); 
  610. $group_link = bp_get_group_permalink( $group ); 
  611. $amount = 'single'; 
  612.  
  613. if ( (int) $total_items > 1 ) { 
  614. $text = sprintf( __( 'You were promoted to an admin in %d groups', 'buddypress' ), (int) $total_items ); 
  615. $amount = 'multiple'; 
  616. $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1'; 
  617.  
  618. if ( 'string' == $format ) { 
  619. /** 
  620. * Filters multiple promoted to group admin notification for string format. 
  621. * Complete filter - bp_groups_multiple_member_promoted_to_admin_notification. 
  622. * 
  623. * @since 1.0.0 
  624. * 
  625. * @param string $string HTML anchor tag for notification. 
  626. * @param int $total_items Total number of rejected requests. 
  627. * @param string $text Notification content. 
  628. * @param string $notification_link The permalink for notification. 
  629. */ 
  630. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link ); 
  631. } else { 
  632. /** 
  633. * Filters multiple promoted to group admin notification for non-string format. 
  634. * Complete filter - bp_groups_multiple_member_promoted_to_admin_notification. 
  635. * 
  636. * @since 1.0.0 
  637. * 
  638. * @param array $array Array holding permalink and content for notification. 
  639. * @param int $total_items Total number of rejected requests. 
  640. * @param string $text Notification content. 
  641. * @param string $notification_link The permalink for notification. 
  642. */ 
  643. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  644. 'link' => $notification_link,  
  645. 'text' => $text 
  646. ), $total_items, $text, $notification_link ); 
  647. } else { 
  648. $text = sprintf( __( 'You were promoted to an admin in the group "%s"', 'buddypress' ), $group->name ); 
  649. $notification_link = $group_link . '?n=1'; 
  650.  
  651. if ( 'string' == $format ) { 
  652. /** 
  653. * Filters single promoted to group admin notification for non-string format. 
  654. * Complete filter - bp_groups_single_member_promoted_to_admin_notification. 
  655. * 
  656. * @since 1.0.0 
  657. * 
  658. * @param string $string HTML anchor tag for notification. 
  659. * @param int $group_link The permalink for the group. 
  660. * @param string $group->name Name of the group. 
  661. * @param string $text Notification content. 
  662. * @param string $notification_link The permalink for notification. 
  663. */ 
  664. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link ); 
  665. } else { 
  666. /** 
  667. * Filters single promoted to group admin notification for non-string format. 
  668. * Complete filter - bp_groups_single_member_promoted_to_admin_notification. 
  669. * 
  670. * @since 1.0.0 
  671. * 
  672. * @param array $array Array holding permalink and content for notification. 
  673. * @param int $group_link The permalink for the group. 
  674. * @param string $group->name Name of the group. 
  675. * @param string $text Notification content. 
  676. * @param string $notification_link The permalink for notification. 
  677. */ 
  678. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  679. 'link' => $notification_link,  
  680. 'text' => $text 
  681. ), $group_link, $group->name, $text, $notification_link ); 
  682.  
  683. break; 
  684.  
  685. case 'member_promoted_to_mod': 
  686. $group_id = $item_id; 
  687.  
  688. $group = groups_get_group( $group_id ); 
  689. $group_link = bp_get_group_permalink( $group ); 
  690. $amount = 'single'; 
  691.  
  692. if ( (int) $total_items > 1 ) { 
  693. $text = sprintf( __( 'You were promoted to a mod in %d groups', 'buddypress' ), (int) $total_items ); 
  694. $amount = 'multiple'; 
  695. $notification_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . '?n=1'; 
  696.  
  697. if ( 'string' == $format ) { 
  698. /** 
  699. * Filters multiple promoted to group mod notification for string format. 
  700. * Complete filter - bp_groups_multiple_member_promoted_to_mod_notification. 
  701. * 
  702. * @since 1.0.0 
  703. * 
  704. * @param string $string HTML anchor tag for notification. 
  705. * @param int $total_items Total number of rejected requests. 
  706. * @param string $text Notification content. 
  707. * @param string $notification_link The permalink for notification. 
  708. */ 
  709. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '" title="' . __( 'Groups', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link ); 
  710. } else { 
  711. /** 
  712. * Filters multiple promoted to group mod notification for non-string format. 
  713. * Complete filter - bp_groups_multiple_member_promoted_to_mod_notification. 
  714. * 
  715. * @since 1.0.0 
  716. * 
  717. * @param array $array Array holding permalink and content for notification. 
  718. * @param int $total_items Total number of rejected requests. 
  719. * @param string $text Notification content. 
  720. * @param string $notification_link The permalink for notification. 
  721. */ 
  722. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  723. 'link' => $notification_link,  
  724. 'text' => $text 
  725. ), $total_items, $text, $notification_link ); 
  726. } else { 
  727. $text = sprintf( __( 'You were promoted to a mod in the group "%s"', 'buddypress' ), $group->name ); 
  728. $notification_link = $group_link . '?n=1'; 
  729.  
  730. if ( 'string' == $format ) { 
  731. /** 
  732. * Filters single promoted to group mod notification for string format. 
  733. * Complete filter - bp_groups_single_member_promoted_to_mod_notification. 
  734. * 
  735. * @since 1.0.0 
  736. * 
  737. * @param string $string HTML anchor tag for notification. 
  738. * @param int $group_link The permalink for the group. 
  739. * @param string $group->name Name of the group. 
  740. * @param string $text Notification content. 
  741. * @param string $notification_link The permalink for notification. 
  742. */ 
  743. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link ); 
  744. } else { 
  745. /** 
  746. * Filters single promoted to group admin notification for non-string format. 
  747. * Complete filter - bp_groups_single_member_promoted_to_mod_notification. 
  748. * 
  749. * @since 1.0.0 
  750. * 
  751. * @param array $array Array holding permalink and content for notification. 
  752. * @param int $group_link The permalink for the group. 
  753. * @param string $group->name Name of the group. 
  754. * @param string $text Notification content. 
  755. * @param string $notification_link The permalink for notification. 
  756. */ 
  757. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  758. 'link' => $notification_link,  
  759. 'text' => $text 
  760. ), $group_link, $group->name, $text, $notification_link ); 
  761.  
  762. break; 
  763.  
  764. case 'group_invite': 
  765. $group_id = $item_id; 
  766. $group = groups_get_group( $group_id ); 
  767. $group_link = bp_get_group_permalink( $group ); 
  768. $amount = 'single'; 
  769.  
  770. $notification_link = bp_loggedin_user_domain() . bp_get_groups_slug() . '/invites/?n=1'; 
  771.  
  772. if ( (int) $total_items > 1 ) { 
  773. $text = sprintf( __( 'You have %d new group invitations', 'buddypress' ), (int) $total_items ); 
  774. $amount = 'multiple'; 
  775.  
  776. if ( 'string' == $format ) { 
  777. /** 
  778. * Filters multiple group invitation notification for string format. 
  779. * Complete filter - bp_groups_multiple_group_invite_notification. 
  780. * 
  781. * @since 1.0.0 
  782. * 
  783. * @param string $string HTML anchor tag for notification. 
  784. * @param int $total_items Total number of rejected requests. 
  785. * @param string $text Notification content. 
  786. * @param string $notification_link The permalink for notification. 
  787. */ 
  788. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '" title="' . __( 'Group Invites', 'buddypress' ) . '">' . $text . '</a>', $total_items, $text, $notification_link ); 
  789. } else { 
  790. /** 
  791. * Filters multiple group invitation notification for non-string format. 
  792. * Complete filter - bp_groups_multiple_group_invite_notification. 
  793. * 
  794. * @since 1.0.0 
  795. * 
  796. * @param array $array Array holding permalink and content for notification. 
  797. * @param int $total_items Total number of rejected requests. 
  798. * @param string $text Notification content. 
  799. * @param string $notification_link The permalink for notification. 
  800. */ 
  801. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  802. 'link' => $notification_link,  
  803. 'text' => $text 
  804. ), $total_items, $text, $notification_link ); 
  805. } else { 
  806. $text = sprintf( __( 'You have an invitation to the group: %s', 'buddypress' ), $group->name ); 
  807. $filter = 'bp_groups_single_group_invite_notification'; 
  808.  
  809. if ( 'string' == $format ) { 
  810. /** 
  811. * Filters single group invitation notification for string format. 
  812. * Complete filter - bp_groups_single_group_invite_notification. 
  813. * 
  814. * @since 1.0.0 
  815. * 
  816. * @param string $string HTML anchor tag for notification. 
  817. * @param int $group_link The permalink for the group. 
  818. * @param string $group->name Name of the group. 
  819. * @param string $text Notification content. 
  820. * @param string $notification_link The permalink for notification. 
  821. */ 
  822. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', '<a href="' . $notification_link . '">' . $text . '</a>', $group_link, $group->name, $text, $notification_link ); 
  823. } else { 
  824. /** 
  825. * Filters single group invitation notification for non-string format. 
  826. * Complete filter - bp_groups_single_group_invite_notification. 
  827. * 
  828. * @since 1.0.0 
  829. * 
  830. * @param array $array Array holding permalink and content for notification. 
  831. * @param int $group_link The permalink for the group. 
  832. * @param string $group->name Name of the group. 
  833. * @param string $text Notification content. 
  834. * @param string $notification_link The permalink for notification. 
  835. */ 
  836. return apply_filters( 'bp_groups_' . $amount . '_' . $action . '_notification', array( 
  837. 'link' => $notification_link,  
  838. 'text' => $text 
  839. ), $group_link, $group->name, $text, $notification_link ); 
  840.  
  841. break; 
  842.  
  843. default: 
  844.  
  845. /** 
  846. * Filters plugin-added group-related custom component_actions. 
  847. * 
  848. * @since 2.4.0 
  849. * 
  850. * @param string $notification Null value. 
  851. * @param int $item_id The primary item ID. 
  852. * @param int $secondary_item_id The secondary item ID. 
  853. * @param int $total_items The total number of messaging-related notifications 
  854. * waiting for the user. 
  855. * @param string $format 'string' for BuddyBar-compatible notifications; 
  856. * 'array' for WP Toolbar. 
  857. */ 
  858. $custom_action_notification = apply_filters( 'bp_groups_' . $action . '_notification', null, $item_id, $secondary_item_id, $total_items, $format ); 
  859.  
  860. if ( ! is_null( $custom_action_notification ) ) { 
  861. return $custom_action_notification; 
  862.  
  863. break; 
  864.  
  865. /** 
  866. * Fires right before returning the formatted group notifications. 
  867. * 
  868. * @since 1.0.0 
  869. * 
  870. * @param string $action The type of notification being rendered. 
  871. * @param int $item_id The primary item ID. 
  872. * @param int $secondary_item_id The secondary item ID. 
  873. * @param int $total_items Total amount of items to format. 
  874. */ 
  875. do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 
  876.  
  877. return false; 
  878.  
  879. /** 
  880. * Remove all notifications for any member belonging to a specific group. 
  881. * 
  882. * @since 1.9.0 
  883. * 
  884. * @param int $group_id ID of the group. 
  885. */ 
  886. function bp_groups_delete_group_delete_all_notifications( $group_id ) { 
  887. if ( bp_is_active( 'notifications' ) ) { 
  888. bp_notifications_delete_all_notifications_by_type( $group_id, buddypress()->groups->id ); 
  889. add_action( 'groups_delete_group', 'bp_groups_delete_group_delete_all_notifications', 10 ); 
  890.  
  891. /** 
  892. * When a demotion takes place, delete any corresponding promotion notifications. 
  893. * 
  894. * @since 2.0.0 
  895. * 
  896. * @param int $user_id ID of the user. 
  897. * @param int $group_id ID of the group. 
  898. */ 
  899. function bp_groups_delete_promotion_notifications( $user_id = 0, $group_id = 0 ) { 
  900. if ( bp_is_active( 'notifications' ) && ! empty( $group_id ) && ! empty( $user_id ) ) { 
  901. bp_notifications_delete_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'member_promoted_to_admin' ); 
  902. bp_notifications_delete_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'member_promoted_to_mod' ); 
  903. add_action( 'groups_demoted_member', 'bp_groups_delete_promotion_notifications', 10, 2 ); 
  904.  
  905. /** 
  906. * Mark notifications read when a member accepts a group invitation. 
  907. * 
  908. * @since 1.9.0 
  909. * 
  910. * @param int $user_id ID of the user. 
  911. * @param int $group_id ID of the group. 
  912. */ 
  913. function bp_groups_accept_invite_mark_notifications( $user_id, $group_id ) { 
  914. if ( bp_is_active( 'notifications' ) ) { 
  915. bp_notifications_mark_notifications_by_item_id( $user_id, $group_id, buddypress()->groups->id, 'group_invite' ); 
  916. add_action( 'groups_accept_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 ); 
  917. add_action( 'groups_reject_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 ); 
  918. add_action( 'groups_delete_invite', 'bp_groups_accept_invite_mark_notifications', 10, 2 ); 
  919.  
  920. /** 
  921. * Mark notifications read when a member's group membership request is granted. 
  922. * 
  923. * @since 2.8.0 
  924. * 
  925. * @param int $user_id ID of the user. 
  926. * @param int $group_id ID of the group. 
  927. */ 
  928. function bp_groups_accept_request_mark_notifications( $user_id, $group_id ) { 
  929. if ( bp_is_active( 'notifications' ) ) { 
  930. // First null parameter marks read for all admins. 
  931. bp_notifications_mark_notifications_by_item_id( null, $group_id, buddypress()->groups->id, 'new_membership_request', $user_id ); 
  932. add_action( 'groups_membership_accepted', 'bp_groups_accept_request_mark_notifications', 10, 2 ); 
  933. add_action( 'groups_membership_rejected', 'bp_groups_accept_request_mark_notifications', 10, 2 ); 
  934.  
  935. /** 
  936. * Mark notifications read when a member views their group memberships. 
  937. * 
  938. * @since 1.9.0 
  939. */ 
  940. function bp_groups_screen_my_groups_mark_notifications() { 
  941.  
  942. // Delete group request notifications for the user. 
  943. if ( isset( $_GET['n'] ) && bp_is_active( 'notifications' ) ) { 
  944.  
  945. // Get the necessary ID's. 
  946. $group_id = buddypress()->groups->id; 
  947. $user_id = bp_loggedin_user_id(); 
  948.  
  949. // Mark notifications read. 
  950. bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'membership_request_accepted' ); 
  951. bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'membership_request_rejected' ); 
  952. bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'member_promoted_to_mod' ); 
  953. bp_notifications_mark_notifications_by_type( $user_id, $group_id, 'member_promoted_to_admin' ); 
  954. add_action( 'groups_screen_my_groups', 'bp_groups_screen_my_groups_mark_notifications', 10 ); 
  955. add_action( 'groups_screen_group_home', 'bp_groups_screen_my_groups_mark_notifications', 10 ); 
  956.  
  957. /** 
  958. * Mark group invitation notifications read when a member views their invitations. 
  959. * 
  960. * @since 1.9.0 
  961. */ 
  962. function bp_groups_screen_invites_mark_notifications() { 
  963. if ( bp_is_active( 'notifications' ) ) { 
  964. bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->groups->id, 'group_invite' ); 
  965. add_action( 'groups_screen_group_invites', 'bp_groups_screen_invites_mark_notifications', 10 ); 
  966.  
  967. /** 
  968. * Mark group join requests read when an admin or moderator visits the group administration area. 
  969. * 
  970. * @since 1.9.0 
  971. */ 
  972. function bp_groups_screen_group_admin_requests_mark_notifications() { 
  973. if ( bp_is_active( 'notifications' ) ) { 
  974. bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->groups->id, 'new_membership_request' ); 
  975. add_action( 'groups_screen_group_admin_requests', 'bp_groups_screen_group_admin_requests_mark_notifications', 10 ); 
  976.  
  977. /** 
  978. * Delete new group membership notifications when a user is being deleted. 
  979. * 
  980. * @since 1.9.0 
  981. * 
  982. * @param int $user_id ID of the user. 
  983. */ 
  984. function bp_groups_remove_data_for_user_notifications( $user_id ) { 
  985. if ( bp_is_active( 'notifications' ) ) { 
  986. bp_notifications_delete_notifications_from_user( $user_id, buddypress()->groups->id, 'new_membership_request' ); 
  987. add_action( 'groups_remove_data_for_user', 'bp_groups_remove_data_for_user_notifications', 10 ); 
.