WordPress.org

Ready to get started?Download WordPress

Forums

RSS calendar feed sorted wrong (1 post)

  1. bignides
    Member
    Posted 11 months ago #

    The site I'm working on has a customized plugin that is supposed to pull Helios calendar events from an RSS feed and display them in order of nearest event first. For some reason, it is always sorted furthest start date first. This is true whether the RSS feed data is sorted in ASC or DESC order. I don't event see any code for sorting at all so I don't know why it's not just displaying exactly what's in the feed. This has been messing with me for days so any help would be greatly appreciated. Thanks.

    /**
     * Display the RSS entries in a list.
     *
     * @since 2.5.0
     *
     * @param string|array|object $rss RSS url.
     * @param array $args Widget arguments.
     */
    function jfmc_wp_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>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
    		return;
    	}
    
    	$default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
    	$args = wp_parse_args( $args, $default_args );
    	extract( $args, EXTR_SKIP );
    
    	$items = (int) $items;
    	if ( $items < 1 || 20 < $items )
    		$items = 10;
    	$show_summary  = (int) $show_summary;
    	$show_author   = (int) $show_author;
    	$show_date     = (int) $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>';
    	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_attr(strip_tags($item->get_title()));
    		if ( empty($title) )
    			$title = __('Untitled');
    
    		$desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
    		$desc = wp_html_excerpt( $desc, 60 );
    
    		// Append ellipsis. Change existing [...] to [&hellip;].
    		if ( '...' == substr( $desc, -5 ) )
    			$desc = substr( $desc, 0, -5 ) . '&hellip;';
    		elseif ( '&hellip;' != substr( $desc, -10 ) )
    			$desc .= ' &hellip;';
    
    		$desc = esc_html( $desc );
    
    		if ( $show_summary ) {
    			$summary = "<div class='rssSummary'>$desc</div>";
    		} else {
    			$summary = '';
    		}
    
    		$date = '';
    		if ( $show_date ) {
    			$date = $item->get_date( 'U' );
    
    			if ( $date ) {
    				$date = ' <div class="rss-date ewidget-time"><span class="ewidget-date">' . date_i18n( 'd', $date ) . '</span><br /><div class="ewidget-mon">' . date_i18n('M', $date ) . '</div></div>';
    			}
    		}
    
    		$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 == '' ) {
    			echo "<li>$title{$date}{$summary}{$author}</li>";
    		} else {
    			echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
    		}
    	}
    	echo '</ul>';
    	$rss->__destruct();
    	unset($rss);
    }
    /**
     * Process RSS feed widget data and optionally retrieve feed items.
     *
     * The feed widget can not have more than 20 items or it will reset back to the
     * default, which is 10.
     *
     * The resulting array has the feed title, feed url, feed link (from channel),
     * feed items, error (if any), and whether to show summary, author, and date.
     * All respectively in the order of the array elements.
     *
     * @since 2.5.0
     *
     * @param array $widget_rss RSS widget feed data. Expects unescaped data.
     * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
     * @return array
     */
    function jfmc_wp_widget_rss_process( $widget_rss, $check_feed = true ) {
    	$items = (int) $widget_rss['items'];
    	if ( $items < 1 || 20 < $items )
    		$items = 10;
    	$url           = esc_url_raw(strip_tags( $widget_rss['url'] ));
    	$title         = trim(strip_tags( $widget_rss['title'] ));
    	$show_summary  = isset($widget_rss['show_summary']) ? (int) $widget_rss['show_summary'] : 0;
    	$show_author   = isset($widget_rss['show_author']) ? (int) $widget_rss['show_author'] :0;
    	$show_date     = isset($widget_rss['show_date']) ? (int) $widget_rss['show_date'] : 0;
    
    	if ( $check_feed ) {
    		$rss = fetch_feed($url);
    		$error = false;
    		$link = '';
    		if ( is_wp_error($rss) ) {
    			$error = $rss->get_error_message();
    		} else {
    			$link = esc_url(strip_tags($rss->get_permalink()));
    			while ( stristr($link, 'http') != $link )
    				$link = substr($link, 1);
    
    			$rss->__destruct();
    			unset($rss);
    		}
    	}
    
    	return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
    }

Reply

You must log in to post.

About this Topic

Tags