[ 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      $friendship = new BP_Friends_Friendship( $friendship_id );
  27      if ( ! $friendship ) {
  28          return false;
  29      }
  30  
  31      wp_cache_delete( 'friends_friend_ids_' . $friendship->initiator_user_id, 'bp' );
  32      wp_cache_delete( 'friends_friend_ids_' . $friendship->friend_user_id, 'bp' );
  33  }
  34  
  35  // List actions to clear object caches on.
  36  add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' );
  37  add_action( 'friends_friendship_deleted', 'friends_clear_friend_object_cache' );
  38  
  39  /**
  40   * Clear friendship caches on friendship changes.
  41   *
  42   * @since 2.7.0
  43   *
  44   * @param int $friendship_id     ID of the friendship that has changed.
  45   * @param int $initiator_user_id ID of the first user.
  46   * @param int $friend_user_id    ID of the second user.
  47   */
  48  function bp_friends_clear_bp_friends_friendships_cache( $friendship_id, $initiator_user_id, $friend_user_id ) {
  49      // Clear friendship ID cache for each user.
  50      wp_cache_delete( $initiator_user_id, 'bp_friends_friendships_for_user' );
  51      wp_cache_delete( $friend_user_id, 'bp_friends_friendships_for_user' );
  52  
  53      // Clear the friendship object cache.
  54      wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
  55  
  56      // Clear incremented cache.
  57      $friendship = new stdClass;
  58      $friendship->initiator_user_id = $initiator_user_id;
  59      $friendship->friend_user_id    = $friend_user_id;
  60      bp_friends_delete_cached_friendships_on_friendship_save( $friendship );
  61  }
  62  add_action( 'friends_friendship_requested', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  63  add_action( 'friends_friendship_accepted', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  64  add_action( 'friends_friendship_deleted', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 );
  65  
  66  /**
  67   * Clear friendship caches on friendship changes.
  68   *
  69   * @since 2.7.0
  70   *
  71   * @param int                   $friendship_id The friendship ID.
  72   * @param BP_Friends_Friendship $friendship    The friendship object.
  73   */
  74  function bp_friends_clear_bp_friends_friendships_cache_remove( $friendship_id, $friendship ) {
  75      // Clear friendship ID cache for each user.
  76      wp_cache_delete( $friendship->initiator_user_id, 'bp_friends_friendships_for_user' );
  77      wp_cache_delete( $friendship->friend_user_id, 'bp_friends_friendships_for_user' );
  78  
  79      // Clear the friendship object cache.
  80      wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
  81  
  82      // Clear incremented cache.
  83      bp_friends_delete_cached_friendships_on_friendship_save( $friendship );
  84  }
  85  add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
  86  add_action( 'friends_friendship_rejected', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 );
  87  
  88  /**
  89   * Clear the friend request cache for the user not initiating the friendship.
  90   *
  91   * @since 2.0.0
  92   *
  93   * @param int $friend_user_id The user ID not initiating the friendship.
  94   */
  95  function bp_friends_clear_request_cache( $friend_user_id ) {
  96      wp_cache_delete( $friend_user_id, 'bp_friends_requests' );
  97  }
  98  
  99  /**
 100   * Clear the friend request cache when a friendship is saved.
 101   *
 102   * A friendship is deemed saved when a friendship is requested or accepted.
 103   *
 104   * @since 2.0.0
 105   *
 106   * @param int $friendship_id     The friendship ID.
 107   * @param int $initiator_user_id The user ID initiating the friendship.
 108   * @param int $friend_user_id    The user ID not initiating the friendship.
 109   */
 110  function bp_friends_clear_request_cache_on_save( $friendship_id, $initiator_user_id, $friend_user_id ) {
 111      bp_friends_clear_request_cache( $friend_user_id );
 112  }
 113  add_action( 'friends_friendship_requested', 'bp_friends_clear_request_cache_on_save', 10, 3 );
 114  add_action( 'friends_friendship_accepted', 'bp_friends_clear_request_cache_on_save', 10, 3 );
 115  
 116  /**
 117   * Clear the friend request cache when a friendship is removed.
 118   *
 119   * A friendship is deemed removed when a friendship is withdrawn or rejected.
 120   *
 121   * @since 2.0.0
 122   *
 123   * @param int                   $friendship_id The friendship ID.
 124   * @param BP_Friends_Friendship $friendship    The friendship object.
 125   */
 126  function bp_friends_clear_request_cache_on_remove( $friendship_id, $friendship ) {
 127      bp_friends_clear_request_cache( $friendship->friend_user_id );
 128  }
 129  add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_request_cache_on_remove', 10, 2 );
 130  add_action( 'friends_friendship_rejected', 'bp_friends_clear_request_cache_on_remove', 10, 2 );
 131  
 132  /**
 133   * Delete individual friendships from the cache when they are changed.
 134   *
 135   * @since 3.0.0
 136   *
 137   * @param BP_Friends_Friendship $friendship The friendship object.
 138   */
 139  function bp_friends_delete_cached_friendships_on_friendship_save( $friendship ) {
 140      bp_core_delete_incremented_cache( $friendship->friend_user_id . ':' . $friendship->initiator_user_id, 'bp_friends' );
 141      bp_core_delete_incremented_cache( $friendship->initiator_user_id . ':' . $friendship->friend_user_id, 'bp_friends' );
 142  }
 143  add_action( 'friends_friendship_after_save', 'bp_friends_delete_cached_friendships_on_friendship_save' );
 144  
 145  // List actions to clear super cached pages on, if super cache is installed.
 146  add_action( 'friends_friendship_rejected',  'bp_core_clear_cache' );
 147  add_action( 'friends_friendship_accepted',  'bp_core_clear_cache' );
 148  add_action( 'friends_friendship_deleted',   'bp_core_clear_cache' );
 149  add_action( 'friends_friendship_requested', 'bp_core_clear_cache' );


Generated: Wed Oct 20 01:01:02 2021 Cross-referenced by PHPXref 0.7.1