| @@ -1,114 +1,73 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | -* | |
| 3 | +* | |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | -* | |
| 7 | +* | |
| 8 | 8 | */ |
| 9 | 9 | class CJT_Framework_Wordpress_Feed { |
| 10 | - | |
| 10 | + | |
| 11 | 11 | /** |
| 12 | 12 | * put your comment there... |
| 13 | - * | |
| 13 | + * | |
| 14 | 14 | * @var mixed |
| 15 | 15 | */ |
| 16 | 16 | protected $feed; |
| 17 | - | |
| 17 | + | |
| 18 | 18 | /** |
| 19 | 19 | * put your comment there... |
| 20 | - * | |
| 20 | + * | |
| 21 | 21 | * @var mixed |
| 22 | 22 | */ |
| 23 | 23 | protected $fields; |
| 24 | - | |
| 24 | + | |
| 25 | 25 | /** |
| 26 | 26 | * put your comment there... |
| 27 | - * | |
| 27 | + * | |
| 28 | 28 | * @var mixed |
| 29 | 29 | */ |
| 30 | 30 | protected $path; |
| 31 | - | |
| 31 | + | |
| 32 | 32 | /** |
| 33 | 33 | * put your comment there... |
| 34 | - * | |
| 34 | + * | |
| 35 | 35 | * @var mixed |
| 36 | 36 | */ |
| 37 | 37 | protected $site; |
| 38 | - | |
| 38 | + | |
| 39 | 39 | /** |
| 40 | 40 | * put your comment there... |
| 41 | - * | |
| 41 | + * | |
| 42 | 42 | * @param mixed $site |
| 43 | 43 | * @param mixed $path |
| 44 | 44 | * @return CJT_Framework_Wordpress_Feed |
| 45 | 45 | */ |
| 46 | - public function __construct($site, $path, $fields) | |
| 47 | - { | |
| 48 | - | |
| 46 | + public function __construct($site, $path, $fields) { | |
| 47 | + # Initialize. | |
| 49 | 48 | $this->site =& $site; |
| 50 | 49 | $this->path =& $path; |
| 51 | 50 | $this->fields = $fields; |
| 52 | - | |
| 53 | 51 | # 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>https://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; | |
| 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)); | |
| 89 | 55 | } |
| 90 | - # Return items. | |
| 91 | - return $items; | |
| 92 | 56 | } |
| 93 | 57 | |
| 94 | 58 | /** |
| 95 | 59 | * put your comment there... |
| 96 | - * | |
| 60 | + * | |
| 97 | 61 | * @param mixed $count |
| 98 | 62 | */ |
| 99 | 63 | public function getLatestItems($count) { |
| 100 | - # Initialize. | |
| 64 | + // Initialize. | |
| 101 | 65 | $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. | |
| 66 | + // Read only items count specifed by $count param. | |
| 108 | 67 | for ($currentIndex = 0; $currentIndex < $count; $currentIndex++) { |
| 109 | 68 | # Copy only title and link. |
| 110 | - $xmlItem = $xmlItems[$currentIndex]; | |
| 69 | + $xmlItem = $this->feed->channel->item[$currentIndex]; | |
| 111 | 70 | # Read fields. |
| 112 | 71 | $item = array(); |
| 113 | 72 | foreach ($this->fields as $field) { |
| 114 | 73 | $item[$field] = (string) $xmlItem->$field; |
| @@ -116,22 +75,22 @@ | ||
| 116 | 75 | # Add to list |
| 117 | 76 | $items[] = $item; |
| 118 | 77 | } |
| 119 | 78 | # Return items. |
| 120 | - return $items; | |
| 79 | + return $items; | |
| 121 | 80 | } |
| 122 | 81 | |
| 123 | 82 | /** |
| 124 | 83 | * put your comment there... |
| 125 | - * | |
| 84 | + * | |
| 126 | 85 | */ |
| 127 | 86 | protected function getPath() { |
| 128 | 87 | return $this->path; |
| 129 | 88 | } |
| 130 | - | |
| 89 | + | |
| 131 | 90 | /** |
| 132 | 91 | * put your comment there... |
| 133 | - * | |
| 92 | + * | |
| 134 | 93 | */ |
| 135 | 94 | public function getSite() { |
| 136 | 95 | return $this->site; |
| 137 | 96 | } |
| @@ -137,11 +96,11 @@ | ||
| 137 | 96 | } |
| 138 | 97 | |
| 139 | 98 | /** |
| 140 | 99 | * put your comment there... |
| 141 | - * | |
| 100 | + * | |
| 142 | 101 | */ |
| 143 | 102 | public function isError() { |
| 144 | - return !$this->feed || $this->feed->channel->attributes()->cjt_error; | |
| 103 | + return !$this->feed; | |
| 145 | 104 | } |
| 146 | 105 | |
| 147 | 106 | } // End class. |