PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/extras/wordpress/thinkup/classes/ThinkUpUser.class.php

https://github.com/devsatish/ThinkUp
PHP | 312 lines | 147 code | 33 blank | 132 comment | 27 complexity | a777dc08513e9198a5a8c23873b22096 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/extras/wordpress/thinkup/classes/ThinkUpUser.class.php
  5. *
  6. * Copyright (c) 2009-2011 Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. * ThinkUp User
  24. * This class allows you to instantiate a user and retrieve their information from the ThinkUp user database table.
  25. *
  26. * @author Sam Rose
  27. */
  28. class ThinkUpUser {
  29. /**
  30. * The username of the user.
  31. *
  32. * @var string
  33. */
  34. private $username;
  35. /**
  36. * The network that the user is on (e.g. Facebook, Twitter)
  37. *
  38. * @var string
  39. */
  40. private $network;
  41. /**
  42. * Cache variable for storing user info.
  43. *
  44. * @var object
  45. */
  46. private $user_info;
  47. /**
  48. * Constructor
  49. * @param str $username
  50. * @param str $network
  51. * @return ThinkUpUser
  52. */
  53. public function __construct($username, $network ='twitter') {
  54. if (!$username || $username == '') {
  55. $username = get_option('thinkup_twitter_username');
  56. }
  57. $this->username = $username;
  58. $this->network = $network;
  59. }
  60. /**
  61. * Returns the username of this user.
  62. *
  63. * @return string User's username
  64. */
  65. public function getUsername() {
  66. return $this->username;
  67. }
  68. /**
  69. * Returns an object as returned by $wpdb->get_row() of the user's row in the ThinkUp tu_users database table.
  70. *
  71. * @return array
  72. */
  73. public function getUserInfo() {
  74. if (!$this->user_info) {
  75. $wpdb = ThinkUpWordPressPlugin::getDatabaseConnection();
  76. $options_array = ThinkUpWordPressPlugin::getOptionsArray();
  77. // database may be on same server but not same db as wordpress
  78. $db = $wpdb->escape($options_array['thinkup_db']['value']);
  79. $prefix = $options_array['thinkup_table_prefix']['value'];
  80. $sql = $wpdb->prepare("SELECT * FROM $db.{$prefix}users
  81. WHERE user_name='%s'
  82. AND network='%s'", $this->username, $this->network);
  83. $this->user_info = $wpdb->get_row($sql);
  84. }
  85. return $this->user_info;
  86. }
  87. /**
  88. * Returns the user's ID.
  89. *
  90. * @return int
  91. */
  92. public function getUserID() {
  93. $user = $this->getUserInfo();
  94. return $user->user_id;
  95. }
  96. /**
  97. * Returns a string containing the network that this user belongs to.
  98. *
  99. * e.g. twitter, facebook
  100. *
  101. * @return str The network that this user belongs to.
  102. */
  103. public function getNetwork() {
  104. return $this->network;
  105. }
  106. /**
  107. * If the $text parameter is supplied, this function will return an anchor tagged link to this user's profile.
  108. *
  109. * If $text is not supplied, this function will return just a URL to this user's profile.
  110. *
  111. * This function is network aware.
  112. *
  113. * @param str $text
  114. * @return str
  115. */
  116. public function getLink($text = null) {
  117. if ($this->network == 'twitter') {
  118. if ($text) {
  119. return "<a href=\"http://twitter.com/{$this->getUsername()}".
  120. "\">{$text}</a>";
  121. } else {
  122. return "http://twitter.com/{$this->getUsername()}/";
  123. }
  124. } else if ($this->network == 'facebook') {
  125. if ($text) {
  126. return "<a href=\"http://facebook.com/profile.php?id={$this->getUserID()}\">{$text}</a>";
  127. } else {
  128. return "http://facebook.com/profile.php?id={$this->getUserID()}";
  129. }
  130. } else {
  131. // not implemented yet
  132. return null;
  133. }
  134. }
  135. /**
  136. * Returns an array of objects (as returned by $wpdb->get_results()) of this user's recent posts.
  137. *
  138. * @param int $count Amount of rows to return.
  139. * @return ObjectArray
  140. */
  141. public function getRecentPosts($count = 15, $order = 'DESC') {
  142. $wpdb = ThinkUpWordPressPlugin::getDatabaseConnection();
  143. $options_array = ThinkUpWordPressPlugin::getOptionsArray();
  144. // database may be on same server but not same db as wordpress
  145. $db = $wpdb->escape($options_array['thinkup_db']['value']);
  146. $prefix = $options_array['thinkup_table_prefix']['value'];
  147. if ($count >= 0) {
  148. $sql = $wpdb->prepare("
  149. SELECT *
  150. FROM $db.".$prefix."posts
  151. WHERE author_username='%s'
  152. AND in_reply_to_user_id is null
  153. AND network='%s'
  154. ORDER BY pub_date {$wpdb->escape($order)}
  155. LIMIT %d", $this->username, $this->network, $count);
  156. }
  157. else {
  158. $sql = $wpdb->prepare("
  159. SELECT *
  160. FROM $db.".$prefix."posts
  161. WHERE author_username='%s'
  162. AND in_reply_to_user_id is null
  163. AND network='%s'
  164. ORDER BY pub_date {$wpdb->escape($order)}",
  165. $this->username, $this->network);
  166. }
  167. return $wpdb->get_results($sql);
  168. }
  169. /**
  170. * Returns an array of ThinkUpPost objects of this user's recent posts.
  171. *
  172. * @param int $count Amount of rows to return.
  173. * @return ObjectArray
  174. */
  175. public function getRecentPostsAsPosts($count = 15, $order = 'DESC') {
  176. $posts = $this->getRecentPosts($count, $order);
  177. $posts_as_posts = array();
  178. foreach ($posts as $post) {
  179. $posts_as_posts[] = new ThinkUpPost($post->post_id, $post->network);
  180. }
  181. return $posts_as_posts;
  182. }
  183. /**
  184. * Displays the recent posts for this user.
  185. *
  186. * @param int $count The number of posts to show.
  187. * @param str $order The order in which to display these posts.
  188. * @param array $atts An array of attributes passed by the shortcode that calls this function.
  189. * @return str Recent posts ready for display.
  190. */
  191. public function displayRecentPosts($count = 15, $order = 'desc', $atts = null) {
  192. if (!is_array($atts)) {
  193. $atts = ThinkUpShortcodeHandler::getShortcodeAtts();
  194. }
  195. return self::displayPosts(
  196. $this->getRecentPostsAsPosts($count, $order), $atts);
  197. }
  198. /**
  199. * Displays a list of posts passed as an array of ThinkUpPost objects in the $posts parameter.
  200. *
  201. * @param string $posts The posts to display as ThinkUpPost objects.
  202. * @param array $atts An array of attributes passed by the shortcode that calls this function.
  203. * @return string The posts ready for display.
  204. */
  205. public static function displayPosts($posts, $atts = null) {
  206. if (!is_array($atts)) {
  207. $atts = ThinkUpShortcodeHandler::getShortcodeAtts();
  208. }
  209. if ($posts) {
  210. $return = '';
  211. foreach ($posts as $post) {
  212. $return .= $post->getParsedContent($atts);
  213. }
  214. return $atts['before'].$return.$atts['after'];
  215. } else {
  216. return $atts['before']."No posts to display.".$atts['after'];
  217. }
  218. }
  219. /**
  220. * Returns an array of this user's posts in chronological order based on the $order variable. Returns posts
  221. * as ThinkUpPost objects.
  222. *
  223. * @param str $order Either 'ASC' or 'DESC'.
  224. * @return array An array of ThinkupPost objects.
  225. */
  226. public function getChronologicalArchive($order = 'DESC') {
  227. return $this->getRecentPostsAsPosts(-1, $order);
  228. }
  229. /**
  230. * Parses a list of special tokens such as #username# and #post_id# into values that can be used in the attributes.
  231. *
  232. * @param array $atts
  233. * @return array
  234. */
  235. private function parseSpecialTokens($atts) {
  236. // check if these atts have already been parsed
  237. if (isset($atts['special_tokens_parsed'])) {
  238. return $atts;
  239. }
  240. foreach ($atts as $key => $att) {
  241. $atts[$key] = str_replace('#username#', $this->getUsername(), $atts[$key]);
  242. $atts[$key] = str_replace('#userlink#', $this->getLink(), $atts[$key]);
  243. }
  244. // flag these atts is parsed
  245. $atts['special_tokens_parsed'] = true;
  246. return $atts;
  247. }
  248. /**
  249. * Formats the chronological posts ready for being displayed. Primarily for use by the chronological
  250. * archive shortcode and takes a lot of arguments as a result.
  251. *
  252. * @param str $order
  253. * @param array $atts The attributes passed by the shortcode function.
  254. * @return str
  255. */
  256. public function getFormattedChronologicalArchive($order = 'DESC', $atts = null) {
  257. $posts = $this->getChronologicalArchive($order);
  258. if (!is_array($atts)) {
  259. $atts = ThinkUpShortcodeHandler::getShortcodeAtts();
  260. }
  261. $output = '';
  262. if ($posts) {
  263. $modified_title = str_replace('#username#', $this->username, $atts['title']);
  264. $modified_title = str_replace('#userlink#', $this->getLink(), $modified_title);
  265. $output .= "{$atts['before']}";
  266. $output .= "{$modified_title}";
  267. foreach ($posts as $post) {
  268. if ($post->getPostID() != 0) {
  269. $output .= $post->getParsedContent($atts);
  270. }
  271. }
  272. $output .= "{$atts['after']}";
  273. } else {
  274. $output .= "No posts found for this user.";
  275. }
  276. return $output;
  277. }
  278. }
  279. ?>