-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsupportflow.php
1144 lines (952 loc) · 35 KB
/
supportflow.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?" );
/**
* Plugin Name: SupportFlow
* Plugin URI: https://wordpress.org/plugins/supportflow/
* Description: Reinventing how you support your customers.
* Author: Daniel Bachhuber, Varun Agrawal, Alex Mills, Andrew Spittle
* Version: 0.7
*
* Text Domain: supportflow
* Domain Path: /languages/
*/
class SupportFlow {
/** Magic *****************************************************************/
/**
* SupportFlow uses many variables, most of which can be filtered to customize
* the way that it works. To prevent unauthorized access, these variables
* are stored in a private array that is magically updated using PHP 5.2+
* methods. This is to prevent third party plugins from tampering with
* essential information indirectly, which would cause issues later.
*
* @see SupportFlow::setup_globals()
* @var array
*/
private $data;
/** Not Magic *************************************************************/
/**
* @var obj Add-ons append to this (Akismet, etc...)
*/
public $extend;
/** Singleton *************************************************************/
/**
* @var SupportFlow The one true SupportFlow
*/
private static $instance;
/**
* Main SupportFlow Instance
*
* SupportFlow is fun
* Please load it only one time
* For this, we thank you
*
* Ensures that only one instance of SupportFlow exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since SupportFlow 0.1
* @staticvar array $instance
* @uses SupportFlow::setup_globals() Setup the globals needed
* @uses SupportFlow::includes() Include the required files
* @uses SupportFlow::setup_actions() Setup the hooks and actions
* @see SupportFlow()
* @return The one true SupportFlow
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new SupportFlow;
if ( self::$instance->requirements_ok() ) {
self::$instance->setup_globals();
self::$instance->includes();
self::$instance->setup_actions();
self::$instance->setup_late_globals();
}
}
return self::$instance;
}
/** Magic Methods *********************************************************/
/**
* A dummy constructor to prevent SupportFlow from being loaded more than once.
*
* @since SupportFlow 0.1
* @see SupportFlow::instance()
* @see SupportFlow();
*/
private function __construct() {
/* Do nothing here */
}
/**
* A dummy magic method to prevent SupportFlow from being cloned
*
* @since SupportFlow 0.1
*/
public function __clone() {
wp_die( __( 'Cheatin’ uh?' ) );
}
/**
* A dummy magic method to prevent SupportFlow from being unserialized
*
* @since SupportFlow 0.1
*/
public function __wakeup() {
wp_die( __( 'Cheatin’ uh?' ) );
}
/**
* Magic method for checking the existence of a certain custom field
*
* @since SupportFlow 0.1
*/
public function __isset( $key ) {
return isset( $this->data[$key] );
}
/**
* Magic method for getting SupportFlow varibles
*
* @since SupportFlow 0.1
*/
public function __get( $key ) {
return isset( $this->data[$key] ) ? $this->data[$key] : null;
}
/**
* Magic method for setting SupportFlow varibles
*
* @since SupportFlow 0.1
*/
public function __set( $key, $value ) {
$this->data[$key] = $value;
}
/** Private Methods *******************************************************/
/**
* Check SupportFlow Requirements.
*
* @access private
*/
private function requirements_ok() {
$this->data['requirements'] = array();
// Require the php imap extension.
if ( ! function_exists( 'imap_open' ) ) {
$this->data['requirements'][] = 'imap';
}
if ( ! has_action( 'admin_notices', array( $this, 'action_requirements_admin_notices' ) ) && ! empty( $this->data['requirements'] ) ) {
add_action( 'admin_notices', array( $this, 'action_requirements_admin_notices' ) );
}
return empty( $this->data['requirements'] );
}
/**
* Set some smart defaults to class variables. Allow some of them to be
* filtered to allow for early overriding.
*
* @since SupportFlow 0.1
* @access private
* @uses plugin_dir_path() To generate SupportFlow plugin path
* @uses plugin_dir_url() To generate SupportFlow plugin url
* @uses apply_filters() Calls various filters
*/
private function setup_globals() {
/** Version ***********************************************************/
$this->version = '0.7'; // SupportFlow version
/** Paths *************************************************************/
// Setup some base path and URL information
$this->file = __FILE__;
$this->script_dev = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$this->basename = apply_filters( 'supportflow_plugin_basenname', plugin_basename( $this->file ) );
$this->plugin_dir = apply_filters( 'supportflow_plugin_dir_path', plugin_dir_path( $this->file ) );
$this->plugin_url = apply_filters( 'supportflow_plugin_dir_url', plugin_dir_url( $this->file ) );
// Languages
$this->lang_dir = apply_filters( 'supportflow_lang_dir', trailingslashit( $this->plugin_dir . 'languages' ) );
/** Identifiers *******************************************************/
$this->post_type = apply_filters( 'supportflow_thread_post_type', 'sf_ticket' );
$this->predefinded_replies_type = apply_filters( 'supportflow_predefinded_replies_type', 'sf_predefs' );
$this->customers_tax = apply_filters( 'supportflow_customers_taxonomy', 'sf_customer' );
$this->tags_tax = apply_filters( 'supportflow_tags_taxonomy', 'sf_tags' );
$this->reply_type = apply_filters( 'supportflow_ticket_reply_type', 'sf_ticket' );
$this->email_term_prefix = 'sf-';
$this->ticket_secret_key = 'ticket_secret';
$this->post_statuses = apply_filters(
'supportflow_ticket_post_statuses', array(
'sf_new' => array(
'show_tickets' => true,
'label' => __( 'New', 'supportflow' ),
'label_count' => _n_noop( 'New <span class="count">(%s)</span>', 'New <span class="count">(%s)</span>', 'supportflow' ),
),
'sf_open' => array(
'show_tickets' => true,
'label' => __( 'Open', 'supportflow' ),
'label_count' => _n_noop( 'Open <span class="count">(%s)</span>', 'Open <span class="count">(%s)</span>', 'supportflow' ),
),
'sf_pending' => array(
'show_tickets' => true,
'label' => __( 'Pending', 'supportflow' ),
'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>', 'supportflow' ),
),
'sf_closed' => array(
'show_tickets' => false,
'label' => __( 'Closed', 'supportflow' ),
'label_count' => _n_noop( 'Closed <span class="count">(%s)</span>', 'Closed <span class="count">(%s)</span>', 'supportflow' ),
),
)
);
/** Misc **************************************************************/
$this->extend = new stdClass(); // Plugins add data here
$this->extend->ui = new stdClass(); // For UI-related plugins
$this->errors = new WP_Error(); // Feedback
}
/**
* Include required files
*
* @since SupportFlow 0.1
* @access private
* @todo Be smarter about conditionally loading code
* @uses is_admin() If in WordPress admin, load additional file
*/
private function includes() {
/** Core **************************************************************/
require_once( $this->plugin_dir . 'classes/class-supportflow-logger.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-json-api.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-attachments.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-emails.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-email-replies.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-permissions.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-email-accounts.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-predefined-replies.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-email-notifications.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-preferences.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-table.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-dashboard.php' );
/** Extensions ********************************************************/
require_once( $this->plugin_dir . 'classes/class-supportflow-ui-submissionform.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-ui-widget.php' );
require_once( $this->plugin_dir . 'classes/class-supportflow-statistics.php' );
/** Tools *************************************************************/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( $this->plugin_dir . '/classes/class-supportflow-wp-cli.php' );
}
# TODO: Akismet plugin?
/** Admin *************************************************************/
// Quick admin check and load if needed
if ( is_admin() ) {
require_once( $this->plugin_dir . 'classes/class-supportflow-admin.php' );
}
}
/**
* Setup the default hooks and actions
*
* @since SupportFlow 0.1
* @access private
* @uses add_action() To add various actions
*/
private function setup_actions() {
add_action( 'init', array( $this, 'action_init_register_post_type' ) );
add_action( 'init', array( $this, 'action_init_register_taxonomies' ) );
add_action( 'init', array( $this, 'action_init_register_post_statuses' ) );
add_action( 'init', array( $this, 'action_init_upgrade' ) );
add_filter( 'cron_schedules', array( $this, 'action_cron_schedules' ) );
add_action( 'init', array( $this, 'action_init_wp_schedule_event' ) );
add_filter( 'wp_insert_post_data', array( $this, 'filter_wp_insert_post_data' ), 10, 2 );
do_action_ref_array( 'supportflow_after_setup_actions', array( &$this ) );
}
/**
* Register the custom post type
*
* @since SupportFlow 0.1
* @uses register_post_type() To register the post type
*/
public function action_init_register_post_type() {
register_post_type(
$this->post_type, array(
'labels' => array(
'menu_name' => __( 'SupportFlow', 'supportflow' ),
'name' => __( 'Tickets', 'supportflow' ),
'singular_name' => __( 'Ticket', 'supportflow' ),
'all_items' => __( 'All Tickets', 'supportflow' ),
'add_new' => __( 'New Ticket', 'supportflow' ),
'add_new_item' => __( 'Start New Ticket', 'supportflow' ),
'edit_item' => __( 'Discussion', 'supportflow' ),
'new_item' => __( 'New Ticket', 'supportflow' ),
'view_item' => __( 'View Ticket', 'supportflow' ),
'search_items' => __( 'Search Tickets', 'supportflow' ),
'not_found' => __( 'No tickets found', 'supportflow' ),
'not_found_in_trash' => __( 'No tickets found in trash', 'supportflow' ),
),
'public' => false,
'show_ui' => true,
'supports' => false,
'menu_icon' => 'dashicons-sos',
)
);
}
/**
* Print requirements admin notices if any.
*/
public function action_requirements_admin_notices() {
if ( empty( $this->requirements ) ) {
return;
}
$message = '';
$strings = array(
'imap' => __( 'The PHP IMAP extension is required to collect incoming e-mail messages.', 'supportflow' ),
);
foreach ( $this->requirements as $key ) {
if ( empty( $strings[ $key ] ) ) {
continue;
}
$message .= sprintf( '<br />%s', esc_html( $strings[ $key ] ) );
}
if ( ! empty( $message ) ) {
$message = sprintf( '<p>%s%s</p>',
esc_html__( 'Your WordPress install did not meet the following requirements for SupportFlow:', 'supportflow' ),
$message );
} else {
$message = '<p>' . esc_html__( 'An unknown SupportFlow error has occurred.', 'supportflow' ) . '</p>';
}
printf( '<div class="error">%s</div>', wp_kses( $message, 'post' ) );
}
/**
* Register our custom taxonomies
*/
public function action_init_register_taxonomies() {
$capabilities = array(
'manage_terms' => 'manage_options',
'edit_terms' => 'manage_options',
'delete_terms' => 'manage_options',
'assign_terms' => 'manage_options',
);
$args_customers_tax = array(
'label' => __( 'Customers', 'supportflow' ),
'labels' => array(
'search_items' => __( 'Search Customers', 'supportflow' ),
'edit_item' => __( 'Edit Customer', 'supportflow' ),
'update_item' => __( 'Update Customer', 'supportflow' ),
'add_new_item' => __( 'Add New Customer', 'supportflow' ),
),
'public' => false,
'rewrite' => false,
'capabilities' => $capabilities,
);
$args_tags_tax = array(
'label' => __( 'Tags', 'supportflow' ),
'labels' => array(
'search_items' => __( 'Search Tags', 'supportflow' ),
'edit_item' => __( 'Edit Tag', 'supportflow' ),
'update_item' => __( 'Update Tag', 'supportflow' ),
'add_new_item' => __( 'Add New Tag', 'supportflow' ),
),
'public' => true,
'show_in_nav_menus' => true,
'rewrite' => false,
'capabilities' => $capabilities,
'update_count_callback' => array($this, 'callback_update_count_callback_tags' ),
);
register_taxonomy( $this->customers_tax, $this->post_type, $args_customers_tax );
register_taxonomy( $this->tags_tax, $this->post_type, $args_tags_tax );
}
/**
* Register the custom post (ticket) statuses
*
* @since SupportFlow 0.1
* @uses register_post_status() To register the post statuses
* @uses apply_filters() To control what statuses are registered
*/
public function action_init_register_post_statuses() {
foreach ( $this->post_statuses as $post_status => $args ) {
$args['public'] = true;
register_post_status( $post_status, $args );
}
}
/**
* Upgrade database records on update of plugin
*
* @since 0.3
*/
public function action_init_upgrade() {
global $wpdb;
$db_prefix = $wpdb->prefix;
$code_version = $this->version;
$db_version = get_option( 'sf_version' );
if ( 0 == version_compare( $code_version, $db_version ) ) {
return;
}
// New installation or 0.1 or 0.2
if ( ! $db_version ) {
// Earlier reply ID were stored as post_parent of attachment. Now attachment ID's are stored as post meta of reply
$attachments = get_posts( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_parent__not_in' => array( '0' ),
'posts_per_page' => - 1,
) );
foreach ( $attachments as $attachment ) {
add_post_meta( $attachment->post_parent, 'sf_attachments', $attachment->ID );
wp_update_post( array( 'ID' => $attachment->ID, 'post_parent' => 0 ) );
}
// Migrate all posts with post type sf_thread to sf_ticket
$wpdb->update( "{$db_prefix}posts", array( 'post_type' => 'sf_ticket' ), array( 'post_type' => 'sf_thread' ) );
// Migrate all terms with taxonomy sf_respondent to sf_customer
$wpdb->update( "{$db_prefix}term_taxonomy", array( 'taxonomy' => 'sf_customer' ), array( 'taxonomy' => 'sf_respondent' ) );
}
// Update db_version to latest one
update_option( 'sf_version', $this->version );
}
/**
* Updated count of tickets using a tag
* WP include only post with `published` status when evaluating count. So it is causing problems as SF use different post_stasuses
*/
public function callback_update_count_callback_tags( $terms, $taxonomy ) {
global $wpdb;
$post_type = $this->post_type;
$statuses = $this->post_statuses;
foreach ( $statuses as $status => $status_data ) {
if ( true == $status_data['show_tickets'] ) {
$status_slugs[] = $status;
}
}
$status_slugs = implode( "', '", $status_slugs );
foreach ( $terms as $term ) {
$count = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE "
. "$wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_type = '$post_type' AND post_status IN ('$status_slugs') AND term_taxonomy_id = %d",
$term
) );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
}
}
public function filter_wp_insert_post_data( $data, $postarr ) {
if ( 0 == $postarr['post_author'] ) {
$data['post_author'] = 0;
}
return $data;
}
/** Helper Functions ******************************************************/
/**
* Validates a user ID
*/
public function validate_user( $user ) {
// User ID
if ( is_numeric( $user ) ) {
$user_object = get_user_by( 'ID', $user );
} // User e-mail address
elseif ( is_email( $user ) ) {
$user_object = get_user_by( 'email', $user );
} // User login
else {
$user_object = get_user_by( 'login', $user );
}
if ( ! $user_object ) {
return false;
}
return $user_object->data->ID;
}
/**
* Turns an e-mail address into a term hash, or false on failure
*/
public function get_email_hash( $email ) {
$email = strtolower( trim( $email ) );
if ( ! is_email( $email ) ) {
return false;
}
$email = $this->email_term_prefix . md5( $email );
return $email;
}
/** Ticket Functions ******************************************************/
/**
* Check whether a post_id is a ticket
*/
public function is_ticket( $post ) {
return (bool) ( $this->post_type == get_post_type( $post ) );
}
/**
* Create a new ticket
*/
public function create_ticket( $args ) {
// The __get() magic doesn't allow key() usage so gotta copy it
$post_statuses = $this->post_statuses;
$defaults = array(
'subject' => '',
'message' => '',
'date' => '',
'customer_id' => 0, // If the requester has a WordPress account (ID or username)
'customer_email' => array(), // And an e-mail address
'reply_author' => '',
'reply_author_email' => '',
'status' => key( $post_statuses ),
'assignee' => - 1, // WordPress user ID or username of ticket assignee/owner
'email_account' => '',
);
$args = wp_parse_args( $args, $defaults );
$ticket = array(
'post_type' => $this->post_type,
'post_title' => $args['subject'],
'post_author' => $args['assignee'],
'post_date' => $args['date'],
);
// Validate the ticket status
if ( ! get_post_status_object( $args['status'] ) ) {
$args['status'] = $defaults['status'];
}
$ticket['post_status'] = $args['status'];
$ticket_id = wp_insert_post( $ticket );
if ( is_wp_error( $ticket_id ) ) {
return $ticket_id;
}
// Assign the customer(s)
if ( ! empty( $args['customer_email'] ) ) {
$this->update_ticket_customers( $ticket_id, $args['customer_email'] );
}
if ( is_numeric( $args['email_account'] ) ) {
update_post_meta( $ticket_id, 'email_account', $args['email_account'] );
}
// If there was a message, add it to the ticket
if ( ! empty( $args['message'] ) && ! empty( $args['customer_email'] ) ) {
$reply_details = array(
'reply_author' => $args['reply_author'],
'reply_author_email' => $args['reply_author_email'],
'user_id' => $args['customer_id'],
);
$this->add_ticket_reply( $ticket_id, $args['message'], $reply_details );
}
return $ticket_id;
}
/**
* @todo This should produce an object with ticket details, customers, and replies
*/
public function get_ticket( $ticket_id ) {
return get_post( $ticket_id );
}
/**
* @todo This should produce a series of ticket objects with customers, replies, etc.
*/
public function get_tickets( $args = array() ) {
$defaults = array(
'customer_email' => '',
'post_status' => '',
'orderby' => 'modified',
);
$args = array_merge( $defaults, $args );
$ticket_args = array();
if ( empty( $args['post_status'] ) ) {
$ticket_args['post_status'] = array_keys( $this->post_statuses );
}
if ( ! empty( $args['customer_email'] ) ) {
$ticket_args['tax_query'] = array(
array(
'taxonomy' => $this->customers_tax,
'field' => 'slug',
'terms' => $this->get_email_hash( $args['customer_email'] ),
),
);
}
$ticket_args['post_type'] = $this->post_type;
$ticket_args['orderby'] = $args['orderby'];
$tickets = new WP_Query( $ticket_args );
if ( is_wp_error( $tickets ) ) {
return $tickets;
}
return $tickets->posts;
}
/**
* Get customers matching $query
*
* @param string $query partial email address to search for
*/
public function get_customers( $args = array() ) {
$defaults = array(
'search' => '',
'number' => 10
);
$args = array_merge( $defaults, $args );
$term_args = array(
'orderby' => 'name',
'hide_empty' => 0,
'fields' => 'all',
'name__like' => $args['search'],
'number' => $args['number'],
);
$matches = get_terms( $this->customers_tax, $term_args );
if ( ! $matches ) {
return array();
}
return $matches;
}
/**
* Get a ticket's customers
*
* @todo support retrieving more fields
*/
public function get_ticket_customers( $ticket_id, $args = array() ) {
$default_args = array(
'fields' => 'all', // 'all', 'emails'
);
$args = array_merge( $default_args, $args );
$raw_customers = wp_get_object_terms( $ticket_id, $this->customers_tax );
if ( is_wp_error( $raw_customers ) ) {
return array();
}
$customers = array();
if ( 'emails' == $args['fields'] ) {
foreach ( $raw_customers as $raw_customer ) {
$customers[] = $raw_customer->name;
}
} elseif ( 'slugs' == $args['fields'] ) {
foreach ( $raw_customers as $raw_customer ) {
$customers[] = $raw_customer->slug;
}
}
return $customers;
}
/**
* Update a ticket's customers
*
* @param int $ticket_id
* @param array $customers An array of email addresses
* @param bool $append Whether or not to append these email addresses to any existing addresses
*/
public function update_ticket_customers( $ticket_id, $customers, $append = false ) {
if ( is_string( $customers ) ) {
$customers = array( $customers );
}
$term_ids = array();
foreach ( $customers as $dirty_customer ) {
if ( empty( $dirty_customer ) ) {
continue;
}
// Create a term if it doesn't yet exist
$email = ( is_array( $dirty_customer ) ) ? $dirty_customer['user_email'] : $dirty_customer;
if ( $term = get_term_by( 'name', $email, $this->customers_tax ) ) {
$term_ids[] = (int) $term->term_id;
} else {
$term = wp_insert_term( $email, $this->customers_tax, array( 'slug' => $this->get_email_hash( $email ) ) );
if ( ! is_wp_error( $term ) ) {
$term_ids[] = (int) $term['term_id'];
}
}
}
wp_set_object_terms( $ticket_id, $term_ids, $this->customers_tax, $append );
}
/**
* Get all of the replies associated with a ticket
*/
public function get_ticket_replies( $ticket_id, $args = array() ) {
$args['post_id'] = $ticket_id;
$ticket_replies = SupportFlow()->get_replies( $args );
return $ticket_replies;
}
/**
* Get all replies based on various arguments
*/
public function get_replies( $args ) {
$default_args = array(
'status' => 'public', // 'public', 'private', 'all'
'post_id' => '',
'search' => '',
'order' => 'DESC', // 'DESC', 'ASC',
'posts_per_page' => - 1,
);
$args = array_merge( $default_args, $args );
$post_args = array(
'search' => $args['search'],
'post_parent' => $args['post_id'],
'post_status' => $args['status'],
'post_type' => $this->reply_type,
'order' => $args['order'],
'posts_per_page' => $args['posts_per_page'],
'suppress_filters' => false,
);
add_filter( 'posts_clauses', array( $this, 'filter_reply_clauses' ), 10, 2 );
$ticket_replies = get_posts( $post_args );
remove_filter( 'posts_clauses', array( $this, 'filter_reply_clauses' ) );
return $ticket_replies;
}
/**
* Convert 'any' reply approved requests to the proper SQL
*/
public function filter_reply_clauses( $clauses, $query ) {
if ( in_array( $query->query_vars['post_status'], array( 'public', 'private' ) ) ) {
$new_post_status = "post_status = '{$query->query_vars['post_status']}' ";
} else {
$new_post_status = "post_status IN ( 'private', 'public' )";
}
if ( preg_match( "~post_status = '[^']+'~", $clauses['where'] ) ) {
$clauses['where'] = preg_replace( "~post_status = '[^']+'~", $new_post_status, $clauses['where'] );
} else {
$clauses['where'] .= " AND ($new_post_status)";
}
return $clauses;
}
/**
* Get the total number of replies associated with a ticket
*
* @todo support filtering to specific types or replier
*/
public function get_ticket_replies_count( $ticket_id, $args = array() ) {
$default_args = array(
'posts_per_page' => 1,
'post_type' => $this->post_type,
'post_status' => 'public',
'post_parent' => $ticket_id,
);
$args = array_merge( $default_args, $args );
$query = new WP_Query( $args );
$count = $query->found_posts;
return (int) $count;
}
/**
* Add a reply to a given ticket
*/
public function add_ticket_reply( $ticket_id, $reply_text, $details = array() ) {
$default_details = array(
'time' => current_time( 'mysql' ),
'reply_author' => '',
'reply_author_email' => '',
'user_id' => '',
'post_status' => 'public',
'cc' => array(),
'bcc' => array(),
);
// @todo This actually probably shouldn't default to current user, so
// we don't have to mandate the arguments be assigned each time
if ( $user = wp_get_current_user() ) {
$default_details['reply_author'] = $user->display_name;
$default_details['reply_author_email'] = $user->user_email;
$default_details['user_id'] = $user->ID;
}
$details = array_merge( $default_details, $details );
// If there are attachments, store them for later
if ( isset( $details['attachment_ids'] ) ) {
$attachment_ids = $details['attachment_ids'];
unset( $details['attachment_ids'] );
} else {
$attachment_ids = false;
}
$reply = array(
'post_content' => $reply_text,
'post_parent' => (int) $ticket_id,
'post_date' => $details['time'],
'post_status' => $details['post_status'],
'post_type' => $this->reply_type,
'post_title' => 'supportflow reply',
'user_id' => (int) $details['user_id'],
);
$reply = apply_filters( 'supportflow_pre_insert_ticket_reply', $reply );
// Remove the save_post admin action callback if it exists.
if ( ! empty( SupportFlow()->extend->admin ) && is_callable( array( SupportFlow()->extend->admin, 'action_save_post' ) ) ) {
remove_action( 'save_post', array( SupportFlow()->extend->admin, 'action_save_post' ) );
$reply_id = wp_insert_post( $reply );
add_action( 'save_post', array( SupportFlow()->extend->admin, 'action_save_post' ) );
} else {
$reply_id = wp_insert_post( $reply );
}
// If there are attachment IDs store them as meta
if ( is_array( $attachment_ids ) ) {
foreach ( $attachment_ids as $attachment_id ) {
// Associate attachment with the reply
add_post_meta( $reply_id, 'sf_attachments', $attachment_id );
SupportFlow()->extend->attachments->insert_attachment_secret_key( $attachment_id );
// It doesn't do anything special other than making sure file is not shown as unattached in media page
wp_update_post( array( 'ID' => $attachment_id, 'post_parent' => $ticket_id ) );
}
}
add_post_meta( $reply_id, 'reply_author', esc_sql( $details['reply_author'] ) );
add_post_meta( $reply_id, 'reply_author_email', esc_sql( $details['reply_author_email'] ) );
// Empty autosaved reply
delete_post_meta( $ticket_id, '_sf_autosave_reply' );
$update_args = array(
'ID' => $ticket_id,
'post_modified' => current_time( 'mysql' ),
);
// Allow plugins to override this behavior.
if ( get_post_status( $ticket_id ) == 'sf_closed' && add_filter( 'supportflow_reopen_ticket_on_reply', true, $reply_id, $ticket_id ) ) {
$update_args['post_status'] = 'sf_open';
}
// Adding a ticket reply updates the post modified time for the ticket
if ( ! empty( SupportFlow()->extend->admin ) && is_callable( array( SupportFlow()->extend->admin, 'action_save_post' ) ) ) {
remove_action( 'save_post', array( SupportFlow()->extend->admin, 'action_save_post' ) );
wp_update_post( $update_args );
add_action( 'save_post', array( SupportFlow()->extend->admin, 'action_save_post' ) );
} else {
wp_update_post( $update_args );
}
clean_post_cache( $ticket_id );
do_action( 'supportflow_ticket_reply_added', $reply_id, $details['cc'], $details['bcc'] );
return $reply_id;
}
/**
* Generate the secure key for replying to this ticket
*
* @todo Rather than storing this in the database, it should be generated on the fly
* with an encryption algorithim
*/
public function get_secret_for_ticket( $ticket_id ) {
if ( $secret = get_post_meta( $ticket_id, $this->ticket_secret_key, true ) ) {
return $secret;
}
$secret = wp_generate_password( 8, false );
update_post_meta( $ticket_id, $this->ticket_secret_key, $secret );
return $secret;
}
/**
* Get the ticket ID from a secret
*/
public function get_ticket_from_secret( $secret ) {
$post_statuses = $this->post_statuses;
$posts = get_posts( array(
'post_type' => SupportFlow()->post_type,
'post_status' => array_keys( $post_statuses ),
'meta_query' => array(
array(
'key' => $this->ticket_secret_key,
'value' => $secret,
),
)
) );
if ( isset( $posts[0] ) ) {
return $posts[0]->ID;
} else {
return 0;
}
}
/**
* SupportFlow alternate of wp_enqueue_script() to increase code reuse and save time. Enqueue a JS script
*
* @param string $handle Name of the script.
* @param string $file_name Path to the script "/plugins/supportflow/js/ is automatically prefixed to it.
* Example: 'attachment.js'.
* @param array $deps Optional. An array of registered handles this script depends on. Default array( 'jquery' )
*
* @return string Handle of enqueued script
*/
public function enqueue_script( $handle, $file_name, $deps = array( 'jquery' ) ) {
if ( ! $this->script_dev ) {
$handle = 'supportflow-minified-scripts';
$file_name = 'supportflow.min.js';
}
foreach ( $deps as $dep ) {
wp_enqueue_script( $dep );
}
$version = $this->version;
$src = SupportFlow()->plugin_url . "js/$file_name";
wp_enqueue_script( $handle, $src, array(), $version );
return $handle;
}
/**
* SupportFlow alternate of wp_enqueue_script() to increase code reuse and save time. Enqueue a CSS stylesheet
*
* @param string $handle Handle of the stylesheet. It can be changed within method. You must use use the handle returned by it