NewOrderNotification.php
4 weeks ago
NewReviewNotification.php
4 weeks ago
Notification.php
4 weeks ago
StockNotification.php
4 weeks ago
NewOrderNotification.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\PushNotifications\Notifications; |
| 6 | |
| 7 | use WC_Order; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Notification for new WooCommerce orders. |
| 13 | * |
| 14 | * @since 10.7.0 |
| 15 | */ |
| 16 | class NewOrderNotification extends Notification { |
| 17 | /** |
| 18 | * The notification type identifier for new orders. |
| 19 | */ |
| 20 | const TYPE = 'store_order'; |
| 21 | |
| 22 | /** |
| 23 | * An array of emojis to select from when forming the payload. |
| 24 | */ |
| 25 | const EMOJI_LIST = array( '🎉', '🎊', '🥳', '👏', '🙌' ); |
| 26 | |
| 27 | /** |
| 28 | * {@inheritDoc} |
| 29 | */ |
| 30 | public function get_type(): string { |
| 31 | return self::TYPE; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Returns the WPCOM-ready payload for this notification. |
| 36 | * |
| 37 | * Returns null if the order no longer exists. |
| 38 | * |
| 39 | * @return array|null |
| 40 | * |
| 41 | * @since 10.7.0 |
| 42 | */ |
| 43 | public function to_payload(): ?array { |
| 44 | $order = WC()->call_function( 'wc_get_order', $this->get_resource_id() ); |
| 45 | |
| 46 | if ( ! $order || ! $order instanceof WC_Order ) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | return array( |
| 51 | 'type' => $this->get_type(), |
| 52 | // This represents the time the notification was triggered, so we can monitor age of notification at delivery. |
| 53 | 'timestamp' => gmdate( 'c' ), |
| 54 | 'resource_id' => $this->get_resource_id(), |
| 55 | 'title' => array( |
| 56 | /** |
| 57 | * This will be translated in WordPress.com, format: |
| 58 | * 1: emoji |
| 59 | */ |
| 60 | 'format' => 'You have a new order! %1$s', |
| 61 | 'args' => array( self::EMOJI_LIST[ wp_rand( 0, count( self::EMOJI_LIST ) - 1 ) ] ), |
| 62 | ), |
| 63 | 'message' => array( |
| 64 | /** |
| 65 | * This will be translated in WordPress.com, format: |
| 66 | * 1: order total, 2: site title |
| 67 | */ |
| 68 | 'format' => 'New order for %1$s on %2$s', |
| 69 | 'args' => array( |
| 70 | wp_strip_all_tags( $order->get_formatted_order_total() ), |
| 71 | wp_strip_all_tags( get_bloginfo( 'name' ) ), |
| 72 | ), |
| 73 | ), |
| 74 | 'meta' => array( |
| 75 | 'order_id' => $this->get_resource_id(), |
| 76 | ), |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritDoc} |
| 82 | * |
| 83 | * Extends the base enabled-toggle check with a minimum-amount threshold. |
| 84 | * When `min_amount` is set in the user's preferences, the order total must |
| 85 | * meet or exceed it for the notification to be sent. |
| 86 | * |
| 87 | * The threshold is interpreted in the order's currency; no currency |
| 88 | * conversion is performed. This mirrors how `WC_Coupon::minimum_amount` |
| 89 | * behaves, so multi-currency merchants should set thresholds with that |
| 90 | * in mind. |
| 91 | * |
| 92 | * @param mixed $pref_value The user's stored preference value, or null. |
| 93 | * @return bool |
| 94 | * |
| 95 | * @since 10.9.0 |
| 96 | */ |
| 97 | public function should_send_to_user( $pref_value ): bool { |
| 98 | if ( ! parent::should_send_to_user( $pref_value ) ) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | if ( ! is_array( $pref_value ) || ! isset( $pref_value['min_amount'] ) ) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | $min_amount = (float) $pref_value['min_amount']; |
| 107 | if ( $min_amount <= 0 ) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | $order = WC()->call_function( 'wc_get_order', $this->get_resource_id() ); |
| 112 | if ( ! $order instanceof WC_Order ) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return (float) $order->get_total() >= $min_amount; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * {@inheritDoc} |
| 121 | * |
| 122 | * @param string $key The meta key. |
| 123 | */ |
| 124 | public function has_meta( string $key ): bool { |
| 125 | $order = WC()->call_function( 'wc_get_order', $this->get_resource_id() ); |
| 126 | return $order instanceof WC_Order && $order->meta_exists( $key ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * {@inheritDoc} |
| 131 | * |
| 132 | * @param string $key The meta key. |
| 133 | */ |
| 134 | public function write_meta( string $key ): void { |
| 135 | $order = WC()->call_function( 'wc_get_order', $this->get_resource_id() ); |
| 136 | |
| 137 | if ( $order instanceof WC_Order ) { |
| 138 | $order->update_meta_data( $key, (string) time() ); |
| 139 | $order->save_meta_data(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | * |
| 146 | * @param string $key The meta key. |
| 147 | */ |
| 148 | public function delete_meta( string $key ): void { |
| 149 | $order = WC()->call_function( 'wc_get_order', $this->get_resource_id() ); |
| 150 | |
| 151 | if ( $order instanceof WC_Order ) { |
| 152 | $order->delete_meta_data( $key ); |
| 153 | $order->save_meta_data(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 |