| 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.9 |
| 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', array(), '1.0.0'); |
| 63 |
wp_enqueue_script('chaport', plugin_dir_url(__FILE__) . 'assets/js/toggle.js', array(), '1.0.0', array('in_footer' => true )); |
| 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 = add_query_arg( |
| 120 |
'page', |
| 121 |
'chaport', |
| 122 |
get_admin_url() . 'admin.php' |
| 123 |
); |
| 124 |
// Create the link. |
| 125 |
$settings_link = sprintf( |
| 126 |
"<a href='%s'>%s</a>", |
| 127 |
esc_url( $url ), |
| 128 |
esc_html__( 'Settings', 'chaport' ) |
| 129 |
); |
| 130 |
// Adds the link to the end of the array. |
| 131 |
array_push( |
| 132 |
$links, |
| 133 |
$settings_link |
| 134 |
); |
| 135 |
return $links; |
| 136 |
} |
| 137 |
|
| 138 |
public function sanitize_options($input) { |
| 139 |
// Preserve existing saved values to avoid erasing other fields |
| 140 |
$options = get_option('chaport_options', array()); |
| 141 |
|
| 142 |
$output = array(); |
| 143 |
$output['installation_type'] = (isset($input['installation_type']) && $input['installation_type'] === 'installationCode') ? 'installationCode' : 'appId'; |
| 144 |
|
| 145 |
if ($this->is_disallow_unfiltered_html) { |
| 146 |
// Revert previous settings |
| 147 |
$output['installation_code'] = isset($options['installation_code']) ? $options['installation_code'] : ''; |
| 148 |
} else { |
| 149 |
$output['installation_code'] = isset($input['installation_code']) ? trim($input['installation_code']) : (isset($options['installation_code']) ? $options['installation_code'] : ''); |
| 150 |
} |
| 151 |
|
| 152 |
// Disallow saving custom code if DISALLOW_UNFILTERED_HTML is set |
| 153 |
if ( |
| 154 |
$output['installation_type'] === 'installationCode' |
| 155 |
&& $this->is_disallow_unfiltered_html |
| 156 |
) { |
| 157 |
// Block saving, show error, revert to previous value |
| 158 |
add_settings_error( |
| 159 |
'chaport_options', // Setting slug |
| 160 |
'chaport_installation_code_error', // Error code |
| 161 |
__('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 |
| 162 |
'error' |
| 163 |
); |
| 164 |
|
| 165 |
// Revert previous settings |
| 166 |
$output['installation_type'] = isset($options['installation_type']) ? $options['installation_type'] : 'appId'; |
| 167 |
$output['app_id'] = isset($options['app_id']) ? $options['app_id'] : ''; |
| 168 |
} else { |
| 169 |
$output['app_id'] = isset($input['app_id']) ? trim($input['app_id']) : (isset($options['app_id']) ? $options['app_id'] : ''); |
| 170 |
} |
| 171 |
|
| 172 |
return $output; |
| 173 |
} |
| 174 |
|
| 175 |
public function get_options() { |
| 176 |
$options = get_option('chaport_options', array()); |
| 177 |
$sanitized = array(); |
| 178 |
$sanitized['app_id'] = isset($options['app_id']) ? trim($options['app_id']) : ''; |
| 179 |
$sanitized['installation_code'] = isset($options['installation_code']) ? trim($options['installation_code']) : ''; |
| 180 |
$sanitized['installation_type'] = isset($options['installation_type']) && $options['installation_type'] === 'installationCode' ? 'installationCode' : 'appId'; |
| 181 |
return $sanitized; |
| 182 |
} |
| 183 |
|
| 184 |
public function render_chaport_general_settings() { |
| 185 |
$status_message = __('Not configured.', 'chaport'); // Default status message |
| 186 |
$status_class = 'chaport-status-warning'; // Default status class |
| 187 |
|
| 188 |
$options = $this->get_options(); |
| 189 |
if (!isset($options['app_id'])) { |
| 190 |
$options['app_id'] = ''; |
| 191 |
} |
| 192 |
if (!isset($options['installation_code'])) { |
| 193 |
$options['installation_code'] = ''; |
| 194 |
} |
| 195 |
if (!isset($options['installation_type'])) { |
| 196 |
$options['installation_type'] = 'appId'; |
| 197 |
} |
| 198 |
|
| 199 |
if (!empty($options['app_id']) && $options['installation_type'] === 'appId') { |
| 200 |
if (ChaportAppId::isValid($options['app_id'])) { |
| 201 |
$status_message = __('Configured.', 'chaport'); |
| 202 |
$status_class = 'chaport-status-ok'; |
| 203 |
} else { |
| 204 |
$status_message = __('Error. Invalid App ID.', 'chaport'); |
| 205 |
$status_class = 'chaport-status-error'; |
| 206 |
} |
| 207 |
} elseif (!empty($options['installation_code']) && $options['installation_type'] === 'installationCode') { |
| 208 |
if (ChaportInstallationCode::isValid($options['installation_code'])) { |
| 209 |
$status_message = __('Configured.', 'chaport'); |
| 210 |
$status_class = 'chaport-status-ok'; |
| 211 |
} else { |
| 212 |
$status_message = __('Error. Invalid Installation Code.', 'chaport'); |
| 213 |
$status_class = 'chaport-status-error'; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
require(dirname(__FILE__) . '/includes/snippets/chaport_status_snippet.php'); |
| 218 |
|
| 219 |
if ($this->is_disallow_unfiltered_html) { |
| 220 |
echo '<div class="chaport-status-box chaport-status-warning">'; |
| 221 |
echo esc_html__('Custom installation code is disabled for site admins. Only host administrators can manage custom JavaScript code due to the current Wordpress security settings.', 'chaport'); |
| 222 |
echo '</div>'; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
public function render_app_id_field() { |
| 227 |
$options = $this->get_options(); |
| 228 |
|
| 229 |
printf( |
| 230 |
"<input id='chaport_app_id_field' name='chaport_options[app_id]' size='40' type='text' value='%s' />", |
| 231 |
esc_attr($options['app_id']) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
public function render_installation_code_field() { |
| 236 |
if (!$this->is_disallow_unfiltered_html) { |
| 237 |
$options = $this->get_options(); |
| 238 |
|
| 239 |
echo "<textarea id='chaport_app_installation_code_field' name='chaport_options[installation_code]' rows='10' cols='60'>"; |
| 240 |
echo esc_textarea($options['installation_code']); |
| 241 |
echo "</textarea>"; |
| 242 |
} else { |
| 243 |
echo "<div id='chaport_app_installation_code_field'>" . esc_html__('Unavailable due to security settings', 'chaport') . "</div>"; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public function render_installation_type_field() { |
| 248 |
$options = $this->get_options(); |
| 249 |
$input_array = array( |
| 250 |
'appId' => array( |
| 251 |
'class' => 'chaport-default chaport-left', |
| 252 |
'id' => 'chaport_default_app_id', |
| 253 |
'value' => 'appId', |
| 254 |
'onclick' => 'ChooseAppId()', |
| 255 |
'label' => __('Default', 'chaport') |
| 256 |
), |
| 257 |
'installationCode' => array( |
| 258 |
'class' => 'chaport-default chaport-right', |
| 259 |
'id' => 'chaport_default_installation_code', |
| 260 |
'value' => 'installationCode', |
| 261 |
'onclick' => $this->is_disallow_unfiltered_html ? 'javascript: void(0)' : 'ChooseInstallationCode()', |
| 262 |
'label' => __('Installation code', 'chaport') |
| 263 |
) |
| 264 |
); |
| 265 |
|
| 266 |
if ($options['installation_type'] !== 'installationCode') { |
| 267 |
$options['installation_type'] = 'appId'; |
| 268 |
} |
| 269 |
|
| 270 |
echo "<div class='switch-chaport' id='chaport_installation_type_field'>\n"; |
| 271 |
foreach ($input_array as $value) { |
| 272 |
printf( |
| 273 |
"<input type='radio' name='chaport_options[installation_type]' class='%s' id='%s' value='%s' onclick='%s'%s%s>\n", |
| 274 |
esc_attr( $value['class'] ), |
| 275 |
esc_attr( $value['id'] ), |
| 276 |
esc_attr( $value['value'] ), |
| 277 |
esc_attr( $value['onclick'] ), |
| 278 |
$options['installation_type'] === $value['value'] ? ' checked' : '', |
| 279 |
( $this->is_disallow_unfiltered_html && $value['value'] === 'installationCode' ) ? ' disabled' : '' |
| 280 |
); |
| 281 |
|
| 282 |
printf( |
| 283 |
"<label for='%s' class='btn'>%s</label>\n", |
| 284 |
esc_attr( $value['id'] ), |
| 285 |
esc_html( $value['label'] ) |
| 286 |
); |
| 287 |
}; |
| 288 |
echo "</div>"; |
| 289 |
} |
| 290 |
|
| 291 |
public function render_settings_page() { |
| 292 |
if (!current_user_can('manage_options')) { |
| 293 |
wp_die(esc_html__("You don't have access to this page", 'chaport')); |
| 294 |
} |
| 295 |
|
| 296 |
require(dirname(__FILE__) . '/includes/snippets/chaport_settings_snippet.php'); |
| 297 |
} |
| 298 |
|
| 299 |
public function render_chaport_code() { |
| 300 |
// ignore requests to widgets.php for legacy widgets |
| 301 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 302 |
if (isset($_GET['legacy-widget-preview'])) { |
| 303 |
return; |
| 304 |
} |
| 305 |
if (!$this->wp_version_is_compatible() || is_feed() || is_robots() || is_trackback() || is_embed()) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
$options = $this->get_options(); |
| 310 |
$app_id = $options['app_id']; |
| 311 |
$installation_code = $options['installation_code']; |
| 312 |
$options['installation_type'] = $options['installation_type']; |
| 313 |
$user_settings = wp_get_current_user(); |
| 314 |
|
| 315 |
if (!empty($app_id) && ChaportAppId::isValid($app_id) && ($options['installation_type'] === 'appId')) { |
| 316 |
$renderer = new ChaportAppIdRenderer(ChaportAppId::fromString($app_id)); |
| 317 |
} elseif(!empty($installation_code) && ChaportInstallationCode::isValid($installation_code) && ($options['installation_type'] === 'installationCode')){ |
| 318 |
$renderer = new ChaportInstallationCodeRenderer(ChaportInstallationCode::fromString($installation_code)); |
| 319 |
} else { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
if (!empty($user_settings->user_email)) { |
| 324 |
$renderer->setUserEmail($user_settings->user_email); |
| 325 |
} |
| 326 |
if (!empty($user_settings->display_name)) { |
| 327 |
$renderer->setUserName($user_settings->display_name); |
| 328 |
} |
| 329 |
|
| 330 |
$renderer->render(); |
| 331 |
} |
| 332 |
} |
| 333 |
|