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 |