Queue
6 days ago
Reports
6 days ago
DebugEventsCleanupTask.php
6 days ago
Meta.php
6 days ago
NotificationsUpdateTask.php
6 days ago
Task.php
6 days ago
Tasks.php
6 days ago
NotificationsUpdateTask.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Tasks; |
| 4 | |
| 5 | use Exception; |
| 6 | |
| 7 | /** |
| 8 | * Class NotificationsUpdateTask. |
| 9 | * |
| 10 | * @since 4.3.0 |
| 11 | */ |
| 12 | class NotificationsUpdateTask extends Task { |
| 13 | |
| 14 | /** |
| 15 | * Action name for this task. |
| 16 | * |
| 17 | * @since 4.3.0 |
| 18 | */ |
| 19 | const ACTION = 'wp_mail_smtp_admin_notifications_update'; |
| 20 | |
| 21 | /** |
| 22 | * Class constructor. |
| 23 | * |
| 24 | * @since 4.3.0 |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | |
| 28 | parent::__construct( self::ACTION ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Initialize the task with all the proper checks. |
| 33 | * |
| 34 | * @since 4.3.0 |
| 35 | */ |
| 36 | public function init() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks |
| 37 | |
| 38 | // Register the action handler. |
| 39 | add_action( self::ACTION, [ $this, 'process' ] ); |
| 40 | |
| 41 | // Exit if notifications are disabled |
| 42 | // or this task is already scheduled. |
| 43 | if ( |
| 44 | ! wp_mail_smtp()->get_notifications()->is_enabled() || |
| 45 | Tasks::is_scheduled( self::ACTION ) !== false |
| 46 | ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Schedule the task. |
| 51 | $this->recurring( |
| 52 | strtotime( '+1 minute' ), |
| 53 | wp_mail_smtp()->get_notifications()->get_notification_update_task_interval() |
| 54 | ) |
| 55 | ->unique() |
| 56 | ->register(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Update the notification feed. |
| 61 | * |
| 62 | * @since 4.3.0 |
| 63 | */ |
| 64 | public function process() { |
| 65 | |
| 66 | // Delete task duplicates. |
| 67 | try { |
| 68 | $this->remove_pending( 1000 ); |
| 69 | } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 70 | // Do nothing. |
| 71 | } |
| 72 | |
| 73 | wp_mail_smtp()->get_notifications()->update(); |
| 74 | } |
| 75 | } |
| 76 |