prefix . 'elevenrank_redirects'; } /** * Name of the 404 log table. * * @since 1.0.0 * * @return string */ public static function log_table(): string { global $wpdb; return $wpdb->prefix . 'elevenrank_404_log'; } /** * Creates or updates the tables of the current site. Safe to run again. * * @since 1.0.0 * * @return void */ public static function install(): void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $redirects = self::redirects_table(); $log = self::log_table(); // dbDelta() is picky: two spaces after PRIMARY KEY, one definition per line. dbDelta( "CREATE TABLE {$redirects} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, source varchar(1000) NOT NULL, source_hash char(32) NOT NULL, match_type varchar(10) NOT NULL DEFAULT 'exact', destination text NOT NULL, type smallint(5) unsigned NOT NULL DEFAULT 301, status varchar(10) NOT NULL DEFAULT 'active', hits bigint(20) unsigned NOT NULL DEFAULT 0, last_hit datetime DEFAULT NULL, created datetime NOT NULL, PRIMARY KEY (id), KEY source_lookup (source_hash,status), KEY match_status (match_type,status) ) {$charset};" ); dbDelta( "CREATE TABLE {$log} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, uri_hash char(32) NOT NULL, uri varchar(1000) NOT NULL, referrer varchar(1000) NOT NULL DEFAULT '', hits bigint(20) unsigned NOT NULL DEFAULT 1, first_seen datetime NOT NULL, last_seen datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY uri_hash (uri_hash), KEY last_seen (last_seen), KEY hits (hits) ) {$charset};" ); } /** * Removes the tables of the current site. * * @since 1.0.0 * * @return void */ public static function drop(): void { global $wpdb; foreach ( array( self::redirects_table(), self::log_table() ) as $table ) { // This runs on uninstall only; the table name is an identifier placeholder. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); } } /** * Whether a table exists. Asks for its columns rather than listing tables, so it also works for the * temporary tables the test suite creates. * * @since 1.0.0 * * @param string $table Full table name, with prefix. * @return bool */ public static function table_exists( string $table ): bool { global $wpdb; $suppress = $wpdb->suppress_errors( true ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $columns = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $table ) ); $wpdb->suppress_errors( $suppress ); return is_array( $columns ) && count( $columns ) > 0; } /** * Whether the tables of this site are in place, judged from the stored schema version. * * Cheap enough for every request: the option autoloads. * * @since 1.0.0 * * @return bool */ public static function is_ready(): bool { return (int) get_option( Upgrader::DB_VERSION_OPTION, 0 ) >= Upgrader::DB_VERSION; } }