Convert rss-feed.php into a WordPress plugin with settings page and shortcode
This commit is contained in:
1 parent
310bba287e
commit
12ad26fdeb
1 file changed
+398
-24
+398
-24
@@ -1,27 +1,401 @@
|
|||||||
<?php
|
<?php
|
||||||
// URL til RSS-feeden
|
/**
|
||||||
$rss_url = "https://news.google.com/rss/search?q=taubane%20OR%20totausbane%20OR%20kabelbane%20OR%20gondol%20when%3A30d&hl=no&gl=NO&ceid=NO%3Ano";
|
* 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.0
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
// Hent RSS-feeden
|
// Exit if accessed directly.
|
||||||
$rss = simplexml_load_file($rss_url);
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
exit;
|
||||||
// Sjekk om RSS-feeden ble lastet inn
|
|
||||||
if ($rss) {
|
|
||||||
//echo "<h1>" . $rss->channel->title . "</h1>";
|
|
||||||
echo "<div class=\"rss-feed\">\n";
|
|
||||||
echo " <h3 class=\"rss-title\">Tau- og kabelbanenytt</h3><ul>\n";
|
|
||||||
foreach ($rss->channel->item as $item) {
|
|
||||||
$source = $item->source;
|
|
||||||
$title = substr($item->title, 0, -3 - strlen($source));
|
|
||||||
//$description = strip_tags($item->description);
|
|
||||||
echo " <li>";
|
|
||||||
//echo "<p>" . $source . "</p>";
|
|
||||||
echo "<a href=\"" . $item->link . "\" target=\"_blank\">" . $title . "</a> <span> | " . $source . "</span>";
|
|
||||||
//echo "<p>" . $description . "</p>";
|
|
||||||
echo "</li>\n";
|
|
||||||
}
|
|
||||||
echo " </ul>\n</div>\n";
|
|
||||||
} else {
|
|
||||||
echo "\n";
|
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
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(
|
||||||
|
'<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.', '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' );
|
||||||
Reference in new issue
Block a user