Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: CubePoints - More Widgets
- Plugin URI: http://www.boiteaweb.fr
- Description: Add 1 new widget for CubePoints plugin
- Version: 1.0
- Author: Juliobox
- Author URI: http://www.boiteaweb.fr
- */
- if( defined( 'CP_VER' ) ) {
- add_action('widgets_init', 'month_cp_widgets');
- function month_cp_getAllPoints($amt=0,$filter_users=array(),$month=0,$year=0){
- global $wpdb;
- $month= (int)$month;
- $year= (int)$year;
- $month = $month <= 0 ? date('n')+$month : $month;
- $year = $year <= 0 ? date('Y')+$year : $year;
- if($amt>0){ $limit = ' LIMIT 0,'.$amt; }
- $extraquery = '';
- if (count($filter_users)>0){
- $extraquery = ' AND '.$wpdb->base_prefix.'users.user_login != \'';
- $extraquery .= implode("' AND ".$wpdb->base_prefix."users.user_login != '",$filter_users);
- $extraquery .= '\' ';
- }
- $mktime_b = mktime( 0, 0, 0, $month, 1, $year );
- $mktime_a = mktime( 0, 0, 0, $month, date('t'), $year );
- $extraquery .= ' AND timestamp >= "' . $mktime_b . '"';
- $extraquery .= ' AND timestamp <= "' . $mktime_a . '"';
- $a = array();
- $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
- FROM `'.$wpdb->base_prefix.'users`, `'.$wpdb->base_prefix.'cp`
- WHERE 1=1
- AND zdemo_users.ID = zdemo_cp.uid
- '.$extraquery.'
- GROUP BY '.$wpdb->base_prefix.'cp.uid
- ORDER BY SUM('.$wpdb->base_prefix.'cp.points)+0 DESC'
- . $limit . ';'
- ,ARRAY_A);
- foreach($array as $x=>$y){
- if( $y['ID'] )
- $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']) );
- }
- return $a;
- }
- function month_cp_widgets(){
- /** CubePoints Top Users Widget */
- class month_cp_topUsersWidget extends WP_Widget {
- // constructor
- function month_cp_topUsersWidget() {
- 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.'));
- }
- // widget main
- function widget($args, $instance) {
- extract($args, EXTR_SKIP);
- echo $before_widget;
- $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
- if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
- $top = month_cp_getAllPoints($instance['num'],get_option('cp_topfilter'),$instance['months']);
- do_action('month_cp_topUsersWidget_before');
- echo apply_filters('month_cp_topUsersWidget_before','<ul>');
- $line = apply_filters('month_cp_topUsersWidget_line','<li class="cp_topUsersWidget top_%place%" style="%style%">%string%</li>');
- $line = str_replace('%style%', $instance['style'], $line);
- foreach($top as $x=>$y){
- $user = get_userdata($y['id']);
- $string = str_replace('%string%', '', $instance['text']);
- $string = str_replace('%string%',$string,$line);
- $string = apply_filters('cp_displayUserInfo',$string,$y,$x+1);
- echo $string;
- }
- echo apply_filters('month_cp_topUsersWidget_after','</ul>');
- do_action('month_cp_topUsersWidget_after');
- echo $after_widget;
- }
- // widget settings update
- function update($new_instance, $old_instance) {
- $instance = $old_instance;
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['num'] = ((int) $new_instance['num'] > 0 ) ? (int) $new_instance['num'] : 1 ;
- $instance['text'] = trim($new_instance['text']);
- $instance['months'] = absint($new_instance['months']);
- $instance['style'] = trim($new_instance['style']);
- return $instance;
- }
- // widget settings form
- function form($instance) {
- $default = array( 'title' => __('Month Top Users', 'cp') , 'num' => 3 , 'months' => 0, 'text' => '%user% (%points%)', 'style' => 'list-style:none;' );
- $instance = wp_parse_args( (array) $instance, $default );
- $field = 'title';
- $field_id = $this->get_field_id($field);
- $field_name = $this->get_field_name($field);
- 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>';
- $field = 'num';
- $field_id = $this->get_field_id($field);
- $field_name = $this->get_field_name($field);
- 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>';
- $field = 'months';
- $field_id = $this->get_field_id($field);
- $field_name = $this->get_field_name($field);
- 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>';
- $field = 'text';
- $field_id = $this->get_field_id($field);
- $field_name = $this->get_field_name($field);
- 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>';
- echo "\r\n".'<small><strong>'.__('Shortcodes', 'cp') . ':</strong><br />';
- echo __('Number of points', 'cp') . ' - %points%' . '<br />';
- echo __('Points (number only)', 'cp') . ' - %npoints%' . '<br />';
- echo __('User display name', 'cp') . ' - %user%' . '<br />';
- echo __('User login ID', 'cp') . ' - %username%' . '<br />';
- echo __('User ID', 'cp') . ' - %userid%' . '<br />';
- echo __('User ranking', 'cp') . ' - %place%' . '<br />';
- echo __('Email MD5 hash', 'cp') . ' - %emailhash%' . '<br />';
- echo '<br /></small>';
- $field = 'style';
- $field_id = $this->get_field_id($field);
- $field_name = $this->get_field_name($field);
- 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>';
- 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 />';
- }
- }
- // register widget
- register_widget("month_cp_topUsersWidget");
- }
- function cubeshortcode( $atts, $content )
- {
- extract(shortcode_atts(array(
- "month" => 0,
- "year" => 0,
- "users" => 3,
- "style" => 'list-style:none;',
- "title" => '<strong>%date%</strong><br />'
- ), $atts));
- $month = $month <= 0 ? date('n')+$month : $month;
- $year = $year <= 0 ? date('Y')+$year : $year;
- $adate = date('F Y', mktime( 0, 0, 0, $month, 1, $year ) );
- $title = str_replace( array( '%date%' ), array( $adate ), $title);
- $res = $title;
- $top = month_cp_getAllPoints($users,get_option('cp_topfilter'),$month,$year);
- $res.= '<ul>';
- $line = apply_filters('month_cp_topUsersWidget_line','<li class="cp_topUsersWidget top_%place%" style="%style%">%string%</li>');
- $line = str_replace( array( '%style%' ), array( $style ), $line);
- foreach($top as $x=>$y){
- $user = get_userdata($y['id']);
- $string = str_replace('%string%', '', $content);
- $string = str_replace('%string%',$string,$line);
- $string = apply_filters('cp_displayUserInfo',$string,$y,$x+1);
- $res.= $string;
- }
- $res.= '</ul>';
- return $res;
- }
- add_shortcode( 'cp', 'cubeshortcode' );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement