Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: CubePoints
- Plugin URI: http://techcube.net/cubepoints/
- 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><?php cp_displayPoints(); ?></code> in your template or activate the sidebar widget.
- Version: 2.1.2
- Author: Jonathan Lau & Peter Zhang
- Author URI: http://techcube.net
- */
- global $wpdb;
- define('POINTS', 'cpoints');
- define('RANK', 'cpRank');
- define('PLUGIN_DIR', get_bloginfo('url') . '/wp-content/plugins/'.dirname(plugin_basename(__FILE__)).'/');
- define('CPDB', $wpdb->prefix . 'cubepoints');
- define('CPRDB', $wpdb->prefix . 'cubepointsRanks');
- load_plugin_textdomain('cp', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/lang');
- if($wpdb->prefix == 'wp_1_'){
- $db_prefix = $wpmuBaseTablePrefix;
- }else {
- $db_prefix = $wpdb->prefix;
- }
- function cp_install() {
- // set default values
- add_option('cp_comment_points', 5);
- add_option('cp_reg_points', 100);
- add_option('cp_del_comment_points', 5);
- add_option('cp_post_points', 20);
- add_option('cp_prefix', '$');
- add_option('cp_suffix', '');
- add_option('cp_about_text', '?');
- add_option('cp_logs_enabled', true);
- add_option('cp_ranks_enabled', false);
- add_option('cp_daily_points', 5);
- add_option('cp_daily_points_time', 86400);
- add_option('cp_cron_auth_key', substr(md5(uniqid()),3,10));
- add_option('cp_about_posts', true);
- add_option('cp_about_comments', true);
- add_option('cp_mypoints', true);
- // create database
- global $wpdb;
- if($wpdb->get_var("SHOW TABLES LIKE '".CPDB."'") != CPDB || (int) get_option('cp_db_version') < 1.2) {
- $sql = "CREATE TABLE " . CPDB . " (
- id bigint(20) NOT NULL AUTO_INCREMENT,
- uid bigint(20) NOT NULL,
- type VARCHAR(55) NOT NULL,
- source VARCHAR(100) NOT NULL,
- points bigint(20) NOT NULL,
- timestamp bigint(20) NOT NULL,
- UNIQUE KEY id (id)
- );";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- add_option("cp_db_version", "1.2");
- }
- if ($wpdb->get_var("SHOW TABLES LIKE '".CPRDB."'") != CPRDB || (int) get_option('cp_db_version') < 1.2) {
- $sql = "CREATE TABLE " . CPRDB . " (
- name VARCHAR(55) NOT NULL UNIQUE,
- image VARCHAR(100),
- points bigint(20) NOT NULL
- );";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- add_option("cp_db_version", "1.2");
- }
- }
- //Actions
- add_action('comment_post', 'cp_newComment');
- add_action('delete_comment', 'cp_rmvComment');
- add_action('publish_post', 'cp_newPost');
- add_action('user_register', 'cp_newUser');
- //Hook for installation of CubePoints
- register_activation_hook(__FILE__, 'cp_install');
- //Hook for adding admin menus
- add_action('admin_menu', 'cp_admin');
- add_action('widgets_init', 'cp_pointsWidget_init');
- //Hook for posting top points
- add_filter('the_content', cp_topPointsPost);
- //Dontion
- //- Hook for JS
- add_action('init', 'cp_init',1);
- add_action('wp_footer', 'cp_footer',1000);
- //- Add link
- if (get_option('cp_donation')) {
- add_filter('comment_text', 'cp_addDonateLink', 1000);
- }
- if (get_option('cp_about_text')) {
- add_filter('comment_text', 'cp_addAboutLink', 1000);
- }
- include('cp_admin_manage.php');
- include('cp_admin_config.php');
- include('cp_admin_logs.php');
- include('cp_admin_modules.php');
- include('cp_admin_ranks.php');
- include('cp_widgets.php');
- function cp_admin() {
- add_menu_page('CubePoints', 'CubePoints', 8, 'cp_admin_manage', 'cp_admin_manage');
- add_submenu_page('cp_admin_manage', 'Manage', __('Manage','cp'), 8, 'cp_admin_manage', 'cp_admin_manage');
- add_submenu_page('cp_admin_manage', 'Configure', __('Configure','cp'), 8, 'cp_admin_config', 'cp_admin_config');
- add_submenu_page('cp_admin_manage', 'Logs', __('Logs','cp'), 8, 'cp_admin_logs', 'cp_admin_logs');
- add_submenu_page('cp_admin_manage', 'Modules', __('Modules (BETA)','cp'), 8, 'cp_admin_modules', 'cp_admin_modules');
- add_submenu_page('cp_admin_manage', 'Ranks', __('Ranks (BETA)','cp'), 8, 'cp_admin_ranks', 'cp_admin_ranks');
- if(get_option('cp_mypoints')) {
- add_submenu_page('cp_admin_manage', 'My Points', __('My Points','cp'), 0, 'cp_user_logs', 'cp_user_logs');
- }
- }
- function cp_log($type, $uid, $points, $source){
- $userinfo = get_userdata($uid);
- if($userinfo->user_login==''){ return false; }
- if($points==0 && $type!='reset'){ return false; }
- global $wpdb;
- $wpdb->query("INSERT INTO `".CPDB."` (`id`, `uid`, `type`, `source`, `points`, `timestamp`)
- VALUES (NULL, '".$uid."', '".$type."', '".$source."', '".$points."', ".time().");");
- return true;
- }
- function cp_getPoints($uid) {
- $points = get_usermeta($uid, POINTS);
- if ($points == '') {
- return 0;
- } else {
- return $points;
- }
- }
- function cp_updatePoints($uid, $points) {
- // no negative points
- if ($points < 0) {
- $points = 0;
- }
- update_usermeta($uid, POINTS, $points);
- update_usermeta($uid, RANK, cp_findRank($points));
- }
- function cp_alterPoints($uid, $points) {
- cp_updatePoints($uid, cp_getPoints($uid) + $points);
- }
- function cp_formatPoints($points){
- return get_option('cp_prefix') . $points . get_option('cp_suffix');
- }
- function cp_displayPoints($uid = 0, $return = 0, $format = 1) {
- if ($uid == 0) {
- if (!is_user_logged_in()) {
- return false;
- }
- $uid = cp_currentUser();
- }
- if ($format == 1) {
- $fpoints = cp_formatPoints(cp_getPoints($uid));
- } else {
- $fpoints = cp_getPoints($uid);
- }
- if (!$return ) {
- echo $fpoints;
- } else {
- return $fpoints;
- }
- }
- function cp_donatePoints($uid1, $uid2, $points) {
- $points = (int)$points;
- if ($points < 1) {
- return(__('Error: You must donate at least 1 point!','cp'));
- }
- if (cp_getPoints($uid1) < $points) {
- return(__('Error: Insufficient points to donate!','cp'));
- }
- cp_alterPoints($uid1, -$points);
- cp_alterPoints($uid2, $points);
- //log
- cp_log('donate',$uid1,-$points,$uid2);
- cp_log('donate',$uid2,$points,$uid1);
- $user_info = get_userdata($uid2);
- return sprintf(__("%s have been donated to %s",'cp'),cp_formatPoints($points),$user_info->user_login) . '.';
- }
- function cp_getAllPoints($amt=0,$filter_users=array(),$start=0){
- global $wpdb, $db_prefix;
- if($amt>0){ $limit = ' LIMIT ' . $start.','.$amt; }
- if (count($filter_users)>0){
- $extraquery = ' WHERE '.$db_prefix.'users.user_login != \'';
- $extraquery .= implode("' AND ".$db_prefix."users.user_login != '",$filter_users);
- $extraquery .= '\' ';
- }
- $array = $wpdb->get_results('SELECT '.$db_prefix.'users.id, '.$db_prefix.'users.user_login, '.$db_prefix.'users.display_name, '.$db_prefix.'usermeta.meta_value
- FROM `'.$db_prefix.'users`
- LEFT JOIN `'.$db_prefix.'usermeta` ON '.$db_prefix.'users.id = '.$db_prefix.'usermeta.user_id
- AND '.$db_prefix.'usermeta.meta_key=\''.POINTS.'\''.$extraquery.'
- ORDER BY '.$db_prefix.'usermeta.meta_value+0 DESC'
- . $limit . ';'
- ,ARRAY_A);
- return $array;
- }
- function cp_findRank($points) {
- global $wpdb;
- $result = $wpdb->get_var("SELECT `name` FROM `".CPRDB."` WHERE `points` <= ".$points." ORDER BY `points` DESC LIMIT 1");
- return $result;
- }
- function cp_getRankImage($points) {
- global $wpdb;
- $result = $wpdb->get_var("SELECT `image` FROM `".CPRDB."` WHERE `points` <= ".$points." ORDER BY `points` DESC LIMIT 1");
- if($result !='') {
- if(file_exists('../wp-content/plugins/cubepoints/ranks/'.$result) || file_exists('ranks/'.$result)){
- return '<img src="'.PLUGIN_DIR.'ranks/'.$result.'" />';
- }
- }
- }
- function cp_getRank($uid) {
- $rank = get_usermeta($uid, RANK);
- if ($rank == '') {
- return cp_findRank(0);
- } else {
- return $rank;
- }
- }
- function cp_newComment($cid) {
- if (is_user_logged_in()) {
- cp_alterPoints(cp_currentUser(), get_option('cp_comment_points'));
- //log
- cp_log('comment',cp_currentUser(),get_option('cp_comment_points'),$cid);
- }
- }
- function cp_newPost($pid) {
- $post = get_post($pid);
- $uid = $post->post_author;
- global $wpdb;
- $count = (int) $wpdb->get_var("select count(id) from `".CPDB."` where `type`='post' and `source`=". $pid);
- if($count==0){
- cp_alterPoints($uid, get_option('cp_post_points'));
- //log
- cp_log('post',$uid,get_option('cp_post_points'),$pid);
- }
- }
- function cp_newUser($uid) {
- global $wpdb;
- cp_alterPoints($uid, get_option('cp_reg_points'));
- //log
- cp_log('reg', $uid, get_option('cp_reg_points',''), $uid);
- }
- function cp_rmvComment($cid) {
- $comment = get_comment($cid);
- $uid = $comment->user_id;
- cp_alterPoints($uid, -get_option('cp_del_comment_points'));
- //log
- cp_log('comment',$uid,-get_option('cp_del_comment_points'),$cid);
- }
- function cp_topPointsDisplay($count = 10, $return = 0){
- global $wpdb;
- $options = get_option('cp_topPointsWidget');
- if(!is_array($options['cp_topPointsWidget_ufilter'])){$options['cp_topPointsWidget_ufilter']=array();}
- $arr = cp_getAllPoints($count, $options['cp_topPointsWidget_ufilter']);
- $i=0;
- $uniqid=uniqid();
- foreach ($arr as $row => $data) {
- if($data['user_login']!=''){
- $id = $wpdb->get_var("SELECT `ID` FROM `".$wpdb->users."` WHERE `user_login` = '".$data['user_login']."' LIMIT 1");
- $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>';
- }
- }
- $html = '<ul>'.$html.'</ul>';
- if($return==0){echo $html;}
- else { return $html; }
- }
- function cp_topPointsPost($content) {
- $options = get_option('cp_topPointsWidget');
- if(!is_array($options['cp_topPointsWidget_ufilter'])){$options['cp_topPointsWidget_ufilter']=array();}
- preg_match_all("/\[cubepoints (.*?) (.*?)\]/i", $content, $count);
- foreach($count[2] as $id=>$num){
- if(strToLower($count[1][$id])=='toplist'){
- if($num==''){$num = 10;}
- $content=str_replace($count[0][$id],cp_topPointsDisplay((int)$num,1),$content);
- }
- if(strToLower($count[1][$id])=='topuser-name'){
- if($num==''||$num<1){$num = 1;}
- $data = cp_getAllPoints(1,$options['cp_topPointsWidget_ufilter'],(int)$num-1);
- $content=str_replace($count[0][$id],$data[0]['display_name'],$content);
- }
- if(strToLower($count[1][$id])=='topuser-points'){
- if($num==''||$num<1){$num = 1;}
- $data = cp_getAllPoints(1,$options['cp_topPointsWidget_ufilter'],(int)$num-1);
- $content=str_replace($count[0][$id],$data[0]['meta_value'],$content);
- }
- }
- return $content;
- }
- function cp_addDonateLink($content){
- if (is_user_logged_in()) {
- global $comment;
- $userinfo = get_userdata($comment->user_id);
- if ($comment->user_id && $comment->user_id != cp_currentUser() && is_user_logged_in()) {
- $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>';
- }
- }
- return $content;
- }
- function cp_addAboutLink($content){
- global $comment;
- if ($comment->user_id) {
- $userinfo = get_userdata($comment->user_id);
- $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>';
- }
- return $content;
- }
- function cp_getTimer($uid) {
- $timer = get_usermeta($uid, 'cptimer');
- if ($timer == '') {
- return 0;
- } else {
- return $timer;
- }
- }
- function cp_updateTimer($uid) {
- update_usermeta($uid, 'cptimer', time());
- }
- function cp_checkTimer($uid) {
- $difference = time()-cp_getTimer($uid);
- if($difference>get_option('cp_daily_points_time')){
- //add points and update timer
- $points_to_add = get_option('cp_daily_points');
- cp_alterPoints($uid, $points_to_add);
- cp_log('login',$uid,$points_to_add,'-');
- cp_updateTimer($uid);
- }
- }
- function cp_init() {
- wp_enqueue_script('jquery');
- wp_enqueue_script('thickbox');
- wp_enqueue_style('thickbox');
- //Daily Points
- if(is_user_logged_in() && get_option('cp_daily_points') > 0 && get_option('cp_daily_points_time') > 0){
- cp_checkTimer(cp_currentUser());
- }
- }
- function cp_footer() {
- echo '<script type="text/javascript">
- var tb_pathToImage = "'.get_bloginfo('url').'/wp-includes/js/thickbox/loadingAnimation.gif";
- var tb_closeImage = "'.get_bloginfo('url').'/wp-includes/js/thickbox/tb-close.png"
- </script>';
- }
- // Helper functions
- function cp_currentUser() {
- global $current_user;
- get_currentuserinfo();
- return $current_user->ID;
- }
- function cp_relativeTime($timestamp){
- $difference = time() - $timestamp;
- $periods = array(__('sec','cp'), __('min','cp'), __('hour','cp'), __('day','cp'), __('week','cp'), __('month','cp'), __('year','cp'), __('decade','cp'));
- $lengths = array("60","60","24","7","4.35","12","10");
- if ($difference > 0) { // this was in the past
- $ending = __('ago','cp');
- } else { // this was in the future
- $difference = -$difference;
- $ending = __('to go','cp');
- }
- for($j = 0; $difference >= $lengths[$j]; $j++)
- $difference /= $lengths[$j];
- $difference = round($difference);
- if($difference != 1) $periods[$j].= 's';
- $text = "$difference $periods[$j] $ending";
- return $text;
- }
- function cp_curPageURL($page = "manage") {
- $link = "?page=cp_admin_".$page;
- $pageURL = 'http';
- if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"].$link;
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"].$link;
- }
- return $pageURL;
- }
- function cp_getPostCount($id){
- global $wpdb;
- return (int) $wpdb->get_var('SELECT count(id) FROM `'.$wpdb->prefix.'posts` where `post_type`=\'post\' and `post_status`=\'publish\' and `post_author`='.$id);
- }
- function cp_getCommentCount($id){
- global $wpdb;
- return (int) $wpdb->get_var('SELECT count(comment_ID) FROM `'.$wpdb->prefix.'comments` where `user_id`='.$id);
- }
- function processInput($input) {
- if(is_array($input)&&count($input)==0){return array();}
- $search = array('\r',' ', '"', "'", '%');
- $input = str_replace($search,"",strip_tags(stripslashes($input)));
- $input = str_replace(",","\n",$input);
- $input = array_unique(explode("\n",$input));
- foreach($input as $key=>$val){
- $input[$key] = trim($val);
- }
- foreach($input as $key => $value) {
- if($value == "") {
- unset($input[$key]);
- }
- }
- $input = array_values($input);
- return $input;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement