prefix . 'a11ymate_' . $name; } /** * Run the upgrade routine if the stored schema is out of date. * * @return void */ public static function maybe_upgrade() { if ( get_option( self::VERSION_OPTION ) !== self::VERSION ) { self::install(); } } /** * Create or update tables with dbDelta. * * @return void */ public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $scans = self::table( 'scans' ); $pages = self::table( 'pages' ); $iss = self::table( 'issues' ); $occ = self::table( 'occurrences' ); dbDelta( "CREATE TABLE {$scans} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, status varchar(20) NOT NULL DEFAULT 'running', source varchar(20) NOT NULL DEFAULT 'admin', started_at datetime NOT NULL, finished_at datetime DEFAULT NULL, total_pages int(11) NOT NULL DEFAULT 0, server_done int(11) NOT NULL DEFAULT 0, rendered_done int(11) NOT NULL DEFAULT 0, with_rendered tinyint(1) NOT NULL DEFAULT 0, score smallint(6) DEFAULT NULL, summary longtext, config longtext, PRIMARY KEY (id), KEY status (status) ) {$charset};" ); dbDelta( "CREATE TABLE {$pages} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, scan_id bigint(20) unsigned NOT NULL, url text NOT NULL, url_hash char(32) NOT NULL, post_id bigint(20) unsigned NOT NULL DEFAULT 0, kind varchar(20) NOT NULL DEFAULT 'post', server_done tinyint(1) NOT NULL DEFAULT 0, rendered_done tinyint(1) NOT NULL DEFAULT 0, http_code smallint(6) NOT NULL DEFAULT 0, title text, error text, score smallint(6) DEFAULT NULL, issue_count int(11) NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY scan_id (scan_id), KEY scan_state (scan_id,server_done,rendered_done), KEY url_hash (url_hash) ) {$charset};" ); dbDelta( "CREATE TABLE {$iss} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, fingerprint char(40) NOT NULL, rule_id varchar(64) NOT NULL, severity varchar(10) NOT NULL, category varchar(32) NOT NULL, pass varchar(10) NOT NULL DEFAULT 'server', mode varchar(14) NOT NULL DEFAULT 'automated', selector text, snippet text, state varchar(10) NOT NULL DEFAULT 'open', ignore_reason varchar(32) DEFAULT NULL, ignore_note text, first_scan bigint(20) unsigned NOT NULL DEFAULT 0, last_scan bigint(20) unsigned NOT NULL DEFAULT 0, first_seen datetime NOT NULL, last_seen datetime NOT NULL, fixed_at datetime DEFAULT NULL, page_count int(11) NOT NULL DEFAULT 0, occurrence_count int(11) NOT NULL DEFAULT 0, sample_url text, detail longtext, PRIMARY KEY (id), UNIQUE KEY fingerprint (fingerprint), KEY state (state), KEY rule_id (rule_id), KEY severity (severity), KEY category (category), KEY last_scan (last_scan) ) {$charset};" ); dbDelta( "CREATE TABLE {$occ} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, issue_id bigint(20) unsigned NOT NULL, scan_id bigint(20) unsigned NOT NULL, page_id bigint(20) unsigned NOT NULL, viewport varchar(10) NOT NULL DEFAULT 'all', detail text, PRIMARY KEY (id), UNIQUE KEY issue_page (issue_id,page_id), KEY scan_id (scan_id), KEY page_id (page_id) ) {$charset};" ); Settings::migrate_brand(); update_option( self::VERSION_OPTION, self::VERSION, false ); } /** * Drop all tables (used by uninstall). * * @return void */ public static function drop() { global $wpdb; foreach ( array( 'occurrences', 'issues', 'pages', 'scans' ) as $name ) { $table = self::table( $name ); $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.SchemaChange } delete_option( self::VERSION_OPTION ); } }