| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_GS_General' ) ) : |
| 7 |
|
| 8 |
final class WPSC_GS_General { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// User interface. |
| 18 |
add_action( 'wp_ajax_wpsc_get_gs_general', array( __CLASS__, 'load_settings_ui' ) ); |
| 19 |
add_action( 'wp_ajax_wpsc_set_gs_general', array( __CLASS__, 'save_settings' ) ); |
| 20 |
add_action( 'wp_ajax_wpsc_reset_gs_general', array( __CLASS__, 'reset_settings' ) ); |
| 21 |
|
| 22 |
// Apply ticket status after customer or agent reply. |
| 23 |
add_action( 'wpsc_post_reply', array( __CLASS__, 'change_ticket_status' ) ); |
| 24 |
|
| 25 |
// after new agent role added. |
| 26 |
add_action( 'wpsc_after_add_agent_role', array( __CLASS__, 'after_add_new_role' ) ); |
| 27 |
|
| 28 |
// after delete agent role. |
| 29 |
add_action( 'wpsc_after_delete_agent_role', array( __CLASS__, 'after_delete_role' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Reset settings |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function reset() { |
| 38 |
|
| 39 |
$roles = get_option( 'wpsc-agent-roles' ); |
| 40 |
$allow_close_ticket = array( 'customer' ); |
| 41 |
$allow_create_ticket = array( 'registered-user' ); |
| 42 |
foreach ( $roles as $key => $role ) { |
| 43 |
$allow_close_ticket[] = $key; |
| 44 |
$allow_create_ticket[] = $key; |
| 45 |
} |
| 46 |
|
| 47 |
$general = apply_filters( |
| 48 |
'wpsc_gs_general', |
| 49 |
array( |
| 50 |
'ticket-status-after-customer-reply' => 3, |
| 51 |
'ticket-status-after-agent-reply' => 2, |
| 52 |
'close-ticket-status' => 4, |
| 53 |
'reply-form-position' => 'top', |
| 54 |
'default-date-format' => 'Y-m-d H:i:s', |
| 55 |
'ticket-alice' => 'Ticket #', |
| 56 |
'allow-close-ticket' => $allow_close_ticket, |
| 57 |
'allow-ar-thread-email' => array( 1, 2 ), |
| 58 |
'allow-create-ticket' => $allow_create_ticket, |
| 59 |
'allowed-search-fields' => array( 'id', 'customer', 'subject', 'threads' ), |
| 60 |
) |
| 61 |
); |
| 62 |
update_option( 'wpsc-gs-general', $general ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Settings user interface |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public static function load_settings_ui() { |
| 71 |
|
| 72 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 73 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 74 |
} |
| 75 |
$statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results']; |
| 76 |
$settings = get_option( 'wpsc-gs-general', array() ); |
| 77 |
$roles = get_option( 'wpsc-agent-roles' ); |
| 78 |
$visibility = array( |
| 79 |
'registered-user' => esc_attr__( 'Registered user', 'supportcandy' ), |
| 80 |
'guest' => esc_attr__( 'Guest', 'supportcandy' ), |
| 81 |
); |
| 82 |
foreach ( $roles as $key => $role ) : |
| 83 |
$visibility[ $key ] = $role['label']; |
| 84 |
endforeach; |
| 85 |
|
| 86 |
$allow_close_ticket = array( |
| 87 |
'customer' => esc_attr__( 'Customer', 'supportcandy' ), |
| 88 |
); |
| 89 |
foreach ( $roles as $key => $role ) : |
| 90 |
$allow_close_ticket[ $key ] = $role['label']; |
| 91 |
endforeach;?> |
| 92 |
|
| 93 |
<form action="#" onsubmit="return false;" class="wpsc-frm-gs-general"> |
| 94 |
<div class="wpsc-dock-container"> |
| 95 |
<?php |
| 96 |
printf( |
| 97 |
/* translators: Click here to see the documentation */ |
| 98 |
esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 99 |
'<a href="https://supportcandy.net/docs/general-settings/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 100 |
); |
| 101 |
?> |
| 102 |
</div> |
| 103 |
<div class="wpsc-input-group"> |
| 104 |
<div class="label-container"> |
| 105 |
<label for=""><?php esc_attr_e( 'Ticket status after customer reply', 'supportcandy' ); ?></label> |
| 106 |
</div> |
| 107 |
<select id="wpsc-tsacr" name="ticket-status-after-customer-reply"> |
| 108 |
<option value="0"><?php esc_attr_e( 'Do not change', 'supportcandy' ); ?></option> |
| 109 |
<?php |
| 110 |
foreach ( $statuses as $status ) : |
| 111 |
?> |
| 112 |
<option <?php selected( $status->id, $settings['ticket-status-after-customer-reply'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 113 |
<?php |
| 114 |
endforeach; |
| 115 |
?> |
| 116 |
</select> |
| 117 |
<script> |
| 118 |
jQuery('#wpsc-tsacr').selectWoo({ |
| 119 |
allowClear: true, |
| 120 |
placeholder: "" |
| 121 |
}); |
| 122 |
</script> |
| 123 |
</div> |
| 124 |
<div class="wpsc-input-group"> |
| 125 |
<div class="label-container"> |
| 126 |
<label for=""><?php esc_attr_e( 'Ticket status after agent reply', 'supportcandy' ); ?></label> |
| 127 |
</div> |
| 128 |
<select id="wpsc-tsaar" name="ticket-status-after-agent-reply"> |
| 129 |
<option value="0"><?php esc_attr_e( 'Do not change', 'supportcandy' ); ?></option> |
| 130 |
<?php |
| 131 |
foreach ( $statuses as $status ) : |
| 132 |
?> |
| 133 |
<option <?php selected( $status->id, $settings['ticket-status-after-agent-reply'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 134 |
<?php |
| 135 |
endforeach; |
| 136 |
?> |
| 137 |
</select> |
| 138 |
<script> |
| 139 |
jQuery('#wpsc-tsaar').selectWoo({ |
| 140 |
allowClear: true, |
| 141 |
placeholder: "" |
| 142 |
}); |
| 143 |
</script> |
| 144 |
</div> |
| 145 |
<div class="wpsc-input-group"> |
| 146 |
<div class="label-container"> |
| 147 |
<label for=""><?php esc_attr_e( 'Close ticket status', 'supportcandy' ); ?></label> |
| 148 |
</div> |
| 149 |
<select id="wpsc-cts" name="close-ticket-status"> |
| 150 |
<?php |
| 151 |
foreach ( $statuses as $status ) : |
| 152 |
?> |
| 153 |
<option <?php selected( $status->id, $settings['close-ticket-status'] ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 154 |
<?php |
| 155 |
endforeach; |
| 156 |
?> |
| 157 |
</select> |
| 158 |
<script> |
| 159 |
jQuery('#wpsc-cts').selectWoo({ |
| 160 |
allowClear: true, |
| 161 |
placeholder: "" |
| 162 |
}); |
| 163 |
</script> |
| 164 |
</div> |
| 165 |
<div class="wpsc-input-group"> |
| 166 |
<div class="label-container"> |
| 167 |
<label for=""><?php esc_attr_e( 'Reply form position', 'supportcandy' ); ?></label> |
| 168 |
</div> |
| 169 |
<select id="wpsc-rfp" name="reply-form-position"> |
| 170 |
<option <?php selected( $settings['reply-form-position'], 'top' ); ?> value="top"><?php esc_attr_e( 'Top', 'supportcandy' ); ?></option> |
| 171 |
<option <?php selected( $settings['reply-form-position'], 'bottom' ); ?> value="bottom"><?php esc_attr_e( 'Bottom', 'supportcandy' ); ?></option> |
| 172 |
</select> |
| 173 |
</div> |
| 174 |
<div class="wpsc-input-group"> |
| 175 |
<div class="label-container"> |
| 176 |
<label for=""><?php esc_attr_e( 'Default date format', 'supportcandy' ); ?></label> |
| 177 |
</div> |
| 178 |
<input id="wpsc-ddf" type="text" name="default-date-format" value="<?php echo esc_attr( $settings['default-date-format'] ); ?>"> |
| 179 |
</div> |
| 180 |
<div class="wpsc-input-group"> |
| 181 |
<div class="label-container"> |
| 182 |
<label for=""><?php esc_attr_e( 'Ticket alice', 'supportcandy' ); ?></label> |
| 183 |
</div> |
| 184 |
<input id="wpsc-ta" type="text" name="ticket-alice" value="<?php echo esc_attr( $settings['ticket-alice'] ); ?>"> |
| 185 |
</div> |
| 186 |
<div class="wpsc-input-group"> |
| 187 |
<div class="label-container"> |
| 188 |
<label for=""><?php esc_attr_e( 'Allow close ticket', 'supportcandy' ); ?></label> |
| 189 |
</div> |
| 190 |
<select id="wpsc-allow-close-ticket" multiple name="allow-close-ticket[]"> |
| 191 |
<?php |
| 192 |
foreach ( $allow_close_ticket as $key => $role ) : |
| 193 |
$selected = in_array( $key, $settings['allow-close-ticket'] ) ? 'selected' : '' |
| 194 |
?> |
| 195 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role ); ?></option> |
| 196 |
<?php |
| 197 |
endforeach; |
| 198 |
?> |
| 199 |
</select> |
| 200 |
<script>jQuery('#wpsc-allow-close-ticket').selectWoo();</script> |
| 201 |
</div> |
| 202 |
<div class="wpsc-input-group"> |
| 203 |
<div class="label-container"> |
| 204 |
<label for=""><?php esc_attr_e( 'Allow create new ticket', 'supportcandy' ); ?></label> |
| 205 |
</div> |
| 206 |
<select id="wpsc-allow-create-ticket" multiple name="allow-create-ticket[]"> |
| 207 |
<?php |
| 208 |
foreach ( $visibility as $key => $role ) : |
| 209 |
$selected = in_array( $key, $settings['allow-create-ticket'] ) ? 'selected' : '' |
| 210 |
?> |
| 211 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role ); ?></option> |
| 212 |
<?php |
| 213 |
endforeach; |
| 214 |
?> |
| 215 |
</select> |
| 216 |
<script>jQuery('#wpsc-allow-create-ticket').selectWoo();</script> |
| 217 |
</div> |
| 218 |
<div class="wpsc-input-group"> |
| 219 |
<div class="label-container"> |
| 220 |
<label for=""><?php esc_attr_e( 'Thread email visibility', 'supportcandy' ); ?></label> |
| 221 |
</div> |
| 222 |
<select multiple id="wpsc-select-agent-roles" name="allow-ar-thread-email[]" placeholder="search agent roles"> |
| 223 |
<?php |
| 224 |
foreach ( $roles as $key => $role ) : |
| 225 |
?> |
| 226 |
<option <?php echo in_array( $key, $settings['allow-ar-thread-email'] ) ? 'selected="selected"' : ''; ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option> |
| 227 |
<?php |
| 228 |
endforeach; |
| 229 |
?> |
| 230 |
</select> |
| 231 |
</div> |
| 232 |
<div class="wpsc-input-group"> |
| 233 |
<div class="label-container"> |
| 234 |
<label for=""><?php esc_attr_e( 'Select allowed search fields', 'supportcandy' ); ?></label> |
| 235 |
</div> |
| 236 |
<select multiple id="wpsc-select-allowed-search-fields" name="allowed-search-fields[]"> |
| 237 |
<option <?php echo in_array( 'threads', $settings['allowed-search-fields'] ) ? 'selected' : ''; ?> value="threads"><?php esc_attr_e( 'Threads', 'supportcandy' ); ?></option> |
| 238 |
<?php |
| 239 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 240 |
if ( ! ( property_exists( $cf->type, 'is_search' ) && $cf->type::$is_search ) || $cf->field == 'usergroup' ) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
$selected = in_array( $cf->slug, $settings['allowed-search-fields'] ) ? 'selected' : ''; |
| 244 |
?> |
| 245 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option> |
| 246 |
<?php |
| 247 |
} |
| 248 |
?> |
| 249 |
</select> |
| 250 |
</div> |
| 251 |
<script> |
| 252 |
jQuery('#wpsc-select-agent-roles, #wpsc-select-allowed-search-fields').selectWoo({ |
| 253 |
allowClear: false, |
| 254 |
placeholder: "" |
| 255 |
}); |
| 256 |
</script> |
| 257 |
<?php do_action( 'wpsc_gs_general' ); ?> |
| 258 |
<input type="hidden" name="action" value="wpsc_set_gs_general"> |
| 259 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_gs_general' ) ); ?>"> |
| 260 |
</form> |
| 261 |
<div class="setting-footer-actions"> |
| 262 |
<button |
| 263 |
class="wpsc-button normal primary margin-right" |
| 264 |
onclick="wpsc_set_gs_general(this);"> |
| 265 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button> |
| 266 |
<button |
| 267 |
class="wpsc-button normal secondary" |
| 268 |
onclick="wpsc_reset_gs_general(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_gs_general' ) ); ?>');"> |
| 269 |
<?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button> |
| 270 |
</div> |
| 271 |
<?php |
| 272 |
wp_die(); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Save settings |
| 277 |
* |
| 278 |
* @return void |
| 279 |
*/ |
| 280 |
public static function save_settings() { |
| 281 |
|
| 282 |
if ( check_ajax_referer( 'wpsc_set_gs_general', '_ajax_nonce', false ) != 1 ) { |
| 283 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 284 |
} |
| 285 |
|
| 286 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 287 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 288 |
} |
| 289 |
|
| 290 |
$general = apply_filters( |
| 291 |
'wpsc_set_gs_general', |
| 292 |
array( |
| 293 |
'ticket-status-after-customer-reply' => isset( $_POST['ticket-status-after-customer-reply'] ) ? intval( $_POST['ticket-status-after-customer-reply'] ) : '', |
| 294 |
'ticket-status-after-agent-reply' => isset( $_POST['ticket-status-after-agent-reply'] ) ? intval( $_POST['ticket-status-after-agent-reply'] ) : '', |
| 295 |
'close-ticket-status' => isset( $_POST['close-ticket-status'] ) ? intval( $_POST['close-ticket-status'] ) : 4, |
| 296 |
'reply-form-position' => isset( $_POST['reply-form-position'] ) ? sanitize_text_field( wp_unslash( $_POST['reply-form-position'] ) ) : 'top', |
| 297 |
'default-date-format' => isset( $_POST['default-date-format'] ) ? sanitize_text_field( wp_unslash( $_POST['default-date-format'] ) ) : 'Y-m-d', |
| 298 |
'ticket-alice' => isset( $_POST['ticket-alice'] ) ? sanitize_text_field( wp_unslash( $_POST['ticket-alice'] ) ) : 'Ticket #', |
| 299 |
'allow-close-ticket' => isset( $_POST['allow-close-ticket'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-close-ticket'] ) ) ) : array(), |
| 300 |
'allow-ar-thread-email' => isset( $_POST['allow-ar-thread-email'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-ar-thread-email'] ) ) ) : array(), |
| 301 |
'allow-create-ticket' => isset( $_POST['allow-create-ticket'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allow-create-ticket'] ) ) ) : array(), |
| 302 |
'allowed-search-fields' => isset( $_POST['allowed-search-fields'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['allowed-search-fields'] ) ) ) : array(), |
| 303 |
) |
| 304 |
); |
| 305 |
update_option( 'wpsc-gs-general', $general ); |
| 306 |
wp_die(); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Reset settings to default |
| 311 |
* |
| 312 |
* @return void |
| 313 |
*/ |
| 314 |
public static function reset_settings() { |
| 315 |
|
| 316 |
if ( check_ajax_referer( 'wpsc_reset_gs_general', '_ajax_nonce', false ) != 1 ) { |
| 317 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 318 |
} |
| 319 |
|
| 320 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 321 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 322 |
} |
| 323 |
self::reset(); |
| 324 |
wp_die(); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Change status after agent or customer reply |
| 329 |
* |
| 330 |
* @param WPSC_Thread $thread - thread object. |
| 331 |
* @return void |
| 332 |
*/ |
| 333 |
public static function change_ticket_status( $thread ) { |
| 334 |
|
| 335 |
$gs = get_option( 'wpsc-gs-general' ); |
| 336 |
$reply_profile = WPSC_Individual_Ticket::$reply_profile; |
| 337 |
|
| 338 |
if ( $reply_profile == 'agent' ) { |
| 339 |
|
| 340 |
if ( ! $gs['ticket-status-after-agent-reply'] ) { |
| 341 |
return; |
| 342 |
} |
| 343 |
$prev = $thread->ticket->status->id; |
| 344 |
$new = intval( $gs['ticket-status-after-agent-reply'] ) ? $gs['ticket-status-after-agent-reply'] : $prev; |
| 345 |
if ( $prev == $new ) { |
| 346 |
return; |
| 347 |
} |
| 348 |
WPSC_Individual_Ticket::change_status( $prev, $new, 0 ); // customer id given as '0' because this is system changing the status based on settings. |
| 349 |
|
| 350 |
} else { |
| 351 |
|
| 352 |
if ( ! $gs['ticket-status-after-customer-reply'] ) { |
| 353 |
return; |
| 354 |
} |
| 355 |
$prev = $thread->ticket->status->id; |
| 356 |
$new = intval( $gs['ticket-status-after-customer-reply'] ) ? $gs['ticket-status-after-customer-reply'] : $prev; |
| 357 |
if ( $prev == $new ) { |
| 358 |
return; |
| 359 |
} |
| 360 |
WPSC_Individual_Ticket::change_status( $prev, $new, 0 ); // customer id given as '0' because this is system changing the status based on settings. |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* After new agent role added add that role in allow create ticket and thread visibility settings |
| 366 |
* |
| 367 |
* @param integer $role_id - agent role id. |
| 368 |
* @return void |
| 369 |
*/ |
| 370 |
public static function after_add_new_role( $role_id ) { |
| 371 |
|
| 372 |
$gs = get_option( 'wpsc-gs-general' ); |
| 373 |
$allow_ticket = $gs['allow-create-ticket']; |
| 374 |
$allow_thread = $gs['allow-ar-thread-email']; |
| 375 |
$allow_close = $gs['allow-close-ticket']; |
| 376 |
|
| 377 |
$allow_ticket[] = $role_id; |
| 378 |
$allow_thread[] = $role_id; |
| 379 |
$allow_close[] = $role_id; |
| 380 |
|
| 381 |
$gs['allow-create-ticket'] = $allow_ticket; |
| 382 |
$gs['allow-ar-thread-email'] = $allow_thread; |
| 383 |
$gs['allow-close-ticket'] = $allow_close; |
| 384 |
update_option( 'wpsc-gs-general', $gs ); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* After agent role deleted remove that role in allow create ticket and thread visibility settings |
| 389 |
* |
| 390 |
* @param integer $role_id - agent role id. |
| 391 |
* @return void |
| 392 |
*/ |
| 393 |
public static function after_delete_role( $role_id ) { |
| 394 |
|
| 395 |
$gs = get_option( 'wpsc-gs-general' ); |
| 396 |
$allow_ticket = $gs['allow-create-ticket']; |
| 397 |
$allow_thread = $gs['allow-ar-thread-email']; |
| 398 |
$allow_close = $gs['allow-close-ticket']; |
| 399 |
|
| 400 |
if ( in_array( $role_id, $allow_ticket ) ) { |
| 401 |
unset( $allow_ticket[ $role_id ] ); |
| 402 |
} |
| 403 |
if ( in_array( $role_id, $allow_thread ) ) { |
| 404 |
unset( $allow_thread[ $role_id ] ); |
| 405 |
} |
| 406 |
if ( in_array( $role_id, $allow_close ) ) { |
| 407 |
unset( $allow_close[ $role_id ] ); |
| 408 |
} |
| 409 |
|
| 410 |
$gs['allow-create-ticket'] = $allow_ticket; |
| 411 |
$gs['allow-ar-thread-email'] = $allow_thread; |
| 412 |
$gs['allow-close-ticket'] = $allow_close; |
| 413 |
update_option( 'wpsc-gs-general', $gs ); |
| 414 |
} |
| 415 |
} |
| 416 |
endif; |
| 417 |
|
| 418 |
WPSC_GS_General::init(); |
| 419 |
|