forked from Awesome-Support/Awesome-Support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions-post.php
1526 lines (1194 loc) · 38.6 KB
/
functions-post.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
/**
* Open a new ticket.
*
* @since 3.0.0
* @param array $data Ticket data
* @return boolean
*/
function wpas_open_ticket( $data ) {
$title = isset( $data['title'] ) ? wp_strip_all_tags( $data['title'] ) : false;
$content = isset( $data['message'] ) ? wp_kses( $data['message'], wp_kses_allowed_html( 'post' ) ) : false;
/**
* Prepare vars
*/
$submit = isset( $_POST['_wp_http_referer'] ) ? wpas_get_submission_page_url( url_to_postid( $_POST['_wp_http_referer'] ) ) : wpas_get_submission_page_url();
// Fallback in case the referrer failed
if ( empty( $submit ) ) {
$submission_pages = wpas_get_option( 'ticket_submit' );
$submit = $submission_pages[0];
$submit = wp_sanitize_redirect( get_permalink( $submit ) );
}
// Verify user capability
if ( !current_user_can( 'create_ticket' ) ) {
// Save the input
wpas_save_values();
// Redirect to submit page
wpas_add_error( 'cannot_open_ticket', __( 'You do not have the capacity to open a new ticket.', 'awesome-support' ) );
wp_redirect( $submit );
// Break
exit;
}
// Make sure we have at least a title and a message
if ( false === $title || empty( $title ) ) {
// Save the input
wpas_save_values();
// Redirect to submit page
wpas_add_error( 'missing_title', __( 'It is mandatory to provide a title for your issue.', 'awesome-support' ) );
wp_redirect( $submit );
// Break
exit;
}
if ( true === ( $description_mandatory = apply_filters( 'wpas_ticket_submission_description_mandatory', true ) ) && ( false === $content || empty( $content ) ) ) {
// Save the input
wpas_save_values();
// Redirect to submit page
wpas_add_error( 'missing_description', __( 'It is mandatory to provide a description for your issue.', 'awesome-support' ) );
wp_redirect( $submit );
// Break
exit;
}
/**
* Allow the submission.
*
* This variable is used to add additional checks in the submission process.
* If the $go var is set to true, it gives a green light to this method
* and the ticket will be submitted. If the var is set to false, the process
* will be aborted.
*
* @since 3.0.0
*/
$go = apply_filters( 'wpas_before_submit_new_ticket_checks', true );
/* Check for the green light */
if ( is_wp_error( $go ) ) {
/* Retrieve error messages. */
$messages = $go->get_error_messages();
/* Save the input */
wpas_save_values();
/* Redirect to submit page */
wpas_add_error( 'validation_issue', $messages );
wp_redirect( $submit );
exit;
}
/**
* Gather current user info
*/
if ( is_user_logged_in() ) {
global $current_user;
$user_id = $current_user->ID;
} else {
// Save the input
wpas_save_values();
// Redirect to submit page
wpas_add_error( 'unknown_user', __( 'Only registered accounts can submit a ticket. Please register first.', 'awesome-support' ) );
wp_redirect( $submit );
exit;
}
/**
* Submit the ticket.
*
* Now that all the verifications are passed
* we can proceed to the actual ticket submission.
*/
$post = apply_filters( 'wpas_open_ticket_data', array(
'post_content' => $content,
'post_name' => $title,
'post_title' => $title,
'post_status' => 'queued',
'post_type' => 'ticket',
'post_author' => $user_id,
'ping_status' => 'closed',
'comment_status' => 'closed',
) );
return wpas_insert_ticket( $post, false, false );
}
add_action( 'wpas_do_submit_new_ticket', 'wpas_new_ticket_submission' );
/**
* Instantiate a new ticket submission
*
* This helper function is used to trigger the creation of a new ticket
* after the ticket submission form is posted on the front-end.
*
* @since 3.3
*
* @param array $data Ticket data required to open a new ticket
*
* @return void
*/
function wpas_new_ticket_submission( $data ) {
if ( ! is_admin() && isset( $data['wpas_title'] ) ) {
// Verify the nonce first
if ( ! isset( $data['wpas_nonce'] ) || ! wp_verify_nonce( $data['wpas_nonce'], 'new_ticket' ) ) {
/* Save the input */
wpas_save_values();
// Redirect to submit page
wpas_add_error( 'nonce_verification_failed', __( 'The authenticity of your submission could not be validated. If this ticket is legitimate please try submitting again.', 'awesome-support' ) );
wp_redirect( wp_sanitize_redirect( home_url( $_POST['_wp_http_referer'] ) ) );
exit;
}
$ticket_id = wpas_open_ticket( array( 'title' => $data['wpas_title'], 'message' => $data['wpas_message'] ) );
/* Submission failure */
if ( false === $ticket_id ) {
/* Save the input */
wpas_save_values();
/**
* Redirect to the newly created ticket
*/
wpas_add_error( 'submission_error', __( 'The ticket couldn\'t be submitted for an unknown reason.', 'awesome-support' ) );
wp_redirect( wp_sanitize_redirect( home_url( $data['_wp_http_referer'] ) ) );
exit;
} /* Submission succeeded */
else {
/**
* Empty the temporary sessions
*/
WPAS()->session->clean( 'submission_form' );
/**
* Redirect to the newly created ticket
*/
wpas_redirect( 'ticket_added', get_permalink( $ticket_id ), $ticket_id );
exit;
}
}
}
/**
* Insert a new ticket in the database
*
* This function is a wrapper function for wp_insert_post
* with additional checks specific to the ticketing system
*
* @param array $data Ticket (post) data
* @param bool|int $post_id Post ID for an update
* @param bool|int $agent_id ID of the agent to assign ticket to
*
* @return bool|int|WP_Error
*/
function wpas_insert_ticket( $data = array(), $post_id = false, $agent_id = false ) {
if ( ! current_user_can( 'create_ticket' ) ) {
return false;
}
$update = false;
/* If a post ID is passed we make sure the post actually exists before trying to update it. */
if ( false !== $post_id ) {
$post = get_post( intval( $post_id ) );
if ( is_null( $post ) ) {
return false;
}
$update = true;
}
$defaults = array(
'post_content' => '',
'post_name' => '',
'post_title' => '',
'post_status' => 'queued',
'post_type' => 'ticket',
'post_author' => '',
'ping_status' => 'closed',
'comment_status' => 'closed',
);
/* Add the post ID if this is an update. */
if ( $update ) {
$defaults['ID'] = $post_id;
}
/* Parse the input data. */
$data = wp_parse_args( $data, $defaults );
/* Sanitize the data */
if ( isset( $data['post_title'] ) && ! empty( $data['post_title'] ) ) {
$data['post_title'] = wp_strip_all_tags( $data['post_title'] );
}
if ( ! empty( $data['post_content'] ) ) {
$data['post_content'] = strip_shortcodes( $data['post_content'] );
}
/**
* Filter the data right before inserting it in the post.
*
* @var array
*/
$data = apply_filters( 'wpas_open_ticket_data', $data );
if ( isset( $data['post_name'] ) && !empty( $data['post_name'] ) ) {
$data['post_name'] = sanitize_text_field( $data['post_name'] );
}
/* Set the current user as author if the field is empty. */
if ( empty( $data['post_author'] ) ) {
global $current_user;
$data['post_author'] = $current_user->ID;
}
/**
* Fire wpas_before_open_ticket just before the post is actually
* inserted in the database.
*/
do_action( 'wpas_open_ticket_before', $data, $post_id );
/**
* Insert the post in database using the regular WordPress wp_insert_post
* function with default values corresponding to our post type structure.
*
* @var boolean
*/
$ticket_id = wp_insert_post( $data, false );
if ( false === $ticket_id ) {
/**
* Fire wpas_open_ticket_failed if the ticket couldn't be inserted.
*/
do_action( 'wpas_open_ticket_failed', $data, $post_id );
return false;
}
/* Set the ticket as open. */
add_post_meta( $ticket_id, '_wpas_status', 'open', true );
if ( false === $agent_id ) {
$agent_id = wpas_find_agent( $ticket_id );
}
/**
* Fire wpas_open_ticket_before_assigned after the post is successfully submitted but before it has been assigned to an agent.
*
* @since 3.2.6
*/
do_action( 'wpas_open_ticket_before_assigned', $ticket_id, $data );
/* Assign an agent to the ticket */
wpas_assign_ticket( $ticket_id, apply_filters( 'wpas_new_ticket_agent_id', $agent_id, $ticket_id, $agent_id ), false );
/**
* Fire wpas_after_open_ticket just after the post is successfully submitted and assigned.
*/
do_action( 'wpas_open_ticket_after', $ticket_id, $data );
return $ticket_id;
}
/**
* Get tickets.
*
* Get a list of tickets matching the arguments passed.
* This function is basically a wrapper for WP_Query with
* the addition of the ticket status.
*
* @since 3.0.0
*
* @param string $ticket_status Ticket status (open or closed)
* @param array $args Additional arguments (see WP_Query)
* @param string|array $post_status Ticket state
* @param bool $cache Whether or not to cache the results
*
* @return array Array of tickets, empty array if no tickets found
*/
function wpas_get_tickets( $ticket_status = 'open', $args = array(), $post_status = 'any', $cache = false ) {
$custom_post_status = wpas_get_post_status();
$post_status_clean = array();
if ( empty( $post_status ) ) {
$post_status = 'any';
}
if ( ! is_array( $post_status ) ) {
if ( 'any' === $post_status ) {
foreach ( $custom_post_status as $status_id => $status_label ) {
$post_status_clean[] = $status_id;
}
$post_status = $post_status_clean;
} else {
if ( ! array_key_exists( $post_status, $custom_post_status ) ) {
$post_status = ''; // This basically will return no result if the post status specified doesn't exist
}
}
} else {
foreach ( $post_status as $key => $status ) {
if ( ! array_key_exists( $status, $custom_post_status ) ) {
unset( $post_status[ $key ] );
}
}
}
$defaults = array(
'post_type' => 'ticket',
'post_status' => $post_status,
'posts_per_page' => - 1,
'no_found_rows' => ! (bool) $cache,
'cache_results' => (bool) $cache,
'update_post_term_cache' => (bool) $cache,
'update_post_meta_cache' => (bool) $cache,
'wpas_query' => true, // We use this parameter to identify our own queries so that we can remove the author parameter
);
$args = wp_parse_args( $args, $defaults );
if ( 'any' !== $ticket_status ) {
if ( in_array( $ticket_status, array( 'open', 'closed' ) ) ) {
$args['meta_query'][] = array(
'key' => '_wpas_status',
'value' => $ticket_status,
'compare' => '=',
'type' => 'CHAR'
);
}
}
$query = new WP_Query( $args );
if ( empty( $query->posts ) ) {
return array();
} else {
return $query->posts;
}
}
/**
* Add a new reply to a ticket.
*
* @param array $data The reply data to insert
* @param boolean|integer $parent_id ID of the parent ticket (post)
* @param boolean|integer $author_id The ID of the reply author (false if none)
*
* @return boolean|integer False on failure or reply ID on success
*/
function wpas_add_reply( $data, $parent_id = false, $author_id = false ) {
if ( false === $parent_id ) {
if ( isset( $data['parent_id'] ) ) {
/* Get the parent ID from $data if not provided in the arguments. */
$parent_id = intval( $data['parent_id'] );
$parent = get_post( $parent_id );
/* Mare sure the parent exists. */
if ( is_null( $parent ) ) {
return false;
}
} else {
return false;
}
}
/**
* Submit the reply.
*
* Now that all the verifications are passed
* we can proceed to the actual ticket submission.
*/
$defaults = array(
'post_content' => '',
'post_name' => sprintf( __( 'Reply to ticket %s', 'awesome-support' ), "#$parent_id" ),
'post_title' => sprintf( __( 'Reply to ticket %s', 'awesome-support' ), "#$parent_id" ),
'post_status' => 'unread',
'post_type' => 'ticket_reply',
'ping_status' => 'closed',
'comment_status' => 'closed',
'post_parent' => $parent_id,
);
$data = wp_parse_args( $data, $defaults );
if ( false !== $author_id ) {
$data['post_author'] = $author_id;
} else {
global $current_user;
$data['post_author'] = $current_user->ID;
}
$insert = wpas_insert_reply( $data, $parent_id );
return $insert;
}
add_action( 'wpas_do_submit_new_reply', 'wpas_new_reply_submission' );
/**
* Instantiate a new reply submission
*
* This helper function is used to trigger the creation of a new reply
* after the reply submission form is posted on the front-end.
*
* @since 3.3
*
* @param array $data Reply data required to open a new ticket
*
* @return void
*/
function wpas_new_reply_submission( $data ) {
// Get parent ticket ID
$parent_id = (int) $data['ticket_id'];
if ( 'ticket' !== get_post_type( $parent_id ) ) {
wpas_add_error( 'reply_added_failed', __( 'Something went wrong. We couldn't identify your ticket. Please try again.', 'awesome-support' ) );
wpas_redirect( 'reply_added_failed', get_permalink( $parent_id ) );
exit;
}
// Define if the ticket must be closed
$close = isset( $data['wpas_close_ticket'] ) ? true : false;
if ( ! empty( $data['wpas_user_reply'] ) ) {
/* Sanitize the data */
$data = array( 'post_content' => wp_kses( $data['wpas_user_reply'], wp_kses_allowed_html( 'post' ) ) );
/* Add the reply */
$reply_id = wpas_add_reply( $data, $parent_id );
}
/* Possibly close the ticket */
if ( $close ) {
wpas_close_ticket( $parent_id );
// Redirect now if no reply was posted
if ( ! isset( $reply_id ) ) {
wpas_add_notification( 'ticket_closed', __( 'The ticket was successfully closed', 'awesome-support' ) );
wpas_redirect( 'ticket_closed', get_permalink( $parent_id ) );
exit;
}
}
if ( isset( $reply_id ) ) {
if ( false === $reply_id ) {
wpas_add_error( 'reply_added_failed', __( 'Your reply could not be submitted for an unknown reason.', 'awesome-support' ) );
wpas_redirect( 'reply_added_failed', get_permalink( $parent_id ) );
exit;
} else {
if ( $close ) {
wpas_add_notification( 'reply_added_closed', __( 'Thanks for your reply. The ticket is now closed.', 'awesome-support' ) );
} else {
wpas_add_notification( 'reply_added', __( 'Your reply has been submitted. Your agent will reply ASAP.', 'awesome-support' ) );
}
if ( false !== $link = wpas_get_reply_link( $reply_id ) ) {
wpas_redirect( 'reply_added', $link );
exit;
}
}
}
}
function wpas_edit_reply( $reply_id = null, $content = '' ) {
if ( is_null( $reply_id ) ) {
if ( isset( $_POST['reply_id'] ) ) {
$reply_id = intval( $_POST['reply_id'] );
} else {
return false;
}
}
if ( empty( $content ) ) {
if ( isset( $_POST['reply_content'] ) ) {
$content = wp_kses( $_POST['reply_content'], wp_kses_allowed_html( 'post' ) );
} else {
return false;
}
}
$reply = get_post( $reply_id );
if ( is_null( $reply ) ) {
return false;
}
$data = apply_filters( 'wpas_edit_reply_data', array(
'ID' => $reply_id,
'post_content' => $content,
'post_status' => 'read',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_date' => $reply->post_date,
'post_date_gmt' => $reply->post_date_gmt,
'post_name' => $reply->post_name,
'post_parent' => $reply->post_parent,
'post_type' => $reply->post_type,
'post_author' => $reply->post_author,
), $reply_id
);
$edited = wp_insert_post( $data, true );
if ( is_wp_error( $edited ) ) {
do_action( 'wpas_edit_reply_failed', $reply_id, $content, $edited );
return $edited;
}
do_action( 'wpas_reply_edited', $reply_id );
return $reply_id;
}
function wpas_mark_reply_read( $reply_id = null ) {
if ( is_null( $reply_id ) ) {
if ( isset( $_POST['reply_id'] ) ) {
$reply_id = intval( $_POST['reply_id'] );
} else {
return false;
}
}
$reply = get_post( $reply_id );
if ( is_null( $reply ) ) {
return false;
}
if ( 'read' === $reply->post_status ) {
return $reply_id;
}
$data = apply_filters( 'wpas_mark_reply_read_data', array(
'ID' => $reply_id,
'post_status' => 'read',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $reply->post_content,
'post_date' => $reply->post_date,
'post_date_gmt' => $reply->post_date_gmt,
'post_name' => $reply->post_name,
'post_parent' => $reply->post_parent,
'post_type' => $reply->post_type,
'post_author' => $reply->post_author,
), $reply_id
);
$edited = wp_insert_post( $data, true );
if ( is_wp_error( $edited ) ) {
do_action( 'wpas_mark_reply_read_failed', $reply_id, $edited );
return $edited;
}
do_action( 'wpas_marked_reply_read', $reply_id );
return $edited;
}
add_action( 'wp_ajax_wpas_mark_reply_read', 'wpas_mark_reply_read_ajax' );
/**
* Mark a ticket reply as read with Ajax
*
* @return void
*/
function wpas_mark_reply_read_ajax() {
$ID = wpas_mark_reply_read();
if ( false === $ID || is_wp_error( $ID ) ) {
$ID = $ID->get_error_message();
}
echo $ID;
die();
}
add_action( 'wp_ajax_wpas_edit_reply', 'wpas_edit_reply_ajax' );
/**
* Edit a reply with Ajax
*
* @return void
*/
function wpas_edit_reply_ajax() {
$ID = wpas_edit_reply();
if ( false === $ID || is_wp_error( $ID ) ) {
$ID = $ID->get_error_message();
}
echo $ID;
die();
}
/**
* Insert a new reply.
*
* The function is basically a wrapper for wp_insert_post
* with some additional checks and new default arguments
* adapted to the needs of the ticket_reply post type.
* If also gives some useful hooks at different steps of
* the process.
*
* @since 3.0.0
* @param array $data Array of arguments for this reply
* @param boolean $post_id ID of the parent post
* @return integer|WP_Error The reply ID on success or WP_Error on failure
*/
function wpas_insert_reply( $data, $post_id = false ) {
if ( false === $post_id ) {
return false;
}
if ( !current_user_can( 'reply_ticket' ) ) {
return false;
}
$defaults = array(
'post_name' => sprintf( __( 'Reply to ticket %s', 'awesome-support' ), "#$post_id" ),
'post_title' => sprintf( __( 'Reply to ticket %s', 'awesome-support' ), "#$post_id" ),
'post_content' => '',
'post_status' => 'unread',
'post_type' => 'ticket_reply',
'post_author' => '',
'post_parent' => $post_id,
'ping_status' => 'closed',
'comment_status' => 'closed',
);
$data = wp_parse_args( $data, $defaults );
/* Set the current user as author if the field is empty. */
if ( empty( $data['post_author'] ) ) {
global $current_user;
$data['post_author'] = $current_user->ID;
}
$data = apply_filters( 'wpas_add_reply_data', $data, $post_id );
/* Sanitize the data */
if ( isset( $data['post_title'] ) && ! empty( $data['post_title'] ) ) {
$data['post_title'] = wp_strip_all_tags( $data['post_title'] );
}
if ( ! empty( $data['post_content'] ) ) {
$data['post_content'] = strip_shortcodes( $data['post_content'] );
}
if ( isset( $data['post_name'] ) && ! empty( $data['post_name'] ) ) {
$data['post_name'] = sanitize_title( $data['post_name'] );
}
/**
* Fire wpas_add_reply_before before the reply is added to the database.
* This hook is fired both on the back-end and the front-end.
*
* @param array $data The data to be inserted to the database
* @param integer $post_id ID of the parent post
*/
do_action( 'wpas_add_reply_before', $data, $post_id );
if ( is_admin() ) {
/**
* Fired right before the data is added to the database on the back-end only.
*
* @since 3.1.2
* @param array $data The data to be inserted to the database
* @param integer $post_id ID of the parent post
*/
do_action( 'wpas_add_reply_admin_before', $data, $post_id );
/**
* wpas_save_reply_before
*
* This hook is now deprecated but stays in the code for backward compatibility.
* Instead of wpas_save_reply_before you should now use wpas_add_reply_admin_before
*
* @deprecated 3.1.2
*/
do_action( 'wpas_save_reply_before' );
} else {
/**
* Fired right before the data is added to the database on the front-end only.
*
* @since 3.1.2
* @param array $data The data to be inserted to the database
* @param integer $post_id ID of the parent post
*/
do_action( 'wpas_add_reply_public_before', $data, $post_id );
}
/* This is where we actually insert the post */
$reply_id = wp_insert_post( $data, true );
if ( is_wp_error( $reply_id ) ) {
/**
* Fire wpas_add_reply_failed if the reply couldn't be inserted.
* This hook will be fired both in the admin and in the front-end.
*
* @param array $data The data we tried to add to the database
* @param integer $post_id ID of the parent post
* @param object $reply_id WP_Error object
*/
do_action( 'wpas_add_reply_failed', $data, $post_id, $reply_id );
if ( is_admin() ) {
/**
* Fired if the reply instertion failed.
* This hook will only be fired in the admin.
*
* @since 3.1.2
* @param array $data The data we tried to add to the database
* @param integer $post_id ID of the parent post
* @param object $reply_id WP_Error object
*/
do_action( 'wpas_add_reply_admin_failed', $data, $post_id, $reply_id );
/**
* wpas_save_reply_after_error hook
*
* This hook is deprecated but stays in the code for backward compatibility.
* You should now use wpas_add_reply_admin_failed instead.
*
* @deprecated 3.1.2
* @param $reply WP_Error object
*/
do_action( 'wpas_save_reply_after_error', $reply_id );
} else {
/**
* Fired if the reply instertion failed.
* This hook will only be fired in the frontèend.
*
* @since 3.1.2
* @param array $data The data we tried to add to the database
* @param integer $post_id ID of the parent post
* @param object $reply_id WP_Error object
*/
do_action( 'wpas_add_reply_public_failed', $data, $post_id, $reply_id );
}
return $reply_id;
}
/**
* Delete the activity transient.
*/
delete_transient( "wpas_activity_meta_post_$post_id" );
/**
* Fire wpas_add_reply_after after the reply was successfully added.
*/
do_action( 'wpas_add_reply_after', $reply_id, $data );
if ( is_admin() ) {
/**
* Fired right after the data is added to the database on the back-end only.
*
* @since 3.1.2
* @param integer $reply_id ID of the reply added to the database
* @param array $data Data inserted to the database
*/
do_action( 'wpas_add_reply_admin_after', $reply_id, $data );
/**
* wpas_save_reply_after hook
*
* This hook is deprecated but stays in the code for backward compatibility.
* You should now use wpas_add_reply_admin_after instead.
*
* @deprecated 3.1.2
* @param integer $reply Reply ID
* @param array $data Data used to add the reply
*/
do_action( 'wpas_save_reply_after', $reply_id, $data );
} else {
/**
* Fired right after the data is added to the database on the front-end only.
*
* @since 3.1.2
* @param integer $reply_id ID of the reply added to the database
* @param array $data Data inserted to the database
*/
do_action( 'wpas_add_reply_public_after', $reply_id, $data );
}
return $reply_id;
}
/**
* Get replies for a specific ticket
*
* @param integer $post_id ID of the post (ticket) to get the replies from
* @param string|array $status Status of the replies to get
* @param array $args Additional arguments (see WP_Query)
* @param string $output Type of data to return. wp_query for the WP_Query object, replies for the WP_Query posts
*
* @return array|WP_Query
*/
function wpas_get_replies( $post_id, $status = 'any', $args = array(), $output = 'replies' ) {
$allowed_status = array(
'any',
'read',
'unread'
);
if ( ! is_array( $status ) ) {
$status = (array) $status;
}
foreach ( $status as $key => $reply_status ) {
if ( ! in_array( $reply_status, $allowed_status ) ) {
unset( $status[ $key ] );
}
}
if ( empty( $status ) ) {
$status = 'any';
}
$defaults = array(
'post_parent' => $post_id,
'post_type' => 'ticket_reply',
'post_status' => $status,
'order' => wpas_get_option( 'replies_order', 'ASC' ),
'orderby' => 'date',
'posts_per_page' => - 1,
'no_found_rows' => true,
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
$args = wp_parse_args( $args, $defaults );
$replies = new WP_Query( $args );
if ( is_wp_error( $replies ) ) {
return $replies;
}
return 'wp_query' === $output ? $replies : $replies->posts;
}
/**
* Find an available agent to assign a ticket to.
*
* This is a super basic attribution system. It just finds the agent
* with the less tickets currently open.
*
* @since 3.0.0
*
* @param boolean|integer $ticket_id The ticket that needs an agent
*
* @return integer ID of the best agent for the job
*/
function wpas_find_agent( $ticket_id = false ) {
if ( defined( 'WPAS_DISABLE_AUTO_ASSIGN' ) && true === WPAS_DISABLE_AUTO_ASSIGN ) {
return apply_filters( 'wpas_find_available_agent', wpas_get_option( 'assignee_default' ), $ticket_id );
}
$users = shuffle_assoc( wpas_get_users( apply_filters( 'wpas_find_agent_get_users_args', array( 'cap' => 'edit_ticket' ) ) ) );
$agent = array();
foreach ( $users->members as $user ) {
$wpas_agent = new WPAS_Member_Agent( $user );
/**
* Make sure the user really is an agent and that he can currently be assigned
*/
if ( true !== $wpas_agent->is_agent() || false === $wpas_agent->can_be_assigned() ) {
continue;
}
$count = $wpas_agent->open_tickets(); // Total number of open tickets for this agent
if ( empty( $agent ) ) {
$agent = array( 'tickets' => $count, 'user_id' => $user->ID );
} else {
if ( $count < $agent['tickets'] ) {
$agent = array( 'tickets' => $count, 'user_id' => $user->ID );
}
}
}
if ( is_array( $agent ) && isset( $agent['user_id'] ) ) {
$agent_id = $agent['user_id'];
} else {