/******* Do not edit this file *******
Simple Custom CSS and JS - by Silkypress.com
Saved: May 01 2025 | 21:25:00 */
.cst-cat .taxonomy-infor {
    display: none;
}
add_shortcode('bookeo_tour', 'fetch_bookeo_tour');

function fetch_bookeo_tour($atts) {
    // Shortcode attributes
    $atts = shortcode_atts(
        array(
            'tour_id' => '', // The specific tour ID
        ),
        $atts,
        'bookeo_tour'
    );

    if (!$atts['tour_id']) {
        return '<p>No tour ID provided.</p>';
    }

    // Bookeo API credentials
    $api_key = 'AEEEP3U67W7YAK7U6NTMF41560PPW6MX17C3DA06C26';
    $secret_key = 'YOUR_SECRET_KEY'; // Replace with your secret key
    $endpoint_url = 'https://api.bookeo.com/v2/products/' . $atts['tour_id'];

    // Check cached data
    $cache_key = 'bookeo_tour_' . $atts['tour_id'];
    $cached_data = get_transient($cache_key);

    if ($cached_data) {
        return $cached_data;
    }

    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: Basic ' . base64_encode("$api_key:$secret_key")
    ));

    $response = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($status_code == 200) {
        $tour_data = json_decode($response, true);

        if (!empty($tour_data['name']) && !empty($tour_data['description']) && !empty($tour_data['bookingUrl'])) {
            $output = '<h2>' . esc_html($tour_data['name']) . '</h2>';
            $output .= '<p>' . esc_html($tour_data['description']) . '</p>';
            $output .= '<a href="' . esc_url($tour_data['bookingUrl']) . '" target="_blank">Book Now</a>';

            // Cache the response for 1 hour
            set_transient($cache_key, $output, HOUR_IN_SECONDS);

            return $output;
        } else {
            return '<p>Tour details are incomplete. Please check the data in Bookeo.</p>';
        }
    } else {
        $error_message = json_decode($response, true)['message'] ?? 'Unknown error';
        return '<p>Error fetching tour details: ' . esc_html($error_message) . '</p>';
    }
}
