/**
 * Horoscope Grid - Live via AstroSage RSS
 * Usage: [horoscope_grid]
 */
add_shortcode('horoscope_grid', function() {
    include_once(ABSPATH . WPINC . '/feed.php');

    // Reliable public RSS feed for daily horoscopes
    $feed_url = 'https://feeds.feedburner.com/dayhoroscope';
   
    $rss = fetch_feed($feed_url);

    if (is_wp_error($rss)) {
        return '<div class="horo-item"><p>Forecasts currently updating. Please check back shortly.</p></div>';
    }

    // Get the first 12 items for the 12 signs
    $items = $rss->get_items(0, 12);
    $output = '<div class="midnight-horoscope-grid">';

    foreach ($items as $item) {
        $output .= sprintf(
            '<div class="horo-item"><h4>%s</h4><p>%s</p></div>',
            esc_html($item->get_title()),
            esc_html($item->get_description())
        );
    }

    $output .= '</div>';
    return $output;
});


// Optional: Clean up expired transients once a day automatically
if ( ! wp_next_scheduled( 'daily_cleanup_transients' ) ) {
    wp_schedule_event( time(), 'daily', 'daily_cleanup_transients' );
}

add_action( 'daily_cleanup_transients', function() {
    global $wpdb;
    $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_horo_%' AND option_value < UNIX_TIMESTAMP()" );
    $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_horo_%' AND option_name NOT IN (SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_horo_%')" );
});