Home
<?php
class Daily_Tarot_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'daily_tarot_widget',
__('🔮 Daily Tarot Widget', 'spellbound-secrets'),
array('description' => __('Displays a daily Tarot card reading.', 'spellbound-secrets'))
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
echo $args['before_title'] . __('Daily Tarot Reading', 'spellbound-secrets') . $args['after_title'];
$tarot_card = $this->fetch_daily_tarot_card();
if ($tarot_card) {
echo '<div class="tarot-widget" style="text-align: center;">';
echo '<p class="tarot-card-name">🃏 ' . esc_html($tarot_card['name']) . '</p>';
echo '<img src="' . esc_url($tarot_card['image']) . '" alt="' . esc_attr($tarot_card['name']) . '" class="tarot-card-image"/>';
echo '<p class="tarot-card-meaning">' . esc_html($tarot_card['meaning']) . '</p>';
echo '</div>';
echo '<style>
.tarot-widget { opacity: 0; animation: fadeIn 2s forwards; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.tarot-card-image { width: 100px; height: auto; margin-top: 10px; }
</style>';
} else {
echo '<p>Unable to fetch today's tarot card. 🔮</p>';
}
echo $args['after_widget'];
}
private function fetch_daily_tarot_card() {
$cached_tarot = get_transient('daily_tarot_card');
if ($cached_tarot) {
return $cached_tarot;
}
$api_url = 'https://rws-cards-api.herokuapp.com/api/v1/cards/random?n=1';
$response = wp_remote_get($api_url);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!empty($data['cards'][0])) {
$card = $data['cards'][0];
$tarot_card = array(
'name' => $card['name'],
'image' => 'https://example.com/path-to-tarot-images/' . strtolower(str_replace(' ', '-', $card['name'])) . '.jpg',
'meaning' => $card['meaning_up']
);
set_transient('daily_tarot_card', $tarot_card, DAY_IN_SECONDS);
return $tarot_card;
}
return false;
}
public function form($instance) {
echo '<p>No settings needed. This widget fetches a new tarot card every day. 🔮</p>';
}
}
function register_daily_tarot_widget() {
register_widget('Daily_Tarot_Widget');
}
add_action('widgets_init', 'register_daily_tarot_widget');