| @@ -1,347 +1,547 @@ | ||
| 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.9.3 | |
| 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.9.3'); | |
| 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 | |
| 1 | +<?php | |
| 2 | +/* | |
| 3 | +Plugin Name: onWebChat Live Chat | |
| 4 | +Plugin URI: https://www.onwebchat.com/wordpress-live-chat-plugin.php | |
| 5 | +Description: live chat service that helps you communicate with your website's visitors. | |
| 6 | +Author: onWebChat | |
| 7 | +Version: 3.2.0 | |
| 8 | +Author URI: https://www.onwebchat.com | |
| 9 | +*/ | |
| 10 | + | |
| 11 | + | |
| 12 | +/* ---------------------------------------------------- * | |
| 13 | +* Create Settings Page | |
| 14 | +* ---------------------------------------------------- */ | |
| 15 | + | |
| 16 | + | |
| 17 | +define('ONWEBCHAT_SMALL_LOGO', plugins_url( 'images/onwebchat-logo.png' , __FILE__ )); | |
| 18 | +define('ONWEBCHAT_SERVER_URL','https://www.onwebchat.com/get-chatid.php'); | |
| 19 | + | |
| 20 | + | |
| 21 | +// add styles | |
| 22 | +add_action( 'admin_enqueue_scripts', 'register_plugin_styles' ); | |
| 23 | + | |
| 24 | +// call function on hook admin_menu | |
| 25 | +add_action('admin_menu','onwebchat_setup_menu'); | |
| 26 | + | |
| 27 | +// create login error notice | |
| 28 | +add_action( 'admin_notices', 'onwebchat_login_error' ); | |
| 29 | + | |
| 30 | +// define the settings | |
| 31 | +add_action('admin_init','onwebchat_register_setttings'); | |
| 32 | + | |
| 33 | + | |
| 34 | +/* ---------------------------------------------------- * | |
| 35 | +* Display onWebChat on footer | |
| 36 | +* ---------------------------------------------------- */ | |
| 37 | + | |
| 38 | +// add widget plugin to the footer | |
| 39 | +add_action('wp_footer', 'onwebchat_add_script_at_footer'); | |
| 40 | + | |
| 41 | + | |
| 42 | +/* ----------------------------------------------------- * | |
| 43 | +* Admin Page registration | |
| 44 | +* ----------------------------------------------------- */ | |
| 45 | + | |
| 46 | +function onwebchat_setup_menu() { | |
| 47 | + add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO); | |
| 48 | +} | |
| 49 | + | |
| 50 | + | |
| 51 | +/* ------------------------------------------------------ * | |
| 52 | +* Admin Page Display Function | |
| 53 | +* ------------------------------------------------------ */ | |
| 54 | + | |
| 55 | +function onwebchat_init_menu_page() { | |
| 56 | + | |
| 57 | + | |
| 58 | + $options = get_option('onwebchat_plugin_option'); | |
| 59 | + $chatId = $options['text_string']; | |
| 60 | + | |
| 61 | + $isConnected = false; | |
| 62 | + if($chatId !="") { | |
| 63 | + $isConnected = true; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $isSecondPage = false; | |
| 67 | + | |
| 68 | + | |
| 69 | + /***************************************************************** | |
| 70 | + * Ask server for chatId | |
| 71 | + *****************************************************************/ | |
| 72 | + if ( isset( $_POST["action"] ) && $_POST["action"] == "login" ) { | |
| 73 | + | |
| 74 | + | |
| 75 | + // the following lines are added to verify a correct security nonce(token) by using "wp_verify_nonce()" | |
| 76 | + if (! isset($_POST['_wpnonce']) | |
| 77 | + || ! wp_verify_nonce( $_POST['_wpnonce'], 'on_web_chat_nonce')){ | |
| 78 | + print 'Sorry, your nonce did not verify.'; | |
| 79 | + exit; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $options = get_option('onwebchat_plugin_option'); | |
| 83 | + $chatId = $options['text_string']; | |
| 84 | + //**************************** | |
| 85 | + | |
| 86 | + // get username, password and chatId | |
| 87 | + $userName = $_POST["onWebChatUser"]; | |
| 88 | + | |
| 89 | + if(isset($_POST["onWebChatPass"])) { | |
| 90 | + $userPass = $_POST["onWebChatPass"]; | |
| 91 | + } | |
| 92 | + | |
| 93 | + $chatId = $_POST["chatId"]; | |
| 94 | + | |
| 95 | + | |
| 96 | + if( isset($_POST["pages-select"])) { | |
| 97 | + $pagesSelect = $_POST["pages-select"]; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if( isset($_POST["showonpages"])) { | |
| 101 | + $showOnPages = $_POST["showonpages"]; | |
| 102 | + } | |
| 103 | + | |
| 104 | + if(isset($_POST["hideonpages"])) { | |
| 105 | + $hideOnPages = $_POST["hideonpages"]; | |
| 106 | + } | |
| 107 | + | |
| 108 | + if(isset($_POST["onwebchat-api"])) { | |
| 109 | + $onwebchatApi = $_POST["onwebchat-api"]; | |
| 110 | + } | |
| 111 | + | |
| 112 | + | |
| 113 | + if(isChatIdValid($chatId)) { | |
| 114 | + | |
| 115 | + if ( isset($_POST["isSecondPage"])){ | |
| 116 | + $isSecondPage = $_POST["isSecondPage"]; | |
| 117 | + } | |
| 118 | + | |
| 119 | + //----------- update chatId option ------------ | |
| 120 | + $my_options = get_option('onwebchat_plugin_option'); | |
| 121 | + | |
| 122 | + $my_options['text_string'] = $chatId; | |
| 123 | + | |
| 124 | + //Update entire array | |
| 125 | + update_option('onwebchat_plugin_option', $my_options); | |
| 126 | + //----------------------------------------------- | |
| 127 | + | |
| 128 | + // remove user email from db | |
| 129 | + if(!$isSecondPage) { | |
| 130 | + update_option( 'onwebchat_plugin_option_user', ''); | |
| 131 | + update_option( 'onwebchat_plugin_option_pages_select', 1); | |
| 132 | + | |
| 133 | + } else { | |
| 134 | + | |
| 135 | + update_option( 'onwebchat_plugin_option_pages_select', $pagesSelect); | |
| 136 | + | |
| 137 | + if ($pagesSelect==2){ | |
| 138 | + update_option( 'onwebchat_plugin_option_show_pages', $showOnPages); | |
| 139 | + } else if ($pagesSelect==3){ | |
| 140 | + update_option( 'onwebchat_plugin_option_hide_pages', $hideOnPages); | |
| 141 | + } | |
| 142 | + | |
| 143 | + //TODO update textarea | |
| 144 | + update_option( 'onwebchat_plugin_option_api_code', $onwebchatApi); | |
| 145 | + } | |
| 146 | + | |
| 147 | + // move to next page | |
| 148 | + print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); | |
| 149 | + | |
| 150 | + } else { | |
| 151 | + | |
| 152 | + // save username at options | |
| 153 | + update_option( 'onwebchat_plugin_option_user', $userName ); | |
| 154 | + | |
| 155 | + // ask server for chatId | |
| 156 | + $response = wp_remote_post(ONWEBCHAT_SERVER_URL, array( | |
| 157 | + 'method' => 'POST', | |
| 158 | + 'timeout' => 45, | |
| 159 | + 'redirection' => 5, | |
| 160 | + 'httpversion' => '1.0', | |
| 161 | + 'blocking' => true, | |
| 162 | + 'headers' => array(), | |
| 163 | + 'body' => array( 'email' => $userName, 'pass' => $userPass ), | |
| 164 | + 'cookies' => array() | |
| 165 | + )); | |
| 166 | + | |
| 167 | + if ( is_wp_error( $response ) ) { | |
| 168 | + $error_message = $response->get_error_message(); | |
| 169 | + | |
| 170 | + } else { | |
| 171 | + // If no chatId returned | |
| 172 | + if($response['body'] == '-1') { | |
| 173 | + // display an error for wrong credentials | |
| 174 | + onwebchat_login_error(true); | |
| 175 | + | |
| 176 | + } else { | |
| 177 | + | |
| 178 | + //----------- update chatId | |
| 179 | + $my_options = get_option('onwebchat_plugin_option'); | |
| 180 | + | |
| 181 | + $my_options['text_string'] = $response['body']; | |
| 182 | + | |
| 183 | + update_option('onwebchat_plugin_option', $my_options); | |
| 184 | + update_option( 'onwebchat_plugin_option_pages_select', 1); | |
| 185 | + //-------------------------------------------- | |
| 186 | + | |
| 187 | + // move to next page | |
| 188 | + print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + } | |
| 192 | + | |
| 193 | + $m_option_hide = false; | |
| 194 | + if(isset($_POST['onwebchat_plugin_option_hide'])) { | |
| 195 | + $m_option_hide = $_POST['onwebchat_plugin_option_hide']; | |
| 196 | + } | |
| 197 | + | |
| 198 | + update_option('onwebchat_plugin_option_hide', $m_option_hide); //an einai stin proti selida tote to kanei keno (opote by default den einai epilegmeno) | |
| 199 | + | |
| 200 | + } | |
| 201 | + | |
| 202 | + //******************************************************************** | |
| 203 | + | |
| 204 | + | |
| 205 | + /********************************************************************* | |
| 206 | + * Disconnect account | |
| 207 | + *********************************************************************/ | |
| 208 | + if( isset($_GET["action"]) && $_GET["action"] == "deactivate" && $isConnected) { | |
| 209 | + $isConnected = false; | |
| 210 | + | |
| 211 | + $my_options = get_option('onwebchat_plugin_option'); | |
| 212 | + | |
| 213 | + $my_options['text_string'] = ''; | |
| 214 | + | |
| 215 | + update_option('onwebchat_plugin_option', $my_options); | |
| 216 | + update_option( 'onwebchat_plugin_option_pages_select', 1); | |
| 217 | + | |
| 218 | + $chatIdOption = get_option('onwebchat_plugin_option'); | |
| 219 | + $chatId = $chatIdOption['text_string']; | |
| 220 | + | |
| 221 | + print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); | |
| 222 | + | |
| 223 | + } | |
| 224 | + //*********************************************************************** | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + ?> | |
| 229 | + <div> | |
| 230 | + <h2 style="font-size:22px; color:#59808c">onWebChat <?php ($isConnected == true) ? print 'Settings' : print 'Activation'; ?> </h2> | |
| 231 | + | |
| 232 | + <form action="admin.php?page=onwebchat_settings" method="post"> | |
| 233 | + <input type="hidden" name="action" value="login"> | |
| 234 | + <?php | |
| 235 | + | |
| 236 | + //create nonce(token) | |
| 237 | + wp_nonce_field('on_web_chat_nonce'); | |
| 238 | + | |
| 239 | + // Login Page | |
| 240 | + if($isConnected != true) { | |
| 241 | + | |
| 242 | + $chatId = get_option( 'onwebchat_plugin_option' ); | |
| 243 | + $chatId = $chatId['text_string']; | |
| 244 | + ?> | |
| 245 | + | |
| 246 | + <hr class="small-hr2"> | |
| 247 | + | |
| 248 | + <h3 class="header-1">Connect with your onWebChat account</h3> | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + <div class="username-div"> | |
| 253 | + <!-- sanitize user-provided parameter --> | |
| 254 | + <strong>Email: </strong><input class="username-text-field" type="text" name="onWebChatUser" value="<?php echo esc_attr(get_option( 'onwebchat_plugin_option_user' )); ?>"/> | |
| 255 | + </div> | |
| 256 | + | |
| 257 | + <div class="password-div"> | |
| 258 | + <strong>Password: </strong><input class="password-text-field" type="password" name="onWebChatPass" value="<?php echo get_option( 'onWebChatPass' ); ?>"/> | |
| 259 | + </div> | |
| 260 | + | |
| 261 | + <div style="width: 560px; height: 10px; border-bottom: 1px solid #bfbfbf; text-align: center; margin-top:30px"> | |
| 262 | + <span class="header-or" style="font-size: 23px; background-color: #f1f1f1; padding: 0 10px;"> | |
| 263 | + OR | |
| 264 | + </span> | |
| 265 | + </div> | |
| 266 | + | |
| 267 | + | |
| 268 | + <h3 class="header-2">Paste your onWebChat Chat Id</h3> | |
| 269 | + <div class="chatid-div"> | |
| 270 | + <strong>Chat Id:</strong> <input class="chatid-text-field" type="text" name="chatId" value="<?php echo $chatId; ?>"/> | |
| 271 | + </div> | |
| 272 | + | |
| 273 | + <hr class="small-hr2"> | |
| 274 | + | |
| 275 | + <div class="new-account-link"> | |
| 276 | + <span class="info-star">*</span> If you don't have an account on onWebChat live chat service, you should create one <a href="https://www.onwebchat.com/signup.php" target="_blank">here</a> | |
| 277 | + </div> | |
| 278 | + | |
| 279 | + | |
| 280 | + <?php | |
| 281 | + } | |
| 282 | + | |
| 283 | + // 2nd Page | |
| 284 | + else { | |
| 285 | + $options = get_option('onwebchat_plugin_option_user'); | |
| 286 | + | |
| 287 | + // display user email | |
| 288 | + if($options!=''){ | |
| 289 | + //sanitize user-provided parameter | |
| 290 | + $email = esc_html($options); | |
| 291 | + $html = '<br><h3 class="header-1-p2">Activated for onWebChat account: </h3>'; | |
| 292 | + $html .= "<strong class='account-id'>$email</strong> "; | |
| 293 | + } | |
| 294 | + | |
| 295 | + // display chatId | |
| 296 | + else { | |
| 297 | + $chatId = get_option( 'onwebchat_plugin_option' ); | |
| 298 | + $chatId = $chatId['text_string']; | |
| 299 | + $html = '<br><h3 class="header-1-p2">Activated for onWebChat Chat Id: </h3>'; | |
| 300 | + $html .= "<strong class='account-id'>$chatId</strong> "; | |
| 301 | + } | |
| 302 | + $html .= ' <a href="admin.php?page=onwebchat_settings&action=deactivate">Deactivate</a>'; | |
| 303 | + //$html .= ' <div class="admin-login-info"> <span>*</span> To connect to onWebChat Operator Console click <a target="_blank" href="https://www.onwebchat.com/login.php">here</a> </div>'; | |
| 304 | + echo $html; | |
| 305 | + | |
| 306 | + $options = get_option('onwebchat_plugin_option_hide'); | |
| 307 | + | |
| 308 | + $chatId = get_option( 'onwebchat_plugin_option' ); | |
| 309 | + $chatId = $chatId['text_string']; | |
| 310 | + | |
| 311 | + //TODO get correct value | |
| 312 | + $onwebchatApi = get_option('onwebchat_plugin_option_api_code'); | |
| 313 | + $onwebchatApi = str_replace('\\','',$onwebchatApi); | |
| 314 | + ?> | |
| 315 | + <!-- <div class="hide-div">--> | |
| 316 | + <!-- <strong>Hide Chat Widget:</strong> <input type="checkbox" id="plugin_text_checkbox" name="onwebchat_plugin_option_hide" value="1" --><?php //checked( $options, 1 ); ?><!-- />--> | |
| 317 | + <!-- </div>--> | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + <div class="hide-div"> | |
| 322 | + <select id="pages-select" name="pages-select" onchange="onwc_select_change()"> | |
| 323 | + <option value="1" <?php selected( get_option('onwebchat_plugin_option_pages_select'), 1 ); ?>>Show chat widget on all pages</option> | |
| 324 | + <option value="2" <?php selected( get_option('onwebchat_plugin_option_pages_select'), 2 ); ?>>Show on the following pages:</option> | |
| 325 | + <option value="3" <?php selected( get_option('onwebchat_plugin_option_pages_select'), 3 ); ?>>Hide on the following pages:</option> | |
| 326 | + <option value="4" <?php selected( get_option('onwebchat_plugin_option_pages_select'), 4 ); ?>>Hide chat widget from all pages</option> | |
| 327 | + </select> | |
| 328 | + </div> | |
| 329 | + | |
| 330 | + <div id="onwc_show_on_pages_div" style="display:none"> | |
| 331 | + <input id="showonpages" name="showonpages" class="showhidepages" type="text" value="<?php echo esc_attr(get_option( 'onwebchat_plugin_option_show_pages' )); ?>" /><a href="#" style="text-decoration: none;" onmouseover="document.getElementById('help').style.visibility = 'visible'"; ONMOUSEOUT="document.getElementById('help').style.visibility = 'hidden'"><strong><font size="4" face="Arial"> ? </font></strong></a> | |
| 332 | + </div> | |
| 333 | + <div id="onwc_hide_on_pages_div" style="display:none"> | |
| 334 | + <input id="hideonpages" name="hideonpages" class="showhidepages" type="text" value="<?php echo esc_attr(get_option( 'onwebchat_plugin_option_hide_pages' )); ?>" /><a href="#" style="text-decoration: none;" onmouseover="document.getElementById('help').style.visibility = 'visible'"; ONMOUSEOUT="document.getElementById('help').style.visibility = 'hidden'"><strong><font size="4" face="Arial"> ? </font></strong></a> | |
| 335 | + </div> | |
| 336 | + | |
| 337 | + <div id="help"> | |
| 338 | + <span>You can insert multiple pages seperated by space. You can insert a part of the page or URL. e.g. index pric contact.php blog/</span> | |
| 339 | + </div> | |
| 340 | + | |
| 341 | + <h3 class="header-2">Write your onWebChat Chat API code</h3> | |
| 342 | + <div class="chatid-div"> | |
| 343 | + <strong>onWebChat API:</strong> | |
| 344 | + <br> | |
| 345 | + <!-- sanitize user-provided parameter --> | |
| 346 | + <textarea class="chatid-text-field" style="margin-left: 0px;" rows="10" name="onwebchat-api"><?php echo esc_html($onwebchatApi); ?></textarea> | |
| 347 | + <br> | |
| 348 | + <br> | |
| 349 | + </div> | |
| 350 | + | |
| 351 | + <div> | |
| 352 | + <br /><br /> | |
| 353 | + </div> | |
| 354 | + | |
| 355 | + <!-- hiden fields --> | |
| 356 | + <input class="chatid-text-field-hide" type="text" name="chatId" value="<?php echo esc_attr($chatId); ?>"/> | |
| 357 | + <input class="chatid-text-field-hide" type="text" name="onWebChatUser" value="<?php echo esc_attr(get_option( 'onwebchat_plugin_option_user' )); ?>"/> | |
| 358 | + <input class="chatid-text-field-hide" type="text" name="isSecondPage" value="1"/> | |
| 359 | + | |
| 360 | + <div class="new-account-link"> <span>*</span> To connect to onWebChat Operator Console click <a target="_blank" href="https://www.onwebchat.com/login.php">here</a> </div> | |
| 361 | + <?php | |
| 362 | + | |
| 363 | + | |
| 364 | + } | |
| 365 | + | |
| 366 | + // Display the Save Button | |
| 367 | + $html = '<input class="button button-primary" style="margin-left: 230px;" type="submit" value="Save Changes"/>'; | |
| 368 | + echo $html; | |
| 369 | + ?> | |
| 370 | + </form> | |
| 371 | + </div> | |
| 372 | + | |
| 373 | + <script type="text/javascript"> | |
| 374 | + | |
| 375 | + | |
| 376 | + function onwc_select_change() { | |
| 377 | + var e = document.getElementById("pages-select"); | |
| 378 | + var selected = e.options[e.selectedIndex].value; | |
| 379 | + if (selected == 1){ | |
| 380 | + document.getElementById("onwc_show_on_pages_div").style.display = "none"; | |
| 381 | + document.getElementById("onwc_hide_on_pages_div").style.display = "none"; | |
| 382 | + } else if (selected == 2){ | |
| 383 | + document.getElementById("onwc_show_on_pages_div").style.display = "block"; | |
| 384 | + document.getElementById("onwc_hide_on_pages_div").style.display = "none"; | |
| 385 | + } else if (selected == 3){ | |
| 386 | + document.getElementById("onwc_show_on_pages_div").style.display = "none"; | |
| 387 | + document.getElementById("onwc_hide_on_pages_div").style.display = "block"; | |
| 388 | + } else if (selected == 4){ | |
| 389 | + document.getElementById("onwc_show_on_pages_div").style.display = "none"; | |
| 390 | + document.getElementById("onwc_hide_on_pages_div").style.display = "none"; | |
| 391 | + } | |
| 392 | + | |
| 393 | + } | |
| 394 | + document.addEventListener('DOMContentLoaded', function() { | |
| 395 | + onwc_select_change(); | |
| 396 | + }, false); | |
| 397 | + </script> | |
| 398 | + <?php | |
| 399 | +} | |
| 400 | + | |
| 401 | + | |
| 402 | +/* ----------------------------------------------------- * | |
| 403 | +* Admin Page Styles Registration | |
| 404 | +* ----------------------------------------------------- */ | |
| 405 | + | |
| 406 | +function register_plugin_styles() { | |
| 407 | + wp_register_style( 'onwebchat', plugins_url( 'onwebchat/css/onwebchat.css' ) ); | |
| 408 | + wp_enqueue_style( 'onwebchat' ); | |
| 409 | +} | |
| 410 | + | |
| 411 | + | |
| 412 | +/* ----------------------------------------------------- * | |
| 413 | +* Tabs Settings Registration | |
| 414 | +* ----------------------------------------------------- */ | |
| 415 | + | |
| 416 | +function onwebchat_register_setttings() { | |
| 417 | + | |
| 418 | + /******************** Account Tab ************************/ | |
| 419 | + //* register the Chat Id | |
| 420 | + register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option'); | |
| 421 | + | |
| 422 | + //* register the username | |
| 423 | + register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_user'); | |
| 424 | + | |
| 425 | + //* register the hide (checkbox) | |
| 426 | + register_setting( 'onwebchat_plugin_option_checkbox', 'onwebchat_plugin_option_hide'); | |
| 427 | + | |
| 428 | + //TODO api code | |
| 429 | + //* register the api code | |
| 430 | + register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_api_code'); | |
| 431 | +} | |
| 432 | + | |
| 433 | + | |
| 434 | +/* ----------------------------------------------------- * | |
| 435 | +* onWebChat Display Function | |
| 436 | +* ----------------------------------------------------- */ | |
| 437 | + | |
| 438 | +// print chat widget code | |
| 439 | +function onwebchat_add_script_at_footer() { | |
| 440 | + | |
| 441 | + $options = get_option('onwebchat_plugin_option'); | |
| 442 | + $chatId = $options['text_string']; | |
| 443 | + //$hideWidget = get_option('onwebchat_plugin_option_hide'); | |
| 444 | + $hideWidget = false; | |
| 445 | + | |
| 446 | + $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>"; | |
| 447 | + | |
| 448 | + $apiCode = get_option('onwebchat_plugin_option_api_code'); | |
| 449 | + | |
| 450 | + $apiCode = str_replace('\\','',$apiCode); | |
| 451 | + | |
| 452 | + $apiCode = "<script type='text/javascript'>$apiCode</script>"; | |
| 453 | + | |
| 454 | + $widgetCode .= $apiCode; | |
| 455 | + | |
| 456 | + | |
| 457 | + //check if user is logged if yes get username, email | |
| 458 | + if ( is_user_logged_in() ) { | |
| 459 | + | |
| 460 | + global $current_user; | |
| 461 | + get_currentuserinfo(); | |
| 462 | + | |
| 463 | + // get user name | |
| 464 | + $onwebchat_user = $current_user->user_login; | |
| 465 | + | |
| 466 | + // get user email | |
| 467 | + $onwebchat_email = $current_user->user_email; | |
| 468 | + | |
| 469 | + //add api info | |
| 470 | + if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") { | |
| 471 | + if ($onwebchat_email != null && $onwebchat_email != ""){ | |
| 472 | + $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user'); | |
| 473 | + onWebChat.set('email','$onwebchat_email'); </script>"; | |
| 474 | + } else { | |
| 475 | + $widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');</script>"; | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + | |
| 480 | + $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") ); | |
| 481 | + $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) ); | |
| 482 | + $currentPage .= "://" . $_SERVER['SERVER_NAME']; | |
| 483 | + $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] ); | |
| 484 | + $currentPage .= $_SERVER['REQUEST_URI']; | |
| 485 | + | |
| 486 | + $pagesSelect = get_option('onwebchat_plugin_option_pages_select'); | |
| 487 | + $showPages = get_option('onwebchat_plugin_option_show_pages'); | |
| 488 | + $showPages = explode(' ', $showPages); | |
| 489 | + $hidePages = get_option('onwebchat_plugin_option_hide_pages'); | |
| 490 | + $hidePages = explode(' ', $hidePages); | |
| 491 | + | |
| 492 | + $showSelect=true; | |
| 493 | + if ($pagesSelect==1){ | |
| 494 | + | |
| 495 | + } else if ($pagesSelect==2){ | |
| 496 | + $showSelect=false; | |
| 497 | + foreach ($showPages as $page){ | |
| 498 | + if ($page !== "") { | |
| 499 | + if (strpos($currentPage, $page) !== false) { | |
| 500 | + $showSelect = true; | |
| 501 | + } | |
| 502 | + } | |
| 503 | + } | |
| 504 | + } else if ($pagesSelect==3){ | |
| 505 | + $showSelect=true; | |
| 506 | + foreach ($hidePages as $page){ | |
| 507 | + if ($page !== "") { | |
| 508 | + if (strpos($currentPage, $page) !== false) { | |
| 509 | + $showSelect = false; | |
| 510 | + } | |
| 511 | + } | |
| 512 | + } | |
| 513 | + } else if($pagesSelect == 4) { | |
| 514 | + //TODO we hide chat widget from all pages | |
| 515 | + $hideWidget = true; | |
| 516 | + } | |
| 517 | + | |
| 518 | + if(!$hideWidget && $showSelect) { | |
| 519 | + echo $widgetCode; | |
| 520 | + } | |
| 521 | + | |
| 522 | +} | |
| 523 | + | |
| 524 | +/************************************************************************ | |
| 525 | + * display error function | |
| 526 | + ***********************************************************************/ | |
| 527 | +function onwebchat_login_error($contition = false) { | |
| 528 | + if($contition) { | |
| 529 | + ?> | |
| 530 | + <div class="error"> | |
| 531 | + <p><strong>Wrong credentials!</strong> Please enter correct <u>email and password</u> <strong>OR</strong> a valid <u>Chat Id</u>.</p> | |
| 532 | + </div> | |
| 533 | + <?php | |
| 534 | + } | |
| 535 | + else { | |
| 536 | + //display nothing | |
| 537 | + } | |
| 538 | +} | |
| 539 | + | |
| 540 | +/************************************************************************ | |
| 541 | + * Validate Chat Id function | |
| 542 | + ***********************************************************************/ | |
| 543 | +function isChatIdValid($input) { | |
| 544 | + return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input); | |
| 545 | +} | |
| 546 | + | |
| 547 | +?> | |