2014-05-26 21:45:10 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RssBridgeWordpress
|
|
|
|
* Returns the 3 newest full posts of a Wordpress blog
|
|
|
|
*
|
|
|
|
* @name Wordpress Bridge
|
2014-05-28 05:28:36 +04:00
|
|
|
* @homepage https://wordpress.com/
|
2014-05-26 21:45:10 +04:00
|
|
|
* @description Returns the 3 newest full posts of a Wordpress blog
|
2014-05-28 05:28:36 +04:00
|
|
|
* @maintainer aledeg
|
2015-09-05 15:40:31 +03:00
|
|
|
* @update 2015-09-05
|
2014-05-26 21:45:10 +04:00
|
|
|
* @use1(url="blog URL (required)", name="blog name")
|
|
|
|
*/
|
2014-07-14 21:12:01 +04:00
|
|
|
class WordPressBridge extends BridgeAbstract {
|
2014-05-26 21:45:10 +04:00
|
|
|
|
|
|
|
private $url;
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
public function collectData(array $param) {
|
|
|
|
$this->processParams($param);
|
|
|
|
|
|
|
|
if (!$this->hasUrl()) {
|
|
|
|
$this->returnError('You must specify a URL', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = file_get_html($this->url) or $this->returnError("Could not request {$this->url}.", 404);
|
2015-09-05 15:40:31 +03:00
|
|
|
$posts = $html->find('.post');
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2014-05-28 05:28:36 +04:00
|
|
|
if(!empty($posts) ) {
|
2014-05-26 21:45:10 +04:00
|
|
|
$i=0;
|
|
|
|
foreach ($html->find('.post') as $article) {
|
|
|
|
if($i < 3) {
|
|
|
|
$uri = $article->find('a', 0)->href;
|
2015-09-05 15:40:31 +03:00
|
|
|
$thumbnail = $article->find('img', 0)->src;
|
|
|
|
$this->items[] = $this->getDetails($uri, $thumbnail);
|
2014-05-26 21:45:10 +04:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 15:40:31 +03:00
|
|
|
private function getDetails($uri, $thumbnail) {
|
2014-05-26 21:45:10 +04:00
|
|
|
$html = file_get_html($uri) or exit;
|
2015-09-05 15:40:31 +03:00
|
|
|
$article = $html->find('.post', 0);
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2015-09-05 15:40:31 +03:00
|
|
|
$title = $article->find('h1', 0)->innertext;
|
|
|
|
if (strlen($title) == 0)
|
|
|
|
$title = $article->find('h2', 0)->innertext;
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2015-09-05 15:40:31 +03:00
|
|
|
$item = new \Item();
|
2014-05-26 21:45:10 +04:00
|
|
|
$item->uri = $uri;
|
2015-09-05 15:40:31 +03:00
|
|
|
$item->title = htmlspecialchars_decode($title);
|
|
|
|
$item->author = $article->find('a[rel=author]', 0)->innertext;
|
|
|
|
$item->thumbnailUri = $thumbnail;
|
2014-05-26 21:45:10 +04:00
|
|
|
$item->content = $this->clearContent($article->find('.entry-content,.entry', 0)->innertext);
|
|
|
|
$item->timestamp = $this->getDate($uri);
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function clearContent($content) {
|
|
|
|
$content = preg_replace('/<script.*\/script>/', '', $content);
|
|
|
|
$content = preg_replace('/<div class="wpa".*/', '', $content);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDate($uri) {
|
|
|
|
preg_match('/\d{4}\/\d{2}\/\d{2}/', $uri, $matches);
|
|
|
|
$date = new \DateTime($matches[0]);
|
|
|
|
return $date->format('U');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return "{$this->name} - Wordpress Bridge";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI() {
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration() {
|
|
|
|
return 3600*3; // 3 hours
|
|
|
|
}
|
|
|
|
|
|
|
|
private function hasUrl() {
|
|
|
|
if (empty($this->url)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processParams($param) {
|
|
|
|
$this->url = $param['url'];
|
|
|
|
$this->name = $param['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|