BP_Messages_Thread::get( $args )
Query for recipients.
Description
Parameters
- $args
-
(Required) Array of parameters. All items are optional
Return
(array)
Source
File: bp-messages/classes/class-bp-messages-thread.php
public static function get( $args ) {
global $wpdb;
$bp = buddypress();
$defaults = array(
'orderby' => 'id',
'order' => 'DESC',
'per_page' => 20,
'page' => 1,
'user_id' => 0,
'is_deleted' => false,
'include' => false,
'exclude' => false,
'include_threads' => false,
'exclude_threads' => false,
'is_new' => null,
'fields' => 'all',
'count_total' => false,
'exclude_active_users' => false,
);
$r = bp_parse_args( $args, $defaults, 'bp_recipients_recipient_get' );
$sql = array(
'select' => 'SELECT DISTINCT r.id',
'from' => "{$bp->messages->table_name_recipients} r",
'where' => '',
'orderby' => '',
'pagination' => '',
);
$where_conditions = array();
if ( ! empty( $r['include'] ) ) {
$include = implode( ',', wp_parse_id_list( $r['include'] ) );
$where_conditions['include'] = "r.id IN ({$include})";
}
if ( ! empty( $r['exclude'] ) ) {
$exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) );
$where_conditions['exclude'] = "r.id NOT IN ({$exclude})";
}
if ( ! empty( $r['include_threads'] ) ) {
$include_threads = implode( ',', wp_parse_id_list( $r['include_threads'] ) );
$where_conditions['include_threads'] = "r.thread_id IN ({$include_threads})";
}
if ( ! empty( $r['exclude_threads'] ) ) {
$exclude_threads = implode( ',', wp_parse_id_list( $r['exclude_threads'] ) );
$where_conditions['exclude_threads'] = "r.thread_id NOT IN ({$exclude_threads})";
}
if ( null !== $r['is_new'] ) {
if ( 1 == $r['is_new'] ) {
$where_conditions['is_new'] = "r.unread_count != 0";
} else {
$where_conditions['is_new'] = "r.unread_count = 0";
}
}
if ( ! empty( $r['user_id'] ) ) {
$where_conditions['user'] = $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
}
if ( false !== $r['is_deleted'] ) {
$where_conditions['is_deleted'] = $wpdb->prepare( 'r.is_deleted = %d', $r['is_deleted'] );
}
if ( true === $r['exclude_active_users'] ) {
$where_conditions['exclude_active_users'] = 'user_id NOT IN (SELECT ID FROM ' . $wpdb->users . ')';
}
/* Order/orderby ********************************************/
$order = $r['order'];
$orderby = $r['orderby'];
// Sanitize 'order'.
$order = bp_esc_sql_order( $order );
/**
* Filters the converted 'orderby' term.
*
* @since BuddyBoss 1.5.4
*
* @param string $value Converted 'orderby' term.
* @param string $orderby Original orderby value.
*/
$orderby = apply_filters( 'bp_recipients_recipient_get_orderby', self::convert_orderby_to_order_by_term( $orderby ), $orderby );
$sql['orderby'] = "ORDER BY {$orderby} {$order}";
if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) && - 1 !== $r['per_page'] ) {
$sql['pagination'] = $wpdb->prepare( 'LIMIT %d, %d', intval( ( $r['page'] - 1 ) * $r['per_page'] ), intval( $r['per_page'] ) );
}
/**
* Filters the Where SQL statement.
*
* @since BuddyBoss 1.5.4
*
* @param array $r Array of parsed arguments for the get method.
* @param array $where_conditions Where conditions SQL statement.
*/
$where_conditions = apply_filters( 'bp_recipients_recipient_get_where_conditions', $where_conditions, $r );
$where = '';
if ( ! empty( $where_conditions ) ) {
$sql['where'] = implode( ' AND ', $where_conditions );
$where = "WHERE {$sql['where']}";
}
/**
* Filters the From SQL statement.
*
* @since BuddyBoss 1.5.4
*
* @param array $r Array of parsed arguments for the get method.
* @param string $sql From SQL statement.
*/
$sql['from'] = apply_filters( 'bp_recipients_recipient_get_join_sql', $sql['from'], $r );
$paged_recipients_sql = "{$sql['select']} FROM {$sql['from']} {$where} {$sql['orderby']} {$sql['pagination']}";
/**
* Filters the pagination SQL statement.
*
* @since BuddyBoss 1.5.4
*
* @param string $value Concatenated SQL statement.
* @param array $sql Array of SQL parts before concatenation.
* @param array $r Array of parsed arguments for the get method.
*/
$paged_recipients_sql = apply_filters( 'bp_recipients_recipient_get_paged_sql', $paged_recipients_sql, $sql, $r );
$paged_recipient_ids = $wpdb->get_col( $paged_recipients_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$paged_recipients = array();
if ( 'ids' === $r['fields'] ) {
// We only want the IDs.
$paged_recipients = array_map( 'intval', $paged_recipient_ids );
} elseif ( ! empty( $paged_recipient_ids ) ) {
$recipient_ids_sql = implode( ',', array_map( 'intval', $paged_recipient_ids ) );
$recipient_data_objects = $wpdb->get_results( "SELECT r.* FROM {$bp->messages->table_name_recipients} r WHERE r.id IN ({$recipient_ids_sql})" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
foreach ( (array) $recipient_data_objects as $mdata ) {
$recipient_data_objects[ $mdata->id ] = $mdata;
}
foreach ( $paged_recipient_ids as $paged_recipient_id ) {
$paged_recipients[] = $recipient_data_objects[ $paged_recipient_id ];
}
}
$retval = array(
'recipients' => $paged_recipients,
'total' => 0,
);
if ( ! empty( $r['count_total'] ) ) {
// Find the total number of messages in the results set.
$total_recipients_sql = "SELECT COUNT(DISTINCT r.id) FROM {$sql['from']} $where";
/**
* Filters the SQL used to retrieve total message results.
*
* @since BuddyBoss 1.5.4
*
* @param string $t_sql Concatenated SQL statement used for retrieving total messages results.
* @param array $total_sql Array of SQL parts for the query.
* @param array $r Array of parsed arguments for the get method.
*/
$total_recipients_sql = apply_filters( 'bp_recipients_recipient_get_total_sql', $total_recipients_sql, $sql, $r );
$total_recipients = (int) $wpdb->get_var( $total_recipients_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$retval['total'] = $total_recipients;
}
return $retval;
}
Changelog
| Version | Description |
|---|---|
| BuddyBoss 1.5.4 | 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.