HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dreamsrent-wp/wp-content/plugins/dreamsrent-booking/inc/metabox-season.php
<?php

add_action('load-post.php', 'rental_boxes_pricing_setup');
add_action('load-post-new.php', 'rental_boxes_pricing_setup');

function rental_boxes_pricing_setup() {
    add_action('add_meta_boxes', 'custom_rental_pricing_meta_boxes');
    add_action('save_post', 'save_rental_pricing_meta', 10, 2);
}

function custom_rental_pricing_meta_boxes() {
    add_meta_box(
        'rental-post-pricing-class',
        esc_html__('Add Seasonal Rental Pricing Details', 'bookingcore'),
        'rental_pricing_meta_box',
        'rental',
        'normal',
        'default'
    );
}

function rental_pricing_meta_box($post) {
    wp_nonce_field(basename(__FILE__), 'rental_post_pricing_class_nonce');
    $post_id = get_the_ID();
    
    $pricing_data = get_post_meta($post_id, '_seasonal_rental_pricing_data', true);
    $pricing_data = !empty($pricing_data) ? $pricing_data : array();
    ?>

    <div class="pricing-wrapper">
        <div class="custom-row">
            <div class="col-4">
                <label class="mb-0"><?php echo esc_html__('Select Type', 'bookingcore'); ?></label>
            </div>
            <div class="col-4">
                <label class="mb-0"><?php echo esc_html__('Start Date', 'bookingcore'); ?></label>
            </div>
            <div class="col-4">
                <label class="mb-0"><?php echo esc_html__('End Date', 'bookingcore'); ?></label>
            </div>
            <div class="col-4">
                <label class="mb-0"><?php echo esc_html__('Seasonal Price', 'bookingcore'); ?></label>
            </div>
        </div>

        <div class="additional-pricing-wrapper">
            <?php if (!empty($pricing_data)): ?>
                <?php foreach ($pricing_data as $pricing_row): $today = date('Y-m-d'); ?>
                    <div class="custom-row pricing-fields">
                        <div class="col-4">
                            <select name="seasonal_pricing_type[]" class="form-control">
                                <option value="day" <?php selected($pricing_row['seasonal_pricing_type'], 'day'); ?>>Day</option>
                                <!-- <option value="hour" <?php selected($pricing_row['seasonal_pricing_type'], 'hour'); ?>>Hour</option> -->
                            </select>
                        </div>
                        <div class="col-4">
                        <input type="date" name="seasonal_start_date[]" class="form-control" value="<?php echo esc_attr($pricing_row['seasonal_start_date']); ?>" min="<?php echo esc_attr($today); ?>" >
                        </div>
                        <div class="col-4">
                            <input type="date" name="seasonal_end_date[]" class="form-control" value="<?php echo esc_attr($pricing_row['seasonal_end_date']); ?>" min="<?php echo esc_attr($today); ?>" >
                        </div>
                        <div class="col-4">
                            <input type="number" name="seasonal_price[]" class="form-control" value="<?php echo esc_attr($pricing_row['seasonal_price']); ?>" placeholder="Enter seasonal price" >
                        </div>
                        <div class="col-4 remove_button_wrap">
                            <a href="javascript:void(0);" class="remove_button button-secondary">Remove</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <button class="add_pricing_row btn button-secondary"><?php echo esc_html__('Add More', 'bookingcore'); ?></button>
    </div>

    <script type="text/javascript">
    jQuery(document).ready(function($) {
        // Function to get today's date in 'YYYY-MM-DD' format
        function getTodayDate() {
            var today = new Date();
            var day = String(today.getDate()).padStart(2, '0');
            var month = String(today.getMonth() + 1).padStart(2, '0'); // January is 0
            var year = today.getFullYear();
            return year + '-' + month + '-' + day;
        }

        $('.add_pricing_row').on('click', function(e) {
            e.preventDefault();

            var todayDate = getTodayDate(); // Get today's date

            var newRow = `
                <div class="custom-row pricing-fields">
                    <div class="col-4">
                        <select name="seasonal_pricing_type[]" class="form-control">
                            <option value="day">Day</option>
                        </select>
                    </div>
                    <div class="col-4">
                        <input type="date" name="seasonal_start_date[]" class="form-control" min="${todayDate}" >
                    </div>
                    <div class="col-4">
                        <input type="date" name="seasonal_end_date[]" class="form-control" min="${todayDate}" >
                    </div>
                    <div class="col-4">
                        <input type="number" name="seasonal_price[]" class="form-control" placeholder="Enter seasonal price">
                    </div>
                    <div class="col-4 remove_button_wrap">
                        <a href="javascript:void(0);" class="remove_button button-secondary">Remove</a>
                    </div>
                </div>
            `;

            $('.additional-pricing-wrapper').append(newRow);
        });

        $(document).on('click', '.remove_button', function() {
            $(this).closest('.pricing-fields').remove();
        });
    });
</script>

    <?php
}

function save_rental_pricing_meta($post_id, $post) {
    if (!isset($_POST['rental_post_pricing_class_nonce']) || !wp_verify_nonce($_POST['rental_post_pricing_class_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    if (isset($_POST['seasonal_pricing_type']) && isset($_POST['seasonal_start_date']) && isset($_POST['seasonal_end_date']) && isset($_POST['seasonal_price'])) {
        $pricing_data = array();
        $seasonal_pricing_type = $_POST['seasonal_pricing_type'];
        $seasonal_start_date = $_POST['seasonal_start_date'];
        $seasonal_end_date = $_POST['seasonal_end_date'];
        $seasonal_price = $_POST['seasonal_price'];

        for ($i = 0; $i < count($seasonal_pricing_type); $i++) {
            $pricing_data[] = array(
                'seasonal_pricing_type' => sanitize_text_field($seasonal_pricing_type[$i]),
                'seasonal_start_date' => sanitize_text_field($seasonal_start_date[$i]),
                'seasonal_end_date' => sanitize_text_field($seasonal_end_date[$i]),
                'seasonal_price' => sanitize_text_field($seasonal_price[$i]),
            );
        }

        update_post_meta($post_id, '_seasonal_rental_pricing_data', $pricing_data);
    }
}