bp-activity-akismet.php 2.26 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

lucha's avatar
lucha committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Loads Akismet filtering for activity.
 *
 * @since 1.6.0
 * @since 2.3.0 We only support Akismet 3+.
 */
function bp_activity_setup_akismet() {
	/**
	 * Filters if BuddyPress Activity Akismet support has been disabled by another plugin.
	 *
	 * @since 1.6.0
	 *
	 * @param bool $value Return value of bp_is_akismet_active boolean function.
	 */
	if ( ! apply_filters( 'bp_activity_use_akismet', bp_is_akismet_active() ) ) {
		return;
	}

	// Instantiate Akismet for BuddyPress.
	buddypress()->activity->akismet = new BP_Akismet();
}
add_action( 'bp_activity_setup_globals', 'bp_activity_setup_akismet' );

lechuck's avatar
lechuck committed
40
/**
ale's avatar
ale committed
41
 * Delete old spam activity meta data.
lechuck's avatar
lechuck committed
42
43
44
 *
 * This is done as a clean-up mechanism, as _bp_akismet_submission meta can
 * grow to be quite large.
lechuck's avatar
lechuck committed
45
 *
lechuck's avatar
lechuck committed
46
 * @since 1.6.0
lechuck's avatar
lechuck committed
47
48
 *
 * @global wpdb $wpdb WordPress database object.
lechuck's avatar
lechuck committed
49
50
 */
function bp_activity_akismet_delete_old_metadata() {
ale's avatar
ale committed
51
	global $wpdb;
lechuck's avatar
lechuck committed
52

ale's avatar
ale committed
53
54
55
56
57
	$bp = buddypress();

	/**
	 * Filters the threshold for how many days old Akismet metadata needs to be before being automatically deleted.
	 *
lechuck's avatar
lechuck committed
58
	 * @since 1.6.0
ale's avatar
ale committed
59
60
61
	 *
	 * @param integer 15 How many days old metadata needs to be.
	 */
lechuck's avatar
lechuck committed
62
63
	$interval = apply_filters( 'bp_activity_akismet_delete_meta_interval', 15 );

lechuck's avatar
lechuck committed
64
	// Enforce a minimum of 1 day.
lechuck's avatar
lechuck committed
65
66
	$interval = max( 1, absint( $interval ) );

lechuck's avatar
lechuck committed
67
	// _bp_akismet_submission meta values are large, so expire them after $interval days regardless of the activity status
lechuck's avatar
lechuck committed
68
69
70
71
72
73
74
75
	$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
76
add_action( 'bp_activity_akismet_delete_old_metadata', 'bp_activity_akismet_delete_old_metadata' );