-
Notifications
You must be signed in to change notification settings - Fork 2
/
tawk_to.module
219 lines (181 loc) · 5.83 KB
/
tawk_to.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* @file
* @package tawk.to module for Backdrop CMS
* @copyright (C) 2021 tawk.to
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
require_once 'tawk_to.admin.inc';
require_once 'includes/tawk_to.helper.php';
/**
* Implements hook_preprocess_page().
* Ensures widget appears on all defined pages
*/
function tawk_to_preprocess_page(&$variables) {
global $user;
$base_url = TawkHelper::get_base_url();
// backend checking; do not show chat widget on admin page(s)
if (path_is_admin(current_path())) {
return;
}
$widget = TawkHelper::get_widget();
if ($widget['page_id'] === '' || $widget['widget_id'] === '') {
return null;
}
$page_id = $widget['page_id'];
$widget_id = $widget['widget_id'];
// get visibility options
$options = config_get(
TawkHelper::TAWK_TO_CONFIG_NAME,
TawkHelper::TAWK_TO_WIDGET_OPTIONS
);
// initial markup options
$enable_visitor_recognition = true; // default value
if ($options) {
$options = json_decode($options);
if (isset($options->enable_visitor_recognition)) {
$enable_visitor_recognition = $options->enable_visitor_recognition;
}
// prepare visibility
$currentUrl = $base_url.$_SERVER['REQUEST_URI'];
if (false==$options->always_display) {
$showPages = json_decode($options->show_oncustom);
$show = false;
foreach ($showPages as $slug) {
if ($currentUrl == $base_url.$slug) {
$show = true;
break;
}
}
// taxonomy term pages
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && arg(3) == '') {
if (false != $options->show_ontaxonomy) {
$show = true;
}
}
// front page
if (backdrop_is_front_page()) {
if (false != $options->show_onfrontpage) {
$show = true;
}
}
if (!$show) {
return;
}
}
else {
if (isset($options->hide_oncustom)) {
$hide_pages = json_decode($options->hide_oncustom);
$show = true;
$currentUrl = (string) $currentUrl;
foreach ($hide_pages as $slug) {
// we need to add htmlspecialchars due to slashes added when saving to database
$slug = (string) htmlspecialchars($slug);
if ($currentUrl == $base_url.$slug) {
$show = false;
break;
}
}
if (!$show) {
return;
}
}
}
}
$widget_options = array(
'pageId' => $page_id,
'widgetId' => $widget_id,
'enableVisitorRecognition' => $enable_visitor_recognition,
);
if ($user->uid && $enable_visitor_recognition) {
$widget_options['user'] = array(
'name' => $user->name,
'email' => $user->mail,
);
}
backdrop_add_js(TawkHelper::get_module_path() . '/js/tawk_to.widget.js', 'file');
backdrop_add_js(array(
'tawkto' => $widget_options,
), 'setting');
return $variables;
}
/**
* Hooks up configuration menu items and paths for ajax call endpoints.
*/
function tawk_to_menu() {
$items = array();
$items['admin/config/tawk'] = array(
'title' => 'tawk.to',
'description' => 'tawk.to configuration',
'position' => 'left',
'weight' => -100,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
$items['admin/config/tawk/widget'] = array(
'title' => 'Widget',
'description' => 'Included tawk.to widget configuration',
'page callback' => 'backdrop_get_form',
'page arguments' => array('tawk_to_admin_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/tawk/setwidget'] = array(
'title' => 'Set widget',
'page callback' => 'tawk_to_set_widget',
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items['admin/config/tawk/removewidget'] = array(
'title' => 'Remove widget',
'page callback' => 'tawk_to_remove_widget',
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Ajax endpoint which is used to set selected widget details.
*/
function tawk_to_set_widget() {
if (!isset($_POST['pageId']) || !isset($_POST['widgetId'])) {
return backdrop_json_output(array('success' => false));
}
if (preg_match('/^[0-9A-Fa-f]{24}$/', $_POST['pageId']) !== 1 || preg_match('/^[a-z0-9]{1,50}$/i', $_POST['widgetId']) !== 1) {
return backdrop_json_output(array('success' => false));
}
$config_name = TawkHelper::TAWK_TO_CONFIG_NAME;
config_set($config_name, TawkHelper::TAWK_TO_PAGE_ID, $_POST['pageId']);
config_set($config_name, TawkHelper::TAWK_TO_WIDGET_ID, $_POST['widgetId']);
global $user;
if ($user->uid) {
config_set($config_name, TawkHelper::TAWK_TO_WIDGET_USER_ID, $user->uid);
}
// Flush page cache so widget will appear at next page load
cache_flush('page');
backdrop_json_output(array('success' => true));
}
/**
* Ajax endpoint which is used to remove currently selected widget details.
*/
function tawk_to_remove_widget() {
$config = config(TawkHelper::TAWK_TO_CONFIG_NAME);
$config->clear(TawkHelper::TAWK_TO_PAGE_ID);
$config->clear(TawkHelper::TAWK_TO_WIDGET_ID);
$config->save();
// Flush page cache so widget will disappear at next page load
cache_flush('page');
backdrop_json_output(array('success' => true));
}
/**
* Implements hook_config_info().
*/
function tawk_to_config_info() {
$prefixes[TawkHelper::TAWK_TO_CONFIG_NAME] = array(
'label' => t('tawk.to'),
'group' => t('Configuration'),
);
return $prefixes;
}