Advertisement
Guest User

Cubepoints Top member of the month

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