PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
← All changes | includes/games/store.php +55 -55 0.9.81.1.10 View file →
@@ -1,13 +1,13 @@
1 1 <?php
2 2 /**
3 - * Desktop Mode — Games store.
3 + * OpenStation — Games store.
4 4 *
5 5 * CRUD for the two games tables. Scores are client-asserted (arcade
6 6 * trust model): the server clamps and sanitizes what it can — the
7 7 * game must be server-registered, the score is a non-negative int,
8 8 * the meta blob is a bounded flat scalar map — and exposes the
9 - * `desktop_mode_game_score_pre_save` filter for plugins that want
9 + * `openstation_game_score_pre_save` filter for plugins that want
10 10 * stricter validation.
11 11 *
12 12 * The challenge state machine is enforced HERE, not in REST:
13 13 * `pending → accepted | declined`, `accepted → completed`. Every
@@ -12,9 +12,9 @@
12 12 * The challenge state machine is enforced HERE, not in REST:
13 13 * `pending → accepted | declined`, `accepted → completed`. Every
14 14 * mutation bumps `updated_at_ms`, the Heartbeat high-water mark.
15 15 *
16 - * @package WPDesktopMode
16 + * @package OpenStation
17 17 */
18 18
19 19 defined( 'ABSPATH' ) || exit;
20 20
@@ -25,9 +25,9 @@
25 25 *
26 26 * @param mixed $meta Raw caller input.
27 27 * @return array Sanitized flat map.
28 28 */
29 -function desktop_mode_games_sanitize_score_meta( $meta ) {
29 +function openstation_games_sanitize_score_meta( $meta ) {
30 30 if ( ! is_array( $meta ) ) {
31 31 return array();
32 32 }
33 33 $out = array();
@@ -60,19 +60,19 @@
60 60 * @param array $meta Flexible per-game fields (see the game's
61 61 * `score_columns`).
62 62 * @return int|WP_Error Row id on success.
63 63 */
64 -function desktop_mode_games_save_score( $game, $user_id, $score, $meta = array() ) {
64 +function openstation_games_save_score( $game, $user_id, $score, $meta = array() ) {
65 65 global $wpdb;
66 66
67 67 $game = sanitize_key( (string) $game );
68 68 $user_id = (int) $user_id;
69 69 $score = max( 0, (int) $score );
70 - $meta = desktop_mode_games_sanitize_score_meta( $meta );
70 + $meta = openstation_games_sanitize_score_meta( $meta );
71 71
72 - if ( ! desktop_mode_games_is_registered( $game ) ) {
72 + if ( ! openstation_games_is_registered( $game ) ) {
73 73 return new WP_Error(
74 - 'desktop_mode_unknown_game',
74 + 'openstation_unknown_game',
75 75 __( 'Unknown game.', 'desktop-mode' ),
76 76 array( 'status' => 404 )
77 77 );
78 78 }
@@ -77,9 +77,9 @@
77 77 );
78 78 }
79 79 if ( $user_id <= 0 ) {
80 80 return new WP_Error(
81 - 'desktop_mode_invalid_user',
81 + 'openstation_invalid_user',
82 82 __( 'A valid user is required to save a score.', 'desktop-mode' ),
83 83 array( 'status' => 400 )
84 84 );
85 85 }
@@ -95,14 +95,14 @@
95 95 * @param int $user_id Player.
96 96 * @param int $score Clamped score.
97 97 * @param array $meta Sanitized meta map.
98 98 */
99 - $pre = apply_filters( 'desktop_mode_game_score_pre_save', null, $game, $user_id, $score, $meta );
99 + $pre = apply_filters( 'openstation_game_score_pre_save', null, $game, $user_id, $score, $meta );
100 100 if ( is_wp_error( $pre ) ) {
101 101 return $pre;
102 102 }
103 103
104 - $tables = desktop_mode_games_table_names();
104 + $tables = openstation_games_table_names();
105 105 $ok = $wpdb->insert(
106 106 $tables['scores'],
107 107 array(
108 108 'game' => $game,
@@ -108,15 +108,15 @@
108 108 'game' => $game,
109 109 'user_id' => $user_id,
110 110 'score' => $score,
111 111 'meta' => wp_json_encode( $meta ),
112 - 'created_at_ms' => desktop_mode_games_now_ms(),
112 + 'created_at_ms' => openstation_games_now_ms(),
113 113 ),
114 114 array( '%s', '%d', '%d', '%s', '%d' )
115 115 );
116 116 if ( false === $ok ) {
117 117 return new WP_Error(
118 - 'desktop_mode_score_save_failed',
118 + 'openstation_score_save_failed',
119 119 __( 'Could not save the score.', 'desktop-mode' ),
120 120 array( 'status' => 500 )
121 121 );
122 122 }
@@ -130,9 +130,9 @@
130 130 * @param int $user_id Player.
131 131 * @param int $score Saved score.
132 132 * @param array $meta Saved meta map.
133 133 */
134 - do_action( 'desktop_mode_game_score_saved', $id, $game, $user_id, $score, $meta );
134 + do_action( 'openstation_game_score_saved', $id, $game, $user_id, $score, $meta );
135 135
136 136 return $id;
137 137 }
138 138
@@ -148,9 +148,9 @@
148 148 * @type int $user_id Restrict to one player. Default 0 (all).
149 149 * }
150 150 * @return array{ rows: array[], total: int }
151 151 */
152 -function desktop_mode_games_get_scores( $game, $args = array() ) {
152 +function openstation_games_get_scores( $game, $args = array() ) {
153 153 global $wpdb;
154 154
155 155 $game = sanitize_key( (string) $game );
156 156 $page = max( 1, (int) ( $args['page'] ?? 1 ) );
@@ -158,9 +158,9 @@
158 158 $orderby = ( 'created' === ( $args['orderby'] ?? '' ) ) ? 'created_at_ms' : 'score';
159 159 $order = ( 'asc' === strtolower( (string) ( $args['order'] ?? 'desc' ) ) ) ? 'ASC' : 'DESC';
160 160 $user_id = (int) ( $args['user_id'] ?? 0 );
161 161
162 - $tables = desktop_mode_games_table_names();
162 + $tables = openstation_games_table_names();
163 163 $where = 'game = %s';
164 164 $params = array( $game );
165 165 if ( $user_id > 0 ) {
166 166 $where .= ' AND user_id = %d';
@@ -188,9 +188,9 @@
188 188 ARRAY_A
189 189 );
190 190
191 191 return array(
192 - 'rows' => array_map( 'desktop_mode_games_shape_score', (array) $rows ),
192 + 'rows' => array_map( 'openstation_games_shape_score', (array) $rows ),
193 193 'total' => $total,
194 194 );
195 195 }
196 196
@@ -200,9 +200,9 @@
200 200 *
201 201 * @param array $row Raw table row.
202 202 * @return array
203 203 */
204 -function desktop_mode_games_shape_score( $row ) {
204 +function openstation_games_shape_score( $row ) {
205 205 $user_id = (int) $row['user_id'];
206 206 $user = get_userdata( $user_id );
207 207 $meta = json_decode( (string) ( $row['meta'] ?? '' ), true );
208 208 return array(
@@ -226,9 +226,9 @@
226 226 * @param int $score_to_beat The challenger's score.
227 227 * @param array $score_meta The challenger's score meta map.
228 228 * @return int|WP_Error Challenge id on success.
229 229 */
230 -function desktop_mode_games_create_challenge( $game, $challenger_id, $recipient_id, $score_to_beat, $score_meta = array() ) {
230 +function openstation_games_create_challenge( $game, $challenger_id, $recipient_id, $score_to_beat, $score_meta = array() ) {
231 231 global $wpdb;
232 232
233 233 $game = sanitize_key( (string) $game );
234 234 $challenger_id = (int) $challenger_id;
@@ -233,11 +233,11 @@
233 233 $game = sanitize_key( (string) $game );
234 234 $challenger_id = (int) $challenger_id;
235 235 $recipient_id = (int) $recipient_id;
236 236
237 - if ( ! desktop_mode_games_is_registered( $game ) ) {
237 + if ( ! openstation_games_is_registered( $game ) ) {
238 238 return new WP_Error(
239 - 'desktop_mode_unknown_game',
239 + 'openstation_unknown_game',
240 240 __( 'Unknown game.', 'desktop-mode' ),
241 241 array( 'status' => 404 )
242 242 );
243 243 }
@@ -242,9 +242,9 @@
242 242 );
243 243 }
244 244 if ( $recipient_id <= 0 || ! get_userdata( $recipient_id ) ) {
245 245 return new WP_Error(
246 - 'desktop_mode_invalid_recipient',
246 + 'openstation_invalid_recipient',
247 247 __( 'The challenged user does not exist.', 'desktop-mode' ),
248 248 array( 'status' => 400 )
249 249 );
250 250 }
@@ -249,16 +249,16 @@
249 249 );
250 250 }
251 251 if ( $recipient_id === $challenger_id ) {
252 252 return new WP_Error(
253 - 'desktop_mode_self_challenge',
253 + 'openstation_self_challenge',
254 254 __( 'You cannot challenge yourself.', 'desktop-mode' ),
255 255 array( 'status' => 400 )
256 256 );
257 257 }
258 258
259 - $now = desktop_mode_games_now_ms();
260 - $tables = desktop_mode_games_table_names();
259 + $now = openstation_games_now_ms();
260 + $tables = openstation_games_table_names();
261 261 $ok = $wpdb->insert(
262 262 $tables['challenges'],
263 263 array(
264 264 'game' => $game,
@@ -264,9 +264,9 @@
264 264 'game' => $game,
265 265 'challenger_id' => $challenger_id,
266 266 'recipient_id' => $recipient_id,
267 267 'score_to_beat' => max( 0, (int) $score_to_beat ),
268 - 'score_meta' => wp_json_encode( desktop_mode_games_sanitize_score_meta( $score_meta ) ),
268 + 'score_meta' => wp_json_encode( openstation_games_sanitize_score_meta( $score_meta ) ),
269 269 'state' => 'pending',
270 270 'created_at_ms' => $now,
271 271 'updated_at_ms' => $now,
272 272 ),
@@ -273,9 +273,9 @@
273 273 array( '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%d' )
274 274 );
275 275 if ( false === $ok ) {
276 276 return new WP_Error(
277 - 'desktop_mode_challenge_create_failed',
277 + 'openstation_challenge_create_failed',
278 278 __( 'Could not create the challenge.', 'desktop-mode' ),
279 279 array( 'status' => 500 )
280 280 );
281 281 }
@@ -286,9 +286,9 @@
286 286 *
287 287 * @param int $id Challenge id.
288 288 * @param array $row The challenge row.
289 289 */
290 - do_action( 'desktop_mode_game_challenge_created', $id, desktop_mode_games_get_challenge( $id ) );
290 + do_action( 'openstation_game_challenge_created', $id, openstation_games_get_challenge( $id ) );
291 291
292 292 return $id;
293 293 }
294 294
@@ -297,11 +297,11 @@
297 297 *
298 298 * @param int $id Challenge id.
299 299 * @return array|null Raw table row.
300 300 */
301 -function desktop_mode_games_get_challenge( $id ) {
301 +function openstation_games_get_challenge( $id ) {
302 302 global $wpdb;
303 - $tables = desktop_mode_games_table_names();
303 + $tables = openstation_games_table_names();
304 304 $row = $wpdb->get_row(
305 305 $wpdb->prepare( "SELECT * FROM {$tables['challenges']} WHERE id = %d", (int) $id ),
306 306 ARRAY_A
307 307 );
@@ -315,22 +315,22 @@
315 315 * @param int $id Challenge id.
316 316 * @param string $state 'accepted' | 'declined'.
317 317 * @return true|WP_Error
318 318 */
319 -function desktop_mode_games_set_challenge_state( $id, $state ) {
319 +function openstation_games_set_challenge_state( $id, $state ) {
320 320 global $wpdb;
321 321
322 322 if ( ! in_array( $state, array( 'accepted', 'declined' ), true ) ) {
323 323 return new WP_Error(
324 - 'desktop_mode_invalid_challenge_state',
324 + 'openstation_invalid_challenge_state',
325 325 __( 'Invalid challenge state.', 'desktop-mode' ),
326 326 array( 'status' => 400 )
327 327 );
328 328 }
329 - $row = desktop_mode_games_get_challenge( $id );
329 + $row = openstation_games_get_challenge( $id );
330 330 if ( ! $row ) {
331 331 return new WP_Error(
332 - 'desktop_mode_challenge_not_found',
332 + 'openstation_challenge_not_found',
333 333 __( 'Challenge not found.', 'desktop-mode' ),
334 334 array( 'status' => 404 )
335 335 );
336 336 }
@@ -335,9 +335,9 @@
335 335 );
336 336 }
337 337 if ( 'pending' !== $row['state'] ) {
338 338 return new WP_Error(
339 - 'desktop_mode_challenge_state_conflict',
339 + 'openstation_challenge_state_conflict',
340 340 __( 'This challenge has already been decided.', 'desktop-mode' ),
341 341 array( 'status' => 409 )
342 342 );
343 343 }
@@ -344,10 +344,10 @@
344 344
345 345 // Monotonic bump: a transition landing in the same millisecond as
346 346 // the previous write must still move `updated_at_ms` forward, or
347 347 // version-gated Heartbeat clients would never see the change.
348 - $now = max( desktop_mode_games_now_ms(), (int) $row['updated_at_ms'] + 1 );
349 - $tables = desktop_mode_games_table_names();
348 + $now = max( openstation_games_now_ms(), (int) $row['updated_at_ms'] + 1 );
349 + $tables = openstation_games_table_names();
350 350 $wpdb->update(
351 351 $tables['challenges'],
352 352 array(
353 353 'state' => $state,
@@ -365,9 +365,9 @@
365 365 *
366 366 * @param int $id Challenge id.
367 367 * @param array $row The (pre-transition) challenge row.
368 368 */
369 - do_action( 'desktop_mode_game_challenge_accepted', (int) $id, $row );
369 + do_action( 'openstation_game_challenge_accepted', (int) $id, $row );
370 370 } else {
371 371 /**
372 372 * Fires after a challenge is declined by its recipient.
373 373 *
@@ -373,9 +373,9 @@
373 373 *
374 374 * @param int $id Challenge id.
375 375 * @param array $row The (pre-transition) challenge row.
376 376 */
377 - do_action( 'desktop_mode_game_challenge_declined', (int) $id, $row );
377 + do_action( 'openstation_game_challenge_declined', (int) $id, $row );
378 378 }
379 379
380 380 return true;
381 381 }
@@ -388,15 +388,15 @@
388 388 * @param int $score The recipient's score.
389 389 * @param array $meta The recipient's score meta map.
390 390 * @return array|WP_Error The updated challenge row.
391 391 */
392 -function desktop_mode_games_complete_challenge( $id, $score, $meta = array() ) {
392 +function openstation_games_complete_challenge( $id, $score, $meta = array() ) {
393 393 global $wpdb;
394 394
395 - $row = desktop_mode_games_get_challenge( $id );
395 + $row = openstation_games_get_challenge( $id );
396 396 if ( ! $row ) {
397 397 return new WP_Error(
398 - 'desktop_mode_challenge_not_found',
398 + 'openstation_challenge_not_found',
399 399 __( 'Challenge not found.', 'desktop-mode' ),
400 400 array( 'status' => 404 )
401 401 );
402 402 }
@@ -401,9 +401,9 @@
401 401 );
402 402 }
403 403 if ( 'accepted' !== $row['state'] ) {
404 404 return new WP_Error(
405 - 'desktop_mode_challenge_state_conflict',
405 + 'openstation_challenge_state_conflict',
406 406 __( 'Only an accepted challenge can be completed.', 'desktop-mode' ),
407 407 array( 'status' => 409 )
408 408 );
409 409 }
@@ -408,22 +408,22 @@
408 408 );
409 409 }
410 410
411 411 $score = max( 0, (int) $score );
412 - $meta = desktop_mode_games_sanitize_score_meta( $meta );
412 + $meta = openstation_games_sanitize_score_meta( $meta );
413 413 $result = $score > (int) $row['score_to_beat'] ? 'beaten' : 'not_beaten';
414 414
415 415 // The run also lands on the leaderboard — a challenge game is a
416 416 // real game. A veto from the pre-save filter aborts the whole
417 417 // completion so the two writes can't diverge.
418 - $score_id = desktop_mode_games_save_score( $row['game'], (int) $row['recipient_id'], $score, $meta );
418 + $score_id = openstation_games_save_score( $row['game'], (int) $row['recipient_id'], $score, $meta );
419 419 if ( is_wp_error( $score_id ) ) {
420 420 return $score_id;
421 421 }
422 422
423 423 // Same monotonic-bump rule as `set_challenge_state()` — see there.
424 - $now = max( desktop_mode_games_now_ms(), (int) $row['updated_at_ms'] + 1 );
425 - $tables = desktop_mode_games_table_names();
424 + $now = max( openstation_games_now_ms(), (int) $row['updated_at_ms'] + 1 );
425 + $tables = openstation_games_table_names();
426 426 $wpdb->update(
427 427 $tables['challenges'],
428 428 array(
429 429 'state' => 'completed',
@@ -437,9 +437,9 @@
437 437 array( '%s', '%s', '%d', '%s', '%d', '%d' ),
438 438 array( '%d' )
439 439 );
440 440
441 - $updated = desktop_mode_games_get_challenge( $id );
441 + $updated = openstation_games_get_challenge( $id );
442 442
443 443 /**
444 444 * Fires after a challenge run is completed.
445 445 *
@@ -446,9 +446,9 @@
446 446 * @param int $id Challenge id.
447 447 * @param string $result 'beaten' | 'not_beaten'.
448 448 * @param array $row The updated challenge row.
449 449 */
450 - do_action( 'desktop_mode_game_challenge_completed', (int) $id, $result, $updated );
450 + do_action( 'openstation_game_challenge_completed', (int) $id, $result, $updated );
451 451
452 452 return $updated;
453 453 }
454 454
@@ -461,11 +461,11 @@
461 461 * @param int $since_ms Last-seen `updated_at_ms`. 0 = everything.
462 462 * @param int $cap Row cap.
463 463 * @return array[] Raw rows, oldest change first.
464 464 */
465 -function desktop_mode_games_get_challenges_for_user( $user_id, $since_ms = 0, $cap = 50 ) {
465 +function openstation_games_get_challenges_for_user( $user_id, $since_ms = 0, $cap = 50 ) {
466 466 global $wpdb;
467 - $tables = desktop_mode_games_table_names();
467 + $tables = openstation_games_table_names();
468 468 $rows = $wpdb->get_results(
469 469 $wpdb->prepare(
470 470 "SELECT * FROM {$tables['challenges']}
471 471 WHERE ( challenger_id = %d OR recipient_id = %d )
@@ -488,12 +488,12 @@
488 488 *
489 489 * @param array $row Raw table row.
490 490 * @return array
491 491 */
492 -function desktop_mode_games_shape_challenge( $row ) {
493 - $challenger = get_userdata( (int) $row['challenger_id'] );
494 - $recipient = get_userdata( (int) $row['recipient_id'] );
495 - $score_meta = json_decode( (string) ( $row['score_meta'] ?? '' ), true );
492 +function openstation_games_shape_challenge( $row ) {
493 + $challenger = get_userdata( (int) $row['challenger_id'] );
494 + $recipient = get_userdata( (int) $row['recipient_id'] );
495 + $score_meta = json_decode( (string) ( $row['score_meta'] ?? '' ), true );
496 496 $result_meta = json_decode( (string) ( $row['result_meta'] ?? '' ), true );
497 497 return array(
498 498 'id' => (int) $row['id'],
499 499 'game' => (string) $row['game'],