2014-05-26 21:45:10 +04:00
|
|
|
<?php
|
2016-08-04 21:06:53 +03:00
|
|
|
define('WORDPRESS_TYPE_ATOM', 1); // Content is of type ATOM
|
|
|
|
define('WORDPRESS_TYPE_RSS', 2); // Content is of type RSS
|
2014-07-14 21:12:01 +04:00
|
|
|
class WordPressBridge extends BridgeAbstract {
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2016-08-04 22:56:11 +03:00
|
|
|
public $sitename; // Name of the site
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2016-08-27 22:03:26 +03:00
|
|
|
public $maintainer = "aledeg";
|
|
|
|
public $name = "Wordpress Bridge";
|
|
|
|
public $uri = "https://wordpress.org/";
|
|
|
|
public $description = "Returns the 3 newest full posts of a Wordpress blog";
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-08-27 22:03:26 +03:00
|
|
|
public $parameters = array( array(
|
|
|
|
'url'=>array(
|
|
|
|
'name'=>'Blog URL',
|
|
|
|
'required'=>true
|
|
|
|
)
|
|
|
|
));
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-07-08 20:06:35 +03:00
|
|
|
// Replaces all 'link' tags with 'url' for simplehtmldom to actually find 'links' ('url')
|
2016-08-06 17:00:56 +03:00
|
|
|
private function ReplaceLinkTagsWithUrlTags($element){
|
2016-08-04 21:06:53 +03:00
|
|
|
// We need to fix the 'link' tag as simplehtmldom cannot parse it (just rename it and load back as dom)
|
|
|
|
$element_text = $element->outertext;
|
|
|
|
$element_text = str_replace('<link>', '<url>', $element_text);
|
|
|
|
$element_text = str_replace('</link>', '</url>', $element_text);
|
2016-08-04 21:40:38 +03:00
|
|
|
$element_text = str_replace('<link ', '<url ', $element_text);
|
2016-08-04 21:06:53 +03:00
|
|
|
return str_get_html($element_text);
|
|
|
|
}
|
|
|
|
|
2016-08-06 17:00:56 +03:00
|
|
|
private function StripCDATA($string) {
|
2016-08-04 21:15:28 +03:00
|
|
|
$string = str_replace('<![CDATA[', '', $string);
|
|
|
|
$string = str_replace(']]>', '', $string);
|
|
|
|
return $string;
|
|
|
|
}
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-06 17:00:56 +03:00
|
|
|
private function ClearContent($content) {
|
2016-08-04 22:42:06 +03:00
|
|
|
$content = preg_replace('/<script[^>]*>[^<]*<\/script>/', '', $content);
|
2016-08-04 21:15:28 +03:00
|
|
|
$content = preg_replace('/<div class="wpa".*/', '', $content);
|
2016-08-04 22:42:06 +03:00
|
|
|
$content = preg_replace('/<form.*\/form>/', '', $content);
|
2016-08-04 21:15:28 +03:00
|
|
|
return $content;
|
|
|
|
}
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2016-08-27 21:42:05 +03:00
|
|
|
$param=$this->parameters[$this->queriedContext];
|
2014-05-26 21:45:10 +04:00
|
|
|
|
2016-08-27 21:42:05 +03:00
|
|
|
$html = $this->getSimpleHTMLDOM($this->getURI().'/feed/atom')
|
|
|
|
or $this->returnServerError("Could not request ".$this->getURI().'/feed/atom');
|
2016-08-04 21:06:53 +03:00
|
|
|
|
|
|
|
// Notice: We requested an ATOM feed, however some sites return RSS feeds instead!
|
2016-08-27 21:42:05 +03:00
|
|
|
if($html->find('entry')){
|
|
|
|
$type=WORDPRESS_TYPE_ATOM;
|
|
|
|
}else if($html->find('item')){
|
|
|
|
$type=WORDPRESS_TYPE_RSS;
|
|
|
|
}else{
|
|
|
|
$type=WORDPRESS_TYPE_ATOM; // Make ATOM default
|
|
|
|
}
|
2016-08-04 21:06:53 +03:00
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS)
|
|
|
|
$posts = $html->find('item');
|
|
|
|
else
|
|
|
|
$posts = $html->find('entry');
|
|
|
|
|
2016-08-04 21:45:21 +03:00
|
|
|
if(!empty($posts) ) {
|
2016-08-04 22:56:11 +03:00
|
|
|
$this->sitename = $html->find('title', 0)->plaintext;
|
2016-08-04 21:45:21 +03:00
|
|
|
$i=0;
|
2016-08-04 21:06:53 +03:00
|
|
|
|
|
|
|
foreach ($posts as $article) {
|
2014-05-26 21:45:10 +04:00
|
|
|
if($i < 3) {
|
2016-08-02 16:46:21 +03:00
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = array();
|
2016-08-04 21:12:51 +03:00
|
|
|
|
2016-08-04 21:06:53 +03:00
|
|
|
$article = $this->ReplaceLinkTagsWithUrlTags($article);
|
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS){
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['uri'] = $article->find('url', 0)->innertext; // 'link' => 'url'!
|
|
|
|
$item['title'] = $article->find('title', 0)->plaintext;
|
|
|
|
$item['author'] = trim($this->StripCDATA($article->find('dc:creator', 0)->innertext));
|
|
|
|
$item['timestamp'] = strtotime($article->find('pubDate', 0)->innertext);
|
2016-08-04 21:06:53 +03:00
|
|
|
} else {
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['uri'] = $article->find('url', 0)->getAttribute('href'); // 'link' => 'url'!
|
|
|
|
$item['title'] = $this->StripCDATA($article->find('title', 0)->plaintext);
|
|
|
|
$item['author'] = trim($article->find('author', 0)->innertext);
|
|
|
|
$item['timestamp'] = strtotime($article->find('updated', 0)->innertext);
|
2016-08-04 21:06:53 +03:00
|
|
|
}
|
2016-08-04 21:40:38 +03:00
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$article_html = $this->getSimpleHTMLDOM($item['uri']);
|
2016-08-04 21:40:38 +03:00
|
|
|
|
2016-08-04 22:06:12 +03:00
|
|
|
// Attempt to find most common content div
|
2016-08-22 19:55:59 +03:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 22:06:12 +03:00
|
|
|
$article = $article_html->find('article', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 22:06:12 +03:00
|
|
|
}
|
2016-08-04 21:40:38 +03:00
|
|
|
}
|
2016-08-04 22:06:12 +03:00
|
|
|
|
|
|
|
// another common content div
|
2016-08-22 19:55:59 +03:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 22:06:12 +03:00
|
|
|
$article = $article_html->find('.single-content', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 22:06:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for old WordPress themes without HTML5
|
2016-08-22 19:55:59 +03:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 22:06:12 +03:00
|
|
|
$article = $article_html->find('.post', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 22:06:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:12:51 +03:00
|
|
|
$this->items[] = $item;
|
2014-05-26 21:45:10 +04:00
|
|
|
$i++;
|
|
|
|
}
|
2016-07-08 20:06:35 +03:00
|
|
|
}
|
2016-08-04 21:45:21 +03:00
|
|
|
} else {
|
2016-08-27 21:42:05 +03:00
|
|
|
$this->returnServerError("Sorry, ".$this->getURI()." doesn't seem to be a Wordpress blog.");
|
2014-05-26 21:45:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 21:42:05 +03:00
|
|
|
public function getURI(){
|
|
|
|
$param=$this->parameters[$this->queriedContext];
|
|
|
|
return $param['url']['value'];
|
|
|
|
}
|
|
|
|
|
2014-05-26 21:45:10 +04:00
|
|
|
public function getName() {
|
2016-08-04 22:56:11 +03:00
|
|
|
return "{$this->sitename} - Wordpress Bridge";
|
2014-05-26 21:45:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration() {
|
|
|
|
return 3600*3; // 3 hours
|
|
|
|
}
|
|
|
|
}
|