NewOrderNotificationTrigger.php
3 months ago
NewReviewNotificationTrigger.php
3 months ago
StockNotificationRecoveryHandler.php
4 weeks ago
StockNotificationTrigger.php
4 weeks ago
NewOrderNotificationTrigger.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Triggers; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\PushNotifications\Notifications\NewOrderNotification; |
| 8 | use Automattic\WooCommerce\Internal\PushNotifications\Services\PendingNotificationStore; |
| 9 | use WC_Order; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Listens for new and status-changed orders and feeds notifications into |
| 15 | * the PendingNotificationStore. |
| 16 | * |
| 17 | * @since 10.7.0 |
| 18 | */ |
| 19 | class NewOrderNotificationTrigger { |
| 20 | /** |
| 21 | * Order statuses that should trigger a notification. |
| 22 | */ |
| 23 | const NOTIFIABLE_STATUSES = array( |
| 24 | /** |
| 25 | * Source: WooCommerce plugin. |
| 26 | */ |
| 27 | 'processing', |
| 28 | 'on-hold', |
| 29 | 'completed', |
| 30 | /** |
| 31 | * Source: WooCommerce Pre-Orders plugin. |
| 32 | */ |
| 33 | 'pre-order', |
| 34 | /** |
| 35 | * Source: WPCOM - "commonly used custom pre-order status". |
| 36 | */ |
| 37 | 'pre-ordered', |
| 38 | /** |
| 39 | * Source: WooCommerce Deposits plugin. |
| 40 | */ |
| 41 | 'partial-payment', |
| 42 | ); |
| 43 | |
| 44 | /** |
| 45 | * Registers WordPress hooks for order events. |
| 46 | * |
| 47 | * @return void |
| 48 | * |
| 49 | * @since 10.7.0 |
| 50 | */ |
| 51 | public function register(): void { |
| 52 | add_action( 'woocommerce_new_order', array( $this, 'on_new_order' ), 10, 2 ); |
| 53 | add_action( 'woocommerce_order_status_changed', array( $this, 'on_order_status_changed' ), 10, 4 ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handles the woocommerce_new_order hook. |
| 58 | * |
| 59 | * @param int $order_id The order ID. |
| 60 | * @param WC_Order $order The order object. |
| 61 | * @return void |
| 62 | * |
| 63 | * @since 10.7.0 |
| 64 | */ |
| 65 | public function on_new_order( int $order_id, WC_Order $order ): void { |
| 66 | if ( ! in_array( $order->get_status(), self::NOTIFIABLE_STATUSES, true ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | wc_get_container()->get( PendingNotificationStore::class )->add( |
| 71 | new NewOrderNotification( $order_id ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 76 | /** |
| 77 | * Handles the woocommerce_order_status_changed hook. |
| 78 | * |
| 79 | * @param int $order_id The order ID. |
| 80 | * @param string $previous_status The previous order status. |
| 81 | * @param string $next_status The new order status. |
| 82 | * @param WC_Order $order The order object. |
| 83 | * @return void |
| 84 | * |
| 85 | * @since 10.7.0 |
| 86 | */ |
| 87 | public function on_order_status_changed( |
| 88 | int $order_id, |
| 89 | string $previous_status, |
| 90 | string $next_status, |
| 91 | WC_Order $order |
| 92 | ): void { |
| 93 | // phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 94 | if ( |
| 95 | in_array( $previous_status, self::NOTIFIABLE_STATUSES, true ) |
| 96 | || ! in_array( $next_status, self::NOTIFIABLE_STATUSES, true ) |
| 97 | ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | wc_get_container()->get( PendingNotificationStore::class )->add( |
| 102 | new NewOrderNotification( $order_id ) |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 |