prefix . 'gsfp_' . $name; } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $scans = self::table( 'scans' ); $findings = self::table( 'findings' ); $reports = self::table( 'reports' ); $events = self::table( 'events' ); dbDelta( "CREATE TABLE {$scans} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, scan_uuid CHAR(36) NOT NULL, scan_type VARCHAR(20) NOT NULL, status VARCHAR(20) NOT NULL, scheduled_for DATETIME NULL, started_at DATETIME NOT NULL, completed_at DATETIME NULL, duration_ms INT UNSIGNED NULL, engine_version VARCHAR(20) NOT NULL, feed_version VARCHAR(40) NOT NULL, counts LONGTEXT NULL, check_states LONGTEXT NULL, error_summary TEXT NULL, PRIMARY KEY (id), UNIQUE KEY scan_uuid (scan_uuid), KEY status_started (status, started_at) ) {$charset};" ); dbDelta( "CREATE TABLE {$findings} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, scan_id BIGINT UNSIGNED NOT NULL, finding_key VARCHAR(191) NOT NULL, rule_id VARCHAR(100) NOT NULL, rule_version VARCHAR(20) NOT NULL, title VARCHAR(255) NOT NULL, category VARCHAR(50) NOT NULL, severity VARCHAR(10) NOT NULL, status VARCHAR(60) NOT NULL, confidence VARCHAR(10) NOT NULL, payload LONGTEXT NOT NULL, detected_at DATETIME NOT NULL, PRIMARY KEY (id), KEY scan_id (scan_id), KEY finding_key (finding_key), KEY severity_status (severity, status) ) {$charset};" ); dbDelta( "CREATE TABLE {$reports} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, kind VARCHAR(20) NOT NULL, dedupe_key VARCHAR(191) NOT NULL, scan_id BIGINT UNSIGNED NULL, local_date DATE NULL, recipients TEXT NULL, status VARCHAR(20) NOT NULL, attempts SMALLINT UNSIGNED NOT NULL DEFAULT 0, next_retry_at DATETIME NULL, last_attempt_at DATETIME NULL, sent_at DATETIME NULL, error_text TEXT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY dedupe_key (dedupe_key), KEY status_retry (status, next_retry_at) ) {$charset};" ); dbDelta( "CREATE TABLE {$events} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, event_type VARCHAR(64) NOT NULL, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, details LONGTEXT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY event_type (event_type), KEY created_at (created_at) ) {$charset};" ); update_option( self::VERSION_OPTION, GSFP_DB_VERSION, 'no' ); } /** Used by uninstall.php only when the admin opted into data deletion. */ public static function drop_all() { global $wpdb; foreach ( array( 'scans', 'findings', 'reports', 'events' ) as $name ) { $table = self::table( $name ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange -- internal table name. $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); } delete_option( self::VERSION_OPTION ); } }