[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-notifications/ -> bp-notifications-cache.php (source)

   1  <?php
   2  /**
   3   * Functions related to notifications caching.
   4   *
   5   * @package BuddyPress
   6   * @subpackage NotificationsCache
   7   * @since 2.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Slurp up metadata for a set of notifications.
  15   *
  16   * It grabs all notification meta associated with all of the notifications
  17   * passed in $notification_ids and adds it to WP cache. This improves efficiency
  18   * when using notification meta within a loop context.
  19   *
  20   * @since 2.3.0
  21   *
  22   * @param int|string|array|bool $notification_ids Accepts a single notification_id, or a
  23   *                                                comma-separated list or array of
  24   *                                                notification ids.
  25   */
  26  function bp_notifications_update_meta_cache( $notification_ids = false ) {
  27      bp_update_meta_cache( array(
  28          'object_ids'       => $notification_ids,
  29          'object_type'      => buddypress()->notifications->id,
  30          'cache_group'      => 'notification_meta',
  31          'object_column'    => 'notification_id',
  32          'meta_table'       => buddypress()->notifications->table_name_meta,
  33          'cache_key_prefix' => 'bp_notifications_meta',
  34      ) );
  35  }
  36  
  37  /**
  38   * Clear all notifications cache for a given user ID.
  39   *
  40   * @since 2.3.0
  41   *
  42   * @param int $user_id The user ID's cache to clear.
  43   */
  44  function bp_notifications_clear_all_for_user_cache( $user_id = 0 ) {
  45      wp_cache_delete( 'all_for_user_' . $user_id, 'bp_notifications' );
  46      wp_cache_delete( $user_id, 'bp_notifications_unread_count' );
  47      wp_cache_delete( $user_id, 'bp_notifications_grouped_notifications' );
  48  }
  49  
  50  /**
  51   * Invalidate 'all_for_user_' cache when saving.
  52   *
  53   * @since 2.0.0
  54   *
  55   * @param BP_Notifications_Notification $notification Notification object.
  56   */
  57  function bp_notifications_clear_all_for_user_cache_after_save( $notification ) {
  58      bp_notifications_clear_all_for_user_cache( $notification->user_id );
  59  }
  60  add_action( 'bp_notification_after_save', 'bp_notifications_clear_all_for_user_cache_after_save' );
  61  
  62  /**
  63   * Invalidate the 'all_for_user_' cache when deleting.
  64   *
  65   * @since 2.0.0
  66   *
  67   * @param int $args Notification deletion arguments.
  68   */
  69  function bp_notifications_clear_all_for_user_cache_before_delete( $args ) {
  70  
  71      // Pull up a list of items matching the args (those about te be deleted).
  72      $ns = BP_Notifications_Notification::get( $args );
  73  
  74      $user_ids = array();
  75      foreach ( $ns as $n ) {
  76          $user_ids[] = $n->user_id;
  77      }
  78  
  79      $user_ids = array_unique( $user_ids );
  80      foreach ( $user_ids as $user_id ) {
  81          bp_notifications_clear_all_for_user_cache( $user_id );
  82      }
  83  }
  84  add_action( 'bp_notification_before_delete', 'bp_notifications_clear_all_for_user_cache_before_delete' );
  85  
  86  /**
  87   * Invalidates 'all_for_user_' cache when updating.
  88   *
  89   * @since 2.3.0
  90   *
  91   * @param array $update_args See BP_Notifications_Notification::update() for description.
  92   * @param array $where_args  See BP_Notifications_Notification::update() for description.
  93   */
  94  function bp_notifications_clear_all_for_user_cache_before_update( $update_args, $where_args ) {
  95  
  96      // User ID is passed in where arguments.
  97      if ( ! empty( $where_args['user_id'] ) ) {
  98          bp_notifications_clear_all_for_user_cache( $where_args['user_id'] );
  99  
 100      // Get user ID from Notification ID.
 101      } elseif ( ! empty( $where_args['id'] ) ) {
 102          $n = bp_notifications_get_notification( $where_args['id'] );
 103          bp_notifications_clear_all_for_user_cache( $n->user_id );
 104  
 105          // Get the list of user IDs from notification IDs.
 106      } elseif ( isset( $where_args['ids'] ) && $where_args['ids'] ) {
 107          $ids    = (array) $where_args['ids'];
 108          $is_new = 1;
 109  
 110          if ( isset( $update_args['data']['is_new'] ) && 1 === $update_args['data']['is_new'] ) {
 111              $is_new = 0;
 112          }
 113  
 114          $ns = BP_Notifications_Notification::get(
 115              array(
 116                  'id'      => $ids,
 117                  'is_new'  => $is_new,
 118              )
 119          );
 120  
 121          $user_ids = wp_list_pluck( $ns, 'user_id' );
 122          $user_ids = array_unique( $user_ids );
 123  
 124          foreach ( $user_ids as $user_id ) {
 125              bp_notifications_clear_all_for_user_cache( $user_id );
 126          }
 127      }
 128  }
 129  add_action( 'bp_notification_before_update', 'bp_notifications_clear_all_for_user_cache_before_update', 10, 2 );


Generated: Sat Apr 20 01:00:58 2024 Cross-referenced by PHPXref 0.7.1