|
| 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 | +} |
0 commit comments