Admin
1 month ago
AsyncTasks
1 month ago
Emails
1 month ago
Enums
10 months ago
Frontend
1 month ago
Privacy
1 month ago
Utilities
1 month ago
Config.php
1 month ago
DataRetentionController.php
1 month ago
Factory.php
10 months ago
Notification.php
1 month ago
NotificationQuery.php
10 months ago
StockNotifications.php
1 month ago
StockSyncController.php
10 months ago
DataRetentionController.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus; |
| 8 | |
| 9 | /** |
| 10 | * Controller for managing data retention of customer stock notifications. |
| 11 | * |
| 12 | * This controller handles the scheduling and execution of tasks related to |
| 13 | * deleting overdue notifications based on a configured time threshold. |
| 14 | */ |
| 15 | class DataRetentionController { |
| 16 | public const DAILY_TASK_HOOK = 'customer_stock_notifications_daily'; |
| 17 | |
| 18 | /** |
| 19 | * Constructor to set up hooks for managing data retention tasks. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | add_action( self::DAILY_TASK_HOOK, array( $this, 'do_wc_customer_stock_notifications_daily' ) ); |
| 23 | add_action( 'update_option_woocommerce_customer_stock_notifications_unverified_deletions_days_threshold', array( $this, 'schedule_or_unschedule_daily_task' ), 10, 2 ); |
| 24 | add_action( 'add_option_woocommerce_customer_stock_notifications_unverified_deletions_days_threshold', array( $this, 'schedule_or_unschedule_daily_task' ), 10, 2 ); |
| 25 | register_deactivation_hook( WC_PLUGIN_FILE, array( $this, 'clear_daily_task' ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Tasks to run when WooCommerce is installed or updated. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function on_woo_install_or_update(): void { |
| 34 | $this->schedule_or_unschedule_daily_task( null, Config::get_unverified_deletion_days_threshold() ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Responds to changes in the option for deleting unverified notifications. |
| 39 | * If the new value is numeric and greater than zero, it schedules a daily task. |
| 40 | * If the new value is not numeric or is empty, it clears the scheduled tasks. |
| 41 | * |
| 42 | * @param mixed $unused The old option value or option name (not used in this function). |
| 43 | * @param mixed $new_option_value The new value of the option. |
| 44 | * @return void |
| 45 | */ |
| 46 | public function schedule_or_unschedule_daily_task( $unused, $new_option_value ): void { |
| 47 | if ( ! is_numeric( $new_option_value ) || empty( $new_option_value ) ) { |
| 48 | $this->clear_daily_task(); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( ! wp_next_scheduled( self::DAILY_TASK_HOOK ) ) { |
| 53 | wp_schedule_event( time() + 10, 'daily', self::DAILY_TASK_HOOK ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Unschedule the daily task when the plugin is deactivated, or the option is set to zero. |
| 59 | */ |
| 60 | public function clear_daily_task() { |
| 61 | wp_clear_scheduled_hook( self::DAILY_TASK_HOOK ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Deletes overdue notifications based on the configured time threshold. |
| 66 | * It retrieves notifications that are pending and past the threshold, |
| 67 | * then deletes them. |
| 68 | * |
| 69 | * @return void |
| 70 | */ |
| 71 | public function do_wc_customer_stock_notifications_daily() { |
| 72 | $time_threshold = Config::get_unverified_deletion_days_threshold(); |
| 73 | |
| 74 | if ( 0 === $time_threshold ) { |
| 75 | return; |
| 76 | } |
| 77 | $overdue_threshold = time() - $time_threshold * DAY_IN_SECONDS; |
| 78 | |
| 79 | $overdue_notifications = NotificationQuery::get_notifications( |
| 80 | array( |
| 81 | 'status' => NotificationStatus::PENDING, |
| 82 | 'end_date' => gmdate( 'Y-m-d H:i:s', $overdue_threshold ), |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | foreach ( $overdue_notifications as $notification_id ) { |
| 87 | $notification = Factory::get_notification( $notification_id ); |
| 88 | if ( $notification instanceof Notification ) { |
| 89 | $notification->delete(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 |