Skip to content

Instantly share code, notes, and snippets.

@huub-l
Forked from NateWr/max-capacity-for-rtb.php
Created January 7, 2019 09:47
Show Gist options
  • Save huub-l/95e782dce1b8f17d13fe63be72b3a3c0 to your computer and use it in GitHub Desktop.
Save huub-l/95e782dce1b8f17d13fe63be72b3a3c0 to your computer and use it in GitHub Desktop.
Automatically block bookings when a max per-day capacity is hit.
<?php
/**
* Plugin Name: Max Capacity for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Modifies the Restaurant Reservations plugin to rejects bookings if a max capacity 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( 'mxcfrtbInit' ) ) {
class mxcfrtbInit {
/**
* 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 capacity per night
*/
public $max_capacity = 12;
/**
* Create or retrieve the single instance of the class
*
* @since 0.1
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new mxcfrtbInit;
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-capacity-for-rtb', false, plugin_basename( dirname( __FILE__ ) ) . "/languages/" );
}
/**
* Validate the booking submission to block bookings if max
* capacity 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' => 'party',
'error_msg' => 'Booked to capacity',
'message' => __( 'Sorry, we are fully booked that day. Please visit us another day.', 'max-capacity-for-rtb' ),
);
} elseif ( $party > $capacity ) {
$booking->validation_errors[] = array(
'field' => 'party',
'error_msg' => 'Booking request party is too big',
'message' => sprintf( __( 'Sorry, we only have space for %s more people on that day. Please visit us another day.', 'max-capacity-for-rtb' ), $capacity ),
);
}
}
/**
* Get the remaining capacity for a given day
*/
public function get_day_capacity( $date ) {
$date = new DateTime( $date );
$bookings = new WP_Query(
array(
'post_type' => RTB_BOOKING_POST_TYPE,
'post_status' => 'confirmed',
'posts_per_page' => -1,
'date_query' => array(
array(
'year' => $date->format( 'Y' ),
'month' => $date->format( 'm' ),
'day' => $date->format( 'd' )
)
)
)
);
if ( !$bookings->have_posts() ) {
return $this->max_capacity;
}
$capacity = $this->max_capacity;
while ( $bookings->have_posts() ) {
$bookings->the_post();
$post_meta = get_post_meta( $bookings->post->ID, 'rtb', true );
if ( !empty( $post_meta['party'] ) ) {
$capacity -= $post_meta['party'];
}
}
return max( $capacity, 0 );
}
}
} // endif;
/**
* This function returns one mxcfrtbInit instance everywhere
* and can be used like a global, without needing to declare the global.
*
* Example: $ssfrtb = mxcfrtbInit();
*/
if ( !function_exists( 'mxcfrtbInit' ) ) {
function mxcfrtbInit() {
return mxcfrtbInit::instance();
}
add_action( 'plugins_loaded', 'mxcfrtbInit' );
} // endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment