10
10
* License: GPL v2 or later
11
11
*/
12
12
13
- define ( 'WOOCOMMERCE_SLACK_ABS_PATH ' , ABSPATH . "wp-content/plugins/woocommerce-slack-notifications/ " );
13
+ define ('WOOCOMMERCE_SLACK_ABS_PATH ' , ABSPATH . "wp-content/plugins/woocommerce-slack-notifications/ " );
14
14
15
- include (WOOCOMMERCE_SLACK_ABS_PATH . 'class-testmode.php ' );
16
- include (WOOCOMMERCE_SLACK_ABS_PATH . 'class-settings.php ' );
15
+ include (WOOCOMMERCE_SLACK_ABS_PATH . 'class-testmode.php ' );
16
+ include (WOOCOMMERCE_SLACK_ABS_PATH . 'class-settings.php ' );
17
17
18
- class wp_slack_woocommerce{
19
- private static $ instance = false ;
20
- public static function instance () {
21
- if ( ! self ::$ instance ) {
18
+ class wp_slack_woocommerce
19
+ {
20
+ private static $ instance = false ;
21
+ public static function instance ()
22
+ {
23
+ if (!self ::$ instance ) {
22
24
self ::$ instance = new self ;
23
25
self ::$ instance ->init ();
24
26
}
25
27
return self ::$ instance ;
26
28
}
27
- public function init () {
29
+ public function init ()
30
+ {
28
31
29
- if ( class_exists ( 'wp_slack_woocommerce_testmode ' ) ) {
30
- $ wp_slack_woocommerce_testmode = new wp_slack_woocommerce_testmode ;
31
- $ wp_slack_woocommerce_testmode ->init ();
32
- }
32
+ if (class_exists ('wp_slack_woocommerce_testmode ' ) ) {
33
+ $ wp_slack_woocommerce_testmode = new wp_slack_woocommerce_testmode ;
34
+ $ wp_slack_woocommerce_testmode ->init ();
35
+ }
33
36
34
- if ( class_exists ( 'wp_slack_woocommerce_settings ' ) ) {
35
- $ wp_slack_woocommerce_settings = new wp_slack_woocommerce_settings ;
36
- $ wp_slack_woocommerce_settings ->init ();
37
- }
37
+ if (class_exists ('wp_slack_woocommerce_settings ' ) ) {
38
+ $ wp_slack_woocommerce_settings = new wp_slack_woocommerce_settings ;
39
+ $ wp_slack_woocommerce_settings ->init ();
40
+ }
38
41
39
- add_action ( 'init ' , array ( $ this , 'wp_init ' ), 10 , 3 );
42
+ add_action ('init ' , array ($ this , 'wp_init ' ), 10 , 3 );
40
43
// add_action( 'admin_init', array( $this, 'wp_admin_init'), 10, 3 );
41
44
}
42
- public function wp_init () {
43
- $ enabled = get_option ( 'wc_settings_tab_slack_woocommerce_enable_notifications ' );
44
- if ( $ enabled == 'yes ' ){
45
- add_filter ( 'woocommerce_payment_complete_order_status ' , array ( $ this , 'rfvc_update_order_status ' ), 10 , 2 );
46
- add_action ( 'woocommerce_order_status_completed ' , array ( $ this , 'mysite_woocommerce_order_status_completed ' ));
47
- add_action ( 'woocommerce_order_status_cancelled ' , array ( $ this , 'mysite_woocommerce_order_status_cancelled ' ));
48
- add_action ( 'woocommerce_order_status_failed ' , array ( $ this , 'mysite_woocommerce_order_status_failed ' ));
45
+ public function wp_init ()
46
+ {
47
+ $ enabled = get_option ('wc_settings_tab_slack_woocommerce_enable_notifications ' );
48
+ if ($ enabled == 'yes ' ) {
49
+ add_filter ('woocommerce_payment_complete_order_status ' , array ($ this , 'rfvc_update_order_status ' ), 10 , 2 );
50
+ add_action ('woocommerce_order_status_completed ' , array ($ this , 'mysite_woocommerce_order_status_completed ' ));
51
+ add_action ('woocommerce_order_status_cancelled ' , array ($ this , 'mysite_woocommerce_order_status_cancelled ' ));
52
+ add_action ('woocommerce_order_status_failed ' , array ($ this , 'mysite_woocommerce_order_status_failed ' ));
49
53
}
50
- if ( is_admin ()) {
51
- add_action ( 'wp_ajax_test_slack ' , array ( $ this , 'test_slack ' ) );
52
- add_action ( 'in_admin_footer ' , array ( $ this , 'enqueue_scripts ' ));
54
+ if ( is_admin ()) {
55
+ add_action ('wp_ajax_test_slack ' , array ($ this , 'test_slack ' ));
56
+ add_action ('in_admin_footer ' , array ($ this , 'enqueue_scripts ' ));
53
57
}
54
58
}
55
59
56
- public function enqueue_scripts () {
57
- ?>
60
+ public function enqueue_scripts ()
61
+ {
62
+ ?>
58
63
<script>
59
- jQuery(window).ready(function(){
60
- jQuery("#test_slack").click(function() {
61
- jQuery.ajax({
62
- type : "post",
63
- url : 'admin-ajax.php',
64
- data : {
65
- action: "test_slack"
66
- },
67
- success: function(response) {
68
- console.log(response);
69
- }
70
- });
64
+ jQuery(window).ready(function () {
65
+ jQuery("#test_slack").click(function () {
66
+ jQuery.ajax({
67
+ type: "post",
68
+ url: 'admin-ajax.php',
69
+ data: {
70
+ action: "test_slack"
71
+ },
72
+ success: function (response) {
73
+ console.log(response);
74
+ }
75
+ });
76
+ });
71
77
});
72
- });
73
78
</script>
74
79
<?php
75
80
}
76
-
81
+
77
82
/**
78
83
* Retrieves the ID of the most recent WooCommerce order.
79
84
*
80
85
* @return int|null The ID of the last order or null if no orders are found.
81
86
*/
82
- public function get_last_order_id () {
87
+ public function get_last_order_id ()
88
+ {
83
89
global $ wpdb ;
84
90
85
91
// Get all WooCommerce order statuses
@@ -107,7 +113,8 @@ public function get_last_order_id() {
107
113
* This function checks the status of the latest order and sends a test message to Slack
108
114
* indicating if the order is completed or cancelled.
109
115
*/
110
- public function test_slack () {
116
+ public function test_slack ()
117
+ {
111
118
// Retrieve the ID of the latest order
112
119
$ latestOrderId = $ this ->get_last_order_id ();
113
120
$ order = wc_get_order ($ latestOrderId );
@@ -127,15 +134,15 @@ public function test_slack() {
127
134
$ orderItems = $ order ->get_items ();
128
135
if (count ($ orderItems ) == 1 ) {
129
136
foreach ($ orderItems as $ item ) {
130
- $ productDetails = $ item ->get_data ();
137
+ $ productDetails = $ item ->get_data ();
131
138
}
132
139
}
133
140
134
141
// Construct the Slack message
135
142
$ customerName = $ order ->data ['billing ' ]['first_name ' ] . " " . $ order ->data ['billing ' ]['last_name ' ];
136
143
$ formattedTotal = number_format ($ order ->data ['total ' ], 2 );
137
144
$ message = "" ;
138
- if ( isset ($ productDetails ['name ' ])){
145
+ if ( isset ($ productDetails ['name ' ])) {
139
146
$ message = "{$ productDetails ['name ' ]}\n" ;
140
147
}
141
148
$ message .= "Order *# {$ order ->ID }* by * {$ customerName }* for * {$ formattedTotal }* has been {$ status }" ;
@@ -145,17 +152,18 @@ public function test_slack() {
145
152
$ this ->slack_message ($ message , "Order " . ucfirst ($ orderStatus ) . " TEST " , $ channel , $ icon );
146
153
147
154
// Terminate execution
148
- wp_die ();
155
+ wp_die ();
149
156
}
150
-
157
+
151
158
/**
152
159
* Updates the order status to 'completed' if certain conditions are met.
153
160
*
154
161
* @param string $current_status The current status of the order.
155
162
* @param int $order_id The ID of the WooCommerce order.
156
163
* @return string The updated order status.
157
164
*/
158
- public function rfvc_update_order_status ($ current_status , $ order_id ) {
165
+ public function rfvc_update_order_status ($ current_status , $ order_id )
166
+ {
159
167
// Create an instance of the WooCommerce Order
160
168
$ order = new WC_Order ($ order_id );
161
169
@@ -181,10 +189,11 @@ public function rfvc_update_order_status($current_status, $order_id) {
181
189
* @param string $icon Emoji icon to represent the user (default is robot face emoji).
182
190
* @return mixed The result of the Slack API call.
183
191
*/
184
- public static function slack_message ($ message , $ username , $ channel , $ icon = ":robot_face: " ) {
192
+ public static function slack_message ($ message , $ username , $ channel , $ icon = ":robot_face: " )
193
+ {
185
194
$ slackToken = get_option ('wc_settings_tab_slack_token ' );
186
195
$ slackUrl = "https://slack.com/api/chat.postMessage " ;
187
-
196
+
188
197
// Prepare the data for POST request
189
198
$ postData = http_build_query ([
190
199
"token " => $ slackToken ,
@@ -210,16 +219,19 @@ public static function slack_message($message, $username, $channel, $icon = ":ro
210
219
}
211
220
curl_close ($ curl );
212
221
213
- return $ result ;
222
+ return $ result ;
214
223
}
215
224
216
- public function mysite_woocommerce_order_status_completed ( $ order_id ) {
225
+ public function mysite_woocommerce_order_status_completed ($ order_id )
226
+ {
217
227
$ this ->woocommerce_order_status_report ($ order_id , 'completed ' );
218
228
}
219
- public function mysite_woocommerce_order_status_cancelled ( $ order_id ) {
229
+ public function mysite_woocommerce_order_status_cancelled ($ order_id )
230
+ {
220
231
$ this ->woocommerce_order_status_report ($ order_id , 'cancelled ' );
221
232
}
222
- public function mysite_woocommerce_order_status_failed ( $ order_id ) {
233
+ public function mysite_woocommerce_order_status_failed ($ order_id )
234
+ {
223
235
$ this ->woocommerce_order_status_report ($ order_id , 'failed ' );
224
236
}
225
237
@@ -230,45 +242,46 @@ public function mysite_woocommerce_order_status_failed( $order_id ) {
230
242
* @param string $status The new status of the order.
231
243
* @return void
232
244
*/
233
- public function woocommerce_order_status_report ($ order_id , $ status ) {
245
+ public function woocommerce_order_status_report ($ order_id , $ status )
246
+ {
234
247
// Retrieve order and order items
235
248
$ order = wc_get_order ($ order_id );
236
249
$ order_items = $ order ->get_items ();
237
-
250
+
238
251
// Initialize product name
239
252
$ product_name = "" ;
240
-
253
+
241
254
// Extract product name if only one item in order
242
255
if (count ($ order_items ) == 1 ) {
243
256
foreach ($ order_items as $ item ) {
244
257
$ product_data = $ item ->get_data ();
245
258
$ product_name = preg_replace ("# - Other# " , "" , $ product_data ['name ' ]);
246
259
}
247
260
}
248
-
261
+
249
262
// Edge case: if no product is found, return or handle accordingly
250
263
if (empty ($ product_name )) {
251
264
return ; // or handle as needed
252
265
}
253
-
266
+
254
267
// Determine the status phrasing
255
268
$ hasbeen = $ status == 'failed ' ? 'has ' : 'has been ' ;
256
-
269
+
257
270
// Format the order total
258
271
$ formatted_total = number_format ($ order ->data ['total ' ], 2 );
259
-
272
+
260
273
// Construct the message
261
274
$ customer_name = $ order ->data ['billing ' ]['first_name ' ] . " " . $ order ->data ['billing ' ]['last_name ' ];
262
275
$ message = "{$ product_name }\nOrder *# {$ order ->ID }* by * {$ customer_name }* for * $ {formatted_total}* {$ hasbeen } {$ status }" ;
263
-
276
+
264
277
// Retrieve Slack channel and icon settings
265
278
$ channel = "# " . get_option ('wc_settings_tab_slack_woocommerce_channel ' );
266
279
$ icon = get_option ('wc_settings_tab_slack_woocommerce_slack_icon_ ' . $ status );
267
-
280
+
268
281
// Send the message to Slack
269
282
$ this ->slack_message ($ message , "Order " . ucfirst ($ status ), $ channel , $ icon );
270
283
}
271
-
284
+
272
285
}
273
286
$ wp_slack_woocommerce = new wp_slack_woocommerce ;
274
287
$ wp_slack_woocommerce ->init ();
0 commit comments