BP_Groups_Widget

Groups widget.

Description

Source

File: bp-groups/classes/class-bp-groups-widget.php

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

Changelog

Changelog
Version Description
BuddyPress 1.0.3 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.