ap_get_users_reputation( array $user_ids )

Description #

Get reputation of multiple users.

Parameters #

  • $user_ids
    array (Required) User ids.

Source #

File: includes/reputation.php

function ap_get_users_reputation( $user_ids ) {
	global $wpdb;
	$user_counts = array();

	foreach ( (array) $user_ids as $id ) {
		$user_counts[ (int) $id ] = array();
	}

	$sanitized = implode( ',', array_keys( $user_counts ) );
	$query     = "SELECT count(*) as count, rep_event, rep_user_id FROM {$wpdb->ap_reputations} WHERE rep_user_id IN ({$sanitized}) GROUP BY rep_event, rep_user_id";

	$events = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB

	foreach ( (array) $events as $count ) {
		if ( empty( $event_counts[ $count->rep_user_id ] ) ) {
			$event_counts[ $count->rep_user_id ] = array();
		}

		$user_counts[ $count->rep_user_id ][ $count->rep_event ] = $count->count;
	}

	$counts     = array();
	$all_events = ap_get_reputation_events();

	foreach ( $user_counts as $user_id => $events ) {
		$counts[ $user_id ] = array();
		foreach ( $all_events as $slug => $event ) {
			$counts[ $user_id ][ $slug ] = isset( $events[ $slug ] ) ? ( (int) $events[ $slug ] * (int) $event['points'] ) : 0;
		}
	}

	return $counts;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment