class GST_Database { const TABLE_RECORDS = 'gst_records'; const TABLE_TOKENS = 'gst_tokens'; public static function create_tables() { global $wpdb; $records_table = $wpdb->prefix . self::TABLE_RECORDS; $tokens_table = $wpdb->prefix . self::TABLE_TOKENS; $charset_collate = $wpdb->get_charset_collate(); $sql_records = "CREATE TABLE $records_table ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, unique_code VARCHAR(100) NOT NULL, score_type ENUM('gerd-q', 'rfs') NOT NULL, score TINYINT UNSIGNED NOT NULL, result TEXT NOT NULL, user_id BIGINT UNSIGNED DEFAULT 0, email VARCHAR(255) DEFAULT '', created_at DATETIME NOT NULL, PRIMARY KEY (id), INDEX unique_code_idx (unique_code) ) $charset_collate;"; $sql_tokens = "CREATE TABLE $tokens_table ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, record_id BIGINT UNSIGNED NOT NULL, token CHAR(32) NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY token (token), FOREIGN KEY (record_id) REFERENCES $records_table(id) ON DELETE CASCADE ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql_records); dbDelta($sql_tokens); } public static function save_record($data) { // 保存记录并返回记录ID } public static function generate_token($record_id) { // 生成32位token并存储 } public static function get_record_by_token($token) { // 通过token获取记录 } public static function get_records($filters = []) { // 获取后台记录 } }class GST_Shortcodes { public static function init() { add_shortcode('gerd_q_form', [__CLASS__, 'gerd_q_form']); add_shortcode('rfs_form', [__CLASS__, 'rfs_form']); add_shortcode('score_result', [__CLASS__, 'score_result']); } public static function gerd_q_form() { ob_start(); include GST_PATH . 'templates/gerd-q-form.php'; return ob_get_clean(); } public static function rfs_form() { ob_start(); include GST_PATH . 'templates/rfs-form.php'; return ob_get_clean(); } public static function score_result() { ob_start(); include GST_PATH . 'templates/result-display.php'; return ob_get_clean(); } }class GST_Ajax_Handler { public static function init() { add_action('wp_ajax_gst_submit_score', [__CLASS__, 'submit_score']); add_action('wp_ajax_nopriv_gst_submit_score', [__CLASS__, 'submit_score']); add_action('wp_ajax_gst_send_email', [__CLASS__, 'send_email']); } public static function submit_score() { // 验证nonce check_ajax_referer('gst_nonce', 'nonce'); $data = [ 'score_type' => sanitize_text_field($_POST['type']), 'answers' => array_map('intval', $_POST['answers']), 'unique_code' => sanitize_text_field($_POST['unique_code']), 'email' => sanitize_email($_POST['email']), 'user_id' => get_current_user_id() ]; // 计算分数和结果 $score = self::calculate_score($data); $result = self::generate_result($score, $data['score_type']); // 保存到数据库 $record_id = GST_Database::save_record([ 'unique_code' => $data['unique_code'], 'score_type' => $data['score_type'], 'score' => $score, 'result' => $result, 'user_id' => $data['user_id'], 'email' => $data['email'] ]); // 生成token $token = GST_Database::generate_token($record_id); // 返回结果 wp_send_json_success([ 'score' => $score, 'result' => $result, 'token' => $token, 'unique_code' => $data['unique_code'] ]); } private static function calculate_score($data) { // 根据评分类型计算分数 if ($data['score_type'] === 'gerd-q') { // GERD-Q评分逻辑 return array_sum($data['answers']); } else { // RFS评分逻辑 return array_sum($data['answers']); } } private static function generate_result($score, $type) { // 根据分数和类型生成结果分析 if ($type === 'gerd-q') { if ($score >= 8) return __('High probability of GERD', 'gst'); else return __('Low probability', 'gst'); } else { // RFS结果分析 } } public static function send_email() { // 邮件发送逻辑 $to = sanitize_email($_POST['email']); $subject = __('Your Scoring Result', 'gst'); $message = sanitize_text_field($_POST['result']); $sent = wp_mail($to, $subject, $message); wp_send_json_success(['sent' => $sent]); } }class GST_Admin { public static function init() { add_action('admin_menu', [__CLASS__, 'add_admin_menu']); add_action('admin_init', [__CLASS__, 'register_settings']); } public static function add_admin_menu() { add_menu_page( __('Scoring Records', 'gst'), __('Gastro Scoring', 'gst'), 'manage_options', 'gst_results', [__CLASS__, 'render_admin_page'], 'dashicons-clipboard' ); } public static function register_settings() { register_setting('gst_settings', 'gst_wechat_qr'); add_settings_section('gst_settings_section', '', null, 'gst_settings'); add_settings_field( 'gst_wechat_qr', __('WeChat QR Code URL', 'gst'), [__CLASS__, 'qr_callback'], 'gst_settings', 'gst_settings_section' ); } public static function qr_callback() { $qr = get_option('gst_wechat_qr'); echo ''; echo '

'.__('Upload image via Media Library and paste URL here', 'gst').'

'; } public static function render_admin_page() { // 获取记录 $records = GST_Database::get_records(); // 导出功能 if (isset($_GET['export'])) { self::export_records($records); } include GST_PATH . 'templates/admin-page.php'; } private static function export_records($records) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="scoring_records.csv"'); $output = fopen('php://output', 'w'); fputcsv($output, ['ID', 'Unique Code', 'Score Type', 'Score', 'User', 'Date']); foreach ($records as $record) { fputcsv($output, [ $record->id, $record->unique_code, $record->score_type, $record->score, $record->user_id ? get_userdata($record->user_id)->display_name : __('Guest', 'gst'), $record->created_at ]); } fclose($output); exit; } }