Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/07/2013 05:15:51 PM (11 years ago)
Author:
johnjamesjacoby
Message:

First pass at bp-notifications component. Includes:

  • Backwards compatibility for old core functions.
  • Screens, functions, classes, and actions for Notifications.
  • Improved class methods for getting, updating, and deleting notifications.
  • Template parts in bp-legacy for theme compatibility.
  • A few basic unit tests.

@todo's:

  • Improve template output with dedicated functions, markup, classes, et all.
  • More unit tests.
  • Pagination links.
  • Auto-activate on update, so existing installations do not lose previously core functionality.

See #5148. Props johnjamesjacoby, boonebgorges, r-a-y.

File:
1 edited

Legend:

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

    r7173 r7521  
    44 * BuddyPress Member Notifications
    55 *
    6  * Functions and filters used for member notification
     6 * Backwards compatibility functions and filters used for member notifications.
     7 * Use bp-notifications instead.
    78 *
    89 * @package BuddyPress
     
    2728function bp_core_add_notification( $item_id, $user_id, $component_name, $component_action, $secondary_item_id = 0, $date_notified = false ) {
    2829
    29     if ( empty( $date_notified ) )
    30         $date_notified = bp_core_current_time();
     30    // Bail if notifications is not active
     31    if ( ! bp_is_active( 'notifications' ) ) {
     32        return false;
     33    }
    3134
    32     $notification                   = new BP_Core_Notification;
    33     $notification->item_id          = $item_id;
    34     $notification->user_id          = $user_id;
    35     $notification->component_name   = $component_name;
    36     $notification->component_action = $component_action;
    37     $notification->date_notified    = $date_notified;
    38     $notification->is_new           = 1;
    39 
    40     if ( !empty( $secondary_item_id ) )
    41         $notification->secondary_item_id = $secondary_item_id;
    42 
    43     if ( $notification->save() )
    44         return true;
    45 
    46     return false;
     35    return bp_notifications_add_notification( $item_id, $user_id, $component_name, $component_action, $secondary_item_id = 0, $date_notified = false );
    4736}
    4837
     
    5544 */
    5645function bp_core_delete_notification( $id ) {
    57     if ( !bp_core_check_notification_access( bp_loggedin_user_id(), $id ) )
     46
     47    // Bail if notifications is not active
     48    if ( ! bp_is_active( 'notifications' ) ) {
    5849        return false;
     50    }
    5951
    60     return BP_Core_Notification::delete( $id );
     52    return bp_notifications_delete_notification( $id );
    6153}
    6254
     
    6961 */
    7062function bp_core_get_notification( $id ) {
    71     return new BP_Core_Notification( $id );
     63
     64    // Bail if notifications is not active
     65    if ( ! bp_is_active( 'notifications' ) ) {
     66        return false;
     67    }
     68
     69    return bp_notifications_get_notification( $id );
    7270}
    7371
     
    8280 */
    8381function bp_core_get_notifications_for_user( $user_id, $format = 'simple' ) {
    84     global $bp;
    8582
    86     $notifications         = BP_Core_Notification::get_all_for_user( $user_id );
    87     $grouped_notifications = array(); // Notification groups
    88     $renderable            = array(); // Renderable notifications
    89 
    90     // Group notifications by component and component_action and provide totals
    91     for ( $i = 0, $count = count( $notifications ); $i < $count; ++$i ) {
    92         $notification = $notifications[$i];
    93         $grouped_notifications[$notification->component_name][$notification->component_action][] = $notification;
     83    // Bail if notifications is not active
     84    if ( ! bp_is_active( 'notifications' ) ) {
     85        return false;
    9486    }
    9587
    96     // Bail if no notification groups
    97     if ( empty( $grouped_notifications ) )
    98         return false;
     88    $renderable = bp_notifications_get_notifications_for_user( $user_id, $format );
    9989
    100     // Calculate a renderable output for each notification type
    101     foreach ( $grouped_notifications as $component_name => $action_arrays ) {
    102 
    103         // Skip if group is empty
    104         if ( empty( $action_arrays ) )
    105             continue;
    106 
    107         // Skip inactive components
    108         if ( !bp_is_active( $component_name ) )
    109             continue;
    110 
    111         // Loop through each actionable item and try to map it to a component
    112         foreach ( (array) $action_arrays as $component_action_name => $component_action_items ) {
    113 
    114             // Get the number of actionable items
    115             $action_item_count = count( $component_action_items );
    116 
    117             // Skip if the count is less than 1
    118             if ( $action_item_count < 1 )
    119                 continue;
    120 
    121             // Callback function exists
    122             if ( isset( $bp->{$component_name}->notification_callback ) && is_callable( $bp->{$component_name}->notification_callback ) ) {
    123 
    124                 // Function should return an object
    125                 if ( 'object' == $format ) {
    126 
    127                     // Retrieve the content of the notification using the callback
    128                     $content = call_user_func(
    129                         $bp->{$component_name}->notification_callback,
    130                         $component_action_name,
    131                         $component_action_items[0]->item_id,
    132                         $component_action_items[0]->secondary_item_id,
    133                         $action_item_count,
    134                         'array'
    135                     );
    136 
    137                     // Create the object to be returned
    138                     $notification_object = new stdClass;
    139 
    140                     // Minimal backpat with non-compatible notification
    141                     // callback functions
    142                     if ( is_string( $content ) ) {
    143                         $notification_object->content = $content;
    144                         $notification_object->href    = bp_loggedin_user_domain();
    145                     } else {
    146                         $notification_object->content = $content['text'];
    147                         $notification_object->href    = $content['link'];
    148                     }
    149 
    150                     $notification_object->id = $component_action_items[0]->id;
    151                     $renderable[]            = $notification_object;
    152 
    153                 // Return an array of content strings
    154                 } else {
    155                     $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 );
    156                     $renderable[] = $content;
    157                 }
    158 
    159             // @deprecated format_notification_function - 1.5
    160             } elseif ( isset( $bp->{$component_name}->format_notification_function ) && function_exists( $bp->{$component_name}->format_notification_function ) ) {
    161                 $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 );
    162             }
    163         }
    164     }
    165 
    166     // If renderable is empty array, set to false
    167     if ( empty( $renderable ) )
    168         $renderable = false;
    169 
    170     // Filter and return
    17190    return apply_filters( 'bp_core_get_notifications_for_user', $renderable, $user_id, $format );
    17291}
     
    185104 */
    186105function bp_core_delete_notifications_by_type( $user_id, $component_name, $component_action ) {
    187     return BP_Core_Notification::delete_for_user_by_type( $user_id, $component_name, $component_action );
     106
     107    // Bail if notifications is not active
     108    if ( ! bp_is_active( 'notifications' ) ) {
     109        return false;
     110    }
     111
     112    return bp_notifications_delete_notifications_by_type( $user_id, $component_name, $component_action );
    188113}
    189114
     
    201126 */
    202127function bp_core_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false ) {
    203     return BP_Core_Notification::delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id );
     128
     129    // Bail if notifications is not active
     130    if ( ! bp_is_active( 'notifications' ) ) {
     131        return false;
     132    }
     133
     134    return bp_notifications_delete_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id );
    204135}
    205136
     
    216147 */
    217148function bp_core_delete_all_notifications_by_type( $item_id, $component_name, $component_action = false, $secondary_item_id = false ) {
    218     return BP_Core_Notification::delete_all_by_type( $item_id, $component_name, $component_action, $secondary_item_id );
     149
     150    // Bail if notifications is not active
     151    if ( ! bp_is_active( 'notifications' ) ) {
     152        return false;
     153    }
     154
     155    bp_notifications_delete_all_notifications_by_type( $item_id, $component_name, $component_action, $secondary_item_id );
    219156}
    220157
     
    231168 */
    232169function bp_core_delete_notifications_from_user( $user_id, $component_name, $component_action ) {
    233     return BP_Core_Notification::delete_from_user_by_type( $user_id, $component_name, $component_action );
     170
     171    // Bail if notifications is not active
     172    if ( ! bp_is_active( 'notifications' ) ) {
     173        return false;
     174    }
     175
     176    return bp_notifications_delete_notifications_from_user( $user_id, $component_name, $component_action );
    234177}
    235178
     
    245188 */
    246189function bp_core_check_notification_access( $user_id, $notification_id ) {
    247     if ( !BP_Core_Notification::check_access( $user_id, $notification_id ) )
     190
     191    // Bail if notifications is not active
     192    if ( ! bp_is_active( 'notifications' ) ) {
    248193        return false;
     194    }
    249195
    250     return true;
     196    return bp_notifications_check_notification_access( $user_id, $notification_id );
    251197}
Note: See TracChangeset for help on using the changeset viewer.