In this example we create a plugin that shows which agents are in specific department, and what is their actual status. You can see result on screenshot bellow:
And we'll be able to configure which department we want to show in backend:
Goal: We want to be able to show agents and their statuses in any department. And we want to see this in WordPress sidebar widget.
Requirements: none
Plugin code:
<?php
/*
Plugin Name: LiveAgent example 4
Plugin URI: https://www.qualityunit.com/liveagent
Description: Example 4 - How to show agent statuses in department
Author: QualityUnit
Version: 1.0.0
Author URI: https://www.qualityunit.com
License: GPL2
*/
if (!class_exists('liveAgent_widgetDepartmentAgents')) {
class liveAgent_widgetDepartmentAgents extends WP_Widget
{
const DEPARTMENTID = 'departmentid';
//wordpress stuff
function __construct()
{
parent::__construct('deaprtments_agents', $name = 'Department agents', array(
'description' => 'Shows statuses all agents in department'
));
}
function widget($args, $instance)
{
extract($args);
echo $before_widget;
$this->renderContent($instance);
echo $after_widget;
}
function update($new_instance, $old_instance)
{
return $new_instance;
$instance = array();
$instance[self::DEPARTMENTID] = $new_instance[self::DEPARTMENTID];
return $instance;
}
//LiveAgent stuff
private function getResponseOrNull($rawResult)
{
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;
}
//API call: returns agents in depaertment and their statuses
private function getDepartmentsAgents($departmentId)
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments/' . $departmentId . '/agents?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result->agents;
}
return null;
}
//API call: returns department info - used to obtain department name
private function getDepartmentInfo($departmentId)
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments/' . $departmentId . '?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result;
}
return null;
}
private function getOnlineStatus($code)
{
$codes = str_split($code);
if ($code == 'F') {
return 'Offline';
}
$status = '';
foreach ($codes as $onlineCode) {
switch ($onlineCode) {
case 'M':
$status .= ',Mails';
break;
case 'P':
$status .= ',Phone';
break;
case 'T':
$status .= ',Chat';
break;
}
}
return substr($status, 1);
}
//API call: returns all departments from LiveAgent - include deleted ones
private function getDepartmentsList()
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result->departments;
}
return null;
}
//widget frontend form
protected function renderContent($instance)
{
$agents = $this->getDepartmentsAgents($instance[self::DEPARTMENTID]);
if ($agents == null) {
return;
}
$departmentInfo = $this->getDepartmentInfo($instance[self::DEPARTMENTID]);
if ($departmentInfo == null) {
return;
}
echo '<div>';
echo '<h3 class="widget-title">Department: <b>' . $departmentInfo->name . '</b></h3>';
echo '<lu>';
foreach ($agents as $agent) {
echo '<li><b>' . $agent->firstname . ' ' . $agent->lastname . '</b>: ' . $this->getOnlineStatus($agent->onlinestatus) . '</li>';
}
echo '</lu>';
echo '</div>';
}
//widget backend form
function form($instance)
{
$departmentid = @$instance[self::DEPARTMENTID];
//load all departments fom LiveAgent
$list = $this->getDepartmentsList();
if ($list == null) {
echo '<p>
<label>Unable to obtain departments list from your LiveAgent.</label>
</p>';
return;
}
echo '
<p>
<label for="departmentid">Select department:</label>
<select class="widefat" id="' . $this->get_field_id('departmentid') . '" name="' . $this->get_field_name('departmentid') . '">';
foreach ($list as $department) {
//skip and do not show already deleted departments
if ($department->deleted == 'Y') {
continue;
}
$selected = '';
if ($departmentid == $department->departmentid) {
$selected = 'selected';
}
echo '<option value="' . $department->departmentid . '" ' . $selected . '>' . $department->name . '</option>';
}
echo '</select>
</p>';
}
}
}
if (!class_exists('liveAgentExampleFour')) {
class liveAgentExampleFour
{
const LIVEAGENT_URL = 'https://mysupport.exmple.com/';
const API_KEY = '2afeca3e74c8c8ff60fed782053ec44a';
//wordpress stuff - we use separate calss for widget
public function __construct()
{
add_action('widgets_init', create_function('', 'return register_widget("liveAgent_widgetDepartmentAgents");'));
}
}
}
$liveagent = new liveAgentExampleFour();
?>
/*
Plugin Name: LiveAgent example 4
Plugin URI: https://www.qualityunit.com/liveagent
Description: Example 4 - How to show agent statuses in department
Author: QualityUnit
Version: 1.0.0
Author URI: https://www.qualityunit.com
License: GPL2
*/
if (!class_exists('liveAgent_widgetDepartmentAgents')) {
class liveAgent_widgetDepartmentAgents extends WP_Widget
{
const DEPARTMENTID = 'departmentid';
//wordpress stuff
function __construct()
{
parent::__construct('deaprtments_agents', $name = 'Department agents', array(
'description' => 'Shows statuses all agents in department'
));
}
function widget($args, $instance)
{
extract($args);
echo $before_widget;
$this->renderContent($instance);
echo $after_widget;
}
function update($new_instance, $old_instance)
{
return $new_instance;
$instance = array();
$instance[self::DEPARTMENTID] = $new_instance[self::DEPARTMENTID];
return $instance;
}
//LiveAgent stuff
private function getResponseOrNull($rawResult)
{
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;
}
//API call: returns agents in depaertment and their statuses
private function getDepartmentsAgents($departmentId)
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments/' . $departmentId . '/agents?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result->agents;
}
return null;
}
//API call: returns department info - used to obtain department name
private function getDepartmentInfo($departmentId)
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments/' . $departmentId . '?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result;
}
return null;
}
private function getOnlineStatus($code)
{
$codes = str_split($code);
if ($code == 'F') {
return 'Offline';
}
$status = '';
foreach ($codes as $onlineCode) {
switch ($onlineCode) {
case 'M':
$status .= ',Mails';
break;
case 'P':
$status .= ',Phone';
break;
case 'T':
$status .= ',Chat';
break;
}
}
return substr($status, 1);
}
//API call: returns all departments from LiveAgent - include deleted ones
private function getDepartmentsList()
{
$ch = curl_init(liveAgentExampleFour::LIVEAGENT_URL . 'api/departments?apikey=' . liveAgentExampleFour::API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rawResult = curl_exec($ch);
$result = $this->getResponseOrNull($rawResult);
if ($result !== null) {
return $result->departments;
}
return null;
}
//widget frontend form
protected function renderContent($instance)
{
$agents = $this->getDepartmentsAgents($instance[self::DEPARTMENTID]);
if ($agents == null) {
return;
}
$departmentInfo = $this->getDepartmentInfo($instance[self::DEPARTMENTID]);
if ($departmentInfo == null) {
return;
}
echo '<div>';
echo '<h3 class="widget-title">Department: <b>' . $departmentInfo->name . '</b></h3>';
echo '<lu>';
foreach ($agents as $agent) {
echo '<li><b>' . $agent->firstname . ' ' . $agent->lastname . '</b>: ' . $this->getOnlineStatus($agent->onlinestatus) . '</li>';
}
echo '</lu>';
echo '</div>';
}
//widget backend form
function form($instance)
{
$departmentid = @$instance[self::DEPARTMENTID];
//load all departments fom LiveAgent
$list = $this->getDepartmentsList();
if ($list == null) {
echo '<p>
<label>Unable to obtain departments list from your LiveAgent.</label>
</p>';
return;
}
echo '
<p>
<label for="departmentid">Select department:</label>
<select class="widefat" id="' . $this->get_field_id('departmentid') . '" name="' . $this->get_field_name('departmentid') . '">';
foreach ($list as $department) {
//skip and do not show already deleted departments
if ($department->deleted == 'Y') {
continue;
}
$selected = '';
if ($departmentid == $department->departmentid) {
$selected = 'selected';
}
echo '<option value="' . $department->departmentid . '" ' . $selected . '>' . $department->name . '</option>';
}
echo '</select>
</p>';
}
}
}
if (!class_exists('liveAgentExampleFour')) {
class liveAgentExampleFour
{
const LIVEAGENT_URL = 'https://mysupport.exmple.com/';
const API_KEY = '2afeca3e74c8c8ff60fed782053ec44a';
//wordpress stuff - we use separate calss for widget
public function __construct()
{
add_action('widgets_init', create_function('', 'return register_widget("liveAgent_widgetDepartmentAgents");'));
}
}
}
$liveagent = new liveAgentExampleFour();
?>
Explanation
In this example we will use three API calls in these methods: getDepartmentsAgents, getDepartmentInfo, getDepartmentsList
Names are pretty clear and you can find out more about them in our API reference.
Basically we obtain department list from LiveAgent and put it to widget back-end form. We skip deleted departments. User will pick one department and then save the widget.
In front-end we receive department info to show its name and then list of all agents involved in this department and their statuses.
WordPress Widget API can be found here: https://codex.wordpress.org/Widgets_API