Queue
5 days ago
Reports
5 days ago
DebugEventsCleanupTask.php
5 days ago
Meta.php
5 days ago
NotificationsUpdateTask.php
5 days ago
Task.php
5 days ago
Tasks.php
5 days ago
DebugEventsCleanupTask.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Tasks; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use WPMailSMTP\Admin\DebugEvents\DebugEvents; |
| 8 | use WPMailSMTP\Options; |
| 9 | use WPMailSMTP\WP; |
| 10 | |
| 11 | /** |
| 12 | * Class DebugEventsCleanupTask. |
| 13 | * |
| 14 | * @since 3.6.0 |
| 15 | */ |
| 16 | class DebugEventsCleanupTask extends Task { |
| 17 | |
| 18 | /** |
| 19 | * Action name for this task. |
| 20 | * |
| 21 | * @since 3.6.0 |
| 22 | */ |
| 23 | const ACTION = 'wp_mail_smtp_process_debug_events_cleanup'; |
| 24 | |
| 25 | /** |
| 26 | * Class constructor. |
| 27 | * |
| 28 | * @since 3.6.0 |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | |
| 32 | parent::__construct( self::ACTION ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Initialize the task with all the proper checks. |
| 37 | * |
| 38 | * @since 3.6.0 |
| 39 | */ |
| 40 | public function init() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks |
| 41 | |
| 42 | // Register the action handler. |
| 43 | add_action( self::ACTION, [ $this, 'process' ] ); |
| 44 | |
| 45 | // Get the retention period value from the Debug Events settings. |
| 46 | $retention_period = Options::init()->get( 'debug_events', 'retention_period' ); |
| 47 | |
| 48 | // Exit if the retention period is not defined (set to "forever") or this task is already scheduled. |
| 49 | if ( empty( $retention_period ) || Tasks::is_scheduled( self::ACTION ) !== false ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // Schedule the task. |
| 54 | $this->recurring( |
| 55 | strtotime( 'tomorrow' ), |
| 56 | $this->get_debug_events_cleanup_interval() |
| 57 | ) |
| 58 | ->params( $retention_period ) |
| 59 | ->register(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the cleanup interval for the debug events. |
| 64 | * |
| 65 | * @since 3.6.0 |
| 66 | * |
| 67 | * @return int |
| 68 | */ |
| 69 | private function get_debug_events_cleanup_interval() { |
| 70 | |
| 71 | $day_in_seconds = DAY_IN_SECONDS; |
| 72 | |
| 73 | /** |
| 74 | * Filter for the debug events cleanup interval. |
| 75 | * |
| 76 | * @since 3.6.0 |
| 77 | * |
| 78 | * @param int $day_in_seconds Debug events cleanup interval. |
| 79 | */ |
| 80 | return (int) apply_filters( 'wpmailsmtp_tasks_get_debug_events_cleanup_interval', $day_in_seconds ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Perform the cleanup action: remove outdated debug events. |
| 85 | * |
| 86 | * @since 3.6.0 |
| 87 | * |
| 88 | * @param int $meta_id The Meta ID with the stored task parameters. |
| 89 | * |
| 90 | * @throws Exception Exception will be logged in the Action Scheduler logs table. |
| 91 | */ |
| 92 | public function process( $meta_id ) { |
| 93 | |
| 94 | $task_meta = new Meta(); |
| 95 | $meta = $task_meta->get( (int) $meta_id ); |
| 96 | |
| 97 | // We should actually receive the passed parameter. |
| 98 | if ( empty( $meta ) || empty( $meta->data ) || count( $meta->data ) !== 1 ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Date in seconds (examples: 86400, 100500). |
| 104 | * Debug Events older than this period will be deleted. |
| 105 | * |
| 106 | * @var int $retention_period |
| 107 | */ |
| 108 | $retention_period = (int) $meta->data[0]; |
| 109 | |
| 110 | if ( empty( $retention_period ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Bail if DB tables was not created. |
| 115 | if ( ! DebugEvents::is_valid_db() ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $wpdb = WP::wpdb(); |
| 120 | $table = DebugEvents::get_table_name(); |
| 121 | $date = ( new DateTime( "- $retention_period seconds" ) )->format( WP::datetime_mysql_format() ); |
| 122 | |
| 123 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching |
| 124 | $wpdb->query( |
| 125 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 126 | $wpdb->prepare( "DELETE FROM `$table` WHERE created_at < %s", $date ) |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 |