PluginProbe
wpForo Forum / 3.1.5
wpForo Forum v3.1.5
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / widgets / OnlineMembers.php

OnlineMembers.php in wpForo Forum 3.1.5, at widgets/OnlineMembers.php

149 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\widgets;
4
5 use WP_Widget;
6
7 class OnlineMembers extends WP_Widget {
8 /**
9 * @var array
10 */
11 private $default_instance;
12
13 function __construct() {
14 parent::__construct( 'wpforo_online_members', 'wpForo Online Members', [ 'description' => 'Online members.' ] );
15 $this->init_local_vars();
16 add_action( 'wp_ajax_wpforo_load_ajax_widget_OnlineMembers', [ $this, 'load_ajax_widget' ] );
17 add_action( 'wp_ajax_nopriv_wpforo_load_ajax_widget_OnlineMembers', [ $this, 'load_ajax_widget' ] );
18 }
19
20 private function init_local_vars() {
21 $this->default_instance = [
22 'title' => __( 'Online Members', 'wpforo' ),
23 'count' => 15,
24 'display_avatar' => true,
25 'groupids' => WPF()->usergroup->get_visible_usergroup_ids(),
26 'refresh_interval' => 0,
27 ];
28 }
29
30 public function get_widget( $instance ) {
31 $online_members = WPF()->member->get_online_members( $instance['count'], $instance['groupids'] );
32 ob_start();
33 if( ! empty( $online_members ) ) {
34 echo '<ul>
35 <li>
36 <div class="wpforo-list-item">';
37 foreach( $online_members as $member ) {
38 if( $instance['display_avatar'] ): ?>
39 <?php wpforo_member_link( $member, '', 96, 'onlineavatar', true, 'avatar', 'style="width:95%;" class="avatar"' ) ?>
40 <?php else: ?>
41 <?php wpforo_member_link( $member, '', 30, 'onlineuser' ) ?>
42 <?php endif; ?>
43 <?php
44 }
45 echo '<div class="wpf-clear"></div>
46 </div>
47 </li>
48 </ul>';
49 } else {
50 echo '<p class="wpf-widget-note">&nbsp;' . wpforo_phrase( 'No online members at the moment', false ) . '</p>';
51 }
52
53 return ob_get_clean();
54 }
55
56 public function load_ajax_widget() {
57 $_POST = wp_unslash( $_POST );
58 $instance = json_decode( (string) wpfval( $_POST, 'instance' ), true );
59
60 // SECURITY: Sanitize instance parameters
61 if( is_array( $instance ) ) {
62 // Intersect groupids with visible usergroups to prevent hidden group enumeration
63 if( isset( $instance['groupids'] ) ) {
64 $visible_groupids = WPF()->usergroup->get_visible_usergroup_ids();
65 $instance['groupids'] = array_values( array_intersect(
66 array_map( 'intval', (array) $instance['groupids'] ),
67 $visible_groupids
68 ) );
69 }
70
71 // Cap count to prevent resource exhaustion
72 if( isset( $instance['count'] ) ) {
73 $instance['count'] = min( 50, max( 1, intval( $instance['count'] ) ) );
74 }
75 }
76
77 wp_send_json_success( ['html' => $this->get_widget( $instance )] );
78 }
79
80 public function widget( $args, $instance ) {
81 $instance = wpforo_parse_args( $instance, $this->default_instance );
82 $data = [
83 'boardid' => 0,
84 'action' => 'wpforo_load_ajax_widget_OnlineMembers',
85 'instance' => $instance,
86 ];
87 if( WPF()->board->get_current( 'boardid' ) !== 0 ) $data['referer'] = home_url();
88 $html = $this->get_widget( $data['instance'] );
89 $json = wp_json_encode( $data );
90 wp_enqueue_script( 'wpforo-widgets-js' );
91 echo $args['before_widget'] . '<div id="wpf-widget-online-users" class="wpforo-widget-wrap">';
92 if( $instance['title'] ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
93 echo '<div class="wpforo-widget-content wpforo-ajax-widget wpforo-ajax-widget-onload-false" data-json="' . esc_attr( $json ) . '">' . $html . '</div></div>' . $args['after_widget'];
94 }
95
96 public function form( $instance ) {
97 $instance = wpforo_parse_args( $instance, $this->default_instance );
98 $title = (string) $instance['title'];
99 $count = (int) $instance['count'];
100 $display_avatar = (bool) $instance['display_avatar'];
101 $groupids = array_filter( wpforo_parse_args( ( wpforo_is_json( $instance['groupids'] ) ? json_decode( $instance['groupids'], true ) : $instance['groupids'] ) ) );
102 $refresh_interval = (int) $instance['refresh_interval'];
103 ?>
104 <div class="wpf-wdg-wrapper">
105 <p>
106 <label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title', 'wpforo' ); ?>:</label>
107 <input id="<?php echo $this->get_field_id( 'title' ) ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
108 </p>
109 <p>
110 <label for="<?php echo $this->get_field_id( 'groupids' ) ?>"><?php _e( 'User Groups', 'wpforo' ); ?></label>&nbsp;
111 <select id="<?php echo $this->get_field_id( 'groupids' ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'groupids' ) ); ?>[]" multiple required>
112 <?php WPF()->usergroup->show_selectbox( $groupids, 4 ) ?>
113 </select>
114 </p>
115 <p>
116 <label for="<?php echo $this->get_field_id( 'count' ) ?>"><?php _e( 'Number of Items', 'wpforo' ); ?></label>&nbsp;
117 <input id="<?php echo $this->get_field_id( 'count' ) ?>" type="number" min="1" style="width: 53px;" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" value="<?php echo esc_attr( $count ); ?>">
118 </p>
119 <p>
120 <span><?php _e( 'Display with avatars', 'wpforo' ); ?> : </span>
121
122 <label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label>
123 <input id="<?php echo $this->get_field_id( 'display_avatar' ) ?>_1" value="1" <?php checked( $display_avatar ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'display_avatar' ) ); ?>">
124
125 <label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label>
126 <input id="<?php echo $this->get_field_id( 'display_avatar' ) ?>_0" value="0" <?php checked( $display_avatar, false ); ?> type="radio" name="<?php echo esc_attr( $this->get_field_name( 'display_avatar' ) ); ?>">
127 </p>
128 <p>
129 <label for="<?php echo $this->get_field_id( 'refresh_interval' ) ?>"><?php _e( 'Auto Refresh Interval Seconds', 'wpforo' ); ?></label>&nbsp;
130 <input id="<?php echo $this->get_field_id( 'refresh_interval' ) ?>" type="number" min="0" style="display: inline-block; width: 53px;" name="<?php echo esc_attr( $this->get_field_name( 'refresh_interval' ) ); ?>" value="<?php echo esc_attr( $refresh_interval ); ?>">
131 <span style="color: #ccc"><?php _e( 'Set 0 to disable autorefresh', 'wpforo' ) ?></span>
132 </p>
133 </div>
134 <?php
135 }
136
137 public function update( $new_instance, $old_instance ) {
138 $new_instance = wpforo_parse_args( $new_instance, $this->default_instance );
139 $instance = [];
140 $instance['title'] = strip_tags( (string) $new_instance['title'] );
141 $instance['count'] = (int) $new_instance['count'];
142 $instance['display_avatar'] = (bool) (int) $new_instance['display_avatar'];
143 $instance['groupids'] = array_filter( wpforo_parse_args( $new_instance['groupids'] ) );
144 $instance['refresh_interval'] = (int) $new_instance['refresh_interval'];
145
146 return $instance;
147 }
148 }
149