suretriggers
/
src
/
Integrations
/
modern-events-calendar
/
actions
/
register-user-for-an-event.php
register-user-for-an-event.php
254 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RegisterUserForAnEvent. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RegisterUserForAnEvent |
| 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 | namespace SureTriggers\Integrations\ModernEventsCalendar\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Integrations\ModernEventsCalendar\ModernEventsCalendar; |
| 19 | use SureTriggers\Integrations\WordPress\WordPress; |
| 20 | use SureTriggers\Traits\SingletonLoader; |
| 21 | /** |
| 22 | * RegisterUserForAnEvent |
| 23 | * |
| 24 | * @category RegisterUserForAnEvent |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class RegisterUserForAnEvent extends AutomateAction { |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Integration type. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $integration = 'ModernEventsCalendar'; |
| 40 | |
| 41 | /** |
| 42 | * Action name. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $action = 'mec_register_user_for_an_event'; |
| 47 | |
| 48 | use SingletonLoader; |
| 49 | |
| 50 | /** |
| 51 | * Register a action. |
| 52 | * |
| 53 | * @param array $actions actions. |
| 54 | * @return array |
| 55 | */ |
| 56 | public function register( $actions ) { |
| 57 | |
| 58 | $actions[ $this->integration ][ $this->action ] = [ |
| 59 | 'label' => __( 'Register User for an Event', 'suretriggers' ), |
| 60 | 'action' => $this->action, |
| 61 | 'function' => [ $this, 'action_listener' ], |
| 62 | ]; |
| 63 | |
| 64 | return $actions; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action listener. |
| 70 | * |
| 71 | * @param int $user_id user_id. |
| 72 | * @param int $automation_id automation_id. |
| 73 | * @param array $fields fields. |
| 74 | * @param array $selected_options selected_options. |
| 75 | * |
| 76 | * @return array|void |
| 77 | * |
| 78 | * @throws Exception Exception. |
| 79 | */ |
| 80 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 81 | |
| 82 | // Check hard dependency for \MEC_gateway_pay_locally class if it exists. |
| 83 | if ( ! class_exists( '\MEC_gateway_pay_locally' ) ) { |
| 84 | return [ |
| 85 | 'status' => 'error', |
| 86 | 'message' => __( '\MEC_gateway_pay_locally class not found.', 'suretriggers' ), |
| 87 | |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | // Check hard dependency for \MEC_feature_books class if it exists. |
| 92 | if ( ! class_exists( '\MEC_feature_books' ) ) { |
| 93 | return [ |
| 94 | 'status' => 'error', |
| 95 | 'message' => __( '\MEC_feature_books class not found.', 'suretriggers' ), |
| 96 | |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | $gateway = new \MEC_gateway_pay_locally(); |
| 101 | $mec_book = new \MEC_feature_books(); |
| 102 | |
| 103 | $event_id = absint( sanitize_text_field( $selected_options['event_id'] ) ); |
| 104 | $selected_ticket_id = absint( sanitize_text_field( $selected_options['ticket_id'] ) ); |
| 105 | $wp_user_email = sanitize_text_field( $selected_options['wp_user_email'] ); |
| 106 | |
| 107 | if ( ! is_email( $wp_user_email ) ) { |
| 108 | return [ |
| 109 | 'status' => 'error', |
| 110 | 'message' => 'Invalid user email.', |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | $user = get_user_by( 'email', $wp_user_email ); |
| 115 | |
| 116 | if ( ! $user ) { |
| 117 | return [ |
| 118 | 'status' => 'error', |
| 119 | 'message' => 'User email not exists.', |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | $book = $mec_book->getBook(); |
| 124 | |
| 125 | $attendee = [ |
| 126 | 'email' => $user->user_email, |
| 127 | 'name' => $user->display_name, |
| 128 | 'reg' => [], |
| 129 | ]; |
| 130 | |
| 131 | // Generate new user id from gateway registration. |
| 132 | $user_id = $gateway->register_user( $attendee ); |
| 133 | |
| 134 | // The date. |
| 135 | $event_date = null; |
| 136 | $event_date_from_meta = get_post_meta( $event_id, 'mec_date', true ); |
| 137 | |
| 138 | // OCC Timestamp. |
| 139 | if ( is_array( $event_date_from_meta ) && isset( $event_date_from_meta['start'] ) && isset( $event_date_from_meta['end'] ) ) { |
| 140 | $event_date = $book->timestamp( $event_date_from_meta['start'], $event_date_from_meta['end'] ); |
| 141 | } else { |
| 142 | // log error here. |
| 143 | $error_message = 'Event Start Date and End Date is missing. Please check if the select Event has a corresponding dates.'; |
| 144 | return [ |
| 145 | 'status' => 'error', |
| 146 | 'message' => $error_message, |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | // The attendees count. We will set it to `1` since there can only be 1 logged-in user at a time. |
| 151 | $attendees_count = 1; |
| 152 | |
| 153 | // The ticket ID. |
| 154 | $tickets = []; |
| 155 | |
| 156 | // This will hold the comma separated value later on for the ticket IDs. |
| 157 | $ticket_ids = ''; |
| 158 | |
| 159 | for ( $i = 1; $i <= $attendees_count; $i ++ ) { |
| 160 | $tickets[] = array_merge( |
| 161 | $attendee, |
| 162 | [ |
| 163 | 'id' => $selected_ticket_id, |
| 164 | // MEC_SELECTED_TICKET_ID. |
| 165 | 'count' => 1, |
| 166 | 'variations' => [], |
| 167 | 'reg' => $attendee['reg'], |
| 168 | ] |
| 169 | ); |
| 170 | |
| 171 | $ticket_ids .= $selected_ticket_id . ','; |
| 172 | } |
| 173 | |
| 174 | $raw_tickets = [ $selected_ticket_id => $attendees_count ]; |
| 175 | $event_tickets = get_post_meta( $event_id, 'mec_tickets', true ); |
| 176 | |
| 177 | // Calculate price of bookings. |
| 178 | $price_details = $book->get_price_details( $raw_tickets, $event_id, $event_tickets, [] ); |
| 179 | |
| 180 | // Configure the transaction. |
| 181 | $transaction = [ |
| 182 | 'tickets' => $tickets, |
| 183 | 'date' => $event_date, |
| 184 | 'event_id' => $event_id, |
| 185 | 'price_details' => $price_details, |
| 186 | 'total' => $price_details['total'], |
| 187 | 'discount' => 0, |
| 188 | 'price' => $price_details['total'], |
| 189 | 'coupon' => null, |
| 190 | 'fields' => [], |
| 191 | ]; |
| 192 | |
| 193 | // Save The Transaction. |
| 194 | $transaction_id = $book->temporary( $transaction ); |
| 195 | |
| 196 | // Create new booking (CPT). |
| 197 | $book_args = [ |
| 198 | 'post_author' => $user_id, |
| 199 | 'post_type' => 'mec-books', |
| 200 | 'post_title' => sprintf( '%s - %s', $user->display_name, $user->user_email ), |
| 201 | ]; |
| 202 | |
| 203 | $booking_id = $book->add( $book_args, $transaction_id, ',' . trim( $ticket_ids, ', ' ) . ',' ); |
| 204 | |
| 205 | // Update the `mec_attendees`. |
| 206 | update_post_meta( $booking_id, 'mec_attendees', $tickets ); |
| 207 | update_post_meta( $booking_id, 'mec_reg', $attendee['reg'] ); |
| 208 | update_post_meta( $booking_id, 'mec_gateway', 'MEC_gateway_pay_locally' ); |
| 209 | update_post_meta( $booking_id, 'mec_gateway_label', $gateway->title() ); |
| 210 | |
| 211 | // For Booking Badge. |
| 212 | update_post_meta( $booking_id, 'mec_book_date_submit', gmdate( 'YmdHis', time() ) ); |
| 213 | |
| 214 | // Execute pending action. |
| 215 | do_action( 'mec_booking_pended', $booking_id ); |
| 216 | |
| 217 | // Send notification if it's a new booking. |
| 218 | try { |
| 219 | if ( $this->is_new_booking( $booking_id ) ) { |
| 220 | do_action( 'mec_booking_completed', $booking_id ); |
| 221 | } |
| 222 | } catch ( \Exception $e ) { |
| 223 | throw new Exception( $e->getMessage() ); |
| 224 | } |
| 225 | |
| 226 | $context = []; |
| 227 | $context['booking_id'] = $booking_id; |
| 228 | $context['ticket_id'] = $selected_ticket_id; |
| 229 | |
| 230 | return array_merge( |
| 231 | WordPress::get_user_context( $user->ID ), |
| 232 | ModernEventsCalendar::get_event_context( $event_id ), |
| 233 | $context |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Check if booking is new or not. |
| 239 | * |
| 240 | * @param int $booking_id Booking ID. |
| 241 | * @return bool True if booking already exists. Otherwise, false. |
| 242 | * @throws \Exception Exception. |
| 243 | */ |
| 244 | public function is_new_booking( $booking_id = 0 ) { |
| 245 | if ( empty( $booking_id ) ) { |
| 246 | throw new \Exception( 'Booking ID is empty.' ); |
| 247 | } |
| 248 | // Return true since the MEC action `Register the user for {event}` always registers a new booking. |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | RegisterUserForAnEvent::get_instance(); |
| 254 |