BP_Activity_List_Table::prepare_items()
Handle filtering of data, sorting, pagination, and any other data manipulation prior to rendering.
Description
Source
File: bp-activity/classes/class-bp-activity-list-table.php
function prepare_items() {
// Option defaults.
$filter = array();
$filter_query = false;
$include_id = false;
$search_terms = false;
$sort = 'DESC';
$spam = 'ham_only';
// Set current page.
$page = $this->get_pagenum();
// Set per page from the screen options.
$per_page = $this->get_items_per_page( str_replace( '-', '_', "{$this->screen->id}_per_page" ) );
// Check if we're on the "Spam" view.
if ( !empty( $_REQUEST['activity_status'] ) && 'spam' == $_REQUEST['activity_status'] ) {
$spam = 'spam_only';
$this->view = 'spam';
}
// Sort order.
if ( !empty( $_REQUEST['order'] ) && 'desc' != $_REQUEST['order'] )
$sort = 'ASC';
// Order by.
/*if ( !empty( $_REQUEST['orderby'] ) ) {
}*/
// Filter.
if ( ! empty( $_REQUEST['activity_type'] ) ) {
$filter = array( 'action' => $_REQUEST['activity_type'] );
/**
* Filter here to override the filter with a filter query
*
* @since BuddyPress 2.5.0
*
* @param array $filter
*/
$has_filter_query = apply_filters( 'bp_activity_list_table_filter_activity_type_items', $filter );
if ( ! empty( $has_filter_query['filter_query'] ) ) {
// Reset the filter
$filter = array();
// And use the filter query instead
$filter_query = $has_filter_query['filter_query'];
}
}
// Are we doing a search?
if ( !empty( $_REQUEST['s'] ) )
$search_terms = $_REQUEST['s'];
// Check if user has clicked on a specific activity (if so, fetch only that, and any related, activity).
if ( !empty( $_REQUEST['aid'] ) )
$include_id = (int) $_REQUEST['aid'];
// Get the spam total (ignoring any search query or filter).
$spams = bp_activity_get( array(
'display_comments' => 'stream',
'show_hidden' => true,
'spam' => 'spam_only',
'count_total' => 'count_query',
) );
$this->spam_count = $spams['total'];
unset( $spams );
// Get the activities from the database.
$activities = bp_activity_get( array(
'display_comments' => 'stream',
'filter' => $filter,
'in' => $include_id,
'page' => $page,
'per_page' => $per_page,
'search_terms' => $search_terms,
'filter_query' => $filter_query,
'show_hidden' => true,
// 'sort' => $sort,
'spam' => $spam,
'count_total' => 'count_query',
) );
// If we're viewing a specific activity, flatten all activities into a single array.
if ( $include_id ) {
$activities['activities'] = BP_Activity_List_Table::flatten_activity_array( $activities['activities'] );
$activities['total'] = count( $activities['activities'] );
// Sort the array by the activity object's date_recorded value.
usort( $activities['activities'], function( $a, $b ) { return $a->date_recorded > $b->date_recorded; } );
}
// The bp_activity_get function returns an array of objects; cast these to arrays for WP_List_Table.
$new_activities = array();
foreach ( $activities['activities'] as $activity_item ) {
$new_activities[] = (array) $activity_item;
// Build an array of activity-to-user ID mappings for better efficiency in the In Response To column.
$this->activity_user_id[$activity_item->id] = $activity_item->user_id;
}
// Set raw data to display.
$this->items = $new_activities;
// Store information needed for handling table pagination.
$this->set_pagination_args( array(
'per_page' => $per_page,
'total_items' => $activities['total'],
'total_pages' => ceil( $activities['total'] / $per_page )
) );
// Don't truncate activity items; bp_activity_truncate_entry() needs to be used inside a BP_Activity_Template loop.
remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 1.6.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.