-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclass-supportflow-admin.php
1259 lines (1060 loc) · 49.3 KB
/
class-supportflow-admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
*/
defined( 'ABSPATH' ) or die( "Cheatin' uh?" );
class SupportFlow_Admin extends SupportFlow {
function __construct() {
add_action( 'wp_ajax_sf_forward_conversation', array( $this, 'action_wp_ajax_sf_email_conversation' ) );
add_filter( 'heartbeat_received', array( $this, 'filter_heartbeat_received' ), 10, 2 );
add_action( 'wp_ajax_ticket_attachment_upload', array( $this, 'action_wp_ajax_ticket_attachment_upload' ) );
add_action( 'supportflow_after_setup_actions', array( $this, 'setup_actions' ) );
add_action( 'add_attachment', array( $this, 'action_add_attachment' ) );
}
public function setup_actions() {
// Creating or updating a ticket
add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'action_save_post' ) );
if ( ! $this->is_edit_screen() ) {
return;
}
// Everything
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
add_filter( 'post_updated_messages', array( $this, 'filter_post_updated_messages' ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
// Manage tickets view
add_filter( 'manage_' . SupportFlow()->post_type . '_posts_columns', array( $this, 'filter_manage_post_columns' ) );
add_filter( 'manage_edit-' . SupportFlow()->post_type . '_sortable_columns', array( $this, 'manage_sortable_columns' ) );
add_action( 'manage_posts_custom_column', array( $this, 'action_manage_posts_custom_column' ), 10, 2 );
add_filter( 'views_edit-' . SupportFlow()->post_type, array( $this, 'filter_views' ) );
add_filter( 'post_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . SupportFlow()->post_type, array( $this, 'filter_bulk_actions' ) );
add_action( 'pre_get_posts', array( $this, 'action_pre_get_posts' ) );
add_action( 'admin_action_change_status', array( $this, 'handle_action_change_status' ) );
add_action( 'restrict_manage_posts', array( $this, 'action_restrict_manage_posts' ) );
}
/**
* Re-sort the custom statuses so trash appears last
*/
function action_admin_init() {
global $wp_post_statuses, $pagenow;
$trash_status = $wp_post_statuses['trash'];
unset( $wp_post_statuses['trash'] );
$wp_post_statuses['trash'] = $trash_status;
if ( 'edit.php' == $pagenow ) {
add_filter( 'get_the_excerpt', array( $this, 'filter_get_the_excerpt' ) );
}
}
/**
* Add any CSS or JS we need for the admin
*/
public function action_admin_enqueue_scripts() {
global $pagenow;
$handle = SupportFlow()->enqueue_style( 'supportflow-admin', 'admin.css' );
if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) {
wp_enqueue_media();
$customers_autocomplete_handle = SupportFlow()->enqueue_script( 'supportflow-customers-autocomplete', 'customers-autocomplete.js', array( 'jquery', 'jquery-ui-autocomplete' ) );
$ticket_attachment_handle = SupportFlow()->enqueue_script( 'supportflow-ticket-attachments', 'ticket_attachments.js' );
$supportflow_tickets_handle = SupportFlow()->enqueue_script( 'supportflow-tickets', 'tickets.js' );
$auto_save_handle = SupportFlow()->enqueue_script( 'supportflow-auto-save', 'auto_save.js', array( 'jquery', 'heartbeat' ) );
wp_localize_script( $customers_autocomplete_handle, 'SFCustomersAc', array(
'ajax_url' => add_query_arg( 'action', SupportFlow()->extend->jsonapi->action, admin_url( 'admin-ajax.php' ) )
) );
wp_localize_script( $ticket_attachment_handle, 'SFTicketAttachments', array(
'frame_title' => __( 'Attach files', 'supportflow' ),
'button_title' => __( 'Insert as attachment', 'supportflow' ),
'remove_attachment' => __( 'Remove', 'supportflow' ),
'sure_remove' => __( 'Are you sure want to remove this attachment?', 'supportflow' ),
) );
wp_localize_script( $supportflow_tickets_handle, 'SFTickets', array(
'no_title_msg' => __( 'You must need to specify the subject of the ticket', 'supportpress' ),
'no_customer_msg' => __( 'You must need to add atleast one customer', 'supportpress' ),
'pagenow' => $pagenow,
'send_msg' => __( 'Send Message', 'supportflow' ),
'add_private_note' => __( 'Add Private Note', 'supportflow' ),
) );
wp_localize_script( $auto_save_handle, 'SFAutoSave', array(
'ticket_id' => get_the_ID(),
) );
}
if ( 'post.php' == $pagenow ) {
$email_conversation_handle = SupportFlow()->enqueue_script( 'supportflow-email-conversation', 'email_conversation.js' );
wp_localize_script( $email_conversation_handle, 'SFEmailConversation', array(
'post_id' => get_the_ID(),
'sending_emails' => __( 'Please wait while sending E-Mail(s)', 'supportflow' ),
'failed_sending' => __( 'Failed sending E-Mails', 'supportflow' ),
'_email_conversation_nonce' => wp_create_nonce( 'sf_email_conversation' ),
) );
}
}
/**
*
*/
public function action_wp_ajax_sf_email_conversation() {
if ( false === check_ajax_referer( 'sf_email_conversation', '_email_conversation_nonce', false ) ) {
_e( 'Invalid request. Please try refreshing the page.', 'supportflow' );
die;
}
if ( ! isset( $_REQUEST['email_ids'] ) || ! isset( $_REQUEST['post_id'] ) ) {
_e( 'Invalid request. Please try refreshing the page.', 'supportflow' );
die;
}
$email_ids = SupportFlow()->extract_email_ids( $_REQUEST['email_ids'] );
$ticket_id = (int) $_REQUEST['post_id'];
if ( ! current_user_can( 'edit_post', $ticket_id ) ) {
_e( 'You are not allowed to edit this item.' );
die;
}
if ( empty( $email_ids ) ) {
_e( 'No valid E-Mail ID found', 'supportflow' );
die;
}
SupportFlow()->extend->emails->email_conversation( $ticket_id, $email_ids );
_e( 'Successfully sented E-Mails', 'supportflow' );
exit;
}
/**
* Add random characters to attachment uploaded through SupportFlow web UI
*
* @todo Conversion to a better way to determine if attachment if uploaded through SF web UI rather than HTTP referer
*/
function action_add_attachment( $attachment_id ) {
if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
return;
}
$post_type = SupportFlow()->post_type;
$referer = $_SERVER['HTTP_REFERER'];
$url = parse_url( $referer );
$path = $url['scheme'] . '://' . $url['host'] . $url['path'];
parse_str( $url['query'], $query );
// Check if referred by SupportFlow ticket page
if ( admin_url( 'post-new.php' ) == $path ) {
if ( empty( $query['post_type'] ) || $query['post_type'] != $post_type ) {
return;
}
} elseif ( admin_url( 'post.php' ) == $path ) {
if ( empty( $query['post'] ) || get_post_type( (int) $query['post'] ) != $post_type ) {
return;
}
} else {
return;
}
SupportFlow()->extend->attachments->secure_attachment_file( $attachment_id );
}
/**
* Filter the messages that appear to the user after they perform an action on a ticket
*/
public function filter_post_updated_messages( $messages ) {
global $post;
$messages[SupportFlow()->post_type] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Ticket updated.', 'supportflow' ),
2 => __( 'Custom field updated.', 'supportflow' ),
3 => __( 'Custom field deleted.', 'supportflow' ),
4 => __( 'Ticket updated.', 'supportflow' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Ticket restored to revision from %s', 'supportflow' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Ticket updated.', 'supportflow' ),
7 => __( 'Ticket updated.', 'supportflow' ),
8 => __( 'Ticket updated.', 'supportflow' ),
9 => __( 'Ticket updated.', 'supportflow' ),
10 => __( 'Ticket updated.', 'supportflow' ),
);
return $messages;
}
public function filter_heartbeat_received( $response, $data ) {
if (
isset( $data['supportflow-autosave'] ) &&
is_array( $data['supportflow-autosave'] ) &&
isset( $data['supportflow-autosave']['ticket_id'] ) &&
current_user_can( 'edit_post', (int) $data['supportflow-autosave']['ticket_id'] )
) {
// Save data received from client to the database as post meta
$ticket_id = (int) $data['supportflow-autosave']['ticket_id'];
unset( $data['supportflow-autosave']['ticket_id'] );
if ( 'auto-draft' == get_post_status( $ticket_id ) ) {
wp_update_post( array( 'ID' => $ticket_id, 'post_status' => 'draft' ) );
}
foreach ( $data['supportflow-autosave'] as $element_id => $element_value ) {
update_post_meta( $ticket_id, "_sf_autosave_$element_id", $element_value );
}
echo $data['supportflow-autosave']['post_title'];
if ( ! empty( $data['supportflow-autosave']['post_title'] ) ) {
wp_update_post( array( 'ID' => $ticket_id, 'post_title' => $data['supportflow-autosave']['post_title'] ) );
}
}
return $response;
}
/**
* Filters links shown over dropdowns in all tickets page
*/
public function filter_views( $views ) {
$post_type = SupportFlow()->post_type;
$total_posts = SupportFlow()->get_tickets_count();
$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['author'] ) ? ' class="current"' : '';
$view_all_link = add_query_arg( array( 'post_type' => SupportFlow()->post_type), admin_url( 'edit.php' ) );
$view_all_title = sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' ), number_format_i18n( $total_posts ) );
$view_all = "<a href='$view_all_link' $class >$view_all_title</a>";
$my_posts = SupportFlow()->get_tickets_count( array( 'author' => get_current_user_id() ) );
$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && ! empty( $_REQUEST['author'] ) && get_current_user_id() == $_REQUEST['author'] ? ' class="current"' : '';
$view_mine_link = add_query_arg( array( 'post_type' => SupportFlow()->post_type, 'author' => get_current_user_id() ), admin_url( 'edit.php' ) );
$view_mine_title = sprintf( _nx( 'Mine <span class="count">(%s)</span>', 'Mine <span class="count">(%s)</span>', $my_posts, 'posts' ), number_format_i18n( $my_posts ) );
$view_mine = "<a href='$view_mine_link' $class >$view_mine_title</a>";
$unassigned_posts = SupportFlow()->get_tickets_count( array( 'author' => 0 ) );
$class = empty( $class ) && empty( $_REQUEST['post_status'] ) && isset( $_REQUEST['author'] ) && 0 == $_REQUEST['author'] ? ' class="current"' : '';
$view_mine_link = add_query_arg( array( 'post_type' => SupportFlow()->post_type, 'author' => 0 ), admin_url( 'edit.php' ) );
$view_unassigned_title = sprintf( _nx( 'Unassigned <span class="count">(%s)</span>', 'Unassigned <span class="count">(%s)</span>', $unassigned_posts, 'posts' ), number_format_i18n( $unassigned_posts ) );
$view_unassigned = "<a href='$view_mine_link' $class >$view_unassigned_title</a>";
// Put 'All' and 'Mine' at the beginning of the array
array_shift( $views );
$views = array_reverse( $views );
$views['unassigned'] = $view_unassigned;
$views['mine'] = $view_mine;
$views['all'] = $view_all;
$views = array_reverse( $views );
// Remove private option from filter links as they are just private replies to ticket
unset( $views['private'] );
return $views;
}
/**
* Add custom filters for the Manage Tickets view
*/
public function action_restrict_manage_posts() {
// Filter to specific agents
$agent_dropdown_args = array(
'show_option_all' => __( 'Show all agents', 'supportflow' ),
'name' => 'author',
'selected' => ( ! empty( $_REQUEST['author'] ) ) ? (int) $_REQUEST['author'] : false,
'who' => 'authors',
);
$agent_dropdown_args = apply_filters( 'supportflow_admin_agent_dropdown_args', $agent_dropdown_args );
wp_dropdown_users( $agent_dropdown_args );
// Filter to specify tag
$tax_slug = SupportFlow()->tags_tax;
$terms = get_terms( SupportFlow()->tags_tax, array( 'hide_empty' => false ) );
echo "<select name='" . esc_attr( $tax_slug ) . "' id='" . esc_attr( $tax_slug ) . "' class='postform'>";
echo "<option value=''>" . __( 'Show All tags', 'supportflow' ) . "</option>";
foreach ( $terms as $term ) {
$selected = selected( isset( $_REQUEST[$tax_slug] ) && ( $_REQUEST[$tax_slug] == $term->slug ), true, false );
echo "<option value='" . esc_attr( $term->slug ) . "' $selected>" . esc_html( $term->name ) . '</option>';
}
echo "</select>";
// Filter to specify E-Mail account
$email_accounts = SupportFlow()->extend->email_accounts->get_email_accounts( true );
echo "<select name='email_account' id='email_account' class='postform'>";
echo "<option value=''>" . __( 'Show All Accounts', 'supportflow' ) . "</option>";
foreach ( $email_accounts as $id => $email_account ) {
$selected = selected( isset( $_REQUEST['email_account'] ) && ( $_REQUEST['email_account'] == $id ), true, false );
echo "<option value='" . esc_attr( $id ) . "'$selected>" . esc_html( $email_account['username'] ) . '</option>';
}
echo "</select>";
}
/**
* Filter the actions available to the agent on the post type
* They are shown in All tickets page in subject column
*/
function filter_post_row_actions( $row_actions, $post ) {
// Rename these actions
if ( isset( $row_actions['edit'] ) ) {
$row_actions['edit'] = str_replace( __( 'Edit' ), __( 'View', 'supportflow' ), str_replace( __( 'Edit this item' ), __( 'View Ticket', 'supportflow' ), $row_actions['edit'] ) );
}
// Save the trash action for the end
if ( isset( $row_actions['trash'] ) ) {
$trash_action = $row_actions['trash'];
unset( $row_actions['trash'] );
} else {
$trash_action = false;
}
// Allow an agent to easily close a ticket
$statuses = SupportFlow()->post_statuses;
$status_slugs = array_keys( $statuses );
$last_status = array_pop( $status_slugs );
if ( ! in_array( get_query_var( 'post_status' ), array( 'trash' ) ) ) {
if ( $last_status == get_post_status( $post->ID ) ) {
$change_to = $status_slugs[2];
} else {
$change_to = $last_status;
}
$args = array(
'action' => 'change_status',
'sf_nonce' => wp_create_nonce( 'sf-change-status' ),
'post_status' => $change_to,
'ticket_id' => $post->ID,
'post_type' => SupportFlow()->post_type,
);
$action_link = add_query_arg( $args, admin_url( 'edit.php' ) );
if ( $last_status == $change_to ) {
$title_attr = esc_attr__( 'Close Ticket', 'supportflow' );
$action_text = esc_html__( 'Close', 'supportflow' );
} else {
$title_attr = esc_attr__( 'Reopen Ticket', 'supportflow' );
$action_text = esc_html__( 'Reopen', 'supportflow' );
}
if ( current_user_can( 'edit_post', $post->ID ) ) {
$row_actions['change_status'] = '<a href="' . esc_url( $action_link ) . '" title="' . $title_attr . '">' . $action_text . '</a>';
}
}
// Actions we don't want
unset( $row_actions['inline hide-if-no-js'] );
unset( $row_actions['view'] );
if ( $trash_action ) {
$row_actions['trash'] = $trash_action;
}
return $row_actions;
}
/**
* Remove the 'edit' bulk action. Doesn't do much for us
*/
public function filter_bulk_actions( $actions ) {
unset( $actions['edit'] );
return $actions;
}
/**
* Handle which tickets are show on the Manage Tickets view when
*/
function action_pre_get_posts( $query ) {
global $pagenow;
if ( 'edit.php' != $pagenow || ! $query->is_main_query() ) {
return;
}
$statuses = SupportFlow()->post_statuses;
$status_slugs = array();
foreach ( $statuses as $status => $status_data ) {
if ( true == $status_data['show_tickets'] ) {
$status_slugs[] = $status;
}
}
// Order posts by post_modified if there's no orderby set
if ( ! $query->get( 'orderby' ) ) {
$query->set( 'orderby', 'modified' );
$query->set( 'order', 'DESC' );
}
// Do our own custom search handling so we can search against reply text
if ( $search = $query->get( 's' ) ) {
// Get all replies that match our results
$args = array(
'search' => $search,
'status' => 'any',
);
$matching_replies = SupportFlow()->get_replies( $args );
$post_ids = wp_list_pluck( $matching_replies, 'post_parent' );
$args = array(
's' => $search,
'post_type' => SupportFlow()->post_type,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'fields' => 'ids',
);
$post_query = new WP_Query( $args );
if ( ! is_wp_error( $post_query ) ) {
$post_ids = array_merge( $post_ids, $post_query->posts );
}
$query->set( 'post__in', $post_ids );
// Ignore the original search query
add_filter( 'posts_search', array( $this, 'filter_posts_search' ) );
}
// Only show tickets with the last status if the last status is set
$post_status = $query->get( 'post_status' );
if ( ! $query->get( 's' ) && empty( $post_status ) ) {
$query->set( 'post_status', $status_slugs );
}
add_action( 'posts_clauses', array( $this, 'filter_author_clause' ), 10, 2 );
if ( isset( $_GET['email_account'] ) && ! empty( $_GET['email_account'] ) ) {
$query->set( 'meta_key', 'email_account' );
$query->set( 'meta_value', (int) $_GET['email_account'] );
}
}
/*
* Show unassigned tickets when query author is 0
*/
public function filter_author_clause( $clauses, $query ) {
if ( isset( $query->query['author'] ) && 0 == $query->query['author'] ) {
$clauses['where'] .= ' AND post_author = 0 ';
}
return $clauses;
}
/**
* Sometimes we want to ignore the original search query because we do our own
*/
public function filter_posts_search( $posts_search ) {
return '';
}
/**
* Handle $_GET actions in the admin
*/
function handle_action_change_status() {
if ( ! isset( $_GET['action'], $_GET['sf_nonce'], $_GET['post_status'], $_GET['ticket_id'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_GET['sf_nonce'], 'sf-change-status' ) ) {
wp_die( __( "Doin' something phishy, huh?", 'supportflow' ) );
}
$ticket_id = (int) $_GET['ticket_id'];
if ( ! current_user_can( 'edit_post', $ticket_id ) ) {
wp_die( __( 'You are not allowed to edit this item.' ) );
}
$post_status = sanitize_key( $_GET['post_status'] );
$new_ticket = array(
'ID' => $ticket_id,
'post_status' => $post_status,
);
wp_update_post( $new_ticket );
wp_safe_redirect( wp_get_referer() );
exit;
}
/**
* Manipulate the meta boxes appearing on the edit post view
*
* When creating a new ticket, you should be able to:
*
* When updating an existing ticket, you should be able to:
*
*/
public function action_add_meta_boxes() {
global $pagenow;
if ( ! $this->is_edit_screen() ) {
return;
}
$customers_box = 'tagsdiv-' . SupportFlow()->customers_tax;
remove_meta_box( 'submitdiv', SupportFlow()->post_type, 'side' );
remove_meta_box( $customers_box, SupportFlow()->post_type, 'side' );
remove_meta_box( 'slugdiv', SupportFlow()->post_type, 'normal' );
add_meta_box( 'supportflow-details', __( 'Details', 'supportflow' ), array( $this, 'meta_box_details' ), SupportFlow()->post_type, 'side', 'high' );
add_meta_box( 'supportflow-subject', __( 'Subject', 'supportflow' ), array( $this, 'meta_box_subject' ), SupportFlow()->post_type, 'normal' );
add_meta_box( 'supportflow-customers', __( 'Customers', 'supportflow' ), array( $this, 'meta_box_customers' ), SupportFlow()->post_type, 'normal' );
add_meta_box( 'supportflow-cc-bcc', __( 'CC and BCC', 'supportflow' ), array( $this, 'meta_box_cc_bcc' ), SupportFlow()->post_type, 'normal' );
add_meta_box( 'supportflow-replies', __( 'Replies', 'supportflow' ), array( $this, 'meta_box_replies' ), SupportFlow()->post_type, 'normal' );
if ( 'post.php' == $pagenow ) {
add_meta_box( 'supportflow-other-customers-tickets', __( 'Customer(s) recent Tickets', 'supportflow' ), array( $this, 'meta_box_other_customers_tickets' ), SupportFlow()->post_type, 'side' );
add_meta_box( 'supportflow-forward_conversation', __( 'Forward this conversation', 'supportflow' ), array( $this, 'meta_box_email_conversation' ), SupportFlow()->post_type, 'side' );
}
}
public function meta_box_other_customers_tickets() {
$ticket_customers = SupportFlow()->get_ticket_customers( get_the_ID(), array( 'fields' => 'slugs' ) );
$statuses = SupportFlow()->post_statuses;
$status_slugs = array_keys($statuses);
$table = new SupportFlow_Table( '', false, false );
if ( empty( $ticket_customers ) ) {
$tickets = array();
} else {
$args = array(
'post_type' => SupportFlow()->post_type,
'post_parent' => 0,
'post_status' => $status_slugs,
'posts_per_page' => 10,
'post__not_in' => array( get_the_id() ),
'tax_query' => array(
array(
'taxonomy' => SupportFlow()->customers_tax,
'field' => 'slug',
'terms' => $ticket_customers,
),
),
);
$wp_query = new WP_Query( $args );
$tickets = $wp_query->posts;
}
$no_items = __( 'No recent tickets found.', 'supportflow' );
$table->set_no_items( $no_items );
$table->set_columns( array(
'title' => __( 'Subject', 'supportflow' ),
'status' => __( 'Status', 'supportflow' ),
) );
$data = array();
foreach ( $tickets as $ticket ) {
$post_date = strtotime( $ticket->post_date );
$post_modified = strtotime( $ticket->post_modified );
$title = '<b>' . esc_html( $ticket->post_title ) . '</b>';
$title = "<a href='post.php?post=" . $ticket->ID . "&action=edit'>" . $title . "</a>";
$data[] = array(
'title' => $title,
'status' => $statuses[$ticket->post_status]['label'],
);
}
$table->set_data( $data );
$table->display();
}
public function meta_box_email_conversation() {
?>
<p class="description"><?php _e( "Please enter E-Mail address seperated by comma to whom you want to send this conversation.", 'supportflow' ) ?></p>
<br />
<input type="text" id="email_conversation_to" />
<?php submit_button( __( 'Send', 'supportflow' ), '', 'email_conversation_submit', false ); ?>
<p id="email_conversation_status"></p>
<?php
}
/**
* Show details about the ticket, and allow the post status and agent to be changed
*/
public function meta_box_details() {
echo '<div id="minor-publishing">
<div id="misc-publishing-actions">';
$this->render_meta_box_details_email_account();
$this->render_meta_box_details_opened();
$this->render_meta_box_details_status();
$this->render_meta_box_details_author();
$this->render_meta_box_details_notifications();
$this->render_meta_box_details_actions();
echo '</div>
</div>';
}
public function render_meta_box_details_email_account() {
// Get post E-Mail account
$email_accounts = SupportFlow()->extend->email_accounts->get_email_accounts( true );
$user_permissions = SupportFlow()->extend->permissions->get_user_permissions_data( get_current_user_id() );
$user_permissions = $user_permissions['email_accounts'];
$email_account_id = get_post_meta( get_the_id(), 'email_account', true );
if ( '' == $email_account_id ) {
$email_account_dropdown = '<select class="meta-item-dropdown">';
foreach ( $email_accounts as $id => $email_account ) {
if ( empty( $email_account ) || ( ! current_user_can( 'manage_options' ) && ! in_array( $id, $user_permissions ) ) ) {
continue;
}
$email_account_dropdown .= '<option value="' . esc_attr( $id ) . '" ' . '>' . esc_html( $email_account['username'] ) . '</option>';
}
$email_account_dropdown .= '</select>';
$email_account_keys = array_keys( $email_accounts );
$email_account_first = $email_account_keys[0];
$email_account_label = $email_accounts[$email_account_first]['username'];
}
if ( '' == $email_account_id ) {
?>
<div class="misc-pub-section meta-item">
<label class="meta-item-toggle-button"><?php _e( 'Account', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php _e( $email_account_label, 'supportflow' ) ?></span>
<a href="#" class="meta-item-toggle-button meta-item-toggle-content hide-if-no-js">
<span aria-hidden="true"><?php _e( 'Edit' ) ?></span>
</a>
<input name="post_email_account" class="meta-item-name" value="<?php echo $email_account_first ?>" type="hidden" />
<div class="meta-item-toggle-content hide-if-js">
<?php echo $email_account_dropdown ?>
<a href="#" class="hide-if-no-js button meta-item-ok-button meta-item-toggle-button"><?php _e( 'OK' ) ?></a>
<a href="#" class="hide-if-no-js button-cancel meta-item-cancel-button meta-item-toggle-button"><?php _e( 'Cancel' ) ?></a>
</div>
</div>
<?php
}
}
public function render_meta_box_details_opened() {
global $pagenow;
// Get post creation and last update time
if ( 'post.php' == $pagenow ) {
$opened = get_the_date() . ' ' . get_the_time();
$modified_gmt = get_post_modified_time( 'U', true, get_the_ID() );
$last_activity = sprintf( __( '%s ago', 'supportflow' ), human_time_diff( $modified_gmt ) );
?>
<div class="misc-pub-section meta-item">
<label><?php _e( 'Opened', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php esc_html_e( $opened ) ?></span>
</div>
<!--Last ticket update time-->
<div class="misc-pub-section meta-item">
<label><?php _e( 'Last Activity', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php esc_html_e( $last_activity ) ?></span>
</div>
<?php
}
}
public function render_meta_box_details_status() {
// Get post status
$post_statuses = SupportFlow()->post_statuses;
$current_status_id = get_post_status( get_the_ID() );
if ( ! isset( $post_statuses[$current_status_id] ) ) {
$post_statuses_key = array_keys( $post_statuses );
$current_status_id = $post_statuses_key[0];
}
$current_status_label = $post_statuses[$current_status_id]['label'];
?>
<!--Ticket status box-->
<div class="misc-pub-section meta-item">
<label class="meta-item-toggle-button"><?php _e( 'Status', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php esc_html_e( $current_status_label, 'supportflow' ) ?></span>
<a href="#" class="meta-item-toggle-button meta-item-toggle-content hide-if-no-js">
<span aria-hidden="true"><?php _e( 'Edit' ) ?></span>
</a>
<input name="post_status" class="meta-item-name" value="<?php esc_attr_e( $current_status_id ) ?>" type="hidden" />
<div class="meta-item-toggle-content hide-if-js">
<select class="meta-item-dropdown">
<?php foreach ( $post_statuses as $slug => $post_status ) : ?>
<option value="<?php esc_attr_e( $slug ) ?>"<?php selected( $current_status_id, $slug ) ?>><?php esc_html_e( $post_status['label'] ) ?></option>;
<?php endforeach; ?>
</select>
<a href="#" class="hide-if-no-js button meta-item-ok-button meta-item-toggle-button"><?php _e( 'OK' ) ?></a>
<a href="#" class="hide-if-no-js button-cancel meta-item-cancel-button meta-item-toggle-button"><?php _e( 'Cancel' ) ?></a>
</div>
</div>
<?php
}
public function render_meta_box_details_author() {
// Get post authors
$post_author_id = get_post( get_the_ID() )->post_author;
// WP change owner to current user if $post_author_id is 0 (returned when ticket is unassigned)
if ( 0 == $post_author_id ) {
$post_author_id = - 1;
}
if ( 0 < $post_author_id ) {
$post_author_label = get_userdata( $post_author_id )->data->user_nicename;
} else {
$post_author_label = __( '-- Unassigned --', 'supportflow' );
}
$args = array(
'show_option_none' => __( '-- Unassigned --', 'supportflow' ),
'selected' => $post_author_id,
'id' => '',
'name' => '',
'who' => 'author',
'class' => 'meta-item-dropdown',
'echo' => false
);
$post_authors_dropdown = wp_dropdown_users( $args );
?>
<div class="misc-pub-section meta-item">
<label class="meta-item-toggle-button"><?php _e( 'Owner', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php _e( $post_author_label, 'supportflow' ) ?></span>
<a href="#" class="meta-item-toggle-button meta-item-toggle-content hide-if-no-js">
<span aria-hidden="true"><?php _e( 'Edit' ) ?></span>
</a>
<input name="post_author" class="meta-item-name" value="<?php esc_attr_e( $post_author_id ) ?>" type="hidden" />
<div class="meta-item-toggle-content hide-if-js">
<?php echo $post_authors_dropdown ?>
<a href="#" class="hide-if-no-js button meta-item-ok-button meta-item-toggle-button"><?php _e( 'OK' ) ?></a>
<a href="#" class="hide-if-no-js button-cancel meta-item-cancel-button meta-item-toggle-button"><?php _e( 'Cancel' ) ?></a>
</div>
</div>
<?php
}
public function render_meta_box_details_notifications() {
global $pagenow;
// Get E-Mail notification settings
$notification_id = 0;
$notification_label = 'Default';
$notification_label_title = 'Choose default if you want to receive E-Mail notifications based on what you set in `E-Mail notification` page. Choose Enable/Disable if you want to override those settings';
$notification_dropdown = '';
$notification_dropdown .= '<select class="meta-item-dropdown">';
if ( 'post-new.php' == $pagenow ) {
$notification_dropdown .= '<option value="default">' . __( 'Default', 'supportflow' ) . '</option>';
$notification_dropdown .= '<option value="enable">' . __( 'Subscribed', 'supportflow' ) . '</option>';
$notification_dropdown .= '<option value="disable">' . __( 'Unsubscribed', 'supportflow' ) . '</option>';
} elseif ( 'post.php' == $pagenow ) {
$email_notifications_override = get_post_meta( get_the_ID(), 'email_notifications_override', true );
$current_user_id = get_current_user_id();
if ( isset( $email_notifications_override[$current_user_id] ) ) {
$override_status = $email_notifications_override[$current_user_id];
if ( 'enable' == $override_status ) {
$notification_label = 'Subscribed';
$notification_id = 1;
} elseif ( 'disable' == $override_status ) {
$notification_label = 'Unsubscribed';
$notification_id = 2;
}
}
$notification_dropdown .= '<option value="default"' . selected( $notification_id, 0, false ) . '>' . __( 'Default', 'supportflow' ) . '</option>';
$notification_dropdown .= '<option value="enable"' . selected( $notification_id, 1, false ) . '>' . __( 'Subscribed', 'supportflow' ) . '</option>';
$notification_dropdown .= '<option value="disable"' . selected( $notification_id, 2, false ) . '>' . __( 'Unsubscribed', 'supportflow' ) . '</option>';
}
$notification_dropdown .= '</select>';
?>
<div class="misc-pub-section meta-item">
<label class="meta-item-toggle-button" title="<?php _e( $notification_label_title, 'supportflow' ) ?>"><?php _e( 'E-Mail Notifications', 'supportflow' ) ?>:</label>
<span class="meta-item-label"><?php esc_html_e( $notification_label, 'supportflow' ) ?></span>
<a href="#" class="meta-item-toggle-button meta-item-toggle-content hide-if-no-js">
<span aria-hidden="true"><?php _e( 'Edit' ) ?></span>
</a>
<input name="post_email_notifications_override" class="meta-item-name" value="<?php echo $notification_id ?>" type="hidden" />
<div class="meta-item-toggle-content hide-if-js">
<?php echo $notification_dropdown ?>
<a href="#" class="hide-if-no-js button meta-item-ok-button meta-item-toggle-button"><?php _e( 'OK' ) ?></a>
<a href="#" class="hide-if-no-js button-cancel meta-item-cancel-button meta-item-toggle-button"><?php _e( 'Cancel' ) ?></a>
</div>
</div>
<?php
}
public function render_meta_box_details_actions() {
global $pagenow;
$post_statuses = SupportFlow()->post_statuses;
$current_status_id = get_post_status( get_the_ID() );
if ( ! isset( $post_statuses[$current_status_id] ) ) {
$post_statuses_key = array_keys( $post_statuses );
$current_status_id = $post_statuses_key[0];
}
$current_status_label = $post_statuses[$current_status_id]['label'];
$close_ticket_label = __( 'Close ticket', 'supportflow' );
// Get submit button label
if ( 'post-new.php' == $pagenow ) {
$submit_text = __( 'Start Ticket', 'supportflow' );
} else {
$submit_text = __( 'Update Ticket', 'supportflow' );
}
?>
<div id="major-publishing-actions">
<?php if ( 'post.php' == $pagenow && $current_status_id != 'sf_closed' ) : ?>
<div id="delete-action">
<?php submit_button( $close_ticket_label, '', 'close-ticket-submit', false, array( 'id' => 'close-ticket-submit' ) ); ?>
</div>
<?php endif; ?>
<div id="publishing-action">
<?php submit_button( $submit_text, 'save-button primary', 'update-ticket', false ); ?>
</div>
<div class="clear"></div>
</div>
<?php
}
/**
* A box that appears at the top
*/
public function meta_box_subject() {
$placeholder = __( 'What is your conversation about?', 'supportflow' );
echo '<h4>' . __( 'Subject', 'supportflow' ) . '</h4>';
echo '<input type="text" id="subject" name="post_title" class="sf_autosave" placeholder="' . $placeholder . '" value="' . get_the_title() . '" autocomplete="off" />';
echo '<p class="description">' . __( 'Please describe what this ticket is about in several words', 'supportflow' ) . '</p>';
}
/**
* Add a form element where the user can change the customers
*/
public function meta_box_customers() {
$placeholder = __( 'Who are you starting a conversation with?', 'supportflow' );
if ( 'draft' == get_post_status( get_the_ID() ) ) {
$customers_string = get_post_meta( get_the_ID(), '_sf_autosave_customers', true );
} else {
$customers = SupportFlow()->get_ticket_customers( get_the_ID(), array( 'fields' => 'emails' ) );
$customers_string = implode( ', ', $customers );
$customers_string .= empty( $customers_string ) ? '' : ', ';
}
echo '<h4>' . __( 'Customer(s)', 'supportflow' ) . '</h4>';
echo '<input type="text" id="customers" name="customers" class="sf_autosave" placeholder="' . $placeholder . '" value="' . esc_attr( $customers_string ) . '" autocomplete="off" />';
echo '<p class="description">' . __( 'Enter each customer email address, separated with a comma', 'supportflow' ) . '</p>';
}
/**
* Add a form element where you can choose cc and bcc receiver of reply
*/
public function meta_box_cc_bcc() {
$cc_value = esc_attr( get_post_meta( get_the_ID(), '_sf_autosave_cc', true ) );
$bcc = esc_attr( get_post_meta( get_the_ID(), '_sf_autosave_bcc', true ) );
?>
<p class="description"> <?php _e( "Please add all the E-Mail ID's seperated by comma.", 'supportflow' ) ?></p>
<h4 class="inline"><?php _e( "CC: ", 'supportflow' ) ?></h4>
<input type="text" class="sf_autosave" id="cc" name="cc" value="<?php echo $cc_value ?>" />
<h4 class="inline"> <?php _e( "BCC: ", 'supportflow' ) ?></h4>
<input type="text" class="sf_autosave" id="bcc" name="bcc" value="<?php echo $bcc ?>" />
<?php
}
/**
* Standard listing of replies includes a form at the top
* and any existing replies listed in reverse chronological order
*/
public function meta_box_replies() {
global $pagenow;
$email_account_id = get_post_meta( get_the_ID(), 'email_account', true );
$email_account = SupportFlow()->extend->email_accounts->get_email_account( $email_account_id );
$ticket_lock = ( null == $email_account && '' != $email_account_id );
$disabled_attr = disabled( $ticket_lock, true, false );
$submit_attr_array = $ticket_lock ? array( 'disabled' => 'true' ) : array();
if ( $ticket_lock ) {
$placeholder = __( "Ticket is locked permanently because E-Mail account associated with it is deleted. Please create a new ticket now. You can't now reply to it.", 'supportflow' );
} else {
$placeholders = array(
__( "What's burning?", 'supportflow' ),
__( 'What do you need to get off your chest?', 'supportflow' ),
);
$rand = array_rand( $placeholders );
$placeholder = $placeholders[$rand];
}
echo '<div class="alignleft"><h4>' . __( 'Conversation', 'supportflow' ) . '</h4></div>';
echo '<div class="alignright">';
SupportFlow()->extend->predefined_replies->get_dropdown_input();
echo '</div>';
echo '<div id="ticket-reply-box">';
echo "<textarea id='reply' name='reply' $disabled_attr class='ticket-reply sf_autosave' rows='4' placeholder='" . esc_attr( $placeholder ) . "'>";
echo esc_html( get_post_meta( get_the_ID(), '_sf_autosave_reply', true ) );
echo "</textarea>";
echo '<div id="message-tools">';
echo '<div id="replies-attachments-wrap">';
echo '<div class="drag-drop-buttons">';
echo '<input id="reply-attachment-browse-button" ' . $disabled_attr . ' type="button" value="' . esc_attr( __( 'Attach files', 'supportflow' ) ) . '" class="button" />';
echo '</div>';
echo '<ul id="replies-attachments-list">';
echo '</ul>';
echo '<input type="hidden" id="reply-attachments" name="reply-attachments" value="," />';
echo '</div>';
echo '<div id="submit-action">';
$signature_label_title = __( 'Append your signature at the bottom of the reply. Signature can be removed or changed in preferences page', 'supportflow' );
$insert_signature_default = (boolean) get_user_meta( get_current_user_id(), 'sf_insert_signature_default', true );
echo '<input type="checkbox" ' . $disabled_attr . checked( $insert_signature_default, true, false ) . ' id="insert-signature" name="insert-signature" />';
echo "<label for='insert-signature' title='$signature_label_title'>" . __( 'Insert signature', 'supportflow' ) . '</label>';
echo '<input type="checkbox" ' . $disabled_attr . ' id="mark-private" name="mark-private" />';
echo '<label for="mark-private">' . __( 'Mark private', 'supportflow' ) . '</label>';
if ( 'post-new.php' == $pagenow ) {
$submit_text = __( 'Start Ticket', 'supportflow' );
} else {
$submit_text = __( 'Send Message', 'supportflow' );
}
submit_button( $submit_text, 'primary save-button', 'insert-reply', false, $submit_attr_array );
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="clear"></div>';
$this->display_ticket_replies();
}
public function display_ticket_replies() {
$all_replies = array(
'private' => SupportFlow()->get_ticket_replies( get_the_ID(), array( 'status' => 'private' ) ),
'public' => SupportFlow()->get_ticket_replies( get_the_ID(), array( 'status' => 'public' ) ),
);
foreach ( $all_replies as $status => $replies ) {
if ( empty( $replies ) ) {
continue;
}
$class = 'private' == $status ? 'private-replies' : 'ticket-replies';
echo "<ul class='$class'>";
foreach ( $replies as $reply ) {
echo '<li>';
if ( 'public' == $status ) {
$reply_author = get_post_meta( $reply->ID, 'reply_author', true );
$reply_author_email = get_post_meta( $reply->ID, 'reply_author_email', true );
echo '<div class="reply-avatar">' . get_avatar( $reply_author_email, 72 );
echo '<p class="reply-author">' . esc_html( $reply_author ) . '</p>';
echo '</div>';
}
echo '<div class="ticket-reply">';
$post_content = wpautop( stripslashes( $reply->post_content ) );
$post_content = make_clickable( $post_content );
$post_content = $this->hide_quoted_text( $post_content );
echo $post_content;
if ( $attachment_ids = get_post_meta( $reply->ID, 'sf_attachments' ) ) {
echo '<ul class="ticket-reply-attachments">';
foreach ( $attachment_ids as $attachment_id ) {
$attachment_link = SupportFlow()->extend->attachments->get_attachment_url( $attachment_id );
echo '<li><a target="_blank" href="' . esc_url( $attachment_link ) . '">' . esc_html( get_the_title( $attachment_id ) ) . '</a></li>';
}
echo '</ul>';
}
echo '</div>';
if ( 'private' == $status ) {
$reply_author = get_post_meta( $reply->ID, 'reply_author', true );