[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Functions related to the BuddyPress Activity component and the WP Cache.
   4   *
   5   * @package BuddyPress
   6   * @subpackage ActivityCache
   7   * @since 1.6.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Slurp up activitymeta for a specified set of activity items.
  15   *
  16   * It grabs all activitymeta associated with all of the activity items passed
  17   * in $activity_ids and adds it to the WP cache. This improves efficiency when
  18   * using querying activitymeta inline.
  19   *
  20   * @since 1.6.0
  21   *
  22   * @param int|string|array|bool $activity_ids Accepts a single activity ID, or a comma-
  23   *                                            separated list or array of activity ids.
  24   */
  25  function bp_activity_update_meta_cache( $activity_ids = false ) {
  26      $bp = buddypress();
  27  
  28      $cache_args = array(
  29          'object_ids'        => $activity_ids,
  30          'object_type'        => $bp->activity->id,
  31          'object_column'    => 'activity_id',
  32          'cache_group'      => 'activity_meta',
  33          'meta_table'        => $bp->activity->table_name_meta,
  34          'cache_key_prefix' => 'bp_activity_meta'
  35      );
  36  
  37      bp_update_meta_cache( $cache_args );
  38  }
  39  
  40  /**
  41   * Clear a cached activity item when that item is updated.
  42   *
  43   * @since 2.0.0
  44   *
  45   * @param BP_Activity_Activity $activity Activity object.
  46   */
  47  function bp_activity_clear_cache_for_activity( $activity ) {
  48      wp_cache_delete( $activity->id, 'bp_activity' );
  49      wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
  50  
  51      // Clear the comments cache for the parent activity ID.
  52      if ( 'activity_comment' === $activity->type ) {
  53          wp_cache_delete( $activity->item_id, 'bp_activity_comments' );
  54      }
  55  }
  56  add_action( 'bp_activity_after_save', 'bp_activity_clear_cache_for_activity' );
  57  
  58  /**
  59   * Clear cached data for deleted activity items.
  60   *
  61   * @since 2.0.0
  62   *
  63   * @param array $deleted_ids IDs of deleted activity items.
  64   */
  65  function bp_activity_clear_cache_for_deleted_activity( $deleted_ids ) {
  66      foreach ( (array) $deleted_ids as $deleted_id ) {
  67          wp_cache_delete( $deleted_id, 'bp_activity' );
  68      }
  69  }
  70  add_action( 'bp_activity_deleted_activities', 'bp_activity_clear_cache_for_deleted_activity' );
  71  
  72  /**
  73   * Reset cache incrementor for the Activity component.
  74   *
  75   * Called whenever an activity item is created, updated, or deleted, this
  76   * function effectively invalidates all cached results of activity queries.
  77   *
  78   * @since 2.7.0
  79   *
  80   * @return bool True on success, false on failure.
  81   */
  82  function bp_activity_reset_cache_incrementor() {
  83      $without_last_activity = bp_core_reset_incrementor( 'bp_activity' );
  84      $with_last_activity    = bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
  85      return $without_last_activity && $with_last_activity;
  86  }
  87  add_action( 'bp_activity_delete',    'bp_activity_reset_cache_incrementor' );
  88  add_action( 'bp_activity_add',       'bp_activity_reset_cache_incrementor' );
  89  add_action( 'added_activity_meta',   'bp_activity_reset_cache_incrementor' );
  90  add_action( 'updated_activity_meta', 'bp_activity_reset_cache_incrementor' );
  91  add_action( 'deleted_activity_meta', 'bp_activity_reset_cache_incrementor' );


Generated: Sun May 22 01:00:58 2022 Cross-referenced by PHPXref 0.7.1