Advertisement
designbymerovingi

Last Months Leaderboard Widget

Dec 15th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. /**
  2.  * Last Months Leaderboard
  3.  * @version 1.0
  4.  */
  5. add_action( 'mycred_widgets_init', 'mycred_load_last_months_leaderboard_widget' );
  6. function mycred_load_last_months_leaderboard_widget() {
  7.  
  8.     if ( ! class_exists( 'myCRED_Widget_Last_Months_Leaderboard' ) ) {
  9.         class myCRED_Widget_Last_Months_Leaderboard extends WP_Widget {
  10.  
  11.             // Constructor
  12.             public function __construct() {
  13.  
  14.                 parent::__construct(
  15.                     'mycred_widget_last_months_leaderboard',
  16.                     sprintf( '(%s) Last Months Leaderboard', mycred_label() ),
  17.                     array(
  18.                         'classname'   => 'widget-mycred-last-months-leaderboard',
  19.                         'description' => 'Shows last months points leaderboard.'
  20.                     )
  21.                 );
  22.  
  23.             }
  24.  
  25.             // Widget Output (what users see)
  26.             public function widget( $args, $instance ) {
  27.  
  28.                 extract( $args, EXTR_SKIP );
  29.  
  30.                 // Load myCRED
  31.                 $mycred = mycred();
  32.  
  33.                 echo $before_widget;
  34.  
  35.                 // Title (if not empty)
  36.                 if ( ! empty( $instance['title'] ) )
  37.                     echo $before_title . $mycred->template_tags_general( $instance['title'] ) . $after_title;
  38.  
  39.                 echo do_shortcode( '[mycred_leaderboard number=' . $instance['number'] . ' timeframe="last-month"]' );
  40.  
  41.                 echo $after_widget;
  42.  
  43.             }
  44.  
  45.             // Widget Settings (when editing / setting up widget)
  46.             public function form( $instance ) {
  47.  
  48.                 $title         = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'This Months Leaderboard', 'mycred' );
  49.                 $number        = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
  50.  
  51. ?>
  52. <p class="myCRED-widget-field">
  53.     <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mycred' ); ?>:</label>
  54.     <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  55. </p>
  56. <p class="myCRED-widget-field">
  57.     <label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of users', 'mycred' ); ?>:</label>
  58.     <input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo $number; ?>" size="3" class="align-right" />
  59. </p>
  60. <?php
  61.  
  62.             }
  63.  
  64.             // Save Widget Settings
  65.             public function update( $new_instance, $old_instance ) {
  66.  
  67.                 $instance           = $old_instance;
  68.                 $instance['number'] = absint( $new_instance['number'] );
  69.                 $instance['title']  = sanitize_text_field( $new_instance['title'] );
  70.  
  71.                 return $instance;
  72.  
  73.             }
  74.  
  75.     }
  76.  
  77.     register_widget( 'myCRED_Widget_Last_Months_Leaderboard' );
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement