WordPressBridge.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * RssBridgeWordpress
  4. * Returns the 3 newest full posts of a Wordpress blog
  5. *
  6. * @name Wordpress Bridge
  7. * @homepage https://wordpress.com/
  8. * @description Returns the 3 newest full posts of a Wordpress blog
  9. * @maintainer aledeg
  10. * @update 2014-05-26
  11. * @use1(url="blog URL (required)", name="blog name")
  12. */
  13. class WordPressBridge extends BridgeAbstract {
  14. private $url;
  15. private $name;
  16. public function collectData(array $param) {
  17. $this->processParams($param);
  18. if (!$this->hasUrl()) {
  19. $this->returnError('You must specify a URL', 400);
  20. }
  21. $html = file_get_html($this->url) or $this->returnError("Could not request {$this->url}.", 404);
  22. $posts = $html->find('.post');
  23. if(!empty($posts) ) {
  24. $i=0;
  25. foreach ($html->find('.post') as $article) {
  26. if($i < 3) {
  27. $uri = $article->find('a', 0)->href;
  28. $this->items[] = $this->getDetails($uri);
  29. $i++;
  30. }
  31. }
  32. }
  33. else {
  34. $this->returnError("Sorry, {$this->url} doesn't seem to be a Wordpress blog.", 404);
  35. }
  36. }
  37. private function getDetails($uri) {
  38. $html = file_get_html($uri) or exit;
  39. $item = new \Item();
  40. $article = $html->find('.post', 0);
  41. $item->uri = $uri;
  42. $item->title = $article->find('h1', 0)->innertext;
  43. $item->content = $this->clearContent($article->find('.entry-content,.entry', 0)->innertext);
  44. $item->timestamp = $this->getDate($uri);
  45. return $item;
  46. }
  47. private function clearContent($content) {
  48. $content = preg_replace('/<script.*\/script>/', '', $content);
  49. $content = preg_replace('/<div class="wpa".*/', '', $content);
  50. return $content;
  51. }
  52. private function getDate($uri) {
  53. preg_match('/\d{4}\/\d{2}\/\d{2}/', $uri, $matches);
  54. $date = new \DateTime($matches[0]);
  55. return $date->format('U');
  56. }
  57. public function getName() {
  58. return "{$this->name} - Wordpress Bridge";
  59. }
  60. public function getURI() {
  61. return $this->url;
  62. }
  63. public function getCacheDuration() {
  64. return 3600*3; // 3 hours
  65. }
  66. private function hasUrl() {
  67. if (empty($this->url)) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. private function processParams($param) {
  73. $this->url = $param['url'];
  74. $this->name = $param['name'];
  75. }
  76. }