| 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"> ' . 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 |
wp_send_json_success( ['html' => $this->get_widget( $instance )] ); |
| 60 |
} |
| 61 |
|
| 62 |
public function widget( $args, $instance ) { |
| 63 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 64 |
$data = [ |
| 65 |
'boardid' => 0, |
| 66 |
'action' => 'wpforo_load_ajax_widget_OnlineMembers', |
| 67 |
'instance' => $instance, |
| 68 |
]; |
| 69 |
if( WPF()->board->get_current( 'boardid' ) !== 0 ) $data['referer'] = home_url(); |
| 70 |
$html = $this->get_widget( $data['instance'] ); |
| 71 |
$json = wp_json_encode( $data ); |
| 72 |
wp_enqueue_script( 'wpforo-widgets-js' ); |
| 73 |
echo $args['before_widget'] . '<div id="wpf-widget-online-users" class="wpforo-widget-wrap">'; |
| 74 |
if( $instance['title'] ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 75 |
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']; |
| 76 |
} |
| 77 |
|
| 78 |
public function form( $instance ) { |
| 79 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 80 |
$title = (string) $instance['title']; |
| 81 |
$count = (int) $instance['count']; |
| 82 |
$display_avatar = (bool) $instance['display_avatar']; |
| 83 |
$groupids = array_filter( wpforo_parse_args( ( wpforo_is_json( $instance['groupids'] ) ? json_decode( $instance['groupids'], true ) : $instance['groupids'] ) ) ); |
| 84 |
$refresh_interval = (int) $instance['refresh_interval']; |
| 85 |
?> |
| 86 |
<div class="wpf-wdg-wrapper"> |
| 87 |
<p> |
| 88 |
<label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title', 'wpforo' ); ?>:</label> |
| 89 |
<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 ); ?>"> |
| 90 |
</p> |
| 91 |
<p> |
| 92 |
<label for="<?php echo $this->get_field_id( 'groupids' ) ?>"><?php _e( 'User Groups', 'wpforo' ); ?></label> |
| 93 |
<select id="<?php echo $this->get_field_id( 'groupids' ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'groupids' ) ); ?>[]" multiple required> |
| 94 |
<?php WPF()->usergroup->show_selectbox( $groupids, 4 ) ?> |
| 95 |
</select> |
| 96 |
</p> |
| 97 |
<p> |
| 98 |
<label for="<?php echo $this->get_field_id( 'count' ) ?>"><?php _e( 'Number of Items', 'wpforo' ); ?></label> |
| 99 |
<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 ); ?>"> |
| 100 |
</p> |
| 101 |
<p> |
| 102 |
<span><?php _e( 'Display with avatars', 'wpforo' ); ?> : </span> |
| 103 |
|
| 104 |
<label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_1"><?php _e( 'Yes', 'wpforo' ); ?></label> |
| 105 |
<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' ) ); ?>"> |
| 106 |
|
| 107 |
<label for="<?php echo $this->get_field_id( 'display_avatar' ) ?>_0"><?php _e( 'No', 'wpforo' ); ?></label> |
| 108 |
<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' ) ); ?>"> |
| 109 |
</p> |
| 110 |
<p> |
| 111 |
<label for="<?php echo $this->get_field_id( 'refresh_interval' ) ?>"><?php _e( 'Auto Refresh Interval Seconds', 'wpforo' ); ?></label> |
| 112 |
<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 ); ?>"> |
| 113 |
<span style="color: #ccc"><?php _e( 'Set 0 to disable autorefresh', 'wpforo' ) ?></span> |
| 114 |
</p> |
| 115 |
</div> |
| 116 |
<?php |
| 117 |
} |
| 118 |
|
| 119 |
public function update( $new_instance, $old_instance ) { |
| 120 |
$new_instance = wpforo_parse_args( $new_instance, $this->default_instance ); |
| 121 |
$instance = []; |
| 122 |
$instance['title'] = strip_tags( (string) $new_instance['title'] ); |
| 123 |
$instance['count'] = (int) $new_instance['count']; |
| 124 |
$instance['display_avatar'] = (bool) (int) $new_instance['display_avatar']; |
| 125 |
$instance['groupids'] = array_filter( wpforo_parse_args( $new_instance['groupids'] ) ); |
| 126 |
$instance['refresh_interval'] = (int) $new_instance['refresh_interval']; |
| 127 |
|
| 128 |
return $instance; |
| 129 |
} |
| 130 |
} |
| 131 |
|