Skip to content

Instantly share code, notes, and snippets.

@spivurno
Created April 21, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spivurno/fb75c523f86ea306d985ce6b2fa95864 to your computer and use it in GitHub Desktop.
Save spivurno/fb75c523f86ea306d985ce6b2fa95864 to your computer and use it in GitHub Desktop.
WP Sessions // Gravity Forms Out of the Box // Bed & Breakfast Demo
<?php
/**
* WP Sessions // Gravity Forms Out of the Box // Bed & Breakfast Demo
* http://wpsessions.com/sessions/gravity-forms-out-of-the-box/
* http://gravitywiz.com
*/
add_action( 'init', 'register_bnb_room_post_type', 0 );
function register_bnb_room_post_type() {
$labels = array(
'name' => _x( 'Rooms', 'Post Type General Name', 'gw_bnb' ),
'singular_name' => _x( 'Room', 'Post Type Singular Name', 'gw_bnb' ),
'menu_name' => __( 'Rooms', 'gw_bnb' ),
'name_admin_bar' => __( 'Room', 'gw_bnb' ),
'parent_item_colon' => __( '', 'gw_bnb' ),
'all_items' => __( 'All Rooms', 'gw_bnb' ),
'add_new_item' => __( 'Add New Room', 'gw_bnb' ),
'add_new' => __( 'Add New', 'gw_bnb' ),
'new_item' => __( 'New Room', 'gw_bnb' ),
'edit_item' => __( 'Edit Room', 'gw_bnb' ),
'update_item' => __( 'Update Room', 'gw_bnb' ),
'view_item' => __( 'View Room', 'gw_bnb' ),
'search_items' => __( 'Search Room', 'gw_bnb' ),
'not_found' => __( 'Not found', 'gw_bnb' ),
'not_found_in_trash' => __( 'Not found in Trash', 'gw_bnb' ),
);
$args = array(
'label' => __( 'Room', 'gw_bnb' ),
'description' => __( 'A room in our Bed & Breakfast.', 'gw_bnb' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-home',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'rooms' )
);
register_post_type( 'gw_bnb_room', $args );
}
add_filter( 'gform_pre_render_936', 'gw_bnb_populate_rooms' );
function gw_bnb_populate_rooms( $form ) {
foreach( $form['fields'] as &$field ) {
if( $field->id != 1 ) {
continue;
}
$room_id = rgget( 'room_id' );
if( $room_id ) {
$room = get_post( $room_id );
$rooms = array( $room );
} else {
$rooms = get_posts( array(
'post_type' => 'gw_bnb_room',
'post_status' => 'publish'
) );
}
$choices = array();
foreach( $rooms as $room ) {
$choices[] = array(
'text' => gw_bnb_get_room_label( $room ),
'value' => $room->post_title,
'price' => get_post_meta( $room->ID, 'price', true ),
'isSelected' => $room_id == true
);
}
$field->cssClass .= ' gw-bnb-rooms';
$field->choices = $choices;
}
return $form;
}
function gw_bnb_get_room_label( $room ) {
ob_start();
?>
<div id="gw-bnb-room-<?php echo $room->ID; ?>" class="gw-bnb-room">
<div class="gw-bnb-featured-image">
<?php echo get_the_post_thumbnail( $room->ID, 'thumbnail' ); ?>
</div>
<div class="gw-bnb-content">
<h3><?php echo $room->post_title; ?></h3>
<div class="gw-bnb-description"><?php echo $room->post_content; ?></div>
<div class="gw-bnb-details">
<ul>
<li class="gw-bnb-price"><?php echo GFCommon::to_money( get_post_meta( $room->ID, 'price', true ) ); ?> per night</li>
<li class="gw-bnb-occupancy"><?php echo get_post_meta( $room->ID, 'occupancy', true ); ?> people</li>
</ul>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_filter( 'gform_enqueue_scripts_936', 'gw_bnb_enqueue_font_awesome' );
function gw_bnb_enqueue_font_awesome() {
wp_enqueue_style( 'fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css', null, GFCommon::$version );
}
add_filter( 'the_content', 'gw_bnb_append_book_button' );
function gw_bnb_append_book_button( $content ) {
if( is_singular( 'gw_bnb_room' ) ) {
global $post;
$form_url = add_query_arg( array( 'room_id' => $post->ID ), get_permalink( 7738 ) );
$content .= sprintf( '<a href="%s" class="button">%s</a>', $form_url, __( 'Book this Room', 'gw_bnb' ) );
}
return $content;
}
add_filter( 'gform_pre_validation_936', 'gw_bnb_add_range' );
function gw_bnb_add_range( $form ) {
foreach( $form['fields'] as &$field ) {
if( $field->id == 3 ) {
$room_value = rgpost( 'input_1' ); // value like 'Skywalker Suite|250',
$room_values = explode( '|', $room_value ); // splits values into array like array( 'Skywalker Suite', 250 )
$room = get_page_by_title( $room_values[0], 'OBJECT', 'gw_bnb_room' );
$field->rangeMin = 1;
$field->rangeMax = get_post_meta( $room->ID, 'occupancy', true );
}
}
return $form;
}
add_filter( 'gform_pre_render_936', 'gw_bnb_remove_range' );
function gw_bnb_remove_range( $form ) {
foreach( $form['fields'] as &$field ) {
if( $field->id == 3 ) {
$field->rangeMin = null;
$field->rangeMax = null;
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment