PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / modern-events-calendar / actions / register-user-for-an-event.php
suretriggers / src / Integrations / modern-events-calendar / actions Last commit date
register-user-for-an-event.php 11 months ago
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