Changeset 5680 for trunk/bp-members/bp-members-notifications.php
- Timestamp:
- 02/09/2012 09:06:00 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-members/bp-members-notifications.php
r5302 r5680 12 12 if ( !defined( 'ABSPATH' ) ) exit; 13 13 14 /** 15 * Add a notification for a specific user, from a specific component 16 * 17 * @since BuddyPress (1.0) 18 * @param string $item_id 19 * @param int $user_id 20 * @param string $component_name 21 * @param string $component_action 22 * @param string $secondary_item_id 23 * @param string $date_notified 24 * @return boolean True on success, false on fail 25 */ 14 26 function bp_core_add_notification( $item_id, $user_id, $component_name, $component_action, $secondary_item_id = 0, $date_notified = false ) { 15 global $bp;16 27 17 28 if ( empty( $date_notified ) ) 18 29 $date_notified = bp_core_current_time(); 19 30 20 $notification 21 $notification->item_id 22 $notification->user_id 23 $notification->component_name 24 $notification->component_action 25 $notification->date_notified 26 $notification->is_new 31 $notification = new BP_Core_Notification; 32 $notification->item_id = $item_id; 33 $notification->user_id = $user_id; 34 $notification->component_name = $component_name; 35 $notification->component_action = $component_action; 36 $notification->date_notified = $date_notified; 37 $notification->is_new = 1; 27 38 28 39 if ( !empty( $secondary_item_id ) ) 29 40 $notification->secondary_item_id = $secondary_item_id; 30 41 31 if ( !$notification->save() ) 32 return false; 33 34 return true; 35 } 36 42 if ( $notification->save() ) 43 return true; 44 45 return false; 46 } 47 48 /** 49 * Delete a specific notification by its ID 50 * 51 * @since BuddyPress (1.0) 52 * @param int $id 53 * @return boolean True on success, false on fail 54 */ 37 55 function bp_core_delete_notification( $id ) { 38 56 if ( !bp_core_check_notification_access( bp_loggedin_user_id(), $id ) ) … … 42 60 } 43 61 62 /** 63 * Get a specific notification by its ID 64 * 65 * @since BuddyPress (1.0) 66 * @param int $id 67 * @return BP_Core_Notification 68 */ 44 69 function bp_core_get_notification( $id ) { 45 70 return new BP_Core_Notification( $id ); 46 71 } 47 72 73 /** 74 * Get notifications for a specific user 75 * 76 * @since BuddyPress (1.0) 77 * @global BuddyPress $bp 78 * @param int $user_id 79 * @param string $format 80 * @return boolean Object or array on success, false on fail 81 */ 48 82 function bp_core_get_notifications_for_user( $user_id, $format = 'simple' ) { 49 83 global $bp; 50 84 51 $notifications = BP_Core_Notification::get_all_for_user( $user_id ); 85 $notifications = BP_Core_Notification::get_all_for_user( $user_id ); 86 $grouped_notifications = array(); // Notification groups 87 $renderable = array(); // Nederable notifications 52 88 53 89 // Group notifications by component and component_action and provide totals … … 57 93 } 58 94 95 // Bail if no notification groups 59 96 if ( empty( $grouped_notifications ) ) 60 97 return false; 61 98 62 $renderable = array();63 64 99 // Calculate a renderable output for each notification type 65 foreach ( (array)$grouped_notifications as $component_name => $action_arrays ) { 66 if ( !$action_arrays ) 100 foreach ( $grouped_notifications as $component_name => $action_arrays ) { 101 102 // Skip if group is empty 103 if ( empty( $action_arrays ) ) 67 104 continue; 68 105 69 foreach ( (array)$action_arrays as $component_action_name => $component_action_items ) { 70 $action_item_count = count($component_action_items); 71 106 // Skip inactive components 107 if ( !bp_is_active( $component_name ) ) 108 continue; 109 110 // Loop through each actionable item and try to map it to a component 111 foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) { 112 113 // Get the number of actionable items 114 $action_item_count = count( $component_action_items ); 115 116 // Skip if the count is less than 1 72 117 if ( $action_item_count < 1 ) 73 118 continue; 74 119 75 // @deprecated format_notification_function - 1.576 if ( isset( $bp->{$component_name}-> format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function) ) {77 $renderable[] = call_user_func( $bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count ); 78 } elseif ( isset( $bp->{$component_name}->notification_callback ) && function_exists( $bp->{$component_name}->notification_callback ) ) {120 // Callback function exists 121 if ( isset( $bp->{$component_name}->notification_callback ) && function_exists( $bp->{$component_name}->notification_callback ) ) { 122 123 // Function should return an object 79 124 if ( 'object' == $format ) { 80 $content = call_user_func( $bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count, 'array' ); 81 125 126 // Retrieve the content of the notification using the callback 127 $content = call_user_func( 128 $bp->{$component_name}->notification_callback, 129 $component_action_name, 130 $component_action_items[0]->item_id, 131 $component_action_items[0]->secondary_item_id, 132 $action_item_count, 133 'array' 134 ); 135 136 // Create the object to be returned 82 137 $notification_object = new stdClass; 83 138 … … 93 148 94 149 $notification_object->id = $component_action_items[0]->id; 95 96 $renderable[] = $notification_object; 150 $renderable[] = $notification_object; 151 152 // Return an array of content strings 97 153 } else { 98 $content = call_user_func( $bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count );154 $content = call_user_func( $bp->{$component_name}->notification_callback, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count ); 99 155 $renderable[] = $content; 100 156 } 157 158 // @deprecated format_notification_function - 1.5 159 } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) { 160 $renderable[] = call_user_func( $bp->{$component_name}->format_notification_function, $component_action_name, $component_action_items[0]->item_id, $component_action_items[0]->secondary_item_id, $action_item_count ); 101 161 } 102 162 } 103 163 } 104 164 105 return isset( $renderable ) ? $renderable : false; 106 } 107 165 // If renderable is empty array, set to false 166 if ( empty( $renderable ) ) 167 $renderable = false; 168 169 // Filter and return 170 return apply_filters( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format ); 171 } 172 173 /** 174 * Delete notifications for a user by type 175 * 176 * Used when clearing out notifications for a specific component when the user 177 * has visited that component. 178 * 179 * @since BuddyPress (1.0) 180 * @param int $user_id 181 * @param string $component_name 182 * @param string $component_action 183 * @return boolean True on success, false on fail 184 */ 108 185 function bp_core_delete_notifications_by_type( $user_id, $component_name, $component_action ) { 109 186 return BP_Core_Notification::delete_for_user_by_type( $user_id, $component_name, $component_action ); 110 187 } 111 188 189 /** 190 * Delete notifications for an item ID 191 * 192 * Used when clearing out notifications for a specific component when the user 193 * has visited that component. 194 * 195 * @since BuddyPress (1.0) 196 * @param int $user_id 197 * @param string $component_name 198 * @param string $component_action 199 * @return boolean True on success, false on fail 200 */ 112 201 function bp_core_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) { 113 202 return BP_Core_Notification::delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id ); 114 203 } 115 204 205 /** 206 * Delete all notifications for by type 207 * 208 * Used when clearing out notifications for an entire component 209 * 210 * @since BuddyPress (1.0) 211 * @param int $user_id 212 * @param string $component_name 213 * @param string $component_action 214 * @return boolean True on success, false on fail 215 */ 116 216 function bp_core_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) { 117 217 return BP_Core_Notification::delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id ); 118 218 } 119 219 220 /** 221 * Delete all notifications for a user 222 * 223 * Used when clearing out all notifications for a user, whene deleted or spammed 224 * 225 * @since BuddyPress (1.0) 226 * @param int $user_id 227 * @param string $component_name 228 * @param string $component_action 229 * @return boolean True on success, false on fail 230 */ 120 231 function bp_core_delete_notifications_from_user( $user_id, $component_name, $component_action ) { 121 232 return BP_Core_Notification::delete_from_user_by_type( $user_id, $component_name, $component_action ); 122 233 } 123 234 235 /** 236 * Check if a user has access to a specific notification 237 * 238 * Used before deleting a notification for a user 239 * 240 * @since BuddyPress (1.0) 241 * @param int $user_id 242 * @param int $notification_id 243 * @return boolean True on success, false on fail 244 */ 124 245 function bp_core_check_notification_access( $user_id, $notification_id ) { 125 246 if ( !BP_Core_Notification::check_access( $user_id, $notification_id ) )
Note: See TracChangeset
for help on using the changeset viewer.