Created
April 1, 2020 09:51
-
-
Save msaari/e07ce8eabb53d64643f76b91c9f964d3 to your computer and use it in GitHub Desktop.
WP RSS Widget Alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Widget RSS Alpha | |
* Description: WP RSS Widget, joka näyttää koko syötteen aakkosjärjestyksessä | |
* Version: 1 | |
* Author: Mikko Saari | |
* Author URI: http://www.mikkosaari.fi/ | |
*/ | |
/** | |
* Widget API: WP_Widget_RSS class | |
* | |
* @package WordPress | |
* @subpackage Widgets | |
* @since 4.4.0 | |
*/ | |
add_action( | |
'widgets_init', | |
function() { | |
$alpha_widget = new WP_Widget_RSS_Alpha(); | |
register_widget( $alpha_widget ); | |
} | |
); | |
/** | |
* Core class used to implement a RSS widget. | |
* | |
* @since 2.8.0 | |
* | |
* @see WP_Widget | |
*/ | |
class WP_Widget_RSS_Alpha extends WP_Widget { | |
/** | |
* Sets up a new RSS widget instance. | |
* | |
* @since 2.8.0 | |
*/ | |
public function __construct() { | |
$widget_ops = array( | |
'description' => __( 'Entries from any RSS or Atom feed.' ), | |
'customize_selective_refresh' => true, | |
); | |
$control_ops = array( | |
'width' => 400, | |
'height' => 200, | |
); | |
parent::__construct( 'rss_alpha', __( 'RSS aakkosissa' ), $widget_ops, $control_ops ); | |
} | |
/** | |
* Outputs the content for the current RSS widget instance. | |
* | |
* @since 2.8.0 | |
* | |
* @param array $args Display arguments including 'before_title', 'after_title', | |
* 'before_widget', and 'after_widget'. | |
* @param array $instance Settings for the current RSS widget instance. | |
*/ | |
public function widget( $args, $instance ) { | |
if ( isset( $instance['error'] ) && $instance['error'] ) { | |
return; | |
} | |
$url = ! empty( $instance['url'] ) ? $instance['url'] : ''; | |
while ( stristr( $url, 'http' ) != $url ) { | |
$url = substr( $url, 1 ); | |
} | |
if ( empty( $url ) ) { | |
return; | |
} | |
// self-url destruction sequence | |
if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) ) { | |
return; | |
} | |
$rss = fetch_feed( $url ); | |
$title = $instance['title']; | |
$desc = ''; | |
$link = ''; | |
if ( ! is_wp_error( $rss ) ) { | |
$desc = esc_attr( strip_tags( html_entity_decode( $rss->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) ); | |
if ( empty( $title ) ) { | |
$title = strip_tags( $rss->get_title() ); | |
} | |
$link = strip_tags( $rss->get_permalink() ); | |
while ( stristr( $link, 'http' ) != $link ) { | |
$link = substr( $link, 1 ); | |
} | |
} | |
if ( empty( $title ) ) { | |
$title = ! empty( $desc ) ? $desc : __( 'Unknown Feed' ); | |
} | |
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ | |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); | |
$url = strip_tags( $url ); | |
$icon = includes_url( 'images/rss.png' ); | |
if ( $title ) { | |
$title = '<a class="rsswidget" href="' . esc_url( $url ) . '"><img class="rss-widget-icon" style="border:0" width="14" height="14" src="' . esc_url( $icon ) . '" alt="RSS" /></a> <a class="rsswidget" href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>'; | |
} | |
echo $args['before_widget']; | |
if ( $title ) { | |
echo $args['before_title'] . $title . $args['after_title']; | |
} | |
mikko_widget_rss_output( $rss, $instance ); | |
echo $args['after_widget']; | |
if ( ! is_wp_error( $rss ) ) { | |
$rss->__destruct(); | |
} | |
unset( $rss ); | |
} | |
/** | |
* Handles updating settings for the current RSS widget instance. | |
* | |
* @since 2.8.0 | |
* | |
* @param array $new_instance New settings for this instance as input by the user via | |
* WP_Widget::form(). | |
* @param array $old_instance Old settings for this instance. | |
* @return array Updated settings to save. | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$testurl = ( isset( $new_instance['url'] ) && ( ! isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) ); | |
return wp_widget_rss_process( $new_instance, $testurl ); | |
} | |
/** | |
* Outputs the settings form for the RSS widget. | |
* | |
* @since 2.8.0 | |
* | |
* @param array $instance Current settings. | |
*/ | |
public function form( $instance ) { | |
if ( empty( $instance ) ) { | |
$instance = array( | |
'title' => '', | |
'url' => '', | |
'items' => 10, | |
'error' => false, | |
'show_summary' => 0, | |
'show_author' => 0, | |
'show_date' => 0, | |
); | |
} | |
$instance['number'] = $this->number; | |
mikko_widget_rss_form( $instance ); | |
} | |
} | |
/** | |
* Display the RSS entries in a list. | |
* | |
* @since 2.5.0 | |
* | |
* @param string|array|object $rss RSS url. | |
* @param array $args Widget arguments. | |
*/ | |
function mikko_widget_rss_output( $rss, $args = array() ) { | |
if ( is_string( $rss ) ) { | |
$rss = fetch_feed( $rss ); | |
} elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { | |
$args = $rss; | |
$rss = fetch_feed( $rss['url'] ); | |
} elseif ( ! is_object( $rss ) ) { | |
return; | |
} | |
if ( is_wp_error( $rss ) ) { | |
if ( is_admin() || current_user_can( 'manage_options' ) ) { | |
echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>'; | |
} | |
return; | |
} | |
$default_args = array( | |
'show_author' => 0, | |
'show_date' => 0, | |
'show_summary' => 0, | |
'items' => 0, | |
); | |
$args = wp_parse_args( $args, $default_args ); | |
$items = $rss->get_item_quantity(); | |
$show_summary = (int) $args['show_summary']; | |
$show_author = (int) $args['show_author']; | |
$show_date = (int) $args['show_date']; | |
if ( ! $rss->get_item_quantity() ) { | |
echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; | |
$rss->__destruct(); | |
unset( $rss ); | |
return; | |
} | |
echo '<ul>'; | |
$rss_items = array(); | |
foreach ( $rss->get_items( 0, $items ) as $item ) { | |
$link = $item->get_link(); | |
while ( stristr( $link, 'http' ) != $link ) { | |
$link = substr( $link, 1 ); | |
} | |
$link = esc_url( strip_tags( $link ) ); | |
$title = esc_html( trim( strip_tags( $item->get_title() ) ) ); | |
if ( empty( $title ) ) { | |
$title = __( 'Untitled' ); | |
} | |
$desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); | |
$desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); | |
$summary = ''; | |
if ( $show_summary ) { | |
$summary = $desc; | |
// Change existing [...] to […]. | |
if ( '[...]' == substr( $summary, -5 ) ) { | |
$summary = substr( $summary, 0, -5 ) . '[…]'; | |
} | |
$summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; | |
} | |
$date = ''; | |
if ( $show_date ) { | |
$date = $item->get_date( 'U' ); | |
if ( $date ) { | |
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>'; | |
} | |
} | |
$author = ''; | |
if ( $show_author ) { | |
$author = $item->get_author(); | |
if ( is_object( $author ) ) { | |
$author = $author->get_name(); | |
$author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>'; | |
} | |
} | |
if ( $link == '' ) { | |
$rss_items[$title] = "<li>$title{$date}{$summary}{$author}</li>"; | |
} elseif ( $show_summary ) { | |
$rss_items[$title] = "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>"; | |
} else { | |
$rss_items[$title] = "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>"; | |
} | |
} | |
ksort( $rss_items ); | |
array_walk( $rss_items, 'printf' ); | |
echo '</ul>'; | |
$rss->__destruct(); | |
unset( $rss ); | |
} | |
/** | |
* Display RSS widget options form. | |
* | |
* The options for what fields are displayed for the RSS form are all booleans | |
* and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author', | |
* 'show_date'. | |
* | |
* @since 2.5.0 | |
* | |
* @param array|string $args Values for input fields. | |
* @param array $inputs Override default display options. | |
*/ | |
function mikko_widget_rss_form( $args, $inputs = null ) { | |
$default_inputs = array( | |
'url' => true, | |
'title' => true, | |
'items' => true, | |
'show_summary' => true, | |
'show_author' => true, | |
'show_date' => true, | |
); | |
$inputs = wp_parse_args( $inputs, $default_inputs ); | |
$args['title'] = isset( $args['title'] ) ? $args['title'] : ''; | |
$args['url'] = isset( $args['url'] ) ? $args['url'] : ''; | |
$args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0; | |
if ( $args['items'] < 1 || 20 < $args['items'] ) { | |
$args['items'] = 10; | |
} | |
$args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary']; | |
$args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author']; | |
$args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; | |
if ( ! empty( $args['error'] ) ) { | |
echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>'; | |
} | |
$esc_number = esc_attr( $args['number'] ); | |
if ( $inputs['url'] ) : | |
?> | |
<p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label> | |
<input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p> | |
<?php endif; if ( $inputs['title'] ) : ?> | |
<p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label> | |
<input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p> | |
<?php endif; if ( $inputs['items'] ) : ?> | |
<p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label> | |
<select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][items]"> | |
<?php | |
for ( $i = 1; $i <= 20; ++$i ) { | |
echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>"; | |
} | |
?> | |
</select></p> | |
<?php endif; if ( $inputs['show_summary'] ) : ?> | |
<p><input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> /> | |
<label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label></p> | |
<?php endif; if ( $inputs['show_author'] ) : ?> | |
<p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> /> | |
<label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p> | |
<?php endif; if ( $inputs['show_date'] ) : ?> | |
<p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/> | |
<label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p> | |
<?php | |
endif; | |
foreach ( array_keys( $default_inputs ) as $input ) : | |
if ( 'hidden' === $inputs[ $input ] ) : | |
$id = str_replace( '_', '-', $input ); | |
?> | |
<input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss_alpha[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" /> | |
<?php | |
endif; | |
endforeach; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment