File: /mnt/data/dreamsrent-wp-demo/wp-content/plugins/dreamsrent-booking/inc/metabox-hour.php
<?php
add_action( 'load-post.php', 'rental_boxes_hourprice_setup' );
add_action( 'load-post-new.php', 'rental_boxes_hourprice_setup' );
function rental_boxes_hourprice_setup() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'dreams_rental_hourprice_meta_boxes' );
/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'rental_save_post_hour_prices_class_meta', 10, 2 );
}
function dreams_rental_hourprice_meta_boxes() {
add_meta_box(
'rental-post-hourprice-class', // Unique ID
esc_html__( 'Add Hour Price Detail', 'bookingcore' ), // Title
'rental_hourprice_meta_box', // Callback function
'rental',
'normal', // Context
'default' // Priority
);
}
function rental_hourprice_meta_box( $post ) {
wp_nonce_field( basename( __FILE__ ), 'rental_post_hourprice_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__('Hour','bookingcore'); ?></label>
</div>
<div class="col-4">
<label class="mb-0"><?php echo esc_html__('Price','bookingcore'); ?></label>
</div>
</div>
<div class="custom-row">
<div class="col-4">
<select name="dreams_booking_meta_rental_type_hour" class="form-control general_select" required>
<option value="day">1</option> </select>
</div>
<div class="col-4">
<input type="number" name="dreams_booking_meta_rhprice" placeholder="Hour Price" value="<?php echo get_post_meta($post_id, 'dreams_booking_meta_rhprice', true) ?>" >
</div>
</div>
<div class="hourprice_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, '_hour_days', true)));
if(!empty($plocation_new))
{
$plocation_html_new = '';
foreach( $plocation_new as $plocation ) {
$plocation_html_new .= '<div class="custom-row">
<div class="col-4">
<select name="hour_days[]" class="form-control general_select">';
for ($day = 1; $day <= 24; $day++) {
$selected = ($plocation['hour_days'] == $day) ? ' selected' : '';
$plocation_html_new .= '<option value="' . $day . '" ' . $selected . '>' . $day . '</option>';
}
$plocation_html_new .= '</select></div> ';
$plocation_html_new .= '<div class="col-4"><input type="number" name="hour_price[]" placeholder="' . __("Day Price", 'bookingcore') . '" value="'.$plocation['hour_price'].'" required=""></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="hour_price_add btn fund-btn skill-add button-secondary" data-post_id="<?php echo $post_id; ?>"> <?php echo __( "Add More", 'bookingcore' ); ?> </button>
</div>
</div>
<?php }
function rental_save_post_hour_prices_class_meta( $post_id, $post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['rental_post_hourprice_class_nonce'] ) || !wp_verify_nonce( $_POST['rental_post_hourprice_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['dreams_booking_meta_rhprice']))
// {
// update_post_meta( $post_id, 'dreams_booking_meta_rhprice', $_POST['dreams_booking_meta_rhprice'] );
// update_post_meta( $post_id, 'dreams_booking_meta_rentaltype_hour', 'hour' );
// }
if (isset($_POST['dreams_booking_meta_rhprice']) && !empty($_POST['dreams_booking_meta_rhprice'])) {
update_post_meta($post_id, 'dreams_booking_meta_rhprice', sanitize_text_field($_POST['dreams_booking_meta_rhprice']));
update_post_meta($post_id, 'dreams_booking_meta_rentaltype_hour', 'hour');
} else {
delete_post_meta($post_id, 'dreams_booking_meta_rhprice');
delete_post_meta($post_id, 'dreams_booking_meta_rentaltype_hour');
}
if(isset($_POST['hour_days']))
{
$hour_days = $_POST['hour_days'];
$hour_price = $_POST['hour_price'];
$integerIDs = array_map('intval', $_POST['hour_days']);
$integerIDs = array_unique($integerIDs);
for($i=0; $i<count($hour_days); $i++)
{
$hour_days_insert = $hour_days[$i];
$hour_price_insert = $hour_price[$i];
$hour_prices[] = array(
"hour_days" => $hour_days_insert,
"hour_price" =>$hour_price_insert,
);
}
// $encoded_location = json_encode($plocation_arr, JSON_UNESCAPED_UNICODE);
$encoded_location = serialize($hour_prices);
update_post_meta( $post_id, '_hour_days', $encoded_location );
}
}
// define the function to be fired for logged in users
add_action("wp_ajax_dreams_get_hourprice_terms", "dreams_get_hourprice_terms");
add_action("wp_ajax_nopriv_dreams_get_hourprice_terms", "dreams_get_hourprice_terms");
function dreams_get_hourprice_terms() {
$post_id = sanitize_text_field($_POST['post_id']);
$plocation_new = unserialize(stripslashes(get_post_meta($post_id, '_hour_days', true)));
$html = '';
$html .= '<div class="custom-row">';
// $terms .= '<select name="hour_days[]">
// <option value="1"> 1</option>';
// $terms .= '</select>';
// $plocation_new = get_post_meta($post_id, '_hour_days', true);
// $data = unserialize($plocation_new);
// $selectedDays = array_column($data, 'hour_days');
$terms .= '<select name="hour_days[]">';
for ($day = 1; $day <= 24; $day++) {
// if (!in_array($day, $selectedDays)) {
$terms .= '<option value="' . $day . '" ' . $selected . '>' . $day . '</option>';
// }
}
$terms .= '</select>';
$terms2 .= '<input type="text" name="hour_price[]" placeholder="Hour Price" required> ';
$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;
}