Repository navigation
Expand file tree
/
Copy pathfunctions-post.php
More file actions
2470 lines (1967 loc) · 68.6 KB
/
Copy pathfunctions-post.php
File metadata and controls
2470 lines (1967 loc) · 68.6 KB
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
/**
* Delete ticket count cache on ticket update
*
* @since 3.0.0
* @param null
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function wpas_clean_ticketcount_cache() {
set_site_transient( 'wpas_tickets_counts', null, 24 * HOUR_IN_SECONDS );
// Delete all cached ticket counts
$cache_keys = get_option( 'wpas_tickets_count_cache_keys', array() );
if ( ! empty( $cache_keys ) ) {
foreach ( $cache_keys as $key ) {
delete_transient( $key );
}
delete_option( 'wpas_tickets_count_cache_keys' );
}
// Fallback backup: delete all transients matching wpas_cnt_ directly from options table
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_wpas_cnt_%' OR option_name LIKE '_transient_timeout_wpas_cnt_%'" );
}
add_action( 'save_post_ticket', 'wpas_clean_ticketcount_cache' );
add_action( 'delete_post', 'wpas_clean_ticketcount_cache' );
add_action( 'trash_post', 'wpas_clean_ticketcount_cache' );
add_action( 'untrash_post', 'wpas_clean_ticketcount_cache' );
add_action( 'wpas_ticket_after_saved', 'wpas_clean_ticketcount_cache' );
/**
* 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;
$bypass_pre_checks = isset( $data['bypass_pre_checks'] ) ? boolval(wp_kses( $data['bypass_pre_checks'], false ) ) : false; // Bypass pre-submission filter checks?
/**
* Prepare vars
*/
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$submit = isset( $_POST['_wp_http_referer'] ) ? wpas_get_submission_page_url( url_to_postid( sanitize_text_field( wp_unslash( $_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' );
if ( ! is_array( $submission_pages ) ) {
$submission_pages = (array) $submission_pages;
}
if( $submission_pages && count( $submission_pages ) > 0 ) {
$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_safe_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_safe_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_safe_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 = true ;
if ( ! $bypass_pre_checks ) {
$go = apply_filters( 'wpas_before_submit_new_ticket_checks', true );
}
/* Check for the green light */
if ( is_wp_error( $go ) ) {
/* Retrieve error message. */
$message = $go->get_error_message();
/* Save the input */
wpas_save_values();
/* Redirect to submit page */
wpas_add_error( 'validation_issue', $message );
wp_safe_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_safe_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, 'standard-ticket-form' );
}
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_safe_redirect( wp_sanitize_redirect( home_url( isset( $_POST['_wp_http_referer']) ? sanitize_text_field( wp_unslash( $_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 referrer since ticket creation failed....
*/
wpas_add_error( 'submission_error', __( 'The ticket couldn\'t be submitted for an unknown reason.', 'awesome-support' ) );
wp_safe_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
*/
if ( ! empty( wpas_get_option( 'new_ticket_redirect_fe', '' ) ) ) {
wpas_redirect( 'ticket_added', wpas_get_option( 'new_ticket_redirect_fe', '' ), $ticket_id );
} else {
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
* @param string $channel_term Source of the ticket
*
* @return bool|int|WP_Error
*/
function wpas_insert_ticket( $data = array(), $post_id = false, $agent_id = false, $channel_term = 'other' ) {
// Save the original data array
$incoming_data = $data;
// First of all we want to set the ticket author so that we can check if (s)he is allowed to open a ticket or not.
if ( empty( $data['post_author'] ) ) {
global $current_user;
$data['post_author'] = $current_user->ID;
}
if ( ! user_can( $data['post_author'], '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'] );
}
/**
* Sanitize the slug
*/
if ( isset( $data['post_name'] ) && ! empty( $data['post_name'] ) ) {
$data['post_name'] = sanitize_text_field( $data['post_name'] );
}
/**
* Filter the data right before inserting it in the post.
*
* @var array
*/
$data = apply_filters( 'wpas_open_ticket_data', $data, $incoming_data );
/**
* Fire wpas_before_open_ticket just before the post is actually
* inserted in the database.
*/
do_action( 'wpas_open_ticket_before', $data, $post_id, $incoming_data );
/**
* 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, $incoming_data );
return false;
}
/**
* Change the slug to the postid if that's the option the admin set in the TICKETS->SETTINGS->Advanced tab.
* Note that we only do this if $update is false signifying a new ticket!
*/
if ( ! $update ) {
wpas_set_ticket_slug( $ticket_id );
}
/* Update the channel on the ticket so that hooks can access it - but only if the $update is false which means we've got a new ticket */
/* It will need to be re-added to the ticket at the bottom of this routine because some hooks overwrite it with a blank. */
if ( ! empty( $channel_term ) && ( ! $update ) ) {
wpas_set_ticket_channel( $ticket_id, $channel_term, false );
}
/* Set the ticket as open. */
add_post_meta( $ticket_id, '_wpas_status', 'open', true );
/* Next - update other some meta values. If you add or delete from this list you also */
/* need to do the same thing in the /includes/admin/functions-post.php file */
add_post_meta( $ticket_id, '_wpas_last_reply_date', null, true );
add_post_meta( $ticket_id, '_wpas_last_reply_date_gmt', null, true );
add_post_meta( $ticket_id, '_wpas_is_waiting_client_reply', ! user_can( $data['post_author'], 'edit_ticket' ), 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, $incoming_data );
/**
* We might want to assign agent manually
*/
if( apply_filters( 'wpas_open_ticket_should_agent_assign', true, $ticket_id ) ) {
/* 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 );
}
/* Update the channel on the ticket - but only if the $update is false which means we've got a new ticket */
/* Need to update it here again because some of the action hooks fired above will overwrite the term. */
if ( ! empty( $channel_term ) && ( ! $update ) ) {
wpas_set_ticket_channel( $ticket_id, $channel_term, false );
}
/**
* Fire wpas_after_open_ticket just after the post is successfully submitted and assigned.
*/
do_action( 'wpas_open_ticket_after', $ticket_id, $data );
do_action( 'wpas_ticket_after_saved', $ticket_id );
//Delete ticket count cache on ticket update
wpas_clean_ticketcount_cache();
return $ticket_id;
}
/**
* Set the channel (ticket source) term/field
*
* @since 3.4.0
*
* @param numeric $ticket_id
* @param string $channel_term
* @param string $overwrite whether or not to overwrite existing channel on the ticket - set to false by default
*
* @return void
*/
function wpas_set_ticket_channel( $ticket_id = -1, $channel_term = 'other', $overwrite = false ) {
/* Does a term already exist on the ticket? If so, do not overwrite it if $overwrite is false */
if ( false === $overwrite ) {
$existing_channel = wp_get_post_terms( $ticket_id, 'ticket_channel' );
if ( ! empty( $existing_channel ) ) {
return;
}
}
/* Get the term id because wp_set_object_terms require an id instead of just a string */
$arr_the_term_id = term_exists( $channel_term, 'ticket_channel' );
if ( $arr_the_term_id ) {
// Need to get array keys first so we can index and extract the first element in the wp_set_object_terms below.
$arr_the_term_id_keys = array_keys( $arr_the_term_id );
$int_the_term_id = (int) $arr_the_term_id[ $arr_the_term_id_keys[0] ];
// Now add the terms (this function call doesn't work consistently for some reason!)
$term_taxonomy_ids = wp_set_object_terms( $ticket_id, (int) $int_the_term_id, 'ticket_channel' );
}
return;
}
/**
* Set ticket slug on new tickets if the admin chooses anything other than the default slug.
*
* @since 3.4.0
*
* @param numeric $ticket_id
*
* @return void
*/
function wpas_set_ticket_slug( $ticket_id = -1 ) {
$use_ticket_id_for_slug = wpas_get_option( 'ticket_topic_slug' );
/* Set ticket slug to the post id / ticket id */
if ( isset( $use_ticket_id_for_slug ) && ( 'ticketid' == $use_ticket_id_for_slug ) ) {
/* Set the data to be updated - in this case just post_name (slug) with the key being the ID passed into this function */
$newdata = array(
'ID' => $ticket_id,
'post_name' => (string) $ticket_id,
);
/* Update the post with the new slug */
wp_update_post( $newdata );
}
/* Set ticket slug to a random number */
if ( isset( $use_ticket_id_for_slug ) && ( 'randomnumber' == $use_ticket_id_for_slug ) ) {
/*Calculate a random number */
$randomslug = wp_rand();
/* Set the data to be updated - in this case just post_name (slug) with the key being the ID passed into this function */
$newdata = array(
'ID' => $ticket_id,
'post_name' => (string) $randomslug,
);
/* Update the post with the new slug */
wp_update_post( $newdata );
}
/* Set ticket slug to a GUID */
if ( isset( $use_ticket_id_for_slug ) && ( 'guid' == $use_ticket_id_for_slug ) ) {
/*Calculate a guid */
$randomguid = wpas_create_pseudo_guid();
/* Set the data to be updated - in this case just post_name (slug) with the key being the ID passed into this function */
$newdata = array(
'ID' => $ticket_id,
'post_name' => $randomguid,
);
/* Update the post with the new slug */
wp_update_post( $newdata );
}
return;
}
/**
* 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, $return_count = 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',
);
}
}
if( $return_count == true ){
$args['no_found_rows'] = false;
$args['posts_per_page'] = 1;
$args['fields'] = 'ids';
}
$query = new WP_Query( $args );
if( $return_count == true ){
return $query->found_posts;
}
if ( empty( $query->posts ) ) {
return array();
} else {
return $query->posts;
}
}
/**
* Get ticket by ticket id and user id.
*
* @since 5.1.1
*
* @param int $id Ticket ID
* @param array $args Additional arguments (see WP_Query)
* @param bool $cache Whether or not to cache the results
*
* @return array
*/
function wpas_get_ticket_by_id( $id, $args = array(), $cache = false ) {
$defaults = [
'p' => intval( $id ),
'post_type' => 'ticket',
'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 );
$query = new WP_Query( $args );
if ( empty( $query->posts ) ) {
return array();
} else {
return $query->posts[0];
}
}
/**
* 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;
}
}
// translators: %s is the ticket reply.
$x_content = __( 'Reply to ticket %s', 'awesome-support' );
/**
* 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( $x_content, "#$parent_id" ),
'post_title' => sprintf( $x_content, "#$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( !wpas_can_reply_ticket( false, $parent_id ) ) return false; // Cheating? Hehe..
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'] ) && apply_filters( 'wpas_user_can_reply_ticket', true, $parent_id ) ) {
/* 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 );
}
$closed = false;
/* Possibly close the ticket */
if ( $close && apply_filters( 'wpas_user_can_close_ticket', true, $parent_id ) ) {
$closed = wpas_close_ticket( $parent_id );
// Redirect now if no reply was posted
if ( ! isset( $reply_id ) && $closed ) {
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 ( $closed ) {
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;
}
}
}
}
/**
* Update a reply with its edited version
*
* @since 3.3.0
*
* @param $int $reply_id - the id of the reply being edited.
* @param array $content - the new content. If blank, the function will attempt to pull the new content from $_POST.
*
* @return void
*/
function wpas_edit_reply( $reply_id = null, $content = '' ) {
if ( is_null( $reply_id ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['reply_id'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$reply_id = intval( $_POST['reply_id'] );
} else {
return false;
}
}
if ( empty( $content ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['reply_content'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$reply = isset($_POST['reply_content'] ) ? wp_kses_post( wp_unslash( $_POST['reply_content'] ) ) : "";
$content = wp_kses( $reply, wp_kses_allowed_html( 'post' ) );
} else {
return false;
}
}
//ensuring that the user is allowed to edit the post given by the reply_id POST parameter
if( !current_user_can( 'edit_post', $reply_id ) )
{
return false;
}
$original_reply = get_post( $reply_id );
if ( is_null( $original_reply ) ) {
return false;
}
//the post type should be checked so arbitrary posts cannot be updated
if( $original_reply->post_type != 'ticket_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' => $original_reply->post_date,
'post_date_gmt' => $original_reply->post_date_gmt,
'post_name' => $original_reply->post_name,
'post_parent' => $original_reply->post_parent,
'post_type' => $original_reply->post_type,
'post_author' => $original_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;
}
/* Add a flag to the reply that shows it was edited */
update_post_meta( $edited, 'wpas_reply_was_edited', '1' );
/* Fire the after-edit action hook */
do_action( 'wpas_reply_edited', $reply_id, $original_reply );
return $reply_id;
}
add_action( 'wpas_reply_edited', 'wpas_log_reply_edits', 10, 2 );
/**
* Log the original contents of a reply after it is edited.
*
* Action hook: wpas_reply_edited
*
* @since 5.2.0
*
* @param $int $reply_id - the id of the reply being edited.
* @param array $original_reply - the original post before the edit reply was added to the database
*
* @TODO: Somehow this hook is getting called three times for every edit when the logging level is LOW. 3 entries end up in the log for every single edit.
*
* @return void
*/
function wpas_log_reply_edits( $reply_id, $original_reply ) {
/* Do we log a summary or detail that includes the original content? */
if ( 'low' === wpas_get_option( 'log_content_edit_level', 'low' ) ) {
$reply_contents_to_log = __( 'Original data not available because detailed logging is not turned on or allowed', 'awesome-support' );
} else {
$reply_contents_to_log = $original_reply->post_content;
}
// translators: %1$s is the reply number, %2$s is the ticket number.
$x_content = __( 'Reply #%1$s located on ticket #%2$s was edited.', 'awesome-support' );
wpas_log_edits( $reply_id, sprintf( $x_content, (string) $reply_id, (string) $original_reply->post_parent ), $reply_contents_to_log );
}
/**
* Mark a reply as read
*
* @since 3.3.0
*
* @param $int $reply_id - the id of the reply being marked as read.
*
* @return void
*/
function wpas_mark_reply_read( $reply_id = null ) {
if ( is_null( $reply_id ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['reply_id'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$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() {
/* Security: verify nonce */
if ( ! check_ajax_referer( 'wpas-mark-reply-read', 'security', false ) ) {
wp_send_json_error( array( 'message' => __( 'Session expired. Please refresh the page and try again.', 'awesome-support' ) ), 403 );
}
/* Security: check capability BEFORE any state change */
if ( ! current_user_can( 'edit_ticket' ) ) {
wp_send_json_error( array( 'message' => __( 'Unauthorized action. You do not have permission to mark a ticket reply as read with Ajax.', 'awesome-support' ) ), 403 );
}
/* Validate reply_id and post_type */
$reply_id = isset( $_POST['reply_id'] ) ? intval( $_POST['reply_id'] ) : 0;
if ( ! $reply_id ) {
wp_send_json_error( array( 'message' => __( 'Invalid reply ID.', 'awesome-support' ) ), 400 );
}
$reply = get_post( $reply_id );
if ( ! $reply || 'ticket_reply' !== $reply->post_type ) {
wp_send_json_error( array( 'message' => __( 'Invalid reply.', 'awesome-support' ) ), 400 );
}
/* All checks passed — now perform the state change */
$ID = wpas_mark_reply_read( $reply_id );
if ( false === $ID || is_wp_error( $ID ) ) {
$ID = is_wp_error( $ID ) ? $ID->get_error_message() : '';
}