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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 / PushNotifications / Notifications / StockNotification.php
woocommerce / src / Internal / PushNotifications / Notifications Last commit date
NewOrderNotification.php 4 weeks ago NewReviewNotification.php 4 weeks ago Notification.php 4 weeks ago StockNotification.php 4 weeks ago
StockNotification.php
366 lines
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace Automattic\WooCommerce\Internal\PushNotifications\Notifications;
6
7 use InvalidArgumentException;
8 use WC_Product;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Notification for product stock events (low stock, out of stock, backorder).
14 *
15 * @since 10.9.0
16 */
17 class StockNotification extends Notification {
18 const TYPE = 'store_stock';
19
20 const EVENT_LOW_STOCK = 'low_stock';
21 const EVENT_OUT_OF_STOCK = 'out_of_stock';
22 const EVENT_ON_BACKORDER = 'on_backorder';
23
24 const VALID_EVENT_TYPES = array(
25 self::EVENT_LOW_STOCK,
26 self::EVENT_OUT_OF_STOCK,
27 self::EVENT_ON_BACKORDER,
28 );
29
30 /**
31 * Emoji appended to the notification title, one per stock event type.
32 */
33 const EMOJI_OUT_OF_STOCK = '🚨';
34 const EMOJI_ON_BACKORDER = '🕐';
35 const EMOJI_LOW_STOCK = '⚠️';
36
37 /**
38 * The stock event that triggered this notification.
39 *
40 * @var string
41 */
42 private string $event_type;
43
44 /**
45 * Stock quantity captured at the moment the WC stock event fired.
46 *
47 * Captured at trigger time rather than read at dispatch time so the
48 * notification reflects the threshold-crossing moment, not whatever
49 * stock level the product happens to be at when the dispatcher (which
50 * runs in a separate process — internal REST endpoint or ActionScheduler
51 * safety net) eventually re-fetches the product. Avoids stale-cache reads
52 * and remains correct even if subsequent orders reduce stock further
53 * before dispatch.
54 *
55 * Only meaningful for the low_stock event today; null for the other two.
56 *
57 * @var int|null
58 */
59 private ?int $stock_quantity_at_trigger;
60
61 /**
62 * Creates a new StockNotification instance.
63 *
64 * @param int $resource_id The product ID.
65 * @param string $event_type One of the EVENT_* constants.
66 * @param int|null $stock_quantity_at_trigger Stock quantity captured when the WC stock event fired, or null if unknown.
67 *
68 * @throws InvalidArgumentException If the resource ID or event type is invalid.
69 *
70 * @since 10.9.0
71 */
72 public function __construct( int $resource_id, string $event_type = self::EVENT_LOW_STOCK, ?int $stock_quantity_at_trigger = null ) {
73 parent::__construct( $resource_id );
74
75 if ( ! in_array( $event_type, self::VALID_EVENT_TYPES, true ) ) {
76 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
77 throw new InvalidArgumentException( sprintf( 'Invalid stock notification event type: %s', $event_type ) );
78 }
79
80 $this->event_type = $event_type;
81 $this->stock_quantity_at_trigger = $stock_quantity_at_trigger;
82 }
83
84 /**
85 * Restores extra state from a serialized notification array.
86 *
87 * Called by {@see Notification::from_array()} after construction to
88 * restore the event type that the default constructor cannot receive.
89 *
90 * Throws when `event_type` is present but unrecognized so the safety-net
91 * caller (which wraps reconstruction in a try/catch) drops the corrupt
92 * job rather than silently dispatching the wrong notification subtype.
93 * A missing `event_type` is allowed — the default set by the constructor
94 * survives, which preserves backward compatibility with any in-flight
95 * scheduled actions that pre-date this field.
96 *
97 * @param array $data The serialized notification data.
98 *
99 * @throws InvalidArgumentException If `event_type` is present but not a known value.
100 *
101 * @since 10.9.0
102 */
103 public function hydrate( array $data ): void {
104 if ( array_key_exists( 'event_type', $data ) ) {
105 $event_type = $data['event_type'];
106
107 if ( ! in_array( $event_type, self::VALID_EVENT_TYPES, true ) ) {
108 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
109 throw new InvalidArgumentException( sprintf( 'Invalid stock notification event type during hydrate: %s', is_scalar( $event_type ) ? (string) $event_type : gettype( $event_type ) ) );
110 }
111
112 $this->event_type = $event_type;
113 }
114
115 if ( array_key_exists( 'stock_quantity_at_trigger', $data ) ) {
116 $stock = $data['stock_quantity_at_trigger'];
117 $this->stock_quantity_at_trigger = is_int( $stock ) ? $stock : null;
118 }
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 public function get_type(): string {
125 return self::TYPE;
126 }
127
128 /**
129 * Returns the stock event type.
130 *
131 * @return string
132 *
133 * @since 10.9.0
134 */
135 public function get_event_type(): string {
136 return $this->event_type;
137 }
138
139 /**
140 * {@inheritDoc}
141 *
142 * Includes `event_type` so the same product can have distinct
143 * notifications for different stock events in-flight simultaneously.
144 */
145 public function get_identifier(): string {
146 return sprintf(
147 '%s_%s_%s_%s',
148 get_current_blog_id(),
149 $this->get_type(),
150 $this->event_type,
151 $this->get_resource_id()
152 );
153 }
154
155 /**
156 * {@inheritDoc}
157 *
158 * Appends `event_type` because it is part of this notification's identity
159 * (see {@see self::get_identifier()}): the same product can have distinct
160 * low_stock / out_of_stock / on_backorder safety nets pending at once, and
161 * the callback needs it to reconstruct the correct subtype.
162 *
163 * `stock_quantity_at_trigger` is deliberately omitted — it is volatile
164 * payload data, not identity, and does not round-trip through every cancel
165 * path, so including it in the match key would risk breaking cancellation.
166 * The safety-net fallback message reads current product stock when it is
167 * absent (see {@see self::build_message()}).
168 *
169 * @return array<int, mixed>
170 *
171 * @since 10.9.0
172 */
173 public function get_safety_net_args(): array {
174 return array(
175 $this->get_type(),
176 $this->get_resource_id(),
177 array( 'event_type' => $this->event_type ),
178 );
179 }
180
181 /**
182 * {@inheritDoc}
183 *
184 * Extends the parent array with `event_type` and the trigger-time stock
185 * snapshot so both fields survive serialization through the safety-net
186 * scheduler and the internal-REST round-trip.
187 *
188 * @return array{type: string, resource_id: int, event_type: string, stock_quantity_at_trigger: int|null}
189 *
190 * @since 10.9.0
191 */
192 public function to_array(): array {
193 return array_merge(
194 parent::to_array(),
195 array(
196 'event_type' => $this->event_type,
197 'stock_quantity_at_trigger' => $this->stock_quantity_at_trigger,
198 )
199 );
200 }
201
202 /**
203 * Returns the WPCOM-ready payload for this notification.
204 *
205 * Returns null if the product no longer exists.
206 *
207 * @return array|null
208 *
209 * @since 10.9.0
210 */
211 public function to_payload(): ?array {
212 $product = WC()->call_function( 'wc_get_product', $this->get_resource_id() );
213
214 if ( ! $product || ! $product instanceof WC_Product ) {
215 return null;
216 }
217
218 $product_name = wp_strip_all_tags( $product->get_name() );
219 $site_title = wp_strip_all_tags( get_bloginfo( 'name' ) );
220
221 // For variations, `meta.product_id` is the parent product ID so the mobile app
222 // can always navigate to the product details screen. `resource_id` keeps the
223 // actual entity ID (variation or simple product) for identification and dedup.
224 $is_variation = $product->is_type( 'variation' );
225 $product_id = $is_variation ? $product->get_parent_id() : $product->get_id();
226
227 return array(
228 'type' => $this->get_type(),
229 'timestamp' => gmdate( 'c' ),
230 'resource_id' => $this->get_resource_id(),
231 'title' => $this->build_title( $product_name ),
232 'message' => $this->build_message( $product_name, $site_title, $product ),
233 'meta' => array(
234 'product_id' => $product_id,
235 'event_type' => $this->event_type,
236 ),
237 );
238 }
239
240 /**
241 * {@inheritDoc}
242 *
243 * Extends the base enabled-toggle check with per-event sub-flag filtering.
244 *
245 * @param mixed $pref_value The user's stored preference value, or null.
246 * @return bool
247 *
248 * @since 10.9.0
249 */
250 public function should_send_to_user( $pref_value ): bool {
251 if ( ! parent::should_send_to_user( $pref_value ) ) {
252 return false;
253 }
254
255 if ( ! is_array( $pref_value ) || ! array_key_exists( $this->event_type, $pref_value ) ) {
256 return true;
257 }
258
259 return (bool) $pref_value[ $this->event_type ];
260 }
261
262 /**
263 * {@inheritDoc}
264 *
265 * @param string $key The meta key.
266 */
267 public function has_meta( string $key ): bool {
268 $product = WC()->call_function( 'wc_get_product', $this->get_resource_id() );
269 return $product instanceof WC_Product && $product->meta_exists( $key . '_' . $this->event_type );
270 }
271
272 /**
273 * {@inheritDoc}
274 *
275 * @param string $key The meta key.
276 */
277 public function write_meta( string $key ): void {
278 $product = WC()->call_function( 'wc_get_product', $this->get_resource_id() );
279
280 if ( $product instanceof WC_Product ) {
281 $product->update_meta_data( $key . '_' . $this->event_type, (string) time() );
282 $product->save_meta_data();
283 }
284 }
285
286 /**
287 * {@inheritDoc}
288 *
289 * @param string $key The meta key.
290 */
291 public function delete_meta( string $key ): void {
292 $product = WC()->call_function( 'wc_get_product', $this->get_resource_id() );
293
294 if ( $product instanceof WC_Product ) {
295 $product->delete_meta_data( $key . '_' . $this->event_type );
296 $product->save_meta_data();
297 }
298 }
299
300 /**
301 * Builds the title payload for the notification.
302 *
303 * @param string $product_name The sanitized product name.
304 * @return array{format: string, args: string[]}
305 */
306 private function build_title( string $product_name ): array {
307 switch ( $this->event_type ) {
308 case self::EVENT_OUT_OF_STOCK:
309 return array(
310 'format' => 'Out of stock: %1$s %2$s',
311 'args' => array( $product_name, self::EMOJI_OUT_OF_STOCK ),
312 );
313
314 case self::EVENT_ON_BACKORDER:
315 return array(
316 'format' => 'Backordered: %1$s %2$s',
317 'args' => array( $product_name, self::EMOJI_ON_BACKORDER ),
318 );
319
320 default:
321 return array(
322 'format' => 'Low stock: %1$s %2$s',
323 'args' => array( $product_name, self::EMOJI_LOW_STOCK ),
324 );
325 }
326 }
327
328 /**
329 * Builds the message payload for the notification.
330 *
331 * @param string $product_name The sanitized product name.
332 * @param string $site_title The sanitized site title.
333 * @param WC_Product $product The product object (used as a fallback when no trigger-time stock was captured).
334 * @return array{format: string, args: string[]}
335 */
336 private function build_message( string $product_name, string $site_title, WC_Product $product ): array {
337 switch ( $this->event_type ) {
338 case self::EVENT_OUT_OF_STOCK:
339 return array(
340 'format' => '%1$s is out of stock on %2$s',
341 'args' => array( $product_name, $site_title ),
342 );
343
344 case self::EVENT_ON_BACKORDER:
345 return array(
346 'format' => '%1$s has been backordered on %2$s',
347 'args' => array( $product_name, $site_title ),
348 );
349
350 default:
351 $stock = null !== $this->stock_quantity_at_trigger
352 ? $this->stock_quantity_at_trigger
353 : $product->get_stock_quantity();
354
355 return array(
356 'format' => '%1$s is running low (%2$s remaining) on %3$s',
357 'args' => array(
358 $product_name,
359 (string) $stock,
360 $site_title,
361 ),
362 );
363 }//end switch
364 }
365 }
366