Skip to:
Content

BuddyPress.org

Ticket #7130: 7130.1.diff

File 7130.1.diff, 2.7 KB (added by mauteri, 7 years ago)

Another approach to issue

  • bp-notifications-functions.php

     
    132132 *
    133133 * @since 2.1.0
    134134 *
    135  * @param int $user_id ID of the user whose notifications are being fetched.
     135 * @param int   $user_id ID of the user whose notifications are being fetched.
     136 * @param array $args Additional arguments to expand notification query.
    136137 * @return array $notifications Array of notifications for user.
    137138 */
    138 function bp_notifications_get_all_notifications_for_user( $user_id = 0 ) {
     139function bp_notifications_get_all_notifications_for_user( $user_id = 0, $args = array() ) {
    139140
    140141        // Default to displayed user if no ID is passed.
    141142        if ( empty( $user_id ) ) {
     
    142143                $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    143144        }
    144145
     146        $args_cache_key = '';
     147        if ( ! empty( $args ) ) {
     148                $args_cache_key = '_' . md5( json_encode( $args ) );
     149        }
     150
    145151        // Get notifications out of the cache, or query if necessary.
    146         $notifications = wp_cache_get( 'all_for_user_' . $user_id, 'bp_notifications' );
     152        $notifications = wp_cache_get( 'all_for_user_' . $user_id . $args_cache_key, 'bp_notifications' );
    147153        if ( false === $notifications ) {
    148                 $notifications = BP_Notifications_Notification::get( array(
    149                         'user_id' => $user_id
     154                // For caching below.
     155                ksort( $args );
     156                $notifications = BP_Notifications_Notification::get( array_merge(
     157                        $args, array( 'user_id' => $user_id )
    150158                ) );
    151                 wp_cache_set( 'all_for_user_' . $user_id, $notifications, 'bp_notifications' );
     159                wp_cache_set( 'all_for_user_' . $user_id . $args_cache_key, $notifications, 'bp_notifications' );
    152160        }
    153161
    154162        /**
     
    170178 * @param int    $user_id ID of the user whose notifications are being fetched.
    171179 * @param string $format  Format of the returned values. 'string' returns HTML,
    172180 *                        while 'object' returns a structured object for parsing.
     181 * @param array  $args    Additional arguments to expand notification query.
    173182 * @return mixed Object or array on success, false on failure.
    174183 */
    175 function bp_notifications_get_notifications_for_user( $user_id, $format = 'string' ) {
     184function bp_notifications_get_notifications_for_user( $user_id, $format = 'string', $args = array() ) {
    176185
    177186        // Setup local variables.
    178187        $bp = buddypress();
    179188
    180189        // Get notifications (out of the cache, or query if necessary).
    181         $notifications         = bp_notifications_get_all_notifications_for_user( $user_id );
     190        $notifications         = bp_notifications_get_all_notifications_for_user( $user_id, $args );
    182191        $grouped_notifications = array(); // Notification groups.
    183192        $renderable            = array(); // Renderable notifications.
    184193