NotificationPreferencesService.php
1 month ago
NotificationProcessor.php
1 month ago
NotificationRetryHandler.php
1 month ago
PendingNotificationStore.php
1 month ago
PendingNotificationStore.php
184 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Services; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | |
| 9 | use Automattic\WooCommerce\Internal\PushNotifications\Dispatchers\InternalNotificationDispatcher; |
| 10 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification; |
| 11 | use Automattic\WooCommerce\Internal\PushNotifications\Services\NotificationProcessor; |
| 12 | |
| 13 | /** |
| 14 | * Store that collects notifications during a request and dispatches them all on |
| 15 | * shutdown via the InternalNotificationDispatcher. Should be accessed from the |
| 16 | * container (`wc_get_container`) to ensure store is shared by all usage. |
| 17 | * |
| 18 | * Notifications are keyed by `{type}_{resource_id}` (with blog ID from |
| 19 | * `get_current_blog_id()`) to prevent duplicates within a single request. |
| 20 | * |
| 21 | * @since 10.7.0 |
| 22 | */ |
| 23 | class PendingNotificationStore { |
| 24 | /** |
| 25 | * Whether the store is enabled and accepting notifications. |
| 26 | * |
| 27 | * @var bool |
| 28 | */ |
| 29 | private bool $enabled = false; |
| 30 | |
| 31 | /** |
| 32 | * The dispatcher that will be used to send notifications on shutdown. |
| 33 | * |
| 34 | * @var InternalNotificationDispatcher |
| 35 | */ |
| 36 | private InternalNotificationDispatcher $dispatcher; |
| 37 | |
| 38 | /** |
| 39 | * Pending notifications keyed by identifier. |
| 40 | * |
| 41 | * @var array<string, Notification> |
| 42 | */ |
| 43 | private array $pending = array(); |
| 44 | |
| 45 | /** |
| 46 | * Whether the shutdown hook has been registered. |
| 47 | * |
| 48 | * @var bool |
| 49 | */ |
| 50 | private bool $shutdown_registered = false; |
| 51 | |
| 52 | /** |
| 53 | * Initialize dependencies. |
| 54 | * |
| 55 | * @internal |
| 56 | * |
| 57 | * @param InternalNotificationDispatcher $dispatcher The dispatcher to use on shutdown. |
| 58 | * |
| 59 | * @since 10.7.0 |
| 60 | */ |
| 61 | final public function init( InternalNotificationDispatcher $dispatcher ): void { |
| 62 | $this->dispatcher = $dispatcher; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Enables the store so it accepts notifications. |
| 67 | * |
| 68 | * Called from PushNotifications::on_init() after enablement checks pass. |
| 69 | * |
| 70 | * @return void |
| 71 | * |
| 72 | * @since 10.7.0 |
| 73 | */ |
| 74 | public function register(): void { |
| 75 | $this->enabled = true; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds a notification to the pending store. |
| 80 | * |
| 81 | * Duplicate notifications (same type and resource ID) within a single |
| 82 | * request are silently ignored. The shutdown hook is registered on the |
| 83 | * first call. |
| 84 | * |
| 85 | * @param Notification $notification The notification to add. |
| 86 | * @return void |
| 87 | * |
| 88 | * @since 10.7.0 |
| 89 | */ |
| 90 | public function add( Notification $notification ): void { |
| 91 | if ( ! $this->enabled ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $key = $notification->get_identifier(); |
| 96 | |
| 97 | if ( isset( $this->pending[ $key ] ) ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | $this->pending[ $key ] = $notification; |
| 102 | |
| 103 | $this->schedule_safety_net( $notification ); |
| 104 | |
| 105 | if ( ! $this->shutdown_registered ) { |
| 106 | add_action( 'shutdown', array( $this, 'dispatch_all' ) ); |
| 107 | $this->shutdown_registered = true; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Schedules an ActionScheduler safety net job for the notification. |
| 113 | * |
| 114 | * If the shutdown hook never fires (OOM, SIGKILL, etc.), this job |
| 115 | * guarantees the notification is still processed. |
| 116 | * |
| 117 | * @param Notification $notification The notification to schedule. |
| 118 | * @return void |
| 119 | * |
| 120 | * @since 10.7.0 |
| 121 | */ |
| 122 | private function schedule_safety_net( Notification $notification ): void { |
| 123 | // Canonical, identity-keyed args shared with NotificationProcessor::cancel_safety_net(). |
| 124 | // Action Scheduler matches stored args by exact equality, so both sides must derive |
| 125 | // them from the same place; see Notification::get_safety_net_args(). |
| 126 | $args = $notification->get_safety_net_args(); |
| 127 | |
| 128 | if ( as_has_scheduled_action( NotificationProcessor::SAFETY_NET_HOOK, $args, NotificationProcessor::ACTION_SCHEDULER_GROUP ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | as_schedule_single_action( |
| 133 | time() + NotificationProcessor::SAFETY_NET_DELAY, |
| 134 | NotificationProcessor::SAFETY_NET_HOOK, |
| 135 | $args, |
| 136 | NotificationProcessor::ACTION_SCHEDULER_GROUP, |
| 137 | true |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Dispatches all pending notifications via InternalNotificationDispatcher. |
| 143 | * |
| 144 | * Called on shutdown. Sends all pending notifications through the |
| 145 | * InternalNotificationDispatcher, then clears the store. |
| 146 | * |
| 147 | * @return void |
| 148 | * |
| 149 | * @since 10.7.0 |
| 150 | */ |
| 151 | public function dispatch_all(): void { |
| 152 | if ( empty( $this->pending ) ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $this->dispatcher->dispatch( array_values( $this->pending ) ); |
| 157 | |
| 158 | $this->enabled = false; |
| 159 | $this->pending = array(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns the number of pending notifications. |
| 164 | * |
| 165 | * @return int |
| 166 | * |
| 167 | * @since 10.7.0 |
| 168 | */ |
| 169 | public function count(): int { |
| 170 | return count( $this->pending ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns all pending notifications. |
| 175 | * |
| 176 | * @return Notification[] |
| 177 | * |
| 178 | * @since 10.7.0 |
| 179 | */ |
| 180 | public function get_all(): array { |
| 181 | return array_values( $this->pending ); |
| 182 | } |
| 183 | } |
| 184 |