| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Games play-time store. | |
| 3 | + * OpenStation — Games play-time store. | |
| 4 | 4 | * |
| 5 | 5 | * Accumulates how long each user has spent playing each game. The |
| 6 | 6 | * framework's launcher measures active window time client-side (the |
| 7 | 7 | * clock pauses while the game window is minimized) and flushes |
| @@ -14,12 +14,12 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * Trust model matches scores (arcade honesty): increments are |
| 16 | 16 | * client-asserted, the server clamps each flush to a filterable cap |
| 17 | 17 | * so a hostile client can't mint years of play in one request, and |
| 18 | - * the `desktop_mode_game_playtime_pre_record` filter is the hook for | |
| 18 | + * the `openstation_game_playtime_pre_record` filter is the hook for | |
| 19 | 19 | * stricter policies. |
| 20 | 20 | * |
| 21 | - * @package WPDesktopMode | |
| 21 | + * @package OpenStation | |
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | defined( 'ABSPATH' ) || exit; |
| 25 | 25 | |
| @@ -24,10 +24,16 @@ | ||
| 24 | 24 | defined( 'ABSPATH' ) || exit; |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * The user-meta key holding the per-game play-time map. |
| 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. | |
| 28 | 34 | */ |
| 29 | -define( 'DESKTOP_MODE_GAMES_PLAYTIME_META', 'desktop_mode_game_playtime' ); | |
| 35 | +define( 'OPENSTATION_GAMES_PLAYTIME_META', 'desktop_mode_game_playtime' ); | |
| 30 | 36 | |
| 31 | 37 | /** |
| 32 | 38 | * The user-meta key holding the per-game DAILY play-time map: |
| 33 | 39 | * `game id => array( 'YYYY-MM-DD' => seconds )`. Backs the |
| @@ -32,13 +38,19 @@ | ||
| 32 | 38 | * The user-meta key holding the per-game DAILY play-time map: |
| 33 | 39 | * `game id => array( 'YYYY-MM-DD' => seconds )`. Backs the |
| 34 | 40 | * Steam-style "last two weeks" figure; days are bucketed in the |
| 35 | 41 | * site's timezone and pruned past a rolling window (see |
| 36 | - * `desktop_mode_games_playtime_history_days`). The lifetime totals | |
| 37 | - * in {@see DESKTOP_MODE_GAMES_PLAYTIME_META} are authoritative and | |
| 42 | + * `openstation_games_playtime_history_days`). The lifetime totals | |
| 43 | + * in {@see OPENSTATION_GAMES_PLAYTIME_META} are authoritative and | |
| 38 | 44 | * never pruned. |
| 45 | + * | |
| 46 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 47 | + * persisted or externally-visible identifier, so renaming it would | |
| 48 | + * orphan data already written by live installs (or break a live | |
| 49 | + * URL). The mismatch between this constant's name and its value is | |
| 50 | + * deliberate — it is NOT a half-finished rename. | |
| 39 | 51 | */ |
| 40 | -define( 'DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META', 'desktop_mode_game_playtime_days' ); | |
| 52 | +define( 'OPENSTATION_GAMES_PLAYTIME_DAYS_META', 'desktop_mode_game_playtime_days' ); | |
| 41 | 53 | |
| 42 | 54 | /** |
| 43 | 55 | * Today's daily-bucket key (`YYYY-MM-DD`, site timezone). |
| 44 | 56 | * |
| @@ -43,9 +55,9 @@ | ||
| 43 | 55 | * Today's daily-bucket key (`YYYY-MM-DD`, site timezone). |
| 44 | 56 | * |
| 45 | 57 | * @return string |
| 46 | 58 | */ |
| 47 | -function desktop_mode_games_playtime_today_key() { | |
| 59 | +function openstation_games_playtime_today_key() { | |
| 48 | 60 | return current_datetime()->format( 'Y-m-d' ); |
| 49 | 61 | } |
| 50 | 62 | |
| 51 | 63 | /** |
| @@ -55,10 +67,10 @@ | ||
| 55 | 67 | * @param string $game Optional game id. Empty returns the full map. |
| 56 | 68 | * @return int|array<string,int> Seconds for one game, or the whole |
| 57 | 69 | * `game id => seconds` map. |
| 58 | 70 | */ |
| 59 | -function desktop_mode_games_get_playtime( $user_id, $game = '' ) { | |
| 60 | - $map = get_user_meta( (int) $user_id, DESKTOP_MODE_GAMES_PLAYTIME_META, true ); | |
| 71 | +function openstation_games_get_playtime( $user_id, $game = '' ) { | |
| 72 | + $map = get_user_meta( (int) $user_id, OPENSTATION_GAMES_PLAYTIME_META, true ); | |
| 61 | 73 | if ( ! is_array( $map ) ) { |
| 62 | 74 | $map = array(); |
| 63 | 75 | } |
| 64 | 76 | $clean = array(); |
| @@ -83,10 +95,10 @@ | ||
| 83 | 95 | * @param string $game Optional game id. Empty returns the full map. |
| 84 | 96 | * @return array Day buckets (`'YYYY-MM-DD' => seconds`) for one game, |
| 85 | 97 | * or the whole `game id => buckets` map. |
| 86 | 98 | */ |
| 87 | -function desktop_mode_games_get_playtime_daily( $user_id, $game = '' ) { | |
| 88 | - $map = get_user_meta( (int) $user_id, DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META, true ); | |
| 99 | +function openstation_games_get_playtime_daily( $user_id, $game = '' ) { | |
| 100 | + $map = get_user_meta( (int) $user_id, OPENSTATION_GAMES_PLAYTIME_DAYS_META, true ); | |
| 89 | 101 | if ( ! is_array( $map ) ) { |
| 90 | 102 | $map = array(); |
| 91 | 103 | } |
| 92 | 104 | $clean = array(); |
| @@ -116,19 +128,19 @@ | ||
| 116 | 128 | * |
| 117 | 129 | * @param string $game Registered game id. |
| 118 | 130 | * @param int $user_id Player. |
| 119 | 131 | * @param int $seconds Seconds to add. Clamped to |
| 120 | - * `[1, desktop_mode_games_playtime_max_increment]`. | |
| 132 | + * `[1, openstation_games_playtime_max_increment]`. | |
| 121 | 133 | * @return int|WP_Error The new total for the game on success. |
| 122 | 134 | */ |
| 123 | -function desktop_mode_games_add_playtime( $game, $user_id, $seconds ) { | |
| 135 | +function openstation_games_add_playtime( $game, $user_id, $seconds ) { | |
| 124 | 136 | $game = sanitize_key( (string) $game ); |
| 125 | 137 | $user_id = (int) $user_id; |
| 126 | 138 | $seconds = (int) $seconds; |
| 127 | 139 | |
| 128 | - if ( ! desktop_mode_games_is_registered( $game ) ) { | |
| 140 | + if ( ! openstation_games_is_registered( $game ) ) { | |
| 129 | 141 | return new WP_Error( |
| 130 | - 'desktop_mode_unknown_game', | |
| 142 | + 'openstation_unknown_game', | |
| 131 | 143 | __( 'Unknown game.', 'desktop-mode' ), |
| 132 | 144 | array( 'status' => 404 ) |
| 133 | 145 | ); |
| 134 | 146 | } |
| @@ -133,9 +145,9 @@ | ||
| 133 | 145 | ); |
| 134 | 146 | } |
| 135 | 147 | if ( $user_id <= 0 ) { |
| 136 | 148 | return new WP_Error( |
| 137 | - 'desktop_mode_invalid_user', | |
| 149 | + 'openstation_invalid_user', | |
| 138 | 150 | __( 'A valid user is required to record play time.', 'desktop-mode' ), |
| 139 | 151 | array( 'status' => 400 ) |
| 140 | 152 | ); |
| 141 | 153 | } |
| @@ -140,9 +152,9 @@ | ||
| 140 | 152 | ); |
| 141 | 153 | } |
| 142 | 154 | if ( $seconds < 1 ) { |
| 143 | 155 | return new WP_Error( |
| 144 | - 'desktop_mode_invalid_playtime', | |
| 156 | + 'openstation_invalid_playtime', | |
| 145 | 157 | __( 'Play time must be a positive number of seconds.', 'desktop-mode' ), |
| 146 | 158 | array( 'status' => 400 ) |
| 147 | 159 | ); |
| 148 | 160 | } |
| @@ -156,9 +168,9 @@ | ||
| 156 | 168 | * @param int $max_seconds Default 900 (15 minutes). |
| 157 | 169 | * @param string $game Game id. |
| 158 | 170 | * @param int $user_id Player. |
| 159 | 171 | */ |
| 160 | - $max = max( 1, (int) apply_filters( 'desktop_mode_games_playtime_max_increment', 900, $game, $user_id ) ); | |
| 172 | + $max = max( 1, (int) apply_filters( 'openstation_games_playtime_max_increment', 900, $game, $user_id ) ); | |
| 161 | 173 | $seconds = min( $seconds, $max ); |
| 162 | 174 | |
| 163 | 175 | /** |
| 164 | 176 | * Short-circuit / veto filter for play-time recording. Return a |
| @@ -169,21 +181,21 @@ | ||
| 169 | 181 | * @param string $game Game id. |
| 170 | 182 | * @param int $user_id Player. |
| 171 | 183 | * @param int $seconds Clamped increment. |
| 172 | 184 | */ |
| 173 | - $pre = apply_filters( 'desktop_mode_game_playtime_pre_record', null, $game, $user_id, $seconds ); | |
| 185 | + $pre = apply_filters( 'openstation_game_playtime_pre_record', null, $game, $user_id, $seconds ); | |
| 174 | 186 | if ( is_wp_error( $pre ) ) { |
| 175 | 187 | return $pre; |
| 176 | 188 | } |
| 177 | 189 | |
| 178 | - $map = desktop_mode_games_get_playtime( $user_id ); | |
| 190 | + $map = openstation_games_get_playtime( $user_id ); | |
| 179 | 191 | $map[ $game ] = ( isset( $map[ $game ] ) ? $map[ $game ] : 0 ) + $seconds; |
| 180 | - update_user_meta( $user_id, DESKTOP_MODE_GAMES_PLAYTIME_META, $map ); | |
| 192 | + update_user_meta( $user_id, OPENSTATION_GAMES_PLAYTIME_META, $map ); | |
| 181 | 193 | |
| 182 | 194 | // Daily bucket (site timezone) for the recent-activity figure, |
| 183 | 195 | // pruned to a rolling window so the meta row stays bounded. The |
| 184 | 196 | // lifetime total above is the source of truth and never shrinks. |
| 185 | - $today = desktop_mode_games_playtime_today_key(); | |
| 197 | + $today = openstation_games_playtime_today_key(); | |
| 186 | 198 | |
| 187 | 199 | /** |
| 188 | 200 | * Filter how many days of daily play-time buckets are retained. |
| 189 | 201 | * The Games hub needs 14 for its "last two weeks" figure. |
| @@ -189,12 +201,12 @@ | ||
| 189 | 201 | * The Games hub needs 14 for its "last two weeks" figure. |
| 190 | 202 | * |
| 191 | 203 | * @param int $days Default 30. |
| 192 | 204 | */ |
| 193 | - $window = max( 1, (int) apply_filters( 'desktop_mode_games_playtime_history_days', 30 ) ); | |
| 205 | + $window = max( 1, (int) apply_filters( 'openstation_games_playtime_history_days', 30 ) ); | |
| 194 | 206 | $cutoff = current_datetime()->modify( '-' . ( $window - 1 ) . ' days' )->format( 'Y-m-d' ); |
| 195 | 207 | |
| 196 | - $daily = desktop_mode_games_get_playtime_daily( $user_id ); | |
| 208 | + $daily = openstation_games_get_playtime_daily( $user_id ); | |
| 197 | 209 | $days = isset( $daily[ $game ] ) ? $daily[ $game ] : array(); |
| 198 | 210 | |
| 199 | 211 | $days[ $today ] = ( isset( $days[ $today ] ) ? $days[ $today ] : 0 ) + $seconds; |
| 200 | 212 | foreach ( array_keys( $days ) as $day ) { |
| @@ -203,9 +215,9 @@ | ||
| 203 | 215 | unset( $days[ $day ] ); |
| 204 | 216 | } |
| 205 | 217 | } |
| 206 | 218 | $daily[ $game ] = $days; |
| 207 | - update_user_meta( $user_id, DESKTOP_MODE_GAMES_PLAYTIME_DAYS_META, $daily ); | |
| 219 | + update_user_meta( $user_id, OPENSTATION_GAMES_PLAYTIME_DAYS_META, $daily ); | |
| 208 | 220 | |
| 209 | 221 | /** |
| 210 | 222 | * Fires after a play-time increment is recorded. |
| 211 | 223 | * |
| @@ -213,8 +225,8 @@ | ||
| 213 | 225 | * @param int $user_id Player. |
| 214 | 226 | * @param int $seconds The recorded increment. |
| 215 | 227 | * @param int $total The user's new total for the game. |
| 216 | 228 | */ |
| 217 | - do_action( 'desktop_mode_game_playtime_recorded', $game, $user_id, $seconds, $map[ $game ] ); | |
| 229 | + do_action( 'openstation_game_playtime_recorded', $game, $user_id, $seconds, $map[ $game ] ); | |
| 218 | 230 | |
| 219 | 231 | return $map[ $game ]; |
| 220 | 232 | } |