Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically block bookings when a max per-day booking limit is hit.
<?php
/**
* Plugin Name: Max Bookings for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Modifies the Restaurant Reservations plugin to rejects bookings if a max bookings limit has been reached for the day.
* Version: 1.0
* Author: Theme of the Crop
* Author URI: http://themeofthecrop.com
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: max-capacity-for-rtb
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) )
exit;
if ( !class_exists( 'mxbfrtbInit' ) ) {
class mxbfrtbInit {
/**
* The single instance of this class
*/
private static $instance;
/**
* Path to the plugin directory
*/
static $plugin_dir;
/**
* URL to the plugin
*/
static $plugin_url;
/**
* Maximum bookings per day
*/
public $max_bookings = 2;
/**
* Create or retrieve the single instance of the class
*
* @since 0.1
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new mxbfrtbInit;
self::$plugin_dir = untrailingslashit( plugin_dir_path( __FILE__ ) );
self::$plugin_url = untrailingslashit( plugin_dir_url( __FILE__ ) );
self::$instance->init();
}
return self::$instance;
}
/**
* Initialize the plugin and register hooks
*/
public function init() {
if ( !defined( 'RTB_BOOKING_POST_TYPE' ) ) {
return;
}
add_action( 'init', array( $this, 'load_textdomain' ) );
// Validate form fields
add_action( 'rtb_validate_booking_submission', array( $this, 'validate_booking_submission' ) );
}
/**
* Load the plugin textdomain for localistion
* @since 0.0.1
*/
public function load_textdomain() {
load_plugin_textdomain( 'max-bookings-for-rtb', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" );
}
/**
* Validate the booking submission to block bookings if max
* bookings reached
*/
public function validate_booking_submission( $booking ) {
$party = absint( $booking->party );
// Check if we have space for this booking
$capacity = $this->get_day_capacity( $booking->date );
if ( $capacity === 0 ) {
$booking->validation_errors[] = array(
'field' => 'date',
'error_msg' => 'Booked to capacity',
'message' => __( 'Sorry, we are fully booked that day. Please visit us another day.', 'max-bookings-for-rtb' ),
);
}
}
/**
* Get the remaining capacity for a given day
*/
public function get_day_capacity( $date ) {
$date = new DateTime( $date );
$bookings = new WP_Query(
array(
'posts_per_page' => 1000, // large upper limit
'post_type' => RTB_BOOKING_POST_TYPE,
'post_status' => 'confirmed',
'date_query' => array(
array(
'year' => $date->format( 'Y' ),
'month' => $date->format( 'm' ),
'day' => $date->format( 'd' )
)
)
)
);
if ( !$bookings->have_posts() ) {
return $this->max_bookings;
}
$capacity = $this->max_bookings - $bookings->found_posts;
return max( $capacity, 0 );
}
}
} // endif;
/**
* This function returns one mxbfrtbInit instance everywhere
* and can be used like a global, without needing to declare the global.
*
* Example: $ssfrtb = mxbfrtbInit();
*/
if ( !function_exists( 'mxbfrtbInit' ) ) {
function mxbfrtbInit() {
return mxbfrtbInit::instance();
}
add_action( 'plugins_loaded', 'mxbfrtbInit' );
} // endif;
@nikitae

This comment has been minimized.

Copy link

commented Sep 3, 2015

Is it possible to change this to max-bookings per time instead of date, and how do I add this plugin to restaurant_reservation part?

@benzdumol

This comment has been minimized.

Copy link

commented Jun 22, 2017

Hi Nate,
Just like what nikitae asked above, I was wondering if it's possible to change the max bookings per time instead of date?
Hoping to hear from you soon!
Thank you in advance.
Cheers,
Benessa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.