| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Text_Editor' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Text_Editor { |
| 9 |
|
| 10 |
/** |
| 11 |
* Tabs for this section |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $tabs; |
| 16 |
|
| 17 |
/** |
| 18 |
* Current tab |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public static $current_tab; |
| 23 |
|
| 24 |
/** |
| 25 |
* Rich text editor toolbar |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
public static $toolbar = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize this class |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function init() { |
| 37 |
|
| 38 |
// Load settings. |
| 39 |
add_action( 'init', array( __CLASS__, 'toolbar_options' ) ); |
| 40 |
|
| 41 |
// Load tabs for this section. |
| 42 |
add_action( 'admin_init', array( __CLASS__, 'load_tabs' ) ); |
| 43 |
|
| 44 |
// Add current tab to admin localization data. |
| 45 |
add_filter( 'wpsc_admin_localizations', array( __CLASS__, 'localizations' ) ); |
| 46 |
|
| 47 |
// Load section tab layout. |
| 48 |
add_action( 'wp_ajax_wpsc_get_rich_text_editor', array( __CLASS__, 'get_rich_text_editor' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Load toolbar settings for global use |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public static function toolbar_options() { |
| 57 |
|
| 58 |
$toolbar = array( |
| 59 |
array( |
| 60 |
'name' => esc_attr__( 'Bold', 'supportcandy' ), |
| 61 |
'value' => 'bold', |
| 62 |
), |
| 63 |
array( |
| 64 |
'name' => esc_attr__( 'Italic', 'supportcandy' ), |
| 65 |
'value' => 'italic', |
| 66 |
), |
| 67 |
array( |
| 68 |
'name' => esc_attr__( 'Underline', 'supportcandy' ), |
| 69 |
'value' => 'underline', |
| 70 |
), |
| 71 |
array( |
| 72 |
'name' => esc_attr__( 'Blockquote', 'supportcandy' ), |
| 73 |
'value' => 'blockquote', |
| 74 |
), |
| 75 |
array( |
| 76 |
'name' => esc_attr__( 'Align', 'supportcandy' ), |
| 77 |
'value' => 'alignleft aligncenter alignright', |
| 78 |
), |
| 79 |
array( |
| 80 |
'name' => esc_attr__( 'Bulleted list', 'supportcandy' ), |
| 81 |
'value' => 'bullist', |
| 82 |
), |
| 83 |
array( |
| 84 |
'name' => esc_attr__( 'Numbered list', 'supportcandy' ), |
| 85 |
'value' => 'numlist', |
| 86 |
), |
| 87 |
array( |
| 88 |
'name' => esc_attr__( 'Right to left', 'supportcandy' ), |
| 89 |
'value' => 'rtl', |
| 90 |
), |
| 91 |
array( |
| 92 |
'name' => esc_attr__( 'Link', 'supportcandy' ), |
| 93 |
'value' => 'link', |
| 94 |
), |
| 95 |
array( |
| 96 |
'name' => esc_attr__( 'Image', 'supportcandy' ), |
| 97 |
'value' => 'wpsc_insert_editor_img', |
| 98 |
), |
| 99 |
array( |
| 100 |
'name' => esc_attr__( 'Text Color', 'supportcandy' ), |
| 101 |
'value' => 'forecolor', |
| 102 |
), |
| 103 |
array( |
| 104 |
'name' => esc_attr__( 'Text Background Color', 'supportcandy' ), |
| 105 |
'value' => 'backcolor', |
| 106 |
), |
| 107 |
array( |
| 108 |
'name' => esc_attr__( 'Strikethrough', 'supportcandy' ), |
| 109 |
'value' => 'strikethrough', |
| 110 |
), |
| 111 |
); |
| 112 |
self::$toolbar = $toolbar; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Load tabs for this section |
| 117 |
*/ |
| 118 |
public static function load_tabs() { |
| 119 |
|
| 120 |
self::$tabs = apply_filters( |
| 121 |
'wpsc_te_tabs', |
| 122 |
array( |
| 123 |
'agent' => array( |
| 124 |
'slug' => 'agent', |
| 125 |
'label' => esc_attr__( 'Agent', 'supportcandy' ), |
| 126 |
'callback' => 'wpsc_get_te_agent', |
| 127 |
), |
| 128 |
'registered-user' => array( |
| 129 |
'slug' => 'registered_user', |
| 130 |
'label' => esc_attr__( 'Registered User', 'supportcandy' ), |
| 131 |
'callback' => 'wpsc_get_te_registered_user', |
| 132 |
), |
| 133 |
'guest-user' => array( |
| 134 |
'slug' => 'guest_user', |
| 135 |
'label' => esc_attr__( 'Guest User', 'supportcandy' ), |
| 136 |
'callback' => 'wpsc_get_te_guest_user', |
| 137 |
), |
| 138 |
'advanced' => array( |
| 139 |
'slug' => 'advanced', |
| 140 |
'label' => esc_attr__( 'Advanced', 'supportcandy' ), |
| 141 |
'callback' => 'wpsc_get_te_advanced', |
| 142 |
), |
| 143 |
) |
| 144 |
); |
| 145 |
self::$current_tab = isset( $_REQUEST['tab'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ) : 'agent'; //phpcs:ignore |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Add localizations to local JS |
| 151 |
* |
| 152 |
* @param array $localizations - localization list. |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public static function localizations( $localizations ) { |
| 156 |
|
| 157 |
if ( ! ( WPSC_Settings::$is_current_page && WPSC_Settings::$current_section === 'rich-text-editor' ) ) { |
| 158 |
return $localizations; |
| 159 |
} |
| 160 |
|
| 161 |
// Current section. |
| 162 |
$localizations['current_tab'] = self::$current_tab; |
| 163 |
|
| 164 |
return $localizations; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* General setion body layout |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public static function get_rich_text_editor() { |
| 173 |
|
| 174 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 175 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 176 |
}?> |
| 177 |
|
| 178 |
<div class="wpsc-setting-tab-container"> |
| 179 |
<?php |
| 180 |
foreach ( self::$tabs as $key => $tab ) : |
| 181 |
$active = self::$current_tab === $key ? 'active' : '' |
| 182 |
?> |
| 183 |
<button |
| 184 |
class="<?php echo esc_attr( $key ) . ' ' . esc_attr( $active ); ?>" |
| 185 |
onclick="<?php echo esc_attr( $tab['callback'] ) . '();'; ?>"> |
| 186 |
<?php echo esc_attr( $tab['label'] ); ?> |
| 187 |
</button> |
| 188 |
<?php |
| 189 |
endforeach; |
| 190 |
?> |
| 191 |
</div> |
| 192 |
<div class="wpsc-setting-section-body"></div> |
| 193 |
<?php |
| 194 |
wp_die(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Print editor (TinyMCE) init scripts |
| 199 |
* |
| 200 |
* @param string $id - textarea id. |
| 201 |
* @param string $body_id - body id. |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public static function print_editor_init_scripts( $id, $body_id ) { |
| 205 |
|
| 206 |
$toolbox = array(); |
| 207 |
$is_editor = false; |
| 208 |
$current_user = WPSC_Current_User::$current_user; |
| 209 |
$advanced = get_option( 'wpsc-te-advanced' ); |
| 210 |
$rich_editing = get_user_meta( $current_user->user->ID, 'rich_editing', true ); |
| 211 |
$rich_editing = filter_var( $rich_editing, FILTER_VALIDATE_BOOLEAN ); |
| 212 |
|
| 213 |
if ( $current_user->is_agent ) { |
| 214 |
|
| 215 |
$agent = get_option( 'wpsc-te-agent' ); |
| 216 |
if ( $agent['enable'] ) { |
| 217 |
$is_editor = true; |
| 218 |
foreach ( $agent['toolbar'] as $key => $value ) { |
| 219 |
$toolbox[] = $value; |
| 220 |
if ( in_array( $value, array( 'blockquote', 'alignright', 'numlist', 'rtl', 'wpsc_insert_editor_img' ) ) ) { |
| 221 |
$toolbox[] = '|'; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} elseif ( $current_user->is_customer && $current_user->user->ID ) { |
| 226 |
|
| 227 |
$reg_user = get_option( 'wpsc-te-registered-user' ); |
| 228 |
if ( $reg_user['enable'] ) { |
| 229 |
$is_editor = true; |
| 230 |
foreach ( $reg_user['toolbar'] as $key => $value ) { |
| 231 |
$toolbox[] = $value; |
| 232 |
if ( in_array( $value, array( 'blockquote', 'alignright', 'numlist', 'rtl', 'wpsc_insert_editor_img' ) ) ) { |
| 233 |
$toolbox[] = '|'; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} else { |
| 238 |
|
| 239 |
$guest = get_option( 'wpsc-te-guest-user' ); |
| 240 |
if ( $guest['enable'] ) { |
| 241 |
$is_editor = true; |
| 242 |
foreach ( $guest['toolbar'] as $key => $value ) { |
| 243 |
$toolbox[] = $value; |
| 244 |
if ( in_array( $value, array( 'blockquote', 'alignright', 'numlist', 'rtl', 'wpsc_insert_editor_img' ) ) ) { |
| 245 |
$toolbox[] = '|'; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
$rich_editing = true; |
| 250 |
} |
| 251 |
|
| 252 |
$allow_paste_img = false; |
| 253 |
if ( in_array( 'wpsc_insert_editor_img', $toolbox ) ) { |
| 254 |
$allow_paste_img = true; |
| 255 |
} |
| 256 |
$toolbox = implode( ' ', $toolbox ); |
| 257 |
?> |
| 258 |
|
| 259 |
var isWPSCEditor = <?php echo esc_attr( $is_editor ) && esc_attr( $rich_editing ) ? '1' : '0'; ?>; |
| 260 |
<?php |
| 261 |
if ( $is_editor && $rich_editing ) : |
| 262 |
?> |
| 263 |
tinymce.remove('#<?php echo esc_attr( $id ); ?>'); |
| 264 |
tinymce.init({ |
| 265 |
selector:'#<?php echo esc_attr( $id ); ?>', |
| 266 |
body_id: '<?php echo esc_attr( $body_id ); ?>', |
| 267 |
menubar: false, |
| 268 |
statusbar: false, |
| 269 |
autoresize_min_height: 150, |
| 270 |
wp_autoresize_on: true, |
| 271 |
plugins: [ |
| 272 |
'lists link directionality wpautoresize textcolor paste' |
| 273 |
], |
| 274 |
<?php echo $advanced['html-pasting'] == 0 ? 'paste_as_text: true,' : ''; ?> |
| 275 |
image_advtab: true, |
| 276 |
toolbar: '<?php echo esc_attr( $toolbox ); ?>', |
| 277 |
directionality: '<?php echo is_rtl() ? 'rtl' : 'ltr'; ?>', |
| 278 |
branding: false, |
| 279 |
autoresize_bottom_margin: 20, |
| 280 |
browser_spellcheck : true, |
| 281 |
relative_urls : false, |
| 282 |
remove_script_host : false, |
| 283 |
convert_urls : false, |
| 284 |
<?php |
| 285 |
if ( $allow_paste_img ) { |
| 286 |
?> |
| 287 |
paste_data_images: true, |
| 288 |
images_upload_handler: function (blobInfo, success, failure) { |
| 289 |
|
| 290 |
form_data = new FormData(); |
| 291 |
form_data.append('file', blobInfo.blob(), blobInfo.filename()); |
| 292 |
form_data.append('action','wpsc_tinymce_upload_file'); |
| 293 |
form_data.append('_ajax_nonce', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tinymce_upload_file' ) ); ?>'); |
| 294 |
|
| 295 |
jQuery.ajax({ |
| 296 |
type : 'post', |
| 297 |
url : supportcandy.ajax_url, |
| 298 |
cache: false, |
| 299 |
contentType: false, |
| 300 |
processData: false, |
| 301 |
data: form_data, |
| 302 |
success: function(res){ |
| 303 |
success(res.imgURL); |
| 304 |
} |
| 305 |
}); |
| 306 |
}, |
| 307 |
<?php |
| 308 |
} |
| 309 |
?> |
| 310 |
setup: function (editor) { |
| 311 |
editor.on('blur', function(e) { |
| 312 |
jQuery( '#<?php echo esc_attr( $id ); ?>' ).text( editor.getContent() ); |
| 313 |
wpsc_check_tff_visibility(); |
| 314 |
}); |
| 315 |
// Add a custom button |
| 316 |
editor.addButton('wpsc_insert_editor_img', { |
| 317 |
title : 'Insert/edit image', |
| 318 |
onclick : function() { |
| 319 |
// Add you own code to execute something on click |
| 320 |
wpsc_add_custom_image_tinymce(editor, '<?php echo esc_attr( wp_create_nonce( 'wpsc_add_custom_image_tinymce' ) ); ?>'); |
| 321 |
} |
| 322 |
}); |
| 323 |
editor.on('click', function (e) { |
| 324 |
if(e.target.nodeName === "IMG"){ |
| 325 |
wpsc_edit_custom_image_tinymce(editor, e.target, '<?php echo esc_attr( wp_create_nonce( 'wpsc_edit_custom_image_tinymce' ) ); ?>'); |
| 326 |
} |
| 327 |
}); |
| 328 |
editor.on('KeyUp', function (e) { |
| 329 |
if( tinyMCE.activeEditor.getContent() == '' ) { |
| 330 |
ticket_id = jQuery('#wpsc-current-ticket').val(); |
| 331 |
wpsc_clear_saved_draft_reply( ticket_id ); |
| 332 |
} |
| 333 |
}); |
| 334 |
editor.on('keyup change input', function () { |
| 335 |
var field = jQuery('#' + editor.id).closest('.wpsc-tff'); |
| 336 |
|
| 337 |
// Check if editor actually has content |
| 338 |
var content = editor.getContent({ format: 'text' }).trim(); |
| 339 |
|
| 340 |
if (content !== '') { |
| 341 |
field.find('.wpsc-error-msg').remove(); |
| 342 |
} |
| 343 |
}); |
| 344 |
} |
| 345 |
}); |
| 346 |
<?php |
| 347 |
endif; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Check attachment are allowed to user |
| 352 |
* |
| 353 |
* @return boolean |
| 354 |
*/ |
| 355 |
public static function is_allow_attachments() { |
| 356 |
|
| 357 |
$current_user = WPSC_Current_User::$current_user; |
| 358 |
$flag = false; |
| 359 |
|
| 360 |
if ( $current_user->is_agent ) { |
| 361 |
|
| 362 |
$agent = get_option( 'wpsc-te-agent' ); |
| 363 |
if ( $agent['allow-attachments'] ) { |
| 364 |
$flag = true; |
| 365 |
} |
| 366 |
} elseif ( $current_user->is_customer && $current_user->user->ID ) { |
| 367 |
|
| 368 |
$reg_user = get_option( 'wpsc-te-registered-user' ); |
| 369 |
if ( $reg_user['allow-attachments'] ) { |
| 370 |
$flag = true; |
| 371 |
} |
| 372 |
} else { |
| 373 |
|
| 374 |
$guest = get_option( 'wpsc-te-guest-user' ); |
| 375 |
if ( $guest['allow-attachments'] ) { |
| 376 |
$flag = true; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return $flag; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Check attachment notice are allowed to user |
| 385 |
* |
| 386 |
* @return boolean |
| 387 |
*/ |
| 388 |
public static function is_attachment_notice() { |
| 389 |
|
| 390 |
$current_user = WPSC_Current_User::$current_user; |
| 391 |
$flag = false; |
| 392 |
|
| 393 |
if ( $current_user->is_agent ) { |
| 394 |
|
| 395 |
$agent = get_option( 'wpsc-te-agent' ); |
| 396 |
if ( $agent['file-attachment-notice'] ) { |
| 397 |
$flag = true; |
| 398 |
} |
| 399 |
} elseif ( $current_user->is_customer && $current_user->user->ID ) { |
| 400 |
|
| 401 |
$reg_user = get_option( 'wpsc-te-registered-user' ); |
| 402 |
if ( $reg_user['file-attachment-notice'] ) { |
| 403 |
$flag = true; |
| 404 |
} |
| 405 |
} else { |
| 406 |
|
| 407 |
$guest = get_option( 'wpsc-te-guest-user' ); |
| 408 |
if ( $guest['file-attachment-notice'] ) { |
| 409 |
$flag = true; |
| 410 |
} |
| 411 |
} |
| 412 |
return $flag; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* File attachment notice text |
| 417 |
* |
| 418 |
* @return string |
| 419 |
*/ |
| 420 |
public static function file_attachment_notice_text() { |
| 421 |
|
| 422 |
$current_user = WPSC_Current_User::$current_user; |
| 423 |
$notice_text = ''; |
| 424 |
|
| 425 |
if ( $current_user->is_agent ) { |
| 426 |
|
| 427 |
$agent = get_option( 'wpsc-te-agent' ); |
| 428 |
$notice_text = $agent['file-attachment-notice-text']; |
| 429 |
|
| 430 |
} elseif ( $current_user->is_customer && $current_user->user->ID ) { |
| 431 |
|
| 432 |
$reg_user = get_option( 'wpsc-te-registered-user' ); |
| 433 |
$notice_text = $reg_user['file-attachment-notice-text']; |
| 434 |
|
| 435 |
} else { |
| 436 |
|
| 437 |
$guest = get_option( 'wpsc-te-guest-user' ); |
| 438 |
$notice_text = $guest['file-attachment-notice-text']; |
| 439 |
|
| 440 |
} |
| 441 |
return $notice_text; |
| 442 |
} |
| 443 |
} |
| 444 |
endif; |
| 445 |
|
| 446 |
WPSC_Text_Editor::init(); |
| 447 |
|