G2A Many GEOs
SHARE
TWEET

Prevent Double Booking Overlapping Events

eventsmanager Mar 16th, 2018 (edited) 210 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  Plugin Name: Events Manager Booking Overlaps
  4.  Version: 1.0
  5.  Plugin URI: http://wp-events-plugin.com
  6.  Description: Prevents booking two events which overlap times. Works with Multiple Bookings mode as well.
  7.  Author: Marcus Sykes
  8.  Author URI: http://wp-events-plugin.com
  9.  Text Domain: events-manager
  10. */
  11. /*
  12. INSTALLATION/USAGE INSTRUCTIONS:
  13. You can install this plugin by pasting this to a file called events-manager-booking-overlaps.php and move it to your plugins folder, or place it within a folder in your plugins folder having the same name as your file, e.g. wp-content/plugins/events-manager-booking-overlaps/events-manager-booking-overlaps.php
  14. */
  15.  
  16. /**
  17.  * @param boolean $result
  18.  * @param EM_Booking $EM_Booking
  19.  */
  20. function my_em_double_booking_prevention( $result, $EM_Booking ){
  21.     if( $result && is_user_logged_in() ){
  22.         $EM_Event = $EM_Booking->get_event();
  23.         add_filter('pre_option_dbem_events_current_are_past', function(){ return 0; }, 1111);
  24.         $EM_Bookings = EM_Bookings::get( array('person'=>get_current_user_id(), 'owner' => false, 'scope' => array($EM_Event->event_start_date, $EM_Event->event_end_date) ) );
  25.         remove_all_filters('pre_option_dbem_events_current_are_past', 1111);
  26.         foreach( $EM_Bookings as $booking ){ /* @var EM_Booking $booking */
  27.             if( in_array($booking->booking_status, array(0,1,4,5)) && my_em_mb_double_booking_prevention_compare($EM_Event, $booking->get_event()) ){
  28.                 $result = false;
  29.                 $EM_Booking->add_error(__('You already have a booking during the time of this event. Please select another event or cancel your previous booking.', 'events-manager'));
  30.             }
  31.         }
  32.     }
  33.     return $result;
  34. }
  35. add_filter('em_booking_validate', 'my_em_double_booking_prevention', 10, 2);
  36.  
  37. /**
  38.  * @param true $result
  39.  * @param EM_Booking $EM_Booking
  40.  * @param EM_Multiple_Booking $EM_Multiple_Booking
  41.  */
  42. function my_em_mb_double_booking_prevention( $result, $EM_Booking, $EM_Multiple_Booking){
  43.     if( $result ){
  44.         $EM_Event = $EM_Booking->get_event();
  45.         foreach( $EM_Multiple_Booking->get_bookings() as $event_id => $booking ){ /* @var EM_Booking $booking */
  46.             if( $EM_Event->event_id != $event_id && my_em_mb_double_booking_prevention_compare($EM_Event, $booking->get_event()) ){
  47.                 $result = false;
  48.                 $EM_Booking->add_error(__('You already have a booking in your cart during the time of this event. Please select another event or remove your other booking from the cart.', 'events-manager'));
  49.                 $EM_Multiple_Booking->remove_booking($booking->get_event()->event_id);
  50.             }
  51.         }
  52.     }
  53.     return $result;
  54. }
  55. add_filter('em_multiple_booking_add_booking', 'my_em_mb_double_booking_prevention', 10, 3);
  56.  
  57. function my_em_mb_double_booking_prevention_compare($EM_Event, $event){
  58.     $start_time = method_exists($EM_Event, 'start') ? $EM_Event->start()->getTimestamp() : $EM_Event->start;
  59.     $end_time = method_exists($EM_Event, 'end') ? $EM_Event->end()->getTimestamp() : $EM_Event->end;
  60.     $event_start_time = method_exists($event, 'start') ? $event->start()->getTimestamp() : $event->start;
  61.     $event_end_time = method_exists($event, 'end') ? $event->end()->getTimestamp() : $event->end;
  62.     if( $event_start_time > $start_time && $event_start_time < $end_time ) return true;
  63.     if( $event_end_time > $start_time && $event_end_time < $end_time ) return true;
  64.     if( $event_start_time <= $start_time && $event_end_time >= $end_time ) return true;
  65.     return false;
  66. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top