PluginProbe
Spoki – Chat Buttons and WooCommerce Notifications / 2.0.1
Spoki – Chat Buttons and WooCommerce Notifications v2.0.1
2.17.4 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.10.0 2.11.0 2.11.1 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.14.0 All 69 releases
spoki / spoki.php

spoki.php in Spoki – Chat Buttons and WooCommerce Notifications 2.0.1, at spoki.php

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