| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ITW_Ticket_Info' ) ) : |
| 7 |
|
| 8 |
final class WPSC_ITW_Ticket_Info { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// Ticket Info. |
| 18 |
add_action( 'wp_ajax_wpsc_get_tw_ticket_info', array( __CLASS__, 'get_tw_ticket_info' ) ); |
| 19 |
add_action( 'wp_ajax_wpsc_set_tw_ticket_info', array( __CLASS__, 'set_tw_ticket_info' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Prints body of current widget |
| 24 |
* |
| 25 |
* @param WPSC_Ticket $ticket - ticket object. |
| 26 |
* @param array $settings - widget settings. |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public static function print_widget( $ticket, $settings ) { |
| 30 |
|
| 31 |
$current_user = WPSC_Current_User::$current_user; |
| 32 |
if ( ! ( |
| 33 |
WPSC_Individual_Ticket::$view_profile == 'agent' && |
| 34 |
in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) |
| 35 |
) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
self::render_widget( $ticket, $settings, true, false ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Prints body of current widget |
| 43 |
* |
| 44 |
* @param WPSC_Ticket $ticket - ticket object. |
| 45 |
* @param array $settings - widget settings. |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public static function print_archive_widget( $ticket, $settings ) { |
| 49 |
|
| 50 |
$current_user = WPSC_Current_User::$current_user; |
| 51 |
if ( ! ( |
| 52 |
WPSC_Individual_Archive_Ticket::$view_profile == 'agent' && |
| 53 |
in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) |
| 54 |
) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
self::render_widget( $ticket, $settings, false, true ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Render widget |
| 62 |
* |
| 63 |
* @param WPSC_Ticket|WPSC_Archive_Ticket $ticket - ticket object. |
| 64 |
* @param array $settings - widget settings. |
| 65 |
* @param bool $show_edit - show edit button. |
| 66 |
* @param bool $is_archive - is archive ticket. |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
private static function render_widget( $ticket, $settings, $show_edit, $is_archive ) { |
| 70 |
$title = $settings['title'] |
| 71 |
? WPSC_Translations::get( 'wpsc-twt-ticket-info', stripslashes( $settings['title'] ) ) |
| 72 |
: stripslashes( $settings['title'] ); |
| 73 |
|
| 74 |
global $current_user; |
| 75 |
?> |
| 76 |
<div class="wpsc-it-widget wpsc-itw-ticket-info"> |
| 77 |
<div class="wpsc-widget-header"> |
| 78 |
<h2><?php echo esc_attr( $title ); ?></h2> |
| 79 |
<span class="wpsc-itw-toggle" data-widget="wpsc-itw-ticket-info"><?php WPSC_Icons::get( 'chevron-up' ); ?></span> |
| 80 |
</div> |
| 81 |
<div class="wpsc-widget-body"> |
| 82 |
<div class="info-list-item"> |
| 83 |
<?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'ip_address' ); ?> |
| 84 |
<div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div> |
| 85 |
<div class="info-val"><?php $cf->type::print_widget_ticket_field_val( $cf, $ticket ); ?></div> |
| 86 |
</div> |
| 87 |
<div class="info-list-item"> |
| 88 |
<?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'source' ); ?> |
| 89 |
<div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div> |
| 90 |
<div class="info-val"><?php echo esc_attr( $cf->type::print_widget_ticket_field_val( $cf, $ticket ) ); ?></div> |
| 91 |
</div> |
| 92 |
<div class="info-list-item"> |
| 93 |
<?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'browser' ); ?> |
| 94 |
<div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div> |
| 95 |
<div class="info-val"><?php echo esc_attr( $cf->type::print_widget_ticket_field_val( $cf, $ticket ) ); ?></div> |
| 96 |
</div> |
| 97 |
<div class="info-list-item"> |
| 98 |
<?php $cf = WPSC_Custom_Field::get_cf_by_slug( 'os' ); ?> |
| 99 |
<div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div> |
| 100 |
<div class="info-val"><?php echo esc_attr( $cf->type::print_widget_ticket_field_val( $cf, $ticket ) ); ?></div> |
| 101 |
</div> |
| 102 |
<?php |
| 103 |
$misc = is_array( $ticket->misc ) ? $ticket->misc : array(); |
| 104 |
if ( ! empty( $misc['chat_session_id'] && $current_user->has_cap( 'manage_options' ) ) ) : |
| 105 |
$chat_session_url = admin_url( 'admin.php?page=wpsc-ai-chatbot&session_id=' . absint( $misc['chat_session_id'] ) ); |
| 106 |
?> |
| 107 |
<div class="info-list-item"> |
| 108 |
<div class="info-label"><?php esc_attr_e( 'Chat session', 'supportcandy' ); ?>:</div> |
| 109 |
<div class="info-val"><a class="wpsc-link" href="<?php echo esc_url( $chat_session_url ); ?>" target="_blank"><?php echo esc_html( $misc['chat_session_id'] ); ?></a></div> |
| 110 |
</div> |
| 111 |
<?php endif; ?> |
| 112 |
<?php do_action( 'wpsc_itw_ticket_info', $ticket ); ?> |
| 113 |
</div> |
| 114 |
</div> |
| 115 |
<?php |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get ticket info |
| 120 |
*/ |
| 121 |
public static function get_tw_ticket_info() { |
| 122 |
|
| 123 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 124 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 125 |
} |
| 126 |
|
| 127 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 128 |
$ticket_info = $ticket_widgets['ticket-info']; |
| 129 |
$title = $ticket_info['title']; |
| 130 |
$roles = get_option( 'wpsc-agent-roles', array() ); |
| 131 |
ob_start(); |
| 132 |
?> |
| 133 |
|
| 134 |
<form action="#" onsubmit="return false;" class="wpsc-frm-edit-ticket-info"> |
| 135 |
<div class="wpsc-input-group"> |
| 136 |
<div class="label-container"> |
| 137 |
<label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label> |
| 138 |
</div> |
| 139 |
<input name="label" type="text" value="<?php echo esc_attr( $ticket_info['title'] ); ?>" autocomplete="off"> |
| 140 |
</div> |
| 141 |
<div class="wpsc-input-group"> |
| 142 |
<div class="label-container"> |
| 143 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 144 |
</div> |
| 145 |
<select name="is_enable"> |
| 146 |
<option <?php selected( $ticket_info['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 147 |
<option <?php selected( $ticket_info['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 148 |
</select> |
| 149 |
</div> |
| 150 |
<div class="wpsc-input-group"> |
| 151 |
<div class="label-container"> |
| 152 |
<label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label> |
| 153 |
</div> |
| 154 |
<select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent..."> |
| 155 |
<?php |
| 156 |
foreach ( $roles as $key => $role ) : |
| 157 |
$selected = in_array( $key, $ticket_info['allowed-agent-roles'] ) ? 'selected="selected"' : '' |
| 158 |
?> |
| 159 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option> |
| 160 |
<?php |
| 161 |
endforeach; |
| 162 |
?> |
| 163 |
</select> |
| 164 |
</div> |
| 165 |
<script> |
| 166 |
jQuery('#wpsc-select-agents').selectWoo({ |
| 167 |
allowClear: false, |
| 168 |
placeholder: "" |
| 169 |
}); |
| 170 |
</script> |
| 171 |
<?php do_action( 'wpsc_get_ticket_info_body' ); ?> |
| 172 |
<input type="hidden" name="action" value="wpsc_set_tw_ticket_info"> |
| 173 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_ticket_info' ) ); ?>"> |
| 174 |
</form> |
| 175 |
<?php |
| 176 |
$body = ob_get_clean(); |
| 177 |
|
| 178 |
ob_start(); |
| 179 |
?> |
| 180 |
<button class="wpsc-button small primary" onclick="wpsc_set_tw_ticket_info(this);"> |
| 181 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 182 |
</button> |
| 183 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 184 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 185 |
</button> |
| 186 |
<?php |
| 187 |
do_action( 'wpsc_get_tw_ticket_info_widget_footer' ); |
| 188 |
$footer = ob_get_clean(); |
| 189 |
|
| 190 |
$response = array( |
| 191 |
'title' => $title, |
| 192 |
'body' => $body, |
| 193 |
'footer' => $footer, |
| 194 |
); |
| 195 |
wp_send_json( $response ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set ticket info |
| 200 |
*/ |
| 201 |
public static function set_tw_ticket_info() { |
| 202 |
|
| 203 |
if ( check_ajax_referer( 'wpsc_set_tw_ticket_info', '_ajax_nonce', false ) != 1 ) { |
| 204 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 208 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 209 |
} |
| 210 |
|
| 211 |
$label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; |
| 212 |
if ( ! $label ) { |
| 213 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 214 |
} |
| 215 |
|
| 216 |
$is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 217 |
$agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array(); |
| 218 |
|
| 219 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 220 |
$ticket_widgets['ticket-info']['title'] = $label; |
| 221 |
$ticket_widgets['ticket-info']['is_enable'] = $is_enable; |
| 222 |
$ticket_widgets['ticket-info']['allowed-agent-roles'] = $agents; |
| 223 |
update_option( 'wpsc-ticket-widget', $ticket_widgets ); |
| 224 |
|
| 225 |
// remove string translations. |
| 226 |
WPSC_Translations::remove( 'wpsc-twt-ticket-info' ); |
| 227 |
WPSC_Translations::add( 'wpsc-twt-ticket-info', stripslashes( $label ) ); |
| 228 |
wp_die(); |
| 229 |
} |
| 230 |
} |
| 231 |
endif; |
| 232 |
|
| 233 |
WPSC_ITW_Ticket_Info::init(); |
| 234 |
|