Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Events Manager Booking Overlaps
- Version: 1.0
- Plugin URI: http://wp-events-plugin.com
- Description: Prevents booking two events which overlap times. Works with Multiple Bookings mode as well.
- Author: Marcus Sykes
- Author URI: http://wp-events-plugin.com
- Text Domain: events-manager
- */
- /*
- INSTALLATION/USAGE INSTRUCTIONS:
- 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
- */
- /**
- * @param boolean $result
- * @param EM_Booking $EM_Booking
- */
- function my_em_double_booking_prevention( $result, $EM_Booking ){
- if( $result && is_user_logged_in() ){
- $EM_Event = $EM_Booking->get_event();
- add_filter('pre_option_dbem_events_current_are_past', function(){ return 0; }, 1111);
- $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) ) );
- remove_all_filters('pre_option_dbem_events_current_are_past', 1111);
- foreach( $EM_Bookings as $booking ){ /* @var EM_Booking $booking */
- if( in_array($booking->booking_status, array(0,1,4,5)) && my_em_mb_double_booking_prevention_compare($EM_Event, $booking->get_event()) ){
- $result = false;
- $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'));
- }
- }
- }
- return $result;
- }
- add_filter('em_booking_validate', 'my_em_double_booking_prevention', 10, 2);
- /**
- * @param true $result
- * @param EM_Booking $EM_Booking
- * @param EM_Multiple_Booking $EM_Multiple_Booking
- */
- function my_em_mb_double_booking_prevention( $result, $EM_Booking, $EM_Multiple_Booking){
- if( $result ){
- $EM_Event = $EM_Booking->get_event();
- foreach( $EM_Multiple_Booking->get_bookings() as $event_id => $booking ){ /* @var EM_Booking $booking */
- if( $EM_Event->event_id != $event_id && my_em_mb_double_booking_prevention_compare($EM_Event, $booking->get_event()) ){
- $result = false;
- $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'));
- $EM_Multiple_Booking->remove_booking($booking->get_event()->event_id);
- }
- }
- }
- return $result;
- }
- add_filter('em_multiple_booking_add_booking', 'my_em_mb_double_booking_prevention', 10, 3);
- function my_em_mb_double_booking_prevention_compare($EM_Event, $event){
- $start_time = method_exists($EM_Event, 'start') ? $EM_Event->start()->getTimestamp() : $EM_Event->start;
- $end_time = method_exists($EM_Event, 'end') ? $EM_Event->end()->getTimestamp() : $EM_Event->end;
- $event_start_time = method_exists($event, 'start') ? $event->start()->getTimestamp() : $event->start;
- $event_end_time = method_exists($event, 'end') ? $event->end()->getTimestamp() : $event->end;
- if( $event_start_time > $start_time && $event_start_time < $end_time ) return true;
- if( $event_end_time > $start_time && $event_end_time < $end_time ) return true;
- if( $event_start_time <= $start_time && $event_end_time >= $end_time ) return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement