| 1 |
<?php |
| 2 |
/** |
| 3 |
* General Tab - Account Connection |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
function onwebchat_general_tab($isConnected = false) { |
| 11 |
|
| 12 |
// Note: Form submissions are handled in admin_init hook (see onwebchat.php) |
| 13 |
// This ensures redirects happen before any WordPress admin output |
| 14 |
|
| 15 |
$options = get_option('onwebchat_plugin_option'); |
| 16 |
$chatId = isset($options['text_string']) ? $options['text_string'] : ''; |
| 17 |
|
| 18 |
?> |
| 19 |
|
| 20 |
<?php if (!$isConnected): ?> |
| 21 |
<!-- Not Connected - Show Login Form --> |
| 22 |
<h2>Connect Your Account</h2> |
| 23 |
<p>Link your WordPress site to your onWebChat account to enable the live chat widget and AI features.</p> |
| 24 |
|
| 25 |
<form action="admin.php?page=onwebchat_settings&tab=general" method="post"> |
| 26 |
<input type="hidden" name="action" value="login"> |
| 27 |
<?php wp_nonce_field('on_web_chat_nonce'); ?> |
| 28 |
|
| 29 |
<!-- Option 1: Email/Password Login --> |
| 30 |
<div class="card" style="max-width: 600px; margin: 20px 0; padding: 20px; background: #fff; border: 1px solid #c3c4c7; box-shadow: 0 1px 1px rgba(0,0,0,.04);"> |
| 31 |
<h3 style="margin-top: 0; display: flex; align-items: center; gap: 8px;"> |
| 32 |
<span style="background: #2271b1; color: #fff; width: 24px; height: 24px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 14px;">1</span> |
| 33 |
Login with Email & Password |
| 34 |
</h3> |
| 35 |
<p style="color: #646970; margin-bottom: 15px;"> |
| 36 |
Recommended — enables automatic WooCommerce sync connection. |
| 37 |
</p> |
| 38 |
|
| 39 |
<table class="form-table" style="margin: 0;"> |
| 40 |
<tr> |
| 41 |
<th scope="row" style="width: 100px; padding: 10px 10px 10px 0;"> |
| 42 |
<label for="onWebChatUser">Email</label> |
| 43 |
</th> |
| 44 |
<td style="padding: 10px 0;"> |
| 45 |
<input type="email" |
| 46 |
id="onWebChatUser" |
| 47 |
name="onWebChatUser" |
| 48 |
class="regular-text" |
| 49 |
placeholder="your@email.com" |
| 50 |
value="<?php echo esc_attr(get_option('onwebchat_plugin_option_user')); ?>"> |
| 51 |
</td> |
| 52 |
</tr> |
| 53 |
<tr> |
| 54 |
<th scope="row" style="width: 100px; padding: 10px 10px 10px 0;"> |
| 55 |
<label for="onWebChatPass">Password</label> |
| 56 |
</th> |
| 57 |
<td style="padding: 10px 0;"> |
| 58 |
<input type="password" |
| 59 |
id="onWebChatPass" |
| 60 |
name="onWebChatPass" |
| 61 |
class="regular-text" |
| 62 |
placeholder="Your onWebChat password"> |
| 63 |
</td> |
| 64 |
</tr> |
| 65 |
</table> |
| 66 |
</div> |
| 67 |
|
| 68 |
<!-- Divider --> |
| 69 |
<div style="max-width: 600px; display: flex; align-items: center; margin: 25px 0;"> |
| 70 |
<div style="flex: 1; height: 1px; background: #c3c4c7;"></div> |
| 71 |
<span style="padding: 0 20px; color: #646970; font-size: 14px; font-weight: 500;">OR</span> |
| 72 |
<div style="flex: 1; height: 1px; background: #c3c4c7;"></div> |
| 73 |
</div> |
| 74 |
|
| 75 |
<!-- Option 2: Chat ID --> |
| 76 |
<div class="card" style="max-width: 600px; margin: 20px 0; padding: 20px; background: #fff; border: 1px solid #c3c4c7; box-shadow: 0 1px 1px rgba(0,0,0,.04);"> |
| 77 |
<h3 style="margin-top: 0; display: flex; align-items: center; gap: 8px;"> |
| 78 |
<span style="background: #646970; color: #fff; width: 24px; height: 24px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: 14px;">2</span> |
| 79 |
Use Chat ID Only |
| 80 |
</h3> |
| 81 |
<p style="color: #646970; margin-bottom: 15px;"> |
| 82 |
Quick setup — paste your Chat ID from your onWebChat dashboard. |
| 83 |
</p> |
| 84 |
|
| 85 |
<table class="form-table" style="margin: 0;"> |
| 86 |
<tr> |
| 87 |
<th scope="row" style="width: 100px; padding: 10px 10px 10px 0;"> |
| 88 |
<label for="chatId">Chat ID</label> |
| 89 |
</th> |
| 90 |
<td style="padding: 10px 0;"> |
| 91 |
<input type="text" |
| 92 |
id="chatId" |
| 93 |
name="chatId" |
| 94 |
class="regular-text" |
| 95 |
placeholder="e.g. 3a02c87b60726a4663b25463a424a248" |
| 96 |
value="<?php echo esc_attr($chatId); ?>"> |
| 97 |
<p class="description">Found in your onWebChat Dashboard → Settings → Installation</p> |
| 98 |
</td> |
| 99 |
</tr> |
| 100 |
</table> |
| 101 |
</div> |
| 102 |
|
| 103 |
<!-- Submit Button --> |
| 104 |
<p style="margin-top: 20px;"> |
| 105 |
<button type="submit" class="button button-primary button-hero"> |
| 106 |
Connect Account |
| 107 |
</button> |
| 108 |
</p> |
| 109 |
|
| 110 |
<!-- New Account Link --> |
| 111 |
<p style="margin-top: 15px; color: #646970;"> |
| 112 |
Don't have an onWebChat account yet? |
| 113 |
<a href="https://www.onwebchat.com/signup.php" target="_blank">Create one here</a> to get started. |
| 114 |
</p> |
| 115 |
</form> |
| 116 |
|
| 117 |
<?php else: ?> |
| 118 |
<!-- Connected Status --> |
| 119 |
<h2>Account Connected</h2> |
| 120 |
|
| 121 |
<p style="color: #646970; margin: 10px 0 20px 0;"> |
| 122 |
<?php |
| 123 |
$userOption = get_option('onwebchat_plugin_option_user'); |
| 124 |
if (!empty($userOption)) { |
| 125 |
echo '<span style="color: #2271b1;">✓</span> <strong>Connected as:</strong> ' . esc_html($userOption); |
| 126 |
} else { |
| 127 |
$chatIdOption = get_option('onwebchat_plugin_option'); |
| 128 |
$displayChatId = isset($chatIdOption['text_string']) ? $chatIdOption['text_string'] : ''; |
| 129 |
echo '<span style="color: #2271b1;">✓</span> <strong>Connected with Chat ID:</strong> <code>' . esc_html($displayChatId) . '</code>'; |
| 130 |
} |
| 131 |
?> |
| 132 |
</p> |
| 133 |
|
| 134 |
<div class="card" style="max-width: 600px; margin: 20px 0; padding: 20px; background: #fff; border: 1px solid #c3c4c7; box-shadow: 0 1px 1px rgba(0,0,0,.04);"> |
| 135 |
<h3 style="margin-top: 0;">Quick Actions</h3> |
| 136 |
|
| 137 |
<p> |
| 138 |
<a href="https://www.onwebchat.com/login.php" target="_blank" class="button button-secondary"> |
| 139 |
Open onWebChat Dashboard → |
| 140 |
</a> |
| 141 |
</p> |
| 142 |
|
| 143 |
<hr style="margin: 20px 0; border: none; border-top: 1px solid #c3c4c7;"> |
| 144 |
|
| 145 |
<p style="margin-bottom: 0;"> |
| 146 |
<a href="admin.php?page=onwebchat_settings&tab=general&action=deactivate" |
| 147 |
style="color: #b32d2e; text-decoration: none;" |
| 148 |
onclick="return confirm('Are you sure you want to unlink your account? The chat widget will be removed from your site.');"> |
| 149 |
Unlink Account |
| 150 |
</a> |
| 151 |
<span style="color: #646970; margin-left: 5px;">— Disconnect this WordPress site from onWebChat</span> |
| 152 |
</p> |
| 153 |
</div> |
| 154 |
|
| 155 |
<?php endif; ?> |
| 156 |
<?php |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Handle form submissions for general tab |
| 161 |
*/ |
| 162 |
function onwebchat_handle_general_actions() { |
| 163 |
|
| 164 |
// Handle login/connection |
| 165 |
if (isset($_POST["action"]) && $_POST["action"] == "login") { |
| 166 |
|
| 167 |
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'on_web_chat_nonce')) { |
| 168 |
wp_die('Sorry, your nonce did not verify.'); |
| 169 |
} |
| 170 |
|
| 171 |
$userName = isset($_POST["onWebChatUser"]) ? sanitize_text_field($_POST["onWebChatUser"]) : ''; |
| 172 |
$userPass = isset($_POST["onWebChatPass"]) ? sanitize_text_field($_POST["onWebChatPass"]) : ''; |
| 173 |
$chatId = isset($_POST["chatId"]) ? sanitize_text_field($_POST["chatId"]) : ''; |
| 174 |
|
| 175 |
if (isChatIdValid($chatId)) { |
| 176 |
// Direct Chat ID provided |
| 177 |
$my_options = get_option('onwebchat_plugin_option'); |
| 178 |
$my_options['text_string'] = $chatId; |
| 179 |
update_option('onwebchat_plugin_option', $my_options); |
| 180 |
update_option('onwebchat_plugin_option_user', ''); |
| 181 |
update_option('onwebchat_plugin_option_pages_select', 1); |
| 182 |
|
| 183 |
wp_redirect(admin_url('admin.php?page=onwebchat_settings&tab=general')); |
| 184 |
exit; |
| 185 |
|
| 186 |
} else { |
| 187 |
// Try to get Chat ID from server |
| 188 |
update_option('onwebchat_plugin_option_user', $userName); |
| 189 |
|
| 190 |
$response = wp_remote_post(ONWEBCHAT_SERVER_URL, array( |
| 191 |
'method' => 'POST', |
| 192 |
'timeout' => 45, |
| 193 |
'body' => array('email' => $userName, 'pass' => $userPass), |
| 194 |
)); |
| 195 |
|
| 196 |
if (!is_wp_error($response) && !empty($response['body'])) { |
| 197 |
$my_options = get_option('onwebchat_plugin_option'); |
| 198 |
$my_options['text_string'] = $response['body']; |
| 199 |
update_option('onwebchat_plugin_option', $my_options); |
| 200 |
update_option('onwebchat_plugin_option_pages_select', 1); |
| 201 |
|
| 202 |
// Also fetch WooCommerce secret with same credentials |
| 203 |
// This way user doesn't need to re-enter credentials in WooCommerce tab |
| 204 |
onwebchat_fetch_wc_secret_on_login($userName, $userPass, $response['body']); |
| 205 |
|
| 206 |
wp_redirect(admin_url('admin.php?page=onwebchat_settings&tab=general')); |
| 207 |
exit; |
| 208 |
} else { |
| 209 |
onwebchat_login_error(true); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// Handle disconnect |
| 215 |
if (isset($_GET["action"]) && $_GET["action"] == "deactivate") { |
| 216 |
$my_options = get_option('onwebchat_plugin_option'); |
| 217 |
$my_options['text_string'] = ''; |
| 218 |
update_option('onwebchat_plugin_option', $my_options); |
| 219 |
update_option('onwebchat_plugin_option_pages_select', 1); |
| 220 |
|
| 221 |
wp_redirect(admin_url('admin.php?page=onwebchat_settings&tab=general')); |
| 222 |
exit; |
| 223 |
} |
| 224 |
} |
| 225 |
|