send-rsvp.php
146 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EventCalendarSendRsvp. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category EventCalendarSendRsvp |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | use SureTriggers\Integrations\AutomateAction; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | |
| 17 | /** |
| 18 | * EventCalendarSendRsvp |
| 19 | * |
| 20 | * @category EventCalendarSendRsvp |
| 21 | * @package SureTriggers |
| 22 | * @author BSF <username@example.com> |
| 23 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 24 | * @link https://www.brainstormforce.com/ |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class EventCalendarSendRsvp extends AutomateAction { |
| 28 | |
| 29 | /** |
| 30 | * Integration type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $integration = 'TheEventCalendar'; |
| 35 | |
| 36 | /** |
| 37 | * Action name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $action = 'event_calendar_send_rsvp'; |
| 42 | |
| 43 | use SingletonLoader; |
| 44 | |
| 45 | /** |
| 46 | * Register a action. |
| 47 | * |
| 48 | * @param array $actions actions. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function register( $actions ) { |
| 52 | $actions[ $this->integration ][ $this->action ] = [ |
| 53 | 'label' => __( 'RSVP on behalf of the user for an event', 'suretriggers' ), |
| 54 | 'action' => $this->action, |
| 55 | 'function' => [ $this, 'action_listener' ], |
| 56 | ]; |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id user_id. |
| 64 | * @param int $automation_id automation_id. |
| 65 | * @param array $fields fields. |
| 66 | * @param array $selected_options selectedOptions. |
| 67 | * @psalm-suppress UndefinedMethod |
| 68 | * @throws Exception Exception. |
| 69 | * |
| 70 | * @return array|bool|void |
| 71 | */ |
| 72 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 73 | |
| 74 | if ( ! class_exists( 'Tribe__Tickets__Main' ) || ! class_exists( 'Tribe__Events__Main' ) || ! class_exists( 'Tribe__Tickets__Tickets_Handler' ) || ! class_exists( 'Tribe__Tickets__RSVP' ) ) { |
| 75 | return [ |
| 76 | 'status' => 'error', |
| 77 | 'message' => 'The Events Calendar or Event Tickets plugin not installed.', |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | $attendee_name = $selected_options['attendee_name']; |
| 82 | $attendee_email = $selected_options['attendee_email']; |
| 83 | $number_of_guests = $selected_options['number_of_guests']; |
| 84 | |
| 85 | if ( ! is_numeric( $number_of_guests ) ) { |
| 86 | return [ |
| 87 | 'status' => 'error', |
| 88 | 'message' => 'Number of Guests should be a numeric value.', |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $ticket_handler = new Tribe__Tickets__Tickets_Handler(); |
| 93 | $get_rsvp_tickets = $ticket_handler->get_event_rsvp_tickets( get_post( $selected_options['event_calendar_rsvp_event'] ) ); |
| 94 | |
| 95 | if ( empty( $get_rsvp_tickets ) ) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | $ticket_id = 0; |
| 100 | foreach ( $get_rsvp_tickets as $rsvp_ticket ) { |
| 101 | if ( $rsvp_ticket->capacity < 0 ) { |
| 102 | $ticket_id = $rsvp_ticket->ID; |
| 103 | } elseif ( $rsvp_ticket->capacity > 0 && $rsvp_ticket->capacity > $rsvp_ticket->qty_sold ) { |
| 104 | $ticket_id = $rsvp_ticket->ID; |
| 105 | } |
| 106 | if ( $ticket_id > 0 ) { |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | $attendee_details = [ |
| 112 | 'full_name' => $attendee_name, |
| 113 | 'email' => $attendee_email, |
| 114 | 'order_status' => 'yes', |
| 115 | 'optout' => false, |
| 116 | 'order_id' => '-1', |
| 117 | ]; |
| 118 | |
| 119 | $order = new Tribe__Tickets__RSVP(); |
| 120 | $status = $order->generate_tickets_for( $ticket_id, $number_of_guests, $attendee_details ); |
| 121 | |
| 122 | if ( ! $status ) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | $context = []; |
| 127 | $context['event_id'] = $selected_options['event_calendar_rsvp_event']; |
| 128 | $context['attendee_name'] = $attendee_name; |
| 129 | $context['attendee_email'] = $attendee_email; |
| 130 | $context['number_of_guests'] = $number_of_guests; |
| 131 | |
| 132 | $event = tribe_get_event( $context['event_id'] ); |
| 133 | |
| 134 | if ( $event ) { |
| 135 | $context['event_name'] = $event->post_title; |
| 136 | $context['event_url'] = get_permalink( $event->ID ); |
| 137 | $context['event_featured_image_id'] = get_post_meta( $event->ID, '_thumbnail_id', true ); |
| 138 | $context['event_featured_image_url'] = get_the_post_thumbnail_url( $event->ID ); |
| 139 | } |
| 140 | |
| 141 | return $context; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | EventCalendarSendRsvp::get_instance(); |
| 146 |