PluginProbe
bbPress / 2.6.18
bbPress v2.6.18
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 2.6.18, at includes/extend/buddypress/notifications.php

276 lines 8.4 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
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
97 } else {
98 $filter = 'bbp_single_new_subscription_notification';
99 $text = ! empty( $secondary_item_id )
100 ? sprintf(
101 /* translators: 1: Number of replies, 2: Topic title, 3: Reply author name */
102 esc_html__( 'You have %1$d new reply to %2$s from %3$s', 'bbpress' ),
103 $action_item_count,
104 $topic_title,
105 bp_core_get_user_displayname( $secondary_item_id )
106 )
107 : sprintf(
108 /* translators: 1: Number of replies, 2: Topic title */
109 esc_html__( 'You have %1$d new reply to %2$s', 'bbpress' ),
110 $action_item_count,
111 $topic_title
112 );
113 }
114
115 // WordPress Toolbar
116 if ( 'string' === $format ) {
117 $return = apply_filters( $filter, '<a href="' . esc_url( $topic_link ) . '" title="' . esc_attr__( 'Topic Replies', 'bbpress' ) . '">' . esc_html( $text ) . '</a>', $action_item_count, $text, $topic_link );
118
119 // Deprecated BuddyBar
120 } else {
121 $return = apply_filters( $filter,
122 array(
123 'text' => $text,
124 'link' => $topic_link
125 ),
126 $topic_link,
127 $action_item_count,
128 $text,
129 $topic_title
130 );
131 }
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 $return;
136 }
137
138 /**
139 * Hooked into the new reply function, this notification action is responsible
140 * for notifying topic and hierarchical reply authors of topic replies.
141 *
142 * @since 2.5.0 bbPress (r5156)
143 *
144 * @param int $reply_id
145 * @param int $topic_id
146 * @param int $forum_id (not used)
147 * @param array $anonymous_data (not used)
148 * @param int $author_id
149 * @param bool $is_edit Used to bail if this gets hooked to an edit action
150 * @param int $reply_to
151 */
152 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 ) {
153
154 // Bail if somehow this is hooked to an edit action
155 if ( ! empty( $is_edit ) ) {
156 return;
157 }
158
159 // Get author information
160 $topic_author_id = bbp_get_topic_author_id( $topic_id );
161 $secondary_item_id = $author_id;
162
163 // Hierarchical replies
164 if ( ! empty( $reply_to ) ) {
165 $reply_to_item_id = bbp_get_topic_author_id( $reply_to );
166 }
167
168 // Get some reply information
169 $args = array(
170 'user_id' => $topic_author_id,
171 'item_id' => $reply_id,
172 'component_name' => bbp_get_component_name(),
173 'component_action' => 'bbp_new_reply_' . $topic_id,
174 'date_notified' => get_post( $reply_id )->post_date,
175 );
176
177 // Notify the topic author if not the current reply author
178 if ( $author_id !== $topic_author_id ) {
179 $args['secondary_item_id'] = $secondary_item_id;
180
181 bp_notifications_add_notification( $args );
182 }
183
184 // Notify the immediate reply author if not the current reply author
185 if ( ! empty( $reply_to ) && ( $author_id !== $reply_to_item_id ) ) {
186 $args['user_id'] = $reply_to_item_id;
187 $args['secondary_item_id'] = $topic_author_id;
188
189 bp_notifications_add_notification( $args );
190 }
191 }
192
193 /**
194 * Mark notifications as read when reading a topic
195 *
196 * @since 2.5.0 bbPress (r5155)
197 *
198 * @return If not trying to mark a notification as read
199 */
200 function bbp_buddypress_mark_notifications( $action = '' ) {
201
202 // Bail if no topic ID is passed
203 if ( empty( $_GET['topic_id'] ) ) {
204 return;
205 }
206
207 // Bail if action is not for this function
208 if ( 'bbp_mark_read' !== $action ) {
209 return;
210 }
211
212 // Get required data
213 $user_id = bp_loggedin_user_id();
214 $topic_id = absint( $_GET['topic_id'] );
215
216 // By default, Redirect to this topic ID
217 $redirect_id = $topic_id;
218
219 // Check nonce
220 if ( ! bbp_verify_nonce_request( 'bbp_mark_topic_' . $topic_id ) ) {
221 bbp_add_error( 'bbp_notification_topic_id', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
222
223 // Check current user's ability to edit the user
224 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
225 bbp_add_error( 'bbp_notification_permission', __( '<strong>Error</strong>: You do not have permission to mark notifications for that user.', 'bbpress' ) );
226 }
227
228 // Bail if we have errors
229 if ( ! bbp_has_errors() ) {
230
231 // Default to nothing marked
232 $reply_marked = $topic_marked = $marked = false;
233
234 // Get these once
235 $post_type = bbp_get_reply_post_type();
236 $component = bbp_get_component_name();
237 $c_action = 'bbp_new_reply_' . $topic_id;
238
239 // Get all reply IDs for the topic
240 $replies = bbp_get_all_child_ids( $topic_id, $post_type );
241
242 // If topic has replies
243 if ( ! empty( $replies ) ) {
244
245 // Loop through each reply and attempt to mark it
246 foreach ( $replies as $reply_id ) {
247
248 // Attempt to clear notification for this user & reply ID
249 $reply_marked = bp_notifications_mark_notifications_by_item_id( $user_id, $reply_id, $component, $c_action );
250
251 // If marked, redirect to this reply ID
252 if ( ! empty( $reply_marked ) ) {
253 $redirect_id = $reply_id;
254 }
255 }
256 }
257
258 // Attempt to clear notifications for this user & topic
259 $topic_marked = bp_notifications_mark_notifications_by_type( $user_id, $component, $c_action );
260
261 // Maybe combine updated/marked rows
262 if ( is_numeric( $topic_marked ) || is_numeric( $reply_marked ) ) {
263 $marked = (int) $topic_marked + (int) $reply_marked;
264 }
265
266 // Do additional subscriptions actions
267 do_action( 'bbp_notifications_handler', $marked, $user_id, $topic_id, $action );
268 }
269
270 // Redirect to the topic
271 $redirect = bbp_get_reply_url( $redirect_id );
272
273 // Redirect
274 bbp_redirect( $redirect );
275 }
276