$log_id, 'point_type_id' => $point_type_id, 'type' => 'award' ], 'success' ); } /** * Callback when points are deducted. */ public function handle_points_deducted($user_id, $points, $context, $log_id, $point_type_id) { self::log( $context, "User lost {$points} points.", $user_id, -$points, [ 'log_id' => $log_id, 'point_type_id' => $point_type_id, 'type' => 'deduct' ], 'success' ); } /** * Static method to insert a log entry into the database. */ public static function log($trigger_key, $message, $user_id = 0, $points_awarded = 0, $meta = [], $status = 'success') { // The settings screen calls the failure level "error"; the status // column is ENUM('success','failed','skipped'). Left as they were, an // admin could only enable a level the column rejects, while the only // status any caller emits was filtered out — so nothing ever recorded a // failure. Fold the two names onto the column's value. $status = ('error' === $status) ? 'failed' : $status; $settings = get_option('gameengine_log_settings', []); $allowed_levels = isset($settings['log_levels']) ? (array) $settings['log_levels'] : ['success', 'error']; $allowed_levels = array_map( static function ($level) { return ('error' === $level) ? 'failed' : $level; }, $allowed_levels ); if (!in_array($status, $allowed_levels, true)) { return; // Stop if admin disabled this log level } global $wpdb; $table = $wpdb->prefix . 'gameengine_logs'; if (!$user_id) { $user_id = get_current_user_id(); } // Check if table exists securely // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->insert($table, [ 'user_id' => $user_id, 'trigger_key' => sanitize_key($trigger_key), 'status' => $status, 'points_awarded' => intval($points_awarded), 'message' => sanitize_text_field($message), 'meta' => json_encode($meta), 'created_at' => current_time('mysql') ]); } }