| 1 |
<?php |
| 2 |
/** |
| 3 |
* Auto Logs Cleanup |
| 4 |
* |
| 5 |
* @package AutomatorWP\Cron\Auto_Logs_Cleanup |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register schedule events |
| 14 |
* |
| 15 |
* @since 1.3.2 |
| 16 |
*/ |
| 17 |
function automatorwp_auto_logs_cleanup_schedule_events() { |
| 18 |
|
| 19 |
if ( function_exists( 'as_schedule_recurring_action' ) ) { |
| 20 |
|
| 21 |
// Action scheduler support |
| 22 |
if ( ! as_next_scheduled_action( 'automatorwp_auto_logs_cleanup_event' ) ) { |
| 23 |
as_schedule_recurring_action( time(), DAY_IN_SECONDS, 'automatorwp_auto_logs_cleanup_event' ); |
| 24 |
} |
| 25 |
|
| 26 |
} else { |
| 27 |
|
| 28 |
// WP Cron |
| 29 |
if ( ! wp_next_scheduled( 'automatorwp_auto_logs_cleanup_event' ) ) { |
| 30 |
wp_schedule_event( time(), 'daily', 'automatorwp_auto_logs_cleanup_event' ); |
| 31 |
} |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
add_action( 'automatorwp_schedule_events', 'automatorwp_auto_logs_cleanup_schedule_events' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Clear scheduled events |
| 40 |
* |
| 41 |
* @since 1.3.2 |
| 42 |
*/ |
| 43 |
function automatorwp_auto_logs_cleanup_clear_scheduled_events() { |
| 44 |
wp_clear_scheduled_hook( 'automatorwp_auto_logs_cleanup_event' ); |
| 45 |
} |
| 46 |
add_action( 'automatorwp_clear_scheduled_events', 'automatorwp_auto_logs_cleanup_clear_scheduled_events' ); |
| 47 |
|
| 48 |
/** |
| 49 |
* Process the auto logs cleanup |
| 50 |
* |
| 51 |
* @since 1.3.2 |
| 52 |
*/ |
| 53 |
function automatorwp_auto_logs_cleanup() { |
| 54 |
|
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
$days = absint( automatorwp_get_option( 'auto_logs_cleanup_days', '' ) ); |
| 58 |
|
| 59 |
// Bail if no days configured |
| 60 |
if( $days === 0 ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
// Setup vars |
| 65 |
$date = date( 'Y-m-d', strtotime( "-{$days} day", current_time( 'timestamp' ) ) ); |
| 66 |
$logs = AutomatorWP()->db->logs; |
| 67 |
$logs_ids = array(); |
| 68 |
$automations = array(); |
| 69 |
$triggers_per_loop = apply_filters( 'automatorwp_auto_logs_cleanup_triggers_per_loop', 200 ); |
| 70 |
|
| 71 |
// Triggers logs |
| 72 |
$triggers_sql = "FROM {$logs} AS l WHERE l.type = 'trigger' AND l.date < '{$date}'"; |
| 73 |
$triggers_count = absint( $wpdb->get_var( "SELECT COUNT(*) {$triggers_sql}" ) ); |
| 74 |
|
| 75 |
// Prevent to query all trigger logs if there is a great number of them |
| 76 |
if( $triggers_count > $triggers_per_loop ) { |
| 77 |
// Get only a limited amount of triggers |
| 78 |
$results = $wpdb->get_results( "SELECT l.id, l.object_id, l.user_id, l.date {$triggers_sql} LIMIT 0, {$triggers_per_loop}" ); |
| 79 |
|
| 80 |
// Schedule this function to run 5 minutes later again |
| 81 |
$datetime_utc = strtotime( "+5 minutes", current_time( 'timestamp', true ) ); |
| 82 |
|
| 83 |
// Check if Action Scheduler is installed, if not then use WordPress functions |
| 84 |
if( function_exists( 'as_schedule_single_action' ) && ! apply_filters( 'automatorwp_force_wp_cron', false ) ) { |
| 85 |
as_schedule_single_action( $datetime_utc, 'automatorwp_auto_logs_cleanup' ); |
| 86 |
} else { |
| 87 |
wp_schedule_single_event( $datetime_utc, 'automatorwp_auto_logs_cleanup' ); |
| 88 |
} |
| 89 |
} else { |
| 90 |
// Get all triggers |
| 91 |
$results = $wpdb->get_results( "SELECT l.id, l.object_id, l.user_id, l.date {$triggers_sql}" ); |
| 92 |
} |
| 93 |
|
| 94 |
foreach( $results as $log ) { |
| 95 |
|
| 96 |
$trigger = automatorwp_get_trigger_object( $log->object_id ); |
| 97 |
|
| 98 |
// If trigger not found, add the log to being removed |
| 99 |
if( ! $trigger ) { |
| 100 |
$logs_ids[] = $log->id; |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
// All users/posts and filters get removed directly |
| 105 |
if( in_array( $trigger->type, array( 'automatorwp_all_users', 'automatorwp_all_posts', 'filter' ) ) ) { |
| 106 |
$logs_ids[] = $log->id; |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$trigger_args = automatorwp_get_trigger( $trigger->type ); |
| 111 |
|
| 112 |
// Anonymous triggers get removed directly |
| 113 |
if( $trigger_args['anonymous'] ) { |
| 114 |
$logs_ids[] = $log->id; |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
$automation_id = absint( $trigger->automation_id ); |
| 119 |
|
| 120 |
// Check if the automation has not been handled yet by this function |
| 121 |
if( ! isset( $automations[$automation_id] ) ) { |
| 122 |
$last_completion = automatorwp_get_user_last_completion( $automation_id, $log->user_id, 'automation' ); |
| 123 |
|
| 124 |
$automations[$automation_id] = ( $last_completion ? $last_completion->date : false ); |
| 125 |
} |
| 126 |
|
| 127 |
// Bail if user has not completed this automation yet |
| 128 |
if( $automations[$automation_id] === false ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
// If user has completed the automation, mark the log to being removed |
| 133 |
if( strtotime( $automations[$automation_id] ) >= strtotime( $log->date ) ) { |
| 134 |
$logs_ids[] = $log->id; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Remove all logs marked to get removed |
| 139 |
if( ! empty( $logs_ids ) ) { |
| 140 |
$wpdb->query( "DELETE FROM {$logs} WHERE id IN ( " . implode( ', ', $logs_ids ) . " )" ); |
| 141 |
} |
| 142 |
|
| 143 |
// Automations, actions, anonymous and filter logs |
| 144 |
// They don't need any specific check so is safe to remove them directly |
| 145 |
$wpdb->query( "DELETE FROM {$logs} WHERE type IN ( 'automation', 'action', 'anonymous', 'filter' ) AND date < '{$date}'" ); |
| 146 |
|
| 147 |
/** |
| 148 |
* Available action to let other plugins process anything after the logs cleanup |
| 149 |
* |
| 150 |
* @since 1.3.2 |
| 151 |
* |
| 152 |
* @param string $date Date from logs has been removed |
| 153 |
*/ |
| 154 |
do_action( 'automatorwp_auto_logs_cleanup_finished', $date ); |
| 155 |
|
| 156 |
} |
| 157 |
add_action( 'automatorwp_auto_logs_cleanup_event', 'automatorwp_auto_logs_cleanup' ); |