Skip to:
Content

BuddyPress.org

Ticket #7072: bp-messages-notifications.php

File bp-messages-notifications.php, 8.9 KB (added by harimay, 10 years ago)

After making the filter Proper

Line 
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.
11defined( 'ABSPATH' ) || exit;
12
13/** Email *********************************************************************/
14
15/**
16 * Email message recipients to alert them of a new unread private message.
17 *
18 * @since 1.0.0
19 *
20 * @param array|BP_Messages_Message $raw_args {
21 *     Array of arguments. Also accepts a BP_Messages_Message object.
22 *     @type array  $recipients    User IDs of recipients.
23 *     @type string $email_subject Subject line of message.
24 *     @type string $email_content Content of message.
25 *     @type int    $sender_id     User ID of sender.
26 * }
27 */
28function messages_notification_new_message( $raw_args = array() ) {
29        if ( is_object( $raw_args ) ) {
30                $args = (array) $raw_args;
31        } else {
32                $args = $raw_args;
33        }
34
35        // These should be extracted below.
36        $recipients    = array();
37        $email_subject = $email_content = '';
38        $sender_id     = 0;
39
40        // Barf.
41        extract( $args );
42
43        if ( empty( $recipients ) ) {
44                return;
45        }
46
47        $sender_name = bp_core_get_user_displayname( $sender_id );
48
49        // Send an email to each recipient.
50        foreach ( $recipients as $recipient ) {
51                if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
52                        continue;
53                }
54
55                // User data and links.
56                $ud = get_userdata( $recipient->user_id );
57                if ( empty( $ud ) ) {
58                        continue;
59                }
60
61                $args = array(
62                        'tokens' => array(
63                                'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
64                                'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
65                                'sender.name' => $sender_name,
66                                'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
67                        ),
68                );
69                bp_send_email( 'messages-unread', $ud, $args );
70        }
71
72        /**
73         * Fires after the sending of a new message email notification.
74         *
75         * @since 1.5.0
76         * @deprecated 2.5.0 Use the filters in BP_Email.
77         *                   $email_subject and $email_content arguments unset and deprecated.
78         *
79         * @param array  $recipients    User IDs of recipients.
80         * @param string $email_subject Deprecated in 2.5; now an empty string.
81         * @param string $email_content Deprecated in 2.5; now an empty string.
82         * @param array  $args          Array of originally provided arguments.
83         */
84        do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
85}
86add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
87
88/** Notifications *************************************************************/
89
90/**
91 * Format notifications for the Messages component.
92 *
93 * @since 1.0.0
94 *
95 * @param string $action            The kind of notification being rendered.
96 * @param int    $item_id           The primary item id.
97 * @param int    $secondary_item_id The secondary item id.
98 * @param int    $total_items       The total number of messaging-related notifications
99 *                                  waiting for the user.
100 * @param string $format            Return value format. 'string' for BuddyBar-compatible
101 *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
102 * @return string|array Formatted notifications.
103 */
104function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
105        $total_items = (int) $total_items;
106        $link        = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
107        $title       = __( 'Inbox', 'buddypress' );
108        $amount      = 'single';
109
110        if ( 'new_message' === $action ) {
111                if ( $total_items > 1 ) {
112                        $amount = 'multiple';
113                        $text   = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items );
114                } else {
115                        $amount = 'single';
116
117                        // Get message thread ID.
118                        $message   = new BP_Messages_Message( $item_id );
119                        $thread_id = $message->thread_id;
120                        $link      = ( ! empty( $thread_id ) )
121                                ? bp_get_message_thread_view_link( $thread_id )
122                                : false;
123
124                        if ( ! empty( $secondary_item_id ) ) {
125                                $text = sprintf( __( '%s sent you a new private message', 'buddypress' ), bp_core_get_user_displayname( $secondary_item_id ) );
126                        } else {
127                                $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) );
128                        }
129                }
130        }
131
132        if ( 'string' === $format ) {
133                if ( ! empty( $link ) ) {
134                        $retval = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
135                } else {
136                        $retval = esc_html( $text );
137                }
138
139                /**
140                 * Filters the new message notification text before the notification is created.
141                 *
142                 * This is a dynamic filter. Possible filter names are:
143                 *   - 'bp_messages_multiple_new_message_notification'.
144                 *   - 'bp_messages_single_new_message_notification'.
145                 *
146                 * @param string $retval            Notification text.
147                 * @param int    $total_items       Number of messages referred to by the notification.
148                 * @param string $text              The raw notification test (ie, not wrapped in a link).
149                 * @param int    $item_id           ID of the associated item.
150                 * @param int    $secondary_item_id ID of the secondary associated item.
151                 */
152                $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $retval, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
153        } else {
154                /** This filter is documented in bp-messages/bp-messages-notifications.php */
155                $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
156                        'text' => $text,
157                        'link' => $link
158                ), (int) $total_items, $text, $link, $item_id, $secondary_item_id );
159        }
160
161        /**
162         * Fires right before returning the formatted message notifications.
163         *
164         * @since 1.0.0
165         *
166         * @param string $action            The type of message notification.
167         * @param int    $item_id           The primary item ID.
168         * @param int    $secondary_item_id The secondary item ID.
169         * @param int    $total_items       Total amount of items to format.
170         */
171        do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
172
173        return $return;
174}
175
176/**
177 * Send notifications to message recipients.
178 *
179 * @since 1.9.0
180 *
181 * @param BP_Messages_Message $message Message object.
182 */
183function bp_messages_message_sent_add_notification( $message ) {
184        if ( bp_is_active( 'notifications' ) && ! empty( $message->recipients ) ) {
185                foreach ( (array) $message->recipients as $recipient ) {
186                        bp_notifications_add_notification( array(
187                                'user_id'           => $recipient->user_id,
188                                'item_id'           => $message->id,
189                                'secondary_item_id' => $message->sender_id,
190                                'component_name'    => buddypress()->messages->id,
191                                'component_action'  => 'new_message',
192                                'date_notified'     => bp_core_current_time(),
193                                'is_new'            => 1,
194                        ) );
195                }
196        }
197}
198add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
199
200/**
201 * Mark new message notification when member reads a message thread directly.
202 *
203 * @since 1.9.0
204 */
205function bp_messages_screen_conversation_mark_notifications() {
206        if ( bp_is_active( 'notifications' ) ) {
207                global $thread_template;
208
209                // Get unread PM notifications for the user.
210                $new_pm_notifications = BP_Notifications_Notification::get( array(
211                        'user_id'           => bp_loggedin_user_id(),
212                        'component_name'    => buddypress()->messages->id,
213                        'component_action'  => 'new_message',
214                        'is_new'            => 1,
215                ) );
216                $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
217
218                // No unread PMs, so stop!
219                if ( empty( $unread_message_ids ) ) {
220                        return;
221                }
222
223                // Get the unread message ids for this thread only.
224                $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
225
226                // Mark each notification for each PM message as read.
227                foreach ( $message_ids as $message_id ) {
228                        bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
229                }
230        }
231}
232add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 );
233
234/**
235 * When a message is deleted, delete corresponding notifications.
236 *
237 * @since 2.0.0
238 *
239 * @param int   $thread_id   ID of the thread.
240 * @param array $message_ids IDs of the messages.
241 */
242function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
243        if ( ! bp_is_active( 'notifications' ) ) {
244                return;
245        }
246
247        // For each recipient, delete notifications corresponding to each message.
248        $thread = new BP_Messages_Thread( $thread_id );
249        foreach ( $thread->get_recipients() as $recipient ) {
250                foreach ( $message_ids as $message_id ) {
251                        bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' );
252                }
253        }
254}
255add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 );