<?php
/**
 * Odysseo Core — Editor meta boxes & term fields.
 *
 * Destination details, State term image/tagline, Tour Package details,
 * Fleet details and Testimonial details — plus their sanitised save handlers.
 *
 * @package    OdysseoCore
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Destination meta box.
 */
function odysseo_destination_meta_box() {
    add_meta_box( 'destination_details', __( 'Destination Details', 'odysseo-core' ), 'odysseo_destination_meta_callback', 'destination', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'odysseo_destination_meta_box' );

/**
 * Renders the Destination details meta box.
 *
 * @param WP_Post $post Current post.
 */
function odysseo_destination_meta_callback( $post ) {
    wp_nonce_field( 'odysseo_destination_meta', 'odysseo_destination_nonce' );

    $tagline     = get_post_meta( $post->ID, '_dest_tagline', true );
    $best_time   = get_post_meta( $post->ID, '_dest_best_time', true );
    $duration    = get_post_meta( $post->ID, '_dest_duration', true );
    $how_reach   = get_post_meta( $post->ID, '_dest_how_reach', true );
    $attractions = get_post_meta( $post->ID, '_dest_attractions', true );
    $things      = get_post_meta( $post->ID, '_dest_things', true );
    $image_url   = get_post_meta( $post->ID, '_dest_image_url', true );
    ?>
    <p>
        <label><strong><?php esc_html_e( 'Tagline', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="dest_tagline" value="<?php echo esc_attr( $tagline ); ?>" style="width:100%" placeholder="e.g. The Coastal Gem">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Best Time to Visit', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="dest_best_time" value="<?php echo esc_attr( $best_time ); ?>" style="width:100%" placeholder="e.g. October to March">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Ideal Duration', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="dest_duration" value="<?php echo esc_attr( $duration ); ?>" style="width:100%" placeholder="e.g. 2–3 days">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'How to Reach', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="dest_how_reach" value="<?php echo esc_attr( $how_reach ); ?>" style="width:100%" placeholder="e.g. 120 km from the capital · Nearest airport: XYZ">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Top Attractions (one per line)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="dest_attractions" rows="6" style="width:100%" placeholder="Old Town Square&#10;Hilltop Fort&#10;Riverside Promenade"><?php echo esc_textarea( $attractions ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Things to Do (one per line)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="dest_things" rows="5" style="width:100%" placeholder="Guided heritage walk&#10;Shop at the local market"><?php echo esc_textarea( $things ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Hero Image (used if no Featured Image)', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="url" name="dest_image_url" id="dest_image_url" value="<?php echo esc_attr( $image_url ); ?>" style="width:74%" placeholder="<?php esc_attr_e( 'https://… or use the button', 'odysseo-core' ); ?>">
        <button type="button" class="button odysseo-media-btn" data-target="dest_image_url" data-title="<?php esc_attr_e( 'Select hero image', 'odysseo-core' ); ?>"><?php esc_html_e( 'Select / Upload', 'odysseo-core' ); ?></button>
        <button type="button" class="button odysseo-media-clear" data-target="dest_image_url"><?php esc_html_e( 'Clear', 'odysseo-core' ); ?></button>
        <br><img id="dest_image_url_preview" src="<?php echo esc_url( $image_url ); ?>" alt="" style="max-width:200px;height:auto;margin-top:8px;border-radius:6px;<?php echo $image_url ? '' : 'display:none;'; ?>">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Gallery Images', 'odysseo-core' ); ?>:</strong></label>
        <a href="#" class="odysseo-gallery-toggle" data-target="dest_gallery"><?php esc_html_e( 'Edit URLs', 'odysseo-core' ); ?></a><br>
        <button type="button" class="button odysseo-media-btn" data-target="dest_gallery" data-multiple="1" data-title="<?php esc_attr_e( 'Add gallery images', 'odysseo-core' ); ?>"><?php esc_html_e( 'Add from Library', 'odysseo-core' ); ?></button>
        <div class="odysseo-gallery-grid" data-target="dest_gallery"></div>
        <textarea name="dest_gallery" id="dest_gallery" rows="4" style="width:100%;display:none;" placeholder="<?php esc_attr_e( 'One image URL per line', 'odysseo-core' ); ?>"><?php echo esc_textarea( get_post_meta( $post->ID, '_dest_gallery', true ) ); ?></textarea>
    </p>
    <?php
}

/**
 * Saves the Destination details meta box.
 *
 * @param int $post_id Post ID.
 */
function odysseo_destination_save_meta( $post_id ) {
    if ( ! isset( $_POST['odysseo_destination_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['odysseo_destination_nonce'] ) ), 'odysseo_destination_meta' ) ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;

    $fields = array(
        'dest_tagline'   => 'sanitize_text_field',
        'dest_best_time' => 'sanitize_text_field',
        'dest_duration'  => 'sanitize_text_field',
        'dest_how_reach' => 'sanitize_text_field',
        'dest_attractions' => 'sanitize_textarea_field',
        'dest_things'      => 'sanitize_textarea_field',
        'dest_image_url'   => 'esc_url_raw',
        'dest_gallery'     => 'sanitize_textarea_field',
    );
    foreach ( $fields as $field => $sanitizer ) {
        if ( isset( $_POST[ $field ] ) ) {
            update_post_meta( $post_id, '_' . $field, call_user_func( $sanitizer, wp_unslash( $_POST[ $field ] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Value is sanitized via the field's whitelisted $sanitizer callback.
        }
    }
}
add_action( 'save_post_destination', 'odysseo_destination_save_meta' );

/**
 * State (taxonomy) custom term meta — image + tagline.
 */
function odysseo_state_term_add_fields( $taxonomy ) { ?>
    <div class="form-field">
        <label for="state_tagline"><?php esc_html_e( 'Tagline', 'odysseo-core' ); ?></label>
        <input type="text" name="state_tagline" id="state_tagline" value="" placeholder="e.g. Land of Kings">
    </div>
    <div class="form-field">
        <label for="state_image_url"><?php esc_html_e( 'Hero Image', 'odysseo-core' ); ?></label>
        <input type="url" name="state_image_url" id="state_image_url" value="" placeholder="<?php esc_attr_e( 'https://… or use the button', 'odysseo-core' ); ?>">
        <button type="button" class="button odysseo-media-btn" data-target="state_image_url" data-title="<?php esc_attr_e( 'Select hero image', 'odysseo-core' ); ?>"><?php esc_html_e( 'Select / Upload', 'odysseo-core' ); ?></button>
        <button type="button" class="button odysseo-media-clear" data-target="state_image_url"><?php esc_html_e( 'Clear', 'odysseo-core' ); ?></button>
    </div>
<?php }
add_action( 'dest_state_add_form_fields', 'odysseo_state_term_add_fields' );

/**
 * Renders the State term edit-form fields (image + tagline).
 *
 * @param WP_Term $term Current term.
 */
function odysseo_state_term_edit_fields( $term ) {
    $tagline = get_term_meta( $term->term_id, '_state_tagline', true );
    $image   = get_term_meta( $term->term_id, '_state_image_url', true );
    ?>
    <tr class="form-field">
        <th><label for="state_tagline"><?php esc_html_e( 'Tagline', 'odysseo-core' ); ?></label></th>
        <td><input type="text" name="state_tagline" id="state_tagline" value="<?php echo esc_attr( $tagline ); ?>" style="width:100%" placeholder="e.g. Land of Kings"></td>
    </tr>
    <tr class="form-field">
        <th><label for="state_image_url"><?php esc_html_e( 'Hero Image', 'odysseo-core' ); ?></label></th>
        <td>
            <input type="url" name="state_image_url" id="state_image_url" value="<?php echo esc_attr( $image ); ?>" style="width:74%" placeholder="<?php esc_attr_e( 'https://… or use the button', 'odysseo-core' ); ?>">
            <button type="button" class="button odysseo-media-btn" data-target="state_image_url" data-title="<?php esc_attr_e( 'Select hero image', 'odysseo-core' ); ?>"><?php esc_html_e( 'Select / Upload', 'odysseo-core' ); ?></button>
            <button type="button" class="button odysseo-media-clear" data-target="state_image_url"><?php esc_html_e( 'Clear', 'odysseo-core' ); ?></button>
        </td>
    </tr>
    <?php
}
add_action( 'dest_state_edit_form_fields', 'odysseo_state_term_edit_fields' );

/**
 * Saves the State term meta (image + tagline).
 *
 * @param int $term_id Term ID.
 */
function odysseo_state_term_save( $term_id ) {
    // Term forms are nonce-protected by core; gate on the term-edit capability.
    if ( ! current_user_can( 'manage_categories' ) ) {
        return;
    }
    // phpcs:disable WordPress.Security.NonceVerification.Missing -- Core verifies the term-form nonce before created_/edited_ term hooks fire; capability checked above.
    if ( isset( $_POST['state_tagline'] ) ) {
        update_term_meta( $term_id, '_state_tagline', sanitize_text_field( wp_unslash( $_POST['state_tagline'] ) ) );
    }
    if ( isset( $_POST['state_image_url'] ) ) {
        update_term_meta( $term_id, '_state_image_url', esc_url_raw( wp_unslash( $_POST['state_image_url'] ) ) );
    }
    // phpcs:enable WordPress.Security.NonceVerification.Missing
}
add_action( 'created_dest_state', 'odysseo_state_term_save' );
add_action( 'edited_dest_state', 'odysseo_state_term_save' );

/**
 * Add meta box to tour_package CPT.
 */
function odysseo_package_meta_box() {
    add_meta_box( 'package_details', __( 'Package Details', 'odysseo-core' ), 'odysseo_package_meta_callback', 'tour_package', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'odysseo_package_meta_box' );

/**
 * Renders the Tour Package details meta box.
 *
 * @param WP_Post $post Current post.
 */
function odysseo_package_meta_callback( $post ) {
    wp_nonce_field( 'odysseo_package_meta', 'odysseo_package_nonce' );
    $count       = get_post_meta( $post->ID, '_pkg_count', true );
    $duration    = get_post_meta( $post->ID, '_pkg_duration', true );
    $price       = get_post_meta( $post->ID, '_pkg_price', true );
    $image       = get_post_meta( $post->ID, '_pkg_image', true );
    $highlights  = get_post_meta( $post->ID, '_pkg_highlights', true );
    $itinerary   = get_post_meta( $post->ID, '_pkg_itinerary', true );
    $inclusions  = get_post_meta( $post->ID, '_pkg_inclusions', true );
    $exclusions  = get_post_meta( $post->ID, '_pkg_exclusions', true );
    $gallery     = get_post_meta( $post->ID, '_pkg_gallery', true );
    $best_season = get_post_meta( $post->ID, '_pkg_best_season', true );
    $group_size  = get_post_meta( $post->ID, '_pkg_group_size', true );
    $featured    = get_post_meta( $post->ID, '_pkg_featured', true );
    ?>
    <p style="padding:8px 10px;background:#f6f7f7;border-left:3px solid #c9a961;">
        <label><input type="checkbox" name="pkg_featured" value="1" <?php checked( '1', $featured ); ?>> <strong><?php esc_html_e( 'Featured — show in "Most loved tour packages" on the home page', 'odysseo-core' ); ?></strong></label>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Number of Packages', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="pkg_count" value="<?php echo esc_attr( $count ); ?>" style="width:100%" placeholder="e.g. 71 Packages">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Typical Duration', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="pkg_duration" value="<?php echo esc_attr( $duration ); ?>" style="width:100%" placeholder="e.g. 5N / 6D">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Starting Price', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="pkg_price" value="<?php echo esc_attr( $price ); ?>" style="width:100%" placeholder="<?php echo esc_attr( 'e.g. ' . get_theme_mod( 'currency_symbol', '$' ) . '1,499' ); ?>">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Best Season', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="pkg_best_season" value="<?php echo esc_attr( $best_season ); ?>" style="width:100%" placeholder="e.g. Oct – Mar">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Group Size', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="pkg_group_size" value="<?php echo esc_attr( $group_size ); ?>" style="width:100%" placeholder="e.g. 2-12 People">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Hero Image', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="url" name="pkg_image" id="pkg_image" value="<?php echo esc_attr( $image ); ?>" style="width:74%" placeholder="<?php esc_attr_e( 'https://… or use the button', 'odysseo-core' ); ?>">
        <button type="button" class="button odysseo-media-btn" data-target="pkg_image" data-title="<?php esc_attr_e( 'Select hero image', 'odysseo-core' ); ?>"><?php esc_html_e( 'Select / Upload', 'odysseo-core' ); ?></button>
        <button type="button" class="button odysseo-media-clear" data-target="pkg_image"><?php esc_html_e( 'Clear', 'odysseo-core' ); ?></button>
        <br><img id="pkg_image_preview" src="<?php echo esc_url( $image ); ?>" alt="" style="max-width:200px;height:auto;margin-top:8px;border-radius:6px;<?php echo $image ? '' : 'display:none;'; ?>">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Highlights (one per line)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="pkg_highlights" rows="5" style="width:100%" placeholder="Iconic landmark visit&#10;Stay in a boutique hotel&#10;Guided local experience"><?php echo esc_textarea( $highlights ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Itinerary (each day separated by blank line; first line of each block is the day title)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="pkg_itinerary" rows="14" style="width:100%" placeholder="Day 1: Arrival&#10;Welcome and transfer to your hotel, evening at leisure.&#10;&#10;Day 2: City sightseeing&#10;Explore the main landmarks with your local guide."><?php echo esc_textarea( $itinerary ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Inclusions (one per line)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="pkg_inclusions" rows="6" style="width:100%" placeholder="Accommodation in 4-star hotels&#10;Daily breakfast and dinner&#10;AC vehicle for all transfers&#10;English-speaking guide"><?php echo esc_textarea( $inclusions ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Exclusions (one per line)', 'odysseo-core' ); ?>:</strong></label><br>
        <textarea name="pkg_exclusions" rows="5" style="width:100%" placeholder="Airfare/train tickets&#10;Lunch and personal expenses&#10;Monument entry fees&#10;Travel insurance"><?php echo esc_textarea( $exclusions ); ?></textarea>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Gallery Images', 'odysseo-core' ); ?>:</strong></label>
        <a href="#" class="odysseo-gallery-toggle" data-target="pkg_gallery"><?php esc_html_e( 'Edit URLs', 'odysseo-core' ); ?></a><br>
        <button type="button" class="button odysseo-media-btn" data-target="pkg_gallery" data-multiple="1" data-title="<?php esc_attr_e( 'Add gallery images', 'odysseo-core' ); ?>"><?php esc_html_e( 'Add from Library', 'odysseo-core' ); ?></button>
        <div class="odysseo-gallery-grid" data-target="pkg_gallery"></div>
        <textarea name="pkg_gallery" id="pkg_gallery" rows="4" style="width:100%;display:none;" placeholder="<?php esc_attr_e( 'One image URL per line', 'odysseo-core' ); ?>"><?php echo esc_textarea( $gallery ); ?></textarea>
    </p>
    <?php
}

/**
 * Saves the Tour Package details meta box.
 *
 * @param int $post_id Post ID.
 */
function odysseo_package_save_meta( $post_id ) {
    if ( ! isset( $_POST['odysseo_package_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['odysseo_package_nonce'] ) ), 'odysseo_package_meta' ) ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;

    $fields = array(
        'pkg_count'       => 'sanitize_text_field',
        'pkg_duration'    => 'sanitize_text_field',
        'pkg_price'       => 'sanitize_text_field',
        'pkg_best_season' => 'sanitize_text_field',
        'pkg_group_size'  => 'sanitize_text_field',
        'pkg_image'       => 'esc_url_raw',
        'pkg_highlights'  => 'sanitize_textarea_field',
        'pkg_itinerary'   => 'wp_kses_post',
        'pkg_inclusions'  => 'sanitize_textarea_field',
        'pkg_exclusions'  => 'sanitize_textarea_field',
        'pkg_gallery'     => 'sanitize_textarea_field',
    );
    foreach ( $fields as $field => $sanitizer ) {
        if ( isset( $_POST[ $field ] ) ) {
            update_post_meta( $post_id, '_' . $field, call_user_func( $sanitizer, wp_unslash( $_POST[ $field ] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Value is sanitized via the field's whitelisted $sanitizer callback.
        }
    }

    // Featured checkbox (unchecked boxes are not posted -> stored as '0').
    update_post_meta( $post_id, '_pkg_featured', isset( $_POST['pkg_featured'] ) ? '1' : '0' );
}
add_action( 'save_post_tour_package', 'odysseo_package_save_meta' );

/**
 * Add custom meta boxes for Fleet
 */
function odysseo_add_fleet_meta_boxes() {
    add_meta_box(
        'fleet_details',
        __( 'Fleet Details', 'odysseo-core' ),
        'odysseo_fleet_meta_callback',
        'fleet',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'odysseo_add_fleet_meta_boxes' );

/**
 * Renders the Fleet details meta box.
 *
 * @param WP_Post $post Current post.
 */
function odysseo_fleet_meta_callback( $post ) {
    wp_nonce_field( 'odysseo_fleet_meta', 'odysseo_fleet_nonce' );
    $seats   = get_post_meta( $post->ID, '_fleet_seats', true );
    $ac      = get_post_meta( $post->ID, '_fleet_ac', true );
    $luggage = get_post_meta( $post->ID, '_fleet_luggage', true );
    $price   = get_post_meta( $post->ID, '_fleet_price', true );
    $type    = get_post_meta( $post->ID, '_fleet_type', true );
    ?>
    <p>
        <label><strong><?php esc_html_e( 'Vehicle Type', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="fleet_type" value="<?php echo esc_attr( $type ); ?>" style="width:100%" placeholder="e.g. Sedan, SUV, Tempo Traveller">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Number of Seats', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="number" name="fleet_seats" value="<?php echo esc_attr( $seats ); ?>" style="width:100%">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'AC Available', 'odysseo-core' ); ?>:</strong></label><br>
        <select name="fleet_ac" style="width:100%">
            <option value="Yes" <?php selected( $ac, 'Yes' ); ?>>Yes</option>
            <option value="No" <?php selected( $ac, 'No' ); ?>>No</option>
        </select>
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Luggage Capacity', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="fleet_luggage" value="<?php echo esc_attr( $luggage ); ?>" style="width:100%" placeholder="e.g. 4 Bags">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Price per Day', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="fleet_price" value="<?php echo esc_attr( $price ); ?>" style="width:100%" placeholder="<?php echo esc_attr( 'e.g. ' . get_theme_mod( 'currency_symbol', '$' ) . '120' ); ?>">
    </p>
    <?php
}

/**
 * Saves the Fleet details meta box.
 *
 * @param int $post_id Post ID.
 */
function odysseo_save_fleet_meta( $post_id ) {
    if ( ! isset( $_POST['odysseo_fleet_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['odysseo_fleet_nonce'] ) ), 'odysseo_fleet_meta' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $fields = array( 'fleet_type', 'fleet_seats', 'fleet_ac', 'fleet_luggage', 'fleet_price' );
    foreach ( $fields as $field ) {
        if ( isset( $_POST[ $field ] ) ) {
            update_post_meta( $post_id, '_' . $field, sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
        }
    }
}
add_action( 'save_post_fleet', 'odysseo_save_fleet_meta' );

/**
 * Add custom meta box for Testimonials
 */
function odysseo_add_testimonial_meta_boxes() {
    add_meta_box(
        'testimonial_details',
        __( 'Testimonial Details', 'odysseo-core' ),
        'odysseo_testimonial_meta_callback',
        'testimonial'
    );
}
add_action( 'add_meta_boxes', 'odysseo_add_testimonial_meta_boxes' );

/**
 * Renders the Testimonial details meta box.
 *
 * @param WP_Post $post Current post.
 */
function odysseo_testimonial_meta_callback( $post ) {
    wp_nonce_field( 'odysseo_testimonial_meta', 'odysseo_testimonial_nonce' );
    $designation = get_post_meta( $post->ID, '_testimonial_designation', true );
    $rating      = get_post_meta( $post->ID, '_testimonial_rating', true );
    ?>
    <p>
        <label><strong><?php esc_html_e( 'Designation / Location', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="text" name="testimonial_designation" value="<?php echo esc_attr( $designation ); ?>" style="width:100%" placeholder="e.g. Tourist from USA">
    </p>
    <p>
        <label><strong><?php esc_html_e( 'Rating (1-5)', 'odysseo-core' ); ?>:</strong></label><br>
        <input type="number" min="1" max="5" name="testimonial_rating" value="<?php echo esc_attr( $rating ?: 5 ); ?>" style="width:100%">
    </p>
    <?php
}

/**
 * Saves the Testimonial details meta box.
 *
 * @param int $post_id Post ID.
 */
function odysseo_save_testimonial_meta( $post_id ) {
    if ( ! isset( $_POST['odysseo_testimonial_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['odysseo_testimonial_nonce'] ) ), 'odysseo_testimonial_meta' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
    if ( isset( $_POST['testimonial_designation'] ) ) {
        update_post_meta( $post_id, '_testimonial_designation', sanitize_text_field( wp_unslash( $_POST['testimonial_designation'] ) ) );
    }
    if ( isset( $_POST['testimonial_rating'] ) ) {
        update_post_meta( $post_id, '_testimonial_rating', sanitize_text_field( wp_unslash( $_POST['testimonial_rating'] ) ) );
    }
}
add_action( 'save_post_testimonial', 'odysseo_save_testimonial_meta' );
