PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.6
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.6
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
desktop-mode / includes / games / heartbeat.php

heartbeat.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.6, at includes/games/heartbeat.php

88 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — Games Heartbeat sync (PHP).
4 *
5 * Piggybacks on the WordPress Heartbeat tick — the same channel
6 * presence and folder sharing use — to deliver challenge activity
7 * to connected users live: a new pending challenge for the
8 * recipient, a completed run for the challenger.
9 *
10 * Wire format. Client sends:
11 *
12 * {
13 * desktop_mode_games_subscribe: {
14 * challengesVersion: lastSeenUpdatedAtMs
15 * }
16 * }
17 *
18 * Server responds:
19 *
20 * desktop_mode_games: {
21 * challenges: [ <ChallengeShape> ], // rows involving me,
22 * // updated_at_ms > version
23 * serverTimeMs: int,
24 * truncated: bool
25 * }
26 *
27 * Version-gated: a quiet tick carries no rows. Truncation kicks in
28 * past `desktop_mode_games_heartbeat_max_rows` (default 50) — the
29 * client falls back to `GET /games/challenges` for a full resync.
30 *
31 * @package WPDesktopMode
32 * @since 0.9.6
33 */
34
35 defined( 'ABSPATH' ) || exit;
36
37 /**
38 * @since 0.9.6
39 *
40 * @param array $response Pre-filtered response.
41 * @param array $data Client-sent payload.
42 * @return array
43 */
44 function desktop_mode_games_heartbeat_received( $response, $data ) {
45 if ( ! is_array( $response ) ) {
46 $response = array();
47 }
48 if ( empty( $data['desktop_mode_games_subscribe'] ) || ! is_array( $data['desktop_mode_games_subscribe'] ) ) {
49 return $response;
50 }
51 if ( ! function_exists( 'desktop_mode_is_enabled' ) || ! desktop_mode_is_enabled() ) {
52 return $response;
53 }
54
55 $user_id = (int) get_current_user_id();
56 if ( $user_id <= 0 ) {
57 return $response;
58 }
59
60 $sub = $data['desktop_mode_games_subscribe'];
61 $version = isset( $sub['challengesVersion'] ) ? (int) $sub['challengesVersion'] : 0;
62
63 /**
64 * Filter the per-tick challenge row cap. Past the cap the
65 * payload is flagged `truncated` and the client resyncs over
66 * REST.
67 *
68 * @since 0.9.6
69 *
70 * @param int $cap Default 50.
71 */
72 $cap = max( 1, (int) apply_filters( 'desktop_mode_games_heartbeat_max_rows', 50 ) );
73
74 $rows = desktop_mode_games_get_challenges_for_user( $user_id, $version, $cap + 1 );
75 $truncated = count( $rows ) > $cap;
76 if ( $truncated ) {
77 $rows = array_slice( $rows, 0, $cap );
78 }
79
80 $response['desktop_mode_games'] = array(
81 'challenges' => array_map( 'desktop_mode_games_shape_challenge', $rows ),
82 'serverTimeMs' => desktop_mode_games_now_ms(),
83 'truncated' => $truncated,
84 );
85 return $response;
86 }
87 add_filter( 'heartbeat_received', 'desktop_mode_games_heartbeat_received', 5, 2 );
88