Advertisement
Guest User

WordPress Cube Point Widget

a guest
Jan 30th, 2012
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.79 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: CubePoints - More Widgets
  4. Plugin URI: http://www.boiteaweb.fr
  5. Description: Add 1 new widget for CubePoints plugin
  6. Version: 1.0
  7. Author: Juliobox
  8. Author URI: http://www.boiteaweb.fr
  9. */
  10. add_action('widgets_init', 'month_cp_widgets');
  11.  
  12. function month_cp_getAllPoints($amt=0,$filter_users=array(),$month,$year=0){
  13.     global $wpdb;
  14.     $month= intval($month);
  15.     $year= intval($year);
  16.         $month = $month <= 0 ? date('n')+$month : $month;
  17.         $year = $year <= 0 ? date('Y')+$year : $year;
  18.     if($amt>0){ $limit = ' LIMIT 0,'.$amt; }
  19.         $extraquery = '';
  20.     if (count($filter_users)>0){
  21.         $extraquery = ' AND '.$wpdb->base_prefix.'users.user_login != \'';
  22.         $extraquery .= implode("' AND ".$wpdb->base_prefix."users.user_login != '",$filter_users);
  23.         $extraquery .= '\' ';
  24.     }
  25.     $mktime_b = mktime( 0, 0, 0, $month, 1, $year );
  26.     $mktime_a = mktime( 0, 0, 0, $month, date('t'), $year );
  27.     $extraquery .= ' AND timestamp >= "' . $mktime_b . '"';
  28.     $extraquery .= ' AND timestamp <= "' . $mktime_a . '"';
  29.         $a = array();
  30.     $array = $wpdb->get_results('SELECT SUM('.$wpdb->base_prefix.'cp.points) AS meta_value, '.$wpdb->base_prefix.'users.ID, '.$wpdb->base_prefix.'users.user_login, '.$wpdb->base_prefix.'users.display_name
  31.         FROM `'.$wpdb->base_prefix.'users`, `'.$wpdb->base_prefix.'cp`
  32.         WHERE 1=1
  33.                AND '.$wpdb->base_prefix.'users.ID = '.$wpdb->base_prefix.'cp.uid
  34.         '.$extraquery.'
  35.         GROUP BY '.$wpdb->base_prefix.'cp.uid
  36.                ORDER BY SUM('.$wpdb->base_prefix.'cp.points)+0 DESC'
  37.         . $limit . ';'
  38.         ,ARRAY_A);
  39.         foreach($array as $x=>$y){
  40.                   if( $y['ID'] )
  41.             $a[$x] = array( "id"=>$y['ID'], "user"=>$y['user_login'], "display_name"=>$y['display_name'], "points"=>($y['meta_value']==0)?0:$y['meta_value'], "points_formatted"=>cp_formatPoints($y['meta_value']) );
  42.         }
  43.     return $a;
  44. }
  45.  
  46. function month_cp_widgets(){
  47.  
  48.     /** CubePoints Top Users Widget */
  49.     class month_cp_topUsersWidget extends WP_Widget {
  50.      
  51.         // constructor
  52.         function month_cp_topUsersWidget() {
  53.             parent::WP_Widget('month_cp_topUsersWidget', 'CubePoints Top Users Month', array('description' => 'Use this widget to showcase the users with the most points of the month.'));
  54.         }
  55.      
  56.         // widget main
  57.         function widget($args, $instance) {
  58.             extract($args, EXTR_SKIP);
  59.             echo $before_widget;
  60.             $title = empty($instance['title']) ? '&nbsp;' : apply_filters('widget_title', $instance['title']);
  61.             if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
  62.             $top = month_cp_getAllPoints($instance['num'],get_option('cp_topfilter'),$instance['months']);
  63.             do_action('month_cp_topUsersWidget_before');
  64.             echo apply_filters('month_cp_topUsersWidget_before','<ul>');
  65.             $line = apply_filters('month_cp_topUsersWidget_line','<li class="cp_topUsersWidget top_%place%" style="%style%">%string%</li>');
  66.             $line = str_replace('%style%', $instance['style'], $line);
  67.             foreach($top as $x=>$y){
  68.                 $user = get_userdata($y['id']);
  69.                 $string = str_replace('%string%', '', $instance['text']);
  70.                 $string = str_replace('%string%',$string,$line);
  71.                 $string = apply_filters('cp_displayUserInfo',$string,$y,$x+1);
  72.                 echo $string;
  73.             }
  74.             echo apply_filters('month_cp_topUsersWidget_after','</ul>');
  75.             do_action('month_cp_topUsersWidget_after');
  76.             echo $after_widget;
  77.         }
  78.      
  79.         // widget settings update
  80.         function update($new_instance, $old_instance) {
  81.             $instance = $old_instance;
  82.             $instance['title'] = strip_tags($new_instance['title']);
  83.             $instance['num'] = ((int) $new_instance['num'] > 0 ) ? (int) $new_instance['num'] : 1 ;
  84.             $instance['text'] = trim($new_instance['text']);
  85.             $instance['months'] = intval($new_instance['months']);
  86.             $instance['style'] = trim($new_instance['style']);
  87.             return $instance;
  88.         }
  89.      
  90.         // widget settings form
  91.         function form($instance) {
  92.             $default =  array( 'title' => __('Month Top Users', 'cp') , 'num' => 3 , 'months' => 0, 'text' => '%user% (%points%)', 'style' => 'list-style:none;' );
  93.             $instance = wp_parse_args( (array) $instance, $default );
  94.      
  95.             $field = 'title';
  96.             $field_id = $this->get_field_id($field);
  97.             $field_name = $this->get_field_name($field);
  98.             echo "\r\n".'<p><label for="'.$field_id.'">'.__('Title', 'cp').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.esc_attr( $instance[$field] ).'" /><label></p>';
  99.            
  100.             $field = 'num';
  101.             $field_id = $this->get_field_id($field);
  102.             $field_name = $this->get_field_name($field);
  103.             echo "\r\n".'<p><label for="'.$field_id.'">'.__('Number of top users to show', 'cp').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.esc_attr( $instance[$field] ).'" /><label></p>';
  104.            
  105.             $field = 'months';
  106.             $field_id = $this->get_field_id($field);
  107.             $field_name = $this->get_field_name($field);
  108.             echo "\r\n".'<p><label for="'.$field_id.'">'.__('How much months back ?', 'cp').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.esc_attr( $instance[$field] ).'" /><label></p>';
  109.            
  110.             $field = 'text';
  111.             $field_id = $this->get_field_id($field);
  112.             $field_name = $this->get_field_name($field);
  113.             echo "\r\n".'<p><label for="'.$field_id.'">'.__('Text', 'cp').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.esc_attr( $instance[$field] ).'" /><label></p>';
  114.  
  115.             echo "\r\n".'<small><strong>'.__('Shortcodes', 'cp') . ':</strong><br />';
  116.             echo __('Number of points', 'cp') . ' - %points%' . '<br />';
  117.             echo __('Points (number only)', 'cp') . ' - %npoints%' . '<br />';
  118.             echo __('User display name', 'cp') . ' - %user%' . '<br />';
  119.             echo __('User login ID', 'cp') . ' - %username%' . '<br />';
  120.             echo __('User ID', 'cp') . ' - %userid%' . '<br />';
  121.             echo __('User ranking', 'cp') . ' - %place%' . '<br />';
  122.             echo __('Email MD5 hash', 'cp') . ' - %emailhash%' . '<br />';
  123.             echo '<br /></small>';
  124.            
  125.             $field = 'style';
  126.             $field_id = $this->get_field_id($field);
  127.             $field_name = $this->get_field_name($field);
  128.             echo "\r\n".'<p><label for="'.$field_id.'">'.__('Style', 'cp').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.esc_attr( $instance[$field] ).'" /><label></p>';
  129.             echo "\r\n".'<small><strong>'.__('Note', 'cp') . ':</strong> '.__('This adds the following style to the list element. Shortcodes from above may be used here. The %emailhash% shortcode, for example, could be used to display gravatars.', 'cp').'</small><br />';
  130.         }
  131.     }
  132.     // register widget
  133.     register_widget("month_cp_topUsersWidget");
  134.  
  135. }
  136. function cubeshortcode( $atts, $content )
  137. {
  138.     extract(shortcode_atts(array(
  139.         "month" => 0,
  140.         "year" => 0,
  141.                 "users" => 3,
  142.                 "style" => 'list-style:none;',
  143.                 "title" => '<strong>%date%</strong><br />'
  144.     ), $atts));
  145.                     $month = $month <= 0 ? date('n')+$month : $month;
  146.                     $year = $year <= 0 ? date('Y')+$year : $year;
  147.             $adate = date('F Y', mktime( 0, 0, 0, $month, 1, $year ) );
  148.             $title = str_replace( array( '%date%' ), array( $adate ), $title);
  149.                     $res = $title;
  150.             $top = month_cp_getAllPoints($users,get_option('cp_topfilter'),$month,$year);
  151.             $res.= '<ul>';
  152.             $line = apply_filters('month_cp_topUsersWidget_line','<li class="cp_topUsersWidget top_%place%" style="%style%">%string%</li>');
  153.             $line = str_replace( array( '%style%' ), array( $style ), $line);
  154.             foreach($top as $x=>$y){
  155.                 $user = get_userdata($y['id']);
  156.                 $string = str_replace('%string%', '', $content);
  157.                 $string = str_replace('%string%',$string,$line);
  158.                 $string = apply_filters('cp_displayUserInfo',$string,$y,$x+1);
  159.                 $res.= $string;
  160.             }
  161.             $res.= '</ul>';
  162.   if( count( $top ) > 0 ){
  163.     return $res;
  164.   }else{
  165.     return '';
  166.   }
  167. }
  168. add_shortcode( 'cp', 'cubeshortcode' );
  169.  
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement