| 15 | |
| 16 | /*---------------------------------------------------------------------------------*/ |
| 17 | /* Online Users Widget */ |
| 18 | /*---------------------------------------------------------------------------------*/ |
| 19 | |
| 20 | |
| 21 | class BBP_Online_Users_Widget extends WP_Widget { |
| 22 | |
| 23 | |
| 24 | |
| 25 | function register_widget() { |
| 26 | register_widget('BBP_Online_Users_Widget'); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | function BBP_Online_Users_Widget() |
| 31 | { |
| 32 | $widget_ops = apply_filters( 'bbp_online_users_widget_options', array( |
| 33 | 'classname' => 'bbp_online_users_widget', |
| 34 | 'description' => __( 'The online users widget.', 'bbpress' ) |
| 35 | ) ); |
| 36 | |
| 37 | parent::WP_Widget( false, __( 'bbPress Online Users Widget', 'bbpress' ), $widget_ops ); |
| 38 | } |
| 39 | |
| 40 | /** @see WP_Widget::widget */ |
| 41 | function widget( $args, $instance ) { |
| 42 | global $current_user; |
| 43 | |
| 44 | extract( $args ); |
| 45 | $time = $instance['time']; |
| 46 | |
| 47 | $title = apply_filters('widget_title', empty($instance['title']) ? __('BBP Online Users') : $instance['title'], $instance, $this->id_base); |
| 48 | echo $before_widget; |
| 49 | |
| 50 | if ( $title ) { |
| 51 | echo $before_title . $title . $after_title; |
| 52 | } |
| 53 | |
| 54 | echo '<ul>'; |
| 55 | |
| 56 | if(is_user_logged_in()) { |
| 57 | update_user_meta( $current_user->ID, 'last_time', time() ); |
| 58 | } |
| 59 | |
| 60 | $wp_user_search = new WP_User_Query( array( 'meta_key' => 'last_time', 'meta_value' => time() - $time, 'meta_compare' => '>' ) ); |
| 61 | $users = $wp_user_search->get_results(); |
| 62 | |
| 63 | if(!empty($users)) { |
| 64 | foreach($users as $user) { |
| 65 | $avatar = get_avatar( $user->ID, 14 ); |
| 66 | echo '<span><a href="' . bbp_get_user_profile_url($user->ID) . '"> ' . $avatar . '</a>' . '<a href="' . bbp_get_user_profile_url($user->ID) . '"> ' . get_the_author_meta( 'user_login', $user->ID ). '</a></span>'; |
| 67 | } |
| 68 | } else { |
| 69 | echo '<p>No online users found!</p>'; |
| 70 | } |
| 71 | |
| 72 | echo '</ul>'; |
| 73 | |
| 74 | echo $after_widget; |
| 75 | } |
| 76 | |
| 77 | /** @see WP_Widget::update */ |
| 78 | function update( $new_instance, $old_instance ) { |
| 79 | $instance = $old_instance; |
| 80 | $instance['title'] = strip_tags($new_instance['title']); |
| 81 | $instance['time'] = strip_tags($new_instance['time']); |
| 82 | return $instance; |
| 83 | } |
| 84 | |
| 85 | /** @see WP_Widget::form */ |
| 86 | function form( $instance ) { |
| 87 | if ( $instance ) { |
| 88 | $title = esc_attr( $instance[ 'title' ] ); |
| 89 | $time = esc_attr( $instance[ 'time' ] ); |
| 90 | } |
| 91 | else { |
| 92 | $title = __( 'Online Users', 'bbpress' ); |
| 93 | $time = '180'; |
| 94 | } |
| 95 | ?> |
| 96 | <p> |
| 97 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 98 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> |
| 99 | <label for="<?php echo $this->get_field_id('time'); ?>"><?php _e('Time (in seconds):'); ?></label> |
| 100 | <input class="widefat" id="<?php echo $this->get_field_id('time'); ?>" name="<?php echo $this->get_field_name('time'); ?>" type="text" value="<?php echo $time; ?>" /> |
| 101 | </p> |
| 102 | <?php |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |