CycleStateService.php
10 months ago
JobManager.php
10 months ago
NotificationsProcessor.php
1 month ago
NotificationsProcessor.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks\JobManager; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks\CycleStateService; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Utilities\EligibilityService; |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailManager; |
| 11 | use Automattic\WooCommerce\Internal\StockNotifications\Factory; |
| 12 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 13 | use Automattic\WooCommerce\Internal\StockNotifications\NotificationQuery; |
| 14 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus; |
| 15 | use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationCancellationSource; |
| 16 | use WC_Product; |
| 17 | |
| 18 | /** |
| 19 | * The async processor for sending stock notifications in bulk. |
| 20 | */ |
| 21 | class NotificationsProcessor { |
| 22 | |
| 23 | /** |
| 24 | * The email manager. |
| 25 | * |
| 26 | * @var EmailManager |
| 27 | */ |
| 28 | private EmailManager $email_manager; |
| 29 | |
| 30 | /** |
| 31 | * The logger. |
| 32 | * |
| 33 | * @var Logger |
| 34 | */ |
| 35 | private $logger; |
| 36 | |
| 37 | /** |
| 38 | * The eligibility service. |
| 39 | * |
| 40 | * @var EligibilityService |
| 41 | */ |
| 42 | private EligibilityService $eligibility_service; |
| 43 | |
| 44 | /** |
| 45 | * The job manager. |
| 46 | * |
| 47 | * @var JobManager |
| 48 | */ |
| 49 | private JobManager $job_manager; |
| 50 | |
| 51 | /** |
| 52 | * The cycle state service. |
| 53 | * |
| 54 | * @var CycleStateService |
| 55 | */ |
| 56 | private CycleStateService $cycle_state_service; |
| 57 | |
| 58 | /** |
| 59 | * The batch size for processing notifications. |
| 60 | */ |
| 61 | protected const BATCH_SIZE = 50; |
| 62 | |
| 63 | /** |
| 64 | * Initialize the controller. |
| 65 | * |
| 66 | * @internal |
| 67 | * |
| 68 | * @param EligibilityService $eligibility_service The eligibility service. |
| 69 | * @param JobManager $job_manager The job manager. |
| 70 | * @param CycleStateService $cycle_state_service The cycle state service. |
| 71 | * @param EmailManager $email_manager The email manager. |
| 72 | * @return void |
| 73 | */ |
| 74 | final public function init( |
| 75 | EligibilityService $eligibility_service, |
| 76 | JobManager $job_manager, |
| 77 | CycleStateService $cycle_state_service, |
| 78 | EmailManager $email_manager |
| 79 | ): void { |
| 80 | $this->eligibility_service = $eligibility_service; |
| 81 | $this->job_manager = $job_manager; |
| 82 | $this->cycle_state_service = $cycle_state_service; |
| 83 | $this->email_manager = $email_manager; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Constructor. |
| 88 | */ |
| 89 | public function __construct() { |
| 90 | $this->logger = \wc_get_logger(); |
| 91 | add_action( JobManager::AS_JOB_SEND_STOCK_NOTIFICATIONS, array( $this, 'process_batch' ) ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the batch size for processing notifications. |
| 96 | * |
| 97 | * @return int |
| 98 | */ |
| 99 | private function get_batch_size(): int { |
| 100 | /** |
| 101 | * Filter: woocommerce_customer_stock_notifications_batch_size |
| 102 | * |
| 103 | * @since 10.2.0 |
| 104 | * |
| 105 | * Allow customization of batch size for processing notifications. |
| 106 | * |
| 107 | * @param int $batch_size Default batch size. |
| 108 | * @return int |
| 109 | */ |
| 110 | return (int) apply_filters( 'woocommerce_customer_stock_notifications_batch_size', self::BATCH_SIZE ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Parse the product ID from the arguments. |
| 115 | * |
| 116 | * @param int $product_id The product ID. |
| 117 | * @return int |
| 118 | * @throws \Exception If the product is not found. |
| 119 | */ |
| 120 | private function parse_args( $product_id ): int { |
| 121 | if ( empty( $product_id ) || ! is_numeric( $product_id ) ) { |
| 122 | throw new \Exception( 'Invalid arguments.' ); |
| 123 | } |
| 124 | |
| 125 | $product_id = (int) $product_id; |
| 126 | if ( $product_id <= 0 ) { |
| 127 | throw new \Exception( 'Product ID is required.' ); |
| 128 | } |
| 129 | |
| 130 | return $product_id; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Parse the product. |
| 135 | * |
| 136 | * @param int $product_id The product ID. |
| 137 | * @return \WC_Product |
| 138 | * @throws \Exception If the product is not valid for notifications. |
| 139 | */ |
| 140 | private function parse_product( int $product_id ): WC_Product { |
| 141 | |
| 142 | $product = wc_get_product( $product_id ); |
| 143 | if ( ! $product instanceof WC_Product ) { |
| 144 | throw new \Exception( sprintf( 'Product %d not found.', absint( $product_id ) ) ); |
| 145 | } |
| 146 | |
| 147 | if ( ! $this->eligibility_service->is_product_eligible( $product ) ) { |
| 148 | throw new \Exception( sprintf( 'Product %d is not eligible for notifications.', $product->get_id() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 149 | } |
| 150 | |
| 151 | if ( ! $this->eligibility_service->is_stock_status_eligible( $product->get_stock_status() ) ) { |
| 152 | throw new \Exception( sprintf( 'Product %d stock status is not eligible for notifications (i.e. not in stock).', $product->get_id() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 153 | } |
| 154 | |
| 155 | return $product; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Process a batch of notifications. |
| 160 | * |
| 161 | * @param int $product_id The product ID from AS job args. |
| 162 | * @return void |
| 163 | */ |
| 164 | public function process_batch( $product_id ) { |
| 165 | // Sanity checks. |
| 166 | try { |
| 167 | $product_id = $this->parse_args( $product_id ); |
| 168 | $cycle_state = $this->cycle_state_service->get_or_initialize_cycle_state( $product_id ); |
| 169 | $product = $this->parse_product( $product_id ); |
| 170 | } catch ( \Throwable $e ) { |
| 171 | $product_id = (int) $product_id; |
| 172 | $this->logger->error( |
| 173 | sprintf( 'Background process for product %s terminated. Reason: %s', $product_id, $e->getMessage() ), |
| 174 | array( |
| 175 | 'source' => 'wc-customer-stock-notifications', |
| 176 | 'product_id' => $product_id, |
| 177 | 'exception' => get_class( $e ), |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | // Clean up the cycle state. |
| 182 | if ( isset( $cycle_state ) ) { |
| 183 | $this->cycle_state_service->complete_cycle( $product_id, $cycle_state ); |
| 184 | } |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $cycle_state['product_ids'] = $this->eligibility_service->get_target_product_ids( $product ); |
| 190 | |
| 191 | // Get notifications. |
| 192 | $notifications = NotificationQuery::get_notifications( |
| 193 | array( |
| 194 | 'status' => NotificationStatus::ACTIVE, |
| 195 | 'product_id' => $cycle_state['product_ids'], |
| 196 | 'last_attempt_limit' => (int) $cycle_state['cycle_start_time'], |
| 197 | 'return' => 'ids', |
| 198 | 'limit' => $this->get_batch_size(), |
| 199 | 'orderby' => 'id', |
| 200 | 'order' => 'ASC', |
| 201 | ) |
| 202 | ); |
| 203 | |
| 204 | if ( empty( $notifications ) ) { |
| 205 | $this->cycle_state_service->complete_cycle( $product_id, $cycle_state ); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | foreach ( $notifications as $notification_id ) { |
| 210 | $notification = Factory::get_notification( $notification_id ); |
| 211 | if ( ! $notification instanceof Notification ) { |
| 212 | $this->logger->error( |
| 213 | sprintf( 'Failed to get notification ID: %d', $notification_id ), |
| 214 | array( 'source' => 'wc-customer-stock-notifications' ) |
| 215 | ); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | $notification->set_date_last_attempt( time() ); |
| 220 | ++$cycle_state['total_count']; |
| 221 | |
| 222 | if ( $this->eligibility_service->should_skip_notification( $notification, $product ) ) { |
| 223 | ++$cycle_state['skipped_count']; |
| 224 | $notification->save(); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | $is_sent = true; |
| 229 | try { |
| 230 | $this->email_manager->send_stock_notification_email( $notification ); |
| 231 | } catch ( \Throwable $e ) { |
| 232 | $is_sent = false; |
| 233 | } |
| 234 | |
| 235 | if ( $is_sent ) { |
| 236 | $notification->set_date_notified( time() ); |
| 237 | $notification->set_status( NotificationStatus::SENT ); |
| 238 | ++$cycle_state['sent_count']; |
| 239 | } else { |
| 240 | $notification->set_status( NotificationStatus::CANCELLED ); |
| 241 | $notification->set_cancellation_source( NotificationCancellationSource::SYSTEM ); |
| 242 | ++$cycle_state['failed_count']; |
| 243 | } |
| 244 | |
| 245 | // Always save the notification to reflect last attempt time. |
| 246 | $notification->save(); |
| 247 | } |
| 248 | |
| 249 | if ( count( $notifications ) === $this->get_batch_size() ) { |
| 250 | $this->cycle_state_service->save_cycle_state( $product_id, $cycle_state ); |
| 251 | $this->job_manager->schedule_next_batch_for_product( $product_id ); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | $this->cycle_state_service->complete_cycle( $product_id, $cycle_state ); |
| 256 | } |
| 257 | } |
| 258 |