PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.7
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 / window.php

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

186 lines 5.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: hub window + desktop icon registration.
4 *
5 * Native window with id `desktop-mode-games` — the "Games folder"
6 * fixture on the wallpaper. The template body is a static skeleton
7 * (a game grid + a detail panel, Steam-library style) that the JS
8 * bundle enhances on first open: the grid paints from the games
9 * registry; selecting a game reveals its description, Play +
10 * Challenge actions, its scoreboard, and its challenges below.
11 *
12 * Both registrations are filterable via
13 * `desktop_mode_games_window_args` / `desktop_mode_games_icon_args`,
14 * mirroring the Recycle Bin.
15 *
16 * @package WPDesktopMode
17 * @since 0.9.6
18 */
19
20 defined( 'ABSPATH' ) || exit;
21
22 /**
23 * The shared gamepad SVG used by the window icon and the desktop
24 * icon.
25 *
26 * @since 0.9.6
27 *
28 * @return string Raw `<svg>` markup.
29 */
30 function desktop_mode_games_icon_svg() {
31 return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">'
32 . '<path fill="#6c5ce7" d="M18 16h28a16 16 0 0 1 16 16v10a10 10 0 0 1-18.3 5.6L39.9 42H24.1l-3.8 5.6A10 10 0 0 1 2 42V32a16 16 0 0 1 16-16z"/>'
33 . '<rect x="12" y="28" width="14" height="5" rx="2.5" fill="#ffffff"/>'
34 . '<rect x="16.5" y="23.5" width="5" height="14" rx="2.5" fill="#ffffff"/>'
35 . '<circle cx="43" cy="27" r="3.4" fill="#ffd166"/>'
36 . '<circle cx="50" cy="34" r="3.4" fill="#06d6a0"/>'
37 . '</svg>';
38 }
39
40 /**
41 * Whether the current user can use desktop games.
42 *
43 * @since 0.9.6
44 *
45 * @return bool
46 */
47 function desktop_mode_games_user_can_use() {
48 $can = is_user_logged_in() && current_user_can( 'read' );
49
50 /**
51 * Filter whether the current user can see the Games window,
52 * icon, and play games.
53 *
54 * @since 0.9.6
55 *
56 * @param bool $can Default: logged-in + `read` capability.
57 */
58 return (bool) apply_filters( 'desktop_mode_games_user_can_use', $can );
59 }
60
61 /**
62 * Echoes the Games window's template body.
63 *
64 * The `data-desktop-mode-games-*` hooks are the contract the JS
65 * render callback relies on — keep them intact (or rename via the
66 * filter) when customizing the layout.
67 *
68 * @since 0.9.6
69 */
70 function desktop_mode_games_render_template() {
71 ob_start();
72 ?>
73 <div class="desktop-mode-games" data-desktop-mode-games-root>
74 <div class="desktop-mode-games__library">
75 <div class="desktop-mode-games__grid" data-desktop-mode-games-grid role="listbox" aria-label="<?php esc_attr_e( 'Games', 'desktop-mode' ); ?>"></div>
76 <div class="desktop-mode-games__detail" data-desktop-mode-games-detail hidden></div>
77 </div>
78 </div>
79 <?php
80 $html = (string) ob_get_clean();
81
82 /**
83 * Filter the Games window's template HTML.
84 *
85 * Keep the `data-desktop-mode-games-*` hooks intact so the JS
86 * render callback can find its mount points.
87 *
88 * @since 0.9.6
89 *
90 * @param string $html Default template HTML.
91 */
92 $filtered = (string) apply_filters( 'desktop_mode_games_template_html', $html );
93 echo wp_kses( $filtered, desktop_mode_native_window_allowed_html() );
94 }
95
96 /**
97 * Register the Games window + desktop icon on `init`.
98 *
99 * Priority 20, after the native-window registry bootstraps — same
100 * timing as the Recycle Bin.
101 *
102 * @since 0.9.6
103 */
104 function desktop_mode_games_register_window() {
105 if ( ! desktop_mode_games_user_can_use() ) {
106 return;
107 }
108
109 $icon_uri = 'data:image/svg+xml;base64,' . base64_encode( desktop_mode_games_icon_svg() );
110
111 $window_args = array(
112 'title' => __( 'Games', 'desktop-mode' ),
113 'icon' => $icon_uri,
114 'template' => 'desktop_mode_games_render_template',
115 'script' => 'desktop-mode-games',
116 'width' => 900,
117 'height' => 600,
118 'min_width' => 560,
119 'min_height' => 400,
120 'placement' => 'dock',
121 );
122
123 /**
124 * Filter the args used to register the Games native window.
125 *
126 * @since 0.9.6
127 *
128 * @param array $window_args Args passed to `desktop_mode_register_window()`.
129 */
130 $window_args = (array) apply_filters( 'desktop_mode_games_window_args', $window_args );
131
132 $registered = desktop_mode_register_window( 'desktop-mode-games', $window_args );
133 if ( is_wp_error( $registered ) ) {
134 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
135 error_log( '[desktop-mode] Games window registration failed: ' . $registered->get_error_message() );
136 return;
137 }
138
139 $icon_args = array(
140 'title' => __( 'Games', 'desktop-mode' ),
141 'icon_svg' => desktop_mode_games_icon_svg(),
142 'window' => 'desktop-mode-games',
143 'position' => 85,
144 );
145
146 /**
147 * Filter the args used to register the Games desktop icon.
148 *
149 * @since 0.9.6
150 *
151 * @param array $icon_args Args passed to `desktop_mode_register_icon()`.
152 */
153 $icon_args = (array) apply_filters( 'desktop_mode_games_icon_args', $icon_args );
154
155 desktop_mode_register_icon( 'desktop-mode-games', $icon_args );
156 }
157 add_action( 'init', 'desktop_mode_games_register_window', 20 );
158
159 /**
160 * Localize REST endpoints for the JS bundle.
161 *
162 * The bundle reads its config off `window.desktopModeGamesConfig`
163 * and never hardcodes URLs.
164 *
165 * @since 0.9.6
166 */
167 function desktop_mode_games_localize_config() {
168 if ( ! desktop_mode_games_user_can_use() ) {
169 return;
170 }
171
172 wp_localize_script(
173 'desktop-mode-games',
174 'desktopModeGamesConfig',
175 array(
176 'restNonce' => wp_create_nonce( 'wp_rest' ),
177 'gamesUrlBase' => esc_url_raw( rest_url( 'desktop-mode/v1/games' ) ),
178 'challengesUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/games/challenges' ) ),
179 'usersSearchUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/games/users/search' ) ),
180 )
181 );
182
183 wp_enqueue_style( 'desktop-mode-games' );
184 }
185 add_action( 'admin_enqueue_scripts', 'desktop_mode_games_localize_config', 30 );
186