[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Members Widget.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersWidgets
   7   * @since 1.0.3
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Members Widget.
  15   *
  16   * @since 1.0.3
  17   */
  18  class BP_Core_Members_Widget extends WP_Widget {
  19  
  20      /**
  21       * Constructor method.
  22       *
  23       * @since 1.5.0
  24       */
  25  	public function __construct() {
  26  
  27          // Setup widget name & description.
  28          $name        = _x( '(BuddyPress) Members', 'widget name', 'buddypress' );
  29          $description = __( 'A dynamic list of recently active, popular, and newest members', 'buddypress' );
  30  
  31          // Call WP_Widget constructor.
  32          parent::__construct( false, $name, array(
  33              'description'                 => $description,
  34              'classname'                   => 'widget_bp_core_members_widget buddypress widget',
  35              'customize_selective_refresh' => true,
  36          ) );
  37  
  38          if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) {
  39              add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  40          }
  41      }
  42  
  43      /**
  44       * Enqueue scripts.
  45       *
  46       * @since 2.6.0
  47       */
  48  	public function enqueue_scripts() {
  49          wp_enqueue_script( 'bp-widget-members' );
  50      }
  51  
  52      /**
  53       * Display the Members widget.
  54       *
  55       * @since 1.0.3
  56       *
  57       * @see WP_Widget::widget() for description of parameters.
  58       *
  59       * @param array $args     Widget arguments.
  60       * @param array $instance Widget settings, as saved by the user.
  61       */
  62  	public function widget( $args, $instance ) {
  63          global $members_template;
  64  
  65          // Get widget settings.
  66          $settings = $this->parse_settings( $instance );
  67  
  68          /**
  69           * Filters the title of the Members widget.
  70           *
  71           * @since 1.8.0
  72           * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
  73           *
  74           * @param string $title    The widget title.
  75           * @param array  $settings The settings for the particular instance of the widget.
  76           * @param string $id_base  Root ID for all widgets of this type.
  77           */
  78          $title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base );
  79          $title = $settings['link_title'] ? '<a href="' . bp_get_members_directory_permalink() . '">' . $title . '</a>' : $title;
  80  
  81          /**
  82           * Filters the separator of the member widget links.
  83           *
  84           * @since 2.4.0
  85           *
  86           * @param string $separator Separator string. Default '|'.
  87           */
  88          $separator = apply_filters( 'bp_members_widget_separator', '|' );
  89  
  90          // Output before widget HTMl, title (and maybe content before & after it).
  91          echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
  92  
  93          $max_limit   = bp_get_widget_max_count_limit( __CLASS__ );
  94          $max_members = $settings['max_members'] > $max_limit ? $max_limit : (int) $settings['max_members'];
  95  
  96          // Setup args for querying members.
  97          $members_args = array(
  98              'user_id'         => 0,
  99              'type'            => $settings['member_default'],
 100              'per_page'        => $max_members,
 101              'max'             => $max_members,
 102              'populate_extras' => true,
 103              'search_terms'    => false,
 104          );
 105  
 106          // Back up the global.
 107          $old_members_template = $members_template;
 108  
 109          ?>
 110  
 111          <?php if ( bp_has_members( $members_args ) ) : ?>
 112  
 113              <div class="item-options" id="members-list-options">
 114                  <a href="<?php bp_members_directory_permalink(); ?>" id="newest-members" <?php if ( 'newest' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Newest', 'buddypress' ); ?></a>
 115                  <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span>
 116                  <a href="<?php bp_members_directory_permalink(); ?>" id="recently-active-members" <?php if ( 'active' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Active', 'buddypress' ); ?></a>
 117  
 118                  <?php if ( bp_is_active( 'friends' ) ) : ?>
 119                      <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span>
 120                      <a href="<?php bp_members_directory_permalink(); ?>" id="popular-members" <?php if ( 'popular' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Popular', 'buddypress' ); ?></a>
 121  
 122                  <?php endif; ?>
 123  
 124              </div>
 125  
 126              <ul id="members-list" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
 127  
 128                  <?php while ( bp_members() ) : bp_the_member(); ?>
 129  
 130                      <li class="vcard">
 131                          <div class="item-avatar">
 132                              <a href="<?php bp_member_permalink() ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
 133                          </div>
 134  
 135                          <div class="item">
 136                              <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a></div>
 137                              <div class="item-meta">
 138                                  <?php if ( 'newest' == $settings['member_default'] ) : ?>
 139                                      <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_registered( array( 'relative' => false ) ) ); ?>"><?php bp_member_registered(); ?></span>
 140                                  <?php elseif ( 'active' == $settings['member_default'] ) : ?>
 141                                      <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_last_active( array( 'relative' => false ) ) ); ?>"><?php bp_member_last_active(); ?></span>
 142                                  <?php else : ?>
 143                                      <span class="activity"><?php bp_member_total_friend_count(); ?></span>
 144                                  <?php endif; ?>
 145                              </div>
 146                          </div>
 147                      </li>
 148  
 149                  <?php endwhile; ?>
 150  
 151              </ul>
 152  
 153              <?php wp_nonce_field( 'bp_core_widget_members', '_wpnonce-members', false ); ?>
 154  
 155              <input type="hidden" name="members_widget_max" id="members_widget_max" value="<?php echo esc_attr( $settings['max_members'] ); ?>" />
 156  
 157          <?php else: ?>
 158  
 159              <div class="widget-error">
 160                  <?php esc_html_e( 'No one has signed up yet!', 'buddypress' ); ?>
 161              </div>
 162  
 163          <?php endif; ?>
 164  
 165          <?php echo $args['after_widget'];
 166  
 167          // Restore the global.
 168          $members_template = $old_members_template;
 169      }
 170  
 171      /**
 172       * Update the Members widget options.
 173       *
 174       * @since 1.0.3
 175       *
 176       * @param array $new_instance The new instance options.
 177       * @param array $old_instance The old instance options.
 178       * @return array $instance The parsed options to be saved.
 179       */
 180  	public function update( $new_instance, $old_instance ) {
 181          $instance = $old_instance;
 182  
 183          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 184  
 185          $instance['title']          = strip_tags( $new_instance['title'] );
 186          $instance['max_members']    = $new_instance['max_members'] > $max_limit ? $max_limit : intval( $new_instance['max_members'] );
 187          $instance['member_default'] = strip_tags( $new_instance['member_default'] );
 188          $instance['link_title']        = ! empty( $new_instance['link_title'] );
 189  
 190          return $instance;
 191      }
 192  
 193      /**
 194       * Output the Members widget options form.
 195       *
 196       * @since 1.0.3
 197       *
 198       * @param array $instance Widget instance settings.
 199       * @return void
 200       */
 201  	public function form( $instance ) {
 202          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 203  
 204          // Get widget settings.
 205          $settings       = $this->parse_settings( $instance );
 206          $title          = strip_tags( $settings['title'] );
 207          $max_members    = $settings['max_members'] > $max_limit ? $max_limit : intval( $settings['max_members'] );
 208          $member_default = strip_tags( $settings['member_default'] );
 209          $link_title     = (bool) $settings['link_title']; ?>
 210  
 211          <p>
 212              <label for="<?php echo $this->get_field_id( 'title' ); ?>">
 213                  <?php esc_html_e( 'Title:', 'buddypress' ); ?>
 214                  <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%" />
 215              </label>
 216          </p>
 217  
 218          <p>
 219              <label for="<?php echo $this->get_field_id( 'link_title' ) ?>">
 220                  <input type="checkbox" name="<?php echo $this->get_field_name( 'link_title' ) ?>" id="<?php echo $this->get_field_id( 'link_title' ) ?>" value="1" <?php checked( $link_title ) ?> />
 221                  <?php esc_html_e( 'Link widget title to Members directory', 'buddypress' ); ?>
 222              </label>
 223          </p>
 224  
 225          <p>
 226              <label for="<?php echo $this->get_field_id( 'max_members' ); ?>">
 227                  <?php esc_html_e( 'Max members to show:', 'buddypress' ); ?>
 228                  <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%" />
 229              </label>
 230          </p>
 231  
 232          <p>
 233              <label for="<?php echo $this->get_field_id( 'member_default' ) ?>"><?php esc_html_e( 'Default members to show:', 'buddypress' ); ?></label>
 234              <select name="<?php echo $this->get_field_name( 'member_default' ) ?>" id="<?php echo $this->get_field_id( 'member_default' ) ?>">
 235                  <option value="newest"  <?php if ( 'newest'  === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Newest',  'buddypress' ); ?></option>
 236                  <option value="active"  <?php if ( 'active'  === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Active',  'buddypress' ); ?></option>
 237                  <option value="popular" <?php if ( 'popular' === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Popular', 'buddypress' ); ?></option>
 238              </select>
 239          </p>
 240  
 241      <?php
 242      }
 243  
 244      /**
 245       * Merge the widget settings into defaults array.
 246       *
 247       * @since 2.3.0
 248       *
 249       *
 250       * @param array $instance Widget instance settings.
 251       * @return array
 252       */
 253  	public function parse_settings( $instance = array() ) {
 254          return bp_parse_args( $instance, array(
 255              'title'          => __( 'Members', 'buddypress' ),
 256              'max_members'      => 5,
 257              'member_default' => 'active',
 258              'link_title'      => false
 259          ), 'members_widget_settings' );
 260      }
 261  }


Generated: Thu Nov 14 01:01:40 2019 Cross-referenced by PHPXref 0.7.1