| 1 |
<?php |
| 2 |
/* @wordpress-plugin |
| 3 |
* Plugin Name: Spoki |
| 4 |
* Description: Spoki is the WooCommerce plugin that allows you to send order status notifications to your customers via WhatsApp and to insert WhatsApp chat buttons on your website! |
| 5 |
* Version: 2.0.4 |
| 6 |
* Author: Reddoak Srl |
| 7 |
* Author URI: https://reddoak.com |
| 8 |
* Text Domain: spoki |
| 9 |
* Domain Path: /languages |
| 10 |
* License: GPLv2 |
| 11 |
*/ |
| 12 |
|
| 13 |
define('SPOKI_DIR', plugin_dir_path(__FILE__)); |
| 14 |
include_once SPOKI_DIR . 'includes/constants.php'; |
| 15 |
include_once SPOKI_DIR . 'includes/spoki-functions.php'; |
| 16 |
|
| 17 |
$spoki = Spoki(); |
| 18 |
|
| 19 |
function Spoki() |
| 20 |
{ |
| 21 |
return WpSpoki::instance(); |
| 22 |
} |
| 23 |
|
| 24 |
final class WpSpoki |
| 25 |
{ |
| 26 |
protected static $_instance = null; |
| 27 |
private $version = ''; |
| 28 |
private $langs = ''; |
| 29 |
public $shop = []; |
| 30 |
|
| 31 |
private $api_plan = SPOKI_BASE_API . "plan/"; |
| 32 |
private $api_status = SPOKI_BASE_API . "status/"; |
| 33 |
private $api_enable_flex = SPOKI_BASE_API . "enable/"; |
| 34 |
private $api_account = SPOKI_BASE_API . "account/"; |
| 35 |
|
| 36 |
public static function instance() |
| 37 |
{ |
| 38 |
if (is_null(self::$_instance)) { |
| 39 |
self::$_instance = new self(); |
| 40 |
} |
| 41 |
return self::$_instance; |
| 42 |
} |
| 43 |
|
| 44 |
private function __construct() |
| 45 |
{ |
| 46 |
$data = get_file_data(__FILE__, array('ver' => 'Version', 'langs' => 'Domain Path')); |
| 47 |
$this->version = $data['ver']; |
| 48 |
$this->langs = $data['langs']; |
| 49 |
$this->options = get_option(SPOKI_OPTIONS); |
| 50 |
$this->shop = [ |
| 51 |
"name" => isset($this->options['shop_name']) ? $this->options['shop_name'] : get_bloginfo('name'), |
| 52 |
"url" => get_bloginfo('url'), |
| 53 |
"email" => isset($this->options['email']) ? $this->options['email'] : get_bloginfo('admin_email'), |
| 54 |
"language" => get_bloginfo('language'), |
| 55 |
"telephone" => isset($this->options['telephone']) ? $this->options['telephone'] : '', |
| 56 |
"contact_link" => isset($this->options['account_info']['contact_link']) ? $this->options['account_info']['contact_link'] : null, |
| 57 |
]; |
| 58 |
|
| 59 |
add_action('init', 'spoki_update_po_file'); |
| 60 |
add_action('admin_menu', array($this, 'add_option_menu')); |
| 61 |
add_filter("plugin_action_links_" . plugin_basename(__FILE__), array($this, 'plugin_settings_link')); |
| 62 |
add_action('wp_enqueue_scripts', array($this, 'add_styles')); |
| 63 |
add_action('admin_enqueue_scripts', array($this, 'add_admin_styles')); |
| 64 |
add_action('updated_option', array($this, 'on_option_added'), 10, 3); |
| 65 |
|
| 66 |
$this->includes(); |
| 67 |
$this->handle_woocommerce(); |
| 68 |
$this->render_buttons(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Include all Spoki required files |
| 73 |
*/ |
| 74 |
private function includes() |
| 75 |
{ |
| 76 |
require_once SPOKI_DIR . 'includes/spoki-functions.php'; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get the setting link of the plugin |
| 81 |
* |
| 82 |
* @param $links |
| 83 |
* @return mixed |
| 84 |
*/ |
| 85 |
public function plugin_settings_link($links) |
| 86 |
{ |
| 87 |
$settings_link = '<a href="options-general.php?page=' . SPOKI_PLUGIN_NAME . '">' . __('Settings', SPOKI_PLUGIN_NAME) . '</a>'; |
| 88 |
array_unshift($links, $settings_link); |
| 89 |
return $links; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add the Spoki option to the menu |
| 94 |
*/ |
| 95 |
public function add_option_menu() |
| 96 |
{ |
| 97 |
add_menu_page( |
| 98 |
'Spoki Options', |
| 99 |
'Spoki', |
| 100 |
'manage_options', |
| 101 |
'spoki', |
| 102 |
array($this, 'render_setup_page'), |
| 103 |
plugins_url() . '/' . SPOKI_PLUGIN_NAME . '/assets/images/logo.svg', |
| 104 |
98 |
| 105 |
); |
| 106 |
add_action('admin_init', array($this, 'register_option_var')); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Register the Spoki option |
| 111 |
*/ |
| 112 |
public function register_option_var() |
| 113 |
{ |
| 114 |
register_setting('wp-spoki-option', SPOKI_OPTIONS); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add website spoki styles |
| 119 |
*/ |
| 120 |
public function add_styles() |
| 121 |
{ |
| 122 |
$styles = ['buttons.css']; |
| 123 |
|
| 124 |
foreach ($styles as $style) { |
| 125 |
echo "<style>"; |
| 126 |
include_once SPOKI_DIR . 'assets/css/' . $style; |
| 127 |
echo "</style>"; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Add admin spoki styles |
| 133 |
*/ |
| 134 |
public function add_admin_styles() |
| 135 |
{ |
| 136 |
$styles = ['admin.css', 'onboarding.css', 'account-overview.css', 'spoki-overview.css']; |
| 137 |
|
| 138 |
foreach ($styles as $style) { |
| 139 |
echo "<style>"; |
| 140 |
include_once SPOKI_DIR . 'assets/css/' . $style; |
| 141 |
echo "</style>"; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Handle option submit |
| 147 |
* |
| 148 |
* @param $option |
| 149 |
* @param $old_value |
| 150 |
* @param $value |
| 151 |
*/ |
| 152 |
public function on_option_added($option, $old_value, $value) |
| 153 |
{ |
| 154 |
if ($option == SPOKI_OPTIONS) { |
| 155 |
|
| 156 |
/** New Telephone from settings */ |
| 157 |
if (isset($value['telephone'])) { |
| 158 |
$this->update_options($value, ["telephone" => spoki_get_formatted_telephone($value['telephone'])]); |
| 159 |
} |
| 160 |
|
| 161 |
/** Onboarding Without WooCommerce */ |
| 162 |
if (isset($value['onboarding']['without_wc'])) { |
| 163 |
$this->update_options($value, [ |
| 164 |
"telephone" => spoki_get_formatted_telephone($value['onboarding']['telephone']), |
| 165 |
"onboarding" => null, |
| 166 |
]); |
| 167 |
$url = admin_url('/admin.php?page=' . SPOKI_PLUGIN_NAME . '&tab=' . urlencode('Floating Button')); |
| 168 |
header("Location: {$url}"); |
| 169 |
exit; |
| 170 |
} |
| 171 |
|
| 172 |
/** Onboarding With WooCommerce */ |
| 173 |
if (isset($value['onboarding']['with_wc'])) { |
| 174 |
$response = $this->create_spoki_account($value['onboarding']['telephone'], $value['onboarding']['email'], $value['onboarding']['shop_name']); |
| 175 |
if (isset($response['secret']) && isset($response['delivery_url'])) { |
| 176 |
$this->update_options($value, [ |
| 177 |
"telephone" => spoki_get_formatted_telephone($value['onboarding']['telephone']), |
| 178 |
"email" => $value['onboarding']['email'], |
| 179 |
"shop_name" => $value['onboarding']['shop_name'], |
| 180 |
"secret" => $response['secret'], |
| 181 |
"delivery_url" => $response['delivery_url'], |
| 182 |
"onboarding" => null, |
| 183 |
]); |
| 184 |
$url = admin_url('/admin.php?page=' . SPOKI_PLUGIN_NAME . '&tab=' . urlencode('WooCommerce')); |
| 185 |
header("Location: {$url}"); |
| 186 |
exit; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** Settings updated for registered account */ |
| 191 |
if (isset($value['is_settings'])) { |
| 192 |
$keys_changed = ($value['secret'] != $old_value['secret']) || ($value['delivery_url'] != $old_value['delivery_url']); |
| 193 |
|
| 194 |
$shop_name_changed = $value['shop_name'] != $old_value['shop_name']; |
| 195 |
$email_changed = $value['email'] != $old_value['email']; |
| 196 |
$telephone_changed = $value['telephone'] != $old_value['telephone']; |
| 197 |
$contact_link_changed = $value['contact_link'] != $old_value['contact_link']; |
| 198 |
$billing_data_changed = $value['billing_data'] != $old_value['billing_data']; |
| 199 |
|
| 200 |
if ($shop_name_changed || $email_changed || $telephone_changed || $contact_link_changed || $billing_data_changed) { |
| 201 |
$this->update_spoki_account($value['telephone'], $value['email'], $value['shop_name'], $value['contact_link'], $value['billing_data']); |
| 202 |
} |
| 203 |
|
| 204 |
if ($keys_changed) { |
| 205 |
$url = admin_url('/admin.php?page=' . SPOKI_PLUGIN_NAME . '&tab=' . urlencode('Welcome')); |
| 206 |
header("Location: {$url}"); |
| 207 |
exit; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Handle WooCommerce features |
| 215 |
*/ |
| 216 |
public function handle_woocommerce() |
| 217 |
{ |
| 218 |
if (isset($this->options['secret'])) { |
| 219 |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 220 |
if (is_plugin_active('woocommerce/woocommerce.php')) { |
| 221 |
if (isset($this->options['woocommerce']['order_updated']) && $this->options['woocommerce']['order_updated'] == 1) { |
| 222 |
add_action('woocommerce_order_status_changed', array($this, 'send_woocommerce_order_updated_alert'), 10, 3); |
| 223 |
} |
| 224 |
if (isset($this->options['woocommerce']['order_created']) && $this->options['woocommerce']['order_created'] == 1) { |
| 225 |
add_action('woocommerce_checkout_order_created', array($this, 'send_woocommerce_order_created_alert'), 10, 3); |
| 226 |
} |
| 227 |
if (isset($this->options['woocommerce']['order_deleted']) && $this->options['woocommerce']['order_deleted'] == 1) { |
| 228 |
add_action('woocommerce_cancelled_order', array($this, 'send_woocommerce_order_deleted_alert'), 10, 3); |
| 229 |
add_action('woocommerce_trash_order', array($this, 'send_woocommerce_order_deleted_alert'), 10, 3); |
| 230 |
} |
| 231 |
// TODO |
| 232 |
// if (isset($this->options['woocommerce']['cart_abandoned']) && $this->options['woocommerce']['cart_abandoned'] == 1) { |
| 233 |
// add_action('woocommerce_ajax_added_to_cart', array($this, 'send_woocommerce_order_alert'), 10, 3); |
| 234 |
// } |
| 235 |
if (isset($this->options['woocommerce']['order_note_added']) && $this->options['woocommerce']['order_note_added'] == 1) { |
| 236 |
add_action('woocommerce_order_note_added', array($this, 'send_woocommerce_note_alert'), 10, 2); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
$this->fetch_secret_status(); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Send the Spoki notification for order status updated |
| 245 |
* |
| 246 |
* @param $order_get_id |
| 247 |
*/ |
| 248 |
public function send_woocommerce_order_updated_alert($order_get_id) |
| 249 |
{ |
| 250 |
$this->send_woocommerce_order_alert($order_get_id, 'order.updated'); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Send the Spoki notification for order created |
| 255 |
* |
| 256 |
* @param $order_get_id |
| 257 |
*/ |
| 258 |
public function send_woocommerce_order_created_alert($order_get_id) |
| 259 |
{ |
| 260 |
$this->send_woocommerce_order_alert($order_get_id, 'order.created'); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Send the Spoki notification for order deleted |
| 265 |
* |
| 266 |
* @param $order_get_id |
| 267 |
*/ |
| 268 |
public function send_woocommerce_order_deleted_alert($order_get_id) |
| 269 |
{ |
| 270 |
$this->send_woocommerce_order_alert($order_get_id, 'order.updated'); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Send the Spoki notification for order status |
| 275 |
* |
| 276 |
* @param $order_get_id |
| 277 |
*/ |
| 278 |
public function send_woocommerce_order_alert($order_get_id, $target) |
| 279 |
{ |
| 280 |
$order = wc_get_order($order_get_id); |
| 281 |
$shop = ["shop" => $this->shop]; |
| 282 |
$order_data = (isset($order) && false != $order ? $order->get_data() : ["id" => $order_get_id]); |
| 283 |
$this->spoki_send(array_merge($order_data, $shop), $target); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Send the Spoki notification for tracking number |
| 288 |
* |
| 289 |
* @param $id |
| 290 |
* @param $order |
| 291 |
*/ |
| 292 |
public function send_woocommerce_note_alert($id, $order) |
| 293 |
{ |
| 294 |
$comment = get_comment($id); |
| 295 |
|
| 296 |
if (isset($comment->comment_content)) { |
| 297 |
$is_gls_tracking = substr(strtolower($comment->comment_content), 0, 4) == '[gls'; |
| 298 |
$is_user_tracking = substr(strtolower($comment->comment_content), 0, 10) == '[tracking]'; |
| 299 |
|
| 300 |
/** Send the message only if it is a tracking order note */ |
| 301 |
if ($comment->comment_type == 'order_note' && ($is_gls_tracking || $is_user_tracking)) { |
| 302 |
$order_data = $order->get_data(); |
| 303 |
$shop = ["shop" => $this->shop]; |
| 304 |
$note = ["note" => $comment]; |
| 305 |
$this->spoki_send(array_merge($order_data, $shop, $note), 'order.tracking'); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Send the Spoki notification |
| 312 |
* |
| 313 |
* @param $data |
| 314 |
* @return bool |
| 315 |
*/ |
| 316 |
private function spoki_send($data, $topic): bool |
| 317 |
{ |
| 318 |
$request_params = array( |
| 319 |
"headers" => array( |
| 320 |
"Authorization" => $this->options['secret'], |
| 321 |
"language" => $this->shop['language'], |
| 322 |
"X-Wc-Webhook-Topic" => $topic, |
| 323 |
), |
| 324 |
"body" => wp_json_encode($data) |
| 325 |
); |
| 326 |
$response = wp_remote_post($this->options['delivery_url'], $request_params); |
| 327 |
$code = wp_remote_retrieve_response_code($response); |
| 328 |
return $code == 200; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Create a new Spoki Free account |
| 333 |
* |
| 334 |
* @param $telephone |
| 335 |
* @param $email |
| 336 |
* @param $shop_name |
| 337 |
* @return array |
| 338 |
*/ |
| 339 |
private function create_spoki_account($telephone, $email, $shop_name): array |
| 340 |
{ |
| 341 |
$request_params = array( |
| 342 |
"headers" => array("Authorization" => $this->options['secret'], "language" => $this->shop['language']), |
| 343 |
"body" => [ |
| 344 |
"phone" => $telephone, |
| 345 |
"email" => $email, |
| 346 |
"name" => $shop_name, |
| 347 |
], |
| 348 |
); |
| 349 |
|
| 350 |
$response = wp_remote_post($this->api_enable_flex, $request_params); |
| 351 |
$code = wp_remote_retrieve_response_code($response); |
| 352 |
|
| 353 |
if ($code >= 200 && $code < 300) { |
| 354 |
$data = json_decode(wp_remote_retrieve_body($response), true); |
| 355 |
return [ |
| 356 |
"secret" => isset($data['secret']) ? $data['secret'] : null, |
| 357 |
"delivery_url" => isset($data['delivery_url']) ? $data['delivery_url'] : null, |
| 358 |
]; |
| 359 |
} |
| 360 |
return []; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Update a Spoki account |
| 366 |
* |
| 367 |
* @param $telephone |
| 368 |
* @param $email |
| 369 |
* @param $shop_name |
| 370 |
* @param $contact_link |
| 371 |
* @param $billing_data |
| 372 |
*/ |
| 373 |
private function update_spoki_account($telephone, $email, $shop_name, $contact_link, $billing_data = []) |
| 374 |
{ |
| 375 |
$request_params = array( |
| 376 |
"headers" => array("Authorization" => $this->options['secret'], "language" => $this->shop['language']), |
| 377 |
"body" => [ |
| 378 |
"phone" => $telephone ?: '', |
| 379 |
"email" => $email ?: '', |
| 380 |
"name" => $shop_name ?: '', |
| 381 |
"contact_link" => $contact_link ?: '', |
| 382 |
"zip_code" => $billing_data['zip_code'] ?: '', |
| 383 |
"province" => $billing_data['province'] ?: '', |
| 384 |
"country" => $billing_data['country'] ?: '', |
| 385 |
"route" => $billing_data['route'] ?: '', |
| 386 |
"city" => $billing_data['city'] ?: '', |
| 387 |
"vat_number" => $billing_data['vat_number'] ?: '', |
| 388 |
"vat_name" => $billing_data['vat_name'] ?: '', |
| 389 |
"c_f" => $billing_data['c_f'] ?: '', |
| 390 |
"pec" => $billing_data['pec'] ?: '', |
| 391 |
"sid" => $billing_data['sid'] ?: '', |
| 392 |
], |
| 393 |
); |
| 394 |
wp_remote_post($this->api_account, $request_params); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Fetch and update the account_info |
| 399 |
*/ |
| 400 |
public function fetch_account_info() |
| 401 |
{ |
| 402 |
$request_params = array("headers" => array("Authorization" => $this->options['secret'], "language" => $this->shop['language'])); |
| 403 |
$response = wp_remote_get($this->api_plan, $request_params); |
| 404 |
$this->update_options(get_option(SPOKI_OPTIONS), ["account_info" => json_decode(wp_remote_retrieve_body($response), true)]); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Check the status of the spoki keys |
| 409 |
* |
| 410 |
* @return array|WP_Error |
| 411 |
*/ |
| 412 |
public function check_spoki_status() |
| 413 |
{ |
| 414 |
$request_params = array("headers" => array("Authorization" => $this->options['secret'], "language" => $this->shop['language'])); |
| 415 |
$response = wp_remote_get($this->api_status, $request_params); |
| 416 |
return $response; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Render the admin setup page |
| 421 |
*/ |
| 422 |
public function render_setup_page() |
| 423 |
{ |
| 424 |
require_once SPOKI_DIR . 'includes/page-setup.php'; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Render the WhatsApp buttons in the website |
| 429 |
*/ |
| 430 |
public function render_buttons() |
| 431 |
{ |
| 432 |
// Floating Button |
| 433 |
if (isset($this->options['buttons']['fixed_support_button_check']) && $this->options['buttons']['fixed_support_button_check'] == 1) { |
| 434 |
add_action('wp_footer', array($this, 'render_floating_button'), 1); |
| 435 |
} |
| 436 |
|
| 437 |
// Product Item Listing Button |
| 438 |
if (isset($this->options['woocommerce']['product_item_listing_button_check']) && $this->options['woocommerce']['product_item_listing_button_check'] == 1) { |
| 439 |
add_action('woocommerce_after_shop_loop_item', array($this, 'render_product_item_listing_button'), 20); |
| 440 |
} |
| 441 |
|
| 442 |
// Cart Page Button |
| 443 |
if (isset($this->options['woocommerce']['cart_button_check']) && $this->options['woocommerce']['cart_button_check'] == 1) { |
| 444 |
add_action('woocommerce_after_cart_totals', array($this, 'render_cart_button'), 20); |
| 445 |
} |
| 446 |
|
| 447 |
// Single Product Page Button |
| 448 |
if (isset($this->options['woocommerce']['single_product_button_check']) && $this->options['woocommerce']['single_product_button_check'] == 1) { |
| 449 |
$position = isset($this->options['woocommerce']['single_product_button_position']) ? $this->options['woocommerce']['single_product_button_position'] : 'after_atc'; |
| 450 |
switch ($position) { |
| 451 |
case 'under_atc': |
| 452 |
add_action('woocommerce_after_add_to_cart_form', array($this, 'render_single_product_button'), 5); |
| 453 |
break; |
| 454 |
case 'after_shortdesc': |
| 455 |
add_action('woocommerce_before_add_to_cart_form', array($this, 'render_single_product_button'), 5); |
| 456 |
break; |
| 457 |
case 'after_atc': |
| 458 |
default: |
| 459 |
add_action('woocommerce_after_add_to_cart_button', array($this, 'render_single_product_button'), 5); |
| 460 |
break; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Render the WhatsApp FAB on the website in every page |
| 467 |
*/ |
| 468 |
public function render_floating_button() |
| 469 |
{ |
| 470 |
if (isset($this->options['telephone']) && $this->options['telephone'] != '') { |
| 471 |
$phone = $this->options['telephone']; |
| 472 |
$text = $this->options['buttons']['fixed_support_button_text']; |
| 473 |
$position = $this->options['buttons']['fixed_support_button_position'] == 'Left' ? 'left' : 'right'; |
| 474 |
$wa_link = "https://api.whatsapp.com/send/?phone=$phone&text=" . urlencode($text); |
| 475 |
$shortcode = " |
| 476 |
(function (w, a, z, y, s) { |
| 477 |
a = w.getElementsByTagName('head')[0]; |
| 478 |
y = w.getElementsByTagName('body')[0]; |
| 479 |
z = w.createElement('style'); |
| 480 |
z.innerText = \"#spoki-fixed-btn{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI','Roboto','Oxygen','Ubuntu','Cantarell','Fira Sans','Droid Sans','Helvetica Neue',sans-serif!important;-webkit-font-smoothing:antialiased!important;display:flex!important;flex-direction:column!important;align-items:center!important;justify-content:center!important;position:fixed!important;$position:12px!important;bottom:24px!important;z-index:9999!important;}#spoki-fixed-btn #spoki-chat-link{display:flex!important;background-color:#23D366!important;width:50px!important;height:50px!important;border-radius:50%!important;cursor:pointer!important;align-items:center!important;justify-content:center!important;box-shadow:0 3px 5px -1px rgba(0,0,0,0.2)!important;}#spoki-fixed-btn #spoki-chat-link:hover{opacity:0.9!important;}#spoki-fixed-btn img{width:auto!important;height:auto!important;max-width:32px!important;max-height:32px!important;}\"; |
| 481 |
a.appendChild(z); |
| 482 |
s = w.createElement('div'); |
| 483 |
s.id = 'spoki-fixed-btn'; |
| 484 |
s.style = 'display:none'; |
| 485 |
s.innerHTML = '<a id=\"spoki-chat-link\" href=\"$wa_link\" target=\"_blank\"><img alt=\"WhatsApp logo\" src=\"https://app.spoki.it/static/png/whatsapp-logo-squared.png\"/></a>'; |
| 486 |
y.appendChild(s); |
| 487 |
})(document); |
| 488 |
"; |
| 489 |
print("<script>$shortcode</script>"); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Render the product button for every product in shop page |
| 495 |
*/ |
| 496 |
public function render_product_item_listing_button() |
| 497 |
{ |
| 498 |
global $product; |
| 499 |
$phone = $this->options['telephone']; |
| 500 |
$cta = __("Request support on WhatsApp", "spoki"); |
| 501 |
if (isset($this->options['woocommerce']['product_item_listing_button_cta']) && !empty($this->options['woocommerce']['product_item_listing_button_cta'])) { |
| 502 |
$cta = $this->options['woocommerce']['product_item_listing_button_cta']; |
| 503 |
} |
| 504 |
$message = __("Hi, I want to buy:", "spoki"); |
| 505 |
if (isset($this->options['woocommerce']['product_item_listing_button_text']) && !empty($this->options['woocommerce']['product_item_listing_button_text'])) { |
| 506 |
$message = $this->options['woocommerce']['product_item_listing_button_text']; |
| 507 |
} |
| 508 |
$this->render_product_button($product, $phone, $cta, $message); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* Render the button in the cart page |
| 513 |
*/ |
| 514 |
public function render_cart_button() |
| 515 |
{ |
| 516 |
global $product; |
| 517 |
$phone = $this->options['telephone']; |
| 518 |
$cta = __("Order via WhatsApp", "spoki"); |
| 519 |
if (isset($this->options['woocommerce']['cart_button_cta']) && !empty($this->options['woocommerce']['cart_button_cta'])) { |
| 520 |
$cta = $this->options['woocommerce']['cart_button_cta']; |
| 521 |
} |
| 522 |
$message = __("Hi, I want to buy:", "spoki"); |
| 523 |
if (isset($this->options['woocommerce']['cart_button_text']) && !empty($this->options['woocommerce']['cart_button_text'])) { |
| 524 |
$message = $this->options['woocommerce']['cart_button_text']; |
| 525 |
} |
| 526 |
$final_message = urlencode($message); |
| 527 |
|
| 528 |
$products = WC()->cart->get_cart(); |
| 529 |
|
| 530 |
foreach ($products as $item) { |
| 531 |
$product_id = $item['product_id']; |
| 532 |
$qty = $item['quantity']; |
| 533 |
$product = wc_get_product($product_id); |
| 534 |
$product_url = $product->get_permalink(); |
| 535 |
$product_title = $product->get_name(); |
| 536 |
$price = wp_strip_all_tags(wc_price(wc_get_price_including_tax($product))); |
| 537 |
$encoded_title = urlencode($product_title); |
| 538 |
$encoded_product_url = urlencode($product_url); |
| 539 |
$final_message .= "%0D%0A%0D%0A(ID:%20$product_id)%20*$encoded_title*%20$price%20x$qty%0D%0A$encoded_product_url"; |
| 540 |
} |
| 541 |
|
| 542 |
$href = "https://api.whatsapp.com/send?phone=$phone&text=$final_message"; |
| 543 |
$title = "$cta"; |
| 544 |
$class = 'button spoki-button size-4'; |
| 545 |
echo "<div class='spoki-button-relative'><a href='$href' target='_blank' title='$title' class='$class'><img class='spoki-wa-icon' alt='WhatsApp logo' src='https://app.spoki.it/static/png/whatsapp-logo-squared.png'/><span>$cta</span></a></div>"; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Render the button in the product page |
| 550 |
*/ |
| 551 |
public function render_single_product_button() |
| 552 |
{ |
| 553 |
global $product; |
| 554 |
$phone = $this->options['telephone']; |
| 555 |
$cta = __("Request support on WhatsApp", "spoki"); |
| 556 |
if (isset($this->options['woocommerce']['single_product_button_cta']) && !empty($this->options['woocommerce']['single_product_button_cta'])) { |
| 557 |
$cta = $this->options['woocommerce']['single_product_button_cta']; |
| 558 |
} |
| 559 |
$message = __("Hi, I want to buy:", "spoki"); |
| 560 |
if (isset($this->options['woocommerce']['single_product_button_text']) && !empty($this->options['woocommerce']['single_product_button_text'])) { |
| 561 |
$message = $this->options['woocommerce']['single_product_button_text']; |
| 562 |
} |
| 563 |
$position = isset($this->options['woocommerce']['single_product_button_position']) ? $this->options['woocommerce']['single_product_button_position'] : 'after_atc'; |
| 564 |
$this->render_product_button($product, $phone, $cta, $message, $position); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Render the button of a product |
| 569 |
* |
| 570 |
* @param $product |
| 571 |
* @param $phone |
| 572 |
* @param $cta |
| 573 |
* @param $message |
| 574 |
* @param null $position |
| 575 |
*/ |
| 576 |
public function render_product_button($product, $phone, $cta, $message, $position = null) |
| 577 |
{ |
| 578 |
$product_url = $product->get_permalink(); |
| 579 |
$product_title = $product->get_name(); |
| 580 |
$product_id = $product->get_id(); |
| 581 |
|
| 582 |
$class = sprintf('button spoki-button product_type_%s', $product->get_type()); |
| 583 |
if (isset($position)) { |
| 584 |
$class .= " size-2 $position"; |
| 585 |
} else { |
| 586 |
$class .= " size-4"; |
| 587 |
} |
| 588 |
$price = wp_strip_all_tags(wc_price(wc_get_price_including_tax($product))); |
| 589 |
|
| 590 |
$encoded_message = urlencode($message); |
| 591 |
$encoded_title = urlencode($product_title); |
| 592 |
$encoded_product_url = urlencode($product_url); |
| 593 |
|
| 594 |
$final_message = "$encoded_message%0D%0A%0D%0A(ID:%20$product_id)%20*$encoded_title*%20$price%0D%0A$encoded_product_url"; |
| 595 |
$href = "https://api.whatsapp.com/send?phone=$phone&text=$final_message"; |
| 596 |
$title = "$cta $product_title"; |
| 597 |
|
| 598 |
echo "<div class='spoki-button-relative'><a href='$href' target='_blank' title='$title' class='$class'><img class='spoki-wa-icon' alt='WhatsApp logo' src='https://app.spoki.it/static/png/whatsapp-logo-squared.png'/><span>$cta</span></a></div>"; |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* Get the link to change plan |
| 603 |
* |
| 604 |
* @param $is_upgrade |
| 605 |
* @return string |
| 606 |
*/ |
| 607 |
public function get_plan_link($is_upgrade): string |
| 608 |
{ |
| 609 |
$url = $this->shop['language'] == 'it-IT' ? 'https://spoki.it/spoki-flex-acquista-pacchetti-flex/?' : 'https://spoki.it/spoki-flex-acquista-pacchetti-flex/spoki-flex-packages/?'; |
| 610 |
|
| 611 |
if (isset($this->options['account_info']['upgrade_url']) ? $this->options['account_info']['upgrade_url'] : '') { |
| 612 |
$url = $this->options['account_info']['upgrade_url']; |
| 613 |
} |
| 614 |
if ($is_upgrade) { |
| 615 |
$url .= '&is_upgrade=true'; |
| 616 |
} |
| 617 |
return $url; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Update Spoki options |
| 622 |
* |
| 623 |
* @param $current_options |
| 624 |
* @param $new_options |
| 625 |
*/ |
| 626 |
private function update_options($current_options, $new_options) |
| 627 |
{ |
| 628 |
$c_options = is_string($current_options) ? [] : $current_options; |
| 629 |
update_option(SPOKI_OPTIONS, array_merge($c_options, $new_options)); |
| 630 |
$this->options = get_option(SPOKI_OPTIONS); |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Fetch the status of the Spoki keys |
| 635 |
*/ |
| 636 |
public function fetch_secret_status() |
| 637 |
{ |
| 638 |
if (!isset($this->options['secret']) || $this->options['secret'] == '' || !isset($this->options['delivery_url']) || $this->options['delivery_url'] == '') { |
| 639 |
$this->update_options(get_option(SPOKI_OPTIONS), ["secret_status" => [ |
| 640 |
'secret' => isset($this->options['secret']) ? $this->options['secret'] : '', |
| 641 |
'delivery_url' => isset($this->options['delivery_url']) ? $this->options['delivery_url'] : '', |
| 642 |
'code' => 0, |
| 643 |
'message' => '' |
| 644 |
]]); |
| 645 |
} else if (!isset($this->options['secret_status']) || $this->options['secret_status']['code'] != 200 || $this->options['secret'] != $this->options['secret_status']['secret'] || $this->options['delivery_url'] != $this->options['secret_status']['delivery_url']) { |
| 646 |
$response = $this->check_spoki_status(); |
| 647 |
$this->update_options(get_option(SPOKI_OPTIONS), ["secret_status" => [ |
| 648 |
'secret' => isset($this->options['secret']) ? $this->options['secret'] : '', |
| 649 |
'delivery_url' => isset($this->options['delivery_url']) ? $this->options['delivery_url'] : '', |
| 650 |
'code' => wp_remote_retrieve_response_code($response), |
| 651 |
'message' => wp_remote_retrieve_response_message($response) |
| 652 |
]]); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|