Created
May 21, 2014 03:59
-
-
Save wokamoto/070f9b84fea6288ace97 to your computer and use it in GitHub Desktop.
[WordPress] fetch_feed_wrapper
This file contains 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 | |
if ( !function_exists('fetch_feed') ) | |
include_once(ABSPATH . WPINC . "/feed.php"); | |
if ( !class_exists('fetch_feed_wrapper') ): | |
class fetch_feed_wrapper { | |
private $feed; | |
private $current = 0; | |
private $current_item; | |
private $max_items; | |
public function __construct($feed_url, $max = 0) { | |
$this->feed = fetch_feed($feed_url); | |
$this->current = 0; | |
$this->max_items = $this->max($max); | |
} | |
public function error(){ | |
return is_wp_error($this->feed); | |
} | |
public function error_code(){ | |
if ( is_wp_error($this->feed) ) | |
return $this->feed->get_error_code(); | |
else | |
return false; | |
} | |
public function error_message(){ | |
if ( is_wp_error($this->feed) ) | |
return $this->feed->get_error_message(); | |
else | |
return ''; | |
} | |
public function length(){ | |
return $this->max_items; | |
} | |
private function max($max = 0) { | |
if ($this->error()) | |
return false; | |
$this->max_items = $this->feed->get_item_quantity($max); | |
return $this->max_items; | |
} | |
public function fetch($current = false){ | |
if ($this->error()) | |
return false; | |
if ($this->current > $this->max_items) { | |
$this->current_item = false; | |
return false; | |
} | |
$current = $current ? $current : $this->current; | |
$this->current_item = $this->item($current); | |
$this->current++; | |
return $this->current_item; | |
} | |
private function item($x){ | |
if ($this->error()) | |
return false; | |
$item = $this->feed->get_item($x); | |
return $item; | |
} | |
private function check_item(){ | |
if ($this->error()) | |
return false; | |
if (!$this->current_item) | |
if ( !$this->fetch() ) | |
return false; | |
return true; | |
} | |
public function namespaces(){ | |
if ( ! $this->check_item() ) | |
return false; | |
return array_keys($this->current_item->data['child']); | |
} | |
public function id($hash = false){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_id($hash); | |
} | |
public function title(){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_title(); | |
} | |
public function permalink(){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_permalink(); | |
} | |
public function publish($format = 'Y-m-d H:i:s'){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_date($format); | |
} | |
public function updated($format = 'Y-m-d H:i:s'){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_updated_date($format); | |
} | |
public function categories(){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_categories(); | |
} | |
public function authors(){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_authors(); | |
} | |
public function enclosures() { | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_enclosures(); | |
} | |
public function latitude() { | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item-> get_latitude(); | |
} | |
public function excerpt($description_only = false ){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_description($description_only); | |
} | |
public function content($content_only = false){ | |
if ( ! $this->check_item() ) | |
return false; | |
return $this->current_item->get_description($content_only); | |
} | |
public function child($tag, $singleval = true){ | |
if ( ! $this->check_item() ) | |
return false; | |
$results = array(); | |
foreach ($this->current_item->data['child'] as $child) { | |
if ( !isset($child[$tag]) ) | |
continue; | |
foreach ($child[$tag] as $data){ | |
if ( isset($data['data']) ) { | |
$results[] = $data['data']; | |
if ($singleval) | |
break; | |
} | |
} | |
} | |
return | |
$singleval | |
? ( isset($results[0]) ? $results[0] : '' ) | |
: $results; | |
} | |
} | |
endif; |
This file contains 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 | |
$feed = new fetch_feed_wrapper('http://example.com/feed', 0); | |
while ($item = $feed->fetch()) { | |
var_dump($feed->title()); | |
var_dump($feed->child('custome_tag')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment