log_transaction($user_id, abs($points), $context, $args); } /** * Deduct points from a user and log the transaction. */ public function deduct(int $user_id, int $points, string $context, array $args = []) { if ($user_id <= 0 || $points <= 0) { return false; } return $this->log_transaction($user_id, -abs($points), $context, $args); } /** * Get the total points for a specific user and point type. * Includes caching to prevent heavy queries. */ public function get_total(int $user_id, int $point_type_id = 1): int { $safe_user_id = (int) $user_id; $safe_pt_id = (int) $point_type_id; if ($safe_user_id <= 0) { return 0; } // Check Cache first $cache_key = "gameengine_user_points_{$safe_user_id}_{$safe_pt_id}"; $total = wp_cache_get($cache_key, 'gameengine'); if (false === $total) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $total = (int) $wpdb->get_var($wpdb->prepare( "SELECT SUM(points) FROM {$wpdb->prefix}gameengine_points_log WHERE user_id = %d AND point_type_id = %d", $safe_user_id, $safe_pt_id )); wp_cache_set($cache_key, $total, 'gameengine'); } return (int) $total; } /** * Internal function to handle the database insertion. */ private function log_transaction(int $user_id, int $points_value, string $context, array $args) { global $wpdb; $safe_user_id = (int) $user_id; $point_type_id = isset($args['point_type_id']) ? absint($args['point_type_id']) : 1; $requirement_id = isset($args['requirement_id']) ? absint($args['requirement_id']) : null; $description = isset($args['description']) ? sanitize_text_field($args['description']) : null; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->insert( $wpdb->prefix . 'gameengine_points_log', [ 'user_id' => $safe_user_id, 'point_type_id' => $point_type_id, 'points' => (int) $points_value, 'context' => sanitize_key($context), 'requirement_id' => $requirement_id, 'description' => $description, 'created_at' => current_time('mysql'), ], ['%d', '%d', '%d', '%s', '%d', '%s', '%s'] ); if (! $result) { return false; } $log_id = $wpdb->insert_id; // Clear User Level Cache (if applicable) wp_cache_delete("gameengine_user_points_{$safe_user_id}_{$point_type_id}", 'gameengine'); wp_cache_delete("gameengine_user_grand_total_{$safe_user_id}", 'gameengine'); // Increment Global Frontend Cache Versions \GameEngine\Helper::clear_cache_group('leaderboard'); \GameEngine\Helper::clear_cache_group('dashboard'); if ($points_value > 0) { do_action('gameengine_points_added', $safe_user_id, $points_value, $context, $log_id, $point_type_id); } else { do_action('gameengine_points_deducted', $safe_user_id, abs($points_value), $context, $log_id, $point_type_id); } return $log_id; } /** * Get grand total points (sum of all point types). */ /** * Every published point type, for a currency picker. * * Static because the front-end templates that need a currency list have no * manager instance to hand. * * @return array List of rows with id, name, plural_name and slug. */ public static function get_point_types(): array { global $wpdb; $cached = wp_cache_get('gameengine_published_point_types', 'gameengine'); if (false !== $cached) { return (array) $cached; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( "SELECT id, name, plural_name, slug FROM {$wpdb->prefix}gameengine_point_types WHERE status = 'publish' ORDER BY id ASC", ARRAY_A ) ?: array(); wp_cache_set('gameengine_published_point_types', $rows, 'gameengine', 5 * MINUTE_IN_SECONDS); return $rows; } /** * What a point type's points are called, for labels like "100 Community Points". * * @param int $point_type_id Point type ID. * @return string Its plural name, else its name, else "points". */ public static function get_point_type_label(int $point_type_id): string { foreach (self::get_point_types() as $row) { $row = (array) $row; if ((int) $row['id'] !== $point_type_id) { continue; } if (! empty($row['plural_name'])) { return (string) $row['plural_name']; } if (! empty($row['name'])) { return (string) $row['name']; } } return __('points', 'gameengine'); } /** * The point type to use when a caller doesn't name a published one. * * Returns `$preferred` when it is a published point type, otherwise the * first published type, or 0 when there is none. A site need not have #1. * * @param int $preferred Point type id to prefer. * @return int */ public static function resolve_point_type_id(int $preferred = 0): int { $published = array(); foreach (self::get_point_types() as $row) { $published[] = (int) ((array) $row)['id']; } if ($preferred > 0 && in_array($preferred, $published, true)) { return $preferred; } return $published[0] ?? 0; } public function get_grand_total(int $user_id): int { $safe_user_id = (int) $user_id; if ($safe_user_id <= 0) { return 0; } global $wpdb; $cache_key = "gameengine_user_grand_total_{$safe_user_id}"; $total = wp_cache_get($cache_key, 'gameengine'); if (false === $total) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $total = (int) $wpdb->get_var($wpdb->prepare( "SELECT SUM(points) FROM {$wpdb->prefix}gameengine_points_log WHERE user_id = %d", $safe_user_id )); wp_cache_set($cache_key, $total, 'gameengine'); } return (int) $total; } }