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


Generated: Sun Nov 24 01:00:53 2024 Cross-referenced by PHPXref 0.7.1