| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: onWebChat Live Chat |
| 4 |
Plugin URI: https://www.onwebchat.com/wp_plugin.php |
| 5 |
Description: onWebChat is a live chat system, that helps you communicate with your website's visitors. |
| 6 |
Author: onWebChat |
| 7 |
Version: 2.0.2 |
| 8 |
Author URI: https://www.onwebchat.com |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* ---------------------------------------------------- * |
| 13 |
* Create Settings Page |
| 14 |
* ---------------------------------------------------- */ |
| 15 |
|
| 16 |
// get logo url |
| 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 |
// add styles |
| 21 |
add_action( 'admin_enqueue_scripts', 'register_plugin_styles' ); |
| 22 |
|
| 23 |
// call function on hook admin_menu |
| 24 |
add_action('admin_menu','onwebchat_setup_menu'); |
| 25 |
|
| 26 |
// create login error notice |
| 27 |
add_action( 'admin_notices', 'onwebchat_login_error' ); |
| 28 |
|
| 29 |
// define the settings |
| 30 |
add_action('admin_init','onwebchat_register_setttings'); |
| 31 |
|
| 32 |
|
| 33 |
/* ---------------------------------------------------- * |
| 34 |
* Display onWebChat on footer |
| 35 |
* ---------------------------------------------------- */ |
| 36 |
|
| 37 |
// add widget plugin to the footer |
| 38 |
add_action('wp_footer', 'onwebchat_add_script_at_footer'); |
| 39 |
|
| 40 |
|
| 41 |
/* ----------------------------------------------------- * |
| 42 |
* Admin Page registration |
| 43 |
* ----------------------------------------------------- */ |
| 44 |
|
| 45 |
function onwebchat_setup_menu() { |
| 46 |
add_menu_page( 'onWebChat Settings', 'onWebChat', 'administrator', 'onwebchat_settings', 'onwebchat_init_menu_page', ONWEBCHAT_SMALL_LOGO); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/* ------------------------------------------------------ * |
| 51 |
* Admin Page Display Function |
| 52 |
* ------------------------------------------------------ */ |
| 53 |
|
| 54 |
function onwebchat_init_menu_page() { |
| 55 |
|
| 56 |
$options = get_option('onwebchat_plugin_option'); |
| 57 |
$chatId = $options['text_string']; |
| 58 |
|
| 59 |
$isConnected = false; |
| 60 |
if($chatId !="") { |
| 61 |
$isConnected = true; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/***************************************************************** |
| 66 |
* Ask server for chatId |
| 67 |
*****************************************************************/ |
| 68 |
if ( isset( $_POST["action"] ) && $_POST["action"] == "login" ) { |
| 69 |
|
| 70 |
//retrive again chatId ******* |
| 71 |
$options = get_option('onwebchat_plugin_option'); |
| 72 |
$chatId = $options['text_string']; |
| 73 |
//**************************** |
| 74 |
|
| 75 |
// get username, password and chatId |
| 76 |
$userName = $_POST["onWebChatUser"]; |
| 77 |
$userPass = $_POST["onWebChatPass"]; |
| 78 |
$chatId = $_POST["chatId"]; |
| 79 |
$isSecondPage = $_POST["isSecondPage"]; |
| 80 |
|
| 81 |
// if user give chatId save it directly |
| 82 |
|
| 83 |
if(isChatIdValid($chatId)) { |
| 84 |
|
| 85 |
//----------- update chatId option ------------ |
| 86 |
//Get entire array |
| 87 |
$my_options = get_option('onwebchat_plugin_option'); |
| 88 |
|
| 89 |
//Alter the options array appropriately |
| 90 |
$my_options['text_string'] = $chatId; |
| 91 |
|
| 92 |
//Update entire array |
| 93 |
update_option('onwebchat_plugin_option', $my_options); |
| 94 |
//----------------------------------------------- |
| 95 |
|
| 96 |
// remove user email from db |
| 97 |
if(!$isSecondPage) |
| 98 |
update_option( 'onwebchat_plugin_option_user', ''); |
| 99 |
|
| 100 |
// move to next page |
| 101 |
print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); |
| 102 |
} |
| 103 |
|
| 104 |
// else ask server |
| 105 |
else { |
| 106 |
|
| 107 |
// save username at options |
| 108 |
update_option( 'onwebchat_plugin_option_user', $userName ); |
| 109 |
|
| 110 |
// ask server for chatId |
| 111 |
$response = wp_remote_post(ONWEBCHAT_SERVER_URL, array( |
| 112 |
'method' => 'POST', |
| 113 |
'timeout' => 45, |
| 114 |
'redirection' => 5, |
| 115 |
'httpversion' => '1.0', |
| 116 |
'blocking' => true, |
| 117 |
'headers' => array(), |
| 118 |
'body' => array( 'email' => $userName, 'pass' => $userPass ), |
| 119 |
'cookies' => array() |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
if ( is_wp_error( $response ) ) { |
| 124 |
$error_message = $response->get_error_message(); |
| 125 |
} else { |
| 126 |
|
| 127 |
// If no chatId returned |
| 128 |
if($response['body'] == '-1') { |
| 129 |
// display an error for wrong username or password |
| 130 |
onwebchat_login_error(true); |
| 131 |
} |
| 132 |
|
| 133 |
// If we have the chatId |
| 134 |
else { |
| 135 |
//----------- update chatId option ------------ |
| 136 |
//Get entire array |
| 137 |
$my_options = get_option('onwebchat_plugin_option'); |
| 138 |
|
| 139 |
//Alter the options array appropriately |
| 140 |
$my_options['text_string'] = $response['body']; |
| 141 |
|
| 142 |
//Update entire array |
| 143 |
update_option('onwebchat_plugin_option', $my_options); |
| 144 |
//-------------------------------------------- |
| 145 |
|
| 146 |
// move to next page |
| 147 |
print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// save hideCheckBox |
| 153 |
update_option('onwebchat_plugin_option_hide', $_POST['onwebchat_plugin_option_hide']); |
| 154 |
} |
| 155 |
//******************************************************************** |
| 156 |
|
| 157 |
|
| 158 |
/********************************************************************* |
| 159 |
* Disconnect account |
| 160 |
*********************************************************************/ |
| 161 |
if( isset($_GET["action"]) && $_GET["action"] == "deactivate" && $isConnected) { |
| 162 |
$isConnected = false; |
| 163 |
|
| 164 |
//Get entire array |
| 165 |
$my_options = get_option('onwebchat_plugin_option'); |
| 166 |
|
| 167 |
//Alter the options array appropriately |
| 168 |
$my_options['text_string'] = ''; |
| 169 |
|
| 170 |
//Update entire array |
| 171 |
update_option('onwebchat_plugin_option', $my_options); |
| 172 |
|
| 173 |
$chatIdOption = get_option('onwebchat_plugin_option'); |
| 174 |
$chatId = $chatIdOption['text_string']; |
| 175 |
|
| 176 |
//redirects |
| 177 |
print('<script>window.location.href="admin.php?page=onwebchat_settings"</script>'); |
| 178 |
|
| 179 |
} |
| 180 |
//*********************************************************************** |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
?> |
| 185 |
<div> |
| 186 |
<h2>onWebChat <?php ($isConnected == true) ? print 'Settings' : print 'Activation'; ?> </h2> |
| 187 |
</br> |
| 188 |
<form action="admin.php?page=onwebchat_settings" method="post"> |
| 189 |
<input type="hidden" name="action" value="login"> |
| 190 |
<?php |
| 191 |
|
| 192 |
// Login Page (1st page) |
| 193 |
if($isConnected != true) { |
| 194 |
|
| 195 |
//get chatId from db |
| 196 |
$chatId = get_option( 'onwebchat_plugin_option' ); |
| 197 |
$chatId = $chatId['text_string']; |
| 198 |
?> |
| 199 |
|
| 200 |
<h3 class="header-1">Connect with your onWebChat account</h3> |
| 201 |
|
| 202 |
<div class="username-div"> |
| 203 |
<strong>Email: </strong><input class="username-text-field" type="text" name="onWebChatUser" value="<?php echo get_option( 'onwebchat_plugin_option_user' ); ?>"/> |
| 204 |
</div> |
| 205 |
<div class="password-div"> |
| 206 |
<strong>Password: </strong><input class="password-text-field" type="password" name="onWebChatPass" value="<?php echo get_option( 'onWebChatPass' ); ?>"/> |
| 207 |
</div> |
| 208 |
<h3 class="header-or">OR</h3> |
| 209 |
<hr class="small-hr"> |
| 210 |
<h3 class="header-2">Paste your onWebChat Chat Id</h3> |
| 211 |
<div class="chatid-div"> |
| 212 |
<strong>Chat Id:</strong> <input class="chatid-text-field" type="text" name="chatId" value="<?php echo $chatId; ?>"/> |
| 213 |
</div> |
| 214 |
|
| 215 |
<div class="new-account-link"> |
| 216 |
<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> |
| 217 |
</div> |
| 218 |
<?php |
| 219 |
} |
| 220 |
|
| 221 |
// Deactivate Page (2nd page) |
| 222 |
else { |
| 223 |
$options = get_option('onwebchat_plugin_option_user'); |
| 224 |
|
| 225 |
// display user email as account identification |
| 226 |
if($options!=''){ |
| 227 |
$html = '<h3 class="header-1-p2">Activated for onWebChat account: </h3>'; |
| 228 |
$html .= "<strong class='account-id'>$options</strong> "; |
| 229 |
} |
| 230 |
|
| 231 |
// display chatId as account identification |
| 232 |
else { |
| 233 |
$chatId = get_option( 'onwebchat_plugin_option' ); |
| 234 |
$chatId = $chatId['text_string']; |
| 235 |
$html = '<h3 class="header-1-p2">Activated for onWebChat Chat Id: </h3>'; |
| 236 |
$html .= "<strong class='account-id'>$chatId</strong> "; |
| 237 |
} |
| 238 |
$html .= ' <a href="admin.php?page=onwebchat_settings&action=deactivate">Deactivate</a>'; |
| 239 |
$html .= ' <div class="admin-login-info"> <span>*</span> To connect on onWebChat operator console click <a target="_blank" href="https://www.onwebchat.com/login.php">here</a> </div>'; |
| 240 |
echo $html; |
| 241 |
|
| 242 |
$checkBoxValue = checked(1, get_option('onwebchat_plugin_option_hide'), false); |
| 243 |
|
| 244 |
$options = get_option('onwebchat_plugin_option_hide'); |
| 245 |
|
| 246 |
//get chatId from db |
| 247 |
$chatId = get_option( 'onwebchat_plugin_option' ); |
| 248 |
$chatId = $chatId['text_string']; |
| 249 |
|
| 250 |
?> |
| 251 |
<div class="hide-div"> |
| 252 |
<strong>Hide Chat Widget:</strong> <input type="checkbox" id="plugin_text_checkbox" name="onwebchat_plugin_option_hide" value="1" <?php checked( $options, 1 ); ?> /> |
| 253 |
</div> |
| 254 |
|
| 255 |
<!-- hiden fields --> |
| 256 |
<input class="chatid-text-field-hide" type="text" name="chatId" value="<?php echo $chatId; ?>"/> |
| 257 |
<input class="chatid-text-field-hide" type="text" name="onWebChatUser" value="<?php echo get_option( 'onwebchat_plugin_option_user' ); ?>"/> |
| 258 |
<input class="chatid-text-field-hide" type="text" name="isSecondPage" value="1"/> |
| 259 |
<?php |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
// Display the Save Button |
| 264 |
$html = '<input class="button button-primary" type="submit" value="Save Changes"/>'; |
| 265 |
echo $html; |
| 266 |
?> |
| 267 |
</form> |
| 268 |
</div> |
| 269 |
<?php |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/* ----------------------------------------------------- * |
| 274 |
* Admin Page Styles Registration |
| 275 |
* ----------------------------------------------------- */ |
| 276 |
|
| 277 |
function register_plugin_styles() { |
| 278 |
wp_register_style( 'onwebchat', plugins_url( 'onwebchat/css/onwebchat.css' ) ); |
| 279 |
wp_enqueue_style( 'onwebchat' ); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/* ----------------------------------------------------- * |
| 284 |
* Tabs Settings Registration |
| 285 |
* ----------------------------------------------------- */ |
| 286 |
|
| 287 |
function onwebchat_register_setttings() { |
| 288 |
|
| 289 |
/******************** Account Tab ************************/ |
| 290 |
//* register the Chat Id |
| 291 |
register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option'); |
| 292 |
|
| 293 |
//* register the username |
| 294 |
register_setting( 'onwebchat_plugin_option', 'onwebchat_plugin_option_user'); |
| 295 |
|
| 296 |
//* register the hide (checkbox) |
| 297 |
register_setting( 'onwebchat_plugin_option_checkbox', 'onwebchat_plugin_option_hide'); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/* ----------------------------------------------------- * |
| 302 |
* onWebChat Display Function |
| 303 |
* ----------------------------------------------------- */ |
| 304 |
|
| 305 |
// print chat widget code |
| 306 |
function onwebchat_add_script_at_footer() { |
| 307 |
|
| 308 |
$options = get_option('onwebchat_plugin_option'); |
| 309 |
$chatId = $options['text_string']; |
| 310 |
|
| 311 |
$hideWidget = get_option('onwebchat_plugin_option_hide'); |
| 312 |
|
| 313 |
$widgetCode = "<script type='text/javascript'> |
| 314 |
var onWebChat={ar:[], set: function(a,b){if (typeof onWebChat_==='undefined'){this.ar. |
| 315 |
push([a,b]);}else{onWebChat_.set(a,b);}},get:function(a){return(onWebChat_.get(a));},w |
| 316 |
:(function(){ var ga=document.createElement('script'); ga.type = 'text/javascript';ga. |
| 317 |
async=1;ga.src='//www.onwebchat.com/clientchat/$chatId'; |
| 318 |
var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})()} |
| 319 |
</script>"; |
| 320 |
|
| 321 |
$apiCode = get_option('onwebchat_plugin_option_api'); |
| 322 |
|
| 323 |
$widgetCode .= $apiCode; |
| 324 |
|
| 325 |
|
| 326 |
//check if user is logged if yes get username, email (all users registered have them) |
| 327 |
if ( is_user_logged_in() ) { |
| 328 |
|
| 329 |
global $current_user; |
| 330 |
get_currentuserinfo(); |
| 331 |
|
| 332 |
// get user name |
| 333 |
$onwebchat_user = $current_user->user_login; |
| 334 |
|
| 335 |
// get user email |
| 336 |
$onwebchat_email = $current_user->user_email; |
| 337 |
|
| 338 |
//add api info |
| 339 |
$widgetCode = $widgetCode."<script type='text/javascript'>onWebChat.set('name','$onwebchat_user');onWebChat.set('email','$onwebchat_email'); </script>"; |
| 340 |
} |
| 341 |
|
| 342 |
if(!$hideWidget) { |
| 343 |
echo $widgetCode; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
/************************************************************************ |
| 348 |
* display error function |
| 349 |
***********************************************************************/ |
| 350 |
function onwebchat_login_error($contition = false) { |
| 351 |
if($contition) { |
| 352 |
?> |
| 353 |
<div class="error"> |
| 354 |
<!--<p>Wrong Username or Password!</p>--> |
| 355 |
<p><strong>Wrong credentials!</strong> Please enter correct <u>email and password</u> <strong>OR</strong> a valid <u>Chat Id</u>.</p> |
| 356 |
</div> |
| 357 |
<?php |
| 358 |
} |
| 359 |
else { |
| 360 |
//display nothing |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/************************************************************************ |
| 365 |
* Validate Chat Id function |
| 366 |
***********************************************************************/ |
| 367 |
function isChatIdValid($input) { |
| 368 |
return preg_match('/^[a-f0-9]{32,40}+[\\/]?+[0-9]{0,2}+[\\/]?+[0-9]{0,2}$/i', $input); |
| 369 |
} |
| 370 |
|
| 371 |
?> |
| 372 |
|