File: /mnt/data/dreamsrent-wp-demo/wp-content/plugins/dreamsrent-booking/inc/metabox-new.php
<?php
add_action( 'load-post.php', 'rental_boxes_dayprice_setup' );
add_action( 'load-post-new.php', 'rental_boxes_dayprice_setup' );
function rental_boxes_dayprice_setup() {
add_action( 'add_meta_boxes', 'dreams_rental_dayprice_meta_boxes' );
add_action( 'save_post', 'rental_save_post_day_prices_class_meta', 10, 2 );
}
function dreams_rental_dayprice_meta_boxes() {
add_meta_box(
'rental-post-dayprice-class',
esc_html__( 'Add Price Detail', 'bookingcore' ),
'rental_dayprice_meta_box',
'rental',
'normal',
'default'
);
}
function rental_dayprice_meta_box( $post ) {
wp_nonce_field( basename( __FILE__ ), 'rental_post_dayprice_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__('Day','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" class="form-control general_select" required>
<option value="day">1</option> </select>
</div>
<div class="col-4">
<input type="number" name="dreams_booking_meta_rdprice" placeholder="Day Price" value="<?php echo get_post_meta($post_id, 'dreams_booking_meta_rdprice', true) ?>" >
</div>
</div>
<div class="dayprice_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, '_day_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="day_days[]" class="form-control general_select">';
for ($day = 2; $day <= 31; $day++) {
$selected = ($plocation['day_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="day_price[]" placeholder="' . __("Day Price", 'bookingcore') . '" value="'.$plocation['day_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="day_price_add btn fund-btn skill-add button-secondary" data-post_id="<?php echo $post_id; ?>"> <?php echo __( "Add More", 'bookingcore' ); ?> </button>
</div>
</div> <!-- skills -->
<?php }
function rental_save_post_day_prices_class_meta($post_id, $post) {
/* Verify the nonce before proceeding. */
if (!isset($_POST['rental_post_dayprice_class_nonce']) || !wp_verify_nonce($_POST['rental_post_dayprice_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;
}
// Update or delete the `dreams_booking_meta_rdprice` meta value.
if (isset($_POST['dreams_booking_meta_rdprice']) && !empty($_POST['dreams_booking_meta_rdprice'])) {
update_post_meta($post_id, 'dreams_booking_meta_rdprice', sanitize_text_field($_POST['dreams_booking_meta_rdprice']));
update_post_meta($post_id, 'dreams_booking_meta_rentaltype_day', 'day');
} else {
delete_post_meta($post_id, 'dreams_booking_meta_rdprice');
delete_post_meta($post_id, 'dreams_booking_meta_rentaltype_day');
}
// Update or delete the `_day_days` meta value.
if (isset($_POST['day_days']) && !empty($_POST['day_days'])) {
$day_days = $_POST['day_days'];
$day_price = $_POST['day_price'];
$day_prices = [];
for ($i = 0; $i < count($day_days); $i++) {
$day_prices[] = array(
"day_days" => sanitize_text_field($day_days[$i]),
"day_price" => sanitize_text_field($day_price[$i]),
);
}
$encoded_location = serialize($day_prices);
update_post_meta($post_id, '_day_days', $encoded_location);
} else {
// Store an empty value if `day_days` is null or empty.
update_post_meta($post_id, '_day_days', serialize([]));
}
}
// define the function to be fired for logged in users
add_action("wp_ajax_dreams_get_dayprice_terms", "dreams_get_dayprice_terms");
add_action("wp_ajax_nopriv_dreams_get_dayprice_terms", "dreams_get_dayprice_terms");
function dreams_get_dayprice_terms() {
$post_id = sanitize_text_field($_POST['post_id']);
$plocation_new = unserialize(stripslashes(get_post_meta($post_id, '_day_days', true)));
$html = '';
$html .= '<div class="custom-row">';
// $terms .= '<select name="day_days[]">
// <option value="1"> 1</option>';
// $terms .= '</select>';
// $plocation_new = get_post_meta($post_id, '_day_days', true);
// $data = unserialize($plocation_new);
//$selectedDays = array_column($data, 'day_days');
$terms .= '<select name="day_days[]">';
for ($day = 2; $day <= 31; $day++) {
//if (!in_array($day, $selectedDays)) {
$terms .= '<option value="' . $day . '" ' . $selected . '>' . $day . '</option>';
// }
}
$terms .= '</select>';
$terms2 .= '<input type="text" name="day_price[]" placeholder="Day 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;
}