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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / StockNotifications / AsyncTasks / NotificationsProcessor.php
woocommerce / src / Internal / StockNotifications / AsyncTasks Last commit date
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