-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathchatwoot-plugin.php
More file actions
225 lines (211 loc) · 9.96 KB
/
chatwoot-plugin.php
File metadata and controls
225 lines (211 loc) · 9.96 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
<?php
/**
* Plugin Name: Chatwoot Plugin
* Plugin URI: https://www.chatwoot.com/
* Description: Chatwoot Plugin for WordPress. This plugin helps you to quickly integrate Chatwoot live-chat widget on Wordpress websites.
* Author: antpb
* Author URI: chatwoot.com
* Text Domain: chatwoot-plugin
* Version: 0.2.2
*
* @package chatwoot-plugin
*/
add_action('admin_enqueue_scripts', 'admin_styles');
/**
* Load Chatwoot Admin CSS.
*
* @since 0.1.0
*
* @return {void}.
*/
function admin_styles() {
wp_enqueue_style('admin-styles', plugin_dir_url(__FILE__) . '/admin.css');
}
add_action( 'wp_enqueue_scripts', 'chatwoot_assets' );
/**
* Load Chatwoot Assets.
*
* @since 0.1.0
*
* @return {void}.
*/
function chatwoot_assets() {
// Check if chatbox should be displayed only to logged-in users
$logged_in_only = get_option('chatwootLoggedInOnly');
if ($logged_in_only === '1' && !is_user_logged_in()) {
return; // Don't load the chatbox if setting is enabled and user is not logged in
}
wp_enqueue_script( 'chatwoot-client', plugins_url( '/js/chatwoot.js' , __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'chatwoot_load' );
/**
* Initialize embed code options.
*
* @since 0.1.0
*
* @return {void}.
*/
function chatwoot_load() {
// Check if chatbox should be displayed only to logged-in users
$logged_in_only = get_option('chatwootLoggedInOnly');
if ($logged_in_only === '1' && !is_user_logged_in()) {
return; // Don't localize scripts if setting is enabled and user is not logged in
}
// Get our site options for site url and token.
$chatwoot_url = get_option('chatwootSiteURL');
$chatwoot_token = get_option('chatwootSiteToken');
$chatwoot_widget_locale = get_option('chatwootWidgetLocale');
$chatwoot_widget_type = get_option('chatwootWidgetType');
$chatwoot_widget_position = get_option('chatwootWidgetPosition');
$chatwoot_launcher_text = get_option('chatwootLauncherText');
// Localize our variables for the Javascript embed code.
wp_localize_script('chatwoot-client', 'chatwoot_token', $chatwoot_token);
wp_localize_script('chatwoot-client', 'chatwoot_url', $chatwoot_url);
wp_localize_script('chatwoot-client', 'chatwoot_widget_locale', $chatwoot_widget_locale);
wp_localize_script('chatwoot-client', 'chatwoot_widget_type', $chatwoot_widget_type);
wp_localize_script('chatwoot-client', 'chatwoot_launcher_text', $chatwoot_launcher_text);
wp_localize_script('chatwoot-client', 'chatwoot_widget_position', $chatwoot_widget_position);
}
add_action('admin_menu', 'chatwoot_setup_menu');
/**
* Set up Settings options page.
*
* @since 0.1.0
*
* @return {void}.
*/
function chatwoot_setup_menu(){
add_options_page('Option', 'Chatwoot Settings', 'manage_options', 'chatwoot-plugin-options', 'chatwoot_options_page');
}
add_action( 'admin_init', 'chatwoot_register_settings' );
/**
* Register Settings.
*
* @since 0.1.0
*
* @return {void}.
*/
function chatwoot_register_settings() {
add_option('chatwootSiteToken', '');
add_option('chatwootSiteURL', '');
add_option('chatwootWidgetLocale', 'en');
add_option('chatwootWidgetType', 'standard');
add_option('chatwootWidgetPosition', 'right');
add_option('chatwootLauncherText', '');
add_option('chatwootLoggedInOnly', '0');
register_setting('chatwoot-plugin-options', 'chatwootSiteToken' );
register_setting('chatwoot-plugin-options', 'chatwootSiteURL');
register_setting('chatwoot-plugin-options', 'chatwootWidgetLocale' );
register_setting('chatwoot-plugin-options', 'chatwootWidgetType' );
register_setting('chatwoot-plugin-options', 'chatwootWidgetPosition' );
register_setting('chatwoot-plugin-options', 'chatwootLauncherText' );
register_setting('chatwoot-plugin-options', 'chatwootLoggedInOnly' );
}
/**
* Render page.
*
* @since 0.1.0
*
* @return {void}.
*/
function chatwoot_options_page() {
?>
<div>
<h2>Chatwoot Settings</h2>
<form method="post" action="options.php" class="chatwoot--form">
<?php settings_fields('chatwoot-plugin-options'); ?>
<div class="form--input">
<label for="chatwootSiteToken">Chatwoot Website Token</label>
<input
type="text"
name="chatwootSiteToken"
value="<?php echo get_option('chatwootSiteToken'); ?>"
/>
</div>
<div class="form--input">
<label for="chatwootSiteURL">Chatwoot Installation URL</label>
<input
type="text"
name="chatwootSiteURL"
value="<?php echo get_option('chatwootSiteURL'); ?>"
/>
</div>
<hr />
<div class="form--input">
<label for="chatwootLoggedInOnly">
<input
type="checkbox"
name="chatwootLoggedInOnly"
value="1"
<?php checked(get_option('chatwootLoggedInOnly'), '1'); ?>
/>
Display widget only to logged-in users
</label>
</div>
<hr />
<div class="form--input">
<label for="chatwootWidgetType">Widget Design</label>
<select name="chatwootWidgetType">
<option value="standard" <?php selected(get_option('chatwootWidgetType'), 'standard'); ?>>Standard</option>
<option value="expanded_bubble" <?php selected(get_option('chatwootWidgetType'), 'expanded_bubble'); ?>>Expanded Bubble</option>
</select>
</div>
<div class="form--input">
<label for="chatwootWidgetPosition">Widget Position</label>
<select name="chatwootWidgetPosition">
<option value="left" <?php selected(get_option('chatwootWidgetPosition'), 'left'); ?>>Left</option>
<option value="right" <?php selected(get_option('chatwootWidgetPosition'), 'right'); ?>>Right</option>
</select>
</div>
<div class="form--input">
<label for="chatwootWidgetLocale">Language</label>
<select name="chatwootWidgetLocale">
<option <?php selected(get_option('chatwootWidgetLocale'), 'ar'); ?> value="ar">العربية (ar)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ca'); ?> value="ca">Català (ca)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'cs'); ?> value="cs">čeština (cs)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'da'); ?> value="da">dansk (da)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'de'); ?> value="de">Deutsch (de)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'el'); ?> value="el">ελληνικά (el)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'en'); ?> value="en">English (en)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'es'); ?> value="es">Español (es)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'fa'); ?> value="fa">فارسی (fa)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'fi'); ?> value="fi">suomi, suomen kieli (fi)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'fr'); ?> value="fr">Français (fr)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'hi'); ?> value="hi'">हिन्दी (hi)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'hu'); ?> value="hu">magyar nyelv (hu)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'id'); ?> value="id">Bahasa Indonesia (id)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'it'); ?> value="it">Italiano (it)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ja'); ?> value="ja">日本語 (ja)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ko'); ?> value="ko">한국어 (ko)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ml'); ?> value="ml">മലയാളം (ml)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'nl'); ?> value="nl">Nederlands (nl) </option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'no'); ?> value="no">norsk (no)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'pl'); ?> value="pl">język polski (pl)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'pt_BR'); ?> value="pt_BR">Português Brasileiro (pt-BR)
<option <?php selected(get_option('chatwootWidgetLocale'), 'pt'); ?> value="pt">Português (pt)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ro'); ?> value="ro">Română (ro)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ru'); ?> value="ru">русский (ru)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'sv'); ?> value="sv">Svenska (sv)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'ta'); ?> value="ta">தமிழ் (ta)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'tr'); ?> value="tr">Türkçe (tr)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'vi'); ?> value="vi">Tiếng Việt (vi)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'zh_CN'); ?> value="zh_CN">中文 (zh-CN)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'zh_TW'); ?> value="zh_TW">中文 (台湾) (zh-TW)</option>
<option <?php selected(get_option('chatwootWidgetLocale'), 'zh'); ?> value="zh'">中文 (zh)</option>
</select>
</div>
<?php if (get_option('chatwootWidgetType') == 'expanded_bubble') : ?>
<div class="form--input">
<label for="chatwootLauncherText">Launcher Text (Optional)</label>
<input
type="text"
name="chatwootLauncherText"
value="<?php echo get_option('chatwootLauncherText'); ?>"
/>
</div>
<?php endif; ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}