PluginProbe
AI Chatbot for WooCommerce & Live Chat – onWebChat / 3.10.0
AI Chatbot for WooCommerce & Live Chat – onWebChat v3.10.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
← All changes | onwebchat.php +347 -312 3.5.53.10.0 View file →
@@ -1,312 +1,347 @@
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.5.5
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.5.5');
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 sync module if WooCommerce is active
40 -add_action('plugins_loaded', function () {
41 - if (class_exists('WooCommerce')) {
42 - require_once __DIR__ . '/includes/woocommerce-sync.php';
43 - }
44 -});
45 -
46 -// add styles
47 -add_action( 'admin_enqueue_scripts', 'register_plugin_styles' );
48 -
49 -// call function on hook admin_menu
50 -add_action('admin_menu','onwebchat_setup_menu');
51 -
52 -// create login error notice
53 -add_action( 'admin_notices', 'onwebchat_login_error' );
54 -
55 -// define the settings
56 -add_action('admin_init','onwebchat_register_setttings');
57 -
58 -// handle form submissions early (before any output)
59 -add_action('admin_init', 'onwebchat_handle_all_form_submissions');
60 -
61 -
62 -/* ---------------------------------------------------- *
63 -* Display onWebChat on footer
64 -* ---------------------------------------------------- */
65 -
66 -// add widget plugin to the footer
67 -add_action('wp_footer', 'onwebchat_add_script_at_footer');
68 -
69 -
70 -/* ----------------------------------------------------- *
71 -* Admin Page registration
72 -* ----------------------------------------------------- */
73 -
74 -function onwebchat_setup_menu() {
75 - add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO);
76 -}
77 -
78 -
79 -/* ------------------------------------------------------ *
80 -* Admin Page Display Function
81 -* ------------------------------------------------------ */
82 -
83 -function onwebchat_init_menu_page() {
84 - // Load the new tabbed settings page
85 - require_once __DIR__ . '/admin/settings-page.php';
86 - onwebchat_settings_page();
87 -}
88 -
89 -/* ----------------------------------------------------- *
90 -* Admin Page Styles Registration
91 -* ----------------------------------------------------- */
92 -
93 -function register_plugin_styles() {
94 - wp_register_style( 'onwebchat', plugins_url( 'onwebchat/css/onwebchat.css' ) );
95 - wp_enqueue_style( 'onwebchat' );
96 -}
97 -
98 -
99 -/* ----------------------------------------------------- *
100 -* Tabs Settings Registration
101 -* ----------------------------------------------------- */
102 -
103 -function onwebchat_register_setttings() {
104 -
105 - /******************** Account Tab ************************/
106 - //* register the Chat Id
107 - register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option');
108 -
109 - //* register the username
110 - register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_user');
111 -
112 - //* register the hide (checkbox)
113 - register_setting( 'onwebchat_plugin_option_checkbox', 'onwebchat_plugin_option_hide');
114 -
115 - //TODO api code
116 - //* register the api code
117 - register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_api_code');
118 -
119 - /******************** WooCommerce Sync Settings ************************/
120 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_enabled');
121 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_mode');
122 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_include_price');
123 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_secret');
124 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_last_bulk_sync');
125 - register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_excluded_categories');
126 -}
127 -
128 -
129 -/* ----------------------------------------------------- *
130 -* onWebChat Display Function
131 -* ----------------------------------------------------- */
132 -
133 -// print chat widget code
134 -function onwebchat_add_script_at_footer() {
135 -
136 - $options = get_option('onwebchat_plugin_option');
137 - $chatId = (is_array($options) && isset($options['text_string'])) ? $options['text_string'] : '';
138 - //$hideWidget = get_option('onwebchat_plugin_option_hide');
139 - $hideWidget = false;
140 -
141 - // If no chat ID is configured, don't output widget code
142 - if (empty($chatId)) {
143 - return;
144 - }
145 -
146 - $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>";
147 -
148 - $apiCode = get_option('onwebchat_plugin_option_api_code');
149 -
150 - $apiCode = str_replace('\\','',$apiCode);
151 -
152 - $apiCode = "<script type='text/javascript'>$apiCode</script>";
153 -
154 - $widgetCode .= $apiCode;
155 -
156 -
157 - //check if user is logged if yes get username, email
158 - if ( is_user_logged_in() ) {
159 -
160 - $current_user = wp_get_current_user();
161 -
162 - // get user name (escaped for safe use inside the inline script)
163 - $onwebchat_user = esc_js( $current_user->user_login );
164 -
165 - // get user email (escaped for safe use inside the inline script)
166 - $onwebchat_email = esc_js( $current_user->user_email );
167 -
168 - //add api info
169 - if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") {
170 - if ($onwebchat_email != null && $onwebchat_email != ""){
171 - $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');
172 - onWebChat.set('email','$onwebchat_email'); </script>";
173 - } else {
174 - $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');</script>";
175 - }
176 - }
177 - }
178 -
179 - $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") );
180 - $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) );
181 - $currentPage .= "://" . $_SERVER['SERVER_NAME'];
182 - $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] );
183 - $currentPage .= $_SERVER['REQUEST_URI'];
184 -
185 - $pagesSelect = get_option('onwebchat_plugin_option_pages_select');
186 - $showPages = get_option('onwebchat_plugin_option_show_pages');
187 - $showPages = explode(' ', $showPages);
188 - $hidePages = get_option('onwebchat_plugin_option_hide_pages');
189 - $hidePages = explode(' ', $hidePages);
190 -
191 - $showSelect=true;
192 - if ($pagesSelect==1){
193 -
194 - } else if ($pagesSelect==2){
195 - $showSelect=false;
196 - foreach ($showPages as $page){
197 - if ($page !== "") {
198 - if (strpos($currentPage, $page) !== false) {
199 - $showSelect = true;
200 - }
201 - }
202 - }
203 - } else if ($pagesSelect==3){
204 - $showSelect=true;
205 - foreach ($hidePages as $page){
206 - if ($page !== "") {
207 - if (strpos($currentPage, $page) !== false) {
208 - $showSelect = false;
209 - }
210 - }
211 - }
212 - } else if($pagesSelect == 4) {
213 - //hide chat widget from all pages
214 - $hideWidget = true;
215 - }
216 -
217 - if(!$hideWidget && $showSelect) {
218 - echo $widgetCode;
219 - }
220 -
221 -}
222 -
223 -/************************************************************************
224 - * display error function
225 - ***********************************************************************/
226 -function onwebchat_login_error($contition = false) {
227 -
228 -
229 -
230 - if($contition) {
231 - ?>
232 -
233 - <div class="error">
234 - <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>
235 - </div>
236 - <?php
237 - }
238 - else {
239 - //display nothing
240 - }
241 -}
242 -
243 -/************************************************************************
244 - * Validate Chat Id function
245 - ***********************************************************************/
246 -function isChatIdValid($input) {
247 - return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input);
248 -}
249 -
250 -/************************************************************************
251 - * Fetch WooCommerce secret during login
252 - * Called automatically when user logs in with email/password
253 - * This way they don't need to re-enter credentials in WooCommerce tab
254 - ***********************************************************************/
255 -function onwebchat_fetch_wc_secret_on_login($email, $password, $chatId) {
256 - // Extract key part (before first slash if present)
257 - $key = explode('/', $chatId)[0];
258 -
259 - // Determine endpoint based on testing mode constant
260 - $endpoint = ONWEBCHAT_WC_TESTING_MODE
261 - ? 'http://127.0.0.1:81/api/integrations/woocommerce/secret'
262 - : 'https://www.onwebchat.com/api/integrations/woocommerce/secret';
263 -
264 - // Request secret with authentication
265 - $response = wp_remote_post($endpoint, array(
266 - 'timeout' => 15,
267 - 'sslverify' => !ONWEBCHAT_WC_TESTING_MODE,
268 - 'headers' => array(
269 - 'Content-Type' => 'application/json',
270 - ),
271 - 'body' => wp_json_encode(array(
272 - 'email' => $email,
273 - 'password' => $password,
274 - 'site_key' => $key,
275 - 'version' => ONWEBCHAT_PLUGIN_VERSION,
276 - )),
277 - ));
278 -
279 - if (is_wp_error($response)) {
280 - // Silently fail - user can still connect manually in WooCommerce tab
281 - error_log('onWebChat: Failed to fetch WC secret on login: ' . $response->get_error_message());
282 - return;
283 - }
284 -
285 - $status_code = wp_remote_retrieve_response_code($response);
286 - $response_body = wp_remote_retrieve_body($response);
287 - $body = json_decode($response_body, true);
288 -
289 - if ($status_code >= 200 && $status_code < 300 && isset($body['success']) && $body['success']) {
290 - // Success! Store the secret
291 - update_option('onwebchat_wc_sync_secret', $body['secret']);
292 - error_log('onWebChat: WooCommerce sync connected automatically during login');
293 - }
294 - // If it fails, user can still connect manually - no error shown
295 -}
296 -
297 -/************************************************************************
298 - * Handle all form submissions early to avoid blank page on redirect
299 - * Must run on admin_init before any output is sent
300 - ***********************************************************************/
301 -function onwebchat_handle_all_form_submissions() {
302 - // Only run on our settings page
303 - if (!isset($_GET['page']) || $_GET['page'] !== 'onwebchat_settings') {
304 - return;
305 - }
306 -
307 - // Load the general tab handlers
308 - require_once __DIR__ . '/admin/tabs/general.php';
309 - onwebchat_handle_general_actions();
310 -}
311 -
312 -// omit the closing php tag ? > for avoid issues
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.10.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.10.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 + 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 + // Merchant option: show the chat widget only to signed-in users (default: off).
151 + if (get_option('onwebchat_plugin_option_logged_only', 0) && !is_user_logged_in()) {
152 + return;
153 + }
154 +
155 + $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>";
156 +
157 + $apiCode = get_option('onwebchat_plugin_option_api_code');
158 +
159 + $apiCode = str_replace('\\','',$apiCode);
160 +
161 + $apiCode = "<script type='text/javascript'>$apiCode</script>";
162 +
163 + $widgetCode .= $apiCode;
164 +
165 +
166 + // Identity PROOF for the logged-in email: HMAC_SHA256(shared sync secret, lowercase(email)).
167 + // The onWebChat server recomputes it with the same secret; only when it matches does the AI treat
168 + // the email as verified and allow the email-only "my recent orders" lookup. A visitor faking
169 + // onWebChat.set('email') from the console cannot produce this hash, so they can never list another
170 + // customer's orders. The '0' sentinel means "not signed in": it tells onWebChat to DROP any stored
171 + // proof, so on a shared computer nobody inherits the previous customer's identity after a logout.
172 + $onwebchat_email_hash = '0';
173 + $onwebchat_wc_secret = get_option('onwebchat_wc_sync_secret');
174 +
175 + //check if user is logged if yes get username, email
176 + if ( is_user_logged_in() ) {
177 +
178 + $current_user = wp_get_current_user();
179 +
180 + // get user name (escaped for safe use inside the inline script)
181 + $onwebchat_user = esc_js( $current_user->user_login );
182 +
183 + // get user email (escaped for safe use inside the inline script)
184 + $onwebchat_email = esc_js( $current_user->user_email );
185 +
186 + //add api info
187 + if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") {
188 + if ($onwebchat_email != null && $onwebchat_email != ""){
189 + // Hash the raw (unescaped) email so both sides hash identical bytes.
190 + if (!empty($onwebchat_wc_secret)) {
191 + $onwebchat_email_hash = hash_hmac('sha256', strtolower(trim($current_user->user_email)), $onwebchat_wc_secret);
192 + }
193 +
194 + $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');
195 + onWebChat.set('email','$onwebchat_email');</script>";
196 + } else {
197 + $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');</script>";
198 + }
199 + }
200 + }
201 +
202 + if (!empty($onwebchat_wc_secret)) {
203 + $widgetCode .= "<script type='text/javascript'>onWebChat.set('emailHash','" . esc_js($onwebchat_email_hash) . "');</script>";
204 + }
205 +
206 + $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") );
207 + $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) );
208 + $currentPage .= "://" . $_SERVER['SERVER_NAME'];
209 + $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] );
210 + $currentPage .= $_SERVER['REQUEST_URI'];
211 +
212 + $pagesSelect = get_option('onwebchat_plugin_option_pages_select');
213 + $showPages = get_option('onwebchat_plugin_option_show_pages');
214 + $showPages = explode(' ', $showPages);
215 + $hidePages = get_option('onwebchat_plugin_option_hide_pages');
216 + $hidePages = explode(' ', $hidePages);
217 +
218 + $showSelect=true;
219 + if ($pagesSelect==1){
220 +
221 + } else if ($pagesSelect==2){
222 + $showSelect=false;
223 + foreach ($showPages as $page){
224 + if ($page !== "") {
225 + if (strpos($currentPage, $page) !== false) {
226 + $showSelect = true;
227 + }
228 + }
229 + }
230 + } else if ($pagesSelect==3){
231 + $showSelect=true;
232 + foreach ($hidePages as $page){
233 + if ($page !== "") {
234 + if (strpos($currentPage, $page) !== false) {
235 + $showSelect = false;
236 + }
237 + }
238 + }
239 + } else if($pagesSelect == 4) {
240 + //hide chat widget from all pages
241 + $hideWidget = true;
242 + }
243 +
244 + if(!$hideWidget && $showSelect) {
245 + echo $widgetCode;
246 + }
247 +
248 +}
249 +
250 +/************************************************************************
251 + * display error function
252 + ***********************************************************************/
253 +function onwebchat_login_error($contition = false) {
254 +
255 +
256 +
257 + if($contition) {
258 + ?>
259 +
260 + <div class="error">
261 + <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>
262 + </div>
263 + <?php
264 + }
265 + else {
266 + //display nothing
267 + }
268 +}
269 +
270 +/************************************************************************
271 + * Validate Chat Id function
272 + ***********************************************************************/
273 +function isChatIdValid($input) {
274 + return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input);
275 +}
276 +
277 +/************************************************************************
278 + * Fetch WooCommerce secret during login
279 + * Called automatically when user logs in with email/password
280 + * This way they don't need to re-enter credentials in WooCommerce tab
281 + ***********************************************************************/
282 +function onwebchat_fetch_wc_secret_on_login($email, $password, $chatId) {
283 + // Extract key part (before first slash if present)
284 + $key = explode('/', $chatId)[0];
285 +
286 + // Determine endpoint based on testing mode constant
287 + $endpoint = ONWEBCHAT_WC_TESTING_MODE
288 + ? 'http://127.0.0.1:81/api/integrations/woocommerce/secret'
289 + : 'https://www.onwebchat.com/api/integrations/woocommerce/secret';
290 +
291 + // Request secret with authentication
292 + $response = wp_remote_post($endpoint, array(
293 + 'timeout' => 15,
294 + 'sslverify' => !ONWEBCHAT_WC_TESTING_MODE,
295 + 'headers' => array(
296 + 'Content-Type' => 'application/json',
297 + ),
298 + 'body' => wp_json_encode(array(
299 + 'email' => $email,
300 + 'password' => $password,
301 + 'site_key' => $key,
302 + 'version' => ONWEBCHAT_PLUGIN_VERSION,
303 + )),
304 + ));
305 +
306 + if (is_wp_error($response)) {
307 + // Silently fail - user can still connect manually in WooCommerce tab
308 + error_log('onWebChat: Failed to fetch WC secret on login: ' . $response->get_error_message());
309 + return;
310 + }
311 +
312 + $status_code = wp_remote_retrieve_response_code($response);
313 + $response_body = wp_remote_retrieve_body($response);
314 + $body = json_decode($response_body, true);
315 +
316 + if ($status_code >= 200 && $status_code < 300 && isset($body['success']) && $body['success']) {
317 + // Success! Store the secret
318 + update_option('onwebchat_wc_sync_secret', $body['secret']);
319 + error_log('onWebChat: WooCommerce sync connected automatically during login');
320 +
321 + // Enable AI order-status lookup by default on connect and register our
322 + // callback URL with onWebChat (best effort; the merchant can toggle it off).
323 + update_option('onwebchat_wc_order_lookup_enabled', true);
324 + global $onwebchat_wc_orders;
325 + if (isset($onwebchat_wc_orders) && is_object($onwebchat_wc_orders)) {
326 + $onwebchat_wc_orders->push_order_lookup_config(true);
327 + }
328 + }
329 + // If it fails, user can still connect manually - no error shown
330 +}
331 +
332 +/************************************************************************
333 + * Handle all form submissions early to avoid blank page on redirect
334 + * Must run on admin_init before any output is sent
335 + ***********************************************************************/
336 +function onwebchat_handle_all_form_submissions() {
337 + // Only run on our settings page
338 + if (!isset($_GET['page']) || $_GET['page'] !== 'onwebchat_settings') {
339 + return;
340 + }
341 +
342 + // Load the general tab handlers
343 + require_once __DIR__ . '/admin/tabs/general.php';
344 + onwebchat_handle_general_actions();
345 +}
346 +
347 +// omit the closing php tag ? > for avoid issues