[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Friends Caching.
   4   *
   5   * Caching functions handle the clearing of cached objects and pages on specific
   6   * actions throughout BuddyPress.
   7   *
   8   * @package BuddyPress
   9   * @subpackage FriendsCaching
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Clear friends-related cache for members of a specific friendship.
  18   *
  19   * @since 1.0.0
  20   *
  21   * @param int $friendship_id ID of the friendship whose two members should
  22   *                           have their friends cache busted.
  23   * @return bool
  24   */
  25  function friends_clear_friend_object_cache( $friendship_id ) {
  26      if ( !$friendship = new BP_Friends_Friendship( $friendship_id ) )
  27          return false;
  28  
  29      wp_cache_delete( 'friends_friend_ids_' .    $friendship->initiator_user_id, 'bp' );
  30      wp_cache_delete( 'friends_friend_ids_' .    $friendship->friend_user_id,    'bp' );
  31  }
  32  
  33  // List actions to clear object caches on.
  34  add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
  35  add_action( 'friends_friendship_deleted',  'friends_clear_friend_object_cache' );
  36  
  37  /**
  38   * Clear friendship caches on friendship changes.
  39   *
  40   * @since 2.7.0
  41   *
  42   * @param int $friendship_id     ID of the friendship that has changed.
  43   * @param int $initiator_user_id ID of the first user.
  44   * @param int $friend_user_id    ID of the second user.
  45   * @return bool
  46   */
  47  function bp_friends_clear_bp_friends_friendships_cache( $friendship_id, $initiator_user_id, $friend_user_id ) {
  48      // Clear friendship ID cache for each user.
  49      wp_cache_delete( $initiator_user_id, 'bp_friends_friendships_for_user' );
  50      wp_cache_delete( $friend_user_id,    'bp_friends_friendships_for_user' );
  51  
  52      // Clear the friendship object cache.
  53      wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
  54  
  55      // Clear incremented cache.
  56      $friendship = new stdClass;
  57      $friendship->initiator_user_id = $initiator_user_id;
  58      $friendship->friend_user_id    = $friend_user_id;
  59      bp_friends_delete_cached_friendships_on_friendship_save( $friendship );
  60  }
  61  add_action( 'friends_friendship_requested', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  62  add_action( 'friends_friendship_accepted',  'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  63  add_action( 'friends_friendship_deleted',   'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  64  
  65  /**
  66   * Clear friendship caches on friendship changes.
  67   *
  68   * @since 2.7.0
  69   *
  70   * @param int                   $friendship_id The friendship ID.
  71   * @param BP_Friends_Friendship $friendship Friendship object.
  72   */
  73  function bp_friends_clear_bp_friends_friendships_cache_remove( $friendship_id, BP_Friends_Friendship $friendship ) {
  74      // Clear friendship ID cache for each user.
  75      wp_cache_delete( $friendship->initiator_user_id, 'bp_friends_friendships_for_user' );
  76      wp_cache_delete( $friendship->friend_user_id,    'bp_friends_friendships_for_user' );
  77  
  78      // Clear the friendship object cache.
  79      wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
  80  
  81      // Clear incremented cache.
  82      bp_friends_delete_cached_friendships_on_friendship_save( $friendship );
  83  }
  84  add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
  85  add_action( 'friends_friendship_rejected',  'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
  86  
  87  /**
  88   * Clear the friend request cache for the user not initiating the friendship.
  89   *
  90   * @since 2.0.0
  91   *
  92   * @param int $friend_user_id The user ID not initiating the friendship.
  93   */
  94  function bp_friends_clear_request_cache( $friend_user_id ) {
  95      wp_cache_delete( $friend_user_id, 'bp_friends_requests' );
  96  }
  97  
  98  /**
  99   * Clear the friend request cache when a friendship is saved.
 100   *
 101   * A friendship is deemed saved when a friendship is requested or accepted.
 102   *
 103   * @since 2.0.0
 104   *
 105   * @param int $friendship_id     The friendship ID.
 106   * @param int $initiator_user_id The user ID initiating the friendship.
 107   * @param int $friend_user_id    The user ID not initiating the friendship.
 108   */
 109  function bp_friends_clear_request_cache_on_save( $friendship_id, $initiator_user_id, $friend_user_id ) {
 110      bp_friends_clear_request_cache( $friend_user_id );
 111  }
 112  add_action( 'friends_friendship_requested', 'bp_friends_clear_request_cache_on_save', 10, 3 );
 113  add_action( 'friends_friendship_accepted',  'bp_friends_clear_request_cache_on_save', 10, 3 );
 114  
 115  /**
 116   * Clear the friend request cache when a friendship is removed.
 117   *
 118   * A friendship is deemed removed when a friendship is withdrawn or rejected.
 119   *
 120   * @since 2.0.0
 121   *
 122   * @param int                   $friendship_id The friendship ID.
 123   * @param BP_Friends_Friendship $friendship Friendship object.
 124   */
 125  function bp_friends_clear_request_cache_on_remove( $friendship_id, BP_Friends_Friendship $friendship ) {
 126      bp_friends_clear_request_cache( $friendship->friend_user_id );
 127  }
 128  add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_request_cache_on_remove', 10, 2 );
 129  add_action( 'friends_friendship_rejected',  'bp_friends_clear_request_cache_on_remove', 10, 2 );
 130  
 131  /**
 132   * Delete individual friendships from the cache when they are changed.
 133   *
 134   * @since 3.0.0
 135   *
 136   * @param BP_Friends_Friendship $friendship Friendship object.
 137   */
 138  function bp_friends_delete_cached_friendships_on_friendship_save( $friendship ) {
 139      bp_core_delete_incremented_cache( $friendship->friend_user_id . ':' . $friendship->initiator_user_id, 'bp_friends' );
 140      bp_core_delete_incremented_cache( $friendship->initiator_user_id . ':' . $friendship->friend_user_id, 'bp_friends' );
 141  }
 142  add_action( 'friends_friendship_after_save', 'bp_friends_delete_cached_friendships_on_friendship_save' );
 143  
 144  // List actions to clear super cached pages on, if super cache is installed.
 145  add_action( 'friends_friendship_rejected',  'bp_core_clear_cache' );
 146  add_action( 'friends_friendship_accepted',  'bp_core_clear_cache' );
 147  add_action( 'friends_friendship_deleted',   'bp_core_clear_cache' );
 148  add_action( 'friends_friendship_requested', 'bp_core_clear_cache' );


Generated: Thu Sep 24 01:03:44 2020 Cross-referenced by PHPXref 0.7.1