Advertisement
Guest User

Cubepoints.php

a guest
May 13th, 2010
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.42 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: CubePoints
  4. Plugin URI: http://techcube.net/cubepoints/
  5. Description: CubePoints is a point management system designed for Wordpress blogs. Users can earn points by posting comments on your site. To display user's points, just put <code>&lt;?php cp_displayPoints(); ?&gt;</code> in your template or activate the sidebar widget.
  6. Version: 2.1.2
  7. Author: Jonathan Lau & Peter Zhang
  8. Author URI: http://techcube.net
  9. */
  10.  
  11. global $wpdb;
  12.  
  13. define('POINTS', 'cpoints');
  14. define('RANK', 'cpRank');
  15. define('PLUGIN_DIR', get_bloginfo('url') . '/wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/');
  16. define('CPDB', $wpdb->prefix . 'cubepoints');
  17. define('CPRDB', $wpdb->prefix . 'cubepointsRanks');
  18. load_plugin_textdomain('cp', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/lang');
  19.  
  20. if($wpdb->prefix == 'wp_1_'){
  21. $db_prefix = $wpmuBaseTablePrefix;
  22. }else {
  23. $db_prefix = $wpdb->prefix;
  24. }
  25.  
  26. function cp_install() {
  27. // set default values
  28. add_option('cp_comment_points', 5);
  29. add_option('cp_reg_points', 100);
  30. add_option('cp_del_comment_points', 5);
  31. add_option('cp_post_points', 20);
  32. add_option('cp_prefix', '$');
  33. add_option('cp_suffix', '');
  34. add_option('cp_about_text', '?');
  35. add_option('cp_logs_enabled', true);
  36. add_option('cp_ranks_enabled', false);
  37. add_option('cp_daily_points', 5);
  38. add_option('cp_daily_points_time', 86400);
  39. add_option('cp_cron_auth_key', substr(md5(uniqid()),3,10));
  40. add_option('cp_about_posts', true);
  41. add_option('cp_about_comments', true);
  42. add_option('cp_mypoints', true);
  43.  
  44. // create database
  45.  
  46. global $wpdb;
  47.  
  48. if($wpdb->get_var("SHOW TABLES LIKE '".CPDB."'") != CPDB || (int) get_option('cp_db_version') < 1.2) {
  49. $sql = "CREATE TABLE " . CPDB . " (
  50. id bigint(20) NOT NULL AUTO_INCREMENT,
  51. uid bigint(20) NOT NULL,
  52. type VARCHAR(55) NOT NULL,
  53. source VARCHAR(100) NOT NULL,
  54. points bigint(20) NOT NULL,
  55. timestamp bigint(20) NOT NULL,
  56. UNIQUE KEY id (id)
  57. );";
  58. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  59. dbDelta($sql);
  60. add_option("cp_db_version", "1.2");
  61. }
  62.  
  63. if ($wpdb->get_var("SHOW TABLES LIKE '".CPRDB."'") != CPRDB || (int) get_option('cp_db_version') < 1.2) {
  64. $sql = "CREATE TABLE " . CPRDB . " (
  65. name VARCHAR(55) NOT NULL UNIQUE,
  66. image VARCHAR(100),
  67. points bigint(20) NOT NULL
  68. );";
  69. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  70. dbDelta($sql);
  71. add_option("cp_db_version", "1.2");
  72. }
  73. }
  74.  
  75. //Actions
  76. add_action('comment_post', 'cp_newComment');
  77. add_action('delete_comment', 'cp_rmvComment');
  78. add_action('publish_post', 'cp_newPost');
  79. add_action('user_register', 'cp_newUser');
  80. //Hook for installation of CubePoints
  81. register_activation_hook(__FILE__, 'cp_install');
  82.  
  83. //Hook for adding admin menus
  84. add_action('admin_menu', 'cp_admin');
  85. add_action('widgets_init', 'cp_pointsWidget_init');
  86. //Hook for posting top points
  87. add_filter('the_content', cp_topPointsPost);
  88. //Dontion
  89. //- Hook for JS
  90. add_action('init', 'cp_init',1);
  91. add_action('wp_footer', 'cp_footer',1000);
  92. //- Add link
  93. if (get_option('cp_donation')) {
  94. add_filter('comment_text', 'cp_addDonateLink', 1000);
  95. }
  96. if (get_option('cp_about_text')) {
  97. add_filter('comment_text', 'cp_addAboutLink', 1000);
  98. }
  99. include('cp_admin_manage.php');
  100. include('cp_admin_config.php');
  101. include('cp_admin_logs.php');
  102. include('cp_admin_modules.php');
  103. include('cp_admin_ranks.php');
  104. include('cp_widgets.php');
  105.  
  106. function cp_admin() {
  107. add_menu_page('CubePoints', 'CubePoints', 8, 'cp_admin_manage', 'cp_admin_manage');
  108. add_submenu_page('cp_admin_manage', 'Manage', __('Manage','cp'), 8, 'cp_admin_manage', 'cp_admin_manage');
  109. add_submenu_page('cp_admin_manage', 'Configure', __('Configure','cp'), 8, 'cp_admin_config', 'cp_admin_config');
  110. add_submenu_page('cp_admin_manage', 'Logs', __('Logs','cp'), 8, 'cp_admin_logs', 'cp_admin_logs');
  111. add_submenu_page('cp_admin_manage', 'Modules', __('Modules (BETA)','cp'), 8, 'cp_admin_modules', 'cp_admin_modules');
  112. add_submenu_page('cp_admin_manage', 'Ranks', __('Ranks (BETA)','cp'), 8, 'cp_admin_ranks', 'cp_admin_ranks');
  113. if(get_option('cp_mypoints')) {
  114. add_submenu_page('cp_admin_manage', 'My Points', __('My Points','cp'), 0, 'cp_user_logs', 'cp_user_logs');
  115. }
  116. }
  117.  
  118. function cp_log($type, $uid, $points, $source){
  119. $userinfo = get_userdata($uid);
  120. if($userinfo->user_login==''){ return false; }
  121. if($points==0 && $type!='reset'){ return false; }
  122.  
  123. global $wpdb;
  124. $wpdb->query("INSERT INTO `".CPDB."` (`id`, `uid`, `type`, `source`, `points`, `timestamp`)
  125. VALUES (NULL, '".$uid."', '".$type."', '".$source."', '".$points."', ".time().");");
  126. return true;
  127.  
  128. }
  129.  
  130. function cp_getPoints($uid) {
  131. $points = get_usermeta($uid, POINTS);
  132. if ($points == '') {
  133. return 0;
  134. } else {
  135. return $points;
  136. }
  137.  
  138.  
  139. }
  140.  
  141. function cp_updatePoints($uid, $points) {
  142. // no negative points
  143. if ($points < 0) {
  144. $points = 0;
  145. }
  146. update_usermeta($uid, POINTS, $points);
  147. update_usermeta($uid, RANK, cp_findRank($points));
  148. }
  149.  
  150. function cp_alterPoints($uid, $points) {
  151. cp_updatePoints($uid, cp_getPoints($uid) + $points);
  152. }
  153.  
  154. function cp_formatPoints($points){
  155. return get_option('cp_prefix') . $points . get_option('cp_suffix');
  156. }
  157.  
  158. function cp_displayPoints($uid = 0, $return = 0, $format = 1) {
  159. if ($uid == 0) {
  160. if (!is_user_logged_in()) {
  161. return false;
  162. }
  163. $uid = cp_currentUser();
  164. }
  165.  
  166. if ($format == 1) {
  167. $fpoints = cp_formatPoints(cp_getPoints($uid));
  168. } else {
  169. $fpoints = cp_getPoints($uid);
  170. }
  171.  
  172. if (!$return ) {
  173. echo $fpoints;
  174. } else {
  175. return $fpoints;
  176. }
  177. }
  178.  
  179. function cp_donatePoints($uid1, $uid2, $points) {
  180. $points = (int)$points;
  181. if ($points < 1) {
  182. return(__('Error: You must donate at least 1 point!','cp'));
  183. }
  184. if (cp_getPoints($uid1) < $points) {
  185. return(__('Error: Insufficient points to donate!','cp'));
  186. }
  187.  
  188. cp_alterPoints($uid1, -$points);
  189. cp_alterPoints($uid2, $points);
  190. //log
  191. cp_log('donate',$uid1,-$points,$uid2);
  192. cp_log('donate',$uid2,$points,$uid1);
  193.  
  194. $user_info = get_userdata($uid2);
  195.  
  196. return sprintf(__("%s have been donated to %s",'cp'),cp_formatPoints($points),$user_info->user_login) . '.';
  197. }
  198.  
  199.  
  200. function cp_getAllPoints($amt=0,$filter_users=array(),$start=0){
  201. global $wpdb, $db_prefix;
  202. if($amt>0){ $limit = ' LIMIT ' . $start.','.$amt; }
  203. if (count($filter_users)>0){
  204. $extraquery = ' WHERE '.$db_prefix.'users.user_login != \'';
  205. $extraquery .= implode("' AND ".$db_prefix."users.user_login != '",$filter_users);
  206. $extraquery .= '\' ';
  207. }
  208. $array = $wpdb->get_results('SELECT '.$db_prefix.'users.id, '.$db_prefix.'users.user_login, '.$db_prefix.'users.display_name, '.$db_prefix.'usermeta.meta_value
  209. FROM `'.$db_prefix.'users`
  210. LEFT JOIN `'.$db_prefix.'usermeta` ON '.$db_prefix.'users.id = '.$db_prefix.'usermeta.user_id
  211. AND '.$db_prefix.'usermeta.meta_key=\''.POINTS.'\''.$extraquery.'
  212. ORDER BY '.$db_prefix.'usermeta.meta_value+0 DESC'
  213. . $limit . ';'
  214. ,ARRAY_A);
  215. return $array;
  216.  
  217. }
  218.  
  219.  
  220. function cp_findRank($points) {
  221. global $wpdb;
  222. $result = $wpdb->get_var("SELECT `name` FROM `".CPRDB."` WHERE `points` <= ".$points." ORDER BY `points` DESC LIMIT 1");
  223. return $result;
  224. }
  225. function cp_getRankImage($points) {
  226. global $wpdb;
  227. $result = $wpdb->get_var("SELECT `image` FROM `".CPRDB."` WHERE `points` <= ".$points." ORDER BY `points` DESC LIMIT 1");
  228. if($result !='') {
  229. if(file_exists('../wp-content/plugins/cubepoints/ranks/'.$result) || file_exists('ranks/'.$result)){
  230. return '<img src="'.PLUGIN_DIR.'ranks/'.$result.'" />';
  231. }
  232. }
  233. }
  234. function cp_getRank($uid) {
  235. $rank = get_usermeta($uid, RANK);
  236. if ($rank == '') {
  237. return cp_findRank(0);
  238. } else {
  239. return $rank;
  240. }
  241. }
  242. function cp_newComment($cid) {
  243. if (is_user_logged_in()) {
  244. cp_alterPoints(cp_currentUser(), get_option('cp_comment_points'));
  245. //log
  246. cp_log('comment',cp_currentUser(),get_option('cp_comment_points'),$cid);
  247. }
  248. }
  249.  
  250. function cp_newPost($pid) {
  251. $post = get_post($pid);
  252. $uid = $post->post_author;
  253. global $wpdb;
  254. $count = (int) $wpdb->get_var("select count(id) from `".CPDB."` where `type`='post' and `source`=". $pid);
  255. if($count==0){
  256. cp_alterPoints($uid, get_option('cp_post_points'));
  257. //log
  258. cp_log('post',$uid,get_option('cp_post_points'),$pid);
  259. }
  260. }
  261.  
  262. function cp_newUser($uid) {
  263. global $wpdb;
  264. cp_alterPoints($uid, get_option('cp_reg_points'));
  265. //log
  266. cp_log('reg', $uid, get_option('cp_reg_points',''), $uid);
  267. }
  268.  
  269. function cp_rmvComment($cid) {
  270. $comment = get_comment($cid);
  271. $uid = $comment->user_id;
  272. cp_alterPoints($uid, -get_option('cp_del_comment_points'));
  273. //log
  274. cp_log('comment',$uid,-get_option('cp_del_comment_points'),$cid);
  275. }
  276.  
  277. function cp_topPointsDisplay($count = 10, $return = 0){
  278. global $wpdb;
  279. $options = get_option('cp_topPointsWidget');
  280. if(!is_array($options['cp_topPointsWidget_ufilter'])){$options['cp_topPointsWidget_ufilter']=array();}
  281. $arr = cp_getAllPoints($count, $options['cp_topPointsWidget_ufilter']);
  282. $i=0;
  283. $uniqid=uniqid();
  284. foreach ($arr as $row => $data) {
  285. if($data['user_login']!=''){
  286. $id = $wpdb->get_var("SELECT `ID` FROM `".$wpdb->users."` WHERE `user_login` = '".$data['user_login']."' LIMIT 1");
  287. $html .= '<li><a href="'.PLUGIN_DIR.'cp_about.php?u='.$id.'&width=500&height=200" title="CubePoints - '.__('About','cp').'" class="thickbox">'.$data['display_name'].' ('.cp_formatPoints((int) $data['meta_value']).')</a></li>';
  288. }
  289. }
  290. $html = '<ul>'.$html.'</ul>';
  291. if($return==0){echo $html;}
  292. else { return $html; }
  293.  
  294. }
  295.  
  296. function cp_topPointsPost($content) {
  297. $options = get_option('cp_topPointsWidget');
  298. if(!is_array($options['cp_topPointsWidget_ufilter'])){$options['cp_topPointsWidget_ufilter']=array();}
  299. preg_match_all("/\[cubepoints (.*?) (.*?)\]/i", $content, $count);
  300. foreach($count[2] as $id=>$num){
  301.  
  302. if(strToLower($count[1][$id])=='toplist'){
  303. if($num==''){$num = 10;}
  304. $content=str_replace($count[0][$id],cp_topPointsDisplay((int)$num,1),$content);
  305. }
  306.  
  307. if(strToLower($count[1][$id])=='topuser-name'){
  308. if($num==''||$num<1){$num = 1;}
  309. $data = cp_getAllPoints(1,$options['cp_topPointsWidget_ufilter'],(int)$num-1);
  310. $content=str_replace($count[0][$id],$data[0]['display_name'],$content);
  311. }
  312.  
  313.  
  314. if(strToLower($count[1][$id])=='topuser-points'){
  315. if($num==''||$num<1){$num = 1;}
  316. $data = cp_getAllPoints(1,$options['cp_topPointsWidget_ufilter'],(int)$num-1);
  317. $content=str_replace($count[0][$id],$data[0]['meta_value'],$content);
  318. }
  319.  
  320. }
  321.  
  322. return $content;
  323. }
  324.  
  325. function cp_addDonateLink($content){
  326. if (is_user_logged_in()) {
  327. global $comment;
  328. $userinfo = get_userdata($comment->user_id);
  329. if ($comment->user_id && $comment->user_id != cp_currentUser() && is_user_logged_in()) {
  330. $content .= '<a href="'.PLUGIN_DIR.'cp_donate.php?d=1&u='.$comment->user_id.'&width=500&height=200" title="CubePoints - '.__('Donate','cp').'" class="thickbox">Donate Points</a>';
  331. }
  332. }
  333. return $content;
  334. }
  335.  
  336. function cp_addAboutLink($content){
  337. global $comment;
  338. if ($comment->user_id) {
  339. $userinfo = get_userdata($comment->user_id);
  340. $content .= ' <a href="'.PLUGIN_DIR.'cp_about.php?u='.$comment->user_id.'&width=500&height=200" title="CubePoints - '.__('About','cp').'" class="thickbox">'.get_option('cp_about_text').'</a>';
  341. }
  342. return $content;
  343. }
  344. function cp_getTimer($uid) {
  345. $timer = get_usermeta($uid, 'cptimer');
  346. if ($timer == '') {
  347. return 0;
  348. } else {
  349. return $timer;
  350. }
  351. }
  352.  
  353. function cp_updateTimer($uid) {
  354. update_usermeta($uid, 'cptimer', time());
  355. }
  356.  
  357. function cp_checkTimer($uid) {
  358. $difference = time()-cp_getTimer($uid);
  359. if($difference>get_option('cp_daily_points_time')){
  360. //add points and update timer
  361. $points_to_add = get_option('cp_daily_points');
  362. cp_alterPoints($uid, $points_to_add);
  363. cp_log('login',$uid,$points_to_add,'-');
  364. cp_updateTimer($uid);
  365. }
  366. }
  367.  
  368. function cp_init() {
  369. wp_enqueue_script('jquery');
  370. wp_enqueue_script('thickbox');
  371. wp_enqueue_style('thickbox');
  372.  
  373. //Daily Points
  374. if(is_user_logged_in() && get_option('cp_daily_points') > 0 && get_option('cp_daily_points_time') > 0){
  375. cp_checkTimer(cp_currentUser());
  376. }
  377. }
  378. function cp_footer() {
  379. echo '<script type="text/javascript">
  380. var tb_pathToImage = "'.get_bloginfo('url').'/wp-includes/js/thickbox/loadingAnimation.gif";
  381. var tb_closeImage = "'.get_bloginfo('url').'/wp-includes/js/thickbox/tb-close.png"
  382. </script>';
  383. }
  384.  
  385. // Helper functions
  386. function cp_currentUser() {
  387. global $current_user;
  388. get_currentuserinfo();
  389. return $current_user->ID;
  390. }
  391.  
  392. function cp_relativeTime($timestamp){
  393. $difference = time() - $timestamp;
  394. $periods = array(__('sec','cp'), __('min','cp'), __('hour','cp'), __('day','cp'), __('week','cp'), __('month','cp'), __('year','cp'), __('decade','cp'));
  395. $lengths = array("60","60","24","7","4.35","12","10");
  396.  
  397. if ($difference > 0) { // this was in the past
  398. $ending = __('ago','cp');
  399. } else { // this was in the future
  400. $difference = -$difference;
  401. $ending = __('to go','cp');
  402. }
  403. for($j = 0; $difference >= $lengths[$j]; $j++)
  404. $difference /= $lengths[$j];
  405. $difference = round($difference);
  406. if($difference != 1) $periods[$j].= 's';
  407. $text = "$difference $periods[$j] $ending";
  408. return $text;
  409. }
  410.  
  411. function cp_curPageURL($page = "manage") {
  412. $link = "?page=cp_admin_".$page;
  413. $pageURL = 'http';
  414. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  415. $pageURL .= "://";
  416. if ($_SERVER["SERVER_PORT"] != "80") {
  417. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"].$link;
  418. } else {
  419. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"].$link;
  420. }
  421. return $pageURL;
  422. }
  423. function cp_getPostCount($id){
  424. global $wpdb;
  425. return (int) $wpdb->get_var('SELECT count(id) FROM `'.$wpdb->prefix.'posts` where `post_type`=\'post\' and `post_status`=\'publish\' and `post_author`='.$id);
  426. }
  427.  
  428. function cp_getCommentCount($id){
  429. global $wpdb;
  430. return (int) $wpdb->get_var('SELECT count(comment_ID) FROM `'.$wpdb->prefix.'comments` where `user_id`='.$id);
  431. }
  432. function processInput($input) {
  433. if(is_array($input)&&count($input)==0){return array();}
  434. $search = array('\r',' ', '"', "'", '%');
  435. $input = str_replace($search,"",strip_tags(stripslashes($input)));
  436. $input = str_replace(",","\n",$input);
  437. $input = array_unique(explode("\n",$input));
  438.  
  439. foreach($input as $key=>$val){
  440. $input[$key] = trim($val);
  441. }
  442.  
  443. foreach($input as $key => $value) {
  444. if($value == "") {
  445. unset($input[$key]);
  446. }
  447. }
  448. $input = array_values($input);
  449. return $input;
  450. }
  451. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement