From 12ad26fdeb0ee5b06d200296faa011afb5fae599 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Wed, 14 Oct 2026 07:29:29 +0000 Subject: [PATCH] Convert rss-feed.php into a WordPress plugin with settings page and shortcode --- super-simple-rssreader.php | 422 ++++++++++++++++++++++++++++++++++--- 1 file changed, 398 insertions(+), 24 deletions(-) diff --git a/super-simple-rssreader.php b/super-simple-rssreader.php index ea3b6eb..5cce87a 100644 --- a/super-simple-rssreader.php +++ b/super-simple-rssreader.php @@ -1,27 +1,401 @@ " . $rss->channel->title . ""; - echo "
\n"; - echo "

Tau- og kabelbanenytt

\n
\n"; -} else { - echo "\n"; +// Exit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; } -?> + +define( 'SSRSS_VERSION', '1.0.0' ); +define( 'SSRSS_OPTION_KEY', 'ssrss_settings' ); + +/** + * Default settings. + * + * The defaults match the original standalone rss-feed.php so the plugin + * behaves the same way out of the box, while still being configurable. + * + * @return array + */ +function ssrss_get_defaults() { + return array( + 'url' => 'https://news.google.com/rss/search?q=taubane%20OR%20totausbane%20OR%20kabelbane%20OR%20gondol%20when%3A30d&hl=no&gl=NO&ceid=NO%3Ano', + 'heading' => 'Tau- og kabelbanenytt', + 'limit' => 0, + ); +} + +/** + * Get the saved settings merged with the defaults. + * + * @return array + */ +function ssrss_get_settings() { + $stored = get_option( SSRSS_OPTION_KEY, array() ); + if ( ! is_array( $stored ) ) { + $stored = array(); + } + + return wp_parse_args( $stored, ssrss_get_defaults() ); +} + +/** + * Cache lifetime in seconds. + * + * @return int + */ +function ssrss_cache_ttl() { + /** + * Filter the RSS response cache lifetime. + * + * @param int $ttl Lifetime in seconds. + */ + return (int) apply_filters( 'ssrss_cache_ttl', 15 * MINUTE_IN_SECONDS ); +} + +/** + * Store default options on activation. + * + * @return void + */ +function ssrss_activate() { + if ( false === get_option( SSRSS_OPTION_KEY ) ) { + add_option( SSRSS_OPTION_KEY, ssrss_get_defaults() ); + } +} +register_activation_hook( __FILE__, 'ssrss_activate' ); + +/** + * Clear the cached feed when the settings change. + * + * @param array $old Previous settings. + * @param array $new New settings. + * @return void + */ +function ssrss_clear_cache( $old, $new ) { + foreach ( array( $old, $new ) as $settings ) { + if ( is_array( $settings ) && ! empty( $settings['url'] ) ) { + delete_transient( 'ssrss_' . md5( trim( (string) $settings['url'] ) ) ); + } + } +} +add_action( 'update_option_' . SSRSS_OPTION_KEY, 'ssrss_clear_cache', 10, 2 ); + +/** + * Register the admin settings page. + * + * @return void + */ +function ssrss_admin_menu() { + add_options_page( + __( 'Super Simple RSS Reader', 'super-simple-rssreader' ), + __( 'RSS Reader', 'super-simple-rssreader' ), + 'manage_options', + 'ssrss', + 'ssrss_render_settings_page' + ); +} +add_action( 'admin_menu', 'ssrss_admin_menu' ); + +/** + * Register settings, sections and fields. + * + * @return void + */ +function ssrss_register_settings() { + register_setting( + 'ssrss_settings_group', + SSRSS_OPTION_KEY, + array( + 'type' => 'array', + 'sanitize_callback' => 'ssrss_sanitize_settings', + 'default' => ssrss_get_defaults(), + ) + ); + + add_settings_section( + 'ssrss_main', + __( 'Feed settings', 'super-simple-rssreader' ), + '__return_false', + 'ssrss' + ); + + add_settings_field( + 'ssrss_url', + __( 'Feed URL', 'super-simple-rssreader' ), + 'ssrss_field_url', + 'ssrss', + 'ssrss_main' + ); + + add_settings_field( + 'ssrss_heading', + __( 'Heading', 'super-simple-rssreader' ), + 'ssrss_field_heading', + 'ssrss', + 'ssrss_main' + ); + + add_settings_field( + 'ssrss_limit', + __( 'Max items', 'super-simple-rssreader' ), + 'ssrss_field_limit', + 'ssrss', + 'ssrss_main' + ); +} +add_action( 'admin_init', 'ssrss_register_settings' ); + +/** + * Sanitize the submitted settings. + * + * @param mixed $input Raw input. + * @return array + */ +function ssrss_sanitize_settings( $input ) { + $defaults = ssrss_get_defaults(); + $input = is_array( $input ) ? $input : array(); + + return array( + 'url' => isset( $input['url'] ) ? esc_url_raw( trim( (string) $input['url'] ) ) : $defaults['url'], + 'heading' => isset( $input['heading'] ) ? sanitize_text_field( (string) $input['heading'] ) : $defaults['heading'], + 'limit' => isset( $input['limit'] ) ? absint( $input['limit'] ) : 0, + ); +} + +/** + * Render the Feed URL field. + * + * @return void + */ +function ssrss_field_url() { + $settings = ssrss_get_settings(); + printf( + '', + esc_attr( SSRSS_OPTION_KEY ), + esc_attr( $settings['url'] ) + ); + echo '

' . esc_html__( 'Full URL of the RSS feed to read.', 'super-simple-rssreader' ) . '

'; +} + +/** + * Render the Heading field. + * + * @return void + */ +function ssrss_field_heading() { + $settings = ssrss_get_settings(); + printf( + '', + esc_attr( SSRSS_OPTION_KEY ), + esc_attr( $settings['heading'] ) + ); + echo '

' . esc_html__( 'Optional heading shown above the list. Leave empty for no heading.', 'super-simple-rssreader' ) . '

'; +} + +/** + * Render the Max items field. + * + * @return void + */ +function ssrss_field_limit() { + $settings = ssrss_get_settings(); + printf( + '', + esc_attr( SSRSS_OPTION_KEY ), + esc_attr( (int) $settings['limit'] ) + ); + echo '

' . esc_html__( 'Maximum number of items to show. Use 0 to show all items.', 'super-simple-rssreader' ) . '

'; +} + +/** + * Render the settings page. + * + * @return void + */ +function ssrss_render_settings_page() { + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + ?> +
+

+

+ [super_simple_rssreader]' + ); + ?> +

+
+ +
+
+ 10, + 'redirection' => 3, + 'user-agent' => 'SuperSimpleRSSReader/' . SSRSS_VERSION . '; ' . home_url(), + ) + ); + + if ( is_wp_error( $response ) ) { + return array(); + } + + $code = (int) wp_remote_retrieve_response_code( $response ); + $body = wp_remote_retrieve_body( $response ); + if ( 200 !== $code || '' === $body ) { + return array(); + } + + $items = ssrss_parse_feed( $body, $limit ); + if ( ! empty( $items ) ) { + set_transient( $cache_key, $items, ssrss_cache_ttl() ); + } + + return $items; +} + +/** + * Parse an RSS document into a simple list of items. + * + * @param string $body Raw RSS XML. + * @param int $limit Maximum number of items (0 = all). + * @return array + */ +function ssrss_parse_feed( $body, $limit = 0 ) { + $use_errors = libxml_use_internal_errors( true ); + $rss = simplexml_load_string( $body ); + libxml_clear_errors(); + libxml_use_internal_errors( $use_errors ); + + if ( false === $rss || ! isset( $rss->channel->item ) ) { + return array(); + } + + $limit = absint( $limit ); + $items = array(); + + foreach ( $rss->channel->item as $item ) { + $source = trim( (string) $item->source ); + $title = trim( (string) $item->title ); + + // Google News appends " - Source" to every title. Strip that suffix so + // the source is not repeated next to the title (legacy behaviour). + if ( '' !== $source ) { + $suffix = ' - ' . $source; + if ( strlen( $title ) > strlen( $suffix ) && substr( $title, -strlen( $suffix ) ) === $suffix ) { + $title = substr( $title, 0, -strlen( $suffix ) ); + } + } + + $items[] = array( + 'title' => $title, + 'link' => trim( (string) $item->link ), + 'source' => $source, + ); + + if ( $limit > 0 && count( $items ) >= $limit ) { + break; + } + } + + return $items; +} + +/** + * Render the feed list. + * + * @param array $atts Shortcode attributes. + * @return string + */ +function ssrss_render_list( $atts ) { + $settings = ssrss_get_settings(); + + $atts = shortcode_atts( + array( + 'url' => $settings['url'], + 'title' => $settings['heading'], + 'limit' => $settings['limit'], + ), + $atts, + 'super_simple_rssreader' + ); + + $items = ssrss_fetch_items( $atts['url'], $atts['limit'] ); + if ( empty( $items ) ) { + return ''; + } + + $heading = trim( (string) $atts['title'] ); + + $html = '
' . "\n"; + if ( '' !== $heading ) { + $html .= '

' . esc_html( $heading ) . '

' . "\n"; + } + $html .= ' ' . "\n"; + $html .= '
' . "\n"; + + return $html; +} + +/** + * Shortcode handler for [super_simple_rssreader]. + * + * @param array $atts Shortcode attributes. + * @return string + */ +function ssrss_shortcode( $atts ) { + return ssrss_render_list( $atts ); +} +add_shortcode( 'super_simple_rssreader', 'ssrss_shortcode' );