| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJT_Framework_Wordpress_Feed { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected $feed; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
protected $fields; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
protected $path; |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @var mixed |
| 36 |
*/ |
| 37 |
protected $site; |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
* @param mixed $site |
| 43 |
* @param mixed $path |
| 44 |
* @return CJT_Framework_Wordpress_Feed |
| 45 |
*/ |
| 46 |
public function __construct($site, $path, $fields) { |
| 47 |
# Initialize. |
| 48 |
$this->site =& $site; |
| 49 |
$this->path =& $path; |
| 50 |
$this->fields = $fields; |
| 51 |
# Request server => get raw XML feed |
| 52 |
$feed = wp_remote_get("http://{$this->site}/{$this->path}"); |
| 53 |
if (gettype($feed) !== 'WP_Error') { |
| 54 |
$this->feed = new SimpleXMLElement(wp_remote_retrieve_body($feed)); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* put your comment there... |
| 60 |
* |
| 61 |
* @param mixed $count |
| 62 |
*/ |
| 63 |
public function getLatestItems($count) { |
| 64 |
// Initialize. |
| 65 |
$items = array(); |
| 66 |
// Read only items count specifed by $count param. |
| 67 |
for ($currentIndex = 0; $currentIndex < $count; $currentIndex++) { |
| 68 |
# Copy only title and link. |
| 69 |
$xmlItem = $this->feed->channel->item[$currentIndex]; |
| 70 |
# Read fields. |
| 71 |
$item = array(); |
| 72 |
foreach ($this->fields as $field) { |
| 73 |
$item[$field] = (string) $xmlItem->$field; |
| 74 |
} |
| 75 |
# Add to list |
| 76 |
$items[] = $item; |
| 77 |
} |
| 78 |
# Return items. |
| 79 |
return $items; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* put your comment there... |
| 84 |
* |
| 85 |
*/ |
| 86 |
protected function getPath() { |
| 87 |
return $this->path; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* put your comment there... |
| 92 |
* |
| 93 |
*/ |
| 94 |
public function getSite() { |
| 95 |
return $this->site; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* put your comment there... |
| 100 |
* |
| 101 |
*/ |
| 102 |
public function isError() { |
| 103 |
return !$this->feed; |
| 104 |
} |
| 105 |
|
| 106 |
} // End class. |
| 107 |
|