| Server IP : 185.11.201.71 / Your IP : 192.168.7.18 Web Server : Apache/2.4.29 (Ubuntu) System : Linux tech-virtual-machine 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64 User : tech ( 1000) PHP Version : 7.4.28 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/enaireretos/wp-content/plugins/wp-mail-smtp-pro/src/Tasks/ |
Upload File : |
<?php
namespace WPMailSMTP\Tasks;
use DateTime;
use Exception;
use WPMailSMTP\Admin\DebugEvents\DebugEvents;
use WPMailSMTP\Options;
use WPMailSMTP\WP;
/**
* Class DebugEventsCleanupTask.
*
* @since 3.6.0
*/
class DebugEventsCleanupTask extends Task {
/**
* Action name for this task.
*
* @since 3.6.0
*/
const ACTION = 'wp_mail_smtp_process_debug_events_cleanup';
/**
* Class constructor.
*
* @since 3.6.0
*/
public function __construct() {
parent::__construct( self::ACTION );
}
/**
* Initialize the task with all the proper checks.
*
* @since 3.6.0
*/
public function init() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
// Register the action handler.
add_action( self::ACTION, [ $this, 'process' ] );
// Get the retention period value from the Debug Events settings.
$retention_period = Options::init()->get( 'debug_events', 'retention_period' );
// Exit if the retention period is not defined (set to "forever") or this task is already scheduled.
if ( empty( $retention_period ) || Tasks::is_scheduled( self::ACTION ) !== false ) {
return;
}
// Schedule the task.
$this->recurring(
strtotime( 'tomorrow' ),
$this->get_debug_events_cleanup_interval()
)
->params( $retention_period )
->register();
}
/**
* Get the cleanup interval for the debug events.
*
* @since 3.6.0
*
* @return int
*/
private function get_debug_events_cleanup_interval() {
$day_in_seconds = DAY_IN_SECONDS;
/**
* Filter for the debug events cleanup interval.
*
* @since 3.6.0
*
* @param int $day_in_seconds Debug events cleanup interval.
*/
return (int) apply_filters( 'wpmailsmtp_tasks_get_debug_events_cleanup_interval', $day_in_seconds );
}
/**
* Perform the cleanup action: remove outdated debug events.
*
* @since 3.6.0
*
* @param int $meta_id The Meta ID with the stored task parameters.
*
* @throws Exception Exception will be logged in the Action Scheduler logs table.
*/
public function process( $meta_id ) {
$task_meta = new Meta();
$meta = $task_meta->get( (int) $meta_id );
// We should actually receive the passed parameter.
if ( empty( $meta ) || empty( $meta->data ) || count( $meta->data ) !== 1 ) {
return;
}
/**
* Date in seconds (examples: 86400, 100500).
* Debug Events older than this period will be deleted.
*
* @var int $retention_period
*/
$retention_period = (int) $meta->data[0];
if ( empty( $retention_period ) ) {
return;
}
// Bail if DB tables was not created.
if ( ! DebugEvents::is_valid_db() ) {
return;
}
$wpdb = WP::wpdb();
$table = DebugEvents::get_table_name();
$date = ( new DateTime( "- $retention_period seconds" ) )->format( WP::datetime_mysql_format() );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->prepare( "DELETE FROM `$table` WHERE created_at < %s", $date )
);
}
}