Skip to content

Commit 790cea9

Browse files
Reformat source code
1 parent d1910cb commit 790cea9

File tree

1 file changed

+79
-66
lines changed

1 file changed

+79
-66
lines changed

woocommerce-slack.php

+79-66
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,82 @@
1010
* License: GPL v2 or later
1111
*/
1212

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/");
1414

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');
1717

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) {
2224
self::$instance = new self;
2325
self::$instance->init();
2426
}
2527
return self::$instance;
2628
}
27-
public function init() {
29+
public function init()
30+
{
2831

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+
}
3336

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+
}
3841

39-
add_action( 'init', array( $this, 'wp_init'), 10, 3 );
42+
add_action('init', array($this, 'wp_init'), 10, 3);
4043
// add_action( 'admin_init', array( $this, 'wp_admin_init'), 10, 3 );
4144
}
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'));
4953
}
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'));
5357
}
5458
}
5559

56-
public function enqueue_scripts() {
57-
?>
60+
public function enqueue_scripts()
61+
{
62+
?>
5863
<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+
});
7177
});
72-
});
7378
</script>
7479
<?php
7580
}
76-
81+
7782
/**
7883
* Retrieves the ID of the most recent WooCommerce order.
7984
*
8085
* @return int|null The ID of the last order or null if no orders are found.
8186
*/
82-
public function get_last_order_id() {
87+
public function get_last_order_id()
88+
{
8389
global $wpdb;
8490

8591
// Get all WooCommerce order statuses
@@ -107,7 +113,8 @@ public function get_last_order_id() {
107113
* This function checks the status of the latest order and sends a test message to Slack
108114
* indicating if the order is completed or cancelled.
109115
*/
110-
public function test_slack() {
116+
public function test_slack()
117+
{
111118
// Retrieve the ID of the latest order
112119
$latestOrderId = $this->get_last_order_id();
113120
$order = wc_get_order($latestOrderId);
@@ -127,15 +134,15 @@ public function test_slack() {
127134
$orderItems = $order->get_items();
128135
if (count($orderItems) == 1) {
129136
foreach ($orderItems as $item) {
130-
$productDetails = $item->get_data();
137+
$productDetails = $item->get_data();
131138
}
132139
}
133140

134141
// Construct the Slack message
135142
$customerName = $order->data['billing']['first_name'] . " " . $order->data['billing']['last_name'];
136143
$formattedTotal = number_format($order->data['total'], 2);
137144
$message = "";
138-
if( isset($productDetails['name'])){
145+
if (isset($productDetails['name'])) {
139146
$message = "{$productDetails['name']}\n";
140147
}
141148
$message .= "Order *#{$order->ID}* by *{$customerName}* for *{$formattedTotal}* has been {$status}";
@@ -145,17 +152,18 @@ public function test_slack() {
145152
$this->slack_message($message, "Order " . ucfirst($orderStatus) . " TEST", $channel, $icon);
146153

147154
// Terminate execution
148-
wp_die();
155+
wp_die();
149156
}
150-
157+
151158
/**
152159
* Updates the order status to 'completed' if certain conditions are met.
153160
*
154161
* @param string $current_status The current status of the order.
155162
* @param int $order_id The ID of the WooCommerce order.
156163
* @return string The updated order status.
157164
*/
158-
public function rfvc_update_order_status($current_status, $order_id) {
165+
public function rfvc_update_order_status($current_status, $order_id)
166+
{
159167
// Create an instance of the WooCommerce Order
160168
$order = new WC_Order($order_id);
161169

@@ -181,10 +189,11 @@ public function rfvc_update_order_status($current_status, $order_id) {
181189
* @param string $icon Emoji icon to represent the user (default is robot face emoji).
182190
* @return mixed The result of the Slack API call.
183191
*/
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+
{
185194
$slackToken = get_option('wc_settings_tab_slack_token');
186195
$slackUrl = "https://slack.com/api/chat.postMessage";
187-
196+
188197
// Prepare the data for POST request
189198
$postData = http_build_query([
190199
"token" => $slackToken,
@@ -210,16 +219,19 @@ public static function slack_message($message, $username, $channel, $icon = ":ro
210219
}
211220
curl_close($curl);
212221

213-
return $result;
222+
return $result;
214223
}
215224

216-
public function mysite_woocommerce_order_status_completed( $order_id ) {
225+
public function mysite_woocommerce_order_status_completed($order_id)
226+
{
217227
$this->woocommerce_order_status_report($order_id, 'completed');
218228
}
219-
public function mysite_woocommerce_order_status_cancelled( $order_id ) {
229+
public function mysite_woocommerce_order_status_cancelled($order_id)
230+
{
220231
$this->woocommerce_order_status_report($order_id, 'cancelled');
221232
}
222-
public function mysite_woocommerce_order_status_failed( $order_id ) {
233+
public function mysite_woocommerce_order_status_failed($order_id)
234+
{
223235
$this->woocommerce_order_status_report($order_id, 'failed');
224236
}
225237

@@ -230,45 +242,46 @@ public function mysite_woocommerce_order_status_failed( $order_id ) {
230242
* @param string $status The new status of the order.
231243
* @return void
232244
*/
233-
public function woocommerce_order_status_report($order_id, $status) {
245+
public function woocommerce_order_status_report($order_id, $status)
246+
{
234247
// Retrieve order and order items
235248
$order = wc_get_order($order_id);
236249
$order_items = $order->get_items();
237-
250+
238251
// Initialize product name
239252
$product_name = "";
240-
253+
241254
// Extract product name if only one item in order
242255
if (count($order_items) == 1) {
243256
foreach ($order_items as $item) {
244257
$product_data = $item->get_data();
245258
$product_name = preg_replace("# - Other#", "", $product_data['name']);
246259
}
247260
}
248-
261+
249262
// Edge case: if no product is found, return or handle accordingly
250263
if (empty($product_name)) {
251264
return; // or handle as needed
252265
}
253-
266+
254267
// Determine the status phrasing
255268
$hasbeen = $status == 'failed' ? 'has' : 'has been';
256-
269+
257270
// Format the order total
258271
$formatted_total = number_format($order->data['total'], 2);
259-
272+
260273
// Construct the message
261274
$customer_name = $order->data['billing']['first_name'] . " " . $order->data['billing']['last_name'];
262275
$message = "{$product_name}\nOrder *#{$order->ID}* by *{$customer_name}* for *${formatted_total}* {$hasbeen} {$status}";
263-
276+
264277
// Retrieve Slack channel and icon settings
265278
$channel = "#" . get_option('wc_settings_tab_slack_woocommerce_channel');
266279
$icon = get_option('wc_settings_tab_slack_woocommerce_slack_icon_' . $status);
267-
280+
268281
// Send the message to Slack
269282
$this->slack_message($message, "Order " . ucfirst($status), $channel, $icon);
270283
}
271-
284+
272285
}
273286
$wp_slack_woocommerce = new wp_slack_woocommerce;
274287
$wp_slack_woocommerce->init();

0 commit comments

Comments
 (0)