Skip to content

Instantly share code, notes, and snippets.

@zorca
Forked from franz-josef-kaiser/simplepie_example.php
Created October 25, 2018 09:15
Show Gist options
  • Save zorca/d7b65f2e6587080c8bdc45136ba7e9f1 to your computer and use it in GitHub Desktop.
Save zorca/d7b65f2e6587080c8bdc45136ba7e9f1 to your computer and use it in GitHub Desktop.
SimplePie - example method to list what information we can retrieve about a feed as a whole and for single items when looping through
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: (WCM) RSS Importer
*/
add_action( 'plugins_loaded', array( 'WCMRSSImporterBootstrap', 'init' ) );
class WCMRSSImporterBootstrap
{
protected static $instance = null;
public $page_hook = '';
private $feed_URl = 'http://www.onthesnow.com/austria/snow-rss.html';
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
// Normally the textdomain is loaded on `init`,
// so other plugins can replace it on `load_textdomain`.
// We don't use it as we add & remove the file on demand to save memory
# add_action( 'init', array( $this, 'load_l18n' ) );
add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
add_action( 'wp_feed_options', array( $this, 'feed_options' ) );
add_filter( 'http_request_args', array( $this, 'wp_http_api_request_args' ), 10, 2 );
// Uncomment the following line if you want a custom feed cache lifetime
# add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'feed_lifetime' ) );
}
public function load_l18n()
{
// No need to call it outside admin, as this is only an importer.
if ( ! is_admin() )
return;
load_plugin_textdomain(
'rss_importer',
false,
// Language files should reside in a subfolder named `lang`.
plugin_basename( dirname( __FILE__ ) ).'/lang'
);
}
public function unload_l18n()
{
unset( $GLOBALS['l10n']['rss_importer'] );
}
public function feed_options( &$feed )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
// Uncomment the following line if you're sure it's a feed
// and SimplePie doesn't recognize it as such.
# $feed->force_feed( true );
# var_dump( $feed );
}
public function wp_http_api_request_args( $defaults, $url )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
# var_dump( $defaults, $url );
return wp_parse_args( array(
// Uncomment the following line if you got a problem
// with a non-verified SSL certificate.
# 'sslverify' => false
), $defaults );
}
public function feed_lifetime( $seconds )
{
return HOUR_IN_SECONDS * 12 + ( rand( 0, 1 ) * MINUTE_IN_SECONDS );
}
public function add_admin_page()
{
$this->load_l18n();
$this->page_hook = add_management_page(
__( 'SimplePie RSS Example', 'rss_importer' ),
__( 'SimplePie RSS Import', 'rss_importer' ),
'manage_options',
'rss-import',
array( $this, 'admin_page_cb' )
);
$this->unload_l18n();
}
public function admin_page_cb()
{
$this->load_l18n();
screen_icon();
?>
<h1><?php _e( 'Example RSS Output:', 'rss_importer' ); ?></h1>
<?php
// Include Feed stuff
include_once( ABSPATH.WPINC.'/feed.php' );
// Get a SimplePie feed object from the specified feed source.
$feed = fetch_feed( $this->feed_URl );
if ( is_wp_error( $feed ) )
return printf(
'%s %s',
$feed->get_error_code(),
$feed->get_error_message()
);
// Figure out how many total items there are, but limit it to 5.
$feed_items_nr = $feed->get_item_quantity( 5 );
$this->get_feed_data( $feed, $feed_items_nr );
// Build an array of all the items, starting with element 0 (first element).
$feed_items = $feed->get_items( 0, $feed_items_nr );
if ( 0 === $feed_items_nr )
return printf(
'<ul><li>%s</li></ul>',
__( 'No items', 'rss_importer' )
);
_e( '<h2>Items</h2>', 'rss_importer' );
?>
<ul>
<?php
foreach ( $feed_items as $item )
$this->get_item_data( $item );
?>
</ul>
<?php
// Avoid memory leaks
$feed->__destruct();
unset( $feed, $feed_items, $feed_items_nr );
$this->unload_l18n();
}
public function get_feed_data( $feed, $feed_items_nr )
{
$feed_data = array(
'Items' => $feed_items_nr,
'Title' => $feed->get_title(),
'Description' => $feed->get_description(),
'Type' => $feed->get_type(),
'Author' => $feed->get_author(),
'Authors' => $feed->get_authors(),
'Contributor' => $feed->get_contributor(),
'Contributors' => $feed->get_contributors(),
'Copyright' => $feed->get_copyright(),
'Encoding' => $feed->get_encoding(),
'Language' => $feed->get_language(),
'Permalink' => $feed->get_permalink(),
'Lat' => $feed->get_latitude(),
'Lng' => $feed->get_longitude(),
'Categories' => $feed->get_categories(),
'Feed Image' => sprintf( '<p><a href="%s" title="%s"><img src="%s" title="%s" width="%s" height="%s" /></a></p>',
$feed->get_image_link(),
$feed->get_image_title(),
$feed->get_image_url(),
$feed->get_image_title(),
$feed->get_image_width(),
$feed->get_image_height()
),
'Link' => sprintf( '<p><a href="%s" title="%s">%s</a></p>',
$feed->get_base(),
$feed->get_title(),
$feed->get_title()
)
);
echo '<table class="widefat">';
foreach ( $feed_data as $desc => $item )
echo "<tr><td class='row-title'>{$desc}</td><td>{$item}</td>";
echo "{$out}</table>";
}
public function get_item_data( $item )
{
// Access to the parent element
# $parent = $item->get_feed();
# var_dump( $parent );
$link_title = sprintf(
__( 'Posted on %s', 'my-text-domain' ),
$item->get_date('U')
? date_i18n( get_option('date_format'), $item->get_date('U') )
: __( 'unknown date and time', 'rss_importer' )
);
?>
<li>
<h2>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>"
title="<?php echo $link_title; ?>">
<?php echo esc_html( $item->get_title() ); ?>
</a>
</h2>
<table class="widefat">
<?php
foreach ( array(
'Description' => $item->get_description(),
'ID' => $item->get_id(),
'Base' => $item->get_base(),
# 'Item Tags' => $item->get_item_tags(),
'Contributor' => $item->get_contributor(),
'Link' => sprintf(
'<a href="%s" title="%s">%s</a>',
$item->get_link(),
$item->get_title(),
$item->get_title()
),
'Copyright' => $item->get_copyright(),
'Source' => $item->get_source(),
'Author' => $item->get_author(),
'Content' => $item->get_content(),
'Enclosure' => $item->get_enclosure(),
# 'Enclosures' => var_export( $item->get_enclosures(), true ),
'Lat' => $item->get_latitude(),
'Lng' => $item->get_longitude(),
'Date' => $item->get_date('U')
? date_i18n( get_option('date_format'), $item->get_date('U') )
: '&mdash;',
'Updated Date' => $item->get_updated_date('U')
? date_i18n( get_option('date_format'), $item->get_updated_date('U') )
: '&mdash;',
'Local Date' => $item->get_local_date('%c'),
'GMDate' => $item->get_gmdate('U')
? date_i18n( get_option('date_format'), $item->get_gmdate('U') )
: '&mdash;',
'Updated GMDate' => $item->get_updated_gmdate('U')
? date_i18n( get_option('date_format'), $item->get_updated_gmdate('U') )
: '&mdash;',
) as $desc => $part )
echo "<tr><td class='row-title'>{$desc}</td><td>{$part}</td></tr>";
?>
</table>
</li>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment