| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ITW_Agentonly_Fields' ) ) : |
| 7 |
|
| 8 |
final class WPSC_ITW_Agentonly_Fields { |
| 9 |
|
| 10 |
/** |
| 11 |
* Ignore ticket custom field types for agentonly fields |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public static $ignore_cft = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize this class |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
|
| 24 |
// ignore cft. |
| 25 |
add_action( 'init', array( __CLASS__, 'ignore_cft' ) ); |
| 26 |
|
| 27 |
// get edit agentonly fields. |
| 28 |
add_action( 'wp_ajax_wpsc_it_get_edit_agentonly_fields', array( __CLASS__, 'it_get_edit_agentonly_fields' ) ); |
| 29 |
add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_agentonly_fields', array( __CLASS__, 'it_get_edit_agentonly_fields' ) ); |
| 30 |
add_action( 'wp_ajax_wpsc_it_set_edit_agentonly_fields', array( __CLASS__, 'it_set_edit_agentonly_fields' ) ); |
| 31 |
add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_agentonly_fields', array( __CLASS__, 'it_set_edit_agentonly_fields' ) ); |
| 32 |
|
| 33 |
// agentonly fields. |
| 34 |
add_action( 'wp_ajax_wpsc_get_tw_agentonly_fields', array( __CLASS__, 'get_tw_agentonly_fields' ) ); |
| 35 |
add_action( 'wp_ajax_wpsc_set_tw_agentonly_fields', array( __CLASS__, 'set_tw_agentonly_fields' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Set ignore custom field types for ticket fields |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public static function ignore_cft() { |
| 44 |
|
| 45 |
self::$ignore_cft = apply_filters( 'wpsc_ignore_edit_agentonly_field_cft', array( 'cf_html' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Prints body of current widget |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
/** |
| 54 |
* Prints body of current widget |
| 55 |
* |
| 56 |
* @param WPSC_Ticket $ticket - ticket object. |
| 57 |
* @param array $settings - widget settings. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public static function print_widget( $ticket, $settings ) { |
| 61 |
|
| 62 |
$current_user = WPSC_Current_User::$current_user; |
| 63 |
if ( ! ( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$flag = false?> |
| 68 |
|
| 69 |
<div class="wpsc-it-widget wpsc-itw-agentonly-fields"> |
| 70 |
<div class="wpsc-widget-header"> |
| 71 |
<h2> |
| 72 |
<?php |
| 73 |
$settings_title = $settings['title'] ? WPSC_Translations::get( 'wpsc-twt-agentonly-fields', stripslashes( $settings['title'] ) ) : stripslashes( $settings['title'] ); |
| 74 |
echo esc_attr( $settings_title ) |
| 75 |
?> |
| 76 |
</h2> |
| 77 |
<?php |
| 78 |
if ( $ticket->is_active && WPSC_Individual_Ticket::$view_profile == 'agent' && WPSC_Individual_Ticket::has_ticket_cap( 'caof' ) ) : |
| 79 |
?> |
| 80 |
<span onclick="wpsc_it_get_edit_agentonly_fields(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_agentonly_fields' ) ); ?>')"><?php WPSC_Icons::get( 'edit' ); ?></span> |
| 81 |
<?php |
| 82 |
endif |
| 83 |
?> |
| 84 |
<span class="wpsc-itw-toggle" data-widget="wpsc-itw-agentonly-fields"><?php WPSC_Icons::get( 'chevron-up' ); ?></span> |
| 85 |
</div> |
| 86 |
<div class="wpsc-widget-body"> |
| 87 |
<?php |
| 88 |
$cft_exclude = apply_filters( 'wpsc_it_widget_exclude_cft', array() ); |
| 89 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) : |
| 90 |
if ( in_array( $cf->type::$slug, $cft_exclude ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
if ( $cf->field == 'agentonly' && ! $cf->type::$is_default && ( $ticket->{$cf->slug} || ( $cf->type::$slug == 'cf_number' && is_numeric( $ticket->{$cf->slug} ) ) ) ) : |
| 94 |
if ( $cf->type::$is_date && ! is_object( $ticket->{$cf->slug} ) ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
$flag = ! $flag ? true : $flag |
| 98 |
?> |
| 99 |
<div class="info-list-item"> |
| 100 |
<div class="info-label"><?php echo esc_attr( stripslashes( $cf->name ) ); ?>:</div> |
| 101 |
<div class="info-val"> |
| 102 |
<?php |
| 103 |
$cf->type::print_widget_ticket_field_val( $cf, $ticket ); |
| 104 |
?> |
| 105 |
</div> |
| 106 |
</div> |
| 107 |
<?php |
| 108 |
endif; |
| 109 |
endforeach; |
| 110 |
if ( ! $flag ) : |
| 111 |
?> |
| 112 |
<div class="wpsc-widget-default"><?php esc_attr_e( 'Not Applicable', 'supportcandy' ); ?></div> |
| 113 |
<?php |
| 114 |
endif; |
| 115 |
do_action( 'wpsc_itw_agent_fields', $ticket ) |
| 116 |
?> |
| 117 |
</div> |
| 118 |
</div> |
| 119 |
<?php |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get edit agentonly fields |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public static function it_get_edit_agentonly_fields() { |
| 128 |
|
| 129 |
if ( check_ajax_referer( 'wpsc_it_get_edit_agentonly_fields', '_ajax_nonce', false ) != 1 ) { |
| 130 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 131 |
} |
| 132 |
|
| 133 |
WPSC_Individual_Ticket::load_current_ticket(); |
| 134 |
|
| 135 |
$current_user = WPSC_Current_User::$current_user; |
| 136 |
if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'caof' ) ) ) { |
| 137 |
wp_send_json_error( 'Something went wrong!', 401 ); |
| 138 |
} |
| 139 |
|
| 140 |
$ticket = WPSC_Individual_Ticket::$ticket; |
| 141 |
|
| 142 |
$widgets = get_option( 'wpsc-ticket-widget' ); |
| 143 |
$title = $widgets['agentonly-fields']['title']; |
| 144 |
|
| 145 |
ob_start() |
| 146 |
?> |
| 147 |
<form action="#" onsubmit="return false;" class="change-agentonly-fields"> |
| 148 |
|
| 149 |
<div class="wpsc-input-group" style="align-items:flex-end;"> |
| 150 |
<input type="text" id="cft_search" placeholder="<?php esc_attr_e( 'Search...', 'supportcandy' ); ?>" autocomplete="off" style="max-width:200px;"> |
| 151 |
</div> |
| 152 |
<?php |
| 153 |
|
| 154 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 155 |
|
| 156 |
if ( |
| 157 |
! class_exists( $cf->type ) || |
| 158 |
$cf->type::$is_default || |
| 159 |
$cf->field != 'agentonly' || |
| 160 |
in_array( $cf->type::$slug, self::$ignore_cft ) |
| 161 |
) { |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
$cf->type::print_edit_ticket_cf( $cf, $ticket ); |
| 166 |
} |
| 167 |
|
| 168 |
do_action( 'wpsc_get_tw_agentonly_fields_body', $ticket ) |
| 169 |
?> |
| 170 |
|
| 171 |
<input type="hidden" name="action" value="wpsc_it_set_edit_agentonly_fields"> |
| 172 |
<input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>"> |
| 173 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_agentonly_fields' ) ); ?>"> |
| 174 |
|
| 175 |
<script> |
| 176 |
jQuery('#cft_search').keyup(function(e) { |
| 177 |
var val = jQuery(this).val().trim(); |
| 178 |
var regex = new RegExp(val, "i"); |
| 179 |
jQuery('.change-agentonly-fields .wpsc-tff').each(function(){ |
| 180 |
var name = jQuery(this).find('.wpsc-tff-label').text(); |
| 181 |
if (name.search(regex) < 0) { |
| 182 |
jQuery(this).hide(); |
| 183 |
} else { |
| 184 |
jQuery(this).show(); |
| 185 |
} |
| 186 |
}) |
| 187 |
}); |
| 188 |
</script> |
| 189 |
|
| 190 |
</form> |
| 191 |
<?php |
| 192 |
|
| 193 |
do_action( 'wpsc_get_tw_agentonly_fields_footer', $ticket ); |
| 194 |
$body = ob_get_clean(); |
| 195 |
|
| 196 |
ob_start(); |
| 197 |
?> |
| 198 |
<button class="wpsc-button small primary" onclick="wpsc_it_set_edit_agentonly_fields(this, <?php echo esc_attr( $ticket->id ); ?>);"> |
| 199 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 200 |
</button> |
| 201 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 202 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 203 |
</button> |
| 204 |
<?php |
| 205 |
$footer = ob_get_clean(); |
| 206 |
|
| 207 |
$response = array( |
| 208 |
'title' => $title, |
| 209 |
'body' => $body, |
| 210 |
'footer' => $footer, |
| 211 |
); |
| 212 |
wp_send_json( $response ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Change agentonly fields |
| 217 |
* |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public static function it_set_edit_agentonly_fields() { |
| 221 |
|
| 222 |
if ( check_ajax_referer( 'wpsc_it_set_edit_agentonly_fields', '_ajax_nonce', false ) != 1 ) { |
| 223 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 224 |
} |
| 225 |
|
| 226 |
WPSC_Individual_Ticket::load_current_ticket(); |
| 227 |
|
| 228 |
$current_user = WPSC_Current_User::$current_user; |
| 229 |
if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'caof' ) ) ) { |
| 230 |
wp_send_json_error( 'Something went wrong!', 401 ); |
| 231 |
} |
| 232 |
|
| 233 |
$ticket = clone WPSC_Individual_Ticket::$ticket; |
| 234 |
if ( ! $ticket->is_active ) { |
| 235 |
wp_send_json_error( 'Something went wrong!', 400 ); |
| 236 |
} |
| 237 |
|
| 238 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 239 |
|
| 240 |
if ( $cf->type::$is_default || $cf->field != 'agentonly' ) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
$ticket = $cf->type::set_edit_ticket_cf( $cf, $ticket ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( $ticket != WPSC_Individual_Ticket::$ticket ) { |
| 247 |
|
| 248 |
$ticket->date_updated = new DateTime(); |
| 249 |
$ticket->save(); |
| 250 |
|
| 251 |
do_action( 'wpsc_change_agentonly_fields', WPSC_Individual_Ticket::$ticket, $ticket ); |
| 252 |
} |
| 253 |
|
| 254 |
wp_die(); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Get agentonly fields |
| 259 |
*/ |
| 260 |
public static function get_tw_agentonly_fields() { |
| 261 |
|
| 262 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 263 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 264 |
} |
| 265 |
|
| 266 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 267 |
$agentonly_fields = $ticket_widgets['agentonly-fields']; |
| 268 |
$title = $agentonly_fields['title']; |
| 269 |
$roles = get_option( 'wpsc-agent-roles', array() ); |
| 270 |
ob_start(); |
| 271 |
?> |
| 272 |
|
| 273 |
<form action="#" onsubmit="return false;" class="wpsc-frm-edit-agentonly-fields"> |
| 274 |
<div class="wpsc-input-group"> |
| 275 |
<div class="label-container"> |
| 276 |
<label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label> |
| 277 |
</div> |
| 278 |
<input name="label" type="text" value="<?php echo esc_attr( $agentonly_fields['title'] ); ?>" autocomplete="off"> |
| 279 |
</div> |
| 280 |
<div class="wpsc-input-group"> |
| 281 |
<div class="label-container"> |
| 282 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 283 |
</div> |
| 284 |
<select name="is_enable"> |
| 285 |
<option <?php selected( $agentonly_fields['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 286 |
<option <?php selected( $agentonly_fields['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 287 |
</select> |
| 288 |
</div> |
| 289 |
<div class="wpsc-input-group"> |
| 290 |
<div class="label-container"> |
| 291 |
<label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label> |
| 292 |
</div> |
| 293 |
<select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent..."> |
| 294 |
<?php |
| 295 |
foreach ( $roles as $key => $role ) : |
| 296 |
$selected = in_array( $key, $agentonly_fields['allowed-agent-roles'] ) ? 'selected="selected"' : '' |
| 297 |
?> |
| 298 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option> |
| 299 |
<?php |
| 300 |
endforeach; |
| 301 |
?> |
| 302 |
</select> |
| 303 |
</div> |
| 304 |
<script> |
| 305 |
jQuery('#wpsc-select-agents').selectWoo({ |
| 306 |
allowClear: false, |
| 307 |
placeholder: "" |
| 308 |
}); |
| 309 |
</script> |
| 310 |
<?php do_action( 'wpsc_get_agentonly_fields_body' ); ?> |
| 311 |
<input type="hidden" name="action" value="wpsc_set_tw_agentonly_fields"> |
| 312 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_agentonly_fields' ) ); ?>"> |
| 313 |
</form> |
| 314 |
<?php |
| 315 |
$body = ob_get_clean(); |
| 316 |
|
| 317 |
ob_start(); |
| 318 |
?> |
| 319 |
<button class="wpsc-button small primary" onclick="wpsc_set_tw_agentonly_fields(this);"> |
| 320 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 321 |
</button> |
| 322 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 323 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 324 |
</button> |
| 325 |
<?php |
| 326 |
do_action( 'wpsc_get_tw_agentonly_fields_widget_footer' ); |
| 327 |
$footer = ob_get_clean(); |
| 328 |
|
| 329 |
$response = array( |
| 330 |
'title' => $title, |
| 331 |
'body' => $body, |
| 332 |
'footer' => $footer, |
| 333 |
); |
| 334 |
wp_send_json( $response ); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Set agentonly fields |
| 339 |
*/ |
| 340 |
public static function set_tw_agentonly_fields() { |
| 341 |
|
| 342 |
if ( check_ajax_referer( 'wpsc_set_tw_agentonly_fields', '_ajax_nonce', false ) != 1 ) { |
| 343 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 344 |
} |
| 345 |
|
| 346 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 347 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 348 |
} |
| 349 |
|
| 350 |
$label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; |
| 351 |
if ( ! $label ) { |
| 352 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 353 |
} |
| 354 |
|
| 355 |
$is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 356 |
$agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array(); |
| 357 |
|
| 358 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 359 |
|
| 360 |
$ticket_widgets['agentonly-fields']['title'] = $label; |
| 361 |
$ticket_widgets['agentonly-fields']['is_enable'] = $is_enable; |
| 362 |
$ticket_widgets['agentonly-fields']['allowed-agent-roles'] = $agents; |
| 363 |
update_option( 'wpsc-ticket-widget', $ticket_widgets ); |
| 364 |
|
| 365 |
// remove string translations. |
| 366 |
WPSC_Translations::remove( 'wpsc-twt-agentonly-fields' ); |
| 367 |
WPSC_Translations::add( 'wpsc-twt-agentonly-fields', stripslashes( $label ) ); |
| 368 |
wp_die(); |
| 369 |
} |
| 370 |
} |
| 371 |
endif; |
| 372 |
|
| 373 |
WPSC_ITW_Agentonly_Fields::init(); |
| 374 |
|