forked from Automattic/camptix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camptix.php
6975 lines (5885 loc) · 253 KB
/
camptix.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
/*
* Plugin Name: CampTix Event Ticketing
* Plugin URI: http://wordcamp.org
* Description: Simple and flexible event ticketing for WordPress.
* Version: 1.4.1
* Author: Automattic
* Author URI: http://wordcamp.org
* License: GPLv2
*/
class CampTix_Plugin {
protected $options;
protected $notices;
protected $errors;
protected $infos;
protected $admin_notices;
protected $tmp;
public $debug;
public $beta_features_enabled;
public $version = 20140325;
public $css_version = 20140827;
public $js_version = 20140902;
public $caps;
public $addons = array();
public $addons_loaded = array();
protected $tickets;
protected $tickets_selected;
protected $tickets_selected_count;
protected $form_data;
protected $reservation;
protected $coupon;
protected $error_flags;
protected $error_data;
protected $did_template_redirect;
protected $did_checkout;
protected $shortcode_contents;
// Allow others to use this.
public $filter_post_meta = false;
const PAYMENT_STATUS_CANCELLED = 1;
const PAYMENT_STATUS_COMPLETED = 2;
const PAYMENT_STATUS_PENDING = 3;
const PAYMENT_STATUS_FAILED = 4;
const PAYMENT_STATUS_TIMEOUT = 5;
const PAYMENT_STATUS_REFUNDED = 6;
const PAYMENT_STATUS_REFUND_FAILED = 7;
/**
* Fired as soon as this file is loaded, don't do anything
* but filters and actions here.
*/
function __construct() {
do_action( 'camptix_pre_init' );
require( dirname( __FILE__ ) . '/inc/class-camptix-addon.php' );
require( dirname( __FILE__ ) . '/inc/class-camptix-payment-method.php' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( dirname( __FILE__ ) . '/inc/class-wp-cli-commands.php' );
}
// Addons
add_action( 'init', array( $this, 'load_addons' ), 8 );
add_action( 'camptix_load_addons', array( $this, 'load_default_addons' ) );
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'schedule_events' ), 9 );
add_action( 'shutdown', array( $this, 'shutdown' ) );
// Load a text domain
load_plugin_textdomain( 'camptix', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Fired during init, doh!
*/
function init() {
$this->options = $this->get_options();
$this->debug = (bool) apply_filters( 'camptix_debug', false );
$this->beta_features_enabled = (bool) apply_filters( 'camptix_beta_features_enabled', false );
$this->tmp = array();
// Capability mapping.
$this->caps = apply_filters( 'camptix_capabilities', array(
'manage_tickets' => 'manage_options',
'manage_attendees' => 'manage_options',
'manage_coupons' => 'manage_options',
'manage_tools' => 'manage_options',
'manage_options' => 'manage_options',
'delete_attendees' => 'manage_options',
'refund_all' => 'manage_options',
) );
// Explicitly disable all beta features if beta features is off.
if ( ! $this->beta_features_enabled )
foreach ( $this->get_beta_features() as $beta_feature )
$this->options[$beta_feature] = false;
// The following three are just different kinds (colors) of user feedback.
// Don't use directly, instead use $this->notice / error / info methods.
$this->infos = array();
$this->notices = array();
$this->errors = array();
// Our main shortcode
add_shortcode( 'camptix', array( $this, 'shortcode_callback' ) );
// Hack to avoid object caching, see revenue report.
add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
// Stuff that might need to redirect, thus not in [camptix] shortcode.
add_action( 'template_redirect', array( $this, 'template_redirect' ), 9 ); // earlier than the others.
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_head', array( $this, 'admin_menu_fix' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'admin_head', array( $this, 'admin_head' ) );
// Handle meta for our post types.
add_action( 'save_post', array( $this, 'save_ticket_post' ) );
add_action( 'save_post', array( $this, 'save_attendee_post' ) );
add_action( 'save_post', array( $this, 'save_coupon_post' ) );
// Used to update stats
add_action( 'transition_post_status', array( $this, 'transition_post_status' ), 10, 3 );
add_action( 'wp_ajax_camptix_client_stats', array( $this, 'process_client_stats' ) );
add_action( 'wp_ajax_nopriv_camptix_client_stats', array( $this, 'process_client_stats' ) );
// Notices, errors and infos, all in one.
add_action( 'camptix_notices', array( $this, 'do_notices' ) );
add_action( 'admin_notices', array( $this, 'do_admin_notices' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
// Sort of admin_init but on the Tickets > Tools page only.
add_action( 'load-tix_ticket_page_camptix_tools', array( $this, 'summarize_extra_fields' ) );
add_action( 'load-tix_ticket_page_camptix_tools', array( $this, 'summarize_admin_init' ) ); // marked as admin init but not really
add_action( 'load-tix_ticket_page_camptix_tools', array( $this, 'export_admin_init' ) ); // same here, but close
add_action( 'load-tix_ticket_page_camptix_tools', array( $this, 'menu_tools_refund_admin_init' ) );
add_action( 'camptix_question_fields_init', array( $this, 'question_fields_init' ) );
add_action( 'camptix_init_notify_shortcodes', array( $this, 'init_notify_shortcodes' ), 9 );
add_action( 'camptix_init_email_templates_shortcodes', array( $this, 'init_email_templates_shortcodes' ), 9 );
// Other things required during init.
$this->custom_columns();
$this->register_post_types();
$this->register_post_statuses();
do_action( 'camptix_init' );
}
/**
* Scheduled events, mainly around e-mail jobs, runs during file load.
*/
function schedule_events() {
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
add_action( 'tix_scheduled_every_ten_minutes', array( $this, 'send_emails_batch' ) );
add_action( 'tix_scheduled_every_ten_minutes', array( $this, 'process_refund_all' ) );
add_action( 'tix_scheduled_daily', array( $this, 'review_timeout_payments' ) );
if ( ! wp_next_scheduled( 'tix_scheduled_every_ten_minutes' ) )
wp_schedule_event( time(), '10-mins', 'tix_scheduled_every_ten_minutes' );
// wp_clear_scheduled_hook( 'tix_scheduled_hourly' );
if ( ! wp_next_scheduled( 'tix_scheduled_daily' ) )
wp_schedule_event( time(), 'daily', 'tix_scheduled_daily' );
}
/**
* Filters cron_schedules
*/
function cron_schedules( $schedules ) {
$schedules['10-mins'] = array(
'interval' => 60 * 10,
'display' => __( 'Once every 10 minutes', 'camptix' ),
);
return $schedules;
}
/**
* Runs during the tix_email_schedule scheduled event, processes e-mail jobs.
*/
function send_emails_batch() {
global $wpdb, $shortcode_tags;
// Sometimes Cron can run before $this->init()
if ( ! did_action( 'camptix_init' ) )
$this->init();
// Grab only one e-mail job at a time.
$email = get_posts( array(
'post_type' => 'tix_email',
'post_status' => 'pending',
'order' => 'ASC',
'posts_per_page' => 1,
'cache_results' => false,
) );
if ( ! $email )
return;
$email = array_shift( $email );
$this->log( 'Executing e-mail job.', $email->ID, null, 'notify' );
$max = apply_filters( 'camptix_notify_recipients_batch_count', 200 ); // plugins can change this.
$recipients_data = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS meta_id, meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = %d AND $wpdb->postmeta.meta_key = %s LIMIT %d;", $email->ID, 'tix_email_recipient_id', $max ) );
$total = $wpdb->get_var( "SELECT FOUND_ROWS();" );
$processed = 0;
$recipients = array();
foreach ( $recipients_data as $recipient )
$recipients[$recipient->meta_value] = $recipient->meta_id;
unset( $recipients_data, $recipient );
if ( $recipients && is_array( $recipients ) && count( $recipients ) > 0 ) {
// Remove all shortcodes before sending the e-mails, but bring them back later.
$this->removed_shortcodes = $shortcode_tags;
remove_all_shortcodes();
do_action( 'camptix_init_notify_shortcodes' );
$paged = 1;
while ( $attendees = get_posts( array(
'post_type' => 'tix_attendee',
'post_status' => 'any',
'post__in' => array_keys( $recipients ),
'fields' => 'ids', // ! no post objects
'orderby' => 'ID',
'order' => 'ASC',
'paged' => $paged++,
'posts_per_page' => min( 100, $max ),
'cache_results' => false, // no caching
) ) ) {
// Prepare post metadata, disable object cache.
$this->filter_post_meta = $this->prepare_metadata_for( $attendees );
foreach ( $attendees as $attendee_id ) {
$attendee_email = get_post_meta( $attendee_id, 'tix_email', true );
$count = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_id = %d LIMIT 1;", $email->ID, $recipients[$attendee_id] ) );
if ( $count > 0 ) {
$data = array(
'email_id' => $email->ID,
'email_title' => $email->post_title,
'attendee_id' => $attendee_id,
'attendee_email' => $attendee_email,
);
if ( ! is_email( $attendee_email ) ) {
$this->log( sprintf( '%s is not a valid e-mail, removing from queue.', $attendee_email ), $email->ID, $data, 'notify' );
} else {
$this->tmp( 'attendee_id', $attendee_id );
$email_content = do_shortcode( $email->post_content );
$email_title = do_shortcode( $email->post_title );
// Decode entities since the e-mails sent is a plain/text, not html.
$email_title = html_entity_decode( $email_title );
$email_content = html_entity_decode( $email_content );
// Attempt to send an e-mail.
if ( $this->wp_mail( $attendee_email, $email_title, $email_content ) ) {
$this->log( sprintf( 'E-mail successfully sent to %s', $attendee_email ), $email->ID, $data, 'notify' );
} else {
$this->log( sprintf( 'Could not send e-mail to %s, removing from queue.', $attendee_email ), $email->ID, $data, 'notify' );
}
}
$processed++;
}
}
// Clean post meta cache.
$this->filter_post_meta = false;
$this->tmp( 'attendee_id', false );
}
// Bring back the original shortcodes.
$shortcode_tags = $this->removed_shortcodes;
$this->removed_shortcodes = array();
}
//update_post_meta( $email->ID, 'tix_email_recipients', $recipients );
$this->log( sprintf( 'Processed %d recipients. %d recipients remaining.', $processed, $total - $processed ), $email->ID, null, 'notify' );
// Let's see if there's anything left.
if ( $total - $processed < 1 ) {
// Published tix_email posts means completed jobs.
wp_update_post( array(
'ID' => $email->ID,
'post_status' => 'publish',
) );
$this->log( 'Email job complete and published.', $email->ID, null, 'notify' );
}
}
function init_email_templates_shortcodes() {
// Use the same ones as the notify shortcode
add_shortcode( 'first_name', array( $this, 'notify_shortcode_first_name' ) );
add_shortcode( 'last_name', array( $this, 'notify_shortcode_last_name' ) );
add_shortcode( 'email', array( $this, 'notify_shortcode_email' ) );
add_shortcode( 'event_name', array( $this, 'email_template_shortcode_event_name' ) );
add_shortcode( 'ticket_url', array( $this, 'email_template_shortcode_ticket_url' ) );
add_shortcode( 'receipt', array( $this, 'email_template_shortcode_receipt' ) );
add_shortcode( 'buyer_full_name', array( $this, 'email_template_shortcode_buyer_full_name' ) );
}
/**
* Returns the event name.
*/
function email_template_shortcode_event_name( $atts ) {
return $this->options['event_name'];
}
/**
* Returns the ticket access/edit URL.
*
* @uses $this->tmp() to retrieve the ticket url
*/
function email_template_shortcode_ticket_url( $atts ) {
return $this->tmp( 'ticket_url' );
}
/**
* Returns the e-mail receipt content.
*
* @uses $this->tmp() to retrieve receipt content.
*/
function email_template_shortcode_receipt( $atts ) {
return $this->tmp( 'receipt' );
}
function email_template_shortcode_buyer_full_name( $atts ) {
return $this->tmp( 'buyer_full_name' );
}
/**
* Creates some shortcodes
* to be used with CampTix Notify.
*/
function init_notify_shortcodes() {
add_shortcode( 'first_name', array( $this, 'notify_shortcode_first_name' ) );
add_shortcode( 'last_name', array( $this, 'notify_shortcode_last_name' ) );
add_shortcode( 'email', array( $this, 'notify_shortcode_email' ) );
add_shortcode( 'ticket_url', array( $this, 'notify_shortcode_ticket_url' ) );
}
/**
* Notify shortcode: returns the attendee first name.
*/
function notify_shortcode_first_name( $atts ) {
if ( $this->tmp( 'attendee_id' ) )
return get_post_meta( $this->tmp( 'attendee_id' ), 'tix_first_name', true );
}
/**
* Notify shortcode: returns the attendee last name.
*/
function notify_shortcode_last_name( $atts ) {
if ( $this->tmp( 'attendee_id' ) )
return get_post_meta( $this->tmp( 'attendee_id' ), 'tix_last_name', true );
}
/**
* Notify shortcode: returns the attendee e-mail address.
*/
function notify_shortcode_email( $atts ) {
if ( $this->tmp( 'attendee_id' ) )
return get_post_meta( $this->tmp( 'attendee_id' ), 'tix_email', true );
}
/**
* Notify shortcode: returns the attendee edit url
*/
function notify_shortcode_ticket_url( $atts ) {
if ( ! $this->tmp( 'attendee_id' ) )
return;
$edit_token = get_post_meta( $this->tmp( 'attendee_id' ), 'tix_edit_token', true );
return $this->get_edit_attendee_link( $this->tmp( 'attendee_id' ), $edit_token );
}
/**
* This is taken out here to illustrate how a third-party plugin or
* theme can hook into CampTix to add their own Summarize fields. This method
* grabs all the available tickets questions and adds them to Summarize.
*/
function summarize_extra_fields() {
if ( 'summarize' != $this->get_tools_section() )
return;
// Adds all questions to Summarize and register the callback that counts all the things.
add_filter( 'camptix_summary_fields', array( $this, 'camptix_summary_fields_extras' ) );
add_action( 'camptix_summarize_by_field', array( $this, 'camptix_summarize_by_field_extras' ), 10, 3 );
}
/**
* Filters camptix_summary_fields to add user-defined
* questions to the Summarize list.
*/
function camptix_summary_fields_extras( $fields ) {
$questions = $this->get_all_questions();
foreach ( $questions as $question )
$fields[ 'tix_q_' . $question->ID ] = apply_filters( 'the_title', $question->post_title );
return $fields;
}
/**
* Runs during camptix_summarize_by_field, fetches answers from
* attendee objects and increments summary.
*/
function camptix_summarize_by_field_extras( $summarize_by, &$summary, $attendee ) {
if ( 'tix_q_' != substr( $summarize_by, 0, 6 ) )
return;
$key = substr( $summarize_by, 6 );
$answers = (array) get_post_meta( $attendee->ID, 'tix_questions', true );
if ( isset( $answers[ $key ] ) && ! empty( $answers[ $key ] ) )
$this->increment_summary( $summary, $answers[ $key ] );
else
$this->increment_summary( $summary, __( 'None', 'camptix' ) );
}
/**
* Get a CSS file, @todo make it removable through an option.
*/
function enqueue_scripts() {
wp_register_style( 'camptix', plugins_url( 'camptix.css', __FILE__ ), array(), $this->css_version );
wp_register_script( 'camptix', plugins_url( 'camptix.js', __FILE__ ), array( 'jquery' ), $this->js_version );
wp_localize_script( 'camptix', 'camptix_l10n', array(
'enterEmail' => __( 'Please enter the e-mail addresses in the forms above.', 'camptix' ),
'ajaxURL' => admin_url( 'admin-ajax.php' ),
) );
// Let's play by the rules and print this in the <head> section.
wp_enqueue_style( 'camptix' );
}
function admin_enqueue_scripts() {
global $wp_query;
if ( ! $wp_query->query_vars ) { // only on singular admin pages
if ( 'tix_ticket' == get_post_type() || 'tix_coupon' == get_post_type() ) {
}
}
// Let's see whether to include admin.css and admin.js
if ( is_admin() ) {
$post_types = array( 'tix_ticket', 'tix_coupon', 'tix_email', 'tix_attendee' );
$pages = array( 'camptix_options', 'camptix_tools' );
if (
( in_array( get_post_type(), $post_types ) ) ||
( isset( $_REQUEST['post_type'] ) && in_array( $_REQUEST['post_type'], $post_types ) ) ||
( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $pages ) )
) {
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_style( 'jquery-ui', plugins_url( '/external/jquery-ui.css', __FILE__ ), array(), $this->version );
wp_enqueue_style( 'camptix-admin', plugins_url( '/admin.css', __FILE__ ), array(), $this->css_version );
wp_enqueue_script( 'camptix-admin', plugins_url( '/admin.js', __FILE__ ), array( 'jquery', 'jquery-ui-datepicker', 'backbone' ), $this->js_version );
wp_dequeue_script( 'autosave' );
}
}
$screen = get_current_screen();
if ( 'tix_ticket_page_camptix_options' == $screen->id ) {
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_style( 'jquery-ui', plugins_url( '/external/jquery-ui.css', __FILE__ ), array(), $this->version );
}
wp_enqueue_style( 'campicons', plugins_url( 'fonts/campicons.css', __FILE__ ), array(), $this->version );
}
/**
* Filters column fields for our new post types, adds extra columns
* and registers callback actions to render column callback.
*/
function custom_columns() {
// Ticket columns
add_filter( 'manage_edit-tix_ticket_columns', array( $this, 'manage_columns_ticket_filter' ) );
add_action( 'manage_tix_ticket_posts_custom_column', array( $this, 'manage_columns_ticket_action' ), 10, 2 );
// Attendee columns
add_filter( 'manage_edit-tix_attendee_columns', array( $this, 'manage_columns_attendee_filter' ) );
add_action( 'manage_tix_attendee_posts_custom_column', array( $this, 'manage_columns_attendee_action' ), 10, 2 );
// Coupon columns
add_filter( 'manage_edit-tix_coupon_columns', array( $this, 'manage_columns_coupon_filter' ) );
add_action( 'manage_tix_coupon_posts_custom_column', array( $this, 'manage_columns_coupon_action' ), 10, 2 );
// E-mail columns
add_filter( 'manage_edit-tix_email_columns', array( $this, 'manage_columns_email_filter' ) );
add_action( 'manage_tix_email_posts_custom_column', array( $this, 'manage_columns_email_action' ), 10, 2 );
// Maybe hide some columns.
add_action( 'load-edit.php', array( $this, 'update_hidden_columns' ) );
}
/**
* Manage columns filter for ticket post type.
*/
function manage_columns_ticket_filter( $columns ) {
$columns['tix_price'] = __( 'Price', 'camptix' );
$columns['tix_quantity'] = __( 'Quantity', 'camptix' );
$columns['tix_purchase_count'] = __( 'Purchased', 'camptix' );
$columns['tix_remaining'] = __( 'Remaining', 'camptix' );
$columns['tix_availability'] = __( 'Availability', 'camptix' );
$date = $columns['date'];
unset( $columns['date'] );
$columns['date'] = $date;
return $columns;
}
/**
* Manage columns action for ticket post type.
*/
function manage_columns_ticket_action( $column, $post_id ) {
switch ( $column ) {
case 'tix_price':
echo $this->append_currency( get_post_meta( $post_id, 'tix_price', true ) );
break;
case 'tix_quantity':
echo intval( get_post_meta( $post_id, 'tix_quantity', true ) );
break;
case 'tix_purchase_count':
$attendees_url = get_admin_url( 0, '/edit.php?post_type=tix_attendee' );
$attendees_url = add_query_arg( 's', 'tix_ticket_id:' . intval( $post_id ), $attendees_url );
printf( '<a href="%s">%d</a>', esc_url( $attendees_url ), intval( $this->get_purchased_tickets_count( $post_id ) ) );
break;
case 'tix_remaining':
echo $this->get_remaining_tickets( $post_id );
if ( $this->options['reservations_enabled'] ) {
$reserved = 0;
$reservations = $this->get_reservations( $post_id );
foreach ( $reservations as $reservation_token => $reservation )
$reserved += $reservation['quantity'] - $this->get_purchased_tickets_count( $post_id, $reservation_token );
if ( $reserved > 0 )
printf( ' ' . __( '(%d reserved)', 'camptix' ), $reserved );
}
break;
case 'tix_availability':
$start = get_post_meta( $post_id, 'tix_start', true );
$end = get_post_meta( $post_id, 'tix_end', true );
if ( ! $start && ! $end ) {
echo __( 'Auto', 'camptix' );
} else {
// translators: 1: "from" date, 2: "to" date
printf( __( '%1$s — %2$s', 'camptix' ), $start, $end );
}
break;
}
}
/**
* Manage columns filter for attendee post type.
*/
function manage_columns_attendee_filter( $columns ) {
$columns['tix_email'] = __( 'E-mail', 'camptix' );
$columns['tix_ticket'] = __( 'Ticket', 'camptix' );
$columns['tix_coupon'] = __( 'Coupon', 'camptix' );
if ( $this->options['reservations_enabled'] )
$columns['tix_reservation'] = __( 'Reservation', 'camptix' );
$columns['tix_ticket_price'] = __( 'Ticket Price', 'camptix' );
$columns['tix_order_total'] = __( 'Order Total', 'camptix' );
$date = $columns['date'];
unset( $columns['date'] );
$columns['date'] = $date;
return $columns;
}
/**
* Manage columns action for attendee post type.
*/
function manage_columns_attendee_action( $column, $post_id ) {
switch ( $column ) {
case 'tix_ticket':
$ticket_id = intval( get_post_meta( $post_id, 'tix_ticket_id', true ) );
$ticket = get_post( $ticket_id );
if ( $ticket ) {
$attendees_url = get_admin_url( 0, '/edit.php?post_type=tix_attendee' );
$attendees_url = add_query_arg( 's', 'tix_ticket_id:' . intval( $ticket->ID ), $attendees_url );
printf( '<a href="%s">%s</a>', esc_url( $attendees_url ), esc_html( $ticket->post_title ) );
}
break;
case 'tix_email':
echo esc_html( get_post_meta( $post_id, 'tix_email', true ) );
break;
case 'tix_coupon':
$coupon_id = get_post_meta( $post_id, 'tix_coupon_id', true );
if ( $coupon_id ) {
$coupon = get_post_meta( $post_id, 'tix_coupon', true );
$attendees_url = get_admin_url( 0, '/edit.php?post_type=tix_attendee' );
$attendees_url = add_query_arg( 's', 'tix_coupon_id:' . intval( $coupon_id ), $attendees_url );
printf( '<a href="%s">%s</a>', esc_url( $attendees_url ), esc_html( $coupon ) );
}
break;
case 'tix_reservation':
$reservation_id = get_post_meta( $post_id, 'tix_reservation_id', true );
echo esc_html( $reservation_id );
break;
case 'tix_order_total':
$order_total = (float) get_post_meta( $post_id, 'tix_order_total', true );
echo $this->append_currency( $order_total );
break;
case 'tix_ticket_price':
$ticket_price = (float) get_post_meta( $post_id, 'tix_ticket_price', true );
echo $this->append_currency( $ticket_price );
break;
}
}
/**
* Manage columns filter for coupon post type.
*/
function manage_columns_coupon_filter( $columns ) {
$columns['tix_quantity'] = __( 'Quantity', 'camptix' );
$columns['tix_used'] = __( 'Used', 'camptix' );
$columns['tix_remaining'] = __( 'Remaining', 'camptix' );
$columns['tix_discount'] = __( 'Discount', 'camptix' );
$columns['tix_availability'] = __( 'Availability', 'camptix' );
$columns['tix_tickets'] = __( 'Tickets', 'camptix' );
$date = $columns['date'];
unset( $columns['date'] );
$columns['date'] = $date;
return $columns;
}
/**
* Manage columns action for coupon post type.
*/
function manage_columns_coupon_action( $column, $post_id ) {
switch ( $column ) {
case 'tix_quantity':
echo intval( get_post_meta( $post_id, 'tix_coupon_quantity', true ) );
break;
case 'tix_used':
$attendees_url = get_admin_url( 0, '/edit.php?post_type=tix_attendee' );
$attendees_url = add_query_arg( 's', 'tix_coupon_id:' . intval( $post_id ), $attendees_url );
printf( '<a href="%s">%d</a>', esc_url( $attendees_url ), $this->get_used_coupons_count( $post_id ) );
break;
case 'tix_remaining':
echo (int) $this->get_remaining_coupons( $post_id );
break;
case 'tix_discount':
$discount_price = (float) get_post_meta( $post_id, 'tix_discount_price', true );
$discount_percent = (int) get_post_meta( $post_id, 'tix_discount_percent', true );
if ( $discount_price > 0 ) {
echo $this->append_currency( $discount_price );
} elseif ( $discount_percent > 0 ) {
echo $discount_percent . '%';
}
break;
case 'tix_tickets':
$tickets = array();
$applies_to = get_post_meta( $post_id, 'tix_applies_to' );
foreach ( $applies_to as $ticket_id )
if ( $this->is_ticket_valid_for_display( $ticket_id ) )
edit_post_link( $this->get_ticket_title( $ticket_id ), '', '<br />', $ticket_id );
break;
case 'tix_availability':
$start = get_post_meta( $post_id, 'tix_coupon_start', true );
$end = get_post_meta( $post_id, 'tix_coupon_end', true );
if ( ! $start && ! $end ) {
echo __( 'Auto', 'camptix' );
} else {
// translators: 1: "from" date, 2: "to" date
printf( __( '%1$s — %2$s', 'camptix' ), $start, $end );
}
break;
}
}
/**
* Manage columns filter for email post type.
*/
function manage_columns_email_filter( $columns ) {
$columns['tix_sent'] = __( 'Sent', 'camptix' );
$columns['tix_remaining'] = __( 'Remaining', 'camptix' );
$columns['tix_total'] = __( 'Total', 'camptix' );
$date = $columns['date'];
unset( $columns['date'] );
$columns['date'] = $date;
return $columns;
}
/**
* Manage columns action for email post type.
*/
function manage_columns_email_action( $column, $post_id ) {
switch ( $column ) {
case 'tix_sent':
$recipients_backup = get_post_meta( $post_id, 'tix_email_recipients_backup', true );
$recipients_remaining = (array) get_post_meta( $post_id, 'tix_email_recipient_id' );
echo count( $recipients_backup ) - count( $recipients_remaining );
break;
case 'tix_remaining':
$recipients_remaining = (array) get_post_meta( $post_id, 'tix_email_recipient_id' );
echo count( $recipients_remaining );
break;
case 'tix_total':
$recipients_backup = get_post_meta( $post_id, 'tix_email_recipients_backup', true );
echo count( $recipients_backup );
break;
}
}
/**
* Hooked to load-edit.php, adds user options for hidden columns if absent.
*/
function update_hidden_columns() {
if ( ! empty( $_REQUEST['post_type' ] ) && ! in_array( $_REQUEST['post_type'], array( 'tix_attendee', 'tix_ticket' ) ) )
return;
// If first time editing, disable advanced items by default.
if ( false === $this->get_user_option( 'manageedit-tix_attendeecolumnshidden' ) ) {
$this->update_user_option( get_current_user_id(), 'manageedit-tix_attendeecolumnshidden', array(
'tix_order_total',
'tix_ticket_price',
'tix_reservation',
'tix_coupon',
), true );
}
if ( false === $this->get_user_option( 'manageedit-tix_ticketcolumnshidden' ) ) {
$this->update_user_option( get_current_user_id(), 'manageedit-tix_ticketcolumnshidden', array(
'tix_purchase_count',
'tix_reserved',
), true );
}
}
/**
* Filterable call to get_user_option
*/
function get_user_option( $option_name, $user_id = 0 ) {
if ( empty( $user_id ) )
$user_id = get_current_user_id();
$value = apply_filters( 'camptix_get_user_option', null, $option_name, $user_id );
if ( is_null( $value ) )
$value = get_user_option( $option_name, $user_id );
return $value;
}
/**
* Filterable call to update_user_option
*/
function update_user_option( $user_id, $option_name, $option_value, $global = false ) {
$value = apply_filters( 'camptix_update_user_option', null, $user_id, $option_name, $option_value, $global );
if ( is_null( $value ) )
$value = update_user_option( $user_id, $option_name, $option_value, $global );
return $value;
}
/**
* Get all questions. Returns an assoc array where the key is a
* sanitized questions (as stored in the database) and the value is
* the question array.
*/
function get_all_questions() {
$questions = get_posts( array(
'post_type' => 'tix_question',
'post_status' => 'publish',
'posts_per_page' => 100,
) );
return $questions;
}
/**
* Takes a ticket id and returns a sorted array of questions.
*
* @param int $ticket_id
*
* @return array
*/
function get_sorted_questions( $ticket_id ) {
$question_ids = (array) get_post_meta( $ticket_id, 'tix_question_id' );
$order = (array) get_post_meta( $ticket_id, 'tix_questions_order', true );
// Make sure we have at least some questions
if ( empty( $question_ids ) )
return array();
$questions = get_posts( array(
'post_type' => 'tix_question',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => $question_ids,
) );
$questions_with_keys = array();
foreach ( $questions as $question )
$questions_with_keys[ $question->ID ] = $question;
$questions = $questions_with_keys;
unset( $questions_with_keys );
$questions_sorted = array();
foreach ( $order as $question_id )
if ( isset( $questions[ $question_id ] ) )
$questions_sorted[] = $questions[ $question_id ];
unset( $questions );
return $questions_sorted;
}
/**
* Fired during init, registers our new post types. $supports depends
* on $this->debug, which if true, renders things like custom fields.
*/
function register_post_types() {
$supports = array( 'title', 'excerpt' );
if ( $this->debug && current_user_can( $this->caps['manage_options'] ) )
$supports[] = 'custom-fields';
register_post_type( 'tix_ticket', array(
'labels' => array(
'name' => __( 'Tickets', 'camptix' ),
'singular_name' => __( 'Ticket', 'camptix' ),
'add_new' => __( 'New Ticket', 'camptix' ),
'add_new_item' => __( 'Add New Ticket', 'camptix' ),
'edit_item' => __( 'Edit Ticket', 'camptix' ),
'new_item' => __( 'New Ticket', 'camptix' ),
'all_items' => __( 'Tickets', 'camptix' ),
'view_item' => __( 'View Ticket', 'camptix' ),
'search_items' => __( 'Search Tickets', 'camptix' ),
'not_found' => __( 'No tickets found', 'camptix' ),
'not_found_in_trash' => __( 'No tickets found in trash', 'camptix' ),
'menu_name' => __( 'Tickets', 'camptix' ),
),
'public' => false,
'query_var' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'supports' => $supports,
'capability_type' => 'tix_ticket',
'capabilities' => array(
'publish_posts' => $this->caps['manage_tickets'],
'edit_posts' => $this->caps['manage_tickets'],
'edit_others_posts' => $this->caps['manage_tickets'],
'delete_posts' => $this->caps['manage_tickets'],
'delete_others_posts' => $this->caps['manage_tickets'],
'read_private_posts' => $this->caps['manage_tickets'],
'edit_post' => $this->caps['manage_tickets'],
'delete_post' => $this->caps['manage_tickets'],
'read_post' => $this->caps['manage_tickets'],
),
) );
register_post_type( 'tix_question', array(
'labels' => array(
'name' => __( 'Questions', 'camptix' ),
'singular_name' => __( 'Question', 'camptix' ),
'add_new' => __( 'New Question', 'camptix' ),
'add_new_item' => __( 'Add New Question', 'camptix' ),
'edit_item' => __( 'Edit Question', 'camptix' ),
'new_item' => __( 'New Question', 'camptix' ),
'all_items' => __( 'Questions', 'camptix' ),
'view_item' => __( 'View Question', 'camptix' ),
'search_items' => __( 'Search Questions', 'camptix' ),
'not_found' => __( 'No questions found', 'camptix' ),
'not_found_in_trash' => __( 'No questions found in trash', 'camptix' ),
'menu_name' => __( 'Questions', 'camptix' ),
),
'public' => false,
'query_var' => false,
'publicly_queryable' => false,
'show_ui' => ( $this->debug && current_user_can( $this->caps['manage_options'] ) ),
'show_in_menu' => ( $this->debug && current_user_can( $this->caps['manage_options'] ) ) ? 'edit.php?post_type=tix_ticket' : false,
'supports' => array( 'title', 'custom-fields' ),
) );
$supports = array( 'title' );
if ( $this->debug && current_user_can( $this->caps['manage_options'] ) ) {
$supports[] = 'custom-fields';
$supports[] = 'editor';
}
register_post_type( 'tix_attendee', array(
'labels' => array(
'name' => __( 'Attendees', 'camptix' ),
'singular_name' => __( 'Attendee', 'camptix' ),
'add_new' => __( 'New Attendee', 'camptix' ),
'add_new_item' => __( 'Add New Attendee', 'camptix' ),
'edit_item' => __( 'Edit Attendee', 'camptix' ),
'new_item' => __( 'Add Attendee', 'camptix' ),
'all_items' => __( 'Attendees', 'camptix' ),
'view_item' => __( 'View Attendee', 'camptix' ),
'search_items' => __( 'Search Attendees', 'camptix' ),
'not_found' => __( 'No attendees found', 'camptix' ),
'not_found_in_trash' => __( 'No attendees found in trash', 'camptix' ),
'menu_name' => __( 'Attendees', 'camptix' ),
),
'public' => false,
'query_var' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=tix_ticket',
'supports' => $supports,
'capability_type' => 'tix_attendee',
'capabilities' => array(
'publish_posts' => $this->caps['manage_attendees'],
'edit_posts' => $this->caps['manage_attendees'],
'edit_others_posts' => $this->caps['manage_attendees'],
'delete_posts' => $this->caps['delete_attendees'],
'delete_others_posts' => $this->caps['delete_attendees'],
'read_private_posts' => $this->caps['manage_attendees'],
'edit_post' => $this->caps['manage_attendees'],
'delete_post' => $this->caps['delete_attendees'],
'read_post' => $this->caps['manage_attendees'],
'create_posts' => 'do_not_allow',
),
) );
$supports = array( 'title' );
if ( $this->debug && current_user_can( $this->caps['manage_options'] ) )
$supports[] = 'custom-fields';
register_post_type( 'tix_coupon', array(
'labels' => array(
'name' => __( 'Coupons', 'camptix' ),
'singular_name' => __( 'Coupon', 'camptix' ),
'add_new' => __( 'New Coupon', 'camptix' ),
'add_new_item' => __( 'Add New Coupon', 'camptix' ),
'edit_item' => __( 'Edit Coupon', 'camptix' ),
'new_item' => __( 'New Coupon', 'camptix' ),
'all_items' => __( 'Coupons', 'camptix' ),
'view_item' => __( 'View Coupon', 'camptix' ),
'search_items' => __( 'Search Coupons', 'camptix' ),
'not_found' => __( 'No coupons found', 'camptix' ),
'not_found_in_trash' => __( 'No coupons found in trash', 'camptix' ),
'menu_name' => __( 'Coupons', 'camptix' ),
),
'public' => false,
'query_var' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=tix_ticket',
'supports' => $supports,
'capability_type' => 'tix_coupon',
'capabilities' => array(
'publish_posts' => $this->caps['manage_coupons'],
'edit_posts' => $this->caps['manage_coupons'],
'edit_others_posts' => $this->caps['manage_coupons'],
'delete_posts' => $this->caps['manage_coupons'],
'delete_others_posts' => $this->caps['manage_coupons'],
'read_private_posts' => $this->caps['manage_coupons'],
'edit_post' => $this->caps['manage_coupons'],
'delete_post' => $this->caps['manage_coupons'],
'read_post' => $this->caps['manage_coupons'],
),
) );
// tix_email will store e-mail jobs.
register_post_type( 'tix_email', array(
'labels' => array(
'name' => __( 'E-mails', 'camptix' ),
'singular_name' => __( 'E-mail', 'camptix' ),