bp-activity-cache.php 2.79 KB
Newer Older
lechuck's avatar
lechuck committed
1
2
<?php
/**
lechuck's avatar
lechuck committed
3
 * Functions related to the BuddyPress Activity component and the WP Cache.
lechuck's avatar
lechuck committed
4
 *
lechuck's avatar
lechuck committed
5
6
7
 * @package BuddyPress
 * @subpackage ActivityCache
 * @since 1.6.0
lechuck's avatar
lechuck committed
8
9
 */

lechuck's avatar
lechuck committed
10
// Exit if accessed directly.
ale's avatar
ale committed
11
defined( 'ABSPATH' ) || exit;
lechuck's avatar
lechuck committed
12
13

/**
lechuck's avatar
lechuck committed
14
 * Slurp up activitymeta for a specified set of activity items.
lechuck's avatar
lechuck committed
15
 *
lechuck's avatar
lechuck committed
16
17
18
 * It grabs all activitymeta associated with all of the activity items passed
 * in $activity_ids and adds it to the WP cache. This improves efficiency when
 * using querying activitymeta inline.
lechuck's avatar
lechuck committed
19
 *
lechuck's avatar
lechuck committed
20
21
 * @since 1.6.0
 *
ale's avatar
ale committed
22
 * @param int|string|array|bool $activity_ids Accepts a single activity ID, or a comma-
lechuck's avatar
lechuck committed
23
 *                                            separated list or array of activity ids.
lechuck's avatar
lechuck committed
24
25
 */
function bp_activity_update_meta_cache( $activity_ids = false ) {
ale's avatar
ale committed
26
	$bp = buddypress();
lechuck's avatar
lechuck committed
27
28
29
30
31

	$cache_args = array(
		'object_ids' 	   => $activity_ids,
		'object_type' 	   => $bp->activity->id,
		'object_column'    => 'activity_id',
lechuck's avatar
lechuck committed
32
		'cache_group'      => 'activity_meta',
lechuck's avatar
lechuck committed
33
34
35
		'meta_table' 	   => $bp->activity->table_name_meta,
		'cache_key_prefix' => 'bp_activity_meta'
	);
lechuck's avatar
lechuck committed
36

lechuck's avatar
lechuck committed
37
38
	bp_update_meta_cache( $cache_args );
}
lechuck's avatar
lechuck committed
39
40

/**
lechuck's avatar
lechuck committed
41
42
 * Clear a cached activity item when that item is updated.
 *
lechuck's avatar
lechuck committed
43
 * @since 2.0.0
lechuck's avatar
lechuck committed
44
 *
lechuck's avatar
lechuck committed
45
 * @param BP_Activity_Activity $activity Activity object.
lechuck's avatar
lechuck committed
46
 */
lechuck's avatar
lechuck committed
47
48
function bp_activity_clear_cache_for_activity( $activity ) {
	wp_cache_delete( $activity->id, 'bp_activity' );
ale's avatar
ale committed
49
	wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
lechuck's avatar
lechuck committed
50
51
}
add_action( 'bp_activity_after_save', 'bp_activity_clear_cache_for_activity' );
lechuck's avatar
lechuck committed
52

lechuck's avatar
lechuck committed
53
54
55
/**
 * Clear cached data for deleted activity items.
 *
lechuck's avatar
lechuck committed
56
 * @since 2.0.0
lechuck's avatar
lechuck committed
57
58
59
60
61
62
 *
 * @param array $deleted_ids IDs of deleted activity items.
 */
function bp_activity_clear_cache_for_deleted_activity( $deleted_ids ) {
	foreach ( (array) $deleted_ids as $deleted_id ) {
		wp_cache_delete( $deleted_id, 'bp_activity' );
lechuck's avatar
lechuck committed
63
64
	}
}
lechuck's avatar
lechuck committed
65
add_action( 'bp_activity_deleted_activities', 'bp_activity_clear_cache_for_deleted_activity' );
lucha's avatar
lucha committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

/**
 * Reset cache incrementor for the Activity component.
 *
 * Called whenever an activity item is created, updated, or deleted, this
 * function effectively invalidates all cached results of activity queries.
 *
 * @since 2.7.0
 *
 * @return bool True on success, false on failure.
 */
function bp_activity_reset_cache_incrementor() {
	$without_last_activity = bp_core_reset_incrementor( 'bp_activity' );
	$with_last_activity    = bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
	return $without_last_activity && $with_last_activity;
}
add_action( 'bp_activity_delete',    'bp_activity_reset_cache_incrementor' );
add_action( 'bp_activity_add',       'bp_activity_reset_cache_incrementor' );
add_action( 'added_activity_meta',   'bp_activity_reset_cache_incrementor' );
add_action( 'updated_activity_meta', 'bp_activity_reset_cache_incrementor' );
add_action( 'deleted_activity_meta', 'bp_activity_reset_cache_incrementor' );