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