Single sign-on example for WordPress

In this example we'll show you how can you implement simple SSO for your admins in Wordpress. As result, they will be able to access LiveAgent admin panel directly from their admin menu.
 
Goal:
Adminstarators and editors from WordPress can go to LiveAgent panel without logging in through log-in dialog.
 
Requirements:
- Administrators in WordPress must have the same e-mail address as their username in LiveAgent
 
So here is the code of very basic WordPress plugin handling SSO for all admins and editors:
 
<?php
/*
Plugin Name: LiveAgent example 1
Plugin URI: https://www.qualityunit.com/liveagent
Description: Example 1 - How to use Liveagent REST API
Author: QualityUnit
Version: 1.0.0
Author URI: https://www.qualityunit.com
License: GPL2
*/


if (!class_exists('liveAgentExampleOne')) {
  class 
liveAgentExampleOne
  
{
    
//LiveAgent url
    
const LIVEAGENT_URL 'https://mysupport.exmple.com/';
    
    
//LiveAgent API key - read more about how to get it: https://support.qualityunit.com/741982-API-key
    
const API_KEY '4a46eb8373c39a8d70d496a94b53fe89';
    
    
//WORDPRESS STUFF
    
private function currentUserHasRole($roleName)
    {
      
$current_user wp_get_current_user();
      return 
in_array($roleName$current_user->roles);
    }
    
    public function 
__construct( )
    {
      
add_action('admin_menu', array(
        
$this,
        
'addPrimaryConfigMenu'
      
));
    }
    
    public function 
addPrimaryConfigMenu( )
    {
      
//we will add one extra menu item labeled LiveAgent
      
add_menu_page(__('LiveAgent'), __('LiveAgent'), 'manage_categories''liveagentPanelLogin', array(
        
$this,
        
'printPanelLoginMenu'
      
));
    }
    
    
//LIVEAGENT STUFF
    
private function getAgentInfo($agentEmail)
    {
      
//we will ask LiveAgent for info about agent with email $agentEmail
      
$ch curl_init(self::LIVEAGENT_URL 'api/agents/' urlencode($agentEmail) '&apikey=' self::API_KEY);
      
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
      
$rawResult curl_exec($ch);
      
//very simple answer checking
      
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;
    }
    
    public function 
printPanelLoginMenu( )
    {
      
$current_user wp_get_current_user();
      
$agentInfo    $this->getAgentInfo($current_user->user_email);
      if ((
$this->currentUserHasRole('administrator') || $this->currentUserHasRole('editor')) && $agentInfo != '') {
        
//if I am an admin or editor -> allow me to jump directly to LiveAgent panel without logging in through login dialog
        
echo 'You can jump directly to LiveAgent <a href="' self::LIVEAGENT_URL 'agent/?AuthToken=' $agentInfo->authtoken .
        
'" target="_blank">here</a>';
        return;
      } else if ((
$this->currentUserHasRole('administrator') || $this->currentUserHasRole('editor')) && $agentInfo == null) {
        
//if I am an admin or editor, but something happes and i'm not able to get my info, allow me to log in manually
        echo 'You can login to LiveAgent <a href="' self::LIVEAGENT_URL 'agent" target="_blank">here</a>';
        return;
      }
      
//if I am not an admin nor an editor then do not allow me to even see how to get to LiveAgnet admin panel
      
echo 'You do not have access to LiveAgent.';
    }
  }
}


$liveagent = new liveAgentExampleOne();
?>
 
Explanation
The code is more or less self-explanatory, but let's focus on the LiveAgent API part:
 
First, we will create a curl object, and ask LiveAgent for info about agent with email $agentEmail. We will ask about specific agent entity from all agents in LiveAgent: .../agents/specific_agent@email. 
Note: Do not forget to attach apikey to your request.
$ch curl_init(self::LIVEAGENT_URL 'api/agents/' urlencode($agentEmail) '?apikey=' self::API_KEY);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$rawResult curl_exec($ch);
 
If we have proper (JSON) result, then decode it and check correct answer:
$result json_decode($rawResult); //if you want to use XML answer format, use SimpleXMLElement object in php to parse raw result
if (!isset($result->response)) {
    return null;
}
if (isset($result->response) && isset($result->response->statuscode) && $result->response->statuscode != 0) {
    return null;
}
return $result->response;
 
You can find out more about this call (complete syntax, mandatory and optional fields and example results in XML and JSON format) in our API reference.
 
Note: If you want the answer in XML, then you must add .xml to the end of URL address, before apikey
$ch curl_init(self::LIVEAGENT_URL 'api/agents/' urlencode($agentEmail) '.xml&apikey=' self::API_KEY);
 
And use SimpleXMLElement PHP object to decode it
×