PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.8
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.8
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.8, at includes/games/window.php

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