· 5 years ago · Oct 21, 2020, 07:54 AM
1<?php
2/** no direct access **/
3defined('MECEXEC') or die();
4
5/**
6 * Webnus MEC fes (Frontend Event Submission) class.
7 * @author Webnus <info@webnus.biz>
8 */
9class MEC_feature_fes extends MEC_base
10{
11 public $factory;
12 public $main;
13 public $db;
14 public $settings;
15 public $PT;
16 public $render;
17
18 /**
19 * Constructor method
20 * @author Webnus <info@webnus.biz>
21 */
22 public function __construct()
23 {
24 // Import MEC Factory
25 $this->factory = $this->getFactory();
26
27 // Import MEC Main
28 $this->main = $this->getMain();
29
30 // Import MEC DB
31 $this->db = $this->getDB();
32
33 // MEC Settings
34 $this->settings = $this->main->get_settings();
35
36 // Event Post Type
37 $this->PT = $this->main->get_main_post_type();
38 }
39
40 /**
41 * Initialize colors feature
42 * @author Webnus <info@webnus.biz>
43 */
44 public function init()
45 {
46 // Frontend Event Submission Form
47 $this->factory->shortcode('MEC_fes_form', array($this, 'vform'));
48
49 // Event Single Page
50 $this->factory->shortcode('MEC_fes_list', array($this, 'vlist'));
51
52 // Process the event form
53 $this->factory->action('wp_ajax_mec_fes_form', array($this, 'fes_form'));
54 $this->factory->action('wp_ajax_nopriv_mec_fes_form', array($this, 'fes_form'));
55
56 // Upload featured image
57 $this->factory->action('wp_ajax_mec_fes_upload_featured_image', array($this, 'fes_upload'));
58 $this->factory->action('wp_ajax_nopriv_mec_fes_upload_featured_image', array($this, 'fes_upload'));
59
60 // Export the event
61 $this->factory->action('wp_ajax_mec_fes_csv_export', array($this, 'mec_fes_csv_export'));
62
63 // Remove the event
64 $this->factory->action('wp_ajax_mec_fes_remove', array($this, 'fes_remove'));
65
66 // Event Published
67 $this->factory->action('transition_post_status', array($this, 'status_changed'), 10, 3);
68 }
69
70 /**
71 * Generate frontend event submission form view
72 * @author Webnus <info@webnus.biz>
73 * @param array $atts
74 * @return string
75 */
76 public function vform($atts = array())
77 {
78 // Force to array
79 if(!is_array($atts)) $atts = array();
80
81 if(isset($_GET['vlist']) and $_GET['vlist'] == 1)
82 {
83 return $this->vlist();
84 }
85
86 // Show login/register message if user is not logged in and guest submission is not enabled.
87 if(!is_user_logged_in() and (!isset($this->settings['fes_guest_status']) or (isset($this->settings['fes_guest_status']) and $this->settings['fes_guest_status'] == '0')))
88 {
89 // Show message
90 $message = sprintf(__('Please %s/%s in order to submit new events.', 'mec'), '<a href="'.wp_login_url($this->main->get_full_url()).'">'.__('Login', 'mec').'</a>', '<a href="'.wp_registration_url().'">'.__('Register', 'mec').'</a>');
91
92 ob_start();
93 include MEC::import('app.features.fes.message', true, true);
94 return $output = ob_get_clean();
95 }
96
97 $post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : -1;
98
99 // Selected post is not an event
100 if($post_id > 0 and get_post_type($post_id) != $this->PT)
101 {
102 // Show message
103 $message = __("Sorry! Selected post is not an event.", 'mec');
104
105 ob_start();
106 include MEC::import('app.features.fes.message', true, true);
107 return $output = ob_get_clean();
108 }
109
110 // Show a warning to current user if modification of post is not possible for him/her
111 if($post_id != -1 and !current_user_can('edit_post', $post_id))
112 {
113 // Show message
114 $message = __("Sorry! You don't have access to modify this event.", 'mec');
115
116 ob_start();
117 include MEC::import('app.features.fes.message', true, true);
118 return $output = ob_get_clean();
119 }
120
121 $post = get_post($post_id);
122
123 if($post_id == -1)
124 {
125 $post = new stdClass();
126 $post->ID = -1;
127 }
128
129 $path = MEC::import('app.features.fes.form', true, true);
130
131 ob_start();
132 include $path;
133 return $output = ob_get_clean();
134 }
135
136 /**
137 * Generate frontend event submission list view
138 * @author Webnus <info@webnus.biz>
139 * @param array $atts
140 * @return string
141 */
142 public function vlist($atts = array())
143 {
144 // Force to array
145 if(!is_array($atts)) $atts = array();
146
147 $post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : NULL;
148
149 // Show a warning to current user if modification of post is not possible for him/her
150 if($post_id > 0 and !current_user_can('edit_post', $post_id))
151 {
152 // Show message
153 $message = __("Sorry! You don't have access to modify this event.", 'mec');
154
155 ob_start();
156 include MEC::import('app.features.fes.message', true, true);
157 return $output = ob_get_clean();
158 }
159 elseif($post_id == -1 or ($post_id > 0 and current_user_can('edit_post', $post_id)))
160 {
161 return $this->vform();
162 }
163
164 // Show login/register message if user is not logged in
165 if(!is_user_logged_in())
166 {
167 // Show message
168 $message = sprintf(__('Please %s/%s in order to manage events.', 'mec'), '<a href="'.wp_login_url($this->main->get_full_url()).'">'.__('Login', 'mec').'</a>', '<a href="'.wp_registration_url().'">'.__('Register', 'mec').'</a>');
169
170 ob_start();
171 include MEC::import('app.features.fes.message', true, true);
172 return $output = ob_get_clean();
173 }
174
175 $path = MEC::import('app.features.fes.list', true, true);
176
177 ob_start();
178 include $path;
179 return $output = ob_get_clean();
180 }
181
182 public function fes_remove()
183 {
184 // Check if our nonce is set.
185 if(!isset($_POST['_wpnonce'])) $this->main->response(array('success'=>0, 'code'=>'NONCE_MISSING'));
186
187 // Verify that the nonce is valid.
188 if(!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'mec_fes_remove')) $this->main->response(array('success'=>0, 'code'=>'NONCE_IS_INVALID'));
189
190 $post_id = isset($_POST['post_id']) ? sanitize_text_field($_POST['post_id']) : 0;
191
192 // Verify current user can remove the event
193 if(!current_user_can('delete_post', $post_id)) $this->main->response(array('success'=>0, 'code'=>'USER_CANNOT_REMOVE_EVENT'));
194
195 // Trash the event
196 wp_delete_post($post_id);
197
198 $this->main->response(array('success'=>1, 'message'=>__('Event removed!', 'mec')));
199 }
200
201 public function mec_fes_csv_export()
202 {
203 if((!isset($_POST['mec_event_id'])) or (!isset($_POST['fes_nonce'])) or (!wp_verify_nonce($_POST['fes_nonce'], 'mec_fes_nonce'))) die(json_encode(array('ex' => "error")));
204
205 $event_id = intval($_POST['mec_event_id']);
206 $timestamp = isset($_POST['timestamp']) ? sanitize_text_field($_POST['timestamp']) : 0;
207 $booking_ids = '';
208
209 ob_start();
210 header('Content-Type: text/csv; charset=utf-8');
211
212 if($timestamp)
213 {
214 $bookings = $this->main->get_bookings($event_id, $timestamp);
215 foreach($bookings as $booking)
216 {
217 $booking_ids .= $booking->ID.',';
218 }
219 }
220
221 $post_ids = trim($booking_ids) ? explode(',', trim($booking_ids, ', ')) : array();
222
223 if(!count($post_ids))
224 {
225 $books = $this->db->select("SELECT `post_id` FROM `#__postmeta` WHERE `meta_key`='mec_event_id' AND `meta_value`={$event_id}", 'loadAssocList');
226 foreach($books as $book) if(isset($book['post_id'])) $post_ids[] = $book['post_id'];
227 }
228
229 $event_ids = array();
230 foreach($post_ids as $post_id) $event_ids[] = get_post_meta($post_id, 'mec_event_id', true);
231 $event_ids = array_unique($event_ids);
232
233 $main_event_id = NULL;
234 if(count($event_ids) == 1) $main_event_id = $event_ids[0];
235
236 $columns = array(__('ID', 'mec'), __('Event', 'mec'), __('Date', 'mec'), __('Order Time', 'mec'), $this->main->m('ticket', __('Ticket', 'mec')), __('Transaction ID', 'mec'), __('Total Price', 'mec'), __('Name', 'mec'), __('Email', 'mec'), __('Ticket Variation', 'mec'), __('Confirmation', 'mec'), __('Verification', 'mec'));
237 $columns = apply_filters('mec_csv_export_columns', $columns);
238
239 $reg_fields = $this->main->get_reg_fields($main_event_id);
240 foreach($reg_fields as $reg_field_key=>$reg_field)
241 {
242 // Placeholder Keys
243 if(!is_numeric($reg_field_key)) continue;
244
245 $type = isset($reg_field['type']) ? $reg_field['type'] : '';
246 $label = isset($reg_field['label']) ? __($reg_field['label'], 'mec') : '';
247
248 if(trim($label) == '' or $type == 'name' or $type == 'mec_email') continue;
249 if($type == 'agreement') $label = sprintf($label, get_the_title($reg_field['page']));
250
251 $columns[] = $label;
252 }
253
254 $columns[] = 'Attachments';
255 $output = fopen('php://output', 'w');
256 fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
257 fputcsv($output, $columns);
258
259 // MEC User
260 $u = $this->getUser();
261
262 foreach($post_ids as $post_id)
263 {
264 $post_id = (int) $post_id;
265
266 $event_id = get_post_meta($post_id, 'mec_event_id', true);
267 $transaction_id = get_post_meta($post_id, 'mec_transaction_id', true);
268 $order_time = get_post_meta($post_id, 'mec_booking_time', true);
269 $tickets = get_post_meta($event_id, 'mec_tickets', true);
270
271 $attendees = get_post_meta($post_id, 'mec_attendees', true);
272 if(!is_array($attendees) or (is_array($attendees) and !count($attendees))) $attendees = array(get_post_meta($post_id, 'mec_attendee', true));
273
274 $price = get_post_meta($post_id, 'mec_price', true);
275 $booker = $u->booking($post_id);
276
277 $confirmed = $this->main->get_confirmation_label(get_post_meta($post_id, 'mec_confirmed', true));
278 $verified = $this->main->get_verification_label(get_post_meta($post_id, 'mec_verified', true));
279
280 $attachments = '';
281 if(isset($attendees['attachments']))
282 {
283 foreach($attendees['attachments'] as $attachment)
284 {
285 $attachments .= @$attachment['url'] . "\n";
286 }
287 }
288
289 $book = $this->getBook();
290 $ticket_variations = $this->main->ticket_variations($post_id);
291 $transaction = $book->get_transaction($transaction_id);
292
293 $counter = 0;
294 foreach($attendees as $key => $attendee)
295 {
296 if($key === 'attachments') continue;
297 if(isset($attendee[0]['MEC_TYPE_OF_DATA'])) continue;
298
299 $ticket_variations_output = '';
300 if(isset($transaction['tickets']) and is_array($transaction['tickets']) and isset($transaction['tickets'][$counter]) and isset($transaction['tickets'][$counter]['variations']))
301 {
302 foreach($transaction['tickets'][$counter]['variations'] as $variation_id => $variation_count)
303 {
304 if($variation_count > 0) $ticket_variations_output .= $ticket_variations[$variation_id]['title'].": ( ".$variation_count.' )'.", ";
305 }
306 }
307
308 $ticket_id = isset($attendee['id']) ? $attendee['id'] : get_post_meta($post_id, 'mec_ticket_id', true);
309 $booking = array($post_id, get_the_title($event_id), get_the_date('', $post_id), $order_time, (isset($tickets[$ticket_id]['name']) ? $tickets[$ticket_id]['name'] : __('Unknown', 'mec')), $transaction_id, $this->main->render_price(($price ? $price : 0)), (isset($attendee['name']) ? $attendee['name'] : (isset($booker->first_name) ? trim($booker->first_name.' '.$booker->last_name) : '')), (isset($attendee['email']) ? $attendee['email'] : @$booker->user_email), trim($ticket_variations_output, ', '), $confirmed, $verified);
310 $booking = apply_filters('mec_csv_export_booking', $booking, $post_id, $event_id);
311
312 $reg_form = isset($attendee['reg']) ? $attendee['reg'] : array();
313 foreach($reg_fields as $field_id=>$reg_field)
314 {
315 // Placeholder Keys
316 if(!is_numeric($field_id)) continue;
317
318 $type = isset($reg_field['type']) ? $reg_field['type'] : '';
319 $label = isset($reg_field['label']) ? __($reg_field['label'], 'mec') : '';
320 if(trim($label) == '' or $type == 'name' or $type == 'mec_email') continue;
321
322 $booking[] = isset($reg_form[$field_id]) ? ((is_string($reg_form[$field_id]) and trim($reg_form[$field_id])) ? $reg_form[$field_id] : (is_array($reg_form[$field_id]) ? implode(' | ', $reg_form[$field_id]) : '---')) : '';
323 }
324
325 if($attachments)
326 {
327 $booking[] = $attachments;
328 $attachments = '';
329 }
330
331 fputcsv($output, $booking);
332 $counter++;
333 }
334 }
335
336 die(json_encode(array('name' => md5(time().mt_rand(100, 999)), 'ex' => "data:text/csv; charset=utf-8;base64,".base64_encode(ob_get_clean()))));
337 }
338
339 public function fes_upload()
340 {
341 // Check if our nonce is set.
342 if(!isset($_POST['_wpnonce'])) $this->main->response(array('success'=>0, 'code'=>'NONCE_MISSING'));
343
344 // Verify that the nonce is valid.
345 if(!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'mec_fes_upload_featured_image')) $this->main->response(array('success'=>0, 'code'=>'NONCE_IS_INVALID'));
346
347 // Include the function
348 if(!function_exists('wp_handle_upload')) require_once ABSPATH.'wp-admin/includes/file.php';
349
350 $uploaded_file = isset($_FILES['file']) ? $_FILES['file'] : NULL;
351
352 // No file
353 if(!$uploaded_file) $this->main->response(array('success'=>0, 'code'=>'NO_FILE', 'message'=>esc_html__('Please upload an image.', 'mec')));
354
355 $allowed = array('gif', 'jpeg', 'jpg', 'png');
356
357 $ex = explode('.', $uploaded_file['name']);
358 $extension = end($ex);
359
360 // Invalid Extension
361 if(!in_array($extension, $allowed)) $this->main->response(array('success'=>0, 'code'=>'INVALID_EXTENSION', 'message'=>sprintf(esc_html__('image extension is invalid. You can upload %s images.', 'mec'), implode(', ', $allowed))));
362
363 // Maximum File Size
364 $max_file_size = isset($this->settings['fes_max_file_size']) ? (int) ($this->settings['fes_max_file_size'] * 1000) : (5000 * 1000);
365
366 // Invalid Size
367 if($uploaded_file['size'] > $max_file_size) $this->main->response(array('success'=>0, 'code'=>'IMAGE_IS_TOO_BIG', 'message'=>sprintf(esc_html__('Image is too big. Maximum size is %s KB.', 'mec'), ($max_file_size / 1000))));
368
369 $movefile = wp_handle_upload($uploaded_file, array('test_form'=>false));
370
371 $success = 0;
372 $data = array();
373
374 if($movefile and !isset($movefile['error']))
375 {
376 $success = 1;
377 $message = __('Image uploaded!', 'mec');
378
379 $data['url'] = $movefile['url'];
380 }
381 else
382 {
383 $message = $movefile['error'];
384 }
385
386 $this->main->response(array('success'=>$success, 'message'=>$message, 'data'=>$data));
387 }
388
389 public function fes_form()
390 {
391 // Check if our nonce is set.
392 if(!isset($_POST['_wpnonce'])) $this->main->response(array('success'=>0, 'code'=>'NONCE_MISSING'));
393
394 // Verify that the nonce is valid.
395 if(!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'mec_fes_form')) $this->main->response(array('success'=>0, 'code'=>'NONCE_IS_INVALID'));
396
397 $mec = isset($_POST['mec']) ? $_POST['mec'] : array();
398
399 // Google recaptcha
400 if($this->main->get_recaptcha_status('fes'))
401 {
402 $g_recaptcha_response = isset($_POST['g-recaptcha-response']) ? sanitize_text_field($_POST['g-recaptcha-response']) : NULL;
403 if(!$this->main->get_recaptcha_response($g_recaptcha_response)) $this->main->response(array('success'=>0, 'message'=>__('Invalid Captcha! Please try again.', 'mec'), 'code'=>'CAPTCHA_IS_INVALID'));
404 }
405
406 $post_id = isset($mec['post_id']) ? sanitize_text_field($mec['post_id']) : -1;
407
408 $start_date = (isset($mec['date']['start']['date']) and trim($mec['date']['start']['date'])) ? $this->main->standardize_format($mec['date']['start']['date']) : date('Y-m-d');
409 $end_date = (isset($mec['date']['end']['date']) and trim($mec['date']['end']['date'])) ? $this->main->standardize_format($mec['date']['end']['date']) : date('Y-m-d');
410
411 $event = $this->db->select("SELECT * FROM `#__mec_events` WHERE `post_id` = {$post_id}", 'loadAssoc');
412 if(!is_array($event)) $event = array();
413
414 $booking_date_update = false;
415 $past_start_date = '';
416 $past_end_date = '';
417
418 if(count($event))
419 {
420 $past_start_date = (isset($event['start']) and trim($event['start'])) ? $event['start'] : '';
421 $past_end_date = (isset($event['end']) and trim($event['end'])) ? $event['end'] : '';
422
423 if(trim($start_date) != trim($past_start_date) or trim($end_date) != trim($past_end_date)) $booking_date_update = true;
424 }
425
426 $post_title = isset($mec['title']) ? sanitize_text_field($mec['title']) : '';
427 $post_content = isset($mec['content']) ? $mec['content'] : '';
428 $post_excerpt = isset($mec['excerpt']) ? $mec['excerpt'] : '';
429 $post_tags = isset($mec['tags']) ? sanitize_text_field($mec['tags']) : '';
430 $post_categories = isset($mec['categories']) ? $mec['categories'] : array();
431 $post_speakers = isset($mec['speakers']) ? $mec['speakers'] : array();
432 $post_labels = isset($mec['labels']) ? $mec['labels'] : array();
433 $featured_image = isset($mec['featured_image']) ? sanitize_text_field($mec['featured_image']) : '';
434
435 if(!trim($post_title)) $this->main->response(array('success'=>0, 'message'=>__('Please fill event title field!', 'mec'), 'code'=>'TITLE_IS_EMPTY'));
436
437 // Post Status
438 $status = 'pending';
439 if(current_user_can('publish_posts')) $status = 'publish';
440
441 $method = 'updated';
442
443 // Create new event
444 if($post_id == -1)
445 {
446 $post = array('post_title'=>$post_title, 'post_content'=>$post_content, 'post_excerpt'=>$post_excerpt, 'post_type'=>$this->PT, 'post_status'=>$status);
447 $post_id = wp_insert_post($post);
448
449 $method = 'added';
450 }
451
452 wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title, 'post_content'=>$post_content, 'post_excerpt'=>$post_excerpt,));
453
454 // Categories
455 $categories = array();
456 foreach($post_categories as $post_category=>$value) $categories[] = (int) $post_category;
457
458 wp_set_post_terms($post_id, $categories, 'mec_category');
459
460 // Speakers
461 if(isset($this->settings['speakers_status']) and $this->settings['speakers_status'])
462 {
463 $speakers = array();
464 foreach($post_speakers as $post_speaker=>$value) $speakers[] = (int) $post_speaker;
465
466 wp_set_post_terms($post_id, $speakers, 'mec_speaker');
467 }
468
469 // Labels
470 $labels = array();
471 foreach($post_labels as $post_label=>$value) $labels[] = (int) $post_label;
472
473 wp_set_post_terms($post_id, $labels, 'mec_label');
474 do_action('mec_label_change_to_radio', $labels, $post_labels, $post_id);
475
476 // Color
477 $color = isset($mec['color']) ? sanitize_text_field(trim($mec['color'], '# ')) : '';
478 update_post_meta($post_id, 'mec_color', $color);
479
480 // Tags
481 wp_set_post_tags($post_id, $post_tags);
482
483 // Featured Image
484 if(trim($featured_image)) $this->main->set_featured_image($featured_image, $post_id);
485 else delete_post_thumbnail($post_id);
486
487 $read_more = isset($mec['read_more']) ? sanitize_text_field($mec['read_more']) : '';
488 $more_info = isset($mec['more_info']) ? (strpos($mec['more_info'], 'http') === false ? 'http://'.sanitize_text_field($mec['more_info']) : sanitize_text_field($mec['more_info'])) : '';
489 $more_info_title = isset($mec['more_info_title']) ? sanitize_text_field($mec['more_info_title']) : '';
490 $more_info_target = isset($mec['more_info_target']) ? sanitize_text_field($mec['more_info_target']) : '';
491 $cost = isset($mec['cost']) ? sanitize_text_field($mec['cost']) : '';
492 $note = isset($mec['note']) ? sanitize_text_field($mec['note']) : '';
493
494 update_post_meta($post_id, 'mec_read_more', $read_more);
495 update_post_meta($post_id, 'mec_more_info', $more_info);
496 update_post_meta($post_id, 'mec_more_info_title', $more_info_title);
497 update_post_meta($post_id, 'mec_more_info_target', $more_info_target);
498 update_post_meta($post_id, 'mec_cost', $cost);
499 update_post_meta($post_id, 'mec_note', $note);
500
501 // Guest Name and Email
502 $fes_guest_email = isset($mec['fes_guest_email']) ? sanitize_email($mec['fes_guest_email']) : '';
503 $fes_guest_name = isset($mec['fes_guest_name']) ? sanitize_text_field($mec['fes_guest_name']) : '';
504
505 update_post_meta($post_id, 'fes_guest_email', $fes_guest_email);
506 update_post_meta($post_id, 'fes_guest_name', $fes_guest_name);
507
508 // Location
509 $location_id = isset($mec['location_id']) ? sanitize_text_field($mec['location_id']) : 1;
510
511 // Selected a saved location
512 if($location_id)
513 {
514 // Set term to the post
515 wp_set_object_terms($post_id, (int) $location_id, 'mec_location');
516 }
517 else
518 {
519 $address = (isset($mec['location']['address']) and trim($mec['location']['address'])) ? sanitize_text_field($mec['location']['address']) : '';
520 $name = (isset($mec['location']['name']) and trim($mec['location']['name'])) ? sanitize_text_field($mec['location']['name']) : (trim($address) ? $address : 'Location Name');
521
522 $term = get_term_by('name', $name, 'mec_location');
523
524 // Term already exists
525 if(is_object($term) and isset($term->term_id))
526 {
527 // Set term to the post
528 wp_set_object_terms($post_id, (int) $term->term_id, 'mec_location');
529 }
530 else
531 {
532 $term = wp_insert_term($name, 'mec_location');
533
534 $location_id = $term['term_id'];
535 if($location_id)
536 {
537 // Set term to the post
538 wp_set_object_terms($post_id, (int) $location_id, 'mec_location');
539
540 $latitude = (isset($mec['location']['latitude']) and trim($mec['location']['latitude'])) ? sanitize_text_field($mec['location']['latitude']) : 0;
541 $longitude = (isset($mec['location']['longitude']) and trim($mec['location']['longitude'])) ? sanitize_text_field($mec['location']['longitude']) : 0;
542 $url = (isset($mec['location']['url']) and trim($mec['location']['url'])) ? esc_url($mec['location']['url']) : '';
543 $thumbnail = (isset($mec['location']['thumbnail']) and trim($mec['location']['thumbnail'])) ? sanitize_text_field($mec['location']['thumbnail']) : '';
544
545 if(!trim($latitude) or !trim($longitude))
546 {
547 $geo_point = $this->main->get_lat_lng($address);
548
549 $latitude = $geo_point[0];
550 $longitude = $geo_point[1];
551 }
552
553 update_term_meta($location_id, 'address', $address);
554 update_term_meta($location_id, 'latitude', $latitude);
555 update_term_meta($location_id, 'longitude', $longitude);
556 update_term_meta($location_id, 'url', $url);
557 update_term_meta($location_id, 'thumbnail', $thumbnail);
558 }
559 else $location_id = 1;
560 }
561 }
562
563 update_post_meta($post_id, 'mec_location_id', $location_id);
564
565 $dont_show_map = isset($mec['dont_show_map']) ? sanitize_text_field($mec['dont_show_map']) : 0;
566 update_post_meta($post_id, 'mec_dont_show_map', $dont_show_map);
567
568 // Organizer
569 $organizer_id = isset($mec['organizer_id']) ? sanitize_text_field($mec['organizer_id']) : 1;
570
571 // Selected a saved organizer
572 if(isset($organizer_id) and $organizer_id)
573 {
574 // Set term to the post
575 wp_set_object_terms($post_id, (int) $organizer_id, 'mec_organizer');
576 }
577 else
578 {
579 $name = (isset($mec['organizer']['name']) and trim($mec['organizer']['name'])) ? sanitize_text_field($mec['organizer']['name']) : 'Organizer Name';
580
581 $term = get_term_by('name', $name, 'mec_organizer');
582
583 // Term already exists
584 if(is_object($term) and isset($term->term_id))
585 {
586 // Set term to the post
587 wp_set_object_terms($post_id, (int) $term->term_id, 'mec_organizer');
588 }
589 else
590 {
591 $term = wp_insert_term($name, 'mec_organizer');
592
593 $organizer_id = $term['term_id'];
594 if($organizer_id)
595 {
596 // Set term to the post
597 wp_set_object_terms($post_id, (int) $organizer_id, 'mec_organizer');
598
599 $tel = (isset($mec['organizer']['tel']) and trim($mec['organizer']['tel'])) ? sanitize_text_field($mec['organizer']['tel']) : '';
600 $email = (isset($mec['organizer']['email']) and trim($mec['organizer']['email'])) ? sanitize_text_field($mec['organizer']['email']) : '';
601 $url = (isset($mec['organizer']['url']) and trim($mec['organizer']['url'])) ? (strpos($mec['organizer']['url'], 'http') === false ? 'http://'.sanitize_text_field($mec['organizer']['url']) : sanitize_text_field($mec['organizer']['url'])) : '';
602 $thumbnail = (isset($mec['organizer']['thumbnail']) and trim($mec['organizer']['thumbnail'])) ? sanitize_text_field($mec['organizer']['thumbnail']) : '';
603
604 update_term_meta($organizer_id, 'tel', $tel);
605 update_term_meta($organizer_id, 'email', $email);
606 update_term_meta($organizer_id, 'url', $url);
607 update_term_meta($organizer_id, 'thumbnail', $thumbnail);
608 }
609 else $organizer_id = 1;
610 }
611 }
612
613 update_post_meta($post_id, 'mec_organizer_id', $organizer_id);
614
615 // Additional Organizers
616 $additional_organizer_ids = isset($mec['additional_organizer_ids']) ? $mec['additional_organizer_ids'] : array();
617
618 foreach($additional_organizer_ids as $additional_organizer_id) wp_set_object_terms($post_id, (int) $additional_organizer_id, 'mec_organizer', true);
619 update_post_meta($post_id, 'mec_additional_organizer_ids', $additional_organizer_ids);
620
621 // Additional locations
622 $additional_location_ids = isset($mec['additional_location_ids']) ? $mec['additional_location_ids'] : array();
623
624 foreach ($additional_location_ids as $additional_location_id) {
625 wp_set_object_terms($post_id, (int)$additional_location_id, 'mec_location', true);
626 }
627 update_post_meta($post_id, 'mec_additional_location_ids', $additional_location_ids);
628
629 // Date Options
630 $date = isset($mec['date']) ? $mec['date'] : array();
631
632 $start_date = date('Y-m-d', strtotime($start_date));
633
634 // Set the start date
635 $date['start']['date'] = $start_date;
636
637 $start_time_hour = isset($date['start']) ? $date['start']['hour'] : '8';
638 $start_time_minutes = isset($date['start']) ? $date['start']['minutes'] : '00';
639 $start_time_ampm = (isset($date['start']) and isset($date['start']['ampm'])) ? $date['start']['ampm'] : 'AM';
640
641 $end_date = date('Y-m-d', strtotime($end_date));
642
643 // Fix end_date if it's smaller than start_date
644 if(strtotime($end_date) < strtotime($start_date)) $end_date = $start_date;
645
646 // Set the end date
647 $date['end']['date'] = $end_date;
648
649 $end_time_hour = isset($date['end']) ? $date['end']['hour'] : '6';
650 $end_time_minutes = isset($date['end']) ? $date['end']['minutes'] : '00';
651 $end_time_ampm = (isset($date['end']) and isset($date['end']['ampm'])) ? $date['end']['ampm'] : 'PM';
652
653 // If 24 hours format is enabled then convert it back to 12 hours
654 if(isset($this->settings['time_format']) and $this->settings['time_format'] == 24)
655 {
656 if($start_time_hour < 12) $start_time_ampm = 'AM';
657 elseif($start_time_hour == 12) $start_time_ampm = 'PM';
658 elseif($start_time_hour > 12)
659 {
660 $start_time_hour -= 12;
661 $start_time_ampm = 'PM';
662 }
663 elseif($start_time_hour == 0)
664 {
665 $start_time_hour = 12;
666 $start_time_ampm = 'AM';
667 }
668
669 if($end_time_hour < 12) $end_time_ampm = 'AM';
670 elseif($end_time_hour == 12) $end_time_ampm = 'PM';
671 elseif($end_time_hour > 12)
672 {
673 $end_time_hour -= 12;
674 $end_time_ampm = 'PM';
675 }
676 elseif($end_time_hour == 0)
677 {
678 $end_time_hour = 12;
679 $end_time_ampm = 'AM';
680 }
681
682 // Set converted values to date array
683 $date['start']['hour'] = $start_time_hour;
684 $date['start']['ampm'] = $start_time_ampm;
685
686 $date['end']['hour'] = $end_time_hour;
687 $date['end']['ampm'] = $end_time_ampm;
688 }
689
690 $allday = isset($date['allday']) ? 1 : 0;
691 $one_occurrence = isset($date['one_occurrence']) ? 1 : 0;
692 $hide_time = isset($date['hide_time']) ? 1 : 0;
693 $hide_end_time = isset($date['hide_end_time']) ? 1 : 0;
694 $comment = isset($date['comment']) ? $date['comment'] : '';
695 $timezone = (isset($mec['timezone']) and trim($mec['timezone']) != '') ? sanitize_text_field($mec['timezone']) : 'global';
696 $countdown_method = (isset($mec['countdown_method']) and trim($mec['countdown_method']) != '') ? sanitize_text_field($mec['countdown_method']) : 'global';
697
698 // Set start time and end time if event is all day
699 if($allday == 1)
700 {
701 $start_time_hour = '8';
702 $start_time_minutes = '00';
703 $start_time_ampm = 'AM';
704
705 $end_time_hour = '6';
706 $end_time_minutes = '00';
707 $end_time_ampm = 'PM';
708 }
709
710 $day_start_seconds = $this->main->time_to_seconds($this->main->to_24hours($start_time_hour, $start_time_ampm), $start_time_minutes);
711 $day_end_seconds = $this->main->time_to_seconds($this->main->to_24hours($end_time_hour, $end_time_ampm), $end_time_minutes);
712
713 update_post_meta($post_id, 'mec_start_date', $start_date);
714 update_post_meta($post_id, 'mec_start_time_hour', $start_time_hour);
715 update_post_meta($post_id, 'mec_start_time_minutes', $start_time_minutes);
716 update_post_meta($post_id, 'mec_start_time_ampm', $start_time_ampm);
717 update_post_meta($post_id, 'mec_start_day_seconds', $day_start_seconds);
718
719 update_post_meta($post_id, 'mec_end_date', $end_date);
720 update_post_meta($post_id, 'mec_end_time_hour', $end_time_hour);
721 update_post_meta($post_id, 'mec_end_time_minutes', $end_time_minutes);
722 update_post_meta($post_id, 'mec_end_time_ampm', $end_time_ampm);
723 update_post_meta($post_id, 'mec_end_day_seconds', $day_end_seconds);
724
725 update_post_meta($post_id, 'mec_date', $date);
726
727 // Repeat Options
728 $repeat = isset($date['repeat']) ? $date['repeat'] : array();
729 $certain_weekdays = isset($repeat['certain_weekdays']) ? $repeat['certain_weekdays'] : array();
730
731 $repeat_status = isset($repeat['status']) ? 1 : 0;
732 $repeat_type = ($repeat_status and isset($repeat['type'])) ? $repeat['type'] : '';
733
734 $repeat_interval = ($repeat_status and isset($repeat['interval']) and trim($repeat['interval'])) ? $repeat['interval'] : 1;
735
736 // Advanced Repeat
737 $advanced = isset( $repeat['advanced'] ) ? sanitize_text_field($repeat['advanced']) : '';
738
739 if(!is_numeric($repeat_interval)) $repeat_interval = NULL;
740
741 if($repeat_type == 'weekly') $interval_multiply = 7;
742 else $interval_multiply = 1;
743
744 // Reset certain weekdays if repeat type is not set to certain weekdays
745 if($repeat_type != 'certain_weekdays') $certain_weekdays = array();
746
747 if(!is_null($repeat_interval)) $repeat_interval = $repeat_interval*$interval_multiply;
748
749 // String To Array
750 if($repeat_type == 'advanced' and trim($advanced)) $advanced = explode('-', $advanced);
751 else $advanced = array();
752
753 $repeat_end = ($repeat_status and isset($repeat['end'])) ? $repeat['end'] : '';
754 $repeat_end_at_occurrences = ($repeat_status and isset($repeat['end_at_occurrences'])) ? ($repeat['end_at_occurrences']-1) : '';
755 $repeat_end_at_date = ($repeat_status and isset($repeat['end_at_date'])) ? $this->main->standardize_format( $repeat['end_at_date'] ) : '';
756
757 update_post_meta($post_id, 'mec_date', $date);
758 update_post_meta($post_id, 'mec_repeat', $repeat);
759 update_post_meta($post_id, 'mec_certain_weekdays', $certain_weekdays);
760 update_post_meta($post_id, 'mec_allday', $allday);
761 update_post_meta($post_id, 'one_occurrence', $one_occurrence);
762 update_post_meta($post_id, 'mec_hide_time', $hide_time);
763 update_post_meta($post_id, 'mec_hide_end_time', $hide_end_time);
764 update_post_meta($post_id, 'mec_comment', $comment);
765 update_post_meta($post_id, 'mec_timezone', $timezone);
766 update_post_meta($post_id, 'mec_countdown_method', $countdown_method);
767 update_post_meta($post_id, 'mec_repeat_status', $repeat_status);
768 update_post_meta($post_id, 'mec_repeat_type', $repeat_type);
769 update_post_meta($post_id, 'mec_repeat_interval', $repeat_interval);
770 update_post_meta($post_id, 'mec_repeat_end', $repeat_end);
771 update_post_meta($post_id, 'mec_repeat_end_at_occurrences', $repeat_end_at_occurrences);
772 update_post_meta($post_id, 'mec_repeat_end_at_date', $repeat_end_at_date);
773 update_post_meta($post_id, 'mec_advanced_days', $advanced);
774
775 // Creating $event array for inserting in mec_events table
776 $event = array('post_id'=>$post_id, 'start'=>$start_date, 'repeat'=>$repeat_status, 'rinterval'=>(!in_array($repeat_type, array('daily', 'weekly')) ? NULL : $repeat_interval), 'time_start'=>$day_start_seconds, 'time_end'=>$day_end_seconds);
777
778 $year = NULL;
779 $month = NULL;
780 $day = NULL;
781 $week = NULL;
782 $weekday = NULL;
783 $weekdays = NULL;
784
785 // MEC weekdays
786 $mec_weekdays = $this->main->get_weekdays();
787
788 // MEC weekends
789 $mec_weekends = $this->main->get_weekends();
790
791 $plus_date = null;
792 if($repeat_type == 'daily')
793 {
794 $plus_date = '+'.$repeat_end_at_occurrences*$repeat_interval.' Days';
795 }
796 elseif($repeat_type == 'weekly')
797 {
798 $plus_date = '+'.$repeat_end_at_occurrences*($repeat_interval).' Days';
799 }
800 elseif($repeat_type == 'weekday')
801 {
802 $repeat_interval = 1;
803 $plus_date = '+'.$repeat_end_at_occurrences*$repeat_interval.' Weekdays';
804
805 $weekdays = ','.implode(',', $mec_weekdays).',';
806 }
807 elseif($repeat_type == 'weekend')
808 {
809 $repeat_interval = 1;
810 $plus_date = '+'.round($repeat_end_at_occurrences/2)*($repeat_interval*7).' Days';
811
812 $weekdays = ','.implode(',', $mec_weekends).',';
813 }
814 elseif($repeat_type == 'certain_weekdays')
815 {
816 $repeat_interval = 1;
817 $plus_date = '+' . ceil(($repeat_end_at_occurrences * $repeat_interval) * (7/count($certain_weekdays))) . ' days';
818
819 $weekdays = ','.implode(',', $certain_weekdays).',';
820 }
821 elseif($repeat_type == 'monthly')
822 {
823 $plus_date = '+'.$repeat_end_at_occurrences*$repeat_interval.' Months';
824
825 $year = '*';
826 $month = '*';
827
828 $s = $start_date;
829 $e = $end_date;
830
831 $_days = array();
832 while(strtotime($s) <= strtotime($e))
833 {
834 $_days[] = date('d', strtotime($s));
835 $s = date('Y-m-d', strtotime('+1 Day', strtotime($s)));
836 }
837
838 $day = ','.implode(',', array_unique($_days)).',';
839
840 $week = '*';
841 $weekday = '*';
842 }
843 elseif($repeat_type == 'yearly')
844 {
845 $plus_date = '+'.$repeat_end_at_occurrences*$repeat_interval.' Years';
846
847 $year = '*';
848
849 $s = $start_date;
850 $e = $end_date;
851
852 $_months = array();
853 $_days = array();
854 while(strtotime($s) <= strtotime($e))
855 {
856 $_months[] = date('m', strtotime($s));
857 $_days[] = date('d', strtotime($s));
858
859 $s = date('Y-m-d', strtotime('+1 Day', strtotime($s)));
860 }
861
862 $month = ','.implode(',', array_unique($_months)).',';
863 $day = ','.implode(',', array_unique($_days)).',';
864
865 $week = '*';
866 $weekday = '*';
867 }
868 elseif($repeat_type == "advanced")
869 {
870 // Render class object
871 $this->render = $this->getRender();
872
873 // Get finish date
874 $event_info = array('start' => $date['start'], 'end' => $date['end']);
875 $dates = $this->render->generate_advanced_days($advanced, $event_info, $repeat_end_at_occurrences +1, date( 'Y-m-d', current_time( 'timestamp', 0 )), 'events');
876
877 $period_date = $this->main->date_diff($start_date, end($dates)['end']['date']);
878
879 $plus_date = '+' . $period_date->days . ' Days';
880 }
881
882 // "In Days" and "Not In Days"
883 $in_days_arr = (isset($mec['in_days']) and is_array($mec['in_days']) and count($mec['in_days'])) ? array_unique($mec['in_days']) : array();
884 $not_in_days_arr = (isset($mec['not_in_days']) and is_array($mec['not_in_days']) and count($mec['not_in_days'])) ? array_unique($mec['not_in_days']) : array();
885
886 $in_days = '';
887 if(count($in_days_arr))
888 {
889 if(isset($in_days_arr[':i:'])) unset($in_days_arr[':i:']);
890
891 $in_days_arr = array_map(function($value)
892 {
893 $ex = explode(':', $value);
894
895 $in_days_times = '';
896 if(isset($ex[2]) and isset($ex[3]))
897 {
898 $in_days_start_time = $ex[2];
899 $in_days_end_time = $ex[3];
900
901 // If 24 hours format is enabled then convert it back to 12 hours
902 if(isset($this->settings['time_format']) and $this->settings['time_format'] == 24)
903 {
904 $ex_start_time = explode('-', $in_days_start_time);
905 $ex_end_time = explode('-', $in_days_end_time);
906
907 $in_days_start_hour = $ex_start_time[0];
908 $in_days_start_minutes = $ex_start_time[1];
909 $in_days_start_ampm = $ex_start_time[2];
910
911 $in_days_end_hour = $ex_end_time[0];
912 $in_days_end_minutes = $ex_end_time[1];
913 $in_days_end_ampm = $ex_end_time[2];
914
915 if(trim($in_days_start_ampm) == '')
916 {
917 if($in_days_start_hour < 12) $in_days_start_ampm = 'AM';
918 elseif($in_days_start_hour == 12) $in_days_start_ampm = 'PM';
919 elseif($in_days_start_hour > 12)
920 {
921 $in_days_start_hour -= 12;
922 $in_days_start_ampm = 'PM';
923 }
924 elseif($in_days_start_hour == 0)
925 {
926 $in_days_start_hour = 12;
927 $in_days_start_ampm = 'AM';
928 }
929 }
930
931 if(trim($in_days_end_ampm) == '')
932 {
933 if($in_days_end_hour < 12) $in_days_end_ampm = 'AM';
934 elseif($in_days_end_hour == 12) $in_days_end_ampm = 'PM';
935 elseif($in_days_end_hour > 12)
936 {
937 $in_days_end_hour -= 12;
938 $in_days_end_ampm = 'PM';
939 }
940 elseif($in_days_end_hour == 0)
941 {
942 $in_days_end_hour = 12;
943 $in_days_end_ampm = 'AM';
944 }
945 }
946
947 if(strlen($in_days_start_hour) == 1) $in_days_start_hour = '0'.$in_days_start_hour;
948 if(strlen($in_days_start_minutes) == 1) $in_days_start_minutes = '0'.$in_days_start_minutes;
949
950 if(strlen($in_days_end_hour) == 1) $in_days_end_hour = '0'.$in_days_end_hour;
951 if(strlen($in_days_end_minutes) == 1) $in_days_end_minutes = '0'.$in_days_end_minutes;
952
953 $in_days_start_time = $in_days_start_hour.'-'.$in_days_start_minutes.'-'.$in_days_start_ampm;
954 $in_days_end_time = $in_days_end_hour.'-'.$in_days_end_minutes.'-'.$in_days_end_ampm;
955 }
956
957 $in_days_times = ':'.$in_days_start_time.':'.$in_days_end_time;
958 }
959
960 return $this->main->standardize_format($ex[0]) . ':' . $this->main->standardize_format($ex[1]).$in_days_times;
961 }, $in_days_arr);
962
963 usort($in_days_arr, function($a, $b)
964 {
965 $ex_a = explode(':', $a);
966 $ex_b = explode(':', $b);
967
968 $date_a = $ex_a[0];
969 $date_b = $ex_b[0];
970
971 $in_day_a_time_label = '';
972 if(isset($ex_a[2]))
973 {
974 $in_day_a_time = $ex_a[2];
975 $pos = strpos($in_day_a_time, '-');
976 if($pos !== false) $in_day_a_time_label = substr_replace($in_day_a_time, ':', $pos, 1);
977
978 $in_day_a_time_label = str_replace('-', ' ', $in_day_a_time_label);
979 }
980
981 $in_day_b_time_label = '';
982 if(isset($ex_b[2]))
983 {
984 $in_day_b_time = $ex_b[2];
985 $pos = strpos($in_day_b_time, '-');
986 if($pos !== false) $in_day_b_time_label = substr_replace($in_day_b_time, ':', $pos, 1);
987
988 $in_day_b_time_label = str_replace('-', ' ', $in_day_b_time_label);
989 }
990
991 return strtotime(trim($date_a.' '.$in_day_a_time_label)) - strtotime(trim($date_b.' '.$in_day_b_time_label));
992 });
993
994 // Don't allow multiple occurrences per day in Lite version
995 if(!$this->getPRO())
996 {
997 $in_days_unique = array();
998 foreach($in_days_arr as $key => $in_day_arr)
999 {
1000 $ex = explode(':', $in_day_arr);
1001 $in_days_unique_key = $ex[0].'-'.$ex[1];
1002
1003 if(isset($in_days_unique[$in_days_unique_key])) unset($in_days_arr[$key]);
1004 $in_days_unique[$in_days_unique_key] = 1;
1005 }
1006 }
1007
1008 if(!isset($in_days_arr[':i:'])) $in_days_arr[':i:'] = ':val:';
1009 foreach($in_days_arr as $key => $in_day_arr)
1010 {
1011 if(is_numeric($key)) $in_days .= $in_day_arr . ',';
1012 }
1013 }
1014
1015 $not_in_days = '';
1016 if(count($not_in_days_arr))
1017 {
1018 foreach($not_in_days_arr as $key => $not_in_day_arr)
1019 {
1020 if(is_numeric($key)) $not_in_days .= $this->main->standardize_format($not_in_day_arr) . ',';
1021 }
1022 }
1023
1024 $in_days = trim($in_days, ', ');
1025 $not_in_days = trim($not_in_days, ', ');
1026
1027 update_post_meta($post_id, 'mec_in_days', $in_days);
1028 update_post_meta($post_id, 'mec_not_in_days', $not_in_days);
1029
1030 // Repeat End Date
1031 if($repeat_end == 'never') $repeat_end_date = '0000-00-00';
1032 elseif($repeat_end == 'date') $repeat_end_date = $repeat_end_at_date;
1033 elseif($repeat_end == 'occurrences')
1034 {
1035 if($plus_date) $repeat_end_date = date('Y-m-d', strtotime($plus_date, strtotime($end_date)));
1036 else $repeat_end_date = '0000-00-00';
1037 }
1038 else $repeat_end_date = '0000-00-00';
1039
1040 // If event is not repeating then set the end date of event correctly
1041 if(!$repeat_status or $repeat_type == 'custom_days') $repeat_end_date = $end_date;
1042
1043 // Add parameters to the $event
1044 $event['end'] = $repeat_end_date;
1045 $event['year'] = $year;
1046 $event['month'] = $month;
1047 $event['day'] = $day;
1048 $event['week'] = $week;
1049 $event['weekday'] = $weekday;
1050 $event['weekdays'] = $weekdays;
1051 $event['days'] = $in_days;
1052 $event['not_in_days'] = $not_in_days;
1053
1054 // Update MEC Events Table
1055 $mec_event_id = $this->db->select("SELECT `id` FROM `#__mec_events` WHERE `post_id`='$post_id'", 'loadResult');
1056
1057 if(!$mec_event_id)
1058 {
1059 $q1 = "";
1060 $q2 = "";
1061
1062 foreach($event as $key=>$value)
1063 {
1064 $q1 .= "`$key`,";
1065
1066 if(is_null($value)) $q2 .= "NULL,";
1067 else $q2 .= "'$value',";
1068 }
1069
1070 $this->db->q("INSERT INTO `#__mec_events` (".trim($q1, ', ').") VALUES (".trim($q2, ', ').")", 'INSERT');
1071 }
1072 else
1073 {
1074 $q = "";
1075
1076 foreach($event as $key=>$value)
1077 {
1078 if(is_null($value)) $q .= "`$key`=NULL,";
1079 else $q .= "`$key`='$value',";
1080 }
1081
1082 $this->db->q("UPDATE `#__mec_events` SET ".trim($q, ', ')." WHERE `id`='$mec_event_id'");
1083 }
1084
1085 // Update Schedule
1086 $schedule = $this->getSchedule();
1087 $schedule->reschedule($post_id, $schedule->get_reschedule_maximum($repeat_type));
1088
1089 // Hourly Schedule Options
1090 $raw_hourly_schedules = isset($mec['hourly_schedules']) ? $mec['hourly_schedules'] : array();
1091 unset($raw_hourly_schedules[':d:']);
1092
1093 $hourly_schedules = array();
1094 foreach($raw_hourly_schedules as $raw_hourly_schedule)
1095 {
1096 unset($raw_hourly_schedule['schedules'][':i:']);
1097 $hourly_schedules[] = $raw_hourly_schedule;
1098 }
1099
1100 update_post_meta($post_id, 'mec_hourly_schedules', $hourly_schedules);
1101
1102 // Booking and Ticket Options
1103 $booking = isset($mec['booking']) ? $mec['booking'] : array();
1104 update_post_meta($post_id, 'mec_booking', $booking);
1105
1106 $tickets = isset($mec['tickets']) ? $mec['tickets'] : array();
1107 unset($tickets[':i:']);
1108
1109 // Unset Ticket Dats
1110 if(count($tickets))
1111 {
1112 $new_tickets = array();
1113 foreach($tickets as $key => $ticket)
1114 {
1115 unset($ticket['dates'][':j:']);
1116
1117 $ticket_start_time_ampm = ((intval($ticket['ticket_start_time_hour']) > 0 and intval($ticket['ticket_start_time_hour']) < 13) and isset($ticket['ticket_start_time_ampm'])) ? $ticket['ticket_start_time_ampm'] : '';
1118 $ticket_render_start_time = date('h:ia', strtotime(sprintf('%02d', $ticket['ticket_start_time_hour']) . ':' . sprintf('%02d', $ticket['ticket_start_time_minute']) . $ticket_start_time_ampm));
1119 $ticket_end_time_ampm = ((intval($ticket['ticket_end_time_hour']) > 0 and intval($ticket['ticket_end_time_hour']) < 13) and isset($ticket['ticket_end_time_ampm'])) ? $ticket['ticket_end_time_ampm'] : '';
1120 $ticket_render_end_time = date('h:ia', strtotime(sprintf('%02d', $ticket['ticket_end_time_hour']) . ':' . sprintf('%02d', $ticket['ticket_end_time_minute']) . $ticket_end_time_ampm));
1121
1122 $ticket['ticket_start_time_hour'] = substr($ticket_render_start_time, 0, 2);
1123 $ticket['ticket_start_time_ampm'] = strtoupper(substr($ticket_render_start_time, 5, 6));
1124 $ticket['ticket_end_time_hour'] = substr($ticket_render_end_time, 0, 2);
1125 $ticket['ticket_end_time_ampm'] = strtoupper(substr($ticket_render_end_time, 5, 6));
1126
1127 // Bellow conditional block code is used to change ticket dates format to compatible ticket past dates structure for store in db.
1128 if ( isset( $ticket[ 'dates' ] ) ) {
1129 foreach ( $ticket[ 'dates' ] as $dates_ticket_key => $dates_ticket_values ) {
1130 if ( isset( $dates_ticket_values[ 'start' ] ) and trim( $dates_ticket_values[ 'start' ] ) ) {
1131 $ticket[ 'dates' ][ $dates_ticket_key ][ 'start' ] = $this->main->standardize_format( $dates_ticket_values[ 'start' ] );
1132 }
1133
1134 if ( isset( $dates_ticket_values[ 'end' ] ) and trim( $dates_ticket_values[ 'end' ] ) ) {
1135 $ticket[ 'dates' ][ $dates_ticket_key ][ 'end' ] = $this->main->standardize_format( $dates_ticket_values[ 'end' ] );
1136 }
1137 }
1138 }
1139
1140 $new_tickets[$key] = $ticket;
1141 }
1142
1143 $tickets = $new_tickets;
1144 }
1145
1146 update_post_meta($post_id, 'mec_tickets', $tickets);
1147
1148 // Fee options
1149 $fees_global_inheritance = isset($mec['fees_global_inheritance']) ? $mec['fees_global_inheritance'] : 1;
1150 update_post_meta($post_id, 'mec_fees_global_inheritance', $fees_global_inheritance);
1151
1152 $fees = isset($mec['fees']) ? $mec['fees'] : array();
1153 update_post_meta($post_id, 'mec_fees', $fees);
1154
1155 // Ticket Variation options
1156 $ticket_variations_global_inheritance = isset($mec['ticket_variations_global_inheritance']) ? $mec['ticket_variations_global_inheritance'] : 1;
1157 update_post_meta($post_id, 'mec_ticket_variations_global_inheritance', $ticket_variations_global_inheritance);
1158
1159 $ticket_variations = isset($mec['ticket_variations']) ? $mec['ticket_variations'] : array();
1160 update_post_meta($post_id, 'mec_ticket_variations', $ticket_variations);
1161
1162 // Registration Fields options
1163 $reg_fields_global_inheritance = isset($mec['reg_fields_global_inheritance']) ? $mec['reg_fields_global_inheritance'] : 1;
1164 update_post_meta($post_id, 'mec_reg_fields_global_inheritance', $reg_fields_global_inheritance);
1165
1166 $reg_fields = isset($mec['reg_fields']) ? $mec['reg_fields'] : array();
1167 if($reg_fields_global_inheritance) $reg_fields = array();
1168
1169 update_post_meta($post_id, 'mec_reg_fields', $reg_fields);
1170
1171 $bfixed_fields = isset($mec['bfixed_fields']) ? $mec['bfixed_fields'] : array();
1172 if($reg_fields_global_inheritance) $bfixed_fields = array();
1173
1174 update_post_meta($post_id, 'mec_bfixed_fields', $bfixed_fields);
1175
1176 // Organizer Payment Options
1177 $op = isset($mec['op']) ? $mec['op'] : array();
1178 update_post_meta($post_id, 'mec_op', $op);
1179 update_user_meta(get_post_field('post_author', $post_id), 'mec_op', $op);
1180
1181 // SEO Schema
1182 $event_status = isset($mec['event_status']) ? sanitize_text_field($mec['event_status']) : 'EventScheduled';
1183 if(!in_array($event_status, array('EventScheduled', 'EventPostponed', 'EventCancelled', 'EventMovedOnline'))) $event_status = 'EventScheduled';
1184 update_post_meta($post_id, 'mec_event_status', $event_status);
1185
1186 $moved_online_link = (isset($mec['moved_online_link']) and filter_var($mec['moved_online_link'], FILTER_VALIDATE_URL)) ? esc_url($mec['moved_online_link']) : '';
1187 update_post_meta($post_id, 'mec_moved_online_link', $moved_online_link);
1188
1189 $cancelled_reason = (isset($mec['cancelled_reason']) and filter_var($mec['cancelled_reason'], FILTER_VALIDATE_URL)) ? esc_url($mec['cancelled_reason']) : '';
1190 update_post_meta($post_id, 'mec_cancelled_reason', $cancelled_reason);
1191
1192 if($booking_date_update)
1193 {
1194 $render_date = $past_start_date . ':' . $past_end_date;
1195 $new_date = $start_date . ':' . $end_date;
1196
1197 $books_query = new WP_Query(array(
1198 'post_type' => 'mec-books',
1199 'nopaging' => true,
1200 'post_status' => array('publish','pending','draft','future','private'),
1201 'meta_query' => array(
1202 'relation' => 'AND',
1203 array(
1204 'key' => 'mec_event_id',
1205 'value' => $post_id.'',
1206 'type' => 'numeric',
1207 'compare' => '='
1208 ),
1209 array(
1210 'key' => 'mec_date',
1211 'value' => $render_date,
1212 'compare' => '=',
1213 )
1214 )
1215 ));
1216
1217 if($books_query->have_posts())
1218 {
1219 $book = $this->getBook();
1220
1221 while($books_query->have_posts())
1222 {
1223 $books_query->the_post();
1224 $booking_id = get_the_ID();
1225
1226 // Update Booking
1227 update_post_meta($booking_id, 'mec_date', trim($new_date));
1228 wp_update_post(array(
1229 'ID' => $booking_id,
1230 'post_date' => $start_date
1231 ));
1232
1233 // Update Transaction
1234 $transaction_id = get_post_meta($booking_id, 'mec_transaction_id', true);
1235 $transaction = $book->get_transaction($transaction_id);
1236
1237 $transaction['date'] = trim($new_date);
1238 $book->update_transaction($transaction_id, $transaction);
1239 }
1240
1241 wp_reset_postdata();
1242 }
1243 }
1244
1245 // MEC Fields
1246 $fields = (isset($mec['fields']) and is_array($mec['fields'])) ? $mec['fields'] : array();
1247 update_post_meta($post_id, 'mec_fields', $fields);
1248
1249 // Downloadable File
1250 if(isset($mec['downloadable_file']))
1251 {
1252 $dl_file = isset($mec['downloadable_file']) ? $mec['downloadable_file'] : '';
1253 update_post_meta($post_id, 'mec_dl_file', $dl_file);
1254 }
1255
1256 do_action('save_fes_meta_action', $post_id, $mec);
1257
1258 // For Event Notification Badge.
1259 if(isset($_REQUEST['mec']['post_id']) and trim($_REQUEST['mec']['post_id']) == '-1') update_post_meta($post_id, 'mec_event_date_submit', date('YmdHis', current_time('timestamp', 0)));
1260
1261 $message = '';
1262 if($status == 'pending') $message = __('Event submitted. It will publish as soon as possible.', 'mec');
1263 elseif($status == 'publish') $message = __('The event published.', 'mec');
1264
1265 // Trigger Event
1266 if($method == 'updated') do_action('mec_fes_updated', $post_id , 'update');
1267 else do_action('mec_fes_added', $post_id , '');
1268
1269 // Save Event Data
1270 do_action('mec_save_event_data', $post_id, $mec);
1271
1272 $this->main->response(array(
1273 'success' => 1,
1274 'message' => $message,
1275 'data'=> array(
1276 'post_id' => $post_id,
1277 'redirect_to' => (isset($this->settings['fes_thankyou_page']) and trim($this->settings['fes_thankyou_page'])) ? get_permalink(intval($this->settings['fes_thankyou_page'])) : '',
1278 ),
1279 ));
1280 }
1281
1282 public function link_add_event()
1283 {
1284 if(isset($this->settings['fes_form_page']) and trim($this->settings['fes_form_page'])) return get_permalink($this->settings['fes_form_page']);
1285 else return $this->main->add_qs_var('post_id', '-1', $this->main->remove_qs_var('vlist'));
1286 }
1287
1288 public function link_edit_event($post_id)
1289 {
1290 if(isset($this->settings['fes_form_page']) and trim($this->settings['fes_form_page'])) return $this->main->add_qs_var('post_id', $post_id, get_permalink($this->settings['fes_form_page']));
1291 else return $this->main->add_qs_var('post_id', $post_id, $this->main->remove_qs_var('vlist'));
1292 }
1293
1294 public function link_list_events()
1295 {
1296 if(isset($this->settings['fes_list_page']) and trim($this->settings['fes_list_page'])) return get_permalink($this->settings['fes_list_page']);
1297 else return $this->main->add_qs_var('vlist', 1, $this->main->remove_qs_var('post_id'));
1298 }
1299
1300 /**
1301 * @param string $new_status
1302 * @param string $old_status
1303 * @param WP_Post $post
1304 */
1305 public function status_changed($new_status, $old_status, $post)
1306 {
1307 // User creation is not enabled
1308 if(!isset($this->settings['fes_guest_user_creation']) or (isset($this->settings['fes_guest_user_creation']) and !$this->settings['fes_guest_user_creation'])) return;
1309
1310 if(('publish' === $new_status && 'publish' !== $old_status) && $this->PT === $post->post_type)
1311 {
1312 $guest_email = get_post_meta($post->ID, 'fes_guest_email', true);
1313 if(!trim($guest_email) or (trim($guest_email) and !is_email($guest_email))) return;
1314
1315 $user_id = 0;
1316 $user_exists = email_exists($guest_email);
1317
1318 if($user_exists and $user_exists == $post->post_author) return;
1319 elseif($user_exists) $user_id = $user_exists;
1320 else
1321 {
1322 $registered = register_new_user($guest_email, $guest_email);
1323 if(!is_wp_error($registered))
1324 {
1325 $user_id = $registered;
1326
1327 $guest_name = get_post_meta($post->ID, 'fes_guest_name', true);
1328 $ex = explode(' ', $guest_name);
1329
1330 $first_name = $ex[0];
1331 unset($ex[0]);
1332
1333 $last_name = implode(' ', $ex);
1334
1335 wp_update_user(array(
1336 'ID' => $user_id,
1337 'first_name' => $first_name,
1338 'last_name' => $last_name,
1339 ));
1340
1341 $user = new WP_User($user_id);
1342 $user->set_role('author');
1343 }
1344 }
1345
1346 if($user_id)
1347 {
1348 $db = $this->getDB();
1349 $db->q("UPDATE `#__posts` SET `post_author`='$user_id' WHERE `ID`='".$post->ID."'");
1350 }
1351 }
1352 }
1353}
1354
1355// FES Categories Custom Walker
1356class FES_Custom_Walker extends Walker_Category
1357{
1358 /**
1359 * This class is a custom walker for front end event submission hierarchical categories customizing
1360 */
1361 private $post_id;
1362
1363 function __construct($post_id)
1364 {
1365 $this->post_id = $post_id;
1366 }
1367
1368 function start_lvl(&$output, $depth = 0, $args = array())
1369 {
1370 $indent = str_repeat("\t", $depth);
1371 $output .= "$indent<div class='mec-fes-category-children'>";
1372 }
1373
1374 function end_lvl(&$output, $depth = 0, $args = array())
1375 {
1376 $indent = str_repeat("\t", $depth);
1377 $output .= "$indent</div>";
1378 }
1379
1380 function start_el(&$output, $category, $depth = 0, $args = array(), $id = 0)
1381 {
1382 $post_categories = get_the_terms($this->post_id, 'mec_category');
1383
1384 $categories = array();
1385 if($post_categories) foreach($post_categories as $post_category) $categories[] = $post_category->term_id;
1386
1387 $output .= '<label for="mec_fes_categories' . $category->term_id . '">
1388 <input type="checkbox" name="mec[categories][' . $category->term_id . ']"
1389 id="mec_fes_categories' . $category->term_id .'" value="1"' . (in_array($category->term_id, $categories) ? 'checked="checked"' : '') . '/>' . $category->name;
1390 }
1391
1392 function end_el(&$output, $page, $depth = 0, $args = array())
1393 {
1394 $output .= '</label>';
1395 }
1396}