PluginProbe
Live Chat & AI Chatbot – onWebChat / 3.7.1
Live Chat & AI Chatbot – onWebChat v3.7.1
3.9.2 3.9.3 3.9.1 3.9.0 3.8.4 3.8.2 3.8.1 3.8.0 3.7.2 3.7.1 3.7.0 3.6.0 3.5.5 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 48 releases
onwebchat / onwebchat.php

onwebchat.php in Live Chat & AI Chatbot – onWebChat 3.7.1, at onwebchat.php

325 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /*
8 Plugin Name: onWebChat - Live Chat & AI Chatbot
9 Plugin URI: https://www.onwebchat.com/wordpress-live-chat-plugin.php
10 Description: Empower visitors with real-time human chat and integrated AI chatbots. onWebChat Live Chat delivers instant responses, smoother interactions, and 24/7 availability - improving engagement, satisfaction, and conversions. Includes WooCommerce product sync for AI training.
11 Author: onWebChat
12 Version: 3.7.1
13 Author URI: https://www.onwebchat.com
14 */
15
16 // Plugin version. Keep in sync with the Version header above on each release.
17 // Sent to onWebChat when the plugin is linked to an account.
18 if (!defined('ONWEBCHAT_PLUGIN_VERSION')) {
19 define('ONWEBCHAT_PLUGIN_VERSION', '3.7.1');
20 }
21
22 /**
23 * WooCommerce Testing Mode Configuration
24 * Set to true for local development (127.0.0.1:81)
25 * Set to false for production (www.onwebchat.com)
26 */
27 if (!defined('ONWEBCHAT_WC_TESTING_MODE')) {
28 define('ONWEBCHAT_WC_TESTING_MODE', false);
29 }
30
31 /* ---------------------------------------------------- *
32 * Create Settings Page
33 * ---------------------------------------------------- */
34
35
36 define('ONWEBCHAT_SMALL_LOGO', plugins_url( 'images/onwebchat-logo.png' , __FILE__ ));
37 define('ONWEBCHAT_SERVER_URL','https://www.onwebchat.com/get-chatid.php');
38
39 // Load WooCommerce modules if WooCommerce is active
40 add_action('plugins_loaded', function () {
41 if (class_exists('WooCommerce')) {
42 // Product catalog sync (plugin -> onWebChat, for AI training).
43 require_once __DIR__ . '/includes/woocommerce-sync.php';
44 // Live order-status lookup (onWebChat -> plugin, during a chat).
45 require_once __DIR__ . '/includes/woocommerce-orders.php';
46 }
47 });
48
49 // add styles
50 add_action( 'admin_enqueue_scripts', 'register_plugin_styles' );
51
52 // call function on hook admin_menu
53 add_action('admin_menu','onwebchat_setup_menu');
54
55 // create login error notice
56 add_action( 'admin_notices', 'onwebchat_login_error' );
57
58 // define the settings
59 add_action('admin_init','onwebchat_register_setttings');
60
61 // handle form submissions early (before any output)
62 add_action('admin_init', 'onwebchat_handle_all_form_submissions');
63
64
65 /* ---------------------------------------------------- *
66 * Display onWebChat on footer
67 * ---------------------------------------------------- */
68
69 // add widget plugin to the footer
70 add_action('wp_footer', 'onwebchat_add_script_at_footer');
71
72
73 /* ----------------------------------------------------- *
74 * Admin Page registration
75 * ----------------------------------------------------- */
76
77 function onwebchat_setup_menu() {
78 add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO);
79 }
80
81
82 /* ------------------------------------------------------ *
83 * Admin Page Display Function
84 * ------------------------------------------------------ */
85
86 function onwebchat_init_menu_page() {
87 // Load the new tabbed settings page
88 require_once __DIR__ . '/admin/settings-page.php';
89 onwebchat_settings_page();
90 }
91
92 /* ----------------------------------------------------- *
93 * Admin Page Styles Registration
94 * ----------------------------------------------------- */
95
96 function register_plugin_styles() {
97 wp_register_style( 'onwebchat', plugins_url( 'onwebchat/css/onwebchat.css' ) );
98 wp_enqueue_style( 'onwebchat' );
99 }
100
101
102 /* ----------------------------------------------------- *
103 * Tabs Settings Registration
104 * ----------------------------------------------------- */
105
106 function onwebchat_register_setttings() {
107
108 /******************** Account Tab ************************/
109 //* register the Chat Id
110 register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option');
111
112 //* register the username
113 register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_user');
114
115 //* register the hide (checkbox)
116 register_setting( 'onwebchat_plugin_option_checkbox', 'onwebchat_plugin_option_hide');
117
118 //TODO api code
119 //* register the api code
120 register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_api_code');
121
122 /******************** WooCommerce Sync Settings ************************/
123 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_enabled');
124 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_mode');
125 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_include_price');
126 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_secret');
127 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_last_bulk_sync');
128 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_excluded_categories');
129 register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_categories');
130 }
131
132
133 /* ----------------------------------------------------- *
134 * onWebChat Display Function
135 * ----------------------------------------------------- */
136
137 // print chat widget code
138 function onwebchat_add_script_at_footer() {
139
140 $options = get_option('onwebchat_plugin_option');
141 $chatId = (is_array($options) && isset($options['text_string'])) ? $options['text_string'] : '';
142 //$hideWidget = get_option('onwebchat_plugin_option_hide');
143 $hideWidget = false;
144
145 // If no chat ID is configured, don't output widget code
146 if (empty($chatId)) {
147 return;
148 }
149
150 $widgetCode = "<script type='text/javascript'>var onWebChat={ar:[], set: function(a,b){if (typeof onWebChat_==='undefined'){this.ar.push([a,b]);}else{onWebChat_.set(a,b);}},get:function(a){return(onWebChat_.get(a));},w:(function(){ var ga=document.createElement('script'); ga.type = 'text/javascript';ga.async=1;ga.src='//www.onwebchat.com/clientchat/$chatId';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})()}</script>";
151
152 $apiCode = get_option('onwebchat_plugin_option_api_code');
153
154 $apiCode = str_replace('\\','',$apiCode);
155
156 $apiCode = "<script type='text/javascript'>$apiCode</script>";
157
158 $widgetCode .= $apiCode;
159
160
161 //check if user is logged if yes get username, email
162 if ( is_user_logged_in() ) {
163
164 $current_user = wp_get_current_user();
165
166 // get user name (escaped for safe use inside the inline script)
167 $onwebchat_user = esc_js( $current_user->user_login );
168
169 // get user email (escaped for safe use inside the inline script)
170 $onwebchat_email = esc_js( $current_user->user_email );
171
172 //add api info
173 if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") {
174 if ($onwebchat_email != null && $onwebchat_email != ""){
175 $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');
176 onWebChat.set('email','$onwebchat_email'); </script>";
177 } else {
178 $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');</script>";
179 }
180 }
181 }
182
183 $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") );
184 $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) );
185 $currentPage .= "://" . $_SERVER['SERVER_NAME'];
186 $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] );
187 $currentPage .= $_SERVER['REQUEST_URI'];
188
189 $pagesSelect = get_option('onwebchat_plugin_option_pages_select');
190 $showPages = get_option('onwebchat_plugin_option_show_pages');
191 $showPages = explode(' ', $showPages);
192 $hidePages = get_option('onwebchat_plugin_option_hide_pages');
193 $hidePages = explode(' ', $hidePages);
194
195 $showSelect=true;
196 if ($pagesSelect==1){
197
198 } else if ($pagesSelect==2){
199 $showSelect=false;
200 foreach ($showPages as $page){
201 if ($page !== "") {
202 if (strpos($currentPage, $page) !== false) {
203 $showSelect = true;
204 }
205 }
206 }
207 } else if ($pagesSelect==3){
208 $showSelect=true;
209 foreach ($hidePages as $page){
210 if ($page !== "") {
211 if (strpos($currentPage, $page) !== false) {
212 $showSelect = false;
213 }
214 }
215 }
216 } else if($pagesSelect == 4) {
217 //hide chat widget from all pages
218 $hideWidget = true;
219 }
220
221 if(!$hideWidget && $showSelect) {
222 echo $widgetCode;
223 }
224
225 }
226
227 /************************************************************************
228 * display error function
229 ***********************************************************************/
230 function onwebchat_login_error($contition = false) {
231
232
233
234 if($contition) {
235 ?>
236
237 <div class="error">
238 <p><strong>Invalid credentials!</strong> Please make sure you have entered the correct email and password, <strong>OR</strong> provide a valid Chat ID</p>
239 </div>
240 <?php
241 }
242 else {
243 //display nothing
244 }
245 }
246
247 /************************************************************************
248 * Validate Chat Id function
249 ***********************************************************************/
250 function isChatIdValid($input) {
251 return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input);
252 }
253
254 /************************************************************************
255 * Fetch WooCommerce secret during login
256 * Called automatically when user logs in with email/password
257 * This way they don't need to re-enter credentials in WooCommerce tab
258 ***********************************************************************/
259 function onwebchat_fetch_wc_secret_on_login($email, $password, $chatId) {
260 // Extract key part (before first slash if present)
261 $key = explode('/', $chatId)[0];
262
263 // Determine endpoint based on testing mode constant
264 $endpoint = ONWEBCHAT_WC_TESTING_MODE
265 ? 'http://127.0.0.1:81/api/integrations/woocommerce/secret'
266 : 'https://www.onwebchat.com/api/integrations/woocommerce/secret';
267
268 // Request secret with authentication
269 $response = wp_remote_post($endpoint, array(
270 'timeout' => 15,
271 'sslverify' => !ONWEBCHAT_WC_TESTING_MODE,
272 'headers' => array(
273 'Content-Type' => 'application/json',
274 ),
275 'body' => wp_json_encode(array(
276 'email' => $email,
277 'password' => $password,
278 'site_key' => $key,
279 'version' => ONWEBCHAT_PLUGIN_VERSION,
280 )),
281 ));
282
283 if (is_wp_error($response)) {
284 // Silently fail - user can still connect manually in WooCommerce tab
285 error_log('onWebChat: Failed to fetch WC secret on login: ' . $response->get_error_message());
286 return;
287 }
288
289 $status_code = wp_remote_retrieve_response_code($response);
290 $response_body = wp_remote_retrieve_body($response);
291 $body = json_decode($response_body, true);
292
293 if ($status_code >= 200 && $status_code < 300 && isset($body['success']) && $body['success']) {
294 // Success! Store the secret
295 update_option('onwebchat_wc_sync_secret', $body['secret']);
296 error_log('onWebChat: WooCommerce sync connected automatically during login');
297
298 // Enable AI order-status lookup by default on connect and register our
299 // callback URL with onWebChat (best effort; the merchant can toggle it off).
300 update_option('onwebchat_wc_order_lookup_enabled', true);
301 global $onwebchat_wc_orders;
302 if (isset($onwebchat_wc_orders) && is_object($onwebchat_wc_orders)) {
303 $onwebchat_wc_orders->push_order_lookup_config(true);
304 }
305 }
306 // If it fails, user can still connect manually - no error shown
307 }
308
309 /************************************************************************
310 * Handle all form submissions early to avoid blank page on redirect
311 * Must run on admin_init before any output is sent
312 ***********************************************************************/
313 function onwebchat_handle_all_form_submissions() {
314 // Only run on our settings page
315 if (!isset($_GET['page']) || $_GET['page'] !== 'onwebchat_settings') {
316 return;
317 }
318
319 // Load the general tab handlers
320 require_once __DIR__ . '/admin/tabs/general.php';
321 onwebchat_handle_general_actions();
322 }
323
324 // omit the closing php tag ? > for avoid issues
325