bp-activity-akismet.php 1.67 KB
Newer Older
lechuck's avatar
lechuck committed
1
2
<?php
/**
lechuck's avatar
lechuck committed
3
 * Akismet support for BuddyPress' Activity Stream.
lechuck's avatar
lechuck committed
4
5
 *
 * @package BuddyPress
lechuck's avatar
lechuck committed
6
7
 * @subpackage ActivityAkismet
 * @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

lucha's avatar
lucha committed
13
14
15
if ( ! buddypress()->do_autoload ) {
	require dirname( __FILE__ ) . '/classes/class-bp-akismet.php';
}
lechuck's avatar
lechuck committed
16
17

/**
ale's avatar
ale committed
18
 * Delete old spam activity meta data.
lechuck's avatar
lechuck committed
19
20
21
 *
 * This is done as a clean-up mechanism, as _bp_akismet_submission meta can
 * grow to be quite large.
lechuck's avatar
lechuck committed
22
 *
lechuck's avatar
lechuck committed
23
 * @since 1.6.0
lechuck's avatar
lechuck committed
24
25
 *
 * @global wpdb $wpdb WordPress database object.
lechuck's avatar
lechuck committed
26
27
 */
function bp_activity_akismet_delete_old_metadata() {
ale's avatar
ale committed
28
	global $wpdb;
lechuck's avatar
lechuck committed
29

ale's avatar
ale committed
30
31
32
33
34
	$bp = buddypress();

	/**
	 * Filters the threshold for how many days old Akismet metadata needs to be before being automatically deleted.
	 *
lechuck's avatar
lechuck committed
35
	 * @since 1.6.0
ale's avatar
ale committed
36
37
38
	 *
	 * @param integer 15 How many days old metadata needs to be.
	 */
lechuck's avatar
lechuck committed
39
40
	$interval = apply_filters( 'bp_activity_akismet_delete_meta_interval', 15 );

lechuck's avatar
lechuck committed
41
	// Enforce a minimum of 1 day.
lechuck's avatar
lechuck committed
42
43
	$interval = max( 1, absint( $interval ) );

lechuck's avatar
lechuck committed
44
	// _bp_akismet_submission meta values are large, so expire them after $interval days regardless of the activity status
lechuck's avatar
lechuck committed
45
46
47
48
49
50
51
52
	$sql          = $wpdb->prepare( "SELECT a.id FROM {$bp->activity->table_name} a LEFT JOIN {$bp->activity->table_name_meta} m ON a.id = m.activity_id WHERE m.meta_key = %s AND DATE_SUB(%s, INTERVAL {$interval} DAY) > a.date_recorded LIMIT 10000", '_bp_akismet_submission', current_time( 'mysql', 1 ) );
	$activity_ids = $wpdb->get_col( $sql );

	if ( ! empty( $activity_ids ) ) {
		foreach ( $activity_ids as $activity_id )
			bp_activity_delete_meta( $activity_id, '_bp_akismet_submission' );
	}
}
lechuck's avatar
lechuck committed
53
add_action( 'bp_activity_akismet_delete_old_metadata', 'bp_activity_akismet_delete_old_metadata' );