[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups 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 Groups
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Slurp up metadata for a set of groups.
  18   *
  19   * This function is called in two places in the BP_Groups_Group class:
  20   *   - in the populate() method, when single group objects are populated
  21   *   - in the get() method, when multiple groups are queried
  22   *
  23   * It grabs all groupmeta associated with all of the groups passed in
  24   * $group_ids and adds it to WP cache. This improves efficiency when using
  25   * groupmeta within a loop context.
  26   *
  27   * @since 1.6.0
  28   *
  29   * @param int|string|array|bool $group_ids Accepts a single group_id, or a
  30   *                                         comma-separated list or array of
  31   *                                         group ids.
  32   */
  33  function bp_groups_update_meta_cache( $group_ids = false ) {
  34      $bp = buddypress();
  35  
  36      $cache_args = array(
  37          'object_ids'        => $group_ids,
  38          'object_type'        => $bp->groups->id,
  39          'cache_group'      => 'group_meta',
  40          'object_column'    => 'group_id',
  41          'meta_table'        => $bp->groups->table_name_groupmeta,
  42          'cache_key_prefix' => 'bp_groups_groupmeta'
  43      );
  44  
  45      bp_update_meta_cache( $cache_args );
  46  }
  47  
  48  /**
  49   * Clear the cached group count.
  50   *
  51   * @since 1.0.0
  52   *
  53   * @param int $group_id Not used.
  54   */
  55  function groups_clear_group_object_cache( $group_id ) {
  56      wp_cache_delete( 'bp_total_group_count', 'bp' );
  57  }
  58  add_action( 'groups_group_deleted',              'groups_clear_group_object_cache' );
  59  add_action( 'groups_settings_updated',           'groups_clear_group_object_cache' );
  60  add_action( 'groups_details_updated',            'groups_clear_group_object_cache' );
  61  add_action( 'groups_group_avatar_updated',       'groups_clear_group_object_cache' );
  62  add_action( 'groups_create_group_step_complete', 'groups_clear_group_object_cache' );
  63  
  64  /**
  65   * Bust group caches when editing or deleting.
  66   *
  67   * @since 1.7.0
  68   *
  69   * @param int $group_id The group being edited.
  70   */
  71  function bp_groups_delete_group_cache( $group_id = 0 ) {
  72      wp_cache_delete( $group_id, 'bp_groups' );
  73  }
  74  add_action( 'groups_delete_group',     'bp_groups_delete_group_cache' );
  75  add_action( 'groups_update_group',     'bp_groups_delete_group_cache' );
  76  add_action( 'groups_details_updated',  'bp_groups_delete_group_cache' );
  77  add_action( 'groups_settings_updated', 'bp_groups_delete_group_cache' );
  78  
  79  /**
  80   * Bust group cache when modifying metadata.
  81   *
  82   * @since 2.0.0
  83   *
  84   * @param int $meta_id Meta ID.
  85   * @param int $group_id Group ID.
  86   */
  87  function bp_groups_delete_group_cache_on_metadata_change( $meta_id, $group_id ) {
  88      wp_cache_delete( $group_id, 'bp_groups' );
  89  }
  90  add_action( 'updated_group_meta', 'bp_groups_delete_group_cache_on_metadata_change', 10, 2 );
  91  add_action( 'added_group_meta', 'bp_groups_delete_group_cache_on_metadata_change', 10, 2 );
  92  
  93  /**
  94   * Clear caches for the group creator when a group is created.
  95   *
  96   * @since 1.6.0
  97   *
  98   * @param int             $group_id  ID of the group.
  99   * @param BP_Groups_Group $group_obj Group object.
 100   */
 101  function bp_groups_clear_group_creator_cache( $group_id, $group_obj ) {
 102      // Clears the 'total groups' for this user.
 103      groups_clear_group_user_object_cache( $group_obj->id, $group_obj->creator_id );
 104  }
 105  add_action( 'groups_created_group', 'bp_groups_clear_group_creator_cache', 10, 2 );
 106  
 107  /**
 108   * Clears caches for all members in a group when a group is deleted.
 109   *
 110   * @since 1.6.0
 111   *
 112   * @param BP_Groups_Group $group_obj Group object.
 113   * @param array           $user_ids  User IDs who were in this group.
 114   */
 115  function bp_groups_clear_group_members_caches( $group_obj, $user_ids ) {
 116      // Clears the 'total groups' cache for each member in a group.
 117      foreach ( (array) $user_ids as $user_id )
 118          groups_clear_group_user_object_cache( $group_obj->id, $user_id );
 119  }
 120  add_action( 'bp_groups_delete_group', 'bp_groups_clear_group_members_caches', 10, 2 );
 121  
 122  /**
 123   * Clear a user's cached total group invite count.
 124   *
 125   * Count is cleared when an invite is accepted, rejected or deleted.
 126   *
 127   * @since 2.0.0
 128   *
 129   * @param int $user_id The user ID.
 130   */
 131  function bp_groups_clear_invite_count_for_user( $user_id ) {
 132      wp_cache_delete( $user_id, 'bp_group_invite_count' );
 133  }
 134  add_action( 'groups_accept_invite', 'bp_groups_clear_invite_count_for_user' );
 135  add_action( 'groups_reject_invite', 'bp_groups_clear_invite_count_for_user' );
 136  add_action( 'groups_delete_invite', 'bp_groups_clear_invite_count_for_user' );
 137  
 138  /**
 139   * Clear a user's cached total group invite count when a user is uninvited.
 140   *
 141   * Groan. Our API functions are not consistent.
 142   *
 143   * @since 2.0.0
 144   *
 145   * @param int $group_id The group ID. Not used in this function.
 146   * @param int $user_id  The user ID.
 147   */
 148  function bp_groups_clear_invite_count_on_uninvite( $group_id, $user_id ) {
 149      bp_groups_clear_invite_count_for_user( $user_id );
 150  }
 151  add_action( 'groups_uninvite_user', 'bp_groups_clear_invite_count_on_uninvite', 10, 2 );
 152  
 153  /**
 154   * Clear a user's cached total group invite count when a new invite is sent.
 155   *
 156   * @since 2.0.0
 157   *
 158   * @param int   $group_id      The group ID. Not used in this function.
 159   * @param array $invited_users Array of invited user IDs.
 160   */
 161  function bp_groups_clear_invite_count_on_send( $group_id, $invited_users ) {
 162      foreach ( $invited_users as $user_id ) {
 163          bp_groups_clear_invite_count_for_user( $user_id );
 164      }
 165  }
 166  add_action( 'groups_send_invites', 'bp_groups_clear_invite_count_on_send', 10, 2 );
 167  
 168  /**
 169   * Clear a user's cached group count.
 170   *
 171   * @since 1.2.0
 172   *
 173   * @param int $group_id The group ID. Not used in this function.
 174   * @param int $user_id  The user ID.
 175   */
 176  function groups_clear_group_user_object_cache( $group_id, $user_id ) {
 177      wp_cache_delete( 'bp_total_groups_for_user_' . $user_id, 'bp' );
 178  }
 179  add_action( 'groups_join_group',    'groups_clear_group_user_object_cache', 10, 2 );
 180  add_action( 'groups_leave_group',   'groups_clear_group_user_object_cache', 10, 2 );
 181  add_action( 'groups_ban_member',    'groups_clear_group_user_object_cache', 10, 2 );
 182  add_action( 'groups_unban_member',  'groups_clear_group_user_object_cache', 10, 2 );
 183  add_action( 'groups_uninvite_user', 'groups_clear_group_user_object_cache', 10, 2 );
 184  add_action( 'groups_remove_member', 'groups_clear_group_user_object_cache', 10, 2 );
 185  
 186  /**
 187   * Clear group administrator and moderator cache.
 188   *
 189   * @since 2.1.0
 190   *
 191   * @param int $group_id The group ID.
 192   */
 193  function groups_clear_group_administrator_cache( $group_id ) {
 194      wp_cache_delete( $group_id, 'bp_group_admins' );
 195      wp_cache_delete( $group_id, 'bp_group_mods' );
 196  }
 197  add_action( 'groups_promote_member', 'groups_clear_group_administrator_cache' );
 198  add_action( 'groups_demote_member',  'groups_clear_group_administrator_cache' );
 199  add_action( 'groups_delete_group',   'groups_clear_group_administrator_cache' );
 200  
 201  /**
 202   * Clear group administrator and moderator cache when a group member is saved.
 203   *
 204   * This accounts for situations where group admins or mods are added manually
 205   * using {@link BP_Groups_Member::save()}.  Usually via a plugin.
 206   *
 207   * @since 2.1.0
 208   *
 209   * @param BP_Groups_Member $member Member object.
 210   */
 211  function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member $member ) {
 212      groups_clear_group_administrator_cache( $member->group_id );
 213  }
 214  add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
 215  
 216  /**
 217   * Clear group administrator and moderator cache when a group member is deleted.
 218   *
 219   * @since 4.0.0
 220   *
 221   * @param int $user_id  User ID.
 222   * @param int $group_id Group ID.
 223   */
 224  function bp_groups_clear_group_administrator_cache_on_member_delete( $user_id, $group_id ) {
 225      groups_clear_group_administrator_cache( $group_id );
 226  }
 227  add_action( 'bp_groups_member_after_delete', 'bp_groups_clear_group_administrator_cache_on_member_delete', 10, 2 );
 228  
 229  /**
 230   * Clear the group type cache for a group.
 231   *
 232   * Called when group is deleted.
 233   *
 234   * @since 2.6.0
 235   *
 236   * @param int $group_id The group ID.
 237   */
 238  function groups_clear_group_type_cache( $group_id = 0 ) {
 239      wp_cache_delete( $group_id, 'bp_groups_group_type' );
 240  }
 241  add_action( 'groups_delete_group', 'groups_clear_group_type_cache' );
 242  
 243  /**
 244   * Clear caches on membership save.
 245   *
 246   * @since 2.6.0
 247   *
 248   * @param BP_Groups_Member $member BP Groups Member instance.
 249   */
 250  function bp_groups_clear_user_group_cache_on_membership_save( BP_Groups_Member $member ) {
 251      wp_cache_delete( $member->user_id, 'bp_groups_memberships_for_user' );
 252      wp_cache_delete( $member->id, 'bp_groups_memberships' );
 253  }
 254  add_action( 'groups_member_before_save', 'bp_groups_clear_user_group_cache_on_membership_save' );
 255  add_action( 'groups_member_before_remove', 'bp_groups_clear_user_group_cache_on_membership_save' );
 256  
 257  /**
 258   * Clear caches on saving a group invitation or request.
 259   * The save action is called when inserting a new record or using the save() method
 260   * to update an existing record.
 261   *
 262   * @since 5.0.0
 263   *
 264   * @param BP_Invitation object $invitation Characteristics of the invitation just saved.
 265   */
 266  function bp_groups_clear_user_group_cache_on_invitation_save( BP_Invitation $invitation ) {
 267      if ( sanitize_key( 'BP_Groups_Invitation_Manager' ) !== $invitation->class ) {
 268          return;
 269      }
 270  
 271      wp_cache_delete( $invitation->id, 'bp_groups_invitations_as_memberships' );
 272  }
 273  add_action( 'bp_invitation_after_save', 'bp_groups_clear_user_group_cache_on_invitation_save', 10, 2 );
 274  
 275  /**
 276   * Clear caches on invitation deletion or update.
 277   * This also catches changes like sending an invite or marking one as accepted.
 278   *
 279   * @since 5.0.0
 280   *
 281   * @param array $args Associative array of columns/values describing invitations about to be deleted.
 282   */
 283  function bp_groups_clear_user_group_cache_on_invitation_change( $args ) {
 284      $args['fields' ] = 'ids';
 285      $affected_invitation_ids = groups_get_invites( $args );
 286      foreach ( $affected_invitation_ids as $invitation_id ) {
 287          wp_cache_delete( $invitation_id, 'bp_groups_invitations_as_memberships' );
 288      }
 289  }
 290  add_action( 'bp_invitation_before_delete', 'bp_groups_clear_user_group_cache_on_invitation_change' );
 291  add_action( 'bp_invitation_before_update', 'bp_groups_clear_user_group_cache_on_invitation_change' );
 292  
 293  /**
 294   * Clear group memberships cache on miscellaneous actions not covered by the 'after_save' hook.
 295   *
 296   * @since 2.6.0
 297   *
 298   * @param int $user_id  Current user ID.
 299   * @param int $group_id Current group ID.
 300   */
 301  function bp_groups_clear_user_group_cache_on_other_events( $user_id, $group_id ) {
 302      wp_cache_delete( $user_id, 'bp_groups_memberships_for_user' );
 303  
 304      $membership = new BP_Groups_Member( $user_id, $group_id );
 305      wp_cache_delete( $membership->id, 'bp_groups_memberships' );
 306  }
 307  add_action( 'bp_groups_member_before_delete', 'bp_groups_clear_user_group_cache_on_other_events', 10, 2 );
 308  
 309  /**
 310   * Reset cache incrementor for the Groups component.
 311   *
 312   * This function invalidates all cached results of group queries,
 313   * whenever one of the following events takes place:
 314   *   - A group is created or updated.
 315   *   - A group is deleted.
 316   *   - A group's metadata is modified.
 317   *
 318   * @since 2.7.0
 319   *
 320   * @return bool True on success, false on failure.
 321   */
 322  function bp_groups_reset_cache_incrementor() {
 323      return bp_core_reset_incrementor( 'bp_groups' );
 324  }
 325  add_action( 'groups_group_after_save', 'bp_groups_reset_cache_incrementor' );
 326  add_action( 'bp_groups_delete_group',  'bp_groups_reset_cache_incrementor' );
 327  add_action( 'updated_group_meta',      'bp_groups_reset_cache_incrementor' );
 328  add_action( 'deleted_group_meta',      'bp_groups_reset_cache_incrementor' );
 329  add_action( 'added_group_meta',        'bp_groups_reset_cache_incrementor' );
 330  
 331  /**
 332   * Reset cache incrementor for Groups component when a group's taxonomy terms change.
 333   *
 334   * We infer that a group is being affected by looking at the objects belonging
 335   * to the taxonomy being affected.
 336   *
 337   * @since 2.7.0
 338   *
 339   * @param int    $object_id ID of the item whose terms are being modified.
 340   * @param array  $terms     Array of object terms.
 341   * @param array  $tt_ids    Array of term taxonomy IDs.
 342   * @param string $taxonomy  Taxonomy slug.
 343   * @return bool True on success, false on failure.
 344   */
 345  function bp_groups_reset_cache_incrementor_on_group_term_change( $object_id, $terms, $tt_ids, $taxonomy ) {
 346      $tax_object = get_taxonomy( $taxonomy );
 347      if ( $tax_object && in_array( 'bp_group', $tax_object->object_type, true ) ) {
 348          return bp_groups_reset_cache_incrementor();
 349      }
 350  
 351      return false;
 352  }
 353  add_action( 'bp_set_object_terms', 'bp_groups_reset_cache_incrementor_on_group_term_change', 10, 4 );
 354  
 355  /**
 356   * Reset cache incrementor for Groups component when a group's taxonomy terms are removed.
 357   *
 358   * We infer that a group is being affected by looking at the objects belonging
 359   * to the taxonomy being affected.
 360   *
 361   * @since 2.7.0
 362   *
 363   * @param int    $object_id ID of the item whose terms are being modified.
 364   * @param array  $terms     Array of object terms.
 365   * @param string $taxonomy  Taxonomy slug.
 366   * @return bool True on success, false on failure.
 367   */
 368  function bp_groups_reset_cache_incrementor_on_group_term_remove( $object_id, $terms, $taxonomy ) {
 369      $tax_object = get_taxonomy( $taxonomy );
 370      if ( $tax_object && in_array( 'bp_group', $tax_object->object_type, true ) ) {
 371          return bp_groups_reset_cache_incrementor();
 372      }
 373  
 374      return false;
 375  }
 376  add_action( 'bp_remove_object_terms', 'bp_groups_reset_cache_incrementor_on_group_term_remove', 10, 3 );
 377  
 378  /* List actions to clear super cached pages on, if super cache is installed */
 379  add_action( 'groups_join_group',                 'bp_core_clear_cache' );
 380  add_action( 'groups_leave_group',                'bp_core_clear_cache' );
 381  add_action( 'groups_accept_invite',              'bp_core_clear_cache' );
 382  add_action( 'groups_reject_invite',              'bp_core_clear_cache' );
 383  add_action( 'groups_invite_user',                'bp_core_clear_cache' );
 384  add_action( 'groups_uninvite_user',              'bp_core_clear_cache' );
 385  add_action( 'groups_details_updated',            'bp_core_clear_cache' );
 386  add_action( 'groups_settings_updated',           'bp_core_clear_cache' );
 387  add_action( 'groups_unban_member',               'bp_core_clear_cache' );
 388  add_action( 'groups_ban_member',                 'bp_core_clear_cache' );
 389  add_action( 'groups_demote_member',              'bp_core_clear_cache' );
 390  add_action( 'groups_promote_member',             'bp_core_clear_cache' );
 391  add_action( 'groups_membership_rejected',        'bp_core_clear_cache' );
 392  add_action( 'groups_membership_accepted',        'bp_core_clear_cache' );
 393  add_action( 'groups_membership_requested',       'bp_core_clear_cache' );
 394  add_action( 'groups_create_group_step_complete', 'bp_core_clear_cache' );
 395  add_action( 'groups_created_group',              'bp_core_clear_cache' );
 396  add_action( 'groups_group_avatar_updated',       'bp_core_clear_cache' );


Generated: Wed Dec 11 01:01:37 2019 Cross-referenced by PHPXref 0.7.1