BP_Messages_Notice::get( array $args = array() )

Query for Notices.

Description

Parameters

$args

(Optional) Array of parameters. All items are optional

Default value: array()

Return

(array)

Source

File: bp-messages/classes/class-bp-messages-notice.php

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
public static function get( $args = array() ) {
    global $wpdb;
 
    $bp = buddypress();
 
    $defaults = array(
        'orderby'     => 'date_sent',
        'order'       => 'DESC',
        'per_page'    => 20,
        'page'        => 1,
        'include'     => false,
        'exclude'     => false,
        'fields'      => 'all',
        'is_active'   => null,
        'count_total' => false,
    );
 
    $r = bp_parse_args( $args, $defaults, 'bp_messages_notice_get' );
 
    $sql = array(
        'select'     => 'SELECT DISTINCT mn.id',
        'from'       => "{$bp->messages->table_name_notices} mn",
        'where'      => '',
        'orderby'    => '',
        'pagination' => '',
    );
 
    $where_conditions = array();
 
    if ( ! empty( $r['include'] ) ) {
        $include                     = implode( ',', wp_parse_id_list( $r['include'] ) );
        $where_conditions['include'] = "mn.id IN ({$include})";
    }
 
    if ( ! empty( $r['exclude'] ) ) {
        $exclude                     = implode( ',', wp_parse_id_list( $r['exclude'] ) );
        $where_conditions['exclude'] = "mn.id NOT IN ({$exclude})";
    }
 
    if ( isset( $r['is_active'] ) ) {
        $where_conditions['is_active'] = "mn.is_active = {$r['is_active']}";
    }
 
    /* Order/orderby ********************************************/
 
    $order   = $r['order'];
    $orderby = $r['orderby'];
 
    // Sanitize 'order'.
    $order = bp_esc_sql_order( $order );
 
    /**
     * Filters the converted 'orderby' term.
     *
     * @param string $value   Converted 'orderby' term.
     * @param string $orderby Original orderby value.
     *
     * @since BuddyBoss 1.5.4
     *
     */
    $orderby = apply_filters( 'bp_messages_notice_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.
     *
     * @param array $r                Array of parsed arguments for the get method.
     * @param array $where_conditions Where conditions SQL statement.
     *
     * @since BuddyBoss 1.5.4
     *
     */
    $where_conditions = apply_filters( 'bp_messages_notice_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.
     *
     * @param array  $r   Array of parsed arguments for the get method.
     * @param string $sql From SQL statement.
     *
     * @since BuddyBoss 1.5.4
     *
     */
    $sql['from'] = apply_filters( 'bp_messages_notice_get_join_sql', $sql['from'], $r );
 
    $paged_notices_sql = "{$sql['select']} FROM {$sql['from']} {$where} {$sql['orderby']} {$sql['pagination']}";
 
    /**
     * Filters the pagination SQL statement.
     *
     * @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.
     *
     * @since BuddyBoss 1.5.4
     *
     */
    $paged_notices_sql = apply_filters( 'bp_messages_notice_get_paged_sql', $paged_notices_sql, $sql, $r );
 
    $paged_notice_ids = $wpdb->get_col( $paged_notices_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
 
    $paged_notices = array();
 
    if ( 'ids' === $r['fields'] ) {
        // We only want the IDs.
        $paged_notices = array_map( 'intval', $paged_notice_ids );
    } elseif ( ! empty( $paged_notice_ids ) ) {
        $notice_ids_sql      = implode( ',', array_map( 'intval', $paged_notice_ids ) );
        $notice_data_objects = $wpdb->get_results( "SELECT mn.* FROM {$bp->messages->table_name_notices} mn WHERE mn.id IN ({$notice_ids_sql})" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
        foreach ( (array) $notice_data_objects as $mdata ) {
            $notice_data_objects[ $mdata->id ] = $mdata;
        }
        foreach ( $paged_notice_ids as $paged_notice_id ) {
            $paged_notices[] = $notice_data_objects[ $paged_notice_id ];
        }
    }
 
    $retval = array(
        'notices' => $paged_notices,
        'total'   => 0,
    );
 
    if ( ! empty( $r['count_total'] ) ) {
        // Find the total number of groups in the results set.
        $total_notices_sql = "SELECT COUNT(DISTINCT mn.id) FROM {$sql['from']} $where";
 
        /**
         * Filters the SQL used to retrieve total group results.
         *
         * @param string $t_sql     Concatenated SQL statement used for retrieving total group results.
         * @param array  $total_sql Array of SQL parts for the query.
         * @param array  $r         Array of parsed arguments for the get method.
         *
         * @since BuddyPress 1.5.0
         *
         */
        $total_notices_sql = apply_filters( 'bp_messages_notice_get_total_sql', $total_notices_sql, $sql, $r );
 
        $total_notices   = (int) $wpdb->get_var( $total_notices_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
        $retval['total'] = $total_notices;
    }
 
    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.