| | 165 | /** |
| | 166 | * Get a user's unread notifications, grouped by component and action. |
| | 167 | * |
| | 168 | * This function returns a list of notifications collapsed by component + action. |
| | 169 | * See BP_Notifications_Notification::get_grouped_notifications_for_user() for |
| | 170 | * more details. |
| | 171 | * |
| | 172 | * @since 3.0.0 |
| | 173 | * |
| | 174 | * @param int $user_id ID of the user whose notifications are being fetched. |
| | 175 | * @return array $notifications |
| | 176 | */ |
| | 177 | function bp_notifications_get_grouped_notifications_for_user( $user_id = 0 ) { |
| | 178 | if ( empty( $user_id ) ) { |
| | 179 | $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); |
| | 180 | } |
| | 181 | |
| | 182 | $notifications = wp_cache_get( $user_id, 'bp_notifications_grouped_notifications' ); |
| | 183 | if ( false === $notifications ) { |
| | 184 | $notifications = BP_Notifications_Notification::get_grouped_notifications_for_user( $user_id ); |
| | 185 | wp_cache_set( $user_id, $notifications, 'bp_notifications_grouped_notifications' ); |
| | 186 | } |
| | 187 | |
| | 188 | return $notifications; |
| | 189 | } |
| | 190 | |
| 180 | | // Get notifications (out of the cache, or query if necessary). |
| 181 | | $notifications = bp_notifications_get_all_notifications_for_user( $user_id ); |
| 182 | | $grouped_notifications = array(); // Notification groups. |
| 183 | | $renderable = array(); // Renderable notifications. |
| 184 | | |
| 185 | | // Group notifications by component and component_action and provide totals. |
| 186 | | for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) { |
| 187 | | $notification = $notifications[$i]; |
| 188 | | $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification; |
| 189 | | } |
| 190 | | |
| 191 | | // Bail if no notification groups. |
| 192 | | if ( empty( $grouped_notifications ) ) { |
| 193 | | return false; |
| 194 | | } |
| | 204 | $notifications = bp_notifications_get_grouped_notifications_for_user( $user_id ); |
| 223 | | // Callback function exists. |
| 224 | | if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) { |
| 225 | | |
| 226 | | // Function should return an object. |
| 227 | | if ( 'object' === $format ) { |
| 228 | | |
| 229 | | // Retrieve the content of the notification using the callback. |
| 230 | | $content = call_user_func( |
| 231 | | $bp->{$component_name}->notification_callback, |
| 232 | | $component_action_name, |
| 233 | | $component_action_items[0]->item_id, |
| 234 | | $component_action_items[0]->secondary_item_id, |
| 235 | | $action_item_count, |
| 236 | | 'array', |
| 237 | | $component_action_items[0]->id |
| 238 | | ); |
| 239 | | |
| 240 | | // Create the object to be returned. |
| 241 | | $notification_object = $component_action_items[0]; |
| 242 | | |
| 243 | | // Minimal backpat with non-compatible notification |
| 244 | | // callback functions. |
| 245 | | if ( is_string( $content ) ) { |
| 246 | | $notification_object->content = $content; |
| 247 | | $notification_object->href = bp_loggedin_user_domain(); |
| 248 | | } else { |
| 249 | | $notification_object->content = $content['text']; |
| 250 | | $notification_object->href = $content['link']; |
| 251 | | } |
| 252 | | |
| 253 | | $renderable[] = $notification_object; |
| 254 | | |
| 255 | | // Return an array of content strings. |
| | 230 | // Minimal backpat with non-compatible notification |
| | 231 | // callback functions. |
| | 232 | if ( is_string( $content ) ) { |
| | 233 | $notification_object->content = $content; |
| | 234 | $notification_object->href = bp_loggedin_user_domain(); |
| 262 | | } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) { |
| 263 | | $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 ); |
| | 249 | } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) { |
| | 250 | $renderable[] = call_user_func( $bp->{$component_name}->notification_callback, $notification_item->component_action, $notification_item->item_id, $notification_item->secondary_item_id, $notification_item->total_count ); |
| 266 | | } else { |
| | 253 | } else { |
| | 254 | |
| | 255 | // The array to reference with apply_filters_ref_array(). |
| | 256 | $ref_array = array( |
| | 257 | $notification_item->component_action, |
| | 258 | $notification_item->item_id, |
| | 259 | $notification_item->secondary_item_id, |
| | 260 | $notification_item->total_count, |
| | 261 | $format, |
| | 262 | $notification_item->component_action, // Duplicated so plugins can check the canonical action name. |
| | 263 | $component_name, |
| | 264 | $notification_item->id, |
| | 265 | ); |
| | 266 | |
| | 267 | // Function should return an object. |
| | 268 | if ( 'object' === $format ) { |
| | 269 | |
| | 270 | /** |
| | 271 | * Filters the notification content for notifications created by plugins. |
| | 272 | * If your plugin extends the {@link BP_Component} class, you should use the |
| | 273 | * 'notification_callback' parameter in your extended |
| | 274 | * {@link BP_Component::setup_globals()} method instead. |
| | 275 | * |
| | 276 | * @since 1.9.0 |
| | 277 | * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters. |
| | 278 | * |
| | 279 | * @param string $content Component action. Deprecated. Do not do checks against this! Use |
| | 280 | * the 6th parameter instead - $component_action_name. |
| | 281 | * @param int $item_id Notification item ID. |
| | 282 | * @param int $secondary_item_id Notification secondary item ID. |
| | 283 | * @param int $action_item_count Number of notifications with the same action. |
| | 284 | * @param string $format Format of return. Either 'string' or 'object'. |
| | 285 | * @param string $component_action_name Canonical notification action. |
| | 286 | * @param string $component_name Notification component ID. |
| | 287 | * @param int $id Notification ID. |
| | 288 | * |
| | 289 | * @return string|array If $format is 'string', return a string of the notification content. |
| | 290 | * If $format is 'object', return an array formatted like: |
| | 291 | * array( 'text' => 'CONTENT', 'link' => 'LINK' ) |
| | 292 | */ |
| | 293 | $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array ); |
| | 294 | |
| | 295 | // Create the object to be returned. |
| | 296 | $notification_object = $notification_item; |
| | 297 | |
| | 298 | // Minimal backpat with non-compatible notification |
| | 299 | // callback functions. |
| | 300 | if ( is_string( $content ) ) { |
| | 301 | $notification_object->content = $content; |
| | 302 | $notification_object->href = bp_loggedin_user_domain(); |
| | 303 | } else { |
| | 304 | $notification_object->content = $content['text']; |
| | 305 | $notification_object->href = $content['link']; |
| | 306 | } |
| 268 | | // The array to reference with apply_filters_ref_array(). |
| 269 | | $ref_array = array( |
| 270 | | $component_action_name, |
| 271 | | $component_action_items[0]->item_id, |
| 272 | | $component_action_items[0]->secondary_item_id, |
| 273 | | $action_item_count, |
| 274 | | $format, |
| 275 | | $component_action_name, // Duplicated so plugins can check the canonical action name. |
| 276 | | $component_name, |
| 277 | | $component_action_items[0]->id |
| 278 | | ); |
| 279 | | |
| 280 | | // Function should return an object. |
| 281 | | if ( 'object' === $format ) { |
| 282 | | |
| 283 | | /** |
| 284 | | * Filters the notification content for notifications created by plugins. |
| 285 | | * |
| 286 | | * If your plugin extends the {@link BP_Component} class, you should use the |
| 287 | | * 'notification_callback' parameter in your extended |
| 288 | | * {@link BP_Component::setup_globals()} method instead. |
| 289 | | * |
| 290 | | * @since 1.9.0 |
| 291 | | * @since 2.6.0 Added $component_action_name, $component_name, $id as parameters. |
| 292 | | * |
| 293 | | * @param string $content Component action. Deprecated. Do not do checks against this! Use |
| 294 | | * the 6th parameter instead - $component_action_name. |
| 295 | | * @param int $item_id Notification item ID. |
| 296 | | * @param int $secondary_item_id Notification secondary item ID. |
| 297 | | * @param int $action_item_count Number of notifications with the same action. |
| 298 | | * @param string $format Format of return. Either 'string' or 'object'. |
| 299 | | * @param string $component_action_name Canonical notification action. |
| 300 | | * @param string $component_name Notification component ID. |
| 301 | | * @param int $id Notification ID. |
| 302 | | * |
| 303 | | * @return string|array If $format is 'string', return a string of the notification content. |
| 304 | | * If $format is 'object', return an array formatted like: |
| 305 | | * array( 'text' => 'CONTENT', 'link' => 'LINK' ) |
| 306 | | */ |
| 307 | | $content = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', $ref_array ); |
| 308 | | |
| 309 | | // Create the object to be returned. |
| 310 | | $notification_object = $component_action_items[0]; |
| 311 | | |
| 312 | | // Minimal backpat with non-compatible notification |
| 313 | | // callback functions. |
| 314 | | if ( is_string( $content ) ) { |
| 315 | | $notification_object->content = $content; |
| 316 | | $notification_object->href = bp_loggedin_user_domain(); |
| 317 | | } else { |
| 318 | | $notification_object->content = $content['text']; |
| 319 | | $notification_object->href = $content['link']; |
| 320 | | } |
| 321 | | |
| 322 | | $renderable[] = $notification_object; |
| | 308 | $renderable[] = $notification_object; |