From 3ba4a63cb294582b6010448dba09c4992eeacaff Mon Sep 17 00:00:00 2001 From: Martien Date: Tue, 24 Jun 2025 14:34:23 +0200 Subject: [PATCH] Add wp-nostr-widget/nostr-widget.php Initial commit, not yet tested. --- wp-nostr-widget/nostr-widget.php | 111 +++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 wp-nostr-widget/nostr-widget.php diff --git a/wp-nostr-widget/nostr-widget.php b/wp-nostr-widget/nostr-widget.php new file mode 100644 index 0000000..03ee47f --- /dev/null +++ b/wp-nostr-widget/nostr-widget.php @@ -0,0 +1,111 @@ + __('Shows Nostr posts from a public key and relay(s).', 'nostr_widget_domain')) + ); + } + + public function widget($args, $instance) { + $pubkey = !empty($instance['pubkey']) ? $instance['pubkey'] : ''; + $relays = $this->get_relays_from_instance($instance); + + echo $args['before_widget']; + if (!empty($pubkey) && !empty($relays[0])) { + echo $args['before_title'] . 'Nostr Posts' . $args['after_title']; + $this->display_nostr_posts($pubkey, $relays); + } else { + echo 'Please configure at least a public key and one relay.'; + } + echo $args['after_widget']; + } + + public function form($instance) { + $pubkey = !empty($instance['pubkey']) ? $instance['pubkey'] : ''; + $relays = $this->get_relays_from_instance($instance); + + ?> +

+ + +

+

+ Relay URLs (at least one, max five): + +
+ + Example: https://nostr.band/api/v0/search (for nostr.band) +

+ 8]); + if (is_wp_error($response)) continue; + + $body = wp_remote_retrieve_body($response); + $data = json_decode($body, true); + + if (!empty($data['events'])) { + $posts_found = true; + echo ''; + break; // Stop after first relay with results + } + } + if (!$posts_found) { + echo 'No posts found on any configured relay.'; + } + } +} + +function register_nostr_widget() { + register_widget('Nostr_Widget'); +} +add_action('widgets_init', 'register_nostr_widget');