AnsPress_Leaderboard_Widget::get_top_users( int $interval, int $limit )

Description #

Get top users from database.

Parameters #

  • $interval
    int (Required) Interval in days.
  • $limit
    int (Required) Limit of users.

Source #

File: widgets/leaderboard.php

	private function get_top_users( $interval, $limit ) {
		global $wpdb;

		$interval = absint( $interval );
		$limit    = absint( $limit );
		$cap_key  = $wpdb->prefix . 'capabilities';

		return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->prepare(
				"SELECT users.ID, users.display_name, sum(rep_ev.points) AS aggregated  FROM $wpdb->ap_reputations rep
				INNER JOIN $wpdb->ap_reputation_events rep_ev ON rep.rep_event = rep_ev.slug
				INNER JOIN $wpdb->users users ON users.ID = rep.rep_user_id
				INNER JOIN $wpdb->usermeta meta ON meta.user_id = users.ID AND meta.meta_key = %s AND meta_value NOT LIKE %s
				WHERE rep.rep_date > current_date - interval %d day
				GROUP BY rep.rep_user_id
				ORDER BY aggregated DESC, users.ID ASC
				LIMIT %d",
				$cap_key,
				"%{$wpdb->esc_like('administrator')}%",
				$interval,
				$limit
			)
		);
	}

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