PluginProbe
AI Chatbot for WooCommerce & Live Chat – onWebChat / 3.7.0
AI Chatbot for WooCommerce & Live Chat – onWebChat v3.7.0
3.10.0 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 All 49 releases
onwebchat / onwebchat.php

onwebchat.php in AI Chatbot for WooCommerce & Live Chat – onWebChat 3.7.0, at onwebchat.php

316 lines 12.0 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.0
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.0');
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 }
130
131
132 /* ----------------------------------------------------- *
133 * onWebChat Display Function
134 * ----------------------------------------------------- */
135
136 // print chat widget code
137 function onwebchat_add_script_at_footer() {
138
139 $options = get_option('onwebchat_plugin_option');
140 $chatId = (is_array($options) && isset($options['text_string'])) ? $options['text_string'] : '';
141 //$hideWidget = get_option('onwebchat_plugin_option_hide');
142 $hideWidget = false;
143
144 // If no chat ID is configured, don't output widget code
145 if (empty($chatId)) {
146 return;
147 }
148
149 $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>";
150
151 $apiCode = get_option('onwebchat_plugin_option_api_code');
152
153 $apiCode = str_replace('\\','',$apiCode);
154
155 $apiCode = "<script type='text/javascript'>$apiCode</script>";
156
157 $widgetCode .= $apiCode;
158
159
160 //check if user is logged if yes get username, email
161 if ( is_user_logged_in() ) {
162
163 $current_user = wp_get_current_user();
164
165 // get user name (escaped for safe use inside the inline script)
166 $onwebchat_user = esc_js( $current_user->user_login );
167
168 // get user email (escaped for safe use inside the inline script)
169 $onwebchat_email = esc_js( $current_user->user_email );
170
171 //add api info
172 if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") {
173 if ($onwebchat_email != null && $onwebchat_email != ""){
174 $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');
175 onWebChat.set('email','$onwebchat_email'); </script>";
176 } else {
177 $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');</script>";
178 }
179 }
180 }
181
182 $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") );
183 $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) );
184 $currentPage .= "://" . $_SERVER['SERVER_NAME'];
185 $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] );
186 $currentPage .= $_SERVER['REQUEST_URI'];
187
188 $pagesSelect = get_option('onwebchat_plugin_option_pages_select');
189 $showPages = get_option('onwebchat_plugin_option_show_pages');
190 $showPages = explode(' ', $showPages);
191 $hidePages = get_option('onwebchat_plugin_option_hide_pages');
192 $hidePages = explode(' ', $hidePages);
193
194 $showSelect=true;
195 if ($pagesSelect==1){
196
197 } else if ($pagesSelect==2){
198 $showSelect=false;
199 foreach ($showPages as $page){
200 if ($page !== "") {
201 if (strpos($currentPage, $page) !== false) {
202 $showSelect = true;
203 }
204 }
205 }
206 } else if ($pagesSelect==3){
207 $showSelect=true;
208 foreach ($hidePages as $page){
209 if ($page !== "") {
210 if (strpos($currentPage, $page) !== false) {
211 $showSelect = false;
212 }
213 }
214 }
215 } else if($pagesSelect == 4) {
216 //hide chat widget from all pages
217 $hideWidget = true;
218 }
219
220 if(!$hideWidget && $showSelect) {
221 echo $widgetCode;
222 }
223
224 }
225
226 /************************************************************************
227 * display error function
228 ***********************************************************************/
229 function onwebchat_login_error($contition = false) {
230
231
232
233 if($contition) {
234 ?>
235
236 <div class="error">
237 <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>
238 </div>
239 <?php
240 }
241 else {
242 //display nothing
243 }
244 }
245
246 /************************************************************************
247 * Validate Chat Id function
248 ***********************************************************************/
249 function isChatIdValid($input) {
250 return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input);
251 }
252
253 /************************************************************************
254 * Fetch WooCommerce secret during login
255 * Called automatically when user logs in with email/password
256 * This way they don't need to re-enter credentials in WooCommerce tab
257 ***********************************************************************/
258 function onwebchat_fetch_wc_secret_on_login($email, $password, $chatId) {
259 // Extract key part (before first slash if present)
260 $key = explode('/', $chatId)[0];
261
262 // Determine endpoint based on testing mode constant
263 $endpoint = ONWEBCHAT_WC_TESTING_MODE
264 ? 'http://127.0.0.1:81/api/integrations/woocommerce/secret'
265 : 'https://www.onwebchat.com/api/integrations/woocommerce/secret';
266
267 // Request secret with authentication
268 $response = wp_remote_post($endpoint, array(
269 'timeout' => 15,
270 'sslverify' => !ONWEBCHAT_WC_TESTING_MODE,
271 'headers' => array(
272 'Content-Type' => 'application/json',
273 ),
274 'body' => wp_json_encode(array(
275 'email' => $email,
276 'password' => $password,
277 'site_key' => $key,
278 'version' => ONWEBCHAT_PLUGIN_VERSION,
279 )),
280 ));
281
282 if (is_wp_error($response)) {
283 // Silently fail - user can still connect manually in WooCommerce tab
284 error_log('onWebChat: Failed to fetch WC secret on login: ' . $response->get_error_message());
285 return;
286 }
287
288 $status_code = wp_remote_retrieve_response_code($response);
289 $response_body = wp_remote_retrieve_body($response);
290 $body = json_decode($response_body, true);
291
292 if ($status_code >= 200 && $status_code < 300 && isset($body['success']) && $body['success']) {
293 // Success! Store the secret
294 update_option('onwebchat_wc_sync_secret', $body['secret']);
295 error_log('onWebChat: WooCommerce sync connected automatically during login');
296 }
297 // If it fails, user can still connect manually - no error shown
298 }
299
300 /************************************************************************
301 * Handle all form submissions early to avoid blank page on redirect
302 * Must run on admin_init before any output is sent
303 ***********************************************************************/
304 function onwebchat_handle_all_form_submissions() {
305 // Only run on our settings page
306 if (!isset($_GET['page']) || $_GET['page'] !== 'onwebchat_settings') {
307 return;
308 }
309
310 // Load the general tab handlers
311 require_once __DIR__ . '/admin/tabs/general.php';
312 onwebchat_handle_general_actions();
313 }
314
315 // omit the closing php tag ? > for avoid issues
316