Skip to content

Instantly share code, notes, and snippets.

@NateWr
Created November 16, 2016 10:11
Show Gist options
  • Save NateWr/a968c13f8ce5231c82feb43c7fd9d5a4 to your computer and use it in GitHub Desktop.
Save NateWr/a968c13f8ce5231c82feb43c7fd9d5a4 to your computer and use it in GitHub Desktop.
This plugin records the IP address from bookings and allows you to block bookings by IP address or email address.
<?php
/**
* Plugin Name: Block Bookings by IP/Email Address for Restaurant Reservations
* Plugin URI: http://themeofthecrop.com
* Description: This plugin records the IP address from bookings and allows you to block bookings by IP address or email address.
* Version: 0.1
* 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
*
* 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
*/
defined( 'ABSPATH' ) || exit;
/**
* Add options fields for entering banned IP addresses and email addresses
*
* @since 0.1
*/
function rtbbip_ban_options( $sap ) {
$sap->add_setting(
'rtb-settings',
'rtb-general',
'textarea',
array(
'id' => 'rtbbip-ban-ips',
'title' => __( 'Banned IP Addresses', 'restaurant-reservations' ),
'description' => __( 'You can block bookings from specific IP addresses. Enter each IP address on a separate line. Be aware that many internet providers rotate their IP address assignments, so an IP address may accidentally refer to a different user. Also, if you block an IP address used by a public connection, such as cafe WIFI, a public library, or a university network, you may inadvertantly block several people.', 'restaurant-reservations' ),
)
);
$sap->add_setting(
'rtb-settings',
'rtb-general',
'textarea',
array(
'id' => 'rtbbip-ban-emails',
'title' => __( 'Banned Email Addresses', 'restaurant-reservations' ),
'description' => __( 'You can block bookings from specific email addresses. Enter each email address on a separate line.', 'restaurant-reservations' ),
)
);
return $sap;
}
add_filter( 'rtb_settings_page', 'rtbbip_ban_options' );
/**
* Validate the IP and email address when submitted
*
* Bookings Managers bypass this validation. No IP address is stored if one is
* already set to prevent overwriting the original IP when a booking is edited.
*
* @since 0.1
*/
function rtbbip_check_banned( $booking ) {
if ( !current_user_can( 'manage_bookings' ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
if ( !rtbbip_is_valid_ip( $ip ) || !rtbbip_is_valid_email( $booking->email ) ) {
$booking->validation_errors[] = array(
'field' => 'date',
'post_variable' => $ip,
'message' => __( 'Your booking has been rejected. Please call us if you would like to make a booking.', 'restaurant-reservations' ),
);
} elseif ( empty( $booking->rtbbip_ip ) ) {
$booking->rtbbip_ip = $ip;
}
}
}
add_action( 'rtb_validate_booking_submission', 'rtbbip_check_banned' );
/**
* Check if an IP has been blocked
*
* @since 0.1
*/
function rtbbip_is_valid_ip( $ip ) {
global $rtb_controller;
$banned_ips = array_filter( explode( "\n", $rtb_controller->settings->get_setting( 'rtbbip-ban-ips' ) ) );
foreach( $banned_ips as $banned_ip ) {
if ( $ip == trim( $banned_ip ) ) {
return false;
}
}
return true;
}
/**
* Check if an email address has been blocked
*
* @since 0.1
*/
function rtbbip_is_valid_email( $email ) {
global $rtb_controller;
$banned_emails = array_filter( explode( "\n", $rtb_controller->settings->get_setting( 'rtbbip-ban-emails' ) ) );
foreach( $banned_emails as $banned_email ) {
if ( $email == trim( $banned_email ) ) {
return false;
}
}
return true;
}
/**
* Save the IP address as metadata
*
* @since 0.1
*/
function rtbbip_save_ip( $meta, $booking ) {
$meta['rtbbip_ip'] = isset( $booking->rtbbip_ip ) ? $booking->rtbbip_ip : '';
return $meta;
}
add_filter( 'rtb_insert_booking_metadata', 'rtbbip_save_ip', 10, 2 );
/**
* Load the IP address into the booking object
*
* @since 0.1
*/
function rtbbip_load_ip( $booking, $post ) {
$meta = get_post_meta( $post->ID, 'rtb', true );
if ( isset( $meta['rtbbip_ip'] ) ) {
$booking->rtbbip_ip = $meta['rtbbip_ip'];
}
}
add_filter( 'rtb_booking_load_post_data', 'rtbbip_load_ip', 10, 2 );
/**
* Add a column to the bookings table for IP address
*
* @since 0.1
*/
function rtbbip_add_bookings_table_ip_column( $columns ) {
$columns['rtbbip_ip'] = __( 'IP Address', 'restaurant-reservations' );
return $columns;
}
add_filter( 'rtb_bookings_table_columns', 'rtbbip_add_bookings_table_ip_column' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment