Skip to:
Content

BuddyPress.org

Changeset 9887


Ignore:
Timestamp:
05/21/2015 07:56:59 PM (5 years ago)
Author:
johnjamesjacoby
Message:

Notifications: Improve all_for_user cache deletion.

  • Introduce bp_notification_before_update action which fires before a Notification is updated
  • Introduce bp_notifications_clear_all_for_user_cache() as wrapper for wp_cache_delete() with appropriate cache group and key
  • Introduce bp_notifications_clear_all_for_user_cache_before_update() hooked to bp_notification_before_update action, to handle cache deletion before a notification is updated

This change fixes a bug where marking single notifications as read/unread would not show anticipated results, as the all_for_user cache was not being properly cleared.

Props r-a-y. Fixes #6445.

Location:
trunk/src/bp-notifications
Files:
2 edited

Legend:

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

    r9819 r9887  
    3232
    3333/**
     34 * Clear all notifications cache for a given user ID
     35 *
     36 * @since BuddyPress (2.3.0)
     37 *
     38 * @param int $user_id The user ID's cache to clear
     39 */
     40function bp_notifications_clear_all_for_user_cache( $user_id = 0 ) {
     41    wp_cache_delete( 'all_for_user_' . $user_id, 'bp_notifications' );
     42}
     43
     44/**
    3445 * Invalidate 'all_for_user_' cache when saving.
    3546 *
     
    3950 */
    4051function bp_notifications_clear_all_for_user_cache_after_save( BP_Notifications_Notification $n ) {
    41     wp_cache_delete( 'all_for_user_' . $n->user_id, 'bp_notifications' );
     52    bp_notifications_clear_all_for_user_cache( $n->user_id );
    4253}
    4354add_action( 'bp_notification_after_save', 'bp_notifications_clear_all_for_user_cache_after_save' );
     
    5162 */
    5263function bp_notifications_clear_all_for_user_cache_before_delete( $args ) {
     64
    5365    // Pull up a list of items matching the args (those about te be deleted)
    5466    $ns = BP_Notifications_Notification::get( $args );
     
    5971    }
    6072
    61     foreach ( array_unique( $user_ids ) as $user_id ) {
    62         wp_cache_delete( 'all_for_user_' . $user_id, 'bp_notifications' );
     73    $user_ids = array_unique( $user_ids );
     74    foreach ( $user_ids as $user_id ) {
     75        bp_notifications_clear_all_for_user_cache( $user_id );
    6376    }
    6477}
    6578add_action( 'bp_notification_before_delete', 'bp_notifications_clear_all_for_user_cache_before_delete' );
     79
     80/**
     81 * Invalidates 'all_for_user_' cache when updating.
     82 *
     83 * @since BuddyPress (2.3.0)
     84 *
     85 * @param array $update_args See BP_Notifications_Notification::update() for description.
     86 * @param array $where_args  See BP_Notifications_Notification::update() for description.
     87 */
     88function bp_notifications_clear_all_for_user_cache_before_update( $update_args, $where_args ) {
     89
     90    // User ID is passed in where arugments
     91    if ( ! empty( $where_args['user_id'] ) ) {
     92        bp_notifications_clear_all_for_user_cache( $where_args['user_id'] );
     93
     94    // Get user ID from Notification ID
     95    } elseif ( ! empty( $where_args['id'] ) ) {
     96        $n = bp_notifications_get_notification( $where_args['id'] );
     97        bp_notifications_clear_all_for_user_cache( $n->user_id );
     98    }
     99}
     100add_action( 'bp_notification_before_update', 'bp_notifications_clear_all_for_user_cache_before_update', 10, 2 );
  • trunk/src/bp-notifications/classes/class-bp-notifications-notification.php

    r9819 r9887  
    831831        $where  = self::get_query_clauses( $where_args  );
    832832
    833         // make sure we delete the notification cache for the user on update
    834         if ( ! empty( $where_args['user_id'] ) ) {
    835             wp_cache_delete( 'all_for_user_' . $where_args['user_id'], 'bp_notifications' );
    836         }
    837 
    838         return self::_update( $update['data'], $where['data'], $update['format'], $where['format'] );
     833        /**
     834         * Fires before the update of a notification item.
     835         *
     836         * @since BuddyPress (2.3.0)
     837         *
     838         * @param array $update_args See BP_Notifications_Notification::update().
     839         * @param array $where_args  See BP_Notifications_Notification::update().
     840         */
     841        do_action( 'bp_notification_before_update', $update_args, $where_args );
     842
     843        return self::_update(
     844            $update['data'],
     845            $where['data'],
     846            $update['format'],
     847            $where['format']
     848        );
    839849    }
    840850
Note: See TracChangeset for help on using the changeset viewer.