Created
January 23, 2018 07:27
-
-
Save markware/a90cb40db4abd4d6023ea131b68512b8 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/* | |
* Template Name: Booking Page | |
*/ | |
?> | |
<?php | |
// Sanitize data, or initialize if they don't exist. | |
$clientname = isset( $_POST['ci_name'] ) ? esc_html( trim( $_POST['ci_name'] ) ) : ''; | |
$email = isset( $_POST['ci_email'] ) ? esc_html( trim( $_POST['ci_email'] ) ) : ''; | |
$mobile = isset( $_POST['ci_mobile'] ) ? esc_html( trim( $_POST['ci_mobile'] ) ) : ''; | |
$arrive = isset( $_POST['arrive'] ) ? esc_html( trim( $_POST['arrive'] ) ) : ''; | |
$depart = isset( $_POST['depart'] ) ? esc_html( trim( $_POST['depart'] ) ) : ''; | |
$guests = isset( $_POST['adults'] ) ? intval( $_POST['adults'] ) : '0'; | |
$children = isset( $_POST['children'] ) ? intval( $_POST['children'] ) : '0'; | |
$message = isset( $_POST['ci_comments'] ) ? sanitize_text_field( stripslashes( $_POST['ci_comments'] ) ) : ''; | |
if ( ! empty( $_POST['room_select'] ) ) { | |
$room_id = intval( $_POST['room_select'] ); | |
} elseif ( ! empty( $_GET['room_select'] ) ) { | |
$room_id = intval( $_GET['room_select'] ); | |
} else { | |
$room_id = ''; | |
} | |
$errorString = ''; | |
$emailSent = false; | |
if ( isset( $_POST['send_booking'] ) ) { | |
// We are here because the form was submitted. Let's validate! | |
if ( empty( $clientname ) or mb_strlen( $clientname ) < 2 ) { | |
$errorString .= '<li> ' . __( 'Your first name is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $lastname ) or mb_strlen( $lastname ) < 2 ) { | |
$errorString .= '<li> ' . __( 'Your last name is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $email ) or ! is_email( $email ) ) { | |
$errorString .= '<li> ' . __( 'A valid email is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $mobile ) or ! is_numeric($mobile) ) { | |
$errorString .= '<li> ' . __( 'Please make sure the mobile number is correct.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $arrive ) or strlen( $arrive ) != 10 ) { | |
$errorString .= '<li> ' . __( 'A complete arrival date is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( ! checkdate( substr( $arrive, 5, 2 ), substr( $arrive, 8, 2 ), substr( $arrive, 0, 4 ) ) ) { | |
$errorString .= '<li> ' . __( 'The arrival date must be in the form yyyy/mm/dd.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $depart ) or strlen( $depart ) != 10 ) { | |
$errorString .= '<li> ' . __( 'A complete departure date is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( ! checkdate( substr( $depart, 5, 2 ), substr( $depart, 8, 2 ), substr( $depart, 0, 4 ) ) ) { | |
$errorString .= '<li> ' . __( 'The departure date must be in the form yyyy/mm/dd.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $guests ) or ! is_numeric( $guests ) or $guests < 1 ) { | |
$errorString .= '<li> ' . __( 'A number of one or more adults is required.', 'ci_theme' ) . '</li>'; | |
} | |
if ( empty( $room_id ) or ! is_numeric( $room_id ) or $room_id < 1 ) { | |
$errorString .= '<li> ' . __( 'You must select the room you are interested in.', 'ci_theme' ) . '</li>'; | |
} else { | |
$room = get_post( $room_id ); | |
if ( $room === null or get_post_type( $room ) != 'cpt_room' ) { | |
// Someone tried to pass a post id that isn't a room. Kinky. | |
$errorString .= '<li> ' . __( 'You must select the room you are interested in.', 'ci_theme' ) . '</li>'; | |
} | |
} | |
// Message is optional, so, no check. | |
// Alright, lets send the email already! | |
if ( empty( $errorString ) ) { | |
$mailbody = __( 'Name:', 'ci_theme' ) . ' ' . $clientname . "\n"; | |
$mailbody = __( 'Last Name:', 'ci_theme' ) . ' ' . $lastname . "\n"; | |
$mailbody .= __( 'Email:', 'ci_theme' ) . ' ' . $email . "\n"; | |
$mailbody .= __( 'Mobile:', 'ci_theme' ) . ' ' . $mobile . "\n"; | |
$mailbody .= __( 'Arrival:', 'ci_theme' ) . ' ' . $arrive . "\n"; | |
$mailbody .= __( 'Departure:', 'ci_theme' ) . ' ' . $depart . "\n"; | |
$mailbody .= __( 'Adults:', 'ci_theme' ) . ' ' . $guests . "\n"; | |
$mailbody .= __( 'Room:', 'ci_theme' ) . ' ' . $room->post_title . "\n"; | |
$mailbody .= __( 'Message:', 'ci_theme' ) . ' ' . $message . "\n"; | |
// If you want to receive the email using the address of the sender, comment the next $emailSent = ... line | |
// and uncomment the one after it. | |
// Keep in mind the following comment from the wp_mail() function source: | |
/* If we don't have an email from the input headers default to wordpress@$sitename | |
* Some hosts will block outgoing mail from this address if it doesn't exist but | |
* there's no easy alternative. Defaulting to admin_email might appear to be another | |
* option but some hosts may refuse to relay mail from an unknown domain. See | |
* http://trac.wordpress.org/ticket/5007. | |
*/ | |
$emailSent = wp_mail( ci_setting( 'booking_form_email' ), get_option( 'blogname' ) . ' - ' . __( 'Booking form', 'ci_theme' ), $mailbody ); | |
// $emailSent = wp_mail( ci_setting( 'contact_form_email' ), get_option( 'blogname' ) . ' - ' . __( 'Booking form', 'ci_theme' ), $mailbody, 'From: "' . $clientname . '" <' . $email . '>' ); | |
} | |
} | |
?> | |
<?php get_header(); ?> | |
<main id="main"> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-md-12 full"> | |
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | |
<h2 class="page-title"><?php the_title(); ?></h2> | |
<div class="row"> | |
<div class="col-sm-8"> | |
<article <?php post_class( 'entry' ); ?>> | |
<?php if ( ! empty( $errorString ) ): ?> | |
<ul id="formerrors"> | |
<?php echo wp_kses_post( $errorString ); ?> | |
</ul> | |
<?php endif; ?> | |
<?php if ( $emailSent === true ): ?> | |
<p id="formsuccess"> <?php _e( 'Your booking request has been sent. We will contact you as soon as possible.', 'ci_theme' ); ?></p> | |
<?php elseif ( $emailSent === false and isset( $_POST['send_booking'] ) and $errorString == '' ): ?> | |
<p id="sendfail"><?php _e( 'There was a problem while sending the email. Please try again later.', 'ci_theme' ); ?></p> | |
<?php endif; ?> | |
<?php the_content(); ?> | |
<?php if ( ! isset( $_POST['send_booking'] ) or ( isset( $_POST['send_booking'] ) and ! empty( $errorString ) ) ): ?> | |
<form class="booking" action="<?php the_permalink(); ?>" method="post"> | |
<div class="row"> | |
<div class="col-md-6"> | |
<input type="text" name="ci_name" id="ci_name" placeholder="<?php esc_attr_e( 'Name', 'ci_theme' ); ?>" value="<?php echo esc_attr( $clientname ); ?>"> | |
</div> | |
<div class="col-md-6"> | |
<input type="text" name="ci_name2" id="ci_name2" placeholder="<?php esc_attr_e( 'Last Name', 'ci_theme' ); ?>" value="<?php echo esc_attr( $clientname ); ?>"> | |
</div> | |
</div> | |
<div class="col-md-6"> | |
<input type="email" name="ci_email" id="ci_email" class="datepicker" placeholder="<?php esc_attr_e( 'your email', 'ci_theme' ); ?>" value="<?php echo esc_attr( $email ); ?>"> | |
</div> | |
<div class="col-md-6"> | |
<input type="text" name="ci_mobile" id="ci_mobile" placeholder="<?php esc_attr_e( 'Mobile No.', 'ci_theme' ); ?>" value="<?php echo esc_attr( $mobile ); ?>"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-6"> | |
<input type="text" name="arrive" id="arrive" class="datepicker" placeholder="<?php esc_attr_e( 'arrival', 'ci_theme' ); ?>" value="<?php echo esc_attr( $arrive ); ?>"> | |
</div> | |
<div class="col-md-6"> | |
<input type="text" name="depart" id="depart" class="datepicker" placeholder="<?php esc_attr_e( 'departure', 'ci_theme' ); ?>" value="<?php echo esc_attr( $depart ); ?>"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-6"> | |
<select name="adults" id="adults" class="dk"> | |
<option value=""><?php _e( 'adults', 'ci_theme' ); ?></option> | |
<?php for( $i=1; $i <= 6; $i++ ): ?> | |
<option value="<?php echo esc_attr( $i ); ?>" <?php selected( $guests, $i ); ?>><?php echo esc_html( $i ); ?></option> | |
<?php endfor; ?> | |
</select> | |
</div> | |
<div class="col-md-6"> | |
<select name="children" id="children" class="dk"> | |
<option value=""><?php _e( 'children', 'ci_theme' ); ?></option> | |
<?php for( $i=1; $i <= 6; $i++ ): ?> | |
<option value="<?php echo esc_attr( $i ); ?>" <?php selected( $children, $i ); ?>><?php echo esc_html( $i ); ?></option> | |
<?php endfor; ?> | |
</select> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-12"> | |
<?php | |
wp_dropdown_posts( array( | |
'id' => 'room_select', | |
'post_type' => 'cpt_room', | |
'selected' => $room_id, | |
'class' => 'dk' | |
), 'room_select' ); | |
?> | |
<textarea name="ci_comments" id="ci_comments" cols="30" rows="10" placeholder="<?php esc_attr_e( 'comments', 'ci_theme' ); ?>"><?php echo esc_textarea( $message ); ?></textarea> | |
<button type="submit" name="send_booking"><?php _e( 'submit', 'ci_theme' ); ?></button> | |
</div> | |
</div> | |
</form> | |
<?php endif; ?> | |
</article> | |
</div> | |
<?php endwhile; endif; ?> | |
<?php get_sidebar(); ?> | |
</div> | |
</div> | |
</div> | |
</div> | |
</main> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment