| 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 |
{ |
| 48 |
|
| 49 |
$this->site =& $site; |
| 50 |
$this->path =& $path; |
| 51 |
$this->fields = $fields; |
| 52 |
|
| 53 |
# Request server => get raw XML feed |
| 54 |
$feed = wp_remote_get( "http://{$this->site}/{$this->path}" ); |
| 55 |
|
| 56 |
# Getting XML content |
| 57 |
$xmlContent = ( is_array( $feed ) && ( $feed[ 'response' ][ 'code' ] == 200 ) ) ? |
| 58 |
wp_remote_retrieve_body( $feed ) : |
| 59 |
'<cjterrorrequest> |
| 60 |
<channel cjt_error="true"> |
| 61 |
<item> |
| 62 |
<title>ERROR !!!</title> |
| 63 |
<description>ERROR Fetching data from CJT Server</description> |
| 64 |
<link>http://css-javascript-toolbox.com/</link> |
| 65 |
</item> |
| 66 |
</channel> |
| 67 |
</cjterrorrequest>'; |
| 68 |
# Creating feed |
| 69 |
$this->feed = new SimpleXMLElement($xmlContent); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* put your comment there... |
| 74 |
* |
| 75 |
*/ |
| 76 |
public function getAllItems() { |
| 77 |
// Initialize. |
| 78 |
$items = array(); |
| 79 |
$xmlItems = $this->feed->channel->xpath('item'); |
| 80 |
// Read only items count specifed by $count param. |
| 81 |
foreach ($xmlItems as $xmlItem) { |
| 82 |
# Read fields. |
| 83 |
$item = array(); |
| 84 |
foreach ($this->fields as $field) { |
| 85 |
$item[$field] = (string) $xmlItem->$field; |
| 86 |
} |
| 87 |
# Add to list |
| 88 |
$items[] = $item; |
| 89 |
} |
| 90 |
# Return items. |
| 91 |
return $items; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* put your comment there... |
| 96 |
* |
| 97 |
* @param mixed $count |
| 98 |
*/ |
| 99 |
public function getLatestItems($count) { |
| 100 |
# Initialize. |
| 101 |
$items = array(); |
| 102 |
$xmlItems = $this->feed->channel->xpath('item'); |
| 103 |
# Get all available items if the total items count is less that what is originally requested |
| 104 |
if (count($xmlItems) < $count) { |
| 105 |
$count = count($xmlItems); |
| 106 |
} |
| 107 |
# Read only items count specifed by $count param. |
| 108 |
for ($currentIndex = 0; $currentIndex < $count; $currentIndex++) { |
| 109 |
# Copy only title and link. |
| 110 |
$xmlItem = $xmlItems[$currentIndex]; |
| 111 |
# Read fields. |
| 112 |
$item = array(); |
| 113 |
foreach ($this->fields as $field) { |
| 114 |
$item[$field] = (string) $xmlItem->$field; |
| 115 |
} |
| 116 |
# Add to list |
| 117 |
$items[] = $item; |
| 118 |
} |
| 119 |
# Return items. |
| 120 |
return $items; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* put your comment there... |
| 125 |
* |
| 126 |
*/ |
| 127 |
protected function getPath() { |
| 128 |
return $this->path; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* put your comment there... |
| 133 |
* |
| 134 |
*/ |
| 135 |
public function getSite() { |
| 136 |
return $this->site; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* put your comment there... |
| 141 |
* |
| 142 |
*/ |
| 143 |
public function isError() { |
| 144 |
return !$this->feed || $this->feed->channel->attributes()->cjt_error; |
| 145 |
} |
| 146 |
|
| 147 |
} // End class. |
| 148 |
|