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

	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.