Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds a minimum party size setting to the Restaurant Reservations plugin.
<?php
/**
* Plugin Name: Party Limits for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: Adds a minimum party size setting to the Restaurant Reservations plugin.
* Version: 0.3
* 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: party-limits-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( 'plrtbInit' ) ) {
class plrtbInit {
/**
* The single instance of this class
*/
private static $instance;
/**
* Path to the plugin directory
*/
static $plugin_dir;
/**
* URL to the plugin
*/
static $plugin_url;
/**
* Create or retrieve the single instance of the class
*
* @since 0.1
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new plrtbInit;
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() {
// Load the textdomain
add_action( 'init', array( $this, 'load_textdomain' ) );
if ( !defined( 'RTB_BOOKING_POST_TYPE' ) ) {
return;
}
// Modify party field options
add_filter( 'rtb_form_party_options', array( $this, 'modify_form_party_options' ) );
// Validate party field submission
add_action( 'rtb_validate_booking_submission', array( $this, 'validate_booking_submission' ) );
// Add min party size setting
add_filter( 'rtb_settings_page', array( $this, 'add_settings' ) );
}
/**
* Load the plugin textdomain for localistion
* @since 0.0.1
*/
public function load_textdomain() {
load_plugin_textdomain( 'party-limits-for-rtb', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Modify the select options in the booking form's party field
*
* @since 0.0.1
*/
public function modify_form_party_options( $options ) {
global $rtb_controller;
$min = (int) $rtb_controller->settings->get_setting( 'party-size-min' );
$new_options = array();
for( $i = $min; $i < count( $options ); $i++ ) {
$new_options[$i] = $i;
}
return $new_options;
}
/**
* Only allow valid party sizes
*/
public function validate_booking_submission( $booking ) {
global $rtb_controller;
$max = $rtb_controller->settings->get_setting( 'party-size' );
$min = $rtb_controller->settings->get_setting( 'party-size-min' );
$party = absint( $booking->party );
if ( $party < $min || $party > $max ) {
// Remove any existing errors on the party field so we
// don't end up with duplicates
for( $i = 0; $i < count( $booking->validation_errors ); $i++ ) {
if ( $booking->validation_errors[$i]['field'] === 'party' ) {
unset( $booking->validation_errors[$i] );
}
}
$booking->validation_errors[] = array(
'field' => 'party',
'error_msg' => 'Booking request party is invalid',
'message' => sprintf( __( 'Sorry, we only take bookings for parties of %s to %s people.', 'party-limits-for-rtb' ), $min, $max ),
);
$booking->validation_errors = array_filter( $booking->validation_errors );
}
}
/**
* Add a min party size setting
*/
public function add_settings( $sap ) {
global $rtb_controller;
$sap->add_setting(
'rtb-settings',
'rtb-general',
'select',
array(
'id' => 'party-size-min',
'title' => __( 'Min Party Size', 'party-limits-for-rtb' ),
'description' => __( 'Set a minimum allowed party size for bookings.', 'party-limits-for-rtb' ),
'blank_option' => false,
'options' => $rtb_controller->settings->get_party_size_setting_options(),
)
);
return $sap;
}
}
} // endif;
/**
* This function returns one plrtbInit instance everywhere
* and can be used like a global, without needing to declare the global.
*
* Example: $plrtb = plrtbInit();
*/
if ( !function_exists( 'plrtbInit' ) ) {
function plrtbInit() {
return plrtbInit::instance();
}
add_action( 'plugins_loaded', 'plrtbInit' );
} // endif;
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.