[ Index ] |
PHP Cross Reference of BBPress |
[Summary view] [Print] [Text view]
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( esc_html__( 'You have %1$d new replies to %2$s', 'bbpress' ), $action_item_count, $topic_title ); 90 91 // Single 92 } else { 93 $filter = 'bbp_single_new_subscription_notification'; 94 $text = ! empty( $secondary_item_id ) 95 ? sprintf( esc_html__( 'You have %1$d new reply to %2$s from %3$s', 'bbpress' ), $action_item_count, $topic_title, bp_core_get_user_displayname( $secondary_item_id ) ) 96 : sprintf( esc_html__( 'You have %1$d new reply to %2$s', 'bbpress' ), $action_item_count, $topic_title ); 97 } 98 99 // WordPress Toolbar 100 if ( 'string' === $format ) { 101 $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 ); 102 103 // Deprecated BuddyBar 104 } else { 105 $return = apply_filters( $filter, array( 106 'text' => $text, 107 'link' => $topic_link 108 ), $topic_link, $action_item_count, $text, $topic_title ); 109 } 110 111 do_action( 'bbp_format_buddypress_notifications', $component_action_name, $item_id, $secondary_item_id, $action_item_count, $format, $component_action_name, $component_name, $id ); 112 113 return $return; 114 } 115 116 /** 117 * Hooked into the new reply function, this notification action is responsible 118 * for notifying topic and hierarchical reply authors of topic replies. 119 * 120 * @since 2.5.0 bbPress (r5156) 121 * 122 * @param int $reply_id 123 * @param int $topic_id 124 * @param int $forum_id (not used) 125 * @param array $anonymous_data (not used) 126 * @param int $author_id 127 * @param bool $is_edit Used to bail if this gets hooked to an edit action 128 * @param int $reply_to 129 */ 130 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 ) { 131 132 // Bail if somehow this is hooked to an edit action 133 if ( ! empty( $is_edit ) ) { 134 return; 135 } 136 137 // Get author information 138 $topic_author_id = bbp_get_topic_author_id( $topic_id ); 139 $secondary_item_id = $author_id; 140 141 // Hierarchical replies 142 if ( ! empty( $reply_to ) ) { 143 $reply_to_item_id = bbp_get_topic_author_id( $reply_to ); 144 } 145 146 // Get some reply information 147 $args = array( 148 'user_id' => $topic_author_id, 149 'item_id' => $reply_id, 150 'component_name' => bbp_get_component_name(), 151 'component_action' => 'bbp_new_reply_' . $topic_id, 152 'date_notified' => get_post( $reply_id )->post_date, 153 ); 154 155 // Notify the topic author if not the current reply author 156 if ( $author_id !== $topic_author_id ) { 157 $args['secondary_item_id'] = $secondary_item_id; 158 159 bp_notifications_add_notification( $args ); 160 } 161 162 // Notify the immediate reply author if not the current reply author 163 if ( ! empty( $reply_to ) && ( $author_id !== $reply_to_item_id ) ) { 164 $args['user_id'] = $reply_to_item_id; 165 $args['secondary_item_id'] = $topic_author_id; 166 167 bp_notifications_add_notification( $args ); 168 } 169 } 170 171 /** 172 * Mark notifications as read when reading a topic 173 * 174 * @since 2.5.0 bbPress (r5155) 175 * 176 * @return If not trying to mark a notification as read 177 */ 178 function bbp_buddypress_mark_notifications( $action = '' ) { 179 180 // Bail if no topic ID is passed 181 if ( empty( $_GET['topic_id'] ) ) { 182 return; 183 } 184 185 // Bail if action is not for this function 186 if ( 'bbp_mark_read' !== $action ) { 187 return; 188 } 189 190 // Get required data 191 $user_id = bp_loggedin_user_id(); 192 $topic_id = absint( $_GET['topic_id'] ); 193 194 // By default, Redirect to this topic ID 195 $redirect_id = $topic_id; 196 197 // Check nonce 198 if ( ! bbp_verify_nonce_request( 'bbp_mark_topic_' . $topic_id ) ) { 199 bbp_add_error( 'bbp_notification_topic_id', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 200 201 // Check current user's ability to edit the user 202 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) { 203 bbp_add_error( 'bbp_notification_permission', __( '<strong>Error</strong>: You do not have permission to mark notifications for that user.', 'bbpress' ) ); 204 } 205 206 // Bail if we have errors 207 if ( ! bbp_has_errors() ) { 208 209 // Get these once 210 $post_type = bbp_get_reply_post_type(); 211 $component = bbp_get_component_name(); 212 213 // Attempt to clear notifications for this topic 214 $marked = bp_notifications_mark_notifications_by_type( $user_id, $component, 'bbp_new_reply_' . $topic_id ); 215 216 // Get all reply IDs for the topic 217 $replies = bbp_get_all_child_ids( $topic_id, $post_type ); 218 219 // If topic has replies 220 if ( ! empty( $replies ) ) { 221 222 // Loop through each reply and attempt to mark it 223 foreach ( $replies as $reply_id ) { 224 225 // Attempt to mark notification for this reply ID 226 $marked = bp_notifications_mark_notifications_by_item_id( $user_id, $reply_id, $component, 'bbp_new_reply' ); 227 228 // If marked, redirect to this reply ID 229 if ( ! empty( $marked ) ) { 230 $redirect_id = $reply_id; 231 } 232 } 233 } 234 235 // Do additional subscriptions actions 236 do_action( 'bbp_notifications_handler', $marked, $user_id, $topic_id, $action ); 237 } 238 239 // Redirect to the topic 240 $redirect = bbp_get_reply_url( $redirect_id ); 241 242 // Redirect 243 bbp_redirect( $redirect ); 244 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 21 01:00:52 2024 | Cross-referenced by PHPXref 0.7.1 |