Tidio Live Chat - Version 3.4.0

Version Description

  • Add asynchronous loading of chat widget option
  • Code cleanup
Download this release

Release Info

Developer lucastidio
Plugin Icon 128x128 Tidio Live Chat
Version 3.4.0
Comparing to
See all releases

Code changes from version 3.3.2 to 3.4.0

Files changed (2) hide show
  1. readme.txt +35 -24
  2. tidio-elements.php +225 -155
readme.txt CHANGED
@@ -2,35 +2,41 @@
2
  Contributors: tytus-tytus, lucastidio
3
  Tags: free live chat, plugin, facebook, admin, live chat, chat, livechat, post, tidio, widget, post, posts, twitter, zendesk, mailchimp
4
  Requires at least: 3.0
5
- Tested up to: 4.9
6
  Stable tag: trunk
7
  License: GPLv2
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
- Meet Tidio Live Chat - a free live chat for your website. No login or registration required - It integrates with your website in less than 20 seconds
11
 
12
  == Description ==
13
-
14
- Tidio Live Chat is a live chat service which allows you to easily communicate with your customers. It is designed specifically for the WordPress community. Once you install the plugin, you will be ready to take full advantage of the benefits our service provides; no registration required.
15
 
16
- = Features =
17
- * Extremely easy installation - once you install the plugin, you’re ready to go
 
 
 
 
 
18
  * Keep track of who visits your website
19
  * Contact any customer that is currently visiting your site
20
  * Adjust the colour scheme to your needs
21
  * Use a dedicated app for mobile and desktop devices (iOS, Android, Windows, OS X, Chrome)
22
- * The integrated widget supports 183 languages
23
- * Available in 2 outstanding designs
24
- * Offline messages form for your visitors to send you an email, when you are not online
25
  * Contact your visitors via email when they leave the chat
26
- * Integration with Zendesk, GetResponse, SalesForce, MailChimp, and many more
 
 
 
27
 
28
- = How does it work? =
29
- Right after you install, open the “Tidio Chat” tab and you’re ready to go! Once you access the Tidio dashboard, you can reply to any incoming chats and manage your settings. Chat box will be visible on your website so your visitors will be able to contact you in real time.
30
 
31
- [youtube https://www.youtube.com/watch?v=lL84qzWi4zQ]
 
32
 
33
- [youtube https://www.youtube.com/watch?v=GYAIWqrWZQQ]
34
 
35
  = Supported languages =
36
  * English
@@ -68,9 +74,6 @@ Please note that Tidio Chat is free. After activating the plugin, your 7 day tri
68
 
69
  == Frequently Asked Questions ==
70
 
71
- = Do I need to sign up in order to use the Chat? =
72
- No, to use the chat you just need to install the plugin and it’s done! No registration or login required.
73
-
74
  = Can I add an infinite number of agents? =
75
  Yes , there is no limit to the number of agents you can add.
76
 
@@ -99,7 +102,7 @@ You are able to create automated messages which start a conversation with your c
99
  Yes, you can download our mobile app from App Store (http://goo.gl/mqth2P) or Google Play (http://goo.gl/ayibFD)
100
 
101
  = Can I install TidioChat on my plain html site? =
102
- Yes you can
103
 
104
  = Is it possible to initiate a conversation before the visitor does? =
105
  Yes, you can do it with the automatic messages feature or simply choose a client from the visitors list and engage a conversation.
@@ -118,6 +121,14 @@ Yes, simply go to your chat panel and switch the “Display” option to off.
118
 
119
  == Changelog ==
120
 
 
 
 
 
 
 
 
 
121
  = 3.3.2 =
122
  * Change of the widget script placement in Wordpress themes to improve the rendering performance.
123
  * Use admin email as a default email for a Tidio account.
@@ -138,24 +149,24 @@ Yes, simply go to your chat panel and switch the “Display” option to off.
138
  = 3.1.0 =
139
  * Cleanup with changes after last modifications.
140
 
141
- = 3.0.1 =
142
  * (added) curl support
143
 
144
- = 3.0 =
145
  * (added) new design
146
  * (added) integration with tidio chat account
147
 
148
- = 2.1.2 =
149
  * (added) compatibility with other tidio plugins
150
 
151
- = 2.1 =
152
  * (added) compatibility with tidio one api
153
 
154
- = 2.0.5 =
155
  * (added) compatibility with tidio one
156
 
157
  = 2.0.2 =
158
- * (added) support to sites with disabled allow_url_fopen flag
159
 
160
  = 2.0.1 =
161
  * (added) new icon
2
  Contributors: tytus-tytus, lucastidio
3
  Tags: free live chat, plugin, facebook, admin, live chat, chat, livechat, post, tidio, widget, post, posts, twitter, zendesk, mailchimp
4
  Requires at least: 3.0
5
+ Tested up to: 5.0
6
  Stable tag: trunk
7
  License: GPLv2
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
+ Meet Tidio Live Chat a free live chat for your website. It integrates with your website in less than 20 seconds.
11
 
12
  == Description ==
 
 
13
 
14
+ Tidio Live Chat is a live chat service which allows you to communicate with your customers easily, also with the help of chatbots. It is designed specifically for the WordPress community. Once you install the plugin, you will be ready to take full advantage of the benefits our service provides. **A free plan is available!**
15
+
16
+ [youtube https://www.youtube.com/watch?v=aMvyiIEzSm4]
17
+
18
+ = What else do we feature? =
19
+ * Extremely easy installation – once you install the plugin, you’re ready to go
20
+ * Tidio Chatbots that will solve your customers’ issues 24/7 and generate more leads
21
  * Keep track of who visits your website
22
  * Contact any customer that is currently visiting your site
23
  * Adjust the colour scheme to your needs
24
  * Use a dedicated app for mobile and desktop devices (iOS, Android, Windows, OS X, Chrome)
25
+ * The integrated widget supports 183 languages, with 21 pre-translated language packs available
26
+ * A modern widget design that suits any website
27
+ * An offline form for your visitors, so that you get email notifications about messages when you're not online
28
  * Contact your visitors via email when they leave the chat
29
+ * Integration with Zendesk, GetResponse, SalesForce, MailChimp, and many more
30
+ * All channels in one place - use chat, email, and Messenger in a single panel
31
+ * Great technical support 24/5
32
+ * A free plan available
33
 
34
+ Our app merges live chat and chatbots in one tool to help you skyrocket your sales and level of customer service. Imagine that a potential client wants to leave you; the Tidio Chatbot can engage them 24/7 (you don’t even have to be online!), offering them help in case of an issue, or asking them for an email address for later contact. Your client is happy, and you have a new lead. Isn’t that excellent customer service?
 
35
 
36
+ = How does it work? =
37
+ Right after you install, open the “Tidio Chat” tab, and you’re ready to go! A chat widget will be visible on your website, so your visitors can contact you in real time. Once you access the Tidio dashboard, you can reply to any incoming chats, configure Chatbots, and manage other settings.
38
 
39
+ [youtube https://www.youtube.com/watch?v=tuSepEpm33M]
40
 
41
  = Supported languages =
42
  * English
74
 
75
  == Frequently Asked Questions ==
76
 
 
 
 
77
  = Can I add an infinite number of agents? =
78
  Yes , there is no limit to the number of agents you can add.
79
 
102
  Yes, you can download our mobile app from App Store (http://goo.gl/mqth2P) or Google Play (http://goo.gl/ayibFD)
103
 
104
  = Can I install TidioChat on my plain html site? =
105
+ Yes you can
106
 
107
  = Is it possible to initiate a conversation before the visitor does? =
108
  Yes, you can do it with the automatic messages feature or simply choose a client from the visitors list and engage a conversation.
121
 
122
  == Changelog ==
123
 
124
+ = 3.4.0 =
125
+ * Add asynchronous loading of chat widget option
126
+ * Code cleanup
127
+
128
+ = 3.3.2 =
129
+ * Change of the widget script placement in Wordpress themes to improve the rendering performance.
130
+ * Use admin email as a default email for a Tidio account.
131
+
132
  = 3.3.2 =
133
  * Change of the widget script placement in Wordpress themes to improve the rendering performance.
134
  * Use admin email as a default email for a Tidio account.
149
  = 3.1.0 =
150
  * Cleanup with changes after last modifications.
151
 
152
+ = 3.0.1 =
153
  * (added) curl support
154
 
155
+ = 3.0 =
156
  * (added) new design
157
  * (added) integration with tidio chat account
158
 
159
+ = 2.1.2 =
160
  * (added) compatibility with other tidio plugins
161
 
162
+ = 2.1 =
163
  * (added) compatibility with tidio one api
164
 
165
+ = 2.0.5 =
166
  * (added) compatibility with tidio one
167
 
168
  = 2.0.2 =
169
+ * (added) support to sites with disabled allow_url_fopen flag
170
 
171
  = 2.0.1 =
172
  * (added) new icon
tidio-elements.php CHANGED
@@ -4,193 +4,116 @@
4
  * Plugin Name: Tidio Chat
5
  * Plugin URI: http://www.tidiochat.com
6
  * Description: Tidio Live Chat - Live chat for your website. No logging in, no signing up - integrates with your website in less than 20 seconds.
7
- * Version: 3.3.0
8
  * Author: Tidio Ltd.
9
  * Author URI: http://www.tidiochat.com
10
  * License: GPL2
11
  */
12
- define('TIDIOCHAT_VERSION', '3.3.2');
13
-
14
- class TidioLiveChat {
15
-
16
- private $scriptUrl = '//code.tidio.co/';
17
- private static $apiUrl = 'https://api-v2.tidio.co';
18
- private static $chatUrl = 'https://www.tidiochat.com';
19
-
20
- public function __construct() {
21
-
 
 
 
 
 
 
22
  if (!empty($_GET['tidio_chat_version'])) {
23
  echo TIDIOCHAT_VERSION;
24
  exit;
25
  }
26
 
27
  /* Before add link to menu - check is user trying to unninstal */
28
- if (is_admin() && !empty($_GET['tidio_one_clear_cache'])) {
29
- delete_option('tidio-one-public-key');
30
- delete_option('tidio-one-private-key');
31
  }
32
 
33
- add_action('admin_menu', array($this, 'addAdminMenuLink'));
34
-
35
- if(get_option('tidio-one-public-key')){
36
  add_action('admin_footer', array($this, 'adminJS'));
37
  }
38
 
39
  if (!is_admin()) {
40
- add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'));
41
- }else{
 
 
 
 
 
42
  add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
43
- }
44
-
45
- add_action('wp_ajax_tidio_chat_save_keys', array($this, 'ajaxTidioChatSaveKeys'));
46
-
47
- /* Ajax functions to set up existing tidio account */
48
- add_action('wp_ajax_get_project_keys', array($this, 'ajaxGetProjectKeys'));
49
- add_action('wp_ajax_get_private_key', array($this, 'ajaxGetPrivateKey'));
50
-
51
- add_filter('plugin_action_links', array($this, 'pluginActionLinks'), 10, 2);
52
- add_action('admin_post_tidio-chat-reset', array($this, 'uninstall'));
53
- }
54
-
55
- public function pluginActionLinks($links, $file) {
56
- if (strpos($file, 'tidio-elements.php') !== false && get_option('tidio-one-private-key')) {
57
- $links[] = '<a href="'.admin_url('admin-post.php').'?action=tidio-chat-reset">'.esc_html__( 'Clear Account Data' , 'tidio-live-chat').'</a>';
58
- }
59
-
60
- return $links;
61
- }
62
-
63
- public function ajaxGetProjectKeys(){
64
- update_option('tidio-one-public-key', $_POST['public_key']);
65
- update_option('tidio-one-private-key', $_POST['private_key']);
66
- echo self::getRedirectUrl($_POST['private_key']);
67
- exit();
68
- }
69
 
70
- // Ajax - Create an new project
 
 
71
 
72
- public function ajaxTidioChatSaveKeys() {
 
73
 
74
- if (!is_admin()) {
75
- exit;
76
  }
77
-
78
- if (empty($_POST['private_key']) || empty($_POST['public_key'])) {
79
- exit;
80
- }
81
-
82
- update_option('tidio-one-public-key', $_POST['public_key']);
83
- update_option('tidio-one-private-key', $_POST['private_key']);
84
-
85
- echo '1';
86
- exit;
87
- }
88
-
89
- // Front End Scripts
90
- public function enqueueScripts() {
91
- wp_enqueue_script('tidio-chat', $this->scriptUrl . self::getPublicKey() . '.js', array(), TIDIOCHAT_VERSION, true);
92
  }
93
 
94
- // Admin scripts and style enquee
95
- public function enqueueAdminScripts(){
96
- wp_enqueue_script('tidio-chat-admin', plugins_url('media/js/options.js', __FILE__), array(), TIDIOCHAT_VERSION, true);
97
- wp_enqueue_style('tidio-chat-admin-style', plugins_url('media/css/options.css', __FILE__), array(), TIDIOCHAT_VERSION);
98
  }
99
 
100
- // Admin JavaScript
101
- public function adminJS() {
102
- $privateKey = self::getPrivateKey();
103
- $redirectUrl = '';
104
-
105
- if ($privateKey && $privateKey != 'false') {
106
- $redirectUrl = self::getRedirectUrl($privateKey);
107
- } else {
108
- $redirectUrl = admin_url('admin-ajax.php?action=tidio_chat_redirect');
109
  }
110
-
111
- echo "<script>jQuery('a[href=\"admin.php?page=tidio-chat\"]').attr('href', '" . $redirectUrl . "').attr('target', '_blank') </script>";
112
- }
113
-
114
- // Menu Pages
115
-
116
- public function addAdminMenuLink() {
117
- $optionPage = add_menu_page(
118
- 'Tidio Chat', 'Tidio Chat', 'manage_options', 'tidio-chat', array($this, 'addAdminPage'), content_url() . '/plugins/tidio-live-chat/media/img/icon.png'
119
- );
120
- }
121
-
122
- public function addAdminPage() {
123
- // Set class property
124
- $dir = plugin_dir_path(__FILE__);
125
- include $dir . 'options.php';
126
- }
127
-
128
- // Uninstall
129
-
130
- public function uninstall() {
131
- delete_option('tidio-one-public-key');
132
- delete_option('tidio-one-private-key');
133
- wp_redirect( admin_url('plugins.php') );
134
- die();
135
  }
136
 
137
- // Get Private Key
138
-
139
- public static function getPrivateKey() {
140
- self::syncPrivateKey();
141
 
142
- $privateKey = get_option('tidio-one-private-key');
143
 
144
  if ($privateKey) {
145
  return $privateKey;
146
  }
147
 
148
  try {
149
- $data = self::getContent(self::getAccessUrl());
150
- } catch(Exception $e){
151
  $data = null;
152
  }
153
  //
154
  if (!$data) {
155
- update_option('tidio-one-private-key', 'false');
156
  return false;
157
  }
158
 
159
  @$data = json_decode($data, true);
160
  if (!$data || !$data['status']) {
161
- update_option('tidio-one-private-key', 'false');
162
  return false;
163
  }
164
 
165
- update_option('tidio-one-private-key', $data['value']['private_key']);
166
- update_option('tidio-one-public-key', $data['value']['public_key']);
 
167
 
168
  return $data['value']['private_key'];
169
  }
170
 
171
- public static function getContent($url){
172
-
173
- if(function_exists('curl_version')){ // load trought curl
174
- $ch = curl_init();
175
-
176
- curl_setopt($ch, CURLOPT_HEADER, 0);
177
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
178
- curl_setopt($ch, CURLOPT_URL, $url);
179
-
180
- $data = curl_exec($ch);
181
- curl_close($ch);
182
-
183
- return $data;
184
- } else { // load trought file get contents
185
- return file_get_contents($url);
186
- }
187
-
188
- }
189
-
190
- // Sync private key with old version
191
-
192
- public static function syncPrivateKey() {
193
- if (get_option('tidio-one-public-key')) {
194
  return false;
195
  }
196
 
@@ -203,20 +126,39 @@ class TidioLiveChat {
203
 
204
  // sync old variables with new one
205
 
206
- update_option('tidio-one-public-key', $publicKey);
207
- update_option('tidio-one-private-key', $privateKey);
208
 
209
  return true;
210
  }
211
 
212
- // Get Access Url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
- public static function getAccessUrl() {
215
- return self::$apiUrl . '/access/external/create?url=' . urlencode(site_url()) . '&platform=wordpress&email=' . urlencode(get_option('admin_email')) . '&_ip=' . $_SERVER['REMOTE_ADDR'];
 
216
  }
217
 
218
- public static function getRedirectUrl($privateKey) {
219
- return self::$chatUrl . '/access?' . http_build_query(
 
220
  array(
221
  'privateKey' => $privateKey,
222
  'utm_source' => 'platform',
@@ -226,31 +168,159 @@ class TidioLiveChat {
226
  );
227
  }
228
 
229
- public static function ajaxGetPrivateKey(){
230
- $privateKey = self::getPrivateKey();
231
- if(!$privateKey || $privateKey=='false'){
232
- echo 'error';
233
- exit();
 
 
 
 
 
 
 
 
 
 
 
234
  }
235
- echo self::getRedirectUrl($privateKey);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  exit();
237
  }
238
 
239
- // Get Public Key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
- public static function getPublicKey() {
242
- $publicKey = get_option('tidio-one-public-key');
 
243
 
244
  if ($publicKey) {
245
  return $publicKey;
246
  }
247
 
248
- self::getPrivateKey();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
- return get_option('tidio-one-public-key');
 
 
 
 
 
251
  }
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  }
254
 
255
- $tidioLiveChat = new TidioLiveChat();
 
 
 
 
 
256
 
 
4
  * Plugin Name: Tidio Chat
5
  * Plugin URI: http://www.tidiochat.com
6
  * Description: Tidio Live Chat - Live chat for your website. No logging in, no signing up - integrates with your website in less than 20 seconds.
7
+ * Version: 3.4.0
8
  * Author: Tidio Ltd.
9
  * Author URI: http://www.tidiochat.com
10
  * License: GPL2
11
  */
12
+ define('TIDIOCHAT_VERSION', '3.4.0');
13
+
14
+ class TidioLiveChat
15
+ {
16
+ const SCRIPT_URL = '//code.tidio.co/';
17
+ const API_URL = 'https://api-v2.tidio.co';
18
+ const CHAT_URL = 'https://www.tidiochat.com';
19
+ const PUBLIC_KEY_OPTION = 'tidio-one-public-key';
20
+ const PRIVATE_KEY_OPTION = 'tidio-one-private-key';
21
+ const ASYNC_LOAD_OPTION = 'tidio-async-load';
22
+ const CLEAR_ACCOUNT_DATA_ACTION = 'tidio-chat-reset';
23
+ const TIDIO_PLUGIN_NAME = 'tidio-live-chat';
24
+ const TOGGLE_ASYNC_ACTION = 'tidio-chat-toggle-async';
25
+
26
+ public function __construct()
27
+ {
28
  if (!empty($_GET['tidio_chat_version'])) {
29
  echo TIDIOCHAT_VERSION;
30
  exit;
31
  }
32
 
33
  /* Before add link to menu - check is user trying to unninstal */
34
+ if (is_admin() && current_user_can('activate_plugins') && !empty($_GET['tidio_one_clear_cache'])) {
35
+ delete_option(TidioLiveChat::PUBLIC_KEY_OPTION);
36
+ delete_option(TidioLiveChat::PRIVATE_KEY_OPTION);
37
  }
38
 
39
+ if (get_option(TidioLiveChat::PUBLIC_KEY_OPTION)) {
 
 
40
  add_action('admin_footer', array($this, 'adminJS'));
41
  }
42
 
43
  if (!is_admin()) {
44
+ if (get_option(TidioLiveChat::ASYNC_LOAD_OPTION)) {
45
+ add_action('wp_footer', array($this, 'enqueueScriptsAsync'), PHP_INT_MAX);
46
+ } else {
47
+ add_action('wp_enqueue_scripts', array($this, 'enqueueScriptsSync'), 1000);
48
+ }
49
+ } else if (current_user_can('activate_plugins')) {
50
+ add_action('admin_menu', array($this, 'addAdminMenuLink'));
51
  add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ add_action('wp_ajax_tidio_chat_save_keys', array($this, 'ajaxTidioChatSaveKeys'));
54
+ add_action('wp_ajax_get_project_keys', array($this, 'ajaxGetProjectKeys'));
55
+ add_action('wp_ajax_get_private_key', array($this, 'ajaxGetPrivateKey'));
56
 
57
+ add_action('admin_post_' . TidioLiveChat::CLEAR_ACCOUNT_DATA_ACTION . '', array($this, 'uninstall'));
58
+ add_action('admin_post_' . TidioLiveChat::TOGGLE_ASYNC_ACTION . '', array($this, 'toggleAsync'));
59
 
60
+ add_filter('plugin_action_links', array($this, 'pluginActionLinks'), 10, 2);
 
61
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  }
63
 
64
+ public static function activate()
65
+ {
66
+ update_option(TidioLiveChat::ASYNC_LOAD_OPTION, true);
 
67
  }
68
 
69
+ public static function ajaxGetPrivateKey()
70
+ {
71
+ $privateKey = TidioLiveChat::getPrivateKey();
72
+ if (!$privateKey || $privateKey == 'false') {
73
+ echo 'error';
74
+ exit();
 
 
 
75
  }
76
+ echo TidioLiveChat::getRedirectUrl($privateKey);
77
+ exit();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  }
79
 
80
+ public static function getPrivateKey()
81
+ {
82
+ TidioLiveChat::syncPrivateKey();
 
83
 
84
+ $privateKey = get_option(TidioLiveChat::PRIVATE_KEY_OPTION);
85
 
86
  if ($privateKey) {
87
  return $privateKey;
88
  }
89
 
90
  try {
91
+ $data = TidioLiveChat::getContent(TidioLiveChat::getAccessUrl());
92
+ } catch (Exception $e) {
93
  $data = null;
94
  }
95
  //
96
  if (!$data) {
97
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, 'false');
98
  return false;
99
  }
100
 
101
  @$data = json_decode($data, true);
102
  if (!$data || !$data['status']) {
103
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, 'false');
104
  return false;
105
  }
106
 
107
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $data['value']['private_key']);
108
+ update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $data['value']['public_key']);
109
+ update_option(TidioLiveChat::ASYNC_LOAD_OPTION, true);
110
 
111
  return $data['value']['private_key'];
112
  }
113
 
114
+ public static function syncPrivateKey()
115
+ {
116
+ if (get_option(TidioLiveChat::PUBLIC_KEY_OPTION)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  return false;
118
  }
119
 
126
 
127
  // sync old variables with new one
128
 
129
+ update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $publicKey);
130
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $privateKey);
131
 
132
  return true;
133
  }
134
 
135
+ public static function getContent($url)
136
+ {
137
+
138
+ if (function_exists('curl_version')) { // load trought curl
139
+ $ch = curl_init();
140
+
141
+ curl_setopt($ch, CURLOPT_HEADER, 0);
142
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
143
+ curl_setopt($ch, CURLOPT_URL, $url);
144
+
145
+ $data = curl_exec($ch);
146
+ curl_close($ch);
147
+
148
+ return $data;
149
+ } else { // load trought file get contents
150
+ return file_get_contents($url);
151
+ }
152
+ }
153
 
154
+ public static function getAccessUrl()
155
+ {
156
+ return TidioLiveChat::API_URL . '/access/external/create?url=' . urlencode(site_url()) . '&platform=wordpress&email=' . urlencode(get_option('admin_email')) . '&_ip=' . $_SERVER['REMOTE_ADDR'];
157
  }
158
 
159
+ public static function getRedirectUrl($privateKey)
160
+ {
161
+ return TidioLiveChat::CHAT_URL . '/access?' . http_build_query(
162
  array(
163
  'privateKey' => $privateKey,
164
  'utm_source' => 'platform',
168
  );
169
  }
170
 
171
+ public function pluginActionLinks($links, $file)
172
+ {
173
+ if (strpos($file, basename(__FILE__)) !== false) {
174
+ if (get_option(TidioLiveChat::PRIVATE_KEY_OPTION)) {
175
+ $links[] = '<a href="' . admin_url('admin-post.php') . '?action=' . TidioLiveChat::CLEAR_ACCOUNT_DATA_ACTION . '">' . esc_html__('Clear Account Data',
176
+ TidioLiveChat::TIDIO_PLUGIN_NAME) . '</a>';
177
+ if (get_option(TidioLiveChat::ASYNC_LOAD_OPTION)) {
178
+ $toggleAsyncLabel = '✓';
179
+ $onclickPart = 'onclick="return confirm(\'Disabling asynchronous loading of the chat widget may affect the page loading time of your website. Are you sure you want to disable the asynchronous loading?\');"';
180
+ } else {
181
+ $toggleAsyncLabel = '✘';
182
+ $onclickPart = '';
183
+ }
184
+ $links[] = '<a href="' . admin_url('admin-post.php') . '?action=' . TidioLiveChat::TOGGLE_ASYNC_ACTION . '" ' . $onclickPart . '>' . esc_html__($toggleAsyncLabel . ' Asynchronous loading',
185
+ TidioLiveChat::TIDIO_PLUGIN_NAME) . '</a>';
186
+ }
187
  }
188
+
189
+ return $links;
190
+ }
191
+
192
+ public function toggleAsync()
193
+ {
194
+ update_option(TidioLiveChat::ASYNC_LOAD_OPTION, !get_option(TidioLiveChat::ASYNC_LOAD_OPTION));
195
+ wp_redirect(admin_url('plugins.php'));
196
+ die();
197
+ }
198
+
199
+ public function ajaxGetProjectKeys()
200
+ {
201
+ update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $_POST['public_key']);
202
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $_POST['private_key']);
203
+ echo TidioLiveChat::getRedirectUrl($_POST['private_key']);
204
  exit();
205
  }
206
 
207
+ public function ajaxTidioChatSaveKeys()
208
+ {
209
+ if (!is_admin()) {
210
+ exit;
211
+ }
212
+
213
+ if (empty($_POST['private_key']) || empty($_POST['public_key'])) {
214
+ exit;
215
+ }
216
+
217
+ update_option(TidioLiveChat::PUBLIC_KEY_OPTION, $_POST['public_key']);
218
+ update_option(TidioLiveChat::PRIVATE_KEY_OPTION, $_POST['private_key']);
219
+
220
+ echo '1';
221
+ exit;
222
+ }
223
+
224
+ public function enqueueScriptsAsync()
225
+ {
226
+ $publicKey = TidioLiveChat::getPublicKey();
227
+ $widgetUrl = TidioLiveChat::SCRIPT_URL . $publicKey . '.js';
228
+ $asyncScript = <<<SRC
229
+ <script type='text/javascript'>
230
+ document.tidioChatCode = "$publicKey";
231
+ (function() {
232
+ function asyncLoad() {
233
+ var tidioScript = document.createElement("script");
234
+ tidioScript.type = "text/javascript";
235
+ tidioScript.async = true;
236
+ tidioScript.src = "{$widgetUrl}";
237
+ document.body.appendChild(tidioScript);
238
+ }
239
+ if (window.attachEvent) {
240
+ window.attachEvent("onload", asyncLoad);
241
+ } else {
242
+ window.addEventListener("load", asyncLoad, false);
243
+ }
244
+ })();
245
+ </script>
246
+ SRC;
247
+ echo $asyncScript;
248
+ }
249
 
250
+ public static function getPublicKey()
251
+ {
252
+ $publicKey = get_option(TidioLiveChat::PUBLIC_KEY_OPTION);
253
 
254
  if ($publicKey) {
255
  return $publicKey;
256
  }
257
 
258
+ TidioLiveChat::getPrivateKey();
259
+
260
+ return get_option(TidioLiveChat::PUBLIC_KEY_OPTION);
261
+ }
262
+
263
+ public function enqueueScriptsSync()
264
+ {
265
+ wp_enqueue_script('tidio-chat', TidioLiveChat::SCRIPT_URL . TidioLiveChat::getPublicKey() . '.js', array(),
266
+ TIDIOCHAT_VERSION,
267
+ true);
268
+ wp_add_inline_script('tidio-chat', 'document.tidioChatCode = "' . TidioLiveChat::getPublicKey() . '";',
269
+ 'before');
270
+ }
271
+
272
+ public function enqueueAdminScripts()
273
+ {
274
+ wp_enqueue_script('tidio-chat-admin', plugins_url('media/js/options.js', __FILE__), array(), TIDIOCHAT_VERSION,
275
+ true);
276
+ wp_enqueue_style('tidio-chat-admin-style', plugins_url('media/css/options.css', __FILE__), array(),
277
+ TIDIOCHAT_VERSION);
278
+ }
279
+
280
+ public function adminJS()
281
+ {
282
+ $privateKey = TidioLiveChat::getPrivateKey();
283
+ $redirectUrl = '';
284
+
285
+ if ($privateKey && $privateKey != 'false') {
286
+ $redirectUrl = TidioLiveChat::getRedirectUrl($privateKey);
287
+ } else {
288
+ $redirectUrl = admin_url('admin-ajax.php?action=tidio_chat_redirect');
289
+ }
290
+
291
+ echo "<script>jQuery('a[href=\"admin.php?page=tidio-chat\"]').attr('href', '" . $redirectUrl . "').attr('target', '_blank') </script>";
292
+ }
293
 
294
+ public function addAdminMenuLink()
295
+ {
296
+ add_menu_page(
297
+ 'Tidio Chat', 'Tidio Chat', 'manage_options', 'tidio-chat', array($this, 'addAdminPage'),
298
+ content_url() . '/plugins/' . TidioLiveChat::TIDIO_PLUGIN_NAME . '/media/img/icon.png'
299
+ );
300
  }
301
 
302
+ public function addAdminPage()
303
+ {
304
+ // Set class property
305
+ $dir = plugin_dir_path(__FILE__);
306
+ include $dir . 'options.php';
307
+ }
308
+
309
+ public function uninstall()
310
+ {
311
+ delete_option(TidioLiveChat::PUBLIC_KEY_OPTION);
312
+ delete_option(TidioLiveChat::PRIVATE_KEY_OPTION);
313
+ delete_option(TidioLiveChat::ASYNC_LOAD_OPTION);
314
+ wp_redirect(admin_url('plugins.php'));
315
+ die();
316
+ }
317
  }
318
 
319
+ add_action('init', 'initialize_tidio');
320
+
321
+ function initialize_tidio()
322
+ {
323
+ $tidioLiveChat = new TidioLiveChat();
324
+ }
325
 
326
+ register_activation_hook(__FILE__, array('TidioLiveChat', 'activate'));