BP_Groups_List_Table::prepare_items()

Set up items for display in the list table.

Description

Handles filtering of data, sorting, pagination, and any other data manipulation required prior to rendering.

Source

File: bp-groups/classes/class-bp-groups-list-table.php

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
public function prepare_items() {
    global $groups_template;
 
    $screen = get_current_screen();
 
    // Option defaults.
    $include_id   = false;
    $search_terms = false;
 
    // Set current page.
    $page = $this->get_pagenum();
 
    // Set per page from the screen options.
    $per_page = $this->get_items_per_page( str_replace( '-', '_', "{$screen->id}_per_page" ) );
 
    // Sort order.
    $order = 'DESC';
    if ( !empty( $_REQUEST['order'] ) ) {
        $order = ( 'desc' == strtolower( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC';
    }
 
    // Order by - default to newest.
    $orderby = 'last_activity';
    if ( ! empty( $_REQUEST['orderby'] ) ) {
        switch ( $_REQUEST['orderby'] ) {
            case 'name' :
                $orderby = 'name';
                break;
            case 'id' :
                $orderby = 'date_created';
                break;
            case 'members' :
                $orderby = 'total_member_count';
                break;
            case 'last_active' :
                $orderby = 'last_activity';
                break;
        }
    }
 
    // Are we doing a search?
    if ( !empty( $_REQUEST['s'] ) )
        $search_terms = $_REQUEST['s'];
 
    // Check if user has clicked on a specific group (if so, fetch only that group).
    if ( !empty( $_REQUEST['gid'] ) )
        $include_id = (int) $_REQUEST['gid'];
 
    // Set the current view.
    if ( isset( $_GET['group_status'] ) && in_array( $_GET['group_status'], array( 'public', 'private', 'hidden' ) ) ) {
        $this->view = $_GET['group_status'];
    }
 
    // We'll use the ids of group status types for the 'include' param.
    $this->group_type_ids = BP_Groups_Group::get_group_type_ids();
 
    // Pass a dummy array if there are no groups of this type.
    $include = false;
    if ( 'all' != $this->view && isset( $this->group_type_ids[ $this->view ] ) ) {
        $include = ! empty( $this->group_type_ids[ $this->view ] ) ? $this->group_type_ids[ $this->view ] : array( 0 );
    }
 
    // Get group type counts for display in the filter tabs.
    $this->group_counts = array();
    foreach ( $this->group_type_ids as $group_type => $group_ids ) {
        $this->group_counts[ $group_type ] = count( $group_ids );
    }
 
    // Group types
    $group_type = false;
    if ( isset( $_GET['bp-group-type'] ) && null !== bp_groups_get_group_type_object( $_GET['bp-group-type'] ) ) {
        $group_type = $_GET['bp-group-type'];
    }
 
    // If we're viewing a specific group, flatten all activities into a single array.
    if ( $include_id ) {
        $groups = array( (array) groups_get_group( $include_id ) );
    } else {
        $groups_args = array(
            'include'  => $include,
            'per_page' => $per_page,
            'page'     => $page,
            'orderby'  => $orderby,
            'order'    => $order
        );
 
        if ( $group_type ) {
            $groups_args['group_type'] = $group_type;
        }
 
        $groups = array();
        if ( bp_has_groups( $groups_args ) ) {
            while ( bp_groups() ) {
                bp_the_group();
                $groups[] = (array) $groups_template->group;
            }
        }
    }
 
    // Set raw data to display.
    $this->items = $groups;
 
    // Store information needed for handling table pagination.
    $this->set_pagination_args( array(
        'per_page'    => $per_page,
        'total_items' => $groups_template->total_group_count,
        'total_pages' => ceil( $groups_template->total_group_count / $per_page )
    ) );
}

Changelog

Changelog
Version Description
BuddyPress 1.7.0 Introduced.

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.