forked from tawk/tawk-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tawk_to.module
195 lines (157 loc) · 5.25 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
<?php
/**
* @file
* @package tawk.to module for Drupal
* @copyright (C) 2021 tawk.to
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
require_once __DIR__ . '/tawk_to.admin.inc';
require_once __DIR__ . '/includes/tawk_to.helper.php';
require_once __DIR__ . '/vendor/autoload.php';
use Tawk\Modules\UrlPatternMatcher;
/**
* Implements hook_preprocess_page().
* Ensures widget appears on all defined pages
*/
function tawk_to_preprocess_page(&$variables) {
global $user;
global $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 = variable_get(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
$current_url = $base_url.$_SERVER['REQUEST_URI'];
if (false === $options->always_display) {
$show_pages = json_decode($options->show_oncustom);
$show = false;
if (UrlPatternMatcher::match($current_url, $show_pages)) {
$show = true;
}
if (arg(0) === 'taxonomy' && arg(1) === 'term' && is_numeric(arg(2)) && empty(arg(3))) {
if (true === $options->show_oncategory) {
$show = true;
}
}
// front page
if (drupal_is_front_page()) {
if (true === $options->show_onfrontpage) {
$show = true;
}
}
if (!$show) {
return;
}
} else {
if (isset($options->hide_oncustom)) {
$hide_pages = json_decode($options->hide_oncustom);
$show = true;
$current_url = (string) $current_url;
if (UrlPatternMatcher::match($current_url, $hide_pages)) {
$show = false;
}
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,
);
}
drupal_add_js(array(
'tawkto' => $widget_options,
), 'setting');
drupal_add_js(TawkHelper::get_module_path() . '/js/tawk_to.widget.js', 'file');
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' => drupal_get_path('module', 'system'),
);
$items['admin/config/tawk/widget'] = array(
'title' => 'Widget',
'description' => 'Included tawk.to widget configuration',
'page callback' => 'drupal_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 drupal_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 drupal_json_output(array('success' => false));
}
variable_set(TawkHelper::TAWK_TO_PAGE_ID, $_POST['pageId']);
variable_set(TawkHelper::TAWK_TO_WIDGET_ID, $_POST['widgetId']);
global $user;
if ($user->uid) {
variable_set(TawkHelper::TAWK_TO_WIDGET_USER_ID, $user->uid);
}
// Flush page cache so widget will appear at next page load
cache_clear_all(NULL, 'cache_page');
drupal_json_output(array('success' => true));
}
/**
* Ajax endpoint which is used to remove currently selected widget details.
*/
function tawk_to_remove_widget() {
variable_del(TawkHelper::TAWK_TO_PAGE_ID);
variable_del(TawkHelper::TAWK_TO_WIDGET_ID);
// Flush page cache so widget will disappear at next page load
cache_clear_all(NULL, 'cache_page');
drupal_json_output(array('success' => true));
}