WordPressBridge.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. class WordPressBridge extends FeedExpander {
  3. const MAINTAINER = "aledeg";
  4. const NAME = "Wordpress Bridge";
  5. const URI = "https://wordpress.org/";
  6. const DESCRIPTION = "Returns the newest full posts of a Wordpress powered website";
  7. const PARAMETERS = array( array(
  8. 'url'=>array(
  9. 'name'=>'Blog URL',
  10. 'required'=>true
  11. )
  12. ));
  13. private function clearContent($content) {
  14. $content = preg_replace('/<script[^>]*>[^<]*<\/script>/', '', $content);
  15. $content = preg_replace('/<div class="wpa".*/', '', $content);
  16. $content = preg_replace('/<form.*\/form>/', '', $content);
  17. return $content;
  18. }
  19. protected function parseItem($newItem){
  20. $item=parent::parseItem($newItem);
  21. $article_html = getSimpleHTMLDOMCached($item['uri']);
  22. $article=null;
  23. switch(true){
  24. case !is_null($article_html->find('article',0)):
  25. // most common content div
  26. $article = $article_html->find('article', 0);
  27. break;
  28. case !is_null($article_html->find('.single-content',0)):
  29. // another common content div
  30. $article = $article_html->find('.single-content', 0);
  31. break;
  32. case !is_null($article_html->find('.post-content',0)):
  33. // another common content div
  34. $article = $article_html->find('.post-content', 0);
  35. break;
  36. case !is_null($article_html->find('.post',0)):
  37. // for old WordPress themes without HTML5
  38. $article = $article_html->find('.post', 0);
  39. break;
  40. }
  41. if(!is_null($article)){
  42. $item['content'] = $this->clearContent($article->innertext);
  43. }
  44. return $item;
  45. }
  46. public function getURI(){
  47. $url = $this->getInput('url');
  48. if(empty($url)){
  49. $url = static::URI;
  50. }
  51. return $url;
  52. }
  53. public function collectData(){
  54. if($this->getInput('url') && substr($this->getInput('url'),0,strlen('http'))!=='http'){
  55. // just in case someone find a way to access local files by playing with the url
  56. returnClientError('The url parameter must either refer to http or https protocol.');
  57. }
  58. $this->collectExpandableDatas($this->getURI().'/feed/atom/');
  59. }
  60. public function getCacheDuration() {
  61. return 3600*3; // 3 hours
  62. }
  63. }