- API v1 Key
/*
Plugin Name: Custom LiveAgent example
Plugin URI: https://www.qualityunit.com/liveagent
Description: Example - How to authenticate subscribers from Wordpress into LiveAgent (on the same domain) as registered visitors
Author: QualityUnit
Version: 1.0.0
Author URI: https://www.qualityunit.com
License: GPL2
*/
if (!class_exists('liveAgentExampleThree')) {
class liveAgentExampleThree
{
const LIVEAGENT_URL = 'https://mysupport.exmple.com/';
const API_KEY = '2afeca3e74c8c8ff60fed782053ec44a'; // API v1
//we will use login/logout wordpress hooks
public function __construct()
{
add_action('wp_login', array(
$this,
'handleLogin'
));
add_action('wp_logout', array(
$this,
'handleLogout'
));
add_action('user_register', array(
$this,
'handleSignup'
));
}
public function handleLogin($login)
{
$user = get_user_by('login', $login);
if ($user == null) {
return;
}
if (!in_array('subscriber', $user->roles)) {
// in case I'm not a subscriber - end
return;
}
$laUser = $this->getVisitorInfo($user->get('user_email'));
if ($laUser == null) {
return;
}
/* if I retrive valid response from LiveAgent, I can register "remember me" cookie.
* Wordpress and LiveAgent are on the same domain so one cookie is enough
*/
setcookie($laUser->browsercookiename, $laUser->authtoken, time() + 60 * 60 * 24 * 356, '/');
}
public function handleSignup($user_id)
{
//let's load newly created user
$user = new WP_User($user_id);
// fill all basic fields - you can find more in complete API reference here: https://support.qualityunit.com/840770-Complete-API-reference
$data = array(
'role' => 'R',
'name' => $user->user_firstname . ' ' . $user->user_lastname,
'email' => $user->user_email,
'apikey' => self::API_KEY
);
//we will create POST call to customers entity
$ch = curl_init(self::LIVEAGENT_URL . 'api/customers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
//here we can handle error result
}
public function handleLogout($user_id)
{
$user = new WP_User($user_id);
if ($user == null) {
return;
}
if (!in_array('subscriber', $user->roles)) {
return;
}
$laUser = $this->getVisitorInfo($user->get('user_email'));
if ($laUser == null) {
return;
}
//on logout we must destrou our "remember me" cookie and also our session cookie from LiveAgent.
setcookie($laUser->browsercookiename, '', time() - 3600, '/');
setcookie('visitor_la_sid', '', time() - 3600, '/');
}
private function getVisitorInfo($visitorEmail)
{
//here we use entity customers
$ch = curl_init(self::LIVEAGENT_URL . 'api/customers/' . urlencode($visitorEmail) . '&apikey=' . self::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
if (!$rawResult) {
return null;
}
$result = json_decode($rawResult);
if (!isset($result->response)) {
return null;
}
if (isset($result->response) && isset($result->response->statuscode) && $result->response->statuscode != 0) {
return null;
}
return $result->response;
}
}
}
$liveagent = new liveAgentExampleThree();
?>
My LiveAgent and my WordPress are on different domains. What then?
- API v1 Key
- SSO API key
- LiveAgent version 5.24 and above
/*
Plugin Name: LiveAgent example Two
Plugin URI: https://www.qualityunit.com/liveagent
Description: Example 2 - How to authenticate subscriber from Wordpress into LiveAgent customer portal (on different domains) as registered visitors
Author: QualityUnit
Version: 1.0.3
Author URI: https://www.qualityunit.com
License: GPL2
*/
if (!class_exists('liveAgentExampleTwo')) {
class liveAgentExampleTwo
{
const LIVEAGENT_URL = 'https://mysupport.exmple.com/';
const API_KEY = '4a46eb8373c39a8d70d496a94b53fe89'; // API v1
const SSO_KEY = 'a48c6a5d8d70d496a94b5d2a2cfe2a4a';
//processing of the auth - we need to run JavaScript code
public function processAuth()
{
/* we will use LiveAgent standard tracking code
* note: if you already have LiveAgent button on your page, you won't need this peace of code. Just make sure
* that rest of it is above your button code (so trackjs.php can fully load)
*/
echo '<script type="text/javascript" id="la_x2s6df8d" src="'.self::LIVEAGENT_URL.'scripts/track.js"></script>';
$actions = get_option('la_actions');
if ($actions == null) {
return;
}
//now bit magic - we process all actions in queue.
foreach ($actions as $action) {
//if we have logout action here then proceed
if ($action['action'] == 'logout') {
$user = get_user_by('login', $action['user']);
continue;
}
$result = $this->logoutVisitor($user);
if ($result === true) {
$this->clearAction('logout', $action['user']);
break;
}
}
if ($action['action'] == 'login') {
//if we have login action here, lets be sure its really me
$user = wp_get_current_user();
if ($action['user'] != $user->get('user_login')) {
continue;
}
$result = $this->loginVisitor($user);
if ($result === true) {
$this->clearAction('login', $user->get('user_login'));
break;
}
}
}
}
//clear actions queue
private function clearAction($actionName, $login)
{
$actions = get_option('la_actions');
$clearedActions = array();
foreach ($actions as $key => $action) {
if ($action['user'] = !$login && $action['user'] != '' && $action['action'] == $actionName) {
$clearedActions[] = $actions[$key];
}
}
update_option('la_actions', $clearedActions);
}
private function createMagicHash($userEmail, $userAuthToken)
{
return md5($userEmail . $userAuthToken . self::SSO_KEY);
}
//logout JavaScript
private function logoutVisitor($user)
{
return;
}
//load visitor info
$visitorInfo = $this->getVisitorInfo($user->get('user_email'));
//create magic hash
$hash = $this->createMagicHash($user->get('user_email'), $visitorInfo->authtoken);
//call logout function from our tracking JavaScript
echo '<script type="text/javascript">
LiveAgentTracker.logoutUserOnServer(\'' . $hash . '\');
</script>';
return true;
}
//login JavaScript
private function loginVisitor($user)
{
//load visitor info
$visitorInfo = $this->getVisitorInfo($user->get('user_email'));
//create magic hash
$hash = $this->createMagicHash($user->get('user_email'), $visitorInfo->authtoken);
//call login function from our tracking JavaScript
echo '<script type="text/javascript">
LiveAgentTracker.loginUserOnServer(\'' . $hash . '\',\'kb\');
</script>';
return true;
}
//add new action to action queue
private function addAction($actionName, $login)
{
$actions = get_option('la_actions');
if ($actions == null) {
$actions = array();
}
$actions[] = array(
'action' => $actionName,
'user' => $login
);
update_option('la_actions', $actions);
}
//WP login hook
public function handleLogin($login)
{
$user = get_user_by('login', $login);
if ($user == null) {
return;
}
if (!in_array($user->roles[0], self::ALLOWED_ROLES)) {
return;
}
$this->addAction('login', $login);
}
//WP logout hook
public function handleLogout($user_id)
{
$user = new WP_User($user_id);
$this->addAction('logout', $user->get('user_login'));
}
//API call for getting visitor info
private function getVisitorInfo($visitorEmail)
{
//here we use entity customers
$ch = curl_init(self::LIVEAGENT_URL . 'api/?handler=customers/' . urlencode($visitorEmail) . '&apikey=' . self::API_KEY);
$rawResult = curl_exec($ch);
if (!$rawResult) {
return null;
}
$result = json_decode($rawResult);
if (!isset($result->response)) {
return null;
}
if (isset($result->response) && isset($result->response->statuscode) && $result->response->statuscode != 0) {
return null;
}
return $result->response;
}
/*
* WordPress stuff
*/
public function settingsInit()
{
register_setting('la_general', 'la_actions');
}
public function __construct()
{
add_action('admin_init', array(
$this,
'settingsInit'
));
add_action('wp_login', array(
$this,
'handleLogin'
));
add_action('wp_logout', array(
$this,
'handleLogout'
));
add_action('wp_footer', array(
$this,
'processAuth'
), 99);
add_filter('admin_footer_text', array(
$this,
'processAuth'
), 99);
add_filter('logout_url', array(
$this,
'logoutUrl'
));
}
//we need to redirect used after logout right to main page
public function logoutUrl($logout_url)
{
$redir = get_option('siteurl');
return $logout_url . '&redirect_to=' . urlencode($redir);
}
}
}
$liveagent = new liveAgentExampleTwo();
?>