-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatnode-toogle.php
More file actions
313 lines (279 loc) · 10.4 KB
/
chatnode-toogle.php
File metadata and controls
313 lines (279 loc) · 10.4 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
Plugin Name: ChatNode Toggle
Description: Allows you to customize the ChatNode Toggle for Wordpress.
Version: 1.0
Author: Daniel M.
Author URI: https://www.dmaurel.cl/
*/
if (!defined('ABSPATH')) {
exit;
}
function chatnode_enqueue_chatbot_script() {
if ('on' === get_option('chatnode_enqueue_script', 'off')) {
$chatbot_id = get_option('chatnode_chatbot_id');
$toggle_position = get_option('chatnode_toggle_position', 'right');
$color_1 = get_option('chatnode_color_1');
$color_2 = get_option('chatnode_color_2');
$tooltip_text = get_option('chatnode_tooltip_text', 'Your CTA');
?>
<script>
function waitForElementToDisplay(selector, time) {
if (document.querySelector(selector) != null) {
addTooltip(selector);
return;
} else {
setTimeout(function () {
waitForElementToDisplay(selector, time);
}, time);
}
}
function addTooltip(selector) {
var element = document.querySelector(selector);
if (element) {
element.setAttribute('data-tooltip', '<?php echo esc_js($tooltip_text); ?>');
element.classList.add('tooltip');
setTimeout(function () {
element.classList.add('show-tooltip');
}, 2000);
element.addEventListener('click', function () {
element.classList.remove('show-tooltip');
element.classList.add('hide-tooltip');
});
}
}
document.addEventListener('DOMContentLoaded', function() {
waitForElementToDisplay('#toggle-btn-<?php echo esc_js($chatbot_id); ?>', 500);
});
</script>
<script src="https://lkjink.com/embed.js" data-chatbot-id="<?php echo esc_attr($chatbot_id); ?>" data-color-1="<?php echo esc_attr($color_1); ?>" data-color-2="<?php echo esc_attr($color_2); ?>"></script>
<style>
.tooltip[data-tooltip]:before, .tooltip[data-tooltip]:after {
transition: opacity 0.3s, transform 0.3s;
<?php if ($toggle_position === 'left') : ?>
margin-left: 10px;
left: 100% !important;
<?php else : ?>
right: 100%;
<?php endif; ?>
}
.tooltip[data-tooltip]:before {
content: attr(data-tooltip);
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 100%;
margin-right: 10px; /* Ajustado para acomodar el caret */
padding: 5px 10px;
min-width: max-content;
text-align: center;
background-color: black;
color: white;
border-radius: 5px;
font-size: 14px;
z-index: 1000;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip[data-tooltip]:after {
content: '';
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 100%;
border-width: 10px;
border-style: solid;
transition: opacity 0.3s;
opacity: 0;
<?php if ($toggle_position === 'left') : ?>
margin-left: -8px;
border-color: transparent black transparent transparent;
left: 100% !important;
<?php else : ?>
margin-right: -8px;
border-color: transparent transparent transparent black;
right: 100%;
<?php endif; ?>
}
.tooltip.show-tooltip[data-tooltip]:before,
.tooltip.show-tooltip[data-tooltip]:after {
opacity: 1;
transform: translateY(-50%);
}
.tooltip.hide-tooltip[data-tooltip]:before,
.tooltip.hide-tooltip[data-tooltip]:after {
opacity: 0;
transform: translateY(-60%); /* Ligeramente hacia arriba para la animación de desvanecimiento */
}
/* Más estilos para el tooltip... */
#toggle-btn-<?php echo esc_js($chatbot_id); ?> {
bottom: 28px !important;
<?php echo $toggle_position; ?>: 28px !important;
width: 60px !important;
height: 60px !important;
}
#chatbot-84469dea83db146f-chat.open {
height: auto;
top: 40px;
opacity: 1;
transform: translateY(0%);
<?php if ($toggle_position === 'left') : ?>
left: 10px !important;
<?php else : ?>
right: 10px;
<?php endif; ?>
bottom: 96px;
}
@media (max-width: 768px) {
#chatbot-9c29a4d3e096eacf-chat.open {
height: calc(100vh - 114px);
opacity: 1;
transform: translateY(0%);
left: 20px;
bottom: 20px;
right: 20px;
top: 20% !important;
border-radius: 16px;
}
}
</style>
<?php
}
}
add_action('wp_head', 'chatnode_enqueue_chatbot_script');
function chatnode_register_settings() {
add_option('chatnode_enqueue_script', 'off');
add_option('chatnode_chatbot_id', '');
add_option('chatnode_color_1', '');
add_option('chatnode_color_2', '');
add_option('chatnode_tooltip_text', 'Hello!');
register_setting('chatnode_options', 'chatnode_enqueue_script', [
'type' => 'string',
'description' => 'Chatbot ID',
'sanitize_callback' => 'chatnode_sanitize_switcher',
'default' => 'on',
]);
register_setting('chatnode_options', 'chatnode_chatbot_id', [
'type' => 'string',
'description' => 'Chatbot ID',
'sanitize_callback' => 'sanitize_text_field',
'default' => null,
'required' => false,
]);
register_setting('chatnode_options', 'chatnode_toggle_position', [
'type' => 'string',
'description' => 'Toggle Position',
'sanitize_callback' => 'chatnode_sanitize_toggle_position',
'default' => 'right',
]);
register_setting('chatnode_options', 'chatnode_color_1', [
'type' => 'string',
'description' => 'Icon Color',
'sanitize_callback' => 'sanitize_hex_color',
'default' => null,
'required' => false,
]);
register_setting('chatnode_options', 'chatnode_color_2', [
'type' => 'string',
'description' => 'Toggle Background',
'sanitize_callback' => 'sanitize_hex_color',
'default' => null,
'required' => false,
]);
register_setting('chatnode_options', 'chatnode_tooltip_text', [
'type' => 'string',
'description' => 'Tooltip Text',
'sanitize_callback' => 'sanitize_text_field',
'default' => 'Ask me!',
]);
}
add_action('admin_init', 'chatnode_register_settings');
function chatnode_sanitize_switcher($input) {
return $input === 'on' ? 'on' : 'off';
}
function chatnode_sanitize_toggle_position($input) {
return in_array($input, ['left', 'right']) ? $input : 'right';
}
function chatnode_options_page() {
?>
<div class="wrap">
<h1>ChatNode Toogle</h1>
<form method="post" action="options.php">
<?php settings_fields('chatnode_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Enable Chatbot:</th>
<td>
<label>
<input type="checkbox" name="chatnode_enqueue_script" <?php checked(get_option('chatnode_enqueue_script', 'off'), 'on'); ?> />
Activate to show ChatNode
</label>
</td>
</tr>
<tr valign="top">
<th scope="row">Chatbot ID:</th>
<td><input type="password" name="chatnode_chatbot_id" id="chatnode-id" value="<?php echo esc_attr(get_option('chatnode_chatbot_id')); ?>" placeholder="Your Chatbot ID" /></td>
</tr>
<tr valign="top">
<th scope="row">Toggle Position:</th>
<td>
<label>
<input type="radio" name="chatnode_toggle_position" value="left" <?php checked(get_option('chatnode_toggle_position'), 'left'); ?> />
Left
</label>
<label>
<input type="radio" name="chatnode_toggle_position" value="right" <?php checked(get_option('chatnode_toggle_position'), 'right'); ?> />
Right
</label>
</td>
</tr>
<tr valign="top">
<th scope="row">Icon Color:</th>
<td><input type="text" name="chatnode_color_1" class="chatnode-color-picker" value="<?php echo esc_attr(get_option('chatnode_color_1')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Toggle Background:</th>
<td><input type="text" name="chatnode_color_2" class="chatnode-color-picker" value="<?php echo esc_attr(get_option('chatnode_color_2')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Tooltip:</th>
<td><input type="text" name="chatnode_tooltip_text" value="<?php echo esc_attr(get_option('chatnode_tooltip_text')); ?>" placeholder="Your CTA" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
// Incluir CSS y JavaScript del color picker.
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wp-color-picker');
// Inicializar color picker.
add_action('admin_footer', 'chatnode_initialize_color_picker');
}
function chatnode_initialize_color_picker() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.chatnode-color-picker').wpColorPicker();
});
</script>
<?php
}
add_action('admin_menu', function() {
add_options_page('Configuración del ChatNode Toggle', 'ChatNode Toggle', 'manage_options', 'chatnode-toggle', 'chatnode_options_page');
});
// Agrega el enlace de configuración en la página de plugins.
function chatnode_add_settings_link($links) {
$settings_link = '<a href="options-general.php?page=chatnode-toggle">' . __('Settings', 'wpturbo') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin_basename = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin_basename", 'chatnode_add_settings_link');
function chatnode_uninstall_plugin() {
delete_option('chatnode_chatbot_id');
delete_option('chatnode_color_1');
delete_option('chatnode_color_2');
delete_option('chatnode_tooltip_text');
}
register_uninstall_hook(__FILE__, 'chatnode_uninstall_plugin');