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


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1