2014-05-26 19:45:10 +02:00
|
|
|
<?php
|
2016-08-04 20:06:53 +02:00
|
|
|
define('WORDPRESS_TYPE_ATOM', 1); // Content is of type ATOM
|
|
|
|
define('WORDPRESS_TYPE_RSS', 2); // Content is of type RSS
|
2016-09-10 19:11:09 +02:00
|
|
|
class WordPressBridge extends BridgeAbstract {
|
2014-05-26 19:45:10 +02:00
|
|
|
|
2016-08-04 21:56:11 +02:00
|
|
|
public $sitename; // Name of the site
|
2014-05-26 19:45:10 +02:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
const MAINTAINER = "aledeg";
|
|
|
|
const NAME = "Wordpress Bridge";
|
|
|
|
const URI = "https://wordpress.org/";
|
|
|
|
const DESCRIPTION = "Returns the 3 newest full posts of a Wordpress blog";
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2016-08-30 11:23:55 +02:00
|
|
|
const PARAMETERS = array( array(
|
2016-08-27 21:03:26 +02:00
|
|
|
'url'=>array(
|
|
|
|
'name'=>'Blog URL',
|
|
|
|
'required'=>true
|
|
|
|
)
|
|
|
|
));
|
2016-08-04 20:06:53 +02:00
|
|
|
|
2016-07-08 19:06:35 +02:00
|
|
|
// Replaces all 'link' tags with 'url' for simplehtmldom to actually find 'links' ('url')
|
2016-08-06 16:00:56 +02:00
|
|
|
private function ReplaceLinkTagsWithUrlTags($element){
|
2016-08-04 20:06:53 +02: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 20:40:38 +02:00
|
|
|
$element_text = str_replace('<link ', '<url ', $element_text);
|
2016-08-04 20:06:53 +02:00
|
|
|
return str_get_html($element_text);
|
|
|
|
}
|
|
|
|
|
2016-08-06 16:00:56 +02:00
|
|
|
private function StripCDATA($string) {
|
2016-08-04 20:15:28 +02:00
|
|
|
$string = str_replace('<![CDATA[', '', $string);
|
|
|
|
$string = str_replace(']]>', '', $string);
|
|
|
|
return $string;
|
|
|
|
}
|
2016-08-02 15:46:21 +02:00
|
|
|
|
2016-08-06 16:00:56 +02:00
|
|
|
private function ClearContent($content) {
|
2016-08-04 21:42:06 +02:00
|
|
|
$content = preg_replace('/<script[^>]*>[^<]*<\/script>/', '', $content);
|
2016-08-04 20:15:28 +02:00
|
|
|
$content = preg_replace('/<div class="wpa".*/', '', $content);
|
2016-08-04 21:42:06 +02:00
|
|
|
$content = preg_replace('/<form.*\/form>/', '', $content);
|
2016-08-04 20:15:28 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2016-08-02 15:46:21 +02:00
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData(){
|
2014-05-26 19:45:10 +02:00
|
|
|
|
2016-08-27 20:42:05 +02:00
|
|
|
$html = $this->getSimpleHTMLDOM($this->getURI().'/feed/atom')
|
|
|
|
or $this->returnServerError("Could not request ".$this->getURI().'/feed/atom');
|
2016-08-04 20:06:53 +02:00
|
|
|
|
|
|
|
// Notice: We requested an ATOM feed, however some sites return RSS feeds instead!
|
2016-08-27 20:42:05 +02: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 20:06:53 +02:00
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS)
|
|
|
|
$posts = $html->find('item');
|
|
|
|
else
|
|
|
|
$posts = $html->find('entry');
|
|
|
|
|
2016-08-04 20:45:21 +02:00
|
|
|
if(!empty($posts) ) {
|
2016-08-04 21:56:11 +02:00
|
|
|
$this->sitename = $html->find('title', 0)->plaintext;
|
2016-08-04 20:45:21 +02:00
|
|
|
$i=0;
|
2016-08-04 20:06:53 +02:00
|
|
|
|
|
|
|
foreach ($posts as $article) {
|
2014-05-26 19:45:10 +02:00
|
|
|
if($i < 3) {
|
2016-08-02 15:46:21 +02:00
|
|
|
|
2016-08-22 18:55:59 +02:00
|
|
|
$item = array();
|
2016-08-04 20:12:51 +02:00
|
|
|
|
2016-08-04 20:06:53 +02:00
|
|
|
$article = $this->ReplaceLinkTagsWithUrlTags($article);
|
|
|
|
|
|
|
|
if($type === WORDPRESS_TYPE_RSS){
|
2016-08-22 18:55:59 +02: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 20:06:53 +02:00
|
|
|
} else {
|
2016-08-22 18:55:59 +02: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 20:06:53 +02:00
|
|
|
}
|
2016-08-04 20:40:38 +02:00
|
|
|
|
2016-09-10 19:11:09 +02:00
|
|
|
$article_html = $this->getSimpleHTMLDOMCached($item['uri']);
|
2016-08-04 20:40:38 +02:00
|
|
|
|
2016-08-04 21:06:12 +02:00
|
|
|
// Attempt to find most common content div
|
2016-08-22 18:55:59 +02:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 21:06:12 +02:00
|
|
|
$article = $article_html->find('article', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 21:06:12 +02:00
|
|
|
}
|
2016-08-04 20:40:38 +02:00
|
|
|
}
|
2016-08-04 21:06:12 +02:00
|
|
|
|
|
|
|
// another common content div
|
2016-08-22 18:55:59 +02:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 21:06:12 +02:00
|
|
|
$article = $article_html->find('.single-content', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 21:06:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for old WordPress themes without HTML5
|
2016-08-22 18:55:59 +02:00
|
|
|
if(!isset($item['content'])){
|
2016-08-04 21:06:12 +02:00
|
|
|
$article = $article_html->find('.post', 0);
|
|
|
|
if(!empty($article)){
|
2016-08-22 18:55:59 +02:00
|
|
|
$item['content'] = $this->ClearContent($article->innertext);
|
2016-08-04 21:06:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 20:12:51 +02:00
|
|
|
$this->items[] = $item;
|
2014-05-26 19:45:10 +02:00
|
|
|
$i++;
|
|
|
|
}
|
2016-07-08 19:06:35 +02:00
|
|
|
}
|
2016-08-04 20:45:21 +02:00
|
|
|
} else {
|
2016-08-27 20:42:05 +02:00
|
|
|
$this->returnServerError("Sorry, ".$this->getURI()." doesn't seem to be a Wordpress blog.");
|
2014-05-26 19:45:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 20:42:05 +02:00
|
|
|
public function getURI(){
|
2016-08-28 01:25:33 +02:00
|
|
|
return $this->getInput('url');
|
2016-08-27 20:42:05 +02:00
|
|
|
}
|
|
|
|
|
2014-05-26 19:45:10 +02:00
|
|
|
public function getName() {
|
2016-08-04 21:56:11 +02:00
|
|
|
return "{$this->sitename} - Wordpress Bridge";
|
2014-05-26 19:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration() {
|
|
|
|
return 3600*3; // 3 hours
|
|
|
|
}
|
|
|
|
}
|