-
Notifications
You must be signed in to change notification settings - Fork 42
/
RSVP.php
executable file
·1460 lines (1221 loc) · 43.7 KB
/
RSVP.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
class Tribe__Tickets__RSVP extends Tribe__Tickets__Tickets {
/**
* Name of the CPT that holds Attendees (tickets holders).
*
* @var string
*/
const ATTENDEE_OBJECT = 'tribe_rsvp_attendees';
/**
* Meta key that relates Attendees and Events.
*
* @var string
*/
const ATTENDEE_EVENT_KEY = '_tribe_rsvp_event';
/**
* Meta key that relates Attendees and Products.
*
* @var string
*/
const ATTENDEE_PRODUCT_KEY = '_tribe_rsvp_product';
/**
* Currently unused for this provider, but defined per the Tribe__Tickets__Tickets spec.
*
* @var string
*/
const ATTENDEE_ORDER_KEY = '';
/**
* Indicates if a ticket for this attendee was sent out via email.
*
* @var boolean
*/
const ATTENDEE_TICKET_SENT = '_tribe_rsvp_attendee_ticket_sent';
/**
*Name of the CPT that holds Tickets
*
* @var string
*/
public $ticket_object = 'tribe_rsvp_tickets';
/**
* Meta key that relates Products and Events
* @var string
*/
public $event_key = '_tribe_rsvp_for_event';
/**
* Meta key that stores if an attendee has checked in to an event
* @var string
*/
public $checkin_key = '_tribe_rsvp_checkedin';
/**
* Meta key that ties attendees together by order
* @var string
*/
public $order_key = '_tribe_rsvp_order';
/**
* Meta key that holds the security code that's printed in the tickets
* @var string
*/
public $security_code = '_tribe_rsvp_security_code';
/**
* Meta key that if this attendee wants to show on the attendee list
*
* @var string
*/
const ATTENDEE_OPTOUT_KEY = '_tribe_rsvp_attendee_optout';
/**
* Meta key that if this attendee rsvp status
*
* @var string
*/
const ATTENDEE_RSVP_KEY = '_tribe_rsvp_status';
/**
* Meta key that holds the full name of the tickets RSVP "buyer"
*
* @var string
*/
public $full_name = '_tribe_rsvp_full_name';
/**
* Meta key that holds the email of the tickets RSVP "buyer"
*
* @var string
*/
public $email = '_tribe_rsvp_email';
/**
* Meta key that holds the name of a ticket to be used in reports if the Product is deleted
* @var string
*/
public $deleted_product = '_tribe_deleted_product_name';
/**
* @var Tribe__Tickets__RSVP__Attendance_Totals
*/
protected $attendance_totals;
/**
* Messages for submission
*/
protected static $messages = array();
/**
* @var Tribe__Tickets__Tickets_View
*/
protected $tickets_view;
/**
* Creates a Variable to prevent Double FE forms
* @var boolean
*/
private $is_frontend_tickets_form_done = false;
/**
* Instance of this class for use as singleton
*/
private static $instance;
/**
* Creates the instance of the class
*
* @static
* @return void
*/
/**
* Get (and instantiate, if necessary) the instance of the class
*
* @static
* @return Tribe__Tickets__RSVP
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Class constructor
*/
public function __construct() {
$main = Tribe__Tickets__Main::instance();
$this->tickets_view = Tribe__Tickets__Tickets_View::instance();
/* Set up some parent's vars */
$this->pluginName = _x( 'RSVP', 'ticket provider', 'event-tickets' );
$this->pluginPath = $main->plugin_path;
$this->pluginUrl = $main->plugin_url;
parent::__construct();
$this->hooks();
add_action( 'init', array( $this, 'init' ) );
/**
* Whenever we are dealing with Redirects we cannot do stuff on `init`
* Use: `template_redirect`
*
* Was running into an issue of `get_permalink( $event_id )` returning
* the wrong url because it was too early on the execution
*/
add_action( 'template_redirect', array( $this, 'generate_tickets' ) );
add_action( 'event_tickets_attendee_update', array( $this, 'update_attendee_data' ), 10, 3 );
add_action( 'event_tickets_after_attendees_update', array( $this, 'maybe_send_tickets_after_status_change' ) );
}
/**
* Registers all actions/filters
*/
public function hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'register_resources' ), 5 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_resources' ), 11 );
add_action( 'trashed_post', array( $this, 'maybe_redirect_to_attendees_report' ) );
add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
add_action( 'rsvp_checkin', array( $this, 'purge_attendees_transient' ) );
add_action( 'rsvp_uncheckin', array( $this, 'purge_attendees_transient' ) );
add_action( 'tribe_events_tickets_attendees_event_details_top', array( $this, 'setup_attendance_totals' ) );
add_filter(
'event_tickets_attendees_rsvp_checkin_stati',
array( $this, 'filter_event_tickets_attendees_rsvp_checkin_stati' )
);
}
/**
* Hooked to the init action
*/
public function init() {
$this->register_types();
}
/**
* registers resources
*/
public function register_resources() {
$main = Tribe__Tickets__Main::instance();
$stylesheet_url = $main->plugin_url . 'src/resources/css/rsvp.css';
$stylesheet_url = Tribe__Template_Factory::getMinFile( $stylesheet_url, true );
// apply filters
$stylesheet_url = apply_filters( 'tribe_tickets_rsvp_stylesheet_url', $stylesheet_url );
wp_register_style(
'event-tickets-rsvp',
$stylesheet_url,
array(),
apply_filters( 'tribe_tickets_rsvp_css_version', Tribe__Tickets__Main::VERSION )
);
$js_url = $main->plugin_url . 'src/resources/js/rsvp.js';
$js_url = Tribe__Template_Factory::getMinFile( $js_url, true );
$js_url = apply_filters( 'tribe_tickets_rsvp_js_url', $js_url );
wp_register_script(
'event-tickets-rsvp',
$js_url,
array( 'jquery', 'jquery-ui-datepicker' ),
apply_filters( 'tribe_tickets_rsvp_js_version', Tribe__Tickets__Main::VERSION ),
true
);
wp_localize_script( 'event-tickets-rsvp', 'tribe_tickets_rsvp_strings', array(
'attendee' => _x( 'Attendee %1$s', 'Attendee number', 'event-tickets' ),
) );
}
/**
* Enqueue the plugin stylesheet(s).
*
* @author caseypicker
* @since 3.9
* @return void
*/
public function enqueue_resources() {
$post_types = Tribe__Tickets__Main::instance()->post_types();
if ( ! is_singular( $post_types ) ) {
return;
}
wp_enqueue_style( 'event-tickets-rsvp' );
wp_enqueue_script( 'event-tickets-rsvp' );
// Check for override stylesheet
$user_stylesheet_url = Tribe__Templates::locate_stylesheet( 'tribe-events/tickets/rsvp.css' );
// If override stylesheet exists, then enqueue it
if ( $user_stylesheet_url ) {
wp_enqueue_style( 'tribe-events-tickets-rsvp-override-style', $user_stylesheet_url );
}
}
/**
* Register our custom post type
*/
public function register_types() {
register_post_type( $this->ticket_object, array(
'label' => 'Tickets',
'labels' => array(
'name' => __( 'RSVP Tickets', 'event-tickets' ),
'singular_name' => __( 'RSVP Ticket', 'event-tickets' ),
),
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
) );
register_post_type( self::ATTENDEE_OBJECT, array(
'label' => 'Attendees',
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
) );
}
/**
* Adds RSVP attendance totals to the summary box of the attendance
* screen.
*
* Expects to fire during 'tribe_tickets_attendees_page_inside', ie
* before the attendee screen is rendered.
*/
public function setup_attendance_totals() {
$this->attendance_totals()->integrate_with_attendee_screen();
}
/**
* @return Tribe__Tickets__RSVP__Attendance_Totals
*/
public function attendance_totals() {
if ( empty( $this->attendance_totals ) ) {
$this->attendance_totals = new Tribe__Tickets__RSVP__Attendance_Totals;
}
return $this->attendance_totals;
}
/**
* Update the RSVP values for this user.
*
* Note that, within this method, $order_id refers to the attendee or ticket ID
* (it does not refer to an "order" in the sense of a transaction that may include
* multiple tickets, as is the case in some other methods for instance).
*
* @param array $data
* @param int $order_id
* @param int $event_id
*/
public function update_attendee_data( $data, $order_id, $event_id ) {
$user_id = get_current_user_id();
$rsvp_orders = $this->tickets_view->get_event_rsvp_attendees( $event_id, $user_id );
$rsvp_order_ids = wp_list_pluck( $rsvp_orders, 'order_id' );
// This makes sure we don't save attendees for orders that are not from this current user and event
if ( ! in_array( $order_id, $rsvp_order_ids ) ) {
return;
}
$attendee = array();
// Get the Attendee Data, it's important for testing
foreach ( $rsvp_orders as $test_attendee ) {
if ( $order_id !== $test_attendee['order_id'] ) {
continue;
}
$attendee = $test_attendee;
}
// Dont try to Save if it's restricted
if ( ! isset( $attendee['product_id'] )
|| $this->tickets_view->is_rsvp_restricted( $event_id, $attendee['product_id'] )
) {
return;
}
$attendee_email = empty( $data['email'] ) ? null : sanitize_email( $data['email'] );
$attendee_email = is_email( $attendee_email ) ? $attendee_email : null;
$attendee_full_name = empty( $data['full_name'] ) ? null : sanitize_text_field( $data['full_name'] );
$attendee_optout = empty( $data['optout'] ) ? false : (bool) $data['optout'];
if ( empty( $data['order_status'] ) || ! $this->tickets_view->is_valid_rsvp_option( $data['order_status'] ) ) {
$attendee_order_status = null;
} else {
$attendee_order_status = $data['order_status'];
}
$product_id = $attendee['product_id'];
$this->update_sales_by_order_status( $order_id, $attendee_order_status, $product_id );
if ( ! is_null( $attendee_order_status ) ) {
update_post_meta( $order_id, self::ATTENDEE_RSVP_KEY, $attendee_order_status );
}
update_post_meta( $order_id, self::ATTENDEE_OPTOUT_KEY, (bool) $attendee_optout );
if ( ! is_null( $attendee_full_name ) ) {
update_post_meta( $order_id, $this->full_name, $attendee_full_name );
}
if ( ! is_null( $attendee_email ) ) {
update_post_meta( $order_id, $this->email, $attendee_email );
}
}
/**
* Triggers the sending of ticket emails after RSVP information is updated.
*
* This is useful if a user initially suggests they will not be attending
* an event (in which case we do not send tickets out) but where they
* incrementally amend the status of one or more of those tickets to
* attending, at which point we should send tickets out for any of those
* newly attending persons.
*
* @param $event_id
*/
public function maybe_send_tickets_after_status_change( $event_id ) {
$transaction_ids = array();
foreach ( $this->get_event_attendees( $event_id ) as $attendee ) {
$transaction = get_post_meta( $attendee[ 'attendee_id' ], $this->order_key, true );
if ( ! empty( $transaction ) ) {
$transaction_ids[ $transaction ] = $transaction;
}
}
foreach ( $transaction_ids as $transaction ) {
// This method takes care of intelligently sending out emails only when
// required, for attendees that have not yet received their tickets
$this->send_tickets_email( $transaction );
}
}
/**
* Generate and store all the attendees information for a new order.
*/
public function generate_tickets( ) {
if ( empty( $_POST['tickets_process'] ) || empty( $_POST['attendee'] ) || empty( $_POST['product_id'] ) ) {
return;
}
$has_tickets = $post_id = false;
/**
* RSVP specific action fired just before a RSVP-driven attendee tickets for an order are generated
*
* @param $data post Parameters comes from RSVP Form
*/
do_action( 'tribe_tickets_rsvp_before_order_processing', $_POST );
$order_id = md5( time() . rand() );
$attendee_email = empty( $_POST['attendee']['email'] ) ? null : sanitize_email( $_POST['attendee']['email'] );
$attendee_email = is_email( $attendee_email ) ? $attendee_email : null;
$attendee_full_name = empty( $_POST['attendee']['full_name'] ) ? null : sanitize_text_field( $_POST['attendee']['full_name'] );
$attendee_optout = empty( $_POST['attendee']['optout'] ) ? false : (bool) $_POST['attendee']['optout'];
if (
empty( $_POST['attendee']['order_status'] )
|| ! $this->tickets_view->is_valid_rsvp_option( $_POST['attendee']['order_status'] )
) {
$attendee_order_status = 'yes';
} else {
$attendee_order_status = $_POST['attendee']['order_status'];
}
if ( ! $attendee_email || ! $attendee_full_name ) {
$url = get_permalink( $post_id );
$url = add_query_arg( 'rsvp_error', 1, $url );
wp_redirect( esc_url_raw( $url ) );
tribe_exit();
}
$rsvp_options = $this->tickets_view->get_rsvp_options( null, false );
// Iterate over each product
foreach ( (array) $_POST['product_id'] as $product_id ) {
$order_attendee_id = 0;
// Get the event this tickets is for
$post_id = get_post_meta( $product_id, $this->event_key, true );
if ( empty( $post_id ) ) {
continue;
}
$ticket_type = $this->get_ticket( $post_id, $product_id );
// if there were no RSVP tickets for the product added to the cart, continue
if ( empty( $_POST[ "quantity_{$product_id}" ] ) ) {
continue;
}
// get the RSVP status `decrease_stock_by` value
$status_stock_size = $rsvp_options[ $attendee_order_status ]['decrease_stock_by'];
$ticket_qty = intval( $_POST["quantity_{$product_id}"] );
// to avoid tickets from not being created on a status stock size of 0
// let's take the status stock size into account and create a number of tickets
// at least equal to the number of tickets the user requested
$ticket_qty = $status_stock_size < 1 ? $ticket_qty : $status_stock_size * $ticket_qty;
$qty = max( $ticket_qty, 0 );
// Throw an error if Qty is bigger then Remaining
if ( $ticket_type->managing_stock() && $qty > $ticket_type->remaining() ) {
$url = add_query_arg( 'rsvp_error', 2, get_permalink( $post_id ) );
wp_redirect( esc_url_raw( $url ) );
tribe_exit();
}
$has_tickets = true;
/**
* RSVP specific action fired just before a RSVP-driven attendee ticket for an event is generated
*
* @param $post_id ID of event
* @param $ticket_type Ticket Type object for the product
* @param $data post Parameters comes from RSVP Form
*/
do_action( 'tribe_tickets_rsvp_before_attendee_ticket_creation', $post_id, $ticket_type, $_POST );
// Iterate over all the amount of tickets purchased (for this product)
for ( $i = 0; $i < $qty; $i ++ ) {
$attendee = array(
'post_status' => 'publish',
'post_title' => $attendee_full_name . ' | ' . ( $i + 1 ),
'post_type' => self::ATTENDEE_OBJECT,
'ping_status' => 'closed',
);
// Insert individual ticket purchased
$attendee_id = wp_insert_post( $attendee );
if ( $status_stock_size > 0 ) {
$sales = (int) get_post_meta( $product_id, 'total_sales', true );
update_post_meta( $product_id, 'total_sales', ++ $sales );
}
update_post_meta( $attendee_id, self::ATTENDEE_PRODUCT_KEY, $product_id );
update_post_meta( $attendee_id, self::ATTENDEE_EVENT_KEY, $post_id );
update_post_meta( $attendee_id, self::ATTENDEE_RSVP_KEY, $attendee_order_status );
update_post_meta( $attendee_id, $this->security_code, $this->generate_security_code( $attendee_id ) );
update_post_meta( $attendee_id, $this->order_key, $order_id );
update_post_meta( $attendee_id, self::ATTENDEE_OPTOUT_KEY, (bool) $attendee_optout );
update_post_meta( $attendee_id, $this->full_name, $attendee_full_name );
update_post_meta( $attendee_id, $this->email, $attendee_email );
/**
* RSVP specific action fired when a RSVP-driven attendee ticket for an event is generated
*
* @param $attendee_id ID of attendee ticket
* @param $post_id ID of event
* @param $order_id RSVP order ID
* @param $product_id RSVP product ID
*/
do_action( 'event_tickets_rsvp_attendee_created', $attendee_id, $post_id, $order_id );
/**
* Action fired when an RSVP attendee ticket is created
*
* @param $attendee_id ID of the attendee post
* @param $post_id Event post ID
* @param $product_id RSVP ticket post ID
* @param $order_attendee_id Attendee # for order
*/
do_action( 'event_tickets_rsvp_ticket_created', $attendee_id, $post_id, $product_id, $order_attendee_id );
$this->record_attendee_user_id( $attendee_id );
$order_attendee_id++;
}
/**
* Action fired when an RSVP has had attendee tickets generated for it
*
* @param $product_id RSVP ticket post ID
* @param $order_id ID of the RSVP order
* @param $qty Quantity ordered
*/
do_action( 'event_tickets_rsvp_tickets_generated_for_product', $product_id, $order_id, $qty );
// After Adding the Values we Update the Transient
Tribe__Post_Transient::instance()->delete( $post_id, Tribe__Tickets__Tickets::ATTENDEES_CACHE );
}
/**
* Fires when an RSVP attendee tickets have been generated.
*
* @param int $order_id ID of the RSVP order
* @param int $post_id ID of the post the order was placed for
* @param string $attendee_order_status 'yes' if the user indicated they will attend
*/
do_action( 'event_tickets_rsvp_tickets_generated', $order_id, $post_id, $attendee_order_status );
$send_mail_stati = array( 'yes' );
/**
* Filters whether a confirmation email should be sent or not for RSVP tickets.
*
* This applies to attendance and non attendance emails.
*
* @param bool $send_mail Defaults to `true`.
*/
$send_mail = apply_filters('tribe_tickets_rsvp_send_mail', true);
if ( $send_mail ) {
/**
* Filters the attendee order stati that should trigger an attendance confirmation.
*
* Any attendee order status not listed here will trigger a non attendance email.
*
* @param array $send_mail_stati An array of default stati triggering an attendance email.
* @param int $order_id ID of the RSVP order
* @param int $post_id ID of the post the order was placed for
* @param string $attendee_order_status 'yes' if the user indicated they will attend
*/
$send_mail_stati = apply_filters(
'tribe_tickets_rsvp_send_mail_stati', $send_mail_stati, $order_id, $post_id, $attendee_order_status
);
// No point sending tickets if their current intention is not to attend
if ( $has_tickets && in_array( $attendee_order_status, $send_mail_stati ) ) {
$this->send_tickets_email( $order_id );
} elseif ( $has_tickets ) {
$this->send_non_attendance_confirmation( $order_id, $post_id );
}
}
// Redirect to the same page to prevent double purchase on refresh
if ( ! empty( $post_id ) ) {
$url = get_permalink( $post_id );
$url = add_query_arg( 'rsvp_sent', 1, $url );
wp_redirect( esc_url_raw( $url ) );
tribe_exit();
}
}
public function send_tickets_email( $order_id ) {
$all_attendees = $this->get_attendees_by_transaction( $order_id );
$to_send = array();
if ( empty( $all_attendees ) ) {
return;
}
// Look at each attendee and check if a ticket was sent: in each case where a ticket
// has not yet been sent we should a) send the ticket out by email and b) record the
// fact it was sent
foreach ( $all_attendees as $single_attendee ) {
// Do not add those attendees/tickets marked as not attending (note that despite the name
// 'qr_ticket_id', this key is not QR code specific, it's simply the attendee post ID)
if ( 'yes' !== get_post_meta( $single_attendee[ 'qr_ticket_id' ], self::ATTENDEE_RSVP_KEY, true ) ) {
continue;
}
// Only add those attendees/tickets that haven't already been sent
if ( empty( $single_attendee[ 'ticket_sent' ] ) ) {
$to_send[] = $single_attendee;
update_post_meta( $single_attendee[ 'qr_ticket_id' ], self::ATTENDEE_TICKET_SENT, true );
}
}
/**
* Controls the list of tickets which will be emailed out.
*
* @param array $to_send list of tickets to be sent out by email
* @param array $all_attendees list of all attendees/tickets, including those already sent out
* @param int $order_id
*
*/
$to_send = (array) apply_filters( 'tribe_tickets_rsvp_tickets_to_send', $to_send, $all_attendees, $order_id );
if ( empty( $to_send ) ) {
return;
}
// For now all ticket holders in an order share the same email
$to = $all_attendees['0']['holder_email'];
if ( ! is_email( $to ) ) {
return;
}
$content = apply_filters( 'tribe_rsvp_email_content', $this->generate_tickets_email_content( $to_send ) );
$headers = apply_filters( 'tribe_rsvp_email_headers', array( 'Content-type: text/html' ) );
$attachments = apply_filters( 'tribe_rsvp_email_attachments', array() );
$to = apply_filters( 'tribe_rsvp_email_recipient', $to );
$subject = apply_filters( 'tribe_rsvp_email_subject',
sprintf( __( 'Your tickets from %s', 'event-tickets' ), stripslashes_deep( html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ) ) );
wp_mail( $to, $subject, $content, $headers, $attachments );
}
/**
* Dispatches a confirmation email that acknowledges the user has RSVP'd
* in cases where they have indicated that they will *not* be attending.
*
* @param int $order_id
* @param int $event_id
*/
public function send_non_attendance_confirmation( $order_id, $event_id ) {
$attendees = $this->get_attendees_by_transaction( $order_id );
if ( empty( $attendees ) ) {
return;
}
// For now all ticket holders in an order share the same email
$to = $attendees['0']['holder_email'];
if ( ! is_email( $to ) ) {
return;
}
$headers = apply_filters( 'tribe_rsvp_email_headers', array( 'Content-type: text/html' ) );
$attachments = apply_filters( 'tribe_rsvp_email_attachments', array() );
$to = apply_filters( 'tribe_rsvp_email_recipient', $to );
$subject = apply_filters( 'tribe_rsvp_email_subject',
sprintf( __( 'You confirmed you will not be attending %s', 'event-tickets' ), get_the_title( $event_id ) )
);
$template_data = array( 'event_id' => $event_id, 'order_id' => $order_id, 'attendees' => $attendees );
$content = apply_filters( 'tribe_rsvp_email_content',
tribe_tickets_get_template_part( 'tickets/email-non-attendance', null, $template_data, false )
);
wp_mail( $to, $subject, $content, $headers, $attachments );
}
protected function get_attendees_by_transaction( $order_id ) {
$attendees = array();
$query = new WP_Query( array(
'post_type' => self::ATTENDEE_OBJECT,
'meta_key' => $this->order_key,
'meta_value' => $order_id,
'posts_per_page' => - 1,
) );
foreach ( $query->posts as $post ) {
$product = get_post( get_post_meta( $post->ID, self::ATTENDEE_PRODUCT_KEY, true ) );
$ticket_unique_id = get_post_meta( $post->ID, '_unique_id', true );
$ticket_unique_id = $ticket_unique_id === '' ? $post->ID : $ticket_unique_id;
$attendees[] = array(
'event_id' => get_post_meta( $post->ID, self::ATTENDEE_EVENT_KEY, true ),
'product_id' => ! empty( $product ) ? $product->ID : false,
'ticket_name' => ! empty( $product ) ? $product->post_title : false,
'holder_name' => get_post_meta( $post->ID, $this->full_name, true ),
'holder_email' => get_post_meta( $post->ID, $this->email, true ),
'order_id' => $order_id,
'ticket_id' => $ticket_unique_id,
'qr_ticket_id' => $post->ID,
'security_code' => get_post_meta( $post->ID, $this->security_code, true ),
'optout' => (bool) get_post_meta( $post->ID, self::ATTENDEE_OPTOUT_KEY, true ),
'ticket_sent' => (bool) get_post_meta( $post->ID, self::ATTENDEE_TICKET_SENT, true ),
);
}
return $attendees;
}
/**
* Generates the validation code that will be printed in the ticket.
* It purpose is to be used to validate the ticket at the door of an event.
*
* @param int $attendee_id
*
* @return string
*/
private function generate_security_code( $attendee_id ) {
return substr( md5( rand() . '_' . $attendee_id ), 0, 10 );
}
/**
* Saves a given ticket (WooCommerce product)
*
* @param int $event_id
* @param Tribe__Tickets__Ticket_Object $ticket
* @param array $raw_data
*
* @return int The updated/created ticket post ID
*/
public function save_ticket( $event_id, $ticket, $raw_data = array() ) {
// assume we are updating until we find out otherwise
$save_type = 'update';
if ( empty( $ticket->ID ) ) {
$save_type = 'create';
/* Create main product post */
$args = array(
'post_status' => 'publish',
'post_type' => $this->ticket_object,
'post_author' => get_current_user_id(),
'post_excerpt' => $ticket->description,
'post_title' => $ticket->name,
);
$ticket->ID = wp_insert_post( $args );
// Relate event <---> ticket
add_post_meta( $ticket->ID, $this->event_key, $event_id );
} else {
$args = array(
'ID' => $ticket->ID,
'post_excerpt' => $ticket->description,
'post_title' => $ticket->name,
);
$ticket->ID = wp_update_post( $args );
}
if ( ! $ticket->ID ) {
return false;
}
update_post_meta( $ticket->ID, '_price', $ticket->price );
if ( trim( $raw_data['ticket_rsvp_stock'] ) !== '' ) {
$stock = (int) $raw_data['ticket_rsvp_stock'];
update_post_meta( $ticket->ID, '_stock', $stock );
update_post_meta( $ticket->ID, '_manage_stock', 'yes' );
} else {
delete_post_meta( $ticket->ID, '_stock_status' );
update_post_meta( $ticket->ID, '_manage_stock', 'no' );
}
if ( isset( $ticket->start_date ) ) {
update_post_meta( $ticket->ID, '_ticket_start_date', $ticket->start_date );
} else {
delete_post_meta( $ticket->ID, '_ticket_start_date' );
}
if ( isset( $ticket->end_date ) ) {
update_post_meta( $ticket->ID, '_ticket_end_date', $ticket->end_date );
} else {
delete_post_meta( $ticket->ID, '_ticket_end_date' );
}
/**
* Generic action fired after saving a ticket (by type)
*
* @var int Post ID of post the ticket is tied to
* @var Tribe__Tickets__Ticket_Object Ticket that was just saved
* @var array Ticket data
* @var string Commerce engine class
*/
do_action( 'event_tickets_after_' . $save_type . '_ticket', $event_id, $ticket, $raw_data, __CLASS__ );
/**
* Generic action fired after saving a ticket
*
* @var int Post ID of post the ticket is tied to
* @var Tribe__Tickets__Ticket_Object Ticket that was just saved
* @var array Ticket data
* @var string Commerce engine class
*/
do_action( 'event_tickets_after_save_ticket', $event_id, $ticket, $raw_data, __CLASS__ );
return $ticket->ID;
}
/**
* Deletes a ticket
*
* @param $event_id
* @param $ticket_id
*
* @return bool
*/
public function delete_ticket( $event_id, $ticket_id ) {
// Ensure we know the event and product IDs (the event ID may not have been passed in)
if ( empty( $event_id ) ) {
$event_id = get_post_meta( $ticket_id, self::ATTENDEE_EVENT_KEY, true );
}
// Additional check (in case we were passed an invalid ticket ID and still can't determine the event)
if ( empty( $event_id ) ) {
return false;
}
$product_id = get_post_meta( $ticket_id, self::ATTENDEE_PRODUCT_KEY, true );
// For attendees whose status ('going' or 'not going') for whom a stock adjustment is required?
$rsvp_options = $this->tickets_view->get_rsvp_options( null, false );
$attendee_status = get_post_meta( $ticket_id, self::ATTENDEE_RSVP_KEY, true );
$adjustment = isset( $rsvp_options[ $attendee_status ]['decrease_stock_by'] )
? absint( $rsvp_options[ $attendee_status ]['decrease_stock_by'] )
: false;
// Adjust the sales figure if required
if ( $adjustment ) {
$sales = (int) get_post_meta( $product_id, 'total_sales', true );
update_post_meta( $product_id, 'total_sales', $sales - $adjustment );
}
//Store name so we can still show it in the attendee list
$attendees = $this->get_attendees( $event_id );
$post_to_delete = get_post( $ticket_id );
foreach ( (array) $attendees as $attendee ) {
if ( $attendee['product_id'] == $ticket_id ) {
update_post_meta( $attendee['attendee_id'], $this->deleted_product,
esc_html( $post_to_delete->post_title ) );
}
}
// Try to kill the actual ticket/attendee post
$delete = wp_delete_post( $ticket_id, true );
if ( is_wp_error( $delete ) ) {
return false;
}
Tribe__Tickets__Attendance::instance( $event_id )->increment_deleted_attendees_count();
do_action( 'tickets_rsvp_ticket_deleted', $ticket_id, $event_id, $product_id );
Tribe__Post_Transient::instance()->delete( $event_id, Tribe__Tickets__Tickets::ATTENDEES_CACHE );
return true;
}
/**
* Returns all the tickets for an event
*
* @param int $event_id
*
* @return array
*/
protected function get_tickets( $event_id ) {
$ticket_ids = $this->get_tickets_ids( $event_id );
if ( ! $ticket_ids ) {
return array();
}
$tickets = array();
foreach ( $ticket_ids as $post ) {
$tickets[] = $this->get_ticket( $event_id, $post );
}
return $tickets;
}
/**
* Shows the tickets form in the front end
*
* @param $content
*
* @return void
*/
public function front_end_tickets_form( $content ) {
if ( $this->is_frontend_tickets_form_done ) {
return $content;
}
$post = $GLOBALS['post'];
// For recurring events (child instances only), default to loading tickets for the parent event
if ( ! empty( $post->post_parent ) && function_exists( 'tribe_is_recurring_event' ) && tribe_is_recurring_event( $post->ID ) ) {
$post = get_post( $post->post_parent );
}
$tickets = $this->get_tickets( $post->ID );
if ( empty( $tickets ) ) {
return;
}
$rsvp_sent = empty( $_GET['rsvp_sent'] ) ? false : true;
$rsvp_error = empty( $_GET['rsvp_error'] ) ? false : intval( $_GET['rsvp_error'] );
if ( $rsvp_sent ) {
$this->add_message( __( 'Your RSVP has been received! Check your email for your RSVP confirmation.', 'event-tickets' ), 'success' );
}
if ( $rsvp_error ) {
switch ( $rsvp_error ) {
case 2:
$this->add_message( __( 'You can\'t RSVP more than the total remaining tickets.', 'event-tickets' ), 'error' );
break;
case 1:
default:
$this->add_message( __( 'In order to RSVP, you must enter your name and a valid email address.', 'event-tickets' ), 'error' );
break;
}
}
$must_login = ! is_user_logged_in() && $this->login_required();
include $this->getTemplateHierarchy( 'tickets/rsvp' );
// It's only done when it's included
$this->is_frontend_tickets_form_done = true;
}
/**
* Indicates if we currently require users to be logged in before they can obtain
* tickets.
*
* @return bool
*/
protected function login_required() {
$requirements = (array) tribe_get_option( 'ticket-authentication-requirements', array() );
return in_array( 'event-tickets_rsvp', $requirements );
}
/**
* Gets an individual ticket