Skip to content

Commit a01e3fa

Browse files
committed
Added more WooCommerce events
1 parent 2da0977 commit a01e3fa

17 files changed

+398
-154
lines changed

includes/Api/Notifications.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public function get_items( $request ) {
7070
'route' => 'sms',
7171
'group' => $obj->get_group(),
7272
'recipients' => $obj->get_recipients_raw(),
73-
'replacements' => array_keys( $obj->replacement_keys() ),
73+
'replacements' => array_merge(
74+
array_keys( $obj->replacement_keys() ),
75+
array_keys( $obj->global_replacement_keys() )
76+
),
7477
];
7578
}, $notifications ),
7679
];

includes/Dispatcher.php

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Texty;
44

5+
use Texty\Integrations\WooCommerce;
6+
57
/**
68
* Dispatcher Class
79
*/
@@ -16,8 +18,8 @@ public function __construct() {
1618
add_action( 'user_register', [ $this, 'user_register' ] );
1719
add_action( 'comment_post', [ $this, 'new_comment' ] );
1820

19-
// WooCommerce Events
20-
add_action( 'woocommerce_order_status_changed', [ $this, 'order_status_changed' ], 10, 4 );
21+
// WooCommerce
22+
new WooCommerce();
2123
}
2224

2325
/**
@@ -31,12 +33,7 @@ public function user_register( $user_id ) {
3133
$class = texty()->notifications()->get( 'registration' );
3234
$notifier = new $class();
3335

34-
if ( ! $notifier->enabled() ) {
35-
return;
36-
}
37-
3836
$notifier->set_user( $user_id );
39-
4037
$notifier->send();
4138
}
4239

@@ -51,43 +48,7 @@ public function new_comment( $comment_id ) {
5148
$class = texty()->notifications()->get( 'comment' );
5249
$notifier = new $class();
5350

54-
if ( ! $notifier->enabled() ) {
55-
return;
56-
}
57-
5851
$notifier->set_comment( $comment_id );
5952
$notifier->send();
6053
}
61-
62-
/**
63-
* Send a message when an order status changes
64-
*
65-
* @param int $order_id
66-
* @param string $from
67-
* @param string $to
68-
* @param WC_Order $order
69-
*
70-
* @return void
71-
*/
72-
public function order_status_changed( $order_id, $from, $to, $order ) {
73-
$new_statuses = [ 'processing', 'completed' ];
74-
75-
if ( in_array( $to, $new_statuses ) ) {
76-
$class = texty()->notifications()->get( 'order_admin' );
77-
$order_admin = new $class();
78-
79-
// if enabled and we haven't sent it already
80-
if ( $order_admin->enabled() ) {
81-
$has_sent = $order->get_meta( '_texty_order_admin', true );
82-
83-
if ( ! $has_sent ) {
84-
$order_admin->set_order( $order );
85-
$order_admin->send();
86-
87-
$order->add_meta_data( '_texty_order_admin', 1 );
88-
$order->save_meta_data();
89-
}
90-
}
91-
}
92-
}
9354
}

includes/Gateways/Twilio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function get_settings() {
7474
'name' => __( 'From Number', 'texty' ),
7575
'type' => 'text',
7676
'value' => isset( $creds['from'] ) ? $creds['from'] : '',
77-
'help' => '',
77+
'help' => __( 'Must be a valid number associated with your Twilio account', 'texty' ),
7878
],
7979
];
8080
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Texty\Integrations;
4+
5+
/**
6+
* WooCommerce Integration Class
7+
*/
8+
class WooCommerce {
9+
10+
/**
11+
* Initialize
12+
*/
13+
public function __construct() {
14+
add_action( 'woocommerce_order_status_changed', [ $this, 'order_status_changed' ], 10, 4 );
15+
}
16+
17+
/**
18+
* Send a message when an order status changes
19+
*
20+
* @param int $order_id
21+
* @param string $old_status
22+
* @param string $order_status
23+
* @param WC_Order $order
24+
*
25+
* @return void
26+
*/
27+
public function order_status_changed( $order_id, $old_status, $order_status, $order ) {
28+
// don't process sub-orders
29+
if ( $order->get_parent_id() ) {
30+
return;
31+
}
32+
33+
switch ( $order_status ) {
34+
case 'on-hold':
35+
$this->send( 'order_customer_hold', $order );
36+
break;
37+
38+
case 'processing':
39+
$this->send( 'order_admin_processing', $order );
40+
$this->send( 'order_customer_processing', $order );
41+
break;
42+
43+
case 'completed':
44+
$this->send( 'order_admin_complete', $order );
45+
$this->send( 'order_customer_complete', $order );
46+
break;
47+
48+
default:
49+
// code...
50+
break;
51+
}
52+
}
53+
54+
/**
55+
* Send notification by event
56+
*
57+
* @param string $event
58+
* @param WC_Order $order
59+
*
60+
* @return void
61+
*/
62+
private function send( $event, $order ) {
63+
$class = texty()->notifications()->get( $event );
64+
$notification = new $class();
65+
66+
$notification->set_order( $order );
67+
$notification->send();
68+
}
69+
}

includes/Notifications.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,19 @@ public function all() {
4949
$notifications = [
5050
'registration' => __NAMESPACE__ . '\Notifications\Registration',
5151
'comment' => __NAMESPACE__ . '\Notifications\Comment',
52-
'order_admin' => __NAMESPACE__ . '\Notifications\OrderAdmin',
5352
];
5453

54+
if ( class_exists( 'WooCommerce' ) ) {
55+
// WC Admin
56+
$notifications['order_admin_processing'] = __NAMESPACE__ . '\Notifications\OrderProcessingAdmin';
57+
$notifications['order_admin_complete'] = __NAMESPACE__ . '\Notifications\OrderCompleteAdmin';
58+
59+
// WC Customers
60+
$notifications['order_customer_hold'] = __NAMESPACE__ . '\Notifications\OrderHoldCustomer';
61+
$notifications['order_customer_processing'] = __NAMESPACE__ . '\Notifications\OrderProcessingCustomer';
62+
$notifications['order_customer_complete'] = __NAMESPACE__ . '\Notifications\OrderCompleteCustomer';
63+
}
64+
5565
$this->notifications = apply_filters( 'texty_available_notifications', $notifications );
5666

5767
return $this->notifications;
@@ -67,10 +77,12 @@ public function get_groups() {
6777
'wp' => [
6878
'title' => __( 'WordPress', 'texty' ),
6979
'description' => '',
80+
'available' => true,
7081
],
7182
'wc' => [
7283
'title' => __( 'WooCommerce', 'texty' ),
7384
'description' => '',
85+
'available' => class_exists( 'WooCommerce' ) ? true : false,
7486
],
7587
] );
7688
}

includes/Notifications/Comment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function get_message() {
5555
$message = str_replace( '{' . $search . '}', $value, $message );
5656
}
5757

58+
$message = $this->replace_global_keys( $message );
59+
5860
return $message;
5961
}
6062

includes/Notifications/Notification.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ public function replacement_keys() {
124124
return __return_empty_array();
125125
}
126126

127+
/**
128+
* Global replacement keys
129+
*
130+
* @return array
131+
*/
132+
public function global_replacement_keys() {
133+
return [
134+
'site_name' => 'get_bloginfo',
135+
'site_url' => 'home_url',
136+
];
137+
}
138+
139+
/**
140+
* Replace messages with global replacement keys
141+
*
142+
* @param string $message
143+
*
144+
* @return string
145+
*/
146+
protected function replace_global_keys( $message ) {
147+
foreach ( $this->global_replacement_keys() as $search => $function ) {
148+
$message = str_replace( '{' . $search . '}', $function(), $message );
149+
}
150+
151+
return $message;
152+
}
153+
127154
/**
128155
* Return recipients
129156
*
@@ -226,15 +253,21 @@ public function settings() {
226253
* @return void
227254
*/
228255
public function send() {
256+
if ( ! $this->enabled() ) {
257+
return;
258+
}
259+
229260
$recipients = $this->get_recipients();
230261

231-
if ( $recipients ) {
232-
$content = $this->get_message();
233-
$gateway = texty()->gateways();
262+
if ( ! $recipients ) {
263+
return;
264+
}
234265

235-
foreach ( $recipients as $number ) {
236-
$gateway->send( $number, $content );
237-
}
266+
$content = $this->get_message();
267+
$gateway = texty()->gateways();
268+
269+
foreach ( $recipients as $number ) {
270+
$gateway->send( $number, $content );
238271
}
239272
}
240273
}

includes/Notifications/OrderAdmin.php renamed to includes/Notifications/OrderBase.php

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,12 @@
22

33
namespace Texty\Notifications;
44

5-
class OrderAdmin extends Notification {
5+
class OrderBase extends Notification {
66

77
/**
88
* @var object
99
*/
10-
private $order;
11-
12-
/**
13-
* Initialize
14-
*/
15-
public function __construct() {
16-
$this->title = __( 'New Order (admin)', 'texty' );
17-
$this->id = 'order_admin';
18-
$this->group = 'wc';
19-
$this->default_recipients = ['administrator'];
20-
21-
$this->default = <<<'EOD'
22-
New order received #{order_id}, paid via {payment_method}.
23-
24-
Customer: {customer_name} ({customer_email})
25-
Status: {status}
26-
Order Total: {order_total}
27-
EOD;
28-
}
10+
protected $order;
2911

3012
/**
3113
* Set the user ID
@@ -55,17 +37,15 @@ public function get_message() {
5537
foreach ( $this->replacement_keys() as $search => $method ) {
5638
$value = method_exists( $this->order, $method ) ? $this->order->$method() : '';
5739

58-
if ( 'customer_name' === $search ) {
59-
$value = $this->order->get_user()->display_name;
60-
}
61-
6240
if ( 'order_total' === $search ) {
6341
$value = strip_tags( html_entity_decode( $value ) );
6442
}
6543

6644
$message = str_replace( '{' . $search . '}', $value, $message );
6745
}
6846

47+
$message = $this->replace_global_keys( $message );
48+
6949
return $message;
7050
}
7151

@@ -91,11 +71,48 @@ public function replacement_keys() {
9171
'payment_method' => 'get_payment_method_title',
9272
'shipping_method' => 'get_shipping_method',
9373
'transaction_id' => 'get_transaction_id',
94-
'customer_name' => 'customer_name',
95-
'customer_email' => 'get_billing_email',
74+
'billing_name' => 'get_formatted_billing_full_name',
75+
'billing_email' => 'get_billing_email',
9676
'order_total' => 'get_formatted_order_total',
9777
'shipping_total' => 'get_shipping_total',
9878
'tax_total' => 'get_total_tax',
9979
];
10080
}
81+
82+
public function send() {
83+
if ( ! $this->enabled() ) {
84+
return;
85+
}
86+
87+
$meta_key = '_texty_' . $this->get_id();
88+
$has_sent = $this->order->get_meta( $meta_key, true );
89+
90+
// if we've already sent the message, don't send again
91+
if ( $has_sent ) {
92+
return;
93+
}
94+
95+
// mark as sent
96+
$this->order->add_meta_data( $meta_key, 1 );
97+
$this->order->save_meta_data();
98+
99+
if ( 'user' === $this->get_type() ) {
100+
$number = $this->order->get_billing_phone();
101+
102+
$recipients = $number ? [ $number ] : [];
103+
} else {
104+
$recipients = $this->get_recipients();
105+
}
106+
107+
if ( ! $recipients ) {
108+
return;
109+
}
110+
111+
$content = $this->get_message();
112+
$gateway = texty()->gateways();
113+
114+
foreach ( $recipients as $number ) {
115+
$gateway->send( $number, $content );
116+
}
117+
}
101118
}

0 commit comments

Comments
 (0)