PluginProbe
bbPress / trunk
bbPress vtrunk
2.6.19 2.6.18 2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 All 74 releases
bbpress / includes / extend / buddypress / notifications.php

notifications.php in bbPress trunk, at includes/extend/buddypress/notifications.php

282 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress BuddyPress Notifications.
5 *
6 * @package bbPress
7 * @subpackage BuddyPress
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 // Hooks
14 add_filter( 'bp_notifications_get_registered_components', 'bbp_filter_notifications_get_registered_components', 10 );
15 add_filter( 'bp_notifications_get_notifications_for_user', 'bbp_format_buddypress_notifications', 10, 8 );
16 add_action( 'bbp_new_reply', 'bbp_buddypress_add_notification', 10, 7 );
17 add_action( 'bbp_get_request', 'bbp_buddypress_mark_notifications', 1 );
18
19 /** BuddyPress Helpers ********************************************************/
20
21 /**
22 * Filter registered notifications components, and add 'forums' to the queried
23 * 'component_name' array.
24 *
25 * @since 2.6.0 bbPress (r5232)
26 *
27 * @see BP_Notifications_Notification::get()
28 * @param array $component_names
29 * @return array
30 */
31 function bbp_filter_notifications_get_registered_components( $component_names = array() ) {
32
33 // Force $component_names to be an array
34 if ( ! is_array( $component_names ) ) {
35 $component_names = array();
36 }
37
38 // Add 'forums' component to registered components array
39 array_push( $component_names, bbp_get_component_name() );
40
41 // Return component's with 'forums' appended
42 return $component_names;
43 }
44
45 /**
46 * Format the BuddyBar/Toolbar notifications.
47 *
48 * @since 2.5.0 bbPress (r5155)
49 *
50 * @package bbPress
51 *
52 * @param string $content Component action. Deprecated. Do not do checks against this! Use
53 * the 6th parameter instead - $component_action_name.
54 * @param int $item_id Notification item ID.
55 * @param int $secondary_item_id Notification secondary item ID.
56 * @param int $action_item_count Number of notifications with the same action.
57 * @param string $format Format of return. Either 'string' or 'object'.
58 * @param string $component_action_name Canonical notification action.
59 * @param string $component_name Notification component ID.
60 * @param int $id Notification ID.
61 */
62 function bbp_format_buddypress_notifications( $content, $item_id, $secondary_item_id, $action_item_count, $format, $component_action_name, $component_name, $id ) {
63
64 // Bail if not the notification action we are looking for
65 if ( 0 !== strpos( $component_action_name, 'bbp_new_reply' ) ) {
66 return $content;
67 }
68
69 // New reply notifications
70 $topic_id = bbp_get_reply_topic_id( $item_id );
71 $topic_title = bbp_get_topic_title( $topic_id );
72 $topic_link = wp_nonce_url(
73 add_query_arg(
74 array(
75 'action' => 'bbp_mark_read',
76 'topic_id' => $topic_id
77 ),
78 bbp_get_reply_url( $item_id )
79 ),
80 'bbp_mark_topic_' . $topic_id
81 );
82
83 // Cast to int
84 $action_item_count = (int) $action_item_count;
85
86 // Multiple replies
87 if ( $action_item_count > 1 ) {
88 $filter = 'bbp_multiple_new_subscription_notification';
89 $text = sprintf(
90 /* translators: 1: Number of replies, 2: Topic title */
91 esc_html__( 'You have %1$d new replies to %2$s', 'bbpress' ),
92 $action_item_count,
93 $topic_title
94 );
95
96 // Single reply
97 } else {
98 $filter = 'bbp_single_new_subscription_notification';
99
100 if ( ! empty( $secondary_item_id ) ) {
101 $text = sprintf(
102 /* translators: 1: Number of replies, 2: Topic title, 3: Reply author name */
103 esc_html__( 'You have %1$d new reply to %2$s from %3$s', 'bbpress' ),
104 $action_item_count,
105 $topic_title,
106 bp_core_get_user_displayname( $secondary_item_id )
107 );
108 } else {
109 $text = sprintf(
110 /* translators: 1: Number of replies, 2: Topic title */
111 esc_html__( 'You have %1$d new reply to %2$s', 'bbpress' ),
112 $action_item_count,
113 $topic_title
114 );
115 }
116 }
117
118 // WordPress Toolbar
119 if ( 'string' === $format ) {
120 $value = '<a href="' . esc_url( $topic_link ) . '" title="' . esc_attr__( 'Topic Replies', 'bbpress' ) . '">' . esc_html( $text ) . '</a>';
121
122 // Deprecated BuddyBar
123 } else {
124 $value = array(
125 'text' => $text,
126 'link' => $topic_link
127 );
128 }
129
130 // Filter the return value
131 $retval = apply_filters( $filter, $value, $action_item_count, $text, $topic_link );
132
133 do_action( 'bbp_format_buddypress_notifications', $component_action_name, $item_id, $secondary_item_id, $action_item_count, $format, $component_action_name, $component_name, $id );
134
135 // Return
136 return $retval;
137 }
138
139 /**
140 * Hooked into the new reply function, this notification action is responsible
141 * for notifying topic and hierarchical reply authors of topic replies.
142 *
143 * @since 2.5.0 bbPress (r5155)
144 *
145 * @param int $reply_id
146 * @param int $topic_id
147 * @param int $forum_id (not used)
148 * @param array $anonymous_data (not used)
149 * @param int $author_id
150 * @param bool $is_edit Used to bail if this gets hooked to an edit action
151 * @param int $reply_to
152 */
153 function bbp_buddypress_add_notification( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $author_id = 0, $is_edit = false, $reply_to = 0 ) {
154
155 // Bail if somehow this is hooked to an edit action
156 if ( ! empty( $is_edit ) ) {
157 return;
158 }
159
160 // Bail if the reply is not published
161 if ( ! bbp_is_reply_published( $reply_id ) ) {
162 return;
163 }
164
165 // Get author information
166 $topic_author_id = bbp_get_topic_author_id( $topic_id );
167 $secondary_item_id = $author_id;
168
169 // Hierarchical replies
170 if ( ! empty( $reply_to ) ) {
171 $reply_to_item_id = bbp_get_topic_author_id( $reply_to );
172 }
173
174 // Get some reply information
175 $args = array(
176 'user_id' => $topic_author_id,
177 'item_id' => $reply_id,
178 'component_name' => bbp_get_component_name(),
179 'component_action' => 'bbp_new_reply_' . $topic_id,
180 'date_notified' => get_post( $reply_id )->post_date,
181 );
182
183 // Notify the topic author if not the current reply author
184 if ( $author_id !== $topic_author_id ) {
185 $args['secondary_item_id'] = $secondary_item_id;
186
187 bp_notifications_add_notification( $args );
188 }
189
190 // Notify the immediate reply author if not the current reply author
191 if ( ! empty( $reply_to ) && ( $author_id !== $reply_to_item_id ) ) {
192 $args['user_id'] = $reply_to_item_id;
193 $args['secondary_item_id'] = $topic_author_id;
194
195 bp_notifications_add_notification( $args );
196 }
197 }
198
199 /**
200 * Mark notifications as read when reading a topic.
201 *
202 * @since 2.5.0 bbPress (r5155)
203 *
204 * @return If not trying to mark a notification as read.
205 */
206 function bbp_buddypress_mark_notifications( $action = '' ) {
207
208 // Bail if no topic ID is passed
209 if ( empty( $_GET['topic_id'] ) ) {
210 return;
211 }
212
213 // Bail if action is not for this function
214 if ( 'bbp_mark_read' !== $action ) {
215 return;
216 }
217
218 // Get required data
219 $user_id = bp_loggedin_user_id();
220 $topic_id = absint( $_GET['topic_id'] );
221
222 // By default, Redirect to this topic ID
223 $redirect_id = $topic_id;
224
225 // Check nonce
226 if ( ! bbp_verify_nonce_request( 'bbp_mark_topic_' . $topic_id ) ) {
227 bbp_add_error( 'bbp_notification_topic_id', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
228
229 // Check current user's ability to edit the user
230 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
231 bbp_add_error( 'bbp_notification_permission', __( '<strong>Error</strong>: You do not have permission to mark notifications for that user.', 'bbpress' ) );
232 }
233
234 // Bail if we have errors
235 if ( ! bbp_has_errors() ) {
236
237 // Default to nothing marked
238 $reply_marked = $topic_marked = $marked = false;
239
240 // Get these once
241 $post_type = bbp_get_reply_post_type();
242 $component = bbp_get_component_name();
243 $c_action = 'bbp_new_reply_' . $topic_id;
244
245 // Get all reply IDs for the topic
246 $replies = bbp_get_all_child_ids( $topic_id, $post_type );
247
248 // If topic has replies
249 if ( ! empty( $replies ) ) {
250
251 // Loop through each reply and attempt to mark it
252 foreach ( $replies as $reply_id ) {
253
254 // Attempt to clear notification for this user & reply ID
255 $reply_marked = bp_notifications_mark_notifications_by_item_id( $user_id, $reply_id, $component, $c_action );
256
257 // If marked, redirect to this reply ID
258 if ( ! empty( $reply_marked ) ) {
259 $redirect_id = $reply_id;
260 }
261 }
262 }
263
264 // Attempt to clear notifications for this user & topic
265 $topic_marked = bp_notifications_mark_notifications_by_type( $user_id, $component, $c_action );
266
267 // Maybe combine updated/marked rows
268 if ( is_numeric( $topic_marked ) || is_numeric( $reply_marked ) ) {
269 $marked = (int) $topic_marked + (int) $reply_marked;
270 }
271
272 // Do additional subscriptions actions
273 do_action( 'bbp_notifications_handler', $marked, $user_id, $topic_id, $action );
274 }
275
276 // Redirect to the topic
277 $redirect = bbp_get_reply_url( $redirect_id );
278
279 // Redirect
280 bbp_redirect( $redirect );
281 }
282