Skip to content

Commit e93458e

Browse files
committed
Cron: clean-up activity older than 14 days.
See #25.
1 parent 53b76fd commit e93458e

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

wp-user-activity.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function _wp_user_activity() {
3232
// Include the files
3333
require_once $dir . 'includes/admin.php';
3434
require_once $dir . 'includes/classes.php';
35+
require_once $dir . 'includes/cron.php';
3536
require_once $dir . 'includes/capabilities.php';
3637
require_once $dir . 'includes/common.php';
3738
require_once $dir . 'includes/list-table.php';

wp-user-activity/includes/cron.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/**
4+
* User Activity Cron
5+
*
6+
* @package User/Activity/Cron
7+
*/
8+
9+
// Exit if accessed directly
10+
defined( 'ABSPATH' ) || exit;
11+
12+
/**
13+
* Schedule cron
14+
*
15+
* @since 2.3.0
16+
* @return void
17+
*/
18+
function wp_user_activity_schedule_cron() {
19+
20+
// Define task name
21+
$task = 'wp_user_activity_trash_old_activities';
22+
23+
// Schedule cron
24+
if ( ! wp_next_scheduled( $task ) ) {
25+
wp_schedule_event( time(), 'daily', $task );
26+
}
27+
}
28+
29+
/**
30+
* Trash old activities
31+
*
32+
* This function is used to trash old activity items, both to keep the
33+
* database clean and to improve performance.
34+
*
35+
* It is scheduled to run daily, and will trash a maximum of 100 items,
36+
* but if it retrieves the maximum number of items, it will schedule the
37+
* next cron to run in 60 seconds, effectively running the cron until
38+
* there are no more items to trash.
39+
*
40+
* @since 2.3.0
41+
* @return void
42+
*/
43+
function wp_user_activity_trash_old_activities() {
44+
45+
/**
46+
* Filter the number of days to keep trashed activities
47+
*
48+
* @since 2.3.0
49+
* @param int $days_to_keep The number of days to keep trashed activities
50+
* @return int
51+
*/
52+
$days_to_keep = (int) apply_filters( 'wp_user_activity_trash_days_to_keep', 14 );
53+
54+
/**
55+
* Filter the time until the next cron loop
56+
*
57+
* @since 2.3.0
58+
* @param int $time_until_next_cron The time until the next cron loop
59+
* @return int
60+
*/
61+
$time_until_next_cron = (int) apply_filters( 'wp_user_activity_trash_time', 60 );
62+
63+
/**
64+
* Filter the maximum number of activities to trash each time
65+
*
66+
* @since 2.3.0
67+
* @param int $max_trash_each_time The maximum number of activities to trash each time
68+
* @return int
69+
*/
70+
$max_trash_each_time = (int) apply_filters( 'wp_user_activity_trash_each', 100 );
71+
72+
// Date arguments
73+
$date_args = array(
74+
'before' => date( 'Y-m-d H:i:s', strtotime( "-{$days_to_keep} days" ) )
75+
);
76+
77+
// Query arguments
78+
$args = array(
79+
'fields' => 'ids',
80+
'post_type' => 'activity',
81+
'post_status' => 'publish',
82+
'orderby' => 'post_date',
83+
'order' => 'DESC',
84+
'date_query' => $date_args,
85+
'posts_per_page' => $max_trash_each_time,
86+
);
87+
88+
// Get activities
89+
$activities = new WP_Query( $args );
90+
91+
// Name of the single event
92+
$single = 'wp_user_activity_trash_old_activities_loop';
93+
94+
// Bail if no activities
95+
if ( empty( $activities ) ) {
96+
wp_clear_scheduled_hook( $single );
97+
wp_clear_scheduled_hook( __FUNCTION__ );
98+
return;
99+
}
100+
101+
// Schedule next cron if we retrieved the max number of activities
102+
if ( ( $activities->post_count === $max_trash_each_time ) && ! wp_next_scheduled( $single ) ) {
103+
wp_schedule_single_event( time() + $time_until_next_cron, $single );
104+
}
105+
106+
// Trash all activities
107+
foreach ( $activities->posts as $activity_id ) {
108+
wp_trash_post( $activity_id );
109+
}
110+
}

wp-user-activity/includes/hooks.php

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
// Untrash
5454
add_filter( 'wp_untrash_post_status', 'wp_user_activity_untrash_to_previous_status', 10, 3 );
5555

56+
// Cron
57+
add_action( 'init', 'wp_user_activity_schedule_cron' );
58+
add_action( 'wp_user_activity_trash_old_activities', 'wp_user_activity_trash_old_activities' );
59+
add_action( 'wp_user_activity_trash_old_activities_loop', 'wp_user_activity_trash_old_activities' );
60+
5661
// Admin only filter for list-table sorting
5762
if ( is_admin() ) {
5863
add_filter( 'pre_get_posts', 'wp_user_activity_maybe_sort_by_fields' );

0 commit comments

Comments
 (0)