PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.2
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / agent-settings / class-wpsc-agent-working-hrs.php

class-wpsc-agent-working-hrs.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.2, at includes/admin/agent-settings/class-wpsc-agent-working-hrs.php

1,049 lines 34.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Agent_Working_Hrs' ) ) :
7
8 final class WPSC_Agent_Working_Hrs {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // section.
18 add_action( 'wp_ajax_wpsc_get_agents_working_hrs', array( __CLASS__, 'get_agents_working_hrs' ) );
19
20 // get working hrs.
21 add_action( 'wp_ajax_wpsc_get_agents_wh_hrs', array( __CLASS__, 'get_agents_wh_hrs' ) );
22 add_action( 'wp_ajax_nopriv_wpsc_get_agents_wh_hrs', array( __CLASS__, 'get_agents_wh_hrs' ) );
23
24 // get exceptions list.
25 add_action( 'wp_ajax_wpsc_get_agents_wh_exceptions', array( __CLASS__, 'get_agents_wh_exceptions' ) );
26 add_action( 'wp_ajax_nopriv_wpsc_get_agents_wh_exceptions', array( __CLASS__, 'get_agents_wh_exceptions' ) );
27
28 // set working hrs.
29 add_action( 'wp_ajax_wpsc_set_agent_wh_hrs', array( __CLASS__, 'set_agent_wh_hrs' ) );
30 add_action( 'wp_ajax_nopriv_wpsc_set_agent_wh_hrs', array( __CLASS__, 'set_agent_wh_hrs' ) );
31
32 // get add exceptions.
33 add_action( 'wp_ajax_wpsc_get_add_agent_wh_exception', array( __CLASS__, 'get_add_agent_wh_exception' ) );
34 add_action( 'wp_ajax_nopriv_wpsc_get_add_agent_wh_exception', array( __CLASS__, 'get_add_agent_wh_exception' ) );
35
36 // set add exceptions.
37 add_action( 'wp_ajax_wpsc_set_add_agent_wh_exception', array( __CLASS__, 'set_add_agent_wh_exception' ) );
38 add_action( 'wp_ajax_nopriv_wpsc_set_add_agent_wh_exception', array( __CLASS__, 'set_add_agent_wh_exception' ) );
39
40 // get edit exceptions.
41 add_action( 'wp_ajax_wpsc_get_edit_agent_wh_exception', array( __CLASS__, 'get_edit_agent_wh_exception' ) );
42 add_action( 'wp_ajax_nopriv_wpsc_get_edit_agent_wh_exception', array( __CLASS__, 'get_edit_agent_wh_exception' ) );
43
44 // set edit exceptions.
45 add_action( 'wp_ajax_wpsc_set_edit_agent_wh_exception', array( __CLASS__, 'set_edit_agent_wh_exception' ) );
46 add_action( 'wp_ajax_nopriv_wpsc_set_edit_agent_wh_exception', array( __CLASS__, 'set_edit_agent_wh_exception' ) );
47
48 // delete exception.
49 add_action( 'wp_ajax_wpsc_delete_agent_wh_exception', array( __CLASS__, 'delete_agent_wh_exception' ) );
50 add_action( 'wp_ajax_nopriv_wpsc_delete_agent_wh_exception', array( __CLASS__, 'delete_agent_wh_exception' ) );
51 }
52
53 /**
54 * Agents working hrs
55 *
56 * @return void
57 */
58 public static function get_agents_working_hrs() {
59
60 if ( ! WPSC_Functions::is_site_admin() ) {
61 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
62 }
63
64 $args = array(
65 'meta_query' => array(
66 'relation' => 'AND',
67 array(
68 'slug' => 'is_active',
69 'compare' => '=',
70 'val' => 1,
71 ),
72 array(
73 'slug' => 'is_agentgroup',
74 'compare' => '=',
75 'val' => 0,
76 ),
77 ),
78 'items_per_page' => 1,
79 );
80 $agent = WPSC_Agent::find( $args )['results'][0];?>
81 <div class="wpsc-setting-header">
82 <h2><?php esc_attr_e( 'Working Hours', 'supportcandy' ); ?></h2>
83 </div>
84 <div class="wpsc-setting-filter-container">
85 <div class="setting-filter-item" style="width: 40%;">
86 <span class="label"><?php esc_attr_e( 'Agent', 'supportcandy' ); ?></span>
87 <select class="wpsc-agent-search">
88 <option value="<?php echo esc_attr( $agent->id ); ?>"><?php echo esc_attr( $agent->name ); ?></option>
89 </select>
90 </div>
91 </div>
92 <div class="wpsc-setting-tab-container" style="margin-top: 10px;">
93 <button class="tab working-hrs active" onclick="wpsc_get_agents_wh_hrs();"><?php esc_attr_e( 'Working Hours', 'supportcandy' ); ?></button>
94 <button class="tab exceptions" onclick="wpsc_get_agents_wh_exceptions();"><?php esc_attr_e( 'Exceptions', 'supportcandy' ); ?></button>
95 </div>
96 <div class="wpsc-setting-section-body"></div>
97 <script>
98 jQuery('select.wpsc-agent-search').selectWoo({
99 ajax: {
100 url: supportcandy.ajax_url,
101 dataType: 'json',
102 delay: 250,
103 data: function (params) {
104 return {
105 q: params.term, // search term
106 page: params.page,
107 action: 'wpsc_agent_autocomplete_admin_access',
108 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_agent_autocomplete_admin_access' ) ); ?>',
109 isMultiple: 1,
110 isAgentgroup: 0
111 };
112 },
113 processResults: function (data, params) {
114 var terms = [];
115 if ( data ) {
116 jQuery.each( data, function( id, text ) {
117 terms.push( { id: text.id, text: text.title } );
118 });
119 }
120 return {
121 results: terms
122 };
123 },
124 cache: true
125 },
126 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
127 minimumInputLength: 1,
128 placeholder: "",
129 });
130 jQuery('select.wpsc-agent-search').change(function(){
131 var agent_id = jQuery(this).val();
132 if (supportcandy.temp.agent_id === agent_id) return;
133 supportcandy.temp.agent_id = agent_id;
134 wpsc_get_agents_wh_hrs();
135 });
136 supportcandy.temp.agent_id = <?php echo esc_attr( $agent->id ); ?>;
137 </script>
138 <?php
139 wp_die();
140 }
141
142 /**
143 * Get agent working hrs
144 *
145 * @return void
146 */
147 public static function get_agents_wh_hrs() {
148
149 if ( check_ajax_referer( 'general', '_ajax_nonce', false ) != 1 ) {
150 wp_send_json_error( 'Unauthorised request!', 401 );
151 }
152
153 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
154 if ( ! $agent_id ) {
155 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
156 }
157
158 $agent = new WPSC_Agent( $agent_id );
159 if ( ! $agent->id ) {
160 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
161 }
162
163 $settings = get_option( 'wpsc-wh-settings', array() );
164
165 $current_user = WPSC_Current_User::$current_user;
166 if ( ! (
167 WPSC_Functions::is_site_admin() ||
168 (
169 $current_user->is_agent &&
170 $settings['allow-agent-modify-wh'] &&
171 $current_user->agent->id == $agent_id
172 )
173 ) ) {
174 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
175 }
176
177 $working_hrs = $agent->get_working_hrs();
178 ?>
179
180 <form action="#" onsubmit="return false;" class="wpsc-frm-agent-wh">
181 <div class="wpsc-dock-container">
182 <?php
183 printf(
184 /* translators: Click here to see the documentation */
185 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
186 '<a href="https://supportcandy.net/docs/working-hours/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
187 );
188 ?>
189 </div>
190 <table class="wpsc-working-hrs">
191 <?php
192 for ( $i = 1; $i <= 7; $i++ ) :
193 $start_time = $working_hrs[ $i ]->start_time;
194 $end_time = $working_hrs[ $i ]->end_time;
195 $style = $start_time == 'off' ? 'display: none;' : '';
196 ?>
197 <tr>
198 <td class="dayName"><?php echo esc_attr( WPSC_Functions::get_day_name( $i ) ); ?>:</td>
199 <td>
200 <select class="wpsc-wh-start-time" name="wh[<?php echo esc_attr( $i ); ?>][start_time]">
201 <?php WPSC_Working_Hour::get_start_time_slots( $start_time ); ?>
202 </select>
203 </td>
204 <td style="<?php echo esc_attr( $style ); ?>">-</td>
205 <td style="<?php echo esc_attr( $style ); ?>">
206 <select class="wpsc-wh-end-time" name="wh[<?php echo esc_attr( $i ); ?>][end_time]">
207 <?php WPSC_Working_Hour::get_end_time_slots( $start_time, $end_time ); ?>
208 </select>
209 </td>
210 </tr>
211 <?php
212 endfor;
213 ?>
214 </table>
215 <input type="hidden" name="action" value="wpsc_set_agent_wh_hrs">
216 <input type="hidden" name="agent_id" value="<?php echo esc_attr( $agent->id ); ?>">
217 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_agent_wh_hrs' ) ); ?>">
218 </form>
219 <div class="setting-footer-actions">
220 <button
221 class="wpsc-button normal primary margin-right"
222 onclick="wpsc_set_agent_wh_hrs(this);">
223 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
224 </button>
225 </div>
226 <script>
227 var end_times = [];
228 <?php
229 $current_slot = new DateTime( '2020-01-01 00:15:00' );
230 $second_last_slot = new DateTime( '2020-01-01 23:45:00' );
231 $last_slot = new DateTime( '2020-01-01 23:59:59' );
232
233 do {
234 $time = $current_slot->format( 'H:i:s' )
235 ?>
236 end_times.push({
237 val: '<?php echo esc_attr( $time ); ?>',
238 display_val: '<?php echo esc_attr( $current_slot->format( 'H:i' ) ); ?>',
239 });
240 <?php
241 if ( $current_slot == $second_last_slot ) {
242 $current_slot->add( new DateInterval( 'PT14M59S' ) );
243 } else {
244 $current_slot->add( new DateInterval( 'PT15M' ) );
245 }
246 } while ( $current_slot <= $last_slot );
247 ?>
248 supportcandy.temp.end_times = end_times;
249
250 // Change event
251 jQuery('.wpsc-wh-start-time').change(function(){
252 var start_time = jQuery(this).val();
253 var td1 = jQuery(this).parent().next();
254 var td2 = td1.next();
255 if (start_time === 'off') {
256 td1.hide();
257 td2.hide();
258 return;
259 } else {
260 td1.show();
261 td2.show();
262 }
263 var tempArr = start_time.split(":");
264 var startDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
265 var cmbEndTime = jQuery(this).closest('tr').find('.wpsc-wh-end-time');
266 cmbEndTime.find('option').remove();
267 jQuery.each(supportcandy.temp.end_times, function(index, end_time){
268 var tempArr = end_time.val.split(":");
269 var endDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
270 if (startDate < endDate) {
271 var obj = document.createElement('OPTION');
272 var displayVal = document.createTextNode(end_time.display_val);
273 obj.setAttribute("value", end_time.val);
274 obj.appendChild(displayVal);
275 cmbEndTime.append(obj);
276 }
277 });
278 });
279 </script>
280 <?php
281
282 wp_die();
283 }
284
285 /**
286 * Get agent working hour exceptions
287 *
288 * @return void
289 */
290 public static function get_agents_wh_exceptions() {
291
292 if ( check_ajax_referer( 'general', '_ajax_nonce', false ) != 1 ) {
293 wp_send_json_error( 'Unauthorised request!', 401 );
294 }
295
296 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
297 if ( ! $agent_id ) {
298 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
299 }
300
301 $agent = new WPSC_Agent( $agent_id );
302 if ( ! $agent->id ) {
303 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
304 }
305
306 $settings = get_option( 'wpsc-wh-settings', array() );
307
308 $current_user = WPSC_Current_User::$current_user;
309 if ( ! (
310 WPSC_Functions::is_site_admin() ||
311 (
312 $current_user->is_agent &&
313 $settings['allow-agent-modify-wh'] &&
314 $current_user->agent->id == $agent_id
315 )
316 ) ) {
317 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
318 }
319
320 // ui source.
321 $source = isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '';
322
323 $exceptions = $agent->get_wh_exceptions();
324 $unique_id = uniqid( 'wpsc_' );
325 ?>
326
327 <div class="wpsc-dock-container">
328 <?php
329 printf(
330 /* translators: Click here to see the documentation */
331 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
332 '<a href="https://supportcandy.net/docs/working-hours/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
333 );
334 ?>
335 </div>
336 <table class="wpsc-setting-tbl <?php echo esc_attr( $unique_id ); ?>">
337 <thead>
338 <tr>
339 <th><?php esc_attr_e( 'Title', 'supportcandy' ); ?></th>
340 <th><?php esc_attr_e( 'Date', 'supportcandy' ); ?></th>
341 <th><?php esc_attr_e( 'Schedule', 'supportcandy' ); ?></th>
342 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
343 </tr>
344 </thead>
345 <tbody>
346 <?php
347 foreach ( $exceptions as $exception ) {
348 ?>
349 <tr data-id="<?php echo esc_attr( $exception->id ); ?>">
350 <td><?php echo esc_attr( $exception->title ); ?></td>
351 <td><?php echo esc_attr( $exception->exception_date->format( 'F d, Y' ) ); ?></td>
352 <td>
353 <?php
354 $start_time = explode( ':', $exception->start_time );
355 $start_time = $start_time[0] . ':' . $start_time[1];
356 $end_time = explode( ':', $exception->end_time );
357 $end_time = $end_time[0] . ':' . $end_time[1];
358 /* translators: %1$s: start time, %2$s: end time e.g. 04:00 - 05:00 */
359 printf( esc_attr__( '%1$s - %2$s', 'supportcandy' ), esc_attr( $start_time ), esc_attr( $end_time ) );
360 ?>
361 </td>
362 <td>
363 <a class="wpsc-link"><span class="edit <?php echo esc_attr( $unique_id ); ?>"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span></a> |
364 <a class="wpsc-link"><span class="delete <?php echo esc_attr( $unique_id ); ?>"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span></a>
365 </td>
366 </tr>
367 <?php
368 }
369 ?>
370 </tbody>
371 </table>
372 <script>
373 var source = '<?php echo esc_attr( $source ); ?>';
374 // Add datatable
375 jQuery('table.<?php echo esc_attr( $unique_id ); ?>').DataTable({
376 ordering: false,
377 pageLength: 20,
378 bLengthChange: false,
379 columnDefs: [
380 { targets: -1, searchable: false },
381 { targets: '_all', className: 'dt-left' }
382 ],
383 layout: {
384 topStart: {
385 buttons: [
386 {
387 text: '<?php echo esc_attr( wpsc__( 'Add new', 'supportcandy' ) ); ?>',
388 className: 'wpsc-button small primary',
389 action: function ( e, dt, node, config ) {
390 if (source === 'agent-profile') {
391 jQuery('.wpsc-section-container').html(supportcandy.loader_html);
392 } else {
393 jQuery('.wpsc-setting-section-body').html(supportcandy.loader_html);
394 }
395 var data = {
396 action: 'wpsc_get_add_agent_wh_exception',
397 agent_id: supportcandy.temp.agent_id,
398 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_add_agent_wh_exception' ) ); ?>'
399 };
400 jQuery.post(supportcandy.ajax_url, data, function (response) {
401 if (source === 'agent-profile') {
402 jQuery('.wpsc-section-container').html(response);
403 } else {
404 jQuery('.wpsc-setting-section-body').html(response);
405 }
406 });
407 }
408 }
409 ],
410 },
411 },
412 language: supportcandy.translations.datatables
413 });
414 // Edit.
415 jQuery('span.edit.<?php echo esc_attr( $unique_id ); ?>').click(function(){
416
417 var exception_id = jQuery(this).closest('tr').data('id');
418 if (source === 'agent-profile') {
419 jQuery('.wpsc-section-container').html(supportcandy.loader_html);
420 } else {
421 jQuery('.wpsc-setting-section-body').html(supportcandy.loader_html);
422 }
423 var data = {
424 action: 'wpsc_get_edit_agent_wh_exception',
425 agent_id: supportcandy.temp.agent_id,
426 exception_id,
427 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent_wh_exception' ) ); ?>'
428 };
429 jQuery.post(supportcandy.ajax_url, data, function (res) {
430 if (source === 'agent-profile') {
431 jQuery('.wpsc-section-container').html(res);
432 } else {
433 jQuery('.wpsc-setting-section-body').html(res);
434 }
435 });
436 });
437 // Delete.
438 jQuery('span.delete.<?php echo esc_attr( $unique_id ); ?>').click(function(){
439
440 var flag = confirm(supportcandy.translations.confirm);
441 if (!flag) return;
442 var exception_id = jQuery(this).closest('tr').data('id');
443 if (source === 'agent-profile') {
444 jQuery('.wpsc-section-container').html(supportcandy.loader_html);
445 } else {
446 jQuery('.wpsc-setting-section-body').html(supportcandy.loader_html);
447 }
448 var data = {
449 action: 'wpsc_delete_agent_wh_exception',
450 agent_id: supportcandy.temp.agent_id,
451 exception_id,
452 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_agent_wh_exception' ) ); ?>'
453 };
454 jQuery.post(supportcandy.ajax_url, data, function (res) {
455 jQuery('button.tab.exceptions').trigger('click');
456 });
457 });
458 supportcandy.temp.uniqueId = '<?php echo esc_attr( $unique_id ); ?>';
459 </script>
460 <?php
461 wp_die();
462 }
463
464 /**
465 * Set agent working hrs
466 *
467 * @return void
468 */
469 public static function set_agent_wh_hrs() {
470
471 if ( check_ajax_referer( 'wpsc_set_agent_wh_hrs', '_ajax_nonce', false ) != 1 ) {
472 wp_send_json_error( 'Unauthorised request!', 401 );
473 }
474
475 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
476 if ( ! $agent_id ) {
477 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
478 }
479
480 $agent = new WPSC_Agent( $agent_id );
481 if ( ! $agent->id ) {
482 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
483 }
484
485 $settings = get_option( 'wpsc-wh-settings', array() );
486
487 $current_user = WPSC_Current_User::$current_user;
488 if ( ! (
489 WPSC_Functions::is_site_admin() ||
490 (
491 $current_user->is_agent &&
492 $settings['allow-agent-modify-wh'] &&
493 $current_user->agent->id == $agent_id
494 )
495 ) ) {
496 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
497 }
498
499 // working hrs.
500 $wh = isset( $_POST['wh'] ) ? map_deep( wp_unslash( $_POST['wh'] ), 'sanitize_text_field' ) : array();
501 if ( ! $wh ) {
502 wp_send_json_error( 'Bad request', 400 );
503 }
504
505 // set working hrs.
506 WPSC_Working_Hour::set( $wh, $agent_id );
507 wp_die();
508 }
509
510 /**
511 * Get add agent working hour exception
512 *
513 * @return void
514 */
515 public static function get_add_agent_wh_exception() {
516
517 if ( check_ajax_referer( 'wpsc_get_add_agent_wh_exception', '_ajax_nonce', false ) != 1 ) {
518 wp_send_json_error( 'Unauthorised request!', 401 );
519 }
520
521 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
522 if ( ! $agent_id ) {
523 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
524 }
525
526 $agent = new WPSC_Agent( $agent_id );
527 if ( ! $agent->id ) {
528 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
529 }
530
531 $settings = get_option( 'wpsc-wh-settings', array() );
532
533 $current_user = WPSC_Current_User::$current_user;
534 if ( ! (
535 WPSC_Functions::is_site_admin() ||
536 (
537 $current_user->is_agent &&
538 $settings['allow-agent-modify-wh'] &&
539 $current_user->agent->id == $agent_id
540 )
541 ) ) {
542 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
543 }
544
545 $unique_id = uniqid( 'wpsc_' );
546 ?>
547
548 <form action="#" onsubmit="return false;" class="wpsc-frm-add-agent-exception">
549 <div class="wpsc-input-group">
550 <div class="label-container">
551 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
552 <span class="required-char">*</span>
553 </div>
554 <input name="title" type="text" autocomplete="off">
555 </div>
556 <div class="wpsc-input-group">
557 <div class="label-container">
558 <label for=""><?php esc_attr_e( 'Date', 'supportcandy' ); ?></label>
559 <span class="required-char">*</span>
560 </div>
561 <input class="date exception_date <?php echo esc_attr( $unique_id ); ?>" name="exception_date" type="text" autocomplete="off">
562 </div>
563 <div class="wpsc-input-group">
564 <div class="label-container">
565 <label for=""><?php esc_attr_e( 'Schedule', 'supportcandy' ); ?></label>
566 </div>
567 <table class="wpsc-working-hrs">
568 <tr>
569 <td style="padding: 0 !important;">
570 <select class="wpsc-wh-start-time" name="start_time">
571 <?php WPSC_Wh_Exception::get_start_time_slots( '00:00:00' ); ?>
572 </select>
573 </td>
574 <td style="text-align: center; width:45px; padding: 0 !important;">-</td>
575 <td style="padding: 0 !important;">
576 <select class="wpsc-wh-end-time" name="end_time">
577 <?php WPSC_Working_Hour::get_end_time_slots( '00:00:00', '00:15:00' ); ?>
578 </select>
579 </td>
580 </tr>
581 </table>
582 </div>
583 <script type="text/javascript">
584 var end_times = [];
585 jQuery('.date.<?php echo esc_attr( $unique_id ); ?>').flatpickr({minDate:new Date, disableMobile:true});
586 <?php
587 $current_slot = new DateTime( '2020-01-01 00:15:00' );
588 $second_last_slot = new DateTime( '2020-01-01 23:45:00' );
589 $last_slot = new DateTime( '2020-01-01 23:59:59' );
590
591 do {
592 $time = $current_slot->format( 'H:i:s' )
593 ?>
594 end_times.push({
595 val: '<?php echo esc_attr( $time ); ?>',
596 display_val: '<?php echo esc_attr( $current_slot->format( 'H:i' ) ); ?>',
597 });
598 <?php
599 if ( $current_slot == $second_last_slot ) {
600 $current_slot->add( new DateInterval( 'PT14M59S' ) );
601 } else {
602 $current_slot->add( new DateInterval( 'PT15M' ) );
603 }
604 } while ( $current_slot <= $last_slot );
605 ?>
606 supportcandy.temp.end_times = end_times;
607
608 // Change event
609 jQuery('.wpsc-wh-start-time').change(function(){
610 var start_time = jQuery(this).val();
611 var tempArr = start_time.split(":");
612 var startDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
613 var cmbEndTime = jQuery('.wpsc-wh-end-time');
614 cmbEndTime.find('option').remove();
615 jQuery.each(supportcandy.temp.end_times, function(index, end_time){
616 var tempArr = end_time.val.split(":");
617 var endDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
618 if (startDate < endDate) {
619 var obj = document.createElement('OPTION');
620 var displayVal = document.createTextNode(end_time.display_val);
621 obj.setAttribute("value", end_time.val);
622 obj.appendChild(displayVal);
623 cmbEndTime.append(obj);
624 }
625 });
626 });
627 </script>
628 <input type="hidden" name="action" value="wpsc_set_add_agent_wh_exception">
629 <input type="hidden" name="agent_id" value="<?php echo esc_attr( $agent->id ); ?>">
630 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent_wh_exception' ) ); ?>">
631 </form>
632 <div class="setting-footer-actions">
633 <button
634 class="wpsc-button normal primary margin-right"
635 onclick="wpsc_set_add_agent_wh_exception(this);">
636 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
637 </button>
638 <button
639 class="wpsc-button normal secondary"
640 onclick="window.location.reload();">
641 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
642 </button>
643 </div>
644 <?php
645
646 wp_die();
647 }
648
649 /**
650 * Save new agent working hour exception
651 *
652 * @return void
653 */
654 public static function set_add_agent_wh_exception() {
655
656 if ( check_ajax_referer( 'wpsc_set_add_agent_wh_exception', '_ajax_nonce', false ) != 1 ) {
657 wp_send_json_error( 'Unauthorised request!', 401 );
658 }
659
660 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
661 if ( ! $agent_id ) {
662 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
663 }
664
665 $agent = new WPSC_Agent( $agent_id );
666 if ( ! $agent->id ) {
667 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
668 }
669
670 $settings = get_option( 'wpsc-wh-settings', array() );
671
672 $current_user = WPSC_Current_User::$current_user;
673 if ( ! (
674 WPSC_Functions::is_site_admin() ||
675 (
676 $current_user->is_agent &&
677 $settings['allow-agent-modify-wh'] &&
678 $current_user->agent->id == $agent_id
679 )
680 ) ) {
681 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
682 }
683
684 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
685 if ( ! $title ) {
686 wp_send_json_error( 'Bad request', 400 );
687 }
688
689 // start date.
690 $exception_date = isset( $_POST['exception_date'] ) ? sanitize_text_field( wp_unslash( $_POST['exception_date'] ) ) : '';
691 if ( ! $exception_date ) {
692 wp_send_json_error( 'Bad request', 400 );
693 }
694 $flag = preg_match( '/\w{4}-\w{2}-\w{2}/', $exception_date );
695 if ( $flag !== 1 ) {
696 wp_send_json_error( 'Bad request', 400 );
697 }
698
699 // start time.
700 $start_time = isset( $_POST['start_time'] ) ? sanitize_text_field( wp_unslash( $_POST['start_time'] ) ) : '';
701 if ( ! $start_time ) {
702 wp_send_json_error( 'Bad request', 400 );
703 }
704 $flag = preg_match( '/\w{2}:\w{2}:00/', $start_time );
705 if ( $flag !== 1 ) {
706 wp_send_json_error( 'Bad request', 400 );
707 }
708
709 // end time.
710 $default_end_time = new DateTime( '2020-01-01 ' . $start_time );
711 $default_end_time->add( new DateInterval( 'PT15M' ) );
712 $end_time = isset( $_POST['end_time'] ) ? sanitize_text_field( wp_unslash( $_POST['end_time'] ) ) : $default_end_time->format( 'H:i:s' );
713 if ( ! $end_time ) {
714 wp_send_json_error( 'Bad request', 400 );
715 }
716 $flag = preg_match( '/\w{2}:\w{2}:00/', $end_time );
717 if ( $flag !== 1 ) {
718 wp_send_json_error( '010', 'Bad request', 400 );
719 }
720
721 // delete existing record for the date if exists.
722 $exceptions = WPSC_Wh_Exception::find(
723 array(
724 'meta_query' => array(
725 'relation' => 'AND',
726 array(
727 'slug' => 'agent',
728 'compare' => '=',
729 'val' => $agent_id,
730 ),
731 array(
732 'slug' => 'exception_date',
733 'compare' => '=',
734 'val' => $exception_date . ' 00:00:00',
735 ),
736 ),
737 )
738 )['results'];
739 if ( $exceptions ) {
740 WPSC_Wh_Exception::destroy( $exceptions[0] );
741 }
742
743 WPSC_Wh_Exception::insert(
744 array(
745 'agent' => $agent_id,
746 'title' => $title,
747 'exception_date' => $exception_date . ' 00:00:00',
748 'start_time' => $start_time,
749 'end_time' => $end_time,
750 )
751 );
752
753 wp_die();
754 }
755
756 /**
757 * Edit agent working hour exceptions
758 *
759 * @return void
760 */
761 public static function get_edit_agent_wh_exception() {
762
763 if ( check_ajax_referer( 'wpsc_get_edit_agent_wh_exception', '_ajax_nonce', false ) != 1 ) {
764 wp_send_json_error( 'Unauthorised request!', 401 );
765 }
766
767 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
768 if ( ! $agent_id ) {
769 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
770 }
771
772 $agent = new WPSC_Agent( $agent_id );
773 if ( ! $agent->id ) {
774 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
775 }
776
777 $settings = get_option( 'wpsc-wh-settings', array() );
778
779 $current_user = WPSC_Current_User::$current_user;
780 if ( ! (
781 WPSC_Functions::is_site_admin() ||
782 (
783 $current_user->is_agent &&
784 $settings['allow-agent-modify-wh'] &&
785 $current_user->agent->id == $agent_id
786 )
787 ) ) {
788 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
789 }
790
791 $id = isset( $_POST['exception_id'] ) ? intval( $_POST['exception_id'] ) : 0;
792 if ( ! $id ) {
793 wp_send_json_error( 'Bad request', 400 );
794 }
795
796 $exception = new WPSC_Wh_Exception( $id );
797 if ( ! $exception->id ) {
798 wp_send_json_error( 'Bad request', 400 );
799 }
800
801 $unique_id = uniqid( 'wpsc_' );
802 ?>
803 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-agent-exception">
804 <div class="wpsc-input-group">
805 <div class="label-container">
806 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
807 <span class="required-char">*</span>
808 </div>
809 <input
810 name="title"
811 type="text"
812 value="<?php echo esc_attr( $exception->title ); ?>"
813 autocomplete="off">
814 </div>
815 <div class="wpsc-input-group">
816 <div class="label-container">
817 <label for=""><?php esc_attr_e( 'Date', 'supportcandy' ); ?></label>
818 <span class="required-char">*</span>
819 </div>
820 <input class="date exception_date <?php echo esc_attr( $unique_id ); ?>" value="<?php echo esc_attr( $exception->exception_date->format( 'Y-m-d' ) ); ?>" name="exception_date" type="text" autocomplete="off">
821 </div>
822 <div class="wpsc-input-group">
823 <div class="label-container">
824 <label for=""><?php esc_attr_e( 'Schedule', 'supportcandy' ); ?></label>
825 </div>
826 <table class="wpsc-working-hrs">
827 <tr>
828 <td style="padding: 0 !important;">
829 <select class="wpsc-wh-start-time" name="start_time">
830 <?php WPSC_Wh_Exception::get_start_time_slots( $exception->start_time ); ?>
831 </select>
832 </td>
833 <td style="text-align: center; width:45px; padding: 0 !important;">-</td>
834 <td style="padding: 0 !important;">
835 <select class="wpsc-wh-end-time" name="end_time">
836 <?php WPSC_Working_Hour::get_end_time_slots( $exception->start_time, $exception->end_time ); ?>
837 </select>
838 </td>
839 </tr>
840 </table>
841 </div>
842 <script>
843 var end_times = [];
844 jQuery('.date.<?php echo esc_attr( $unique_id ); ?>').flatpickr();
845 <?php
846 $current_slot = new DateTime( '2020-01-01 00:15:00' );
847 $second_last_slot = new DateTime( '2020-01-01 23:45:00' );
848 $last_slot = new DateTime( '2020-01-01 23:59:59' );
849
850 do {
851 $time = $current_slot->format( 'H:i:s' )
852 ?>
853 end_times.push({
854 val: '<?php echo esc_attr( $time ); ?>',
855 display_val: '<?php echo esc_attr( $current_slot->format( 'H:i' ) ); ?>',
856 });
857 <?php
858 if ( $current_slot == $second_last_slot ) {
859 $current_slot->add( new DateInterval( 'PT14M59S' ) );
860 } else {
861 $current_slot->add( new DateInterval( 'PT15M' ) );
862 }
863 } while ( $current_slot <= $last_slot );
864 ?>
865 supportcandy.temp.end_times = end_times;
866
867 // Change event
868 jQuery('.wpsc-wh-start-time').change(function(){
869 var start_time = jQuery(this).val();
870 var tempArr = start_time.split(":");
871 var startDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
872 var cmbEndTime = jQuery('.wpsc-wh-end-time');
873 cmbEndTime.find('option').remove();
874 jQuery.each(supportcandy.temp.end_times, function(index, end_time){
875 var tempArr = end_time.val.split(":");
876 var endDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]);
877 if (startDate < endDate) {
878 var obj = document.createElement('OPTION');
879 var displayVal = document.createTextNode(end_time.display_val);
880 obj.setAttribute("value", end_time.val);
881 obj.appendChild(displayVal);
882 cmbEndTime.append(obj);
883 }
884 });
885 });
886 </script>
887 <input type="hidden" name="action" value="wpsc_set_edit_agent_wh_exception">
888 <input type="hidden" name="exception_id" value="<?php echo esc_attr( $exception->id ); ?>">
889 <input type="hidden" name="agent_id" value="<?php echo esc_attr( $agent->id ); ?>">
890 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent_wh_exception' ) ); ?>">
891 </form>
892 <div class="setting-footer-actions">
893 <button
894 class="wpsc-button normal primary margin-right"
895 onclick="wpsc_set_edit_agent_wh_exception(this);">
896 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
897 </button>
898 <button
899 class="wpsc-button normal secondary"
900 onclick="window.location.reload();">
901 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
902 </button>
903 </div>
904 <?php
905
906 wp_die();
907 }
908
909 /**
910 * Update agent working hour exceptions
911 *
912 * @return void
913 */
914 public static function set_edit_agent_wh_exception() {
915
916 if ( check_ajax_referer( 'wpsc_set_edit_agent_wh_exception', '_ajax_nonce', false ) != 1 ) {
917 wp_send_json_error( 'Unauthorised request!', 401 );
918 }
919
920 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
921 if ( ! $agent_id ) {
922 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
923 }
924
925 $agent = new WPSC_Agent( $agent_id );
926 if ( ! $agent->id ) {
927 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
928 }
929
930 $settings = get_option( 'wpsc-wh-settings', array() );
931
932 $current_user = WPSC_Current_User::$current_user;
933 if ( ! (
934 WPSC_Functions::is_site_admin() ||
935 (
936 $current_user->is_agent &&
937 $settings['allow-agent-modify-wh'] &&
938 $current_user->agent->id == $agent_id
939 )
940 ) ) {
941 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
942 }
943
944 $id = isset( $_POST['exception_id'] ) ? intval( $_POST['exception_id'] ) : 0;
945 if ( ! $id ) {
946 wp_send_json_error( 'Bad request', 400 );
947 }
948
949 $exception = new WPSC_Wh_Exception( $id );
950 if ( ! $exception->id ) {
951 wp_send_json_error( 'Bad request', 400 );
952 }
953
954 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
955 if ( ! $title ) {
956 wp_send_json_error( 'Bad request', 400 );
957 }
958 $exception->title = $title;
959
960 // start date.
961 $exception_date = isset( $_POST['exception_date'] ) ? sanitize_text_field( wp_unslash( $_POST['exception_date'] ) ) : '';
962 if ( ! $exception_date ) {
963 wp_send_json_error( 'Bad request', 400 );
964 }
965 $flag = preg_match( '/\w{4}-\w{2}-\w{2}/', $exception_date );
966 if ( $flag !== 1 ) {
967 wp_send_json_error( 'Bad request', 400 );
968 }
969 $exception->exception_date = $exception_date . ' 00:00:00';
970
971 // start time.
972 $start_time = isset( $_POST['start_time'] ) ? sanitize_text_field( wp_unslash( $_POST['start_time'] ) ) : '';
973 if ( ! $start_time ) {
974 wp_send_json_error( 'Bad request', 400 );
975 }
976 $flag = preg_match( '/\w{2}:\w{2}:00/', $start_time );
977 if ( $flag !== 1 ) {
978 wp_send_json_error( 'Bad request', 400 );
979 }
980 $exception->start_time = $start_time;
981
982 // end time.
983 $default_end_time = new DateTime( '2020-01-01 ' . $start_time );
984 $default_end_time->add( new DateInterval( 'PT15M' ) );
985 $end_time = isset( $_POST['end_time'] ) ? sanitize_text_field( wp_unslash( $_POST['end_time'] ) ) : $default_end_time->format( 'H:i:s' );
986 if ( ! $end_time ) {
987 wp_send_json_error( 'Bad request', 400 );
988 }
989 $flag = preg_match( '/\w{2}:\w{2}:00/', $end_time );
990 if ( $flag !== 1 ) {
991 wp_send_json_error( 'Bad request', 400 );
992 }
993 $exception->end_time = $end_time;
994
995 $exception->save();
996 wp_die();
997 }
998
999 /**
1000 * Delete agent working hour exception
1001 */
1002 public static function delete_agent_wh_exception() {
1003
1004 if ( check_ajax_referer( 'wpsc_delete_agent_wh_exception', '_ajax_nonce', false ) != 1 ) {
1005 wp_send_json_error( 'Unauthorised request!', 401 );
1006 }
1007
1008 $agent_id = isset( $_POST['agent_id'] ) ? intval( $_POST['agent_id'] ) : 0;
1009 if ( ! $agent_id ) {
1010 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1011 }
1012
1013 $agent = new WPSC_Agent( $agent_id );
1014 if ( ! $agent->id ) {
1015 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1016 }
1017
1018 $settings = get_option( 'wpsc-wh-settings', array() );
1019
1020 $current_user = WPSC_Current_User::$current_user;
1021 if ( ! (
1022 WPSC_Functions::is_site_admin() ||
1023 (
1024 $current_user->is_agent &&
1025 $settings['allow-agent-modify-wh'] &&
1026 $current_user->agent->id == $agent_id
1027 )
1028 ) ) {
1029 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1030 }
1031
1032 $id = isset( $_POST['exception_id'] ) ? intval( $_POST['exception_id'] ) : 0;
1033 if ( ! $id ) {
1034 wp_send_json_error( 'Bad request', 400 );
1035 }
1036
1037 $exception = new WPSC_Wh_Exception( $id );
1038 if ( ! $exception->id ) {
1039 wp_send_json_error( 'Bad request', 400 );
1040 }
1041
1042 WPSC_Wh_Exception::destroy( $exception );
1043 wp_die();
1044 }
1045 }
1046 endif;
1047
1048 WPSC_Agent_Working_Hrs::init();
1049