[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/classes/ -> class-bp-core-whos-online-widget.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Members Who's Online Widget.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersWidgets
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Who's Online Widget.
  15   *
  16   * @since 1.0.3
  17   */
  18  class BP_Core_Whos_Online_Widget extends WP_Widget {
  19  
  20      /**
  21       * Constructor method.
  22       *
  23       * @since 1.5.0
  24       */
  25  	public function __construct() {
  26          $name        = _x( "(BuddyPress) Who's Online", 'widget name', 'buddypress' );
  27          $description = __( 'Profile photos of online users', 'buddypress' );
  28          parent::__construct( false, $name, array(
  29              'description'                 => $description,
  30              'classname'                   => 'widget_bp_core_whos_online_widget buddypress widget',
  31              'customize_selective_refresh' => true,
  32          ) );
  33      }
  34  
  35      /**
  36       * Display the Who's Online widget.
  37       *
  38       * @since 1.0.3
  39       *
  40       * @see WP_Widget::widget() for description of parameters.
  41       *
  42       * @param array $args     Widget arguments.
  43       * @param array $instance Widget settings, as saved by the user.
  44       */
  45  	public function widget( $args, $instance ) {
  46          global $members_template;
  47  
  48          // Get widget settings.
  49          $settings = $this->parse_settings( $instance );
  50  
  51          /**
  52           * Filters the title of the Who's Online widget.
  53           *
  54           * @since 1.8.0
  55           * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
  56           *
  57           * @param string $title    The widget title.
  58           * @param array  $settings The settings for the particular instance of the widget.
  59           * @param string $id_base  Root ID for all widgets of this type.
  60           */
  61          $title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base );
  62  
  63          echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
  64  
  65          $max_limit   = bp_get_widget_max_count_limit( __CLASS__ );
  66          $max_members = $settings['max_members'] > $max_limit ? $max_limit : (int) $settings['max_members'];
  67  
  68          // Setup args for querying members.
  69          $members_args = array(
  70              'user_id'         => 0,
  71              'type'            => 'online',
  72              'per_page'        => $max_members,
  73              'max'             => $max_members,
  74              'populate_extras' => true,
  75              'search_terms'    => false,
  76          );
  77  
  78          // Back up global.
  79          $old_members_template = $members_template;
  80  
  81          ?>
  82  
  83          <?php if ( bp_has_members( $members_args ) ) : ?>
  84  
  85              <div class="avatar-block">
  86  
  87                  <?php while ( bp_members() ) : bp_the_member(); ?>
  88  
  89                      <div class="item-avatar">
  90                          <a href="<?php bp_member_permalink(); ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
  91                      </div>
  92  
  93                  <?php endwhile; ?>
  94  
  95              </div>
  96  
  97          <?php else: ?>
  98  
  99              <div class="widget-error">
 100                  <?php esc_html_e( 'There are no users currently online', 'buddypress' ); ?>
 101              </div>
 102  
 103          <?php endif; ?>
 104  
 105          <?php echo $args['after_widget'];
 106  
 107          // Restore the global.
 108          $members_template = $old_members_template;
 109      }
 110  
 111      /**
 112       * Update the Who's Online widget options.
 113       *
 114       * @since 1.0.3
 115       *
 116       * @param array $new_instance The new instance options.
 117       * @param array $old_instance The old instance options.
 118       * @return array $instance The parsed options to be saved.
 119       */
 120  	public function update( $new_instance, $old_instance ) {
 121          $instance = $old_instance;
 122  
 123          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 124  
 125          $instance['title']       = strip_tags( $new_instance['title'] );
 126          $instance['max_members'] = $new_instance['max_members'] > $max_limit ? $max_limit : intval( $new_instance['max_members'] );
 127  
 128          return $instance;
 129      }
 130  
 131      /**
 132       * Output the Who's Online widget options form.
 133       *
 134       * @since 1.0.3
 135       *
 136       * @param array $instance Widget instance settings.
 137       * @return void
 138       */
 139  	public function form( $instance ) {
 140          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 141  
 142          // Get widget settings.
 143          $settings    = $this->parse_settings( $instance );
 144          $title       = strip_tags( $settings['title'] );
 145          $max_members = $settings['max_members'] > $max_limit ? $max_limit : intval( $settings['max_members'] );
 146          ?>
 147  
 148          <p>
 149              <label for="<?php echo $this->get_field_id( 'title' ); ?>">
 150                  <?php esc_html_e( 'Title:', 'buddypress' ); ?>
 151                  <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" style="width: 100%" />
 152              </label>
 153          </p>
 154  
 155          <p>
 156              <label for="<?php echo $this->get_field_id( 'max_members' ); ?>">
 157                  <?php esc_html_e( 'Max members to show:', 'buddypress' ); ?>
 158                  <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="number" min="1" max="<?php echo esc_attr( $max_limit ); ?>" value="<?php echo esc_attr( $max_members ); ?>" style="width: 30%" />
 159              </label>
 160          </p>
 161  
 162      <?php
 163      }
 164  
 165      /**
 166       * Merge the widget settings into defaults array.
 167       *
 168       * @since 2.3.0
 169       *
 170       *
 171       * @param array $instance Widget instance settings.
 172       * @return array
 173       */
 174  	public function parse_settings( $instance = array() ) {
 175          return bp_parse_args( $instance, array(
 176              'title'          => __( "Who's Online", 'buddypress' ),
 177              'max_members'      => 15,
 178          ), 'members_widget_settings' );
 179      }
 180  }


Generated: Tue Sep 17 01:01:39 2019 Cross-referenced by PHPXref 0.7.1