PluginProbe
Chaport — Live Chat & Chatbots / 1.1.18
Chaport — Live Chat & Chatbots v1.1.18
trunk 1.1.18 1.1.9
chaport / chaport.php

chaport.php in Chaport — Live Chat & Chatbots 1.1.18, at chaport.php

322 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Chaport — Live Chat & Chatbots
5 * Description: Modern live chat plugin for WordPress. Powerful features: multi-channel, chatbots, customization, etc. Free plan. Unlimited chats & websites.
6 * Version: 1.1.8
7 * Author: Chaport
8 * Author URI: https://www.chaport.com/
9 * Text Domain: chaport
10 * Domain Path: /languages
11 * License: MIT
12 */
13
14 if (!defined('ABSPATH')) exit; // Exit if accessed directly
15
16 require_once(dirname(__FILE__) . '/includes/models/chaport_app_id.php');
17 require_once(dirname(__FILE__) . '/includes/models/chaport_installation_code.php');
18 require_once(dirname(__FILE__) . '/includes/renderers/chaport_installation_code_renderer.php');
19 require_once(dirname(__FILE__) . '/includes/renderers/chaport_app_id_renderer.php');
20
21 return ChaportPlugin::bootstrap();
22
23 final class ChaportPlugin {
24 // Minimum required version of Wordpress for this plugin to work
25 const WP_MAJOR = 2;
26 const WP_MINOR = 8;
27
28 private $is_disallow_unfiltered_html;
29 private static $instance; // singleton
30 public static function bootstrap() {
31 if (self::$instance === NULL) {
32 self::$instance = new self();
33 }
34 return self::$instance;
35 }
36
37 private function __construct() { // constructable via ChaportPlugin::bootstrap()
38 $this->is_disallow_unfiltered_html = true;
39
40 add_action('plugins_loaded', array($this, 'load_textdomain'));
41 add_action('admin_enqueue_scripts', array($this, 'handle_admin_enqueue_scripts') );
42 add_action('admin_menu', array($this, 'handle_admin_menu'));
43 add_action('admin_init', array($this, 'handle_admin_init'));
44 add_action('wp_footer', array($this, 'render_chaport_code'));
45
46 add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'handle_plugin_actions'));
47 }
48
49 public function wp_version_is_compatible() {
50 global $wp_version;
51 $version = array_map('intval', explode('.', $wp_version));
52 return $version[0] > self::WP_MAJOR || ($version[0] === self::WP_MAJOR && $version[1] >= self::WP_MINOR);
53 }
54
55 public function load_textdomain() {
56 load_plugin_textdomain('chaport', false, basename(dirname(__FILE__)) . '/languages');
57 }
58
59 public function handle_admin_enqueue_scripts($hook) {
60 // Include styles _only_ on Chaport Settings page
61 if ($hook === 'settings_page_chaport') {
62 wp_enqueue_style('chaport', plugin_dir_url(__FILE__) . 'assets/css/style.css');
63 wp_enqueue_script('chaport', plugin_dir_url(__FILE__) . 'assets/js/toggle.js' );
64 }
65 }
66
67 public function handle_admin_menu() {
68 add_options_page(
69 __('Chaport Settings', 'chaport'), // $page_title
70 __('Chaport', 'chaport'), // $menu_title
71 'manage_options', // $capability
72 'chaport', // $menu_slug
73 array($this, 'render_settings_page') // $function (callback)
74 );
75 }
76
77 public function handle_admin_init() {
78 $this->is_disallow_unfiltered_html = (
79 defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML
80 );
81
82 // register_setting('chaport_options', 'chaport_options');
83 register_setting('chaport_options', 'chaport_options', array($this, 'sanitize_options'));
84
85 add_settings_section(
86 'chaport_general_settings', // $id
87 __('Chaport Settings', 'chaport'), // $title
88 array($this, 'render_chaport_general_settings'), // $callback
89 'chaport' // $page
90 );
91
92 add_settings_field(
93 'chaport_installation_type_field', // $id
94 __('Installation type', 'chaport'), // $title
95 array($this, 'render_installation_type_field'), // $callback
96 'chaport', // $page
97 'chaport_general_settings' //$section
98 );
99
100 add_settings_field(
101 'chaport_app_id_field', // $id
102 __('App ID', 'chaport'), // $title
103 array($this, 'render_app_id_field'), // $callback
104 'chaport', // $page
105 'chaport_general_settings' //$section
106 );
107
108 add_settings_field(
109 'chaport_app_installation_code_field', // $id
110 __('Installation code', 'chaport'), // $title
111 array($this, 'render_installation_code_field'), // $callback
112 'chaport', // $page
113 'chaport_general_settings' //$section
114 );
115 }
116
117 public function handle_plugin_actions($links) {
118 // Build and escape the URL.
119 $url = esc_url(add_query_arg(
120 'page',
121 'chaport',
122 get_admin_url() . 'admin.php'
123 ));
124 // Create the link.
125 $settings_link = "<a href='$url'>" . __('Settings') . '</a>';
126 // Adds the link to the end of the array.
127 array_push(
128 $links,
129 $settings_link
130 );
131 return $links;
132 }
133
134 public function sanitize_options($input) {
135 // Preserve existing saved values to avoid erasing other fields
136 $options = get_option('chaport_options', array());
137
138 $output = array();
139 $output['installation_type'] = (isset($input['installation_type']) && $input['installation_type'] === 'installationCode') ? 'installationCode' : 'appId';
140
141 if ($this->is_disallow_unfiltered_html) {
142 // Revert previous settings
143 $output['installation_code'] = isset($options['installation_code']) ? $options['installation_code'] : '';
144 } else {
145 $output['installation_code'] = isset($input['installation_code']) ? trim($input['installation_code']) : (isset($options['installation_code']) ? $options['installation_code'] : '');
146 }
147
148 // Disallow saving custom code if DISALLOW_UNFILTERED_HTML is set
149 if (
150 $output['installation_type'] === 'installationCode'
151 && $this->is_disallow_unfiltered_html
152 ) {
153 // Block saving, show error, revert to previous value
154 add_settings_error(
155 'chaport_options', // Setting slug
156 'chaport_installation_code_error', // Error code
157 __('You are not allowed to save custom installation code. Please use the App ID method instead, or contact your host administrator to add it for you.', 'chaport'), // Message
158 'error'
159 );
160
161 // Revert previous settings
162 $output['installation_type'] = isset($options['installation_type']) ? $options['installation_type'] : 'appId';
163 $output['app_id'] = isset($options['app_id']) ? $options['app_id'] : '';
164 } else {
165 $output['app_id'] = isset($input['app_id']) ? trim($input['app_id']) : (isset($options['app_id']) ? $options['app_id'] : '');
166 }
167
168 return $output;
169 }
170
171 public function get_options() {
172 $options = get_option('chaport_options', array());
173 $sanitized = array();
174 $sanitized['app_id'] = isset($options['app_id']) ? trim($options['app_id']) : '';
175 $sanitized['installation_code'] = isset($options['installation_code']) ? trim($options['installation_code']) : '';
176 $sanitized['installation_type'] = isset($options['installation_type']) && $options['installation_type'] === 'installationCode' ? 'installationCode' : 'appId';
177 return $sanitized;
178 }
179
180 public function render_chaport_general_settings() {
181 $status_message = __('Not configured.', 'chaport'); // Default status message
182 $status_class = 'chaport-status-warning'; // Default status class
183
184 $options = $this->get_options();
185 if (!isset($options['app_id'])) {
186 $options['app_id'] = '';
187 }
188 if (!isset($options['installation_code'])) {
189 $options['installation_code'] = '';
190 }
191 if (!isset($options['installation_type'])) {
192 $options['installation_type'] = 'appId';
193 }
194
195 if (!empty($options['app_id']) && $options['installation_type'] === 'appId') {
196 if (ChaportAppId::isValid($options['app_id'])) {
197 $status_message = __('Configured.', 'chaport');
198 $status_class = 'chaport-status-ok';
199 } else {
200 $status_message = __('Error. Invalid App ID.', 'chaport');
201 $status_class = 'chaport-status-error';
202 }
203 } elseif (!empty($options['installation_code']) && $options['installation_type'] === 'installationCode') {
204 if (ChaportInstallationCode::isValid($options['installation_code'])) {
205 $status_message = __('Configured.', 'chaport');
206 $status_class = 'chaport-status-ok';
207 } else {
208 $status_message = __('Error. Invalid Installation Code.', 'chaport');
209 $status_class = 'chaport-status-error';
210 }
211 }
212
213 require(dirname(__FILE__) . '/includes/snippets/chaport_status_snippet.php');
214
215 if ($this->is_disallow_unfiltered_html) {
216 echo '<div class="chaport-status-box chaport-status-warning">';
217 echo __('Custom installation code is disabled for site admins. Only host administrators can manage custom JavaScript code due to the current Wordpress security settings.', 'chaport');
218 echo '</div>';
219 }
220 }
221
222 public function render_app_id_field() {
223 $options = $this->get_options();
224
225 echo "<input id='chaport_app_id_field' name='chaport_options[app_id]' size='40' type='text' value='" . esc_attr($options['app_id']) . "' />";
226 }
227
228 public function render_installation_code_field() {
229 if (!$this->is_disallow_unfiltered_html) {
230 $options = $this->get_options();
231
232 echo "<textarea id='chaport_app_installation_code_field' name='chaport_options[installation_code]' rows='10' cols='60'>";
233 echo $options['installation_code'];
234 echo "</textarea>";
235 } else {
236 echo "<div id='chaport_app_installation_code_field'>" . __('Unavailable due to security settings', 'chaport') . "</div>";
237 }
238 }
239
240 public function render_installation_type_field() {
241 $options = $this->get_options();
242 $input_array = array(
243 'appId' => array(
244 'class' => 'chaport-default chaport-left',
245 'id' => 'chaport_default_app_id',
246 'value' => 'appId',
247 'onclick' => 'ChooseAppId()',
248 'label' => 'Default'
249 ),
250 'installationCode' => array(
251 'class' => 'chaport-default chaport-right',
252 'id' => 'chaport_default_installation_code',
253 'value' => 'installationCode',
254 'onclick' => $this->is_disallow_unfiltered_html ? 'javascript: void(0)' : 'ChooseInstallationCode()',
255 'label' => 'Installation code'
256 )
257 );
258
259 if ($options['installation_type'] !== 'installationCode') {
260 $options['installation_type'] = 'appId';
261 }
262
263 echo "<div class='switch-chaport' id='chaport_installation_type_field'>\n";
264 foreach ($input_array as $value) {
265 $input = "<input type='radio' name='chaport_options[installation_type]' class='" . $value['class'] . "' id='" . $value['id'] . "' value='" . $value['value'] . "' onclick='" . $value['onclick'] . "'";
266 if ($options['installation_type'] === $value['value']) {
267 $input = $input . " checked";
268 }
269
270 if ($this->is_disallow_unfiltered_html && $value['value'] === 'installationCode') {
271 $input = $input . " disabled";
272 }
273
274 $input = $input . ">\n";
275 echo $input;
276 echo "<label for='" . $value['id'] . "' class='btn'>" . __($value['label'], 'chaport') . "</label>\n";
277 };
278 echo "</div>";
279 }
280
281 public function render_settings_page() {
282 if (!current_user_can('manage_options')) {
283 wp_die(__("You don't have access to this page"));
284 }
285
286 require(dirname(__FILE__) . '/includes/snippets/chaport_settings_snippet.php');
287 }
288
289 public function render_chaport_code() {
290 // ignore requests to widgets.php for legacy widgets
291 if (isset($_GET['legacy-widget-preview'])) {
292 return;
293 }
294 if (!$this->wp_version_is_compatible() || is_feed() || is_robots() || is_trackback() || is_embed()) {
295 return;
296 }
297
298 $options = $this->get_options();
299 $app_id = $options['app_id'];
300 $installation_code = $options['installation_code'];
301 $options['installation_type'] = $options['installation_type'];
302 $user_settings = wp_get_current_user();
303
304 if (!empty($app_id) && ChaportAppId::isValid($app_id) && ($options['installation_type'] === 'appId')) {
305 $renderer = new ChaportAppIdRenderer(ChaportAppId::fromString($app_id));
306 } elseif(!empty($installation_code) && ChaportInstallationCode::isValid($installation_code) && ($options['installation_type'] === 'installationCode')){
307 $renderer = new ChaportInstallationCodeRenderer(ChaportInstallationCode::fromString($installation_code));
308 } else {
309 return;
310 }
311
312 if (!empty($user_settings->user_email)) {
313 $renderer->setUserEmail($user_settings->user_email);
314 }
315 if (!empty($user_settings->display_name)) {
316 $renderer->setUserName($user_settings->display_name);
317 }
318
319 $renderer->render();
320 }
321 }
322