PageRenderTime 34ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/twitter.php

https://github.com/jrcryer/twitter-for-wordpress
PHP | 296 lines | 181 code | 31 blank | 84 comment | 7 complexity | db55a592869e7ac7244b135f1c3b1e69 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Simple Twitter for Wordpress
  4. Version: 1.0
  5. Plugin URI: http://www.twitter.com/jrcryer
  6. Description: Displays your public Twitter messages for all to read.
  7. Author: James Cryer
  8. Author URI: http://www.twitter.com/jrcryer
  9. */
  10. define('MAGPIE_CACHE_ON', 1); //2.7 Cache Bug
  11. define('MAGPIE_CACHE_AGE', 180);
  12. define('MAGPIE_INPUT_ENCODING', 'UTF-8');
  13. define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
  14. class TwitterFeed {
  15. public function __contruct() {}
  16. /**
  17. * Extracts the feed items from Twitter
  18. *
  19. * @param string $username
  20. * @param integer $feedItemsNum
  21. * @param bool $encode
  22. * @param bool $extractLinks
  23. * @param bool $extractUsers
  24. * @return array
  25. */
  26. public function getFeedItems($username, $feedItemsNum = 5, $encode = true, $extractLinks = true, $extractUsers = true) {
  27. $aMessage = $this->getTwitterMessages($username, $feedItemsNum);
  28. if(empty($aMessage)) {
  29. return array();
  30. }
  31. $aMessage = $this->parseMessages($aMessage, $encode, $extractLinks, $extractUsers);
  32. return $aMessage;
  33. }
  34. /**
  35. * Returns the messages from the supplied users limited by the number
  36. * of items to display
  37. *
  38. * @param string $username
  39. * @param int $num
  40. * @return array
  41. */
  42. protected function getTwitterMessages($username, $num = 5) {
  43. include_once(ABSPATH . WPINC . '/rss.php');
  44. $aMessage = fetch_rss('http://api.twitter.com/1/statuses/user_timeline/'.$username.'.rss');
  45. if(empty($aMessage)) {
  46. return array();
  47. }
  48. $aMessage = array_slice($aMessage->items, 0, $num);
  49. return $aMessage;
  50. }
  51. /**
  52. * Encodes all message content with UTF-8 encoding
  53. *
  54. * @param array $aMessage
  55. * @return array
  56. */
  57. protected function parseMessages($aMessage, $encode, $extractLinks, $extractUsers) {
  58. $aParsedMsg = array();
  59. foreach($aMessage as $item) {
  60. $content = " ".substr(strstr($item['description'],': '), 2, strlen($item['description']))." ";
  61. if($encode) {
  62. $content = utf8_encode($content);
  63. }
  64. if($extractLinks) {
  65. $content = $this->extractHyperlinks($content);
  66. }
  67. if($extractUsers) {
  68. $content = $this->extractUsers($content);
  69. }
  70. $item['description'] = $content;
  71. $item['date-posted'] = $this->getMessageTimestamp($item['pubdate']);
  72. $aParsedMsg[] = $item;
  73. }
  74. return $aParsedMsg;
  75. }
  76. /**
  77. * Returns the message time stamp based on the publishDate
  78. *
  79. * @param int $publishDate
  80. * @return string
  81. */
  82. protected function getMessageTimestamp($publishDate) {
  83. $h_time = null;
  84. $time = strtotime($publishDate);
  85. if ( ( abs( time() - $time) ) < 86400 ) {
  86. $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
  87. }else {
  88. $h_time = date(__('Y/m/d'), $time);
  89. }
  90. return sprintf( __('%s', 'twitter-for-wordpress'),' <span class="twitter-timestamp"><abbr title="' . date(__('Y/m/d H:i:s'), $time) . '">' . $h_time . '</abbr></span>' );
  91. }
  92. /**
  93. * Extract the links from the messages
  94. *
  95. * @param string $text
  96. * @return string
  97. */
  98. private function extractHyperlinks($text) {
  99. // Props to Allen Shaw & webmancers.com
  100. // match protocol://address/path/file.extension?some=variable&another=asf%
  101. //$text = preg_replace("/\b([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
  102. $text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
  103. // match www.something.domain/path/file.extension?some=variable&another=asf%
  104. //$text = preg_replace("/\b(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);
  105. $text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);
  106. // match name@address
  107. $text = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","<a href=\"mailto://$1\" class=\"twitter-link\">$1</a>", $text);
  108. //mach #trendingtopics. Props to Michael Voigt
  109. $text = preg_replace('/([\.|\,|\:|\�|\�|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/#search?q=$2\" class=\"twitter-link\">#$2</a>$3 ", $text);
  110. return $text;
  111. }
  112. /**
  113. * Extract the user from the messages
  114. *
  115. * @param string $text
  116. * @return string
  117. */
  118. private function extractUsers($text) {
  119. $text = preg_replace('/([\.|\,|\:|\�|\�|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ", $text);
  120. return $text;
  121. }
  122. }
  123. class TwitterWidget extends WP_Widget {
  124. public function TwitterWidget() {
  125. parent::WP_Widget(false, $name = 'TwitterWidget');
  126. }
  127. public function form($instance) {
  128. $title = esc_attr($instance['title']);
  129. $username = esc_attr($instance['username']);
  130. $number = esc_attr($instance['num']);
  131. $update = esc_attr($instance['update']);
  132. $linked = esc_attr($instance['linked']);
  133. $hyperlinks = esc_attr($instance['hyperlinks']);
  134. $twitter_users = esc_attr($instance['twitter_users']);
  135. $encode = esc_attr($instance['encode_utf8']);
  136. ?>
  137. <p>
  138. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?>
  139. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  140. </label>
  141. </p>
  142. <p>
  143. <label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Twitter username:'); ?>
  144. <input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo $username; ?>" />
  145. </label>
  146. </p>
  147. <p>
  148. <label for="<?php echo $this->get_field_id('num'); ?>"><?php _e('Number of tweets:'); ?>
  149. <input class="widefat" id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="text" value="<?php echo $number; ?>" />
  150. </label>
  151. </p>
  152. <p>
  153. <label for="<?php echo $this->get_field_id('update'); ?>"><?php _e('Show date posted:'); ?>
  154. <input id="<?php echo $this->get_field_id('update'); ?>" name="<?php echo $this->get_field_name('update'); ?>" type="checkbox" <?php echo $update? 'checked="chcked"' : ''; ?> />
  155. </label>
  156. </p>
  157. <p>
  158. <label for="<?php echo $this->get_field_id('hyperlinks'); ?>"><?php _e('Discover hyperlinks:'); ?>
  159. <input id="<?php echo $this->get_field_id('hyperlinks'); ?>" name="<?php echo $this->get_field_name('hyperlinks'); ?>" type="checkbox" <?php echo $hyperlinks ? 'checked="checked"' : ''; ?> />
  160. </label>
  161. </p>
  162. <p>
  163. <label for="<?php echo $this->get_field_id('twitter_users'); ?>"><?php _e('Discover @replies:'); ?>
  164. <input id="<?php echo $this->get_field_id('twitter_users'); ?>" name="<?php echo $this->get_field_name('twitter_users'); ?>" type="checkbox" <?php echo $twitter_users ? 'checked="checked"' : ''; ?> />
  165. </label>
  166. </p>
  167. <p>
  168. <label for="<?php echo $this->get_field_id('encode_utf8'); ?>"><?php _e('UTF8 Encode:'); ?>
  169. <input id="<?php echo $this->get_field_id('encode_utf8'); ?>" name="<?php echo $this->get_field_name('encode_utf8'); ?>" type="checkbox" <?php echo $encode ? 'checked="checked"' : ''; ?> />
  170. </label>
  171. </p>
  172. <?php
  173. }
  174. /**
  175. * Update a widget values
  176. *
  177. * @param array $new_instance
  178. * @param array $old_instance
  179. * @return array
  180. */
  181. public function update($new_instance, $old_instance) {
  182. $instance = $old_instance;
  183. $instance['title'] = strip_tags($new_instance['title']);
  184. $instance['username'] = strip_tags($new_instance['username']);
  185. $instance['num'] = strip_tags($new_instance['num']);
  186. $instance['update'] = strip_tags($new_instance['update']);
  187. $instance['linked'] = strip_tags($new_instance['linked']);
  188. $instance['hyperlinks'] = strip_tags($new_instance['hyperlinks']);
  189. $instance['twitter_users'] = strip_tags($new_instance['twitter_users']);
  190. $instance['encode_utf8'] = strip_tags($new_instance['encode_utf8']);
  191. return $instance;
  192. }
  193. /**
  194. * Process the widget and output the content
  195. *
  196. * @param array $args
  197. * @param array $instance
  198. * @return string
  199. */
  200. public function widget($args, $instance) {
  201. extract($args);
  202. $username = $instance['username'];
  203. $num = $instance['num'];
  204. $list = $instance['list'];
  205. $update = $instance['update'];
  206. $linked = $instance['linked'];
  207. $extractLinks = $instance['hyperlinks'];
  208. $extractUsers = $instance['twitter_users'];
  209. $encode = $instance['utf8_encode'];
  210. if(empty($username)) {
  211. return false;
  212. }
  213. $oFeed = new TwitterFeed();
  214. $aMessage = $oFeed->getFeedItems($username, $num, $encode, $extractLinks, $extractUsers);
  215. $content = empty($aMessage) ?
  216. '<p class="notice">There are no public messages.</p>' :
  217. $this->generateMessageOutput($aMessage, $update);
  218. echo sprintf(
  219. "%s%s%s%s%s",
  220. $before_widget,
  221. $before_title,
  222. $this->getTitle($instance),
  223. $after_title,
  224. $content,
  225. $after_widget
  226. );
  227. }
  228. /**
  229. * Returns the HTML for the title of the widget
  230. *
  231. * @param array $instance
  232. * @return string
  233. */
  234. protected function getTitle($instance) {
  235. $title = apply_filters('widget_title', $instance['title']);
  236. $username = $instance['username'];
  237. return sprintf(
  238. '<a class="twitter-title" href="http://twitter.com/%1$s" class="twitter_title_link">%2$s</a>'.
  239. '<a class="twitter-icon" href="http://www.twitter.com/%1$s"><img src="http://twitter-badges.s3.amazonaws.com/t_small-b.png" alt="Follow jrcryer on Twitter"/></a>',
  240. $username, $title
  241. );
  242. }
  243. /**
  244. * Generates HTML list of feed items
  245. *
  246. * @param arrray $aMessage
  247. * @param bool $update
  248. * @return string
  249. */
  250. protected function generateMessageOutput($aMessage, $update = true) {
  251. $output = '<ul class="twitter">';
  252. foreach ( $aMessage as $item ) {
  253. $content = $item['description'];
  254. $link = $item['link'];
  255. $output .= sprintf(
  256. '<li class="twitter-item">%s%s</li>',
  257. $content,
  258. $update ? sprintf('<a href="%s" class="twitter-link">%s</a>', $link, $item['date-posted']) : ''
  259. );
  260. }
  261. $output .= '</ul>';
  262. return $output;
  263. }
  264. }
  265. add_action('widgets_init', create_function('', 'return register_widget("TwitterWidget");'));