PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.9.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.9.0
4.9.0 0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / Tasks / DebugEventsCleanupTask.php
wp-mail-smtp / src / Tasks Last commit date
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