onWebChat, for AI training). require_once __DIR__ . '/includes/woocommerce-sync.php'; // Live order-status lookup (onWebChat -> plugin, during a chat). require_once __DIR__ . '/includes/woocommerce-orders.php'; } }); // add styles add_action( 'admin_enqueue_scripts', 'register_plugin_styles' ); // call function on hook admin_menu add_action('admin_menu','onwebchat_setup_menu'); // create login error notice add_action( 'admin_notices', 'onwebchat_login_error' ); // define the settings add_action('admin_init','onwebchat_register_setttings'); // handle form submissions early (before any output) add_action('admin_init', 'onwebchat_handle_all_form_submissions'); /* ---------------------------------------------------- * * Display onWebChat on footer * ---------------------------------------------------- */ // add widget plugin to the footer add_action('wp_footer', 'onwebchat_add_script_at_footer'); /* ----------------------------------------------------- * * Admin Page registration * ----------------------------------------------------- */ function onwebchat_setup_menu() { add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO); } /* ------------------------------------------------------ * * Admin Page Display Function * ------------------------------------------------------ */ function onwebchat_init_menu_page() { // Load the new tabbed settings page require_once __DIR__ . '/admin/settings-page.php'; onwebchat_settings_page(); } /* ----------------------------------------------------- * * Admin Page Styles Registration * ----------------------------------------------------- */ function register_plugin_styles() { wp_register_style( 'onwebchat', plugins_url( 'onwebchat/css/onwebchat.css' ) ); wp_enqueue_style( 'onwebchat' ); } /* ----------------------------------------------------- * * Tabs Settings Registration * ----------------------------------------------------- */ function onwebchat_register_setttings() { /******************** Account Tab ************************/ //* register the Chat Id register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option'); //* register the username register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_user'); //* register the hide (checkbox) register_setting( 'onwebchat_plugin_option_checkbox', 'onwebchat_plugin_option_hide'); //TODO api code //* register the api code register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_api_code'); /******************** WooCommerce Sync Settings ************************/ register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_enabled'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_mode'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_include_price'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_secret'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_last_bulk_sync'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_excluded_categories'); register_setting( 'onwebchat_wc_sync', 'onwebchat_wc_sync_categories'); } /* ----------------------------------------------------- * * onWebChat Display Function * ----------------------------------------------------- */ // print chat widget code function onwebchat_add_script_at_footer() { $options = get_option('onwebchat_plugin_option'); $chatId = (is_array($options) && isset($options['text_string'])) ? $options['text_string'] : ''; //$hideWidget = get_option('onwebchat_plugin_option_hide'); $hideWidget = false; // If no chat ID is configured, don't output widget code if (empty($chatId)) { return; } // Merchant option: show the chat widget only to signed-in users (default: off). if (get_option('onwebchat_plugin_option_logged_only', 0) && !is_user_logged_in()) { return; } $widgetCode = ""; $apiCode = get_option('onwebchat_plugin_option_api_code'); $apiCode = str_replace('\\','',$apiCode); $apiCode = ""; $widgetCode .= $apiCode; // Identity PROOF for the logged-in email: HMAC_SHA256(shared sync secret, lowercase(email)). // The onWebChat server recomputes it with the same secret; only when it matches does the AI treat // the email as verified and allow the email-only "my recent orders" lookup. A visitor faking // onWebChat.set('email') from the console cannot produce this hash, so they can never list another // customer's orders. The '0' sentinel means "not signed in": it tells onWebChat to DROP any stored // proof, so on a shared computer nobody inherits the previous customer's identity after a logout. $onwebchat_email_hash = '0'; $onwebchat_wc_secret = get_option('onwebchat_wc_sync_secret'); //check if user is logged if yes get username, email if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); // get user name (escaped for safe use inside the inline script) $onwebchat_user = esc_js( $current_user->user_login ); // get user email (escaped for safe use inside the inline script) $onwebchat_email = esc_js( $current_user->user_email ); //add api info if ($onwebchat_user != null && $onwebchat_user != "" && $onwebchat_user != "guest" && $onwebchat_user != "Guest") { if ($onwebchat_email != null && $onwebchat_email != ""){ // Hash the raw (unescaped) email so both sides hash identical bytes. if (!empty($onwebchat_wc_secret)) { $onwebchat_email_hash = hash_hmac('sha256', strtolower(trim($current_user->user_email)), $onwebchat_wc_secret); } $widgetCode = $widgetCode.""; } else { $widgetCode = $widgetCode.""; } } } if (!empty($onwebchat_wc_secret)) { $widgetCode .= ""; } $currentPage = substr( strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos( strtolower($_SERVER['SERVER_PROTOCOL']), "/") ); $currentPage .= ( empty($_SERVER['HTTPS']) ? NULL : ( ($_SERVER['HTTPS'] == "on") ? "s" : NULL) ); $currentPage .= "://" . $_SERVER['SERVER_NAME']; $currentPage .= ( $_SERVER['SERVER_PORT'] == 80 ? "" : ":".$_SERVER['SERVER_PORT'] ); $currentPage .= $_SERVER['REQUEST_URI']; $pagesSelect = get_option('onwebchat_plugin_option_pages_select'); $showPages = get_option('onwebchat_plugin_option_show_pages'); $showPages = explode(' ', $showPages); $hidePages = get_option('onwebchat_plugin_option_hide_pages'); $hidePages = explode(' ', $hidePages); $showSelect=true; if ($pagesSelect==1){ } else if ($pagesSelect==2){ $showSelect=false; foreach ($showPages as $page){ if ($page !== "") { if (strpos($currentPage, $page) !== false) { $showSelect = true; } } } } else if ($pagesSelect==3){ $showSelect=true; foreach ($hidePages as $page){ if ($page !== "") { if (strpos($currentPage, $page) !== false) { $showSelect = false; } } } } else if($pagesSelect == 4) { //hide chat widget from all pages $hideWidget = true; } if(!$hideWidget && $showSelect) { echo $widgetCode; } } /************************************************************************ * display error function ***********************************************************************/ function onwebchat_login_error($contition = false) { if($contition) { ?>

Invalid credentials! Please make sure you have entered the correct email and password, OR provide a valid Chat ID

15, 'sslverify' => !ONWEBCHAT_WC_TESTING_MODE, 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode(array( 'email' => $email, 'password' => $password, 'site_key' => $key, 'version' => ONWEBCHAT_PLUGIN_VERSION, )), )); if (is_wp_error($response)) { // Silently fail - user can still connect manually in WooCommerce tab error_log('onWebChat: Failed to fetch WC secret on login: ' . $response->get_error_message()); return; } $status_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); $body = json_decode($response_body, true); if ($status_code >= 200 && $status_code < 300 && isset($body['success']) && $body['success']) { // Success! Store the secret update_option('onwebchat_wc_sync_secret', $body['secret']); error_log('onWebChat: WooCommerce sync connected automatically during login'); // Enable AI order-status lookup by default on connect and register our // callback URL with onWebChat (best effort; the merchant can toggle it off). update_option('onwebchat_wc_order_lookup_enabled', true); global $onwebchat_wc_orders; if (isset($onwebchat_wc_orders) && is_object($onwebchat_wc_orders)) { $onwebchat_wc_orders->push_order_lookup_config(true); } } // If it fails, user can still connect manually - no error shown } /************************************************************************ * Handle all form submissions early to avoid blank page on redirect * Must run on admin_init before any output is sent ***********************************************************************/ function onwebchat_handle_all_form_submissions() { // Only run on our settings page if (!isset($_GET['page']) || $_GET['page'] !== 'onwebchat_settings') { return; } // Load the general tab handlers require_once __DIR__ . '/admin/tabs/general.php'; onwebchat_handle_general_actions(); } // omit the closing php tag ? > for avoid issues