/bp-friends/bp-friends-cache.php

  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. // List actions to clear object caches on. 
  33. add_action( 'friends_friendship_accepted', 'friends_clear_friend_object_cache' ); 
  34. add_action( 'friends_friendship_deleted', 'friends_clear_friend_object_cache' ); 
  35.  
  36. /** 
  37. * Clear friendship caches on friendship changes. 
  38. * 
  39. * @since 2.7.0 
  40. * 
  41. * @param int $friendship_id ID of the friendship that has changed. 
  42. * @param int $initiator_user_id ID of the first user. 
  43. * @param int $friend_user_id ID of the second user. 
  44. * @return bool 
  45. */ 
  46. function bp_friends_clear_bp_friends_friendships_cache( $friendship_id, $initiator_user_id, $friend_user_id ) { 
  47. // Clear friendship ID cache for each user. 
  48. wp_cache_delete( $initiator_user_id, 'bp_friends_friendships_for_user' ); 
  49. wp_cache_delete( $friend_user_id, 'bp_friends_friendships_for_user' ); 
  50.  
  51. // Clear the friendship object cache. 
  52. wp_cache_delete( $friendship_id, 'bp_friends_friendships' ); 
  53. add_action( 'friends_friendship_requested', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 ); 
  54. add_action( 'friends_friendship_accepted', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 ); 
  55. add_action( 'friends_friendship_deleted', 'bp_friends_clear_bp_friends_friendships_cache', 10, 3 ); 
  56.  
  57. /** 
  58. * Clear friendship caches on friendship changes. 
  59. * 
  60. * @since 2.7.0 
  61. * 
  62. * @param int $friendship_id The friendship ID. 
  63. * @param BP_Friends_Friendship $friendship Friendship object. 
  64. */ 
  65. function bp_friends_clear_bp_friends_friendships_cache_remove( $friendship_id, BP_Friends_Friendship $friendship ) { 
  66. // Clear friendship ID cache for each user. 
  67. wp_cache_delete( $friendship->initiator_user_id, 'bp_friends_friendships_for_user' ); 
  68. wp_cache_delete( $friendship->friend_user_id, 'bp_friends_friendships_for_user' ); 
  69.  
  70. // Clear the friendship object cache. 
  71. wp_cache_delete( $friendship_id, 'bp_friends_friendships' ); 
  72. add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 ); 
  73. add_action( 'friends_friendship_rejected', 'bp_friends_clear_bp_friends_friendships_cache_remove', 10, 2 ); 
  74.  
  75. /** 
  76. * Clear the friend request cache for the user not initiating the friendship. 
  77. * 
  78. * @since 2.0.0 
  79. * 
  80. * @param int $friend_user_id The user ID not initiating the friendship. 
  81. */ 
  82. function bp_friends_clear_request_cache( $friend_user_id ) { 
  83. wp_cache_delete( $friend_user_id, 'bp_friends_requests' ); 
  84.  
  85. /** 
  86. * Clear the friend request cache when a friendship is saved. 
  87. * 
  88. * A friendship is deemed saved when a friendship is requested or accepted. 
  89. * 
  90. * @since 2.0.0 
  91. * 
  92. * @param int $friendship_id The friendship ID. 
  93. * @param int $initiator_user_id The user ID initiating the friendship. 
  94. * @param int $friend_user_id The user ID not initiating the friendship. 
  95. */ 
  96. function bp_friends_clear_request_cache_on_save( $friendship_id, $initiator_user_id, $friend_user_id ) { 
  97. bp_friends_clear_request_cache( $friend_user_id ); 
  98. add_action( 'friends_friendship_requested', 'bp_friends_clear_request_cache_on_save', 10, 3 ); 
  99. add_action( 'friends_friendship_accepted', 'bp_friends_clear_request_cache_on_save', 10, 3 ); 
  100.  
  101. /** 
  102. * Clear the friend request cache when a friendship is removed. 
  103. * 
  104. * A friendship is deemed removed when a friendship is withdrawn or rejected. 
  105. * 
  106. * @since 2.0.0 
  107. * 
  108. * @param int $friendship_id The friendship ID. 
  109. * @param BP_Friends_Friendship $friendship Friendship object. 
  110. */ 
  111. function bp_friends_clear_request_cache_on_remove( $friendship_id, BP_Friends_Friendship $friendship ) { 
  112. bp_friends_clear_request_cache( $friendship->friend_user_id ); 
  113. add_action( 'friends_friendship_withdrawn', 'bp_friends_clear_request_cache_on_remove', 10, 2 ); 
  114. add_action( 'friends_friendship_rejected', 'bp_friends_clear_request_cache_on_remove', 10, 2 ); 
  115.  
  116. // List actions to clear super cached pages on, if super cache is installed. 
  117. add_action( 'friends_friendship_rejected', 'bp_core_clear_cache' ); 
  118. add_action( 'friends_friendship_accepted', 'bp_core_clear_cache' ); 
  119. add_action( 'friends_friendship_deleted', 'bp_core_clear_cache' ); 
  120. add_action( 'friends_friendship_requested', 'bp_core_clear_cache' ); 
.