[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Caching functions specific to BuddyPress Members.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersCache
   7   * @since 2.2.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Pre-fetch member type data when initializing a Members loop.
  15   *
  16   * @since 2.2.0
  17   *
  18   * @param BP_User_Query $bp_user_query BP_User_Query object.
  19   */
  20  function bp_members_prefetch_member_type( BP_User_Query $bp_user_query ) {
  21      $uncached_member_ids = bp_get_non_cached_ids( $bp_user_query->user_ids, 'bp_member_member_type' );
  22  
  23      $member_types = bp_get_object_terms( $uncached_member_ids, bp_get_member_type_tax_name(), array(
  24          'fields' => 'all_with_object_id',
  25      ) );
  26  
  27      // Rekey by user ID.
  28      $keyed_member_types = array();
  29      foreach ( $member_types as $member_type ) {
  30          if ( ! isset( $keyed_member_types[ $member_type->object_id ] ) ) {
  31              $keyed_member_types[ $member_type->object_id ] = array();
  32          }
  33  
  34          $keyed_member_types[ $member_type->object_id ][] = $member_type->name;
  35      }
  36  
  37      $cached_member_ids = array();
  38      foreach ( $keyed_member_types as $user_id => $user_member_types ) {
  39          wp_cache_set( $user_id, $user_member_types, 'bp_member_member_type' );
  40          $cached_member_ids[] = $user_id;
  41      }
  42  
  43      // Cache an empty value for users with no type.
  44      foreach ( array_diff( $uncached_member_ids, $cached_member_ids ) as $no_type_id ) {
  45          wp_cache_set( $no_type_id, '', 'bp_member_member_type' );
  46      }
  47  }
  48  add_action( 'bp_user_query_populate_extras', 'bp_members_prefetch_member_type' );
  49  
  50  /**
  51   * Clear the member_type cache for a user.
  52   *
  53   * Called when the user is deleted or marked as spam.
  54   *
  55   * @since 2.2.0
  56   *
  57   * @param int $user_id ID of the deleted user.
  58   */
  59  function bp_members_clear_member_type_cache( $user_id ) {
  60      wp_cache_delete( $user_id, 'bp_member_member_type' );
  61  }
  62  add_action( 'wpmu_delete_user', 'bp_members_clear_member_type_cache' );
  63  add_action( 'delete_user', 'bp_members_clear_member_type_cache' );
  64  
  65  /**
  66   * Invalidate activity caches when a user's last_activity value is changed.
  67   *
  68   * @since 2.7.0
  69   *
  70   * @return bool True on success, false on failure.
  71   */
  72  function bp_members_reset_activity_cache_incrementor() {
  73      return bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
  74  }
  75  add_action( 'bp_core_user_updated_last_activity', 'bp_members_reset_activity_cache_incrementor' );


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1