File: /mnt/data/dreamsrent-wp-demo/wp-content/plugins/dreamsrent-booking/inc/metabox.php
<?php
/**
* Include and setup custom metaboxes and fields. (make sure you copy this file to outside the CMB2 directory)
*
* Be sure to replace all instances of 'yourprefix_' with your project's prefix.
* http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
*
* @category YourThemeOrPlugin
* @package Demo_CMB2
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/WebDevStudios/CMB2
*/
add_action( 'load-post.php', 'rental_boxes_setup' );
add_action( 'load-post-new.php', 'rental_boxes_setup' );
add_action( 'cmb2_init', 'dreams_booking_metaboxes_default' );
function rental_boxes_setup() {
add_action( 'add_meta_boxes', 'dreams_insurance_meta_boxes' );
add_action('save_post', 'insurance_save_post_class_meta');
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'dreams_rental_meta_boxes' );
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'rental_save_post_class_meta', 10, 2 );
}
function dreams_insurance_meta_boxes() {
add_meta_box(
'insurance-post-class', // Unique ID
esc_html__( 'Add Insurance Details', 'bookingcore' ), // Title
'insurance_meta_box', // Callback function
'rental', // Post type
'normal', // Context
'default' // Priority
);
}
function insurance_meta_box( $post ) {
wp_nonce_field( basename( __FILE__ ), 'insurance_post_class_nonce' );
$post_id = get_the_ID(); ?>
<div class="custom-row">
<div class="col-12">
<div class="insurance_wrapper">
<?php
$insurance_data = unserialize(stripslashes(get_post_meta($post_id, '_insurance_details', true)));
if (!empty($insurance_data)) {
foreach ($insurance_data as $insurance) {
?>
<div class="custom-row">
<div class="col-4">
<input type="text" name="insurance_name[]" class="form-control" placeholder="<?php echo esc_attr__('Insurance Name', 'bookingcore'); ?>" value="<?php echo esc_attr($insurance['insurance_name']); ?>">
</div>
<div class="col-4">
<input type="number" name="insurance_price[]" class="form-control" placeholder="<?php echo esc_attr__('Price', 'bookingcore'); ?>" value="<?php echo esc_attr($insurance['insurance_price']); ?>">
</div>
<div class="col-4">
<select name="insurance_type[]" class="form-control">
<option value="fixed" <?php selected($insurance['insurance_type'], 'fixed'); ?>><?php esc_html_e('Fixed', 'bookingcore'); ?></option>
<option value="day" <?php selected($insurance['insurance_type'], 'day'); ?>><?php esc_html_e('Per Day', 'bookingcore'); ?></option>
</select>
</div>
<div class="remove_button_wrap">
<a href="javascript:void(0);" class="remove_button button-secondary"><?php esc_html_e('Remove', 'bookingcore'); ?></a>
</div>
</div>
<?php
}
}
?>
</div>
<button type="button" class="add_new_insurance btn fund-btn skill-add button-secondary">Add More</button>
</div>
</div>
<?php
}
function insurance_save_post_class_meta($post_id) {
/* Verify the nonce before proceeding. */
if (!isset($_POST['insurance_post_class_nonce']) || !wp_verify_nonce($_POST['insurance_post_class_nonce'], basename(__FILE__))) {
return $post_id;
}
// Check if this is an autosave.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check if the current user has permission to edit the post.
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// If fields are empty, update the meta with an empty serialized value
if (!isset($_POST['insurance_name']) || empty($_POST['insurance_name'])) {
update_post_meta($post_id, '_insurance_details', serialize([]));
return $post_id;
}
// Process submitted fields if they exist
$insurance_name = $_POST['insurance_name'];
$insurance_price = $_POST['insurance_price'];
$insurance_type = $_POST['insurance_type'];
$insurance_data = [];
for ($i = 0; $i < count($insurance_name); $i++) {
$insurance_data[] = array(
"insurance_name" => sanitize_text_field($insurance_name[$i]),
"insurance_price" => floatval($insurance_price[$i]),
"insurance_type" => sanitize_text_field($insurance_type[$i]),
);
}
$encoded_insurance_data = serialize($insurance_data);
update_post_meta($post_id, '_insurance_details', $encoded_insurance_data);
}
function dreams_rental_meta_boxes() {
add_meta_box(
'rental-post-class', // Unique ID
esc_html__( 'Add Location Detail', 'bookingcore' ), // Title
'rental_meta_box', // Callback function
'rental',
'normal', // Context
'default' // Priority
);
}
function rental_meta_box( $post ) {
wp_nonce_field( basename( __FILE__ ), 'rental_post_class_nonce' );
$post_id = get_the_ID();?>
<div class="custom-row">
<div class="col-12">
<div class="custom-row">
<div class="col-4">
<label class="mb-0"><?php echo esc_html__('Pick-up Location','bookingcore'); ?></label>
</div>
<div class="col-4">
<label class="mb-0"><?php echo esc_html__('Drop-off Location','bookingcore'); ?></label>
</div>
</div>
<div class="location_wrapper ">
<?php
//$plocation_new = json_decode(stripslashes(get_post_meta($post_id, '_rent_locations', true)), true);
$plocation_new = unserialize(stripslashes(get_post_meta($post_id, '_rent_locations', true)));
if(!empty($plocation_new))
{
$plocation_html_new = '';
$plocation_taxonomies = dreams_get_terms('location');
foreach( $plocation_new as $plocation ) {
$plocation_html_new .= '<div class="custom-row"><div class="col-4"><select name="plocation[]" class="form-control general_select">';
foreach( $plocation_taxonomies as $plocation_taxonomy ) {
if($plocation_taxonomy->term_id == $plocation['plocation']){ $selected = 'selected ="selected"';}else{$selected = ''; }
if( $plocation_taxonomy->parent == 0 ) {
$plocation_html_new .= '<option value="'. esc_attr( $plocation_taxonomy->term_id ) .'" '.$selected.'>
'. esc_html( $plocation_taxonomy->name ) .'</option>';
}
}
$plocation_html_new .= '</select></div>';
$plocation_html_new .= '<div class="col-4"><select name="dlocation[]" class="form-control general_select">';
foreach( $plocation_taxonomies as $plocation_taxonomy ) {
if($plocation_taxonomy->term_id == $plocation['dlocation']){ $selected = 'selected ="selected"';}else{$selected = ''; }
if( $plocation_taxonomy->parent == 0 ) {
$plocation_html_new .= '<option value="'. esc_attr( $plocation_taxonomy->term_id ) .'" '.$selected.'>
'. esc_html( $plocation_taxonomy->name ) .'</option>';
}
}
$plocation_html_new .= '</select></div>';
// $plocation_html_new .= '<div class="col-4"><input type="number" name="location_price[]" placeholder="' . __("location Price", 'bookingcore') . '" value="'.$plocation['location_price'].'"></div>';
$plocation_html_new .= '<div class="remove_button_wrap"><a href="javascript:void(0);" class="remove_button button-secondary">'. __("Remove", "bookingcore").'</a></div></div>';
}
echo $plocation_html_new;
} ?>
</div>
<button class="add_new_location btn fund-btn skill-add button-secondary" data-taxonomy-name="location"><?php echo __( "Add More", 'bookingcore' ); ?> </button>
</div>
</div> <!-- skills -->
<?php }
function rental_save_post_class_meta( $post_id, $post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['rental_post_class_nonce'] ) || !wp_verify_nonce( $_POST['rental_post_class_nonce'], basename( __FILE__ ) ) ) {
return $post_id;
}
// Check if this is an autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check if this is a revision.
if ( 'product' === $post->post_type ) {
return $post_id;
}
// Check if the current user has permission to edit the post.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// if ( isset( $_POST['location_price'] ) ) {
// update_post_meta( $post_id, '_location_price', sanitize_text_field( $_POST['location_price'] ) );
// }
if(isset($_POST['plocation']))
{
$plocation = $_POST['plocation'];
$dlocation = $_POST['dlocation'];
$location_price = $_POST['location_price'];
$integerIDs = array_map('intval', $_POST['plocation']);
$integerIDs = array_unique($integerIDs);
for($i=0; $i<count($plocation); $i++)
{
$plocation_insert = $plocation[$i];
$dlocation_insert = $dlocation[$i];
$location_price_insert = $location_price[$i];
$plocation_arr[] = array(
"plocation" => $plocation_insert,
"dlocation" =>$dlocation_insert,
"location_price" =>$location_price_insert,
);
}
// $encoded_location = json_encode($plocation_arr, JSON_UNESCAPED_UNICODE);
$encoded_location = serialize($plocation_arr);
wp_set_post_terms( $post_id, $integerIDs, 'locations', false );
update_post_meta( $post_id, '_rent_locations', $encoded_location );
}
}
function dreams_get_terms($term_name = '', $hide_empty = false)
{
$terms = get_terms( array(
'taxonomy' => $term_name,
'hide_empty' => $hide_empty,
'orderby' => 'name',
) );
return $terms;
}
// define the function to be fired for logged in users
add_action("wp_ajax_dreams_get_my_terms", "dreams_get_my_terms");
add_action("wp_ajax_nopriv_dreams_get_my_terms", "dreams_get_my_terms");
function dreams_get_my_terms() {
//echo $_POST['tax_name'];
$tax_terms = dreams_get_terms(sanitize_text_field($_POST['tax_name']));
$html = '';
$html .= '<div class="custom-row">';
$terms .= '<select name="plocation[]">';
foreach( $tax_terms as $tax_term ) {
if( $tax_term->parent == 0 ) {
$terms .= '<option value="'. esc_attr( $tax_term->term_id ) .'">'. esc_html( $tax_term->name ) .'</option>';
}
}
$terms .= '</select>';
$terms2 .= '<select name="dlocation[]">';
foreach( $tax_terms as $tax_term ) {
if( $tax_term->parent == 0 ) {
$terms2 .= '<option value="'. esc_attr( $tax_term->term_id ) .'">'. esc_html( $tax_term->name ) .'</option>';
}
}
$terms2 .= '</select>';
$html .= '<div class="col-4">' . $terms . '</div>';
$html .= '<div class="col-4">' . $terms2 . '</div>';
?>
</div> <?php
$html .= '<div class="remove_location"> <a href="javascript:void(0);" class="remove_button button-secondary">' . __("Remove", 'bookingcore') . '</a></div>';
$html .= '</div>';
echo $html;
die;
}
function dreams_booking_metaboxes_default() {
$prefix = 'dreams_booking_meta_';
$cmb_new = new_cmb2_box( array(
'id' => 'multicar_settings',
'title' => esc_html__( 'Multiple Car Settings', 'bookingcore' ),
'object_types' => array( 'rental'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$field_multicar = $cmb_new->add_field( array(
'name' => 'Quantity',
'desc' => 'Enter a quantity (numbers only)',
'id' => $prefix . 'multicar_settings',
'type' => 'text',
'default' => '1',
'attributes' => array(
'type' => 'number',
'min' => '1',
'step' => '1',
),
) );
/* Rental Settings ***************************************************************************/
/* ************************************************************************************/
$cmb_new = new_cmb2_box( array(
'id' => 'maintain_settings',
'title' => esc_html__( 'Maintenance Settings', 'bookingcore' ),
'object_types' => array( 'rental'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$field_main = $cmb_new->add_field( array(
'name' => 'Maintain type',
'desc' => 'Select an option',
'id' => $prefix . 'maintain_type',
'type' => 'select',
'show_option_none' => false,
'default' => 'day',
'options' => array(
'no' => __( 'No', 'bookingcore' ),
'yes' => __( 'Yes', 'bookingcore' ),
),
) );
$cmb_new = new_cmb2_box( array(
'id' => 'rental_settings',
'title' => esc_html__( 'Rental Settings', 'bookingcore' ),
'object_types' => array( 'rental'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
// $field1 = $cmb_new->add_field( array(
// 'name' => 'Rental type',
// 'desc' => 'Select an option',
// 'id' => $prefix . 'rental_type',
// 'type' => 'select',
// 'show_option_none' => false,
// 'default' => 'day',
// 'options' => array(
// 'day' => __( 'Per Day', 'bookingcore' ),
// // 'hour' => __( 'Any Hour', 'bookingcore' ),
// ),
// ) );
// // Display title of page
// $field2 = $cmb_new->add_field( array(
// 'name' => esc_html__( 'Price' , 'bookingcore'),
// //'description' => esc_html( 'It is Unique, No Special Character, Space' ),
// 'id' => $prefix . 'rdprice_old',
// 'type' => 'text',
// 'show_option_none' => false,
// ) );
$field_gall = $cmb_new->add_field( array(
'name' => 'Gallery',
'desc' => '',
'id' => $prefix . 'id_gallery',
'type' => 'file_list',
// 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
// 'query_args' => array( 'type' => 'image' ), // Only images attachment
// Optional, override default text strings
'text' => array(
'add_upload_files_text' => 'Upload Files', // default: "Add or Upload Files"
'remove_image_text' => 'Remove Image', // default: "Remove Image"
'file_text' => 'File', // default: "File:"
'file_download_text' => 'Download', // default: "Download"
'remove_text' => 'Remove ', // default: "Remove"
),
) );
// $field4 = $cmb_new->add_field( array(
// 'name' => esc_html__( 'Amount of insurance', 'bookingcore'),
// //'description' => esc_html__( 'It is Unique, No Special Character, Space' ),
// 'id' => $prefix . 'amount_insurance',
// 'type' => 'text',
// 'show_option_none' => false,
// 'attributes' => array(
// 'required' => 'required',
// ),
// 'default' => '0',
// ) );
$field_year = $cmb_new->add_field( array(
'name' => esc_html__( 'Make Year', 'bookingcore' ),
//'description' => esc_html__( 'It is Unique, No Special Character, Space' ),
'id' => $prefix . 'make_year',
'type' => 'text',
'show_option_none' => false,
) );
$field5 = $cmb_new->add_field( array(
'name' => esc_html__( 'Youtute Link', 'bookingcore' ),
//'description' => esc_html__( 'It is Unique, No Special Character, Space' ),
'id' => $prefix . 'video_link',
'attributes' => array(
'placeholder' => 'https://www.youtube.com/embed/ExJZAegsOis',
'rows' => 3,
),
'description' => esc_html__( 'Insert youtube link like: https://www.youtube.com/embed/ExJZAegsOis', 'bookingcore'),
'type' => 'text',
'show_option_none' => false,
) );
$field6 = $cmb_new->add_field( array(
'name' => 'Enable deposit',
'desc' => 'Select an option',
'id' => $prefix . 'enable_deposit',
'type' => 'select',
'default' => 'yes',
'options' => array(
'no' => esc_html__( 'No', 'bookingcore'),
'yes' => esc_html__( 'Yes', 'bookingcore'),
'yeswithfull' => esc_html__( 'Yes with full amount', 'bookingcore'),
)
) );
$field7 = $cmb_new->add_field( array(
'name' => 'Type deposit',
'desc' => 'Select an option',
'id' => $prefix . 'type_deposit',
'type' => 'select',
'default' => 'percent',
'attributes' => array( 'class' => 'select short' ),
'options' => array(
'percent' => esc_html__( 'Percentage of price', 'bookingcore'),
'value' => esc_html__( 'Fixed value', 'bookingcore'),
)
) );
$field8 = $cmb_new->add_field( array(
'name' => esc_html__( 'Deposit amount', 'bookingcore' ),
//'description' => esc_html__( 'It is Unique, No Special Character, Space' ),
'id' => $prefix . 'amount_deposit',
'type' => 'text',
'show_option_none' => false,
) );
$field9 = $cmb_new->add_field( array(
'name' => esc_html__( 'Enable full payment option', 'bookingcore' ),
'id' => $prefix . 'enable_full_payment',
'type' => 'select',
'default' => 'percent',
'options' => array(
'yes' => esc_html__( 'Yes', 'bookingcore'),
'no' => esc_html__( 'No', 'bookingcore'),
)
) );
$field10 = $cmb_new->add_field( array(
'name' => esc_html__( 'Cleaning Charge', 'bookingcore' ),
'id' => $prefix . 'cleaning_charge',
'type' => 'text_small',
'attributes' => array(
'type' => 'number',
'required' => 'required', // Add this line to make the field mandatory
),
'show_option_none' => false,
) );
$field11 = $cmb_new->add_field( array(
'name' => esc_html__( 'Booking Notes', 'bookingcore' ),
//'description' => esc_html__( 'It is Unique, No Special Character, Space' ),
'id' => $prefix . 'booking_notes',
'type' => 'textarea',
'show_option_none' => false,
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_new->add_field(array
(
'id' => $prefix.'specifications',
'type' => 'group',
'options' => array
(
'group_title' => __('Specifications {#}', 'bookingcore'), // {#} gets replaced by row number
'add_button' => __( 'Add Another Specifications', 'bookingcore' ),
'remove_button' => __( 'Remove Specifications', 'bookingcore' ),
'sortable' => true,
),
));
$gField_sp1 = $cmb_new->add_group_field($group_field_id, array
(
'name' => 'Title',
'id' => $prefix.'sp_title',
'default' => 'hours',
'type' => 'select',
'attributes' => array( 'class' => 'col-md-5' ),
'options' => array(
'Gear Type' => esc_html__( 'Gear Type', 'bookingcore'),
'Mileage' => esc_html__( 'Mileage', 'bookingcore'),
'Fuel' => esc_html__( 'Fuel', 'bookingcore'),
'Steering' => esc_html__( 'Steering', 'bookingcore'),
'Model' => esc_html__( 'Model', 'bookingcore'),
'Capacity' => esc_html__( 'Capacity', 'bookingcore'),
'Engine' => esc_html__( 'Engine', 'bookingcore'),
)
));
$gField_sp2 = $cmb_new->add_group_field($group_field_id, array
(
'name' => 'Description',
'id' => $prefix.'sp_description',
'type' => 'text',
));
$cmb_new->add_field( array(
'id' => $prefix.'car_fetures',
'type' => 'group',
'options' => array(
'group_title' => 'Car Feature {#}', // No group title
'add_button' => __( 'Add Another Feature', 'bookingcore' ),
'remove_button' => __( 'Remove Feature', 'bookingcore' ),
'sortable' => true, // Enable sorting
),
'fields' => array(
array(
'id' => $prefix.'car_feture_list',
'type' => 'text',
'column' => '12', // Full width
),
),
'repeatable' => true, // Make the group repeatable
'closed' => true, // Group starts closed
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_new->add_field(array
(
'id' => $prefix . 'extra_services',
'type' => 'group',
'options' => array
(
'group_title' => 'Extra Service {#}', // No group title
'add_button' => __( 'Add Another Extra Service', 'bookingcore' ),
'remove_button' => __( 'Remove Extra Service', 'bookingcore' ),
'sortable' => true, // Enable sorting
'show_names' => false, // Hide field names by default
),
'repeatable' => true, // Prevents the display of 1 row by default
));
// $gField7 = $cmb_new->add_group_field($group_field_id, array(
// 'name' => 'Price Type',
// 'id' => $prefix . 'es_type',
// 'type' => 'select',
// 'options' => array(
// 'day' => 'Per Day',
// 'fixed' => 'Fixed Price',
// ),
// ));
$gField4 = $cmb_new->add_group_field($group_field_id, array
(
'name' => 'Unique Id (Ex: _baby_seat)',
'id' => $prefix.'es_uid',
'type' => 'text',
// 'default' => 'unique_id',
// 'attributes' => array(
// 'required' => 'required',
// ),
));
$gField5 = $cmb_new->add_group_field($group_field_id, array
(
'name' => 'Name',
'id' => $prefix.'es_name',
'type' => 'text',
));
$gField6 = $cmb_new->add_group_field($group_field_id, array
(
'name' => 'Price',
'id' => $prefix.'es_price',
'type' => 'text',
));
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
// $group_field_id = $cmb_new->add_field(array
// (
// 'id' => $prefix.'faq',
// 'type' => 'group',
// 'options' => array
// (
// 'group_title' => __('FAQ {#}', 'bookingcore'), // {#} gets replaced by row number
// 'add_button' => __( 'Add Another faq', 'bookingcore' ),
// 'remove_button' => __( 'Remove faq', 'bookingcore' ),
// 'sortable' => true,
// ),
// ));
// $gField_faq1 = $cmb_new->add_group_field($group_field_id, array
// (
// 'name' => 'Title',
// 'id' => $prefix.'faq_title',
// 'type' => 'text',
// ));
// $gField_faq2 = $cmb_new->add_group_field($group_field_id, array
// (
// 'name' => 'Description',
// 'id' => $prefix.'faq_description',
// 'type' => 'text',
// ));
if(!is_admin()){
return;
}
$cmb_2Grid = new \Cmb2Grid\Grid\Cmb2Grid($cmb_new);
$row = $cmb_2Grid->addRow();
//$row->addColumns(array($field1, $field2, $field3));
$row->addColumns(array(
// array($field_main, 'class' => 'col-md-6'),
//array($field1, 'class' => 'col-md-6'),
//array($field2, 'class' => 'col-md-6'),
array($field_gall, 'class' => 'col-md-12'),
// array($field4, 'class' => 'col-md-4'),
array($field_year, 'class' => 'col-md-6'),
array($field5, 'class' => 'col-md-6'),
array($field6, 'class' => 'col-md-4'),
array($field7, 'class' => 'col-md-4'),
array($field8, 'class' => 'col-md-4'),
array($field9, 'class' => 'col-md-5'),
array($field10, 'class' => 'col-md-3'),
array($field11, 'class' => 'col-md-4'),
));
$cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid($cmb_new);
// Create now a Grid of group fields
$cmb2GroupGrid = $cmb2Grid->addCmb2GroupGrid($group_field_id);
// Create now a Grid of group fields
$row = $cmb2GroupGrid->addRow();
//$row->addColumns(array($gField7,$gField4, $gField5, $gField6));
$row->addColumns(array($gField4, $gField5, $gField6));
$row = $cmb2GroupGrid->addRow();
$row->addColumns(array($gField_sp1, $gField_sp2));
}