Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/09/2012 09:06:00 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Clean up notifications code, and prevent notification for inactive components from appearing. Fixes #3650.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-notifications.php

    r5302 r5680  
    1212if ( !defined( 'ABSPATH' ) ) exit;
    1313
     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 */
    1426function bp_core_add_notification( $item_id, $user_id, $component_name, $component_action, $secondary_item_id = 0, $date_notified = false ) {
    15     global $bp;
    1627
    1728    if ( empty( $date_notified ) )
    1829        $date_notified = bp_core_current_time();
    1930
    20     $notification                        = new BP_Core_Notification;
    21     $notification->item_id               = $item_id;
    22     $notification->user_id               = $user_id;
    23     $notification->component_name        = $component_name;
    24     $notification->component_action      = $component_action;
    25     $notification->date_notified         = $date_notified;
    26     $notification->is_new                = 1;
     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;
    2738
    2839    if ( !empty( $secondary_item_id ) )
    2940        $notification->secondary_item_id = $secondary_item_id;
    3041
    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 */
    3755function bp_core_delete_notification( $id ) {
    3856    if ( !bp_core_check_notification_access( bp_loggedin_user_id(), $id ) )
     
    4260}
    4361
     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 */
    4469function bp_core_get_notification( $id ) {
    4570    return new BP_Core_Notification( $id );
    4671}
    4772
     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 */
    4882function bp_core_get_notifications_for_user( $user_id, $format = 'simple' ) {
    4983    global $bp;
    5084
    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
    5288
    5389    // Group notifications by component and component_action and provide totals
     
    5793    }
    5894
     95    // Bail if no notification groups
    5996    if ( empty( $grouped_notifications ) )
    6097        return false;
    6198
    62     $renderable = array();
    63 
    6499    // 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 ) )
    67104            continue;
    68105
    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
    72117            if ( $action_item_count < 1 )
    73118                continue;
    74119
    75             // @deprecated format_notification_function - 1.5
    76             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
    79124                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
    82137                    $notification_object = new stdClass;
    83138
     
    93148
    94149                    $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
    97153                } 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 );
    99155                    $renderable[] = $content;
    100156                }
     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 );
    101161            }
    102162        }
    103163    }
    104164
    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 */
    108185function bp_core_delete_notifications_by_type( $user_id, $component_name, $component_action ) {
    109186    return BP_Core_Notification::delete_for_user_by_type( $user_id, $component_name, $component_action );
    110187}
    111188
     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 */
    112201function bp_core_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
    113202    return BP_Core_Notification::delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id );
    114203}
    115204
     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 */
    116216function bp_core_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) {
    117217    return BP_Core_Notification::delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id );
    118218}
    119219
     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 */
    120231function bp_core_delete_notifications_from_user( $user_id, $component_name, $component_action ) {
    121232    return BP_Core_Notification::delete_from_user_by_type( $user_id, $component_name, $component_action );
    122233}
    123234
     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 */
    124245function bp_core_check_notification_access( $user_id, $notification_id ) {
    125246    if ( !BP_Core_Notification::check_access( $user_id, $notification_id ) )
Note: See TracChangeset for help on using the changeset viewer.