402 lines
9.6 KiB
PHP
402 lines
9.6 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Super Simple RSS Reader
|
|
* Plugin URI: https://gitea.sondreolav.com/ai-bot/super-simple-rssreader
|
|
* Description: A minimal RSS reader. Configure a feed URL in WP Admin and display it anywhere with the [super_simple_rssreader] shortcode.
|
|
* Version: 1.0.1
|
|
* Requires at least: 5.0
|
|
* Requires PHP: 7.0
|
|
* Author: ai-bot
|
|
* License: GPL-2.0-or-later
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
|
* Text Domain: super-simple-rssreader
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
define( 'SSRSS_VERSION', '1.0.1' );
|
|
define( 'SSRSS_OPTION_KEY', 'ssrss_settings' );
|
|
|
|
/**
|
|
* Default settings.
|
|
*
|
|
* No feed URL or heading is pre-filled; the user provides their own feed.
|
|
* Until a feed URL is configured the shortcode renders nothing.
|
|
*
|
|
* @return array
|
|
*/
|
|
function ssrss_get_defaults() {
|
|
return array(
|
|
'url' => '',
|
|
'heading' => '',
|
|
'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(
|
|
'<input type="url" name="%1$s[url]" id="ssrss_url" value="%2$s" class="regular-text code" />',
|
|
esc_attr( SSRSS_OPTION_KEY ),
|
|
esc_attr( $settings['url'] )
|
|
);
|
|
echo '<p class="description">' . esc_html__( 'Full URL of the RSS feed to read. The plugin shows nothing until this is set.', 'super-simple-rssreader' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* Render the Heading field.
|
|
*
|
|
* @return void
|
|
*/
|
|
function ssrss_field_heading() {
|
|
$settings = ssrss_get_settings();
|
|
printf(
|
|
'<input type="text" name="%1$s[heading]" id="ssrss_heading" value="%2$s" class="regular-text" />',
|
|
esc_attr( SSRSS_OPTION_KEY ),
|
|
esc_attr( $settings['heading'] )
|
|
);
|
|
echo '<p class="description">' . esc_html__( 'Optional heading shown above the list. Leave empty for no heading.', 'super-simple-rssreader' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* Render the Max items field.
|
|
*
|
|
* @return void
|
|
*/
|
|
function ssrss_field_limit() {
|
|
$settings = ssrss_get_settings();
|
|
printf(
|
|
'<input type="number" name="%1$s[limit]" id="ssrss_limit" value="%2$s" min="0" step="1" class="small-text" />',
|
|
esc_attr( SSRSS_OPTION_KEY ),
|
|
esc_attr( (int) $settings['limit'] )
|
|
);
|
|
echo '<p class="description">' . esc_html__( 'Maximum number of items to show. Use 0 to show all items.', 'super-simple-rssreader' ) . '</p>';
|
|
}
|
|
|
|
/**
|
|
* Render the settings page.
|
|
*
|
|
* @return void
|
|
*/
|
|
function ssrss_render_settings_page() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
|
|
<p>
|
|
<?php
|
|
printf(
|
|
/* translators: %s: shortcode tag. */
|
|
esc_html__( 'Display the feed anywhere in your theme or content with the shortcode %s.', 'super-simple-rssreader' ),
|
|
'<code>[super_simple_rssreader]</code>'
|
|
);
|
|
?>
|
|
</p>
|
|
<form action="options.php" method="post">
|
|
<?php
|
|
settings_fields( 'ssrss_settings_group' );
|
|
do_settings_sections( 'ssrss' );
|
|
submit_button();
|
|
?>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Fetch and parse the feed items, using a transient cache.
|
|
*
|
|
* @param string $url Feed URL.
|
|
* @param int $limit Maximum number of items (0 = all).
|
|
* @return array List of items with title, link and source keys.
|
|
*/
|
|
function ssrss_fetch_items( $url, $limit = 0 ) {
|
|
$url = trim( (string) $url );
|
|
if ( '' === $url || ! preg_match( '#^https?://#i', $url ) ) {
|
|
return array();
|
|
}
|
|
|
|
$cache_key = 'ssrss_' . md5( $url );
|
|
$cached = get_transient( $cache_key );
|
|
if ( is_array( $cached ) ) {
|
|
return $cached;
|
|
}
|
|
|
|
$response = wp_remote_get(
|
|
$url,
|
|
array(
|
|
'timeout' => 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 = '<div class="rss-feed">' . "\n";
|
|
if ( '' !== $heading ) {
|
|
$html .= ' <h3 class="rss-title">' . esc_html( $heading ) . '</h3>' . "\n";
|
|
}
|
|
$html .= ' <ul>' . "\n";
|
|
foreach ( $items as $item ) {
|
|
$html .= ' <li>';
|
|
$html .= '<a href="' . esc_url( $item['link'] ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $item['title'] ) . '</a>';
|
|
if ( '' !== $item['source'] ) {
|
|
$html .= ' <span> | ' . esc_html( $item['source'] ) . '</span>';
|
|
}
|
|
$html .= '</li>' . "\n";
|
|
}
|
|
$html .= ' </ul>' . "\n";
|
|
$html .= '</div>' . "\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' );
|