/** * Plugin Name: APR Hotel Booking for Gravity Forms * Description: Dynamic hotel booking form with multiple rooms, shared dates, server-side pricing and Gravity Forms entries. * Version: 1.0.0 * Author: APR * Requires PHP: 7.4 */ if (!defined('ABSPATH')) exit; class APR_Hotel_Booking_GF { const OPT = 'apr_hbgf_settings'; const FORM_OPT = 'apr_hbgf_form_id'; const NONCE = 'apr_hbgf_submit'; public function __construct() { add_shortcode('apr_hotel_booking', [$this, 'render_shortcode']); add_action('admin_menu', [$this, 'admin_menu']); add_action('admin_post_apr_hbgf_save', [$this, 'save_settings']); add_action('admin_post_apr_hbgf_create_form', [$this, 'create_gf_form']); add_action('wp_ajax_apr_hbgf_submit', [$this, 'ajax_submit']); add_action('wp_ajax_nopriv_apr_hbgf_submit', [$this, 'ajax_submit']); add_action('wp_enqueue_scripts', [$this, 'register_assets']); } public static function defaults() { return [ 'rates' => ['Single' => '0', 'Double' => '0', 'Twin' => '0', 'Triple' => '0'], 'tax_rate' => '0', 'admin_email' => get_option('admin_email'), 'currency' => 'EUR', ]; } public function settings() { return wp_parse_args(get_option(self::OPT, []), self::defaults()); } public function admin_menu() { add_options_page('APR Hotel Booking', 'APR Hotel Booking', 'manage_options', 'apr-hotel-booking', [$this, 'settings_page']); } public function settings_page() { if (!current_user_can('manage_options')) return; $s = $this->settings(); $form_id = absint(get_option(self::FORM_OPT)); ?>

APR Hotel Booking

Set the room rates and optional accommodation tax. Amounts are per room, per night.

Room rates

Leave at 0 until the applicable tax amount is confirmed.


Gravity Forms connection

Connected Gravity Forms form ID:

Entries from this booking form are saved in Gravity Forms. The plugin also sends booking emails.

No booking form has been created yet.


Shortcode

[apr_hotel_booking]

Insert this shortcode into the accommodation booking page.

$rates, 'tax_rate'=>$tax, 'admin_email'=>$email ?: get_option('admin_email'), 'currency'=>'EUR']); wp_safe_redirect(add_query_arg('updated', '1', admin_url('options-general.php?page=apr-hotel-booking'))); exit; } public function create_gf_form() { if (!current_user_can('manage_options')) wp_die('Unauthorized'); check_admin_referer('apr_hbgf_create_form'); if (!class_exists('GFAPI')) { wp_die('Gravity Forms must be installed and activated before creating the booking form.'); } $existing = absint(get_option(self::FORM_OPT)); if ($existing && GFAPI::get_form($existing)) { wp_safe_redirect(admin_url('options-general.php?page=apr-hotel-booking')); exit; } $fields = [ ['id'=>1,'type'=>'text','label'=>'Booking Contact Name','isRequired'=>true,'adminOnly'=>false], ['id'=>2,'type'=>'email','label'=>'Email Address','isRequired'=>true,'adminOnly'=>false], ['id'=>3,'type'=>'phone','label'=>'Telephone','isRequired'=>false,'adminOnly'=>false], ['id'=>4,'type'=>'date','label'=>'Check-in Date','isRequired'=>true,'adminOnly'=>false], ['id'=>5,'type'=>'date','label'=>'Check-out Date','isRequired'=>true,'adminOnly'=>false], ['id'=>6,'type'=>'textarea','label'=>'Room Reservations Details','isRequired'=>false,'adminOnly'=>false], ['id'=>7,'type'=>'text','label'=>'Accommodation Tax','isRequired'=>false,'adminOnly'=>false], ['id'=>8,'type'=>'number','label'=>'Grand Total (EUR)','isRequired'=>false,'adminOnly'=>false], ]; $form = [ 'title' => 'Hotel Accommodation Booking', 'description' => 'Booking entries submitted through APR Hotel Booking.', 'labelPlacement' => 'top_label', 'descriptionPlacement' => 'below', 'button' => ['type'=>'text','text'=>'Submit Booking','imageUrl'=>''], 'fields' => $fields, 'notifications' => [ 'apr_admin' => [ 'id'=>'apr_admin','name'=>'APR Hotel Booking – Administrator','event'=>'form_submission', 'toType'=>'email','to'=>$this->settings()['admin_email'],'subject'=>'New Hotel Accommodation Booking', 'message'=>'{all_fields}','from'=>'{admin_email}','fromName'=>'{admin_email}', 'disableAutoformat'=>false,'isActive'=>true,'toField'=>'','routing'=>null, 'conditionalLogic'=>null,'notification_conditional_logic'=>false ], 'apr_customer' => [ 'id'=>'apr_customer','name'=>'APR Hotel Booking – Participant','event'=>'form_submission', 'toType'=>'field','to'=>'','toField'=>'2','subject'=>'Hotel Accommodation Booking Received', 'message'=>'Thank you. We have received your hotel accommodation booking. Please find your booking details below.

{all_fields}', 'from'=>'{admin_email}','fromName'=>'Conference Secretariat', 'disableAutoformat'=>false,'isActive'=>true,'routing'=>null, 'conditionalLogic'=>null,'notification_conditional_logic'=>false ], ], 'confirmations' => [ ['id'=>'apr_confirmation','name'=>'Default Confirmation','isDefault'=>true,'type'=>'message', 'message'=>'Thank you. Your accommodation booking has been received. A confirmation email will be sent to you.', 'url'=>'','pageId'=>'','queryString'=>''] ], ]; $new_id = GFAPI::add_form($form); if (is_wp_error($new_id)) wp_die(esc_html($new_id->get_error_message())); update_option(self::FORM_OPT, absint($new_id)); wp_safe_redirect(admin_url('options-general.php?page=apr-hotel-booking&created=1')); exit; } public function register_assets() { wp_register_style('apr-hbgf', plugins_url('assets/booking.css', __FILE__), [], '1.0.0'); wp_register_script('apr-hbgf', plugins_url('assets/booking.js', __FILE__), [], '1.0.0', true); } public function render_shortcode() { wp_enqueue_style('apr-hbgf'); wp_enqueue_script('apr-hbgf'); $s = $this->settings(); $rates = []; foreach (['Single','Double','Twin','Triple'] as $type) $rates[$type] = (float) ($s['rates'][$type] ?? 0); wp_localize_script('apr-hbgf', 'APRHotelBooking', [ 'ajaxUrl'=>admin_url('admin-ajax.php'), 'nonce'=>wp_create_nonce(self::NONCE), 'rates'=>$rates, 'taxRate'=>(float)$s['tax_rate'], 'currency'=>'EUR', 'strings'=>['required'=>'Please complete all required fields.','date'=>'Check-out must be later than check-in.','room'=>'Please add at least one room.','error'=>'Your booking could not be submitted. Please try again.'] ]); ob_start(); ?>

Hotel Accommodation Booking

Accommodation Details

Number of nights: —

Room Reservations

Booking Summary

Select your check-in and check-out dates.
Grand Total€0.00
'Gravity Forms is not active.'], 400); $name = sanitize_text_field(wp_unslash($_POST['full_name'] ?? '')); $email = sanitize_email(wp_unslash($_POST['email'] ?? '')); $phone = sanitize_text_field(wp_unslash($_POST['phone'] ?? '')); $checkin = sanitize_text_field(wp_unslash($_POST['checkin'] ?? '')); $checkout = sanitize_text_field(wp_unslash($_POST['checkout'] ?? '')); $include_tax = !empty($_POST['include_tax']); $raw_rooms = isset($_POST['rooms']) ? wp_unslash($_POST['rooms']) : ''; $rooms = json_decode($raw_rooms, true); if (!$name || !is_email($email) || !$checkin || !$checkout || !is_array($rooms) || count($rooms) < 1) { wp_send_json_error(['message'=>'Please complete all required fields and add at least one room.'], 400); } $in = DateTime::createFromFormat('Y-m-d', $checkin); $out = DateTime::createFromFormat('Y-m-d', $checkout); if (!$in || !$out || $out <= $in || $in->format('Y-m-d') !== $checkin || $out->format('Y-m-d') !== $checkout) { wp_send_json_error(['message'=>'Check-out must be later than check-in.'], 400); } $nights = (int)$in->diff($out)->days; $s = $this->settings(); $clean_rooms = []; $subtotal = 0.0; foreach ($rooms as $room) { $type = sanitize_text_field($room['type'] ?? ''); $guest = sanitize_text_field($room['guest'] ?? ''); if (!in_array($type, ['Single','Double','Twin','Triple'], true)) continue; $rate = (float)($s['rates'][$type] ?? 0); if ($rate <= 0) wp_send_json_error(['message'=>'A valid rate has not yet been configured for '.$type.' Room. Please contact the conference secretariat.'], 400); $line = round($rate * $nights, 2); $subtotal += $line; $clean_rooms[] = ['type'=>$type,'guest'=>$guest,'rate'=>$rate,'nights'=>$nights,'subtotal'=>$line]; } if (!$clean_rooms) wp_send_json_error(['message'=>'Please add at least one valid room.'], 400); $tax_rate = $include_tax ? (float)$s['tax_rate'] : 0; $tax_total = round($tax_rate * $nights * count($clean_rooms), 2); $total = round($subtotal + $tax_total, 2); $form_id = absint(get_option(self::FORM_OPT)); $form = $form_id ? GFAPI::get_form($form_id) : false; if (!$form || is_wp_error($form)) wp_send_json_error(['message'=>'The Gravity Forms booking form has not been created. Please ask the website administrator to complete setup.'], 500); $details = []; foreach ($clean_rooms as $i=>$r) { $details[] = sprintf("Room %d: %s | Guest(s): %s | Rate: €%.2f/night | %d nights | Subtotal: €%.2f", $i+1, $r['type'], $r['guest'] ?: 'Not specified', $r['rate'], $nights, $r['subtotal']); } $details_text = implode("\n", $details); $entry = [ 'form_id'=>$form_id, '1'=>$name, '2'=>$email, '3'=>$phone, '4'=>$checkin, '5'=>$checkout, '6'=>$details_text, '7'=>$include_tax ? sprintf('Included: €%.2f per room/night; total €%.2f', $tax_rate, $tax_total) : 'Not included', '8'=>number_format($total, 2, '.', ''), ]; $entry_id = GFAPI::add_entry($entry); if (is_wp_error($entry_id)) wp_send_json_error(['message'=>'Gravity Forms could not save the booking. Please contact the website administrator.'], 500); if (method_exists('GFAPI', 'send_notifications')) { GFAPI::send_notifications($form, GFAPI::get_entry($entry_id)); } // Notifications are sent through the notifications configured on the // dedicated Gravity Forms form above. This avoids duplicate emails. wp_send_json_success(['message'=>'Your accommodation booking has been submitted successfully. A copy of the booking details has been sent to your email address.','entryId'=>$entry_id]); } } new APR_Hotel_Booking_GF();