/extensions/twitter/extension.inc.php

https://github.com/mousey/wp-lifestream · PHP · 134 lines · 111 code · 16 blank · 7 comment · 10 complexity · 99bbaaae3a800b19191f7e4790c59a76 MD5 · raw file

  1. <?php
  2. class Lifestream_TwitterFeed extends Lifestream_Feed
  3. {
  4. const ID = 'twitter';
  5. const NAME = 'Twitter';
  6. const URL = 'http://www.twitter.com/';
  7. const LABEL = 'Lifestream_MessageLabel';
  8. const CAN_GROUP = false;
  9. const DESCRIPTION = 'Specifying your password will allow Lifestream to pull in protected updates from your profile. Your password is stored in plaintext in the database, so only do this is you have no other option.';
  10. const AUTHOR = 'David Cramer, Kyle McNally';
  11. function __toString()
  12. {
  13. return $this->get_option('username');
  14. }
  15. function get_options()
  16. {
  17. return array(
  18. 'username' => array($this->lifestream->__('Username:'), true, '', ''),
  19. 'hide_username' => array($this->lifestream->__('Hide Username'), false, true, false),
  20. 'hide_replies' => array($this->lifestream->__('Hide Replies'), false, true, false),
  21. );
  22. }
  23. function _get_user_link($match)
  24. {
  25. return $match[1].$this->get_user_link($match[2]);
  26. }
  27. function _get_search_term_link($match)
  28. {
  29. return $match[1].$this->lifestream->get_anchor_html(htmlspecialchars($match[2]), 'https://search.twitter.com/search?q='.urlencode($match[2]), array('class'=>'searchterm'));
  30. }
  31. function get_user_link($user)
  32. {
  33. return $this->lifestream->get_anchor_html('@'.htmlspecialchars($user), $this->get_user_url($user), array('class'=>'user'));
  34. }
  35. function get_user_url($user)
  36. {
  37. return 'http://www.twitter.com/'.urlencode($user);
  38. }
  39. function get_public_url()
  40. {
  41. return $this->get_user_url($this->get_option('username'));
  42. }
  43. function parse_users($text)
  44. {
  45. return preg_replace_callback('/([^\w]*)@([a-z0-9_\-\/]+)\b/i', array($this, '_get_user_link'), $text);
  46. }
  47. function parse_search_term($text)
  48. {
  49. return preg_replace_callback('/([^\w]*)(#[a-z0-9_\-\/]+)\b/i', array($this, '_get_search_term_link'), $text);
  50. }
  51. function get_url($page=1, $count=20)
  52. {
  53. return 'http://api.twitter.com/1/statuses/user_timeline.rss'
  54. . '?screen_name='.$this->get_option('username')
  55. . '&page='.$page
  56. . '&count='.$count;
  57. }
  58. function save()
  59. {
  60. $is_new = (bool)!$this->id;
  61. parent::save();
  62. if ($is_new)
  63. {
  64. // new feed -- attempt to import all statuses up to 2k
  65. $feed_msg = array(true, '');
  66. $page = 0;
  67. while ($feed_msg[0] !== false && $page < 10)
  68. {
  69. $page += 1;
  70. $feed_msg = $this->refresh($this->get_url($page, 200));
  71. }
  72. }
  73. }
  74. function render_item($row, $item)
  75. {
  76. $url = $this->parse_search_term($this->parse_users($this->parse_urls(htmlspecialchars($item['description']))));
  77. if($this->get_option('hide_username')) return $url;
  78. else return $url . ' ['.$this->lifestream->get_anchor_html(htmlspecialchars($this->get_option('username')), $item['link']).']';
  79. }
  80. function yield($row, $url, $key)
  81. {
  82. $data = parent::yield($row, $url, $key);
  83. $string = $this->get_option('username'). ': ';
  84. $description = $this->lifestream->html_entity_decode($row->get_description());
  85. if (lifestream_str_startswith(strtolower($description), strtolower($string)))
  86. {
  87. $description = substr($description, strlen($string));
  88. }
  89. if ($this->get_option('hide_replies') && lifestream_str_startswith($description, '@'))
  90. {
  91. return false;
  92. }
  93. $data['description'] = $description;
  94. return $data;
  95. }
  96. }
  97. /**
  98. * Displays your latest Twitter status.
  99. * @param {Boolean} $links Parse user links.
  100. */
  101. function lifestream_twitter_status($links=true)
  102. {
  103. global $lifestream;
  104. $event = $lifestream->get_single_event('twitter');
  105. if (!$event) return;
  106. if ($links)
  107. {
  108. // to render it with links
  109. echo $event->feed->render_item($event, $event->data[0]);
  110. }
  111. else
  112. {
  113. // or render just the text
  114. echo $event->data[0]['title'];
  115. }
  116. }
  117. $lifestream->register_feed('Lifestream_TwitterFeed');
  118. ?>