Skip to:
Content

BuddyPress.org

Changeset 8744


Ignore:
Timestamp:
07/31/2014 05:31:52 PM (5 years ago)
Author:
johnjamesjacoby
Message:

Introduce bp_notifications_get_all_notifications_for_user() and move duplicated caching code into it.

This enables manipulating of notifications in other ways outside of the grouping performed for the adminbar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-functions.php

    r8577 r8744  
    129129
    130130/**
    131  * Get notifications for a specific user.
    132  *
    133  * @since BuddyPress (1.9.0)
    134  *
    135  * @param int $user_id ID of the user whose notification are being fetched.
    136  * @param string $format Format of the returned values. 'string' returns HTML,
    137  *        while 'object' returns a structured object for parsing.
    138  * @return mixed Object or array on success, false on failure.
    139  */
    140 function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
    141 
    142     // Setup local variables
    143     $bp = buddypress();
     131 * Get all notifications for a user and cache them
     132 *
     133 * @since BuddyPress (2.1.0)
     134 *
     135 * @param int $user_id
     136 * @return array
     137 */
     138function bp_notifications_get_all_notifications_for_user( $user_id = 0 ) {
     139
     140    // Default to displayed user if no ID is passed
     141    if ( empty( $user_id ) ) {
     142        $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
     143    }
    144144
    145145    // Get notifications out of the cache, or query if necessary
     
    152152    }
    153153
     154    // Filter and return
     155    return apply_filters( 'bp_notifications_get_all_notifications_for_user', $notifications, $user_id );
     156}
     157
     158/**
     159 * Get notifications for a specific user.
     160 *
     161 * @since BuddyPress (1.9.0)
     162 *
     163 * @param int $user_id ID of the user whose notification are being fetched.
     164 * @param string $format Format of the returned values. 'string' returns HTML,
     165 *        while 'object' returns a structured object for parsing.
     166 * @return mixed Object or array on success, false on failure.
     167 */
     168function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
     169
     170    // Setup local variables
     171    $bp = buddypress();
     172
     173    // Get notifications (out of the cache, or query if necessary)
     174    $notifications         = bp_notifications_get_all_notifications_for_user( $user_id );
    154175    $grouped_notifications = array(); // Notification groups
    155176    $renderable            = array(); // Renderable notifications
     
    519540 */
    520541function bp_notifications_get_unread_notification_count( $user_id = 0 ) {
    521 
    522     // Default to displayed user if no ID is passed
    523     if ( empty( $user_id ) ) {
    524         $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    525     }
    526 
    527     // Get the notifications, and count them
    528     $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
    529     if ( false === $notifications ) {
    530         $notifications = BP_Notifications_Notification::get( array(
    531             'user_id' => $user_id,
    532         ) );
    533         wp_cache_set( 'all_for_user_' . $user_id, $notifications, 'bp_notifications' );
    534     }
    535 
    536     $count = ! empty( $notifications ) ? count( $notifications ) : 0;
    537 
    538     return apply_filters( 'bp_notifications_get_total_notification_count', $count );
     542    $notifications = bp_notifications_get_all_notifications_for_user( $user_id );
     543    $count         = ! empty( $notifications ) ? count( $notifications ) : 0;
     544
     545    return apply_filters( 'bp_notifications_get_total_notification_count', (int) $count );
    539546}
    540547
Note: See TracChangeset for help on using the changeset viewer.