WordPressBridge.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. class WordPressBridge extends BridgeAbstract {
  3. private $url;
  4. public function loadMetadatas() {
  5. $this->maintainer = "aledeg";
  6. $this->name = "Wordpress Bridge";
  7. $this->uri = "https://wordpress.org/";
  8. $this->description = "Returns the 3 newest full posts of a Wordpress blog";
  9. $this->update = "2016-08-02";
  10. $this->parameters[] =
  11. '[
  12. {
  13. "name" : "blog URL",
  14. "required" : "true",
  15. "identifier" : "url"
  16. }
  17. ]';
  18. }
  19. public function collectData(array $param) {
  20. function StripCDATA($string) {
  21. $string = str_replace('<![CDATA[', '', $string);
  22. $string = str_replace(']]>', '', $string);
  23. return $string;
  24. }
  25. function clearContent($content) {
  26. $content = preg_replace('/<script.*\/script>/', '', $content);
  27. $content = preg_replace('/<div class="wpa".*/', '', $content);
  28. return $content;
  29. }
  30. $this->processParams($param);
  31. if (!$this->hasUrl()) {
  32. $this->returnError('You must specify a URL', 400);
  33. }
  34. $this->url = $this->url.'/feed/atom';
  35. $html = $this->file_get_html($this->url) or $this->returnError("Could not request {$this->url}.", 404);
  36. $posts = $html->find('entry');
  37. if(!empty($posts) ) {
  38. $this->name = $html->find('title', 0)->plaintext;
  39. $i=0;
  40. foreach ($html->find('entry') as $article) {
  41. if($i < 3) {
  42. $this->items[$i]->uri = $article->find('link', 0)->getAttribute('href');
  43. $this->items[$i]->title = StripCDATA($article->find('title', 0)->plaintext);
  44. $this->items[$i]->author = trim($article->find('author', 0)->innertext);
  45. $this->items[$i]->timestamp = strtotime($article->find('updated', 0)->innertext);
  46. $article_html = $this->file_get_html($this->items[$i]->uri);
  47. $this->items[$i]->content = clearContent($article_html->find('article', 0)->innertext);
  48. if(empty($this->items[$i]->content))
  49. $this->items[$i]->content = clearContent($article_html->find('.post', 0)->innertext); // for old WordPress themes without HTML5
  50. $i++;
  51. }
  52. }
  53. }
  54. else {
  55. $this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
  56. }
  57. }
  58. public function getName() {
  59. return "{$this->name} - Wordpress Bridge";
  60. }
  61. public function getURI() {
  62. return $this->url;
  63. }
  64. public function getCacheDuration() {
  65. return 3600*3; // 3 hours
  66. }
  67. private function hasUrl() {
  68. if (empty($this->url)) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. private function processParams($param) {
  74. $this->url = $param['url'];
  75. }
  76. }