ap_get_user_reputation( integer $user_id, boolean $group = false )

Description #

Count reputation points of a user.

Parameters #

  • $user_id
    integer (Required) ID of user.
  • $group
    boolean (Optional) Return total count or group by event count. Default value: false

Source #

File: includes/reputation.php

function ap_get_user_reputation( $user_id, $group = false ) {
	global $wpdb;

	$events = $wpdb->get_results( $wpdb->prepare( "SELECT count(*) as count, rep_event  FROM {$wpdb->ap_reputations} WHERE rep_user_id = %d GROUP BY rep_event", $user_id ) ); // phpcs:ignore WordPress.DB

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

	$count = array();

	$reputation_events = ap_get_reputation_events();

	if ( ! empty( $reputation_events ) ) {
		foreach ( $reputation_events as $slug => $event ) {
			$count[ $slug ] = isset( $event_counts[ $slug ] ) ? ( (int) $event_counts[ $slug ] * (int) $event['points'] ) : 0;
		}
	}

	if ( false === $group ) {
		return array_sum( $count );
	}

	return $count;
}

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