Untitled
unknown
plain_text
3 years ago
21 kB
9
Indexable
<?php
/*
Plugin Name: WooCommerce WhatsApp
Description: Beta V1.1
Version: 1.0.0
Author: NotifyX
Author URI: https://notifyx.io
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
function whatsapp_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
sms_auth varchar(255) NOT NULL,
sms_secret varchar(255) NOT NULL,
instance varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__, 'whatsapp_table');
function getEmail() {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$results = $wpdb->get_results("SELECT email FROM $table_name LIMIT 1");
if ($results) {
$token = $results[0]->email;
} else {
$token = ''; // or set a default value
}
return $token;
}
function instance() {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$results = $wpdb->get_results("SELECT instance FROM $table_name LIMIT 1");
if ($results) {
$token = $results[0]->instance;
} else {
$token = ''; // or set a default value
}
return $token;
}
function getSmsId() {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$results = $wpdb->get_results("SELECT sms_auth FROM $table_name LIMIT 1");
if ($results) {
$token = $results[0]->sms_auth;
} else {
$token = ''; // or set a default value
}
return $token;
}
function getSmsSecret() {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$results = $wpdb->get_results("SELECT sms_secret FROM $table_name LIMIT 1");
if ($results) {
$token = $results[0]->sms_secret;
} else {
$token = ''; // or set a default value
}
return $token;
}
function wpac_settings_page_html() {
if(isset($_POST['submit'])) {
wpac_save_token(getEmail(), $_POST['wpac_token']);
}
if(isset($_POST['second_submit'])) {
wpac_save_token2($_POST['wpac_id'],$_POST['wpac_sec'], getEmail());
}
if(isset($_POST['thirdsubmit'])) {
wpac_save_token3($_POST['wpac_instance'], getEmail());
}
?>
<style>
table, tr, td {
border: 1px solid black;
padding: 10px 10px;
}
</style>
<div style="width: 90%; color: white; font-weight: bolder; background-color: #0275D8; padding: 10px 20px">
<h1 style="color: white">NotifyX</h1>
</div>
<div style="width: 90%; color: black; background-color: white; padding: 10px 20px">
<h2>Welcome To NotifyX</h2>
<p>Your one place to communicating with your customers through the best medium possible!</p>
<form method="POST">
<input style="width: 40%; height: 40px" type="text" name="wpac_token" value="<?php echo esc_attr(getEmail()); ?>" />
<input style="width: 20%; height: 40px; cursor: pointer; background: #0275D8; color: white; border: none" type="submit" name="submit" value="Authorize" />
</form>
</div>
<div style="width: 90%; color: black; background-color: white; padding: 10px 20px">
<h2>Instance ID</h2>
<p>Enter the Instance ID using which you want to send the message!</p>
<form method="POST">
<input style="width: 40%; height: 40px" type="text" name="wpac_instance" value="<?php echo esc_attr(instance()); ?>" />
<input style="width: 20%; height: 40px; cursor: pointer; background: #0275D8; color: white; border: none" type="submit" name="thirdsubmit" value="Authorize" />
</form>
</div>
<br />
<div style="width: 90%; color: black; background-color: white; padding: 10px 20px">
<h2>Phone SMS</h2>
<p>If you have purchased our SMS service, please enter the credentials below to send sms on phone. You can find the information in the Integrations page</p>
<form method="POST">
<input style="width: 40%; height: 40px" type="text" name="wpac_id" value="<?php echo esc_attr(getSmsId()); ?>" />
<input style="width: 40%; height: 40px" type="text" name="wpac_sec" value="<?php echo esc_attr(getSmsSecret()); ?>" />
<input style="width: 20%; height: 40px; cursor: pointer; background: #0275D8; color: white; border: none" type="submit" name="second_submit" value="Authorize" />
</form>
</div>
<br />
<div style="width: 90%; color: black; background-color: white; padding: 10px 20px">
<h2>Getting Started</h2>
<p>Start by creating your first template, the template is the content of the message.</p>
<table>
<thead>
<tr>
<td>Code</td>
<td>Result</td>
</tr>
</thead>
<tbody>
<tr>
<td>
${first_name}
</td>
<td>
Gets the first name.
</td>
</tr>
<tr>
<td>
${last_name}
</td>
<td>
Gets the last name.
</td>
</tr>
<tr>
<td>
${phone}
</td>
<td>
Gets the phone number.
</td>
</tr>
<tr>
<td>
${address}
</td>
<td>
Gets the billing address.
</td>
</tr>
<tr>
<td>
${status}
</td>
<td>
Gets the status of the order.
</td>
</tr>
</tbody>
</table>
<br />
<br />
<table>
<thead>
<tr>
<td>Event</td>
<td>Template</td>
<td>Final Result</td>
</tr>
</thead>
<tbody>
<tr>
<td>
Checkout
</td>
<td>
<code>
Hey ${first_name}! Thank you for shopping at our store. <br /> We have recieved your order!
</code>
</td>
<td>
<code>
Hey John! Thank you for shopping at our store. <br /> We have recieved your order!
</code>
</td>
</tr>
<tr>
<td>
Order Update
</td>
<td>
<code>
Hey ${first_name}! Your order has been updated, new status is ${order_status}.
</code>
</td>
<td>
<code>
Hey John! Your order has been updated, new status Processing.
</code>
</td>
</tr>
<tr>
<td>
Order Ship
</td>
<td>
<code>
Hey ${first_name}! Your order has been shipped at address ${address}.
</code>
</td>
<td>
<code>
Hey John! Your order has been shipped at address 1234 XYZ London.
</code>
</td>
</tr>
</tbody>
</table>
</div>
<?php
settings_fields('wpac_settings');
do_settings_sections('wpac_settings');
?>
<?php
}
function wpac_save_token($prev, $token) {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$existing_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE email = %s", $prev));
if ($existing_row) {
// Update existing row
$wpdb->update($table_name, array('email' => $token), array('id' => $existing_row->id));
} else {
// Insert new row
$wpdb->insert($table_name, array('email' => $token));
}
}
function wpac_save_token2($one, $two, $email) {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$existing_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE email = %s", $email));
if ($existing_row) {
// Update existing row
$wpdb->update($table_name, array('sms_auth' => $one, 'sms_secret' => $two), array('id' => $existing_row->id));
} else {
// Insert new row
$wpdb->insert($table_name, array('sms_auth' => $one, 'sms_secret' => $two));
}
}
function wpac_save_token3($one, $email) {
global $wpdb;
$table_name = $wpdb->prefix . 'whatsapp_table';
$existing_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE email = %s", $email));
if ($existing_row) {
// Update existing row
$wpdb->update($table_name, array('instance' => $one), array('id' => $existing_row->id));
} else {
// Insert new row
$wpdb->insert($table_name, array('instance' => $one));
}
}
function wpac_sanitize_message_template($input) {
return wp_kses_post($input);
}
function wpac_register_settings() {
register_setting('wpac_settings', 'wpac_token', 'wpac_save_token');
register_setting('wpac_settings', 'wpac_id', 'wpac_sec', 'wpac_save_token2');
register_setting('wpac_settings', 'wpac_instance', 'wpac_save_token3');
}
add_action('admin_init', 'wpac_register_settings');
function wp_register_menu_page(){
add_menu_page('WhatsAPP', 'NotifyX', 'manage_options', 'whatsapp_settings', 'wpac_settings_page_html', 'dashicons-buddicons-pm', 30);
}
add_action('admin_init', 'wpac_register_settings');
// add_filter('sanitize_text_field', 'wpac_sanitize_phone_number');
add_filter('sanitize_text_field', 'wpac_sanitize_message_template');
add_action('admin_menu','wp_register_menu_page' );
// Define a function that logs the order details
function getTemplate($type) {
$email = getEmail();
$ch = curl_init();
$url = 'https://45.79.125.42/wordpress.php?email='.$email.'&type=template&action=' . $type;
error_log('INFO GET TEMPLATE: ' . $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$r = json_decode($response, true);
if(curl_errno($ch)) {
error_log('Curl error: ' . curl_error($ch));
} else {
return $r;
error_log('Response: ' . $response);
}
curl_close($ch);
}
function getName() {
$email = getEmail();
$ch = curl_init();
error_log("[ INFO ] Sending request to getName for email " . $email);
$url = 'https://45.79.125.42/wordpress.php?email='.$email.'&type=getname&action=0';
error_log('INFO GET NAME: ' . $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$r = json_decode($response, true);
if(curl_errno($ch)) {
error_log('Curl error: ' . curl_error($ch));
} else {
return $r['first_name'];
error_log('Response: ' . $response);
}
curl_close($ch);
}
function checkout_whatsapp($order_id) {
$order = wc_get_order($order_id);
// Get Template
$messageRawRaw = getTemplate(2);
error_log("MESSAGE RAW RAW " . $messageRawRaw['medium']);
$messageRaw = $messageRawRaw['template'];
error_log("[SUCCESS] Recived template in order checkout " . $messageRaw);
$message = str_replace('${first_name}', $order->get_billing_first_name(), $messageRaw);
$message = str_replace('${bill}', $order->get_total(), $message);
$message = str_replace('${status}', $order->get_status(), $message);
$name = getName();
if($messageRaw == "null" || $messageRaw == null) {
return false;
} else {
$email = getEmail();
$ch = curl_init();
error_log("[ INFO ] Sending request to Send Message for email " . $email);
$url = 'https://45.79.125.42/wordpress.php?email='.$email.'&instance='. instance() .'&type=sendmessage&action=0&sender=' . $name . '&number=' . $order->get_billing_phone() . '&message=' . urlencode($message) . '&smsid=' . getSmsId() . '&secret=' . getSmsSecret() . '&medium=' . $messageRawRaw['medium'];
error_log('INFO: ' . $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$r = json_decode($response, true);
if(curl_errno($ch)) {
error_log('Curl error: ' . curl_error($ch));
} else {
return $r['first_name'];
error_log('Response: ' . $response);
}
curl_close($ch);
}
// CURL END
// $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
// $txt = "John Doe\n";
// fwrite($myfile, $txt);
// $txt = print_r($order, true);
// fwrite($myfile, $txt);
// fclose($myfile);
}
function order_status_updated($order)
{
global $woocommerce, $custom_status_list_temp;
$order = new WC_Order($order);
$status = $order->get_status();
// Get Template
switch ($status) {
case "pending":
$messageRawRaw = getTemplate(9);
break;
case "failed":
$messageRawRaw = getTemplate(10);
break;
case "on-hold":
$messageRawRaw = getTemplate(7);
break;
case "processing":
$messageRawRaw = getTemplate(8);
break;
case "completed":
$messageRawRaw = getTemplate(2);
break;
case "refunded":
$messageRawRaw = getTemplate(11);
break;
case "cancelled":
$messageRawRaw = getTemplate(12);
break;
case "draft":
$messageRawRaw = getTemplate(13);
break;
default:
$messageRawRaw = getTemplate(2);
break;
}
error_log("MESSAGE RAW RAW " . $messageRawRaw['medium']);
$messageRaw = $messageRawRaw['template'];
error_log("[SUCCESS] Recived template in order checkout " . $messageRaw);
$message = str_replace('${first_name}', $order->get_billing_first_name(), $messageRaw);
$message = str_replace('${bill}', $order->get_total(), $message);
$message = str_replace('${status}', $order->get_status(), $message);
$name = getName();
if($messageRaw == "null" || $messageRaw == null) {
return false;
} else {
$email = getEmail();
$ch = curl_init();
error_log("[ INFO ] Sending request to Send Message for email " . $email);
$url = 'https://45.79.125.42/wordpress.php?email='.$email.'&instance='. instance() .'&type=sendmessage&action=0&sender=' . $name . '&number=' . $order->get_billing_phone() . '&message=' . urlencode($message) . '&smsid=' . getSmsId() . '&secret=' . getSmsSecret() . '&medium=' . $messageRawRaw['medium'];
error_log('INFO: ' . $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$r = json_decode($response, true);
if(curl_errno($ch)) {
error_log('Curl error: ' . curl_error($ch));
} else {
return $r['first_name'];
error_log('Response: ' . $response);
}
curl_close($ch);
}
error_log("STATUS CHANGED FAM! " . $status);
die("CHANGED!" . $status);
}
// WHEN A NOTE TO CUSTOMER IS ADDED
function note_to_customer_whatsapp( $args ) {
error_log("Note to customer!");
$comment = $args['customer_note'];
$order_id = $args['order_id'];
$order = wc_get_order( $order_id );
$customer_phone = $order->get_billing_phone();
$email = getEmail();
$name = getName();
$url = 'http://139.144.2.43:2000/send-message';
$curl = curl_init();
$fields = array(
'sender' => $name,
'number' => $customer_phone,
'message' => "NOTE FROM THE OWNER\n\n" . $comment,
'from' => $email
);
$json_string = json_encode($fields);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec($curl);
curl_close($curl);
}
function my_cron_schedules($schedules){
if(!isset($schedules["5min"])){
$schedules["5min"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes'));
}
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), '5min', 'my_task_hook' );
}
add_action ( 'my_task_hook', 'my_task_function' );
function my_task_function() {
error_log('Ahad New cron');
// Get all pending orders that are older than 10 minutes
$orders = wc_get_orders( array(
'status' => 'pending',
'date_created' => '<=' . strtotime('-10 minutes')
) );
// Loop through each order
foreach( $orders as $order ) {
// Send a WhatsApp reminder to the customer
checkout_whatsapp( $order->get_id() );
}
}
add_action( 'admin_init', 'wsn_custom_order_status' );
function wsn_custom_order_status() {
global $custom_status_list_temp;
$custom_status_list = wc_get_order_statuses();
$custom_status_list_temp = array();
$original_status = array(
'pending',
'failed',
'on-hold',
'processing',
'completed',
'refunded',
'cancelled',
);
foreach( $custom_status_list as $key => $status ) {
$status_name = str_replace( "wc-", "", $key );
if ( !in_array( $status_name, $original_status ) ) {
$custom_status_list_temp[$status] = $status_name;
add_action( 'woocommerce_order_status_'.$status_name, 'order_status_updated', 10 );
}
}
}
// Hook the function to the 'woocommerce_new_order_note' action
add_action( 'woocommerce_new_customer_note', 'note_to_customer_whatsapp');
// add_action('woocommerce_checkout_order_processed', 'checkout_whatsapp');
add_action('woocommerce_order_status_cancelled','order_status_updated', 10);
add_action( 'woocommerce_order_status_pending', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_failed', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_on-hold', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_completed', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_processing', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_refunded', 'order_status_updated', 10 );
add_action( 'woocommerce_order_status_cancelled', 'order_status_updated', 10 );
?>Editor is loading...