| 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 (r5155) |
| 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 |
// Bail if the reply is not published |
| 160 |
if ( ! bbp_is_reply_published( $reply_id ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Get author information |
| 165 |
$topic_author_id = bbp_get_topic_author_id( $topic_id ); |
| 166 |
$secondary_item_id = $author_id; |
| 167 |
|
| 168 |
// Hierarchical replies |
| 169 |
if ( ! empty( $reply_to ) ) { |
| 170 |
$reply_to_item_id = bbp_get_topic_author_id( $reply_to ); |
| 171 |
} |
| 172 |
|
| 173 |
// Get some reply information |
| 174 |
$args = array( |
| 175 |
'user_id' => $topic_author_id, |
| 176 |
'item_id' => $reply_id, |
| 177 |
'component_name' => bbp_get_component_name(), |
| 178 |
'component_action' => 'bbp_new_reply_' . $topic_id, |
| 179 |
'date_notified' => get_post( $reply_id )->post_date, |
| 180 |
); |
| 181 |
|
| 182 |
// Notify the topic author if not the current reply author |
| 183 |
if ( $author_id !== $topic_author_id ) { |
| 184 |
$args['secondary_item_id'] = $secondary_item_id; |
| 185 |
|
| 186 |
bp_notifications_add_notification( $args ); |
| 187 |
} |
| 188 |
|
| 189 |
// Notify the immediate reply author if not the current reply author |
| 190 |
if ( ! empty( $reply_to ) && ( $author_id !== $reply_to_item_id ) ) { |
| 191 |
$args['user_id'] = $reply_to_item_id; |
| 192 |
$args['secondary_item_id'] = $topic_author_id; |
| 193 |
|
| 194 |
bp_notifications_add_notification( $args ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Mark notifications as read when reading a topic |
| 200 |
* |
| 201 |
* @since 2.5.0 bbPress (r5155) |
| 202 |
* |
| 203 |
* @return If not trying to mark a notification as read |
| 204 |
*/ |
| 205 |
function bbp_buddypress_mark_notifications( $action = '' ) { |
| 206 |
|
| 207 |
// Bail if no topic ID is passed |
| 208 |
if ( empty( $_GET['topic_id'] ) ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
// Bail if action is not for this function |
| 213 |
if ( 'bbp_mark_read' !== $action ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
// Get required data |
| 218 |
$user_id = bp_loggedin_user_id(); |
| 219 |
$topic_id = absint( $_GET['topic_id'] ); |
| 220 |
|
| 221 |
// By default, Redirect to this topic ID |
| 222 |
$redirect_id = $topic_id; |
| 223 |
|
| 224 |
// Check nonce |
| 225 |
if ( ! bbp_verify_nonce_request( 'bbp_mark_topic_' . $topic_id ) ) { |
| 226 |
bbp_add_error( 'bbp_notification_topic_id', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
| 227 |
|
| 228 |
// Check current user's ability to edit the user |
| 229 |
} elseif ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 230 |
bbp_add_error( 'bbp_notification_permission', __( '<strong>Error</strong>: You do not have permission to mark notifications for that user.', 'bbpress' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
// Bail if we have errors |
| 234 |
if ( ! bbp_has_errors() ) { |
| 235 |
|
| 236 |
// Default to nothing marked |
| 237 |
$reply_marked = $topic_marked = $marked = false; |
| 238 |
|
| 239 |
// Get these once |
| 240 |
$post_type = bbp_get_reply_post_type(); |
| 241 |
$component = bbp_get_component_name(); |
| 242 |
$c_action = 'bbp_new_reply_' . $topic_id; |
| 243 |
|
| 244 |
// Get all reply IDs for the topic |
| 245 |
$replies = bbp_get_all_child_ids( $topic_id, $post_type ); |
| 246 |
|
| 247 |
// If topic has replies |
| 248 |
if ( ! empty( $replies ) ) { |
| 249 |
|
| 250 |
// Loop through each reply and attempt to mark it |
| 251 |
foreach ( $replies as $reply_id ) { |
| 252 |
|
| 253 |
// Attempt to clear notification for this user & reply ID |
| 254 |
$reply_marked = bp_notifications_mark_notifications_by_item_id( $user_id, $reply_id, $component, $c_action ); |
| 255 |
|
| 256 |
// If marked, redirect to this reply ID |
| 257 |
if ( ! empty( $reply_marked ) ) { |
| 258 |
$redirect_id = $reply_id; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
// Attempt to clear notifications for this user & topic |
| 264 |
$topic_marked = bp_notifications_mark_notifications_by_type( $user_id, $component, $c_action ); |
| 265 |
|
| 266 |
// Maybe combine updated/marked rows |
| 267 |
if ( is_numeric( $topic_marked ) || is_numeric( $reply_marked ) ) { |
| 268 |
$marked = (int) $topic_marked + (int) $reply_marked; |
| 269 |
} |
| 270 |
|
| 271 |
// Do additional subscriptions actions |
| 272 |
do_action( 'bbp_notifications_handler', $marked, $user_id, $topic_id, $action ); |
| 273 |
} |
| 274 |
|
| 275 |
// Redirect to the topic |
| 276 |
$redirect = bbp_get_reply_url( $redirect_id ); |
| 277 |
|
| 278 |
// Redirect |
| 279 |
bbp_redirect( $redirect ); |
| 280 |
} |
| 281 |
|