2014-05-26 19:45:10 +02:00
|
|
|
<?php
|
2014-07-14 19:12:01 +02:00
|
|
|
class WordPressBridge extends BridgeAbstract {
|
2014-05-26 19:45:10 +02:00
|
|
|
|
|
|
|
private $url;
|
|
|
|
|
2015-11-05 16:50:18 +01:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = "aledeg";
|
|
|
|
$this->name = "Wordpress Bridge";
|
2016-08-02 15:46:21 +02:00
|
|
|
$this->uri = "https://wordpress.org/";
|
2015-11-05 16:50:18 +01:00
|
|
|
$this->description = "Returns the 3 newest full posts of a Wordpress blog";
|
2016-08-02 15:46:21 +02:00
|
|
|
$this->update = "2016-08-02";
|
2015-11-05 16:50:18 +01:00
|
|
|
|
|
|
|
$this->parameters[] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "blog URL",
|
|
|
|
"required" : "true",
|
|
|
|
"identifier" : "url"
|
|
|
|
}
|
|
|
|
]';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-26 19:45:10 +02:00
|
|
|
public function collectData(array $param) {
|
2016-08-02 15:46:21 +02:00
|
|
|
|
|
|
|
function StripCDATA($string) {
|
|
|
|
$string = str_replace('<![CDATA[', '', $string);
|
|
|
|
$string = str_replace(']]>', '', $string);
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearContent($content) {
|
|
|
|
$content = preg_replace('/<script.*\/script>/', '', $content);
|
|
|
|
$content = preg_replace('/<div class="wpa".*/', '', $content);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2014-05-26 19:45:10 +02:00
|
|
|
$this->processParams($param);
|
|
|
|
|
|
|
|
if (!$this->hasUrl()) {
|
|
|
|
$this->returnError('You must specify a URL', 400);
|
|
|
|
}
|
|
|
|
|
2016-08-02 15:46:21 +02:00
|
|
|
$this->url = $this->url.'/feed/atom';
|
2016-06-25 23:17:42 +02:00
|
|
|
$html = $this->file_get_html($this->url) or $this->returnError("Could not request {$this->url}.", 404);
|
2016-08-02 15:46:21 +02:00
|
|
|
$posts = $html->find('entry');
|
|
|
|
if(!empty($posts) ) {
|
|
|
|
$this->name = $html->find('title', 0)->plaintext;
|
|
|
|
$i=0;
|
|
|
|
foreach ($html->find('entry') as $article) {
|
2014-05-26 19:45:10 +02:00
|
|
|
if($i < 3) {
|
2016-08-02 15:46:21 +02:00
|
|
|
$this->items[$i]->uri = $article->find('link', 0)->getAttribute('href');
|
|
|
|
$this->items[$i]->title = StripCDATA($article->find('title', 0)->plaintext);
|
|
|
|
$this->items[$i]->author = trim($article->find('author', 0)->innertext);
|
|
|
|
$this->items[$i]->timestamp = strtotime($article->find('updated', 0)->innertext);
|
|
|
|
|
|
|
|
$article_html = $this->file_get_html($this->items[$i]->uri);
|
|
|
|
$this->items[$i]->content = clearContent($article_html->find('article', 0)->innertext);
|
2016-08-02 17:48:57 +02:00
|
|
|
if(empty($this->items[$i]->content))
|
|
|
|
$this->items[$i]->content = clearContent($article_html->find('.single-content', 0)->innertext); // another common content div
|
2016-08-02 15:46:21 +02:00
|
|
|
if(empty($this->items[$i]->content))
|
|
|
|
$this->items[$i]->content = clearContent($article_html->find('.post', 0)->innertext); // for old WordPress themes without HTML5
|
|
|
|
|
2014-05-26 19:45:10 +02:00
|
|
|
$i++;
|
|
|
|
}
|
2016-08-02 15:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-26 19:45:10 +02:00
|
|
|
else {
|
|
|
|
$this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|