Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dainis Tillers committed Nov 22, 2014
0 parents commit 438d675
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tawk/tawk-whmcs
================

# About tawk.to
tawk.to is a free live chat app that lets you monitor and chat with visitors on your website
or from a free customizable page. No catch. No spam. No wares. It's truly free and always will be.

# Installation
Copy files modules folder to your whmcs installation folder. After that you will have to activate
it by going to Setup -> Addon modules (admin/configaddonmods.php) and finding and activating
Tawk.to widget module. Make sure that you configure access to this module so that you have access to
it.

# Usage
Go to Addons -> Tawk.to widget and select widget you want to use on whmcs client side.

If you don't have [tawk.to](https://tawk.to/?utm_source=whmcs&utm_medium=link&utm_campaign=signup) account, you can always [create one for free](https://tawk.to/?utm_source=whmcs&utm_medium=link&utm_campaign=signup)
21 changes: 21 additions & 0 deletions modules/addons/tawk_to_widget/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @author Tawk.to
* @copyright Copyright (c) Tawk.to 2014
* @license http://www.whmcs.com/license/ WHMCS Eula
* @link https://tawk.to
*/

if (!defined("WHMCS"))
die("This file cannot be accessed directly");

function tawk_to_widget_retrieve_widget() {
$result = select_query('tawk_to_widget_settings', 'page_id, widget_id', array('id' => 1));
$data = mysql_fetch_array($result);

if(!$data || !isset($data['page_id']) || !isset($data['widget_id'])) {
return array('page_id' => '', 'widget_id' => '');
}

return $data;
}
36 changes: 36 additions & 0 deletions modules/addons/tawk_to_widget/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @author Tawk.to
* @copyright Copyright (c) Tawk.to 2014
* @license http://www.whmcs.com/license/ WHMCS Eula
* @link https://tawk.to
*/

if (!defined("WHMCS"))
die("This file cannot be accessed directly");

require_once dirname(__FILE__) . '/functions.php';

function tawk_to_widget_embed_code_output($vars) {
$widget = tawk_to_widget_retrieve_widget();

if($widget['page_id'] === '' || $widget['widget_id'] === '') {
return;
}

return '<!--Start of Tawk.to Script-->
<script type="text/javascript">
var $_Tawk_API={},$_Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src="https://embed.tawk.to/'.$widget['page_id'].'/'.$page['widget_id'].'";
s1.charset="UTF-8";
s1.setAttribute("crossorigin","*");
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->';
}

add_hook("ClientAreaFooterOutput", 1, "tawk_to_widget_embed_code_output");
1 change: 1 addition & 0 deletions modules/addons/tawk_to_widget/lang/english.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
74 changes: 74 additions & 0 deletions modules/addons/tawk_to_widget/tawk_to_widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @author Tawk.to
* @copyright Copyright (c) Tawk.to 2014
* @license http://www.whmcs.com/license/ WHMCS Eula
* @link https://tawk.to
*/

if (!defined("WHMCS"))
die("This file cannot be accessed directly");

require_once dirname(__FILE__) . '/functions.php';

define('TAWK_TO_WIDGET_PLUGINS_BASE_URL', 'https://plugins.tawk.to');

function tawk_to_widget_config() {
return array(
"name" => "Tawk to widget",
"description" => "This addon allows to change tawk.to widget which is used in your whmcs installation",
"version" => "1.0",
"author" => "Tawkto",
"language" => "english"
);
}

function tawk_to_widget_activate() {

full_query("CREATE TABLE IF NOT EXISTS `tawk_to_widget_settings` (
`id` int NOT NULL PRIMARY KEY,
`page_id` varchar(100) NOT NULL,
`widget_id` varchar(100) NOT NULL
) DEFAULT CHARSET=utf8;");

return array('status'=>'success', 'description'=>'Tawk.to widget addon enabled, make sure to enable user access and then go to Addons => Tawk.to widget and choose widget you want to use');
}

function tawk_to_widget_deactivate() {
full_query('drop table `tawk_to_widget_settings`');
return array('status'=>'success', 'description'=>'Tawk.to widget addon dactived. Tawk.to widget will not be displayed');
}

function tawk_to_widget_output($vars) {

if(isset($_GET['ajax'])) {
header('Content-Type: application/json');

if(isset($_POST['action']) && $_POST['action'] === 'set' && isset($_POST['page_id']) && isset($_POST['widget_id'])) {
$page_id = mysql_real_escape_string($_POST['page_id']);
$widget_id = mysql_real_escape_string($_POST['widget_id']);

full_query("insert into tawk_to_widget_settings
(id, page_id, widget_id)
values(1, '" . $page_id . "', '" . $widget_id . "')
on duplicate key update page_id='".$page_id."', widget_id='".$widget_id."'");

echo json_encode(array('success' => TRUE));
} else if(isset($_POST['action']) && $_POST['action'] === 'remove') {
full_query('delete from `tawk_to_widget_settings` where id=1');
echo json_encode(array('success' => TRUE));
} else {
echo json_encode(array('success' => FALSE));
}

die();
}

$widget = tawk_to_widget_retrieve_widget();

$iframe_url = TAWK_TO_WIDGET_PLUGINS_BASE_URL.'/generic/widgets?currentWidgetId='.$widget['widget_id'].'&currentPageId='.$widget['page_id'];
$base_url = TAWK_TO_WIDGET_PLUGINS_BASE_URL;
$ajax_link = $vars['modulelink'] . '&ajax=1';

require dirname(__FILE__) . '/templates/widget_choose_iframe.php';
}
82 changes: 82 additions & 0 deletions modules/addons/tawk_to_widget/templates/widget_choose_iframe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @author Tawk.to
* @copyright Copyright (c) Tawk.to 2014
* @license http://www.whmcs.com/license/ WHMCS Eula
* @link https://tawk.to
*/

if (!defined("WHMCS"))
die("This file cannot be accessed directly");
?>

<iframe
id="tawkIframe"
src=""
style="min-height: 400px; width : 100%; border: none; margin-top: 20px">
</iframe>

<script type="text/javascript">
var currentHost = window.location.protocol + "//" + window.location.host,
url = "<?php echo $iframe_url?>&parentDomain=" + currentHost,
baseUrl = '<?php echo $base_url ?>',
current_id_tab = '{$tab_id}',
controller = '<?php echo $ajax_link ?>';

jQuery('#tawkIframe').attr('src', url);

var iframe = jQuery('#tawk_widget_customization')[0];

window.addEventListener('message', function(e) {

if(e.origin === baseUrl) {

if(e.data.action === 'setWidget') {
setWidget(e);
}

if(e.data.action === 'removeWidget') {
removeWidget(e);
}
}
});

function setWidget(e) {

$.ajax({
type : 'POST',
url : controller,
dataType : 'json',
data : {
action : 'set',
page_id : e.data.pageId,
widget_id : e.data.widgetId
},
success : function(r) {
if(r.success) {
e.source.postMessage({action: 'setDone'} , baseUrl);
} else {
e.source.postMessage({action: 'setFail'} , baseUrl);
}
}
});
}

function removeWidget(e) {
$.ajax({
type : 'POST',
url : controller,
dataType : 'json',
data : {
action : 'remove',
},
success : function(r) {
if(r.success) {
e.source.postMessage({action: 'removeDone'} , baseUrl);
} else {
e.source.postMessage({action: 'removeFail'} , baseUrl);
}
}
});
}
</script>

0 comments on commit 438d675

Please sign in to comment.