prefix . 'agevow_' . $name; } /** * The full names of all three tables on the current site. * * @return string[] */ public static function names() { return array_map( array( self::class, 'table' ), self::TABLES ); } /** * The CREATE TABLE statements, in the exact layout dbDelta parses (two * spaces after PRIMARY KEY, one field per line, KEY rather than INDEX). * * The checks are indexed by date and by order (§S-LESSONS row 50: the Checks * screen of a busy shop filters by date, an order's page looks up its own row). * The tally's day + source + outcome is unique: a visitor's report adds 1 to * the one row of its kind, it never adds a row. * * @return string */ public static function sql() { global $wpdb; $charset = $wpdb->get_charset_collate(); $checks = self::table( 'checks' ); $tally = self::table( 'tally' ); $history = self::table( 'history' ); return "CREATE TABLE {$checks} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, order_id bigint(20) unsigned NULL DEFAULT NULL, checked_at datetime NOT NULL, source varchar(16) NOT NULL DEFAULT '', method varchar(16) NOT NULL DEFAULT '', min_age tinyint(3) unsigned NOT NULL DEFAULT 0, result varchar(16) NOT NULL DEFAULT '', ref varchar(64) NULL DEFAULT NULL, version varchar(20) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY checked_at (checked_at), KEY order_id (order_id) ) {$charset}; CREATE TABLE {$tally} ( day date NOT NULL, source varchar(16) NOT NULL DEFAULT '', outcome varchar(16) NOT NULL DEFAULT '', n int(10) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (day,source,outcome) ) {$charset}; CREATE TABLE {$history} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, changed_at datetime NOT NULL, user_id bigint(20) unsigned NOT NULL DEFAULT 0, what varchar(32) NOT NULL DEFAULT '', before_value text NULL, after_value text NULL, PRIMARY KEY (id), KEY changed_at (changed_at), KEY user_id (user_id) ) {$charset};"; } /** * Create or upgrade the tables, then record the schema version. * * @return void */ public static function install() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( self::sql() ); update_option( self::OPTION, self::DB_VERSION, true ); } /** * Which of our tables are missing on the current site — one query for all three. * * @return string[] Short names of the missing tables; empty when all exist. */ public static function missing() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- asking the database which of this plugin's own tables exist; there is no API for it, and a cached answer is the one thing this check must not trust. $found = (array) $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix . 'agevow_' ) . '%' ) ); $missing = array(); foreach ( self::TABLES as $name ) { if ( ! in_array( self::table( $name ), $found, true ) ) { $missing[] = $name; } } return $missing; } /** * Whether all three tables exist on the current site. * * @return bool */ public static function exists() { return array() === self::missing(); } /** * Drop all three tables on the current site (uninstall only). * * @return void */ public static function drop() { global $wpdb; foreach ( self::TABLES as $name ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- dropping this plugin's own tables is the point of uninstall. $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', self::table( $name ) ) ); } } }