Created
May 15, 2015 14:00
-
-
Save NateWr/ac01564c40352c99bf24 to your computer and use it in GitHub Desktop.
Automatically block bookings when a max per-day booking limit is hit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @ankitkumarsah, I'm no longer involved with the Restaurant Reservations plugin. You may want to contact the current plugin maintainers at https://wordpress.org/plugins/restaurant-reservations/.