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

1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
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

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.