| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Games schema. | |
| 3 | + * OpenStation — Games schema. | |
| 4 | 4 | * |
| 5 | 5 | * Two custom tables back the game system: |
| 6 | 6 | * |
| 7 | 7 | * - `_desktop_mode_game_scores` — one row per finished game run. |
| @@ -18,25 +18,35 @@ | ||
| 18 | 18 | * |
| 19 | 19 | * `state` columns are VARCHAR rather than ENUM, matching the |
| 20 | 20 | * folder-shares table (house style — dbDelta and ENUM don't mix). |
| 21 | 21 | * |
| 22 | - * @package WPDesktopMode | |
| 23 | - * @since 0.9.6 | |
| 22 | + * @package OpenStation | |
| 24 | 23 | */ |
| 25 | 24 | |
| 26 | 25 | defined( 'ABSPATH' ) || exit; |
| 27 | 26 | |
| 28 | -define( 'DESKTOP_MODE_GAMES_SCHEMA_VERSION', '1' ); | |
| 29 | -define( 'DESKTOP_MODE_GAMES_SCHEMA_OPTION', 'desktop_mode_games_schema_version' ); | |
| 27 | +define( 'OPENSTATION_GAMES_SCHEMA_VERSION', '1' ); | |
| 28 | +/** | |
| 29 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 30 | + * persisted or externally-visible identifier, so renaming it would | |
| 31 | + * orphan data already written by live installs (or break a live | |
| 32 | + * URL). The mismatch between this constant's name and its value is | |
| 33 | + * deliberate — it is NOT a half-finished rename. | |
| 34 | + */ | |
| 35 | +define( 'OPENSTATION_GAMES_SCHEMA_OPTION', 'desktop_mode_games_schema_version' ); | |
| 30 | 36 | |
| 31 | 37 | /** |
| 32 | 38 | * Returns the per-table names with the active prefix applied. |
| 33 | 39 | * |
| 34 | - * @since 0.9.6 | |
| 40 | + * The `desktop_mode_` segment is the pre-rebrand spelling and is frozen: | |
| 41 | + * these are real tables holding real rows on live installs. Renaming | |
| 42 | + * them silently creates a second, empty set and every score and | |
| 43 | + * challenge disappears. The mismatch against the `openstation_*` | |
| 44 | + * function name is deliberate. | |
| 35 | 45 | * |
| 36 | 46 | * @return array{ scores: string, challenges: string } |
| 37 | 47 | */ |
| 38 | -function desktop_mode_games_table_names() { | |
| 48 | +function openstation_games_table_names() { | |
| 39 | 49 | global $wpdb; |
| 40 | 50 | return array( |
| 41 | 51 | 'scores' => $wpdb->prefix . 'desktop_mode_game_scores', |
| 42 | 52 | 'challenges' => $wpdb->prefix . 'desktop_mode_game_challenges', |
| @@ -47,17 +57,15 @@ | ||
| 47 | 57 | * Idempotent `dbDelta` call. Hooked on plugin activation and lazily |
| 48 | 58 | * on `admin_init` / `rest_api_init` / `init` (gated by a |
| 49 | 59 | * version-option mismatch) so a manual file-copy install still ends |
| 50 | 60 | * up with the tables. |
| 51 | - * | |
| 52 | - * @since 0.9.6 | |
| 53 | 61 | */ |
| 54 | -function desktop_mode_games_install_schema() { | |
| 62 | +function openstation_games_install_schema() { | |
| 55 | 63 | global $wpdb; |
| 56 | 64 | |
| 57 | 65 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 58 | 66 | |
| 59 | - $tables = desktop_mode_games_table_names(); | |
| 67 | + $tables = openstation_games_table_names(); | |
| 60 | 68 | $charset_collate = $wpdb->get_charset_collate(); |
| 61 | 69 | |
| 62 | 70 | $scores_sql = "CREATE TABLE {$tables['scores']} ( |
| 63 | 71 | id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| @@ -99,21 +107,19 @@ | ||
| 99 | 107 | // Belt-and-suspenders: dbDelta's DESCRIBE-based table detection has |
| 100 | 108 | // documented failure modes (see the files schema for the full |
| 101 | 109 | // rationale) — verify both tables physically exist and CREATE them |
| 102 | 110 | // explicitly when not. |
| 103 | - desktop_mode_games_ensure_table( $tables['scores'], $scores_sql ); | |
| 104 | - desktop_mode_games_ensure_table( $tables['challenges'], $challenges_sql ); | |
| 111 | + openstation_games_ensure_table( $tables['scores'], $scores_sql ); | |
| 112 | + openstation_games_ensure_table( $tables['challenges'], $challenges_sql ); | |
| 105 | 113 | |
| 106 | - update_option( DESKTOP_MODE_GAMES_SCHEMA_OPTION, DESKTOP_MODE_GAMES_SCHEMA_VERSION ); | |
| 114 | + update_option( OPENSTATION_GAMES_SCHEMA_OPTION, OPENSTATION_GAMES_SCHEMA_VERSION ); | |
| 107 | 115 | |
| 108 | 116 | /** |
| 109 | 117 | * Fires after the games schema is installed / migrated. |
| 110 | 118 | * |
| 111 | - * @since 0.9.6 | |
| 112 | - * | |
| 113 | 119 | * @param string $version The version that was installed. |
| 114 | 120 | */ |
| 115 | - do_action( 'desktop_mode_games_schema_installed', DESKTOP_MODE_GAMES_SCHEMA_VERSION ); | |
| 121 | + do_action( 'openstation_games_schema_installed', OPENSTATION_GAMES_SCHEMA_VERSION ); | |
| 116 | 122 | } |
| 117 | 123 | |
| 118 | 124 | /** |
| 119 | 125 | * Create a table via `CREATE TABLE IF NOT EXISTS` when |
| @@ -120,21 +126,20 @@ | ||
| 120 | 126 | * INFORMATION_SCHEMA says dbDelta left it missing. Errors are |
| 121 | 127 | * suppressed around the CREATE so a concurrent worker that won the |
| 122 | 128 | * same race doesn't log a benign "already exists". |
| 123 | 129 | * |
| 124 | - * @since 0.9.6 | |
| 125 | 130 | * @internal |
| 126 | 131 | * |
| 127 | 132 | * @param string $table Fully-prefixed table name. |
| 128 | 133 | * @param string $sql The dbDelta CREATE TABLE statement for it. |
| 129 | 134 | */ |
| 130 | -function desktop_mode_games_ensure_table( $table, $sql ) { | |
| 135 | +function openstation_games_ensure_table( $table, $sql ) { | |
| 131 | 136 | global $wpdb; |
| 132 | 137 | |
| 133 | 138 | $exists = (int) $wpdb->get_var( |
| 134 | 139 | $wpdb->prepare( |
| 135 | - "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES | |
| 136 | - WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s", | |
| 140 | + 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES | |
| 141 | + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s', | |
| 137 | 142 | $table |
| 138 | 143 | ) |
| 139 | 144 | ); |
| 140 | 145 | if ( 0 < $exists ) { |
| @@ -150,33 +155,29 @@ | ||
| 150 | 155 | /** |
| 151 | 156 | * Lazy migrator — runs when the stored schema version doesn't match |
| 152 | 157 | * the constant. Idempotent: `dbDelta` itself is a no-op when the |
| 153 | 158 | * tables already match. |
| 154 | - * | |
| 155 | - * @since 0.9.6 | |
| 156 | 159 | */ |
| 157 | -function desktop_mode_games_maybe_install_schema() { | |
| 158 | - $installed = get_option( DESKTOP_MODE_GAMES_SCHEMA_OPTION, '' ); | |
| 159 | - if ( $installed === DESKTOP_MODE_GAMES_SCHEMA_VERSION ) { | |
| 160 | +function openstation_games_maybe_install_schema() { | |
| 161 | + $installed = get_option( OPENSTATION_GAMES_SCHEMA_OPTION, '' ); | |
| 162 | + if ( OPENSTATION_GAMES_SCHEMA_VERSION === $installed ) { | |
| 160 | 163 | return; |
| 161 | 164 | } |
| 162 | - desktop_mode_games_install_schema(); | |
| 165 | + openstation_games_install_schema(); | |
| 163 | 166 | } |
| 164 | -add_action( 'admin_init', 'desktop_mode_games_maybe_install_schema' ); | |
| 167 | +add_action( 'admin_init', 'openstation_games_maybe_install_schema' ); | |
| 165 | 168 | // REST + Heartbeat requests never fire `admin_init` — without these a |
| 166 | 169 | // session hitting the scores endpoint before any admin page load |
| 167 | 170 | // would query missing tables. |
| 168 | -add_action( 'rest_api_init', 'desktop_mode_games_maybe_install_schema' ); | |
| 169 | -add_action( 'init', 'desktop_mode_games_maybe_install_schema', 1 ); | |
| 170 | -register_activation_hook( DESKTOP_MODE_FILE, 'desktop_mode_games_install_schema' ); | |
| 171 | +add_action( 'rest_api_init', 'openstation_games_maybe_install_schema' ); | |
| 172 | +add_action( 'init', 'openstation_games_maybe_install_schema', 1 ); | |
| 173 | +register_activation_hook( OPENSTATION_FILE, 'openstation_games_install_schema' ); | |
| 171 | 174 | |
| 172 | 175 | /** |
| 173 | 176 | * Current epoch-ms timestamp. Centralized so the store and the |
| 174 | 177 | * Heartbeat channel stay in lock-step. |
| 175 | 178 | * |
| 176 | - * @since 0.9.6 | |
| 177 | - * | |
| 178 | 179 | * @return int |
| 179 | 180 | */ |
| 180 | -function desktop_mode_games_now_ms() { | |
| 181 | +function openstation_games_now_ms() { | |
| 181 | 182 | return (int) round( microtime( true ) * 1000 ); |
| 182 | 183 | } |