[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups Widget.
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupsWidgets
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Groups widget.
  15   *
  16   * @since 1.0.3
  17   */
  18  class BP_Groups_Widget extends WP_Widget {
  19  
  20      /**
  21       * Working as a group, we get things done better.
  22       *
  23       * @since 1.0.3
  24       */
  25  	public function __construct() {
  26          $widget_ops = array(
  27              'description'                 => __( 'A dynamic list of recently active, popular, newest, or alphabetical groups', 'buddypress' ),
  28              'classname'                   => 'widget_bp_groups_widget buddypress widget',
  29              'customize_selective_refresh' => true,
  30          );
  31          parent::__construct( false, _x( '(BuddyPress) Groups', 'widget name', 'buddypress' ), $widget_ops );
  32  
  33          if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) {
  34              add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  35          }
  36      }
  37  
  38      /**
  39       * Enqueue scripts.
  40       *
  41       * @since 2.6.0
  42       */
  43  	public function enqueue_scripts() {
  44          $min = bp_core_get_minified_asset_suffix();
  45          wp_enqueue_script( 'groups_widget_groups_list-js', buddypress()->plugin_url . "bp-groups/js/widget-groups{$min}.js", array( 'jquery' ), bp_get_version() );
  46      }
  47  
  48      /**
  49       * Extends our front-end output method.
  50       *
  51       * @since 1.0.3
  52       *
  53       * @param array $args     Array of arguments for the widget.
  54       * @param array $instance Widget instance data.
  55       */
  56  	public function widget( $args, $instance ) {
  57          global $groups_template;
  58  
  59          /**
  60           * Filters the user ID to use with the widget instance.
  61           *
  62           * @since 1.5.0
  63           *
  64           * @param string $value Empty user ID.
  65           */
  66          $user_id = apply_filters( 'bp_group_widget_user_id', '0' );
  67  
  68          extract( $args );
  69  
  70          if ( empty( $instance['group_default'] ) ) {
  71              $instance['group_default'] = 'popular';
  72          }
  73  
  74          if ( empty( $instance['title'] ) ) {
  75              $instance['title'] = __( 'Groups', 'buddypress' );
  76          }
  77  
  78          /**
  79           * Filters the title of the Groups widget.
  80           *
  81           * @since 1.8.0
  82           * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
  83           *
  84           * @param string $title    The widget title.
  85           * @param array  $instance The settings for the particular instance of the widget.
  86           * @param string $id_base  Root ID for all widgets of this type.
  87           */
  88          $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  89  
  90          /**
  91           * Filters the separator of the group widget links.
  92           *
  93           * @since 2.4.0
  94           *
  95           * @param string $separator Separator string. Default '|'.
  96           */
  97          $separator = apply_filters( 'bp_groups_widget_separator', '|' );
  98  
  99          echo $before_widget;
 100  
 101          $title = ! empty( $instance['link_title'] ) ? '<a href="' . bp_get_groups_directory_permalink() . '">' . $title . '</a>' : $title;
 102  
 103          echo $before_title . $title . $after_title;
 104  
 105          $max_limit  = bp_get_widget_max_count_limit( __CLASS__ );
 106          $max_groups = ! empty( $instance['max_groups'] ) ? (int) $instance['max_groups'] : 5;
 107  
 108          if ( $max_groups > $max_limit ) {
 109              $max_groups = $max_limit;
 110          }
 111  
 112          $group_args = array(
 113              'user_id'         => $user_id,
 114              'type'            => $instance['group_default'],
 115              'per_page'        => $max_groups,
 116              'max'             => $max_groups,
 117          );
 118  
 119          // Back up the global.
 120          $old_groups_template = $groups_template;
 121  
 122          ?>
 123  
 124          <?php if ( bp_has_groups( $group_args ) ) : ?>
 125              <div class="item-options" id="groups-list-options">
 126                  <a href="<?php bp_groups_directory_permalink(); ?>" id="newest-groups"<?php if ( $instance['group_default'] == 'newest' ) : ?> class="selected"<?php endif; ?>><?php _e("Newest", 'buddypress') ?></a>
 127                  <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span>
 128                  <a href="<?php bp_groups_directory_permalink(); ?>" id="recently-active-groups"<?php if ( $instance['group_default'] == 'active' ) : ?> class="selected"<?php endif; ?>><?php _e("Active", 'buddypress') ?></a>
 129                  <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span>
 130                  <a href="<?php bp_groups_directory_permalink(); ?>" id="popular-groups" <?php if ( $instance['group_default'] == 'popular' ) : ?> class="selected"<?php endif; ?>><?php _e("Popular", 'buddypress') ?></a>
 131                  <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span>
 132                  <a href="<?php bp_groups_directory_permalink(); ?>" id="alphabetical-groups" <?php if ( $instance['group_default'] == 'alphabetical' ) : ?> class="selected"<?php endif; ?>><?php _e("Alphabetical", 'buddypress') ?></a>
 133              </div>
 134  
 135              <ul id="groups-list" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
 136                  <?php while ( bp_groups() ) : bp_the_group(); ?>
 137                      <li <?php bp_group_class(); ?>>
 138                          <div class="item-avatar">
 139                              <a href="<?php bp_group_permalink() ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a>
 140                          </div>
 141  
 142                          <div class="item">
 143                              <div class="item-title"><?php bp_group_link(); ?></div>
 144                              <div class="item-meta">
 145                                  <span class="activity">
 146                                  <?php
 147                                      if ( 'newest' == $instance['group_default'] ) {
 148                                          printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
 149                                      } elseif ( 'popular' == $instance['group_default'] ) {
 150                                          bp_group_member_count();
 151                                      } else {
 152                                          printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
 153                                      }
 154                                  ?>
 155                                  </span>
 156                              </div>
 157                          </div>
 158                      </li>
 159  
 160                  <?php endwhile; ?>
 161              </ul>
 162              <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?>
 163              <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $max_groups ); ?>" />
 164  
 165          <?php else: ?>
 166  
 167              <div class="widget-error">
 168                  <?php _e('There are no groups to display.', 'buddypress') ?>
 169              </div>
 170  
 171          <?php endif; ?>
 172  
 173          <?php echo $after_widget;
 174  
 175          // Restore the global.
 176          $groups_template = $old_groups_template;
 177      }
 178  
 179      /**
 180       * Extends our update method.
 181       *
 182       * @since 1.0.3
 183       *
 184       * @param array $new_instance New instance data.
 185       * @param array $old_instance Original instance data.
 186       * @return array
 187       */
 188  	public function update( $new_instance, $old_instance ) {
 189          $instance = $old_instance;
 190  
 191          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 192  
 193          $instance['title']         = strip_tags( $new_instance['title'] );
 194          $instance['max_groups']    = $new_instance['max_groups'] > $max_limit ? $max_limit : intval( $new_instance['max_groups'] );
 195          $instance['group_default'] = strip_tags( $new_instance['group_default'] );
 196          $instance['link_title']    = ! empty( $new_instance['link_title'] );
 197  
 198          return $instance;
 199      }
 200  
 201      /**
 202       * Extends our form method.
 203       *
 204       * @since 1.0.3
 205       *
 206       * @param array $instance Current instance.
 207       * @return mixed
 208       */
 209  	public function form( $instance ) {
 210          $defaults = array(
 211              'title'         => __( 'Groups', 'buddypress' ),
 212              'max_groups'    => 5,
 213              'group_default' => 'active',
 214              'link_title'    => false
 215          );
 216          $instance = bp_parse_args( (array) $instance, $defaults, 'groups_widget_form' );
 217  
 218          $max_limit = bp_get_widget_max_count_limit( __CLASS__ );
 219  
 220          $title            = strip_tags( $instance['title'] );
 221          $max_groups    = $instance['max_groups'] > $max_limit ? $max_limit : intval( $instance['max_groups'] );
 222          $group_default = strip_tags( $instance['group_default'] );
 223          $link_title    = (bool) $instance['link_title'];
 224          ?>
 225  
 226          <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'buddypress'); ?> <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%" /></label></p>
 227  
 228          <p><label for="<?php echo $this->get_field_id('link_title') ?>"><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 ) ?> /> <?php _e( 'Link widget title to Groups directory', 'buddypress' ) ?></label></p>
 229  
 230          <p><label for="<?php echo $this->get_field_id( 'max_groups' ); ?>"><?php _e( 'Max groups to show:', 'buddypress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_groups' ); ?>" name="<?php echo $this->get_field_name( 'max_groups' ); ?>" type="number" min="1" max="<?php echo esc_attr( $max_limit ); ?>" value="<?php echo esc_attr( $max_groups ); ?>" style="width: 30%" /></label></p>
 231  
 232          <p>
 233              <label for="<?php echo $this->get_field_id( 'group_default' ); ?>"><?php _e('Default groups to show:', 'buddypress'); ?></label>
 234              <select name="<?php echo $this->get_field_name( 'group_default' ); ?>" id="<?php echo $this->get_field_id( 'group_default' ); ?>">
 235                  <option value="newest" <?php selected( $group_default, 'newest' ); ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
 236                  <option value="active" <?php selected( $group_default, 'active' ); ?>><?php _e( 'Active', 'buddypress' ) ?></option>
 237                  <option value="popular"  <?php selected( $group_default, 'popular' ); ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
 238                  <option value="alphabetical" <?php selected( $group_default, 'alphabetical' ); ?>><?php _e( 'Alphabetical', 'buddypress' ) ?></option>
 239              </select>
 240          </p>
 241      <?php
 242      }
 243  }


Generated: Sat Jul 20 01:01:39 2019 Cross-referenced by PHPXref 0.7.1