Skip to content

Commit 7d63f4a

Browse files
authored
Create hooks.php
1 parent 5029f1e commit 7d63f4a

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

chatwoot/hooks.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/***************************************************************************
4+
// * *
5+
// * Chatwoot WHMCS Addon (v1.0). *
6+
// * This addon modules enables you integrate Chatwoot with your WHMCS *
7+
// and leverage its powerful features. *
8+
// * Tested on WHMCS Version: 7.9.2 (7.9.2-release.1). *
9+
// * For assistance on how to use and setup Chatwoot, visit *
10+
// https://www.chatwoot.com/docs/channels/website *
11+
// * *
12+
// *************************************************************************
13+
// * *
14+
// * Contributed by: WevrLabs Hosting *
15+
// * Email: hello@wevrlabs.net *
16+
// * Website: https://wevrlabs.net *
17+
// * *
18+
// *************************************************************************/
19+
20+
if(!defined("WHMCS")) {
21+
die("This file cannot be accessed directly");
22+
}
23+
24+
use WHMCS\Database\Capsule;
25+
26+
function hook_chatwoot_footer_output($vars) {
27+
28+
$chatwoot_jscode = Capsule::table('tbladdonmodules')->where('module', 'chatwoot')->where('setting', 'chatwoot_jscode')->value('value');
29+
$chatwoot_position = Capsule::table('tbladdonmodules')->where('module', 'chatwoot')->where('setting', 'chatwoot_position')->value('value');
30+
$chatwoot_setlabel = Capsule::table('tbladdonmodules')->where('module', 'chatwoot')->where('setting', 'chatwoot_setlabel')->value('value');
31+
$chatwoot_setlabelloggedin = Capsule::table('tbladdonmodules')->where('module', 'chatwoot')->where('setting', 'chatwoot_setlabelloggedin')->value('value');
32+
$isenabled = Capsule::table('tbladdonmodules')->select('value')-> WHERE('module', '=' , 'chatwoot')->WHERE('setting' , '=', 'chatwoot_enable')->WHERE('value' , 'on')->count();
33+
34+
// Disable or Enable Chatwoot
35+
if (empty($isenabled)) {
36+
return;
37+
}
38+
39+
if(!$chatwoot_jscode) {
40+
return;
41+
}
42+
43+
44+
// Fetch labels
45+
$client = Menu::context('client');
46+
if (!is_null($client)){
47+
$chatwoot_label = $chatwoot_setlabelloggedin;
48+
} else {
49+
$chatwoot_label = $chatwoot_setlabel;
50+
}
51+
52+
// Get client ID
53+
if ($vars['clientsdetails']['id']) {
54+
$hmac = hash_hmac("sha256", $id, "nQ1ayoG5bu580LZkSxMJiO2");
55+
$clientid = $hmac;
56+
57+
}
58+
59+
// Get client email
60+
if ($vars['clientsdetails']['email']) {
61+
$clientemail = $vars['clientsdetails']['email'];
62+
}
63+
64+
// Get First and Last name
65+
if ($vars['clientsdetails']['firstname']) {
66+
$clientname = $vars['clientsdetails']['firstname'] . " " . $vars['clientsdetails']['lastname'];
67+
}
68+
69+
// Fetch client avatar if any
70+
$rating = (isset($params['rating']) ? $params['rating'] : 'G');
71+
$default = (isset($params['default']) ? $params['default'] : 'mp');
72+
$size = (isset($params['size']) ? $params['size'] : '150');
73+
$gravatarurl = "https://www.gravatar.com/avatar/".md5($clientemail) . "?r=".$rating . "&d=".$default . "&s=".$size;
74+
75+
76+
$chatwoot_output = "$chatwoot_jscode
77+
<script>
78+
window.onload = (event) => {
79+
window.\$chatwoot.setUser('$clientid', {
80+
email: '$clientemail',
81+
name: '$clientname',
82+
avatar_url: '$gravatarurl',
83+
})
84+
window.\$chatwoot.setLabel('$chatwoot_label')
85+
}
86+
window.chatwootSettings = {
87+
position: '$chatwoot_position',
88+
}
89+
</script>
90+
";
91+
92+
echo $chatwoot_output;
93+
}
94+
95+
96+
function hook_chatwoot_logout_footer_output($vars) {
97+
$chatwoot_logoutJS = "<script>
98+
document.addEventListener('readystatechange', event => {
99+
window.\$chatwoot.reset()
100+
});
101+
</script>
102+
";
103+
104+
echo $chatwoot_logoutJS;
105+
}
106+
107+
add_hook('ClientAreaFooterOutput', 1, 'hook_chatwoot_footer_output');
108+
109+
add_hook('ClientLogout', 1, 'hook_chatwoot_logout_footer_output');

0 commit comments

Comments
 (0)