WordPressBridge.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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('.single-content', 0)->innertext); // another common content div
  50. if(empty($this->items[$i]->content))
  51. $this->items[$i]->content = clearContent($article_html->find('.post', 0)->innertext); // for old WordPress themes without HTML5
  52. $i++;
  53. }
  54. }
  55. }
  56. else {
  57. $this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
  58. }
  59. }
  60. public function getName() {
  61. return "{$this->name} - Wordpress Bridge";
  62. }
  63. public function getURI() {
  64. return $this->url;
  65. }
  66. public function getCacheDuration() {
  67. return 3600*3; // 3 hours
  68. }
  69. private function hasUrl() {
  70. if (empty($this->url)) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. private function processParams($param) {
  76. $this->url = $param['url'];
  77. }
  78. }