[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Messages Notifications. 4 * 5 * @package BuddyPress 6 * @subpackage MessagesNotifications 7 * @since 1.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Format notifications for the Messages component. 15 * 16 * @since 1.0.0 17 * 18 * @param string $action The kind of notification being rendered. 19 * @param int $item_id The primary item id. 20 * @param int $secondary_item_id The secondary item id. 21 * @param int $total_items The total number of messaging-related notifications 22 * waiting for the user. 23 * @param string $format Return value format. 'string' for BuddyBar-compatible 24 * notifications; 'array' for WP Toolbar. Default: 'string'. 25 * @return string|array Formatted notifications. 26 */ 27 function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { 28 $total_items = (int) $total_items; 29 $text = ''; 30 $link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' ); 31 $title = __( 'Inbox', 'buddypress' ); 32 $amount = 'single'; 33 34 if ( 'new_message' === $action ) { 35 if ( $total_items > 1 ) { 36 $amount = 'multiple'; 37 $text = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items ); 38 39 } else { 40 // Get message thread ID. 41 $message = new BP_Messages_Message( $item_id ); 42 $thread_id = $message->thread_id; 43 $link = ( ! empty( $thread_id ) ) 44 ? bp_get_message_thread_view_link( $thread_id ) 45 : false; 46 47 if ( ! empty( $secondary_item_id ) ) { 48 $text = sprintf( __( '%s sent you a new private message', 'buddypress' ), bp_core_get_user_displayname( $secondary_item_id ) ); 49 } else { 50 $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) ); 51 } 52 } 53 54 if ( 'string' === $format ) { 55 if ( ! empty( $link ) ) { 56 $return = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>'; 57 } else { 58 $return = esc_html( $text ); 59 } 60 61 /** 62 * Filters the new message notification text before the notification is created. 63 * 64 * This is a dynamic filter. Possible filter names are: 65 * - 'bp_messages_multiple_new_message_notification'. 66 * - 'bp_messages_single_new_message_notification'. 67 * 68 * @param string $return Notification text. 69 * @param int $total_items Number of messages referred to by the notification. 70 * @param string $text The raw notification test (ie, not wrapped in a link). 71 * @param int $item_id ID of the associated item. 72 * @param int $secondary_item_id ID of the secondary associated item. 73 */ 74 $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 75 } else { 76 /** This filter is documented in bp-messages/bp-messages-notifications.php */ 77 $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array( 78 'text' => $text, 79 'link' => $link 80 ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 81 } 82 83 // Custom notification action for the Messages component 84 } else { 85 if ( 'string' === $format ) { 86 $return = $text; 87 } else { 88 $return = array( 89 'text' => $text, 90 'link' => $link 91 ); 92 } 93 94 /** 95 * Backcompat for plugins that used to filter bp_messages_single_new_message_notification 96 * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification' 97 */ 98 if ( has_filter( 'bp_messages_single_new_message_notification' ) ) { 99 if ( 'string' === $format ) { 100 /** This filter is documented in bp-messages/bp-messages-notifications.php */ 101 $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 102 103 // Notice that there are seven parameters instead of six? Ugh... 104 } else { 105 /** This filter is documented in bp-messages/bp-messages-notifications.php */ 106 $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); 107 } 108 } 109 110 /** 111 * Filters the custom action notification before the notification is created. 112 * 113 * This is a dynamic filter based on the message notification action. 114 * 115 * @since 2.6.0 116 * 117 * @param array $value An associative array containing the text and the link of the notification 118 * @param int $item_id ID of the associated item. 119 * @param int $secondary_item_id ID of the secondary associated item. 120 * @param int $total_items Number of messages referred to by the notification. 121 * @param string $format Return value format. 'string' for BuddyBar-compatible 122 * notifications; 'array' for WP Toolbar. Default: 'string'. 123 */ 124 $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format ); 125 } 126 127 /** 128 * Fires right before returning the formatted message notifications. 129 * 130 * @since 1.0.0 131 * 132 * @param string $action The type of message notification. 133 * @param int $item_id The primary item ID. 134 * @param int $secondary_item_id The secondary item ID. 135 * @param int $total_items Total amount of items to format. 136 */ 137 do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 138 139 return $return; 140 } 141 142 /** 143 * Send notifications to message recipients. 144 * 145 * @since 1.9.0 146 * 147 * @param BP_Messages_Message $message Message object. 148 */ 149 function bp_messages_message_sent_add_notification( $message ) { 150 if ( ! empty( $message->recipients ) ) { 151 foreach ( (array) $message->recipients as $recipient ) { 152 bp_notifications_add_notification( array( 153 'user_id' => $recipient->user_id, 154 'item_id' => $message->id, 155 'secondary_item_id' => $message->sender_id, 156 'component_name' => buddypress()->messages->id, 157 'component_action' => 'new_message', 158 'date_notified' => bp_core_current_time(), 159 'is_new' => 1, 160 ) ); 161 } 162 } 163 } 164 add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 ); 165 166 /** 167 * Mark new message notification when member reads a message thread directly. 168 * 169 * @since 1.9.0 170 */ 171 function bp_messages_screen_conversation_mark_notifications() { 172 global $thread_template; 173 174 /* 175 * Only run on the logged-in user's profile. 176 * If an admin visits a thread, it shouldn't change the read status. 177 */ 178 if ( ! bp_is_my_profile() ) { 179 return; 180 } 181 182 // Get unread PM notifications for the user. 183 $new_pm_notifications = BP_Notifications_Notification::get( array( 184 'user_id' => bp_loggedin_user_id(), 185 'component_name' => buddypress()->messages->id, 186 'component_action' => 'new_message', 187 'is_new' => 1, 188 ) ); 189 $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' ); 190 191 // No unread PMs, so stop! 192 if ( empty( $unread_message_ids ) ) { 193 return; 194 } 195 196 // Get the unread message ids for this thread only. 197 $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) ); 198 199 // Mark each notification for each PM message as read. 200 foreach ( $message_ids as $message_id ) { 201 bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' ); 202 } 203 } 204 add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 ); 205 206 /** 207 * Mark new message notification as read when the corresponding message is mark read. 208 * 209 * This callback covers mark-as-read bulk actions. 210 * 211 * @since 3.0.0 212 * 213 * @param int $thread_id ID of the thread being marked as read. 214 */ 215 function bp_messages_mark_notification_on_mark_thread( $thread_id ) { 216 $thread_messages = BP_Messages_Thread::get_messages( $thread_id ); 217 218 foreach ( $thread_messages as $thread_message ) { 219 bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $thread_message->id, buddypress()->messages->id, 'new_message' ); 220 } 221 } 222 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' ); 223 224 /** 225 * When a message is deleted, delete corresponding notifications. 226 * 227 * @since 2.0.0 228 * 229 * @param int $thread_id ID of the thread. 230 * @param array $message_ids IDs of the messages. 231 */ 232 function bp_messages_message_delete_notifications( $thread_id, $message_ids ) { 233 // For each recipient, delete notifications corresponding to each message. 234 $thread = new BP_Messages_Thread( $thread_id ); 235 foreach ( $thread->get_recipients() as $recipient ) { 236 foreach ( $message_ids as $message_id ) { 237 bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' ); 238 } 239 } 240 } 241 add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 ); 242 243 /** 244 * Render the markup for the Messages section of Settings > Notifications. 245 * 246 * @since 1.0.0 247 */ 248 function messages_screen_notification_settings() { 249 250 if ( bp_action_variables() ) { 251 bp_do_404(); 252 return; 253 } 254 255 if ( !$new_messages = bp_get_user_meta( bp_displayed_user_id(), 'notification_messages_new_message', true ) ) { 256 $new_messages = 'yes'; 257 } ?> 258 259 <table class="notification-settings" id="messages-notification-settings"> 260 <thead> 261 <tr> 262 <th class="icon"></th> 263 <th class="title"><?php _e( 'Messages', 'buddypress' ) ?></th> 264 <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th> 265 <th class="no"><?php _e( 'No', 'buddypress' )?></th> 266 </tr> 267 </thead> 268 269 <tbody> 270 <tr id="messages-notification-settings-new-message"> 271 <td></td> 272 <td><?php _e( 'A member sends you a new message', 'buddypress' ) ?></td> 273 <td class="yes"><input type="radio" name="notifications[notification_messages_new_message]" id="notification-messages-new-messages-yes" value="yes" <?php checked( $new_messages, 'yes', true ) ?>/><label for="notification-messages-new-messages-yes" class="bp-screen-reader-text"><?php 274 /* translators: accessibility text */ 275 _e( 'Yes, send email', 'buddypress' ); 276 ?></label></td> 277 <td class="no"><input type="radio" name="notifications[notification_messages_new_message]" id="notification-messages-new-messages-no" value="no" <?php checked( $new_messages, 'no', true ) ?>/><label for="notification-messages-new-messages-no" class="bp-screen-reader-text"><?php 278 /* translators: accessibility text */ 279 _e( 'No, do not send email', 'buddypress' ); 280 ?></label></td> 281 </tr> 282 283 <?php 284 285 /** 286 * Fires inside the closing </tbody> tag for messages screen notification settings. 287 * 288 * @since 1.0.0 289 */ 290 do_action( 'messages_screen_notification_settings' ); ?> 291 </tbody> 292 </table> 293 294 <?php 295 } 296 add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 11 01:01:37 2019 | Cross-referenced by PHPXref 0.7.1 |