PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.0
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.0
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 / render / assets.php

assets.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.0, at includes/render/assets.php

872 lines 39.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — Asset enqueue.
4 *
5 * Loads the desktop shell CSS + JS bundles when desktop mode is
6 * active and the request isn't chromeless / classic-overridden.
7 * Owns the entire `desktop_mode_enqueue_assets()` body — the
8 * largest hook in the original render.php and the natural seam
9 * for "what does the shell ship to the browser today?".
10 *
11 * Extracted from `render.php` during the architecture-0.8.1 PHP
12 * slicing (phase 6).
13 *
14 * @package Desktop_Mode
15 * @since 0.8.1
16 */
17
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Enqueues the desktop mode shell assets (CSS + JS) when desktop mode is active.
22 *
23 * Only loads the full desktop shell scripts and styles when the user has
24 * desktop mode enabled and the request is not a chromeless iframe load.
25 *
26 * @since 0.1.0
27 */
28 function desktop_mode_enqueue_assets() {
29 if ( ! is_admin() ) {
30 return;
31 }
32
33 // Auto-enqueue the iframe bridge anywhere a desktop-mode user
34 // might land. The bundle self-bails when not inside an iframe
35 // (`window.parent === window`), so it's a no-op on the parent
36 // shell — but cheap insurance against the failure mode the
37 // developer hit: an internal admin navigation drops the
38 // `?desktop_mode_chromeless=1` flag, the chromeless inline bridge doesn't
39 // run, and `wp.desktop.iframe` silently disappears. With this
40 // auto-enqueue, the API is universally present for any same-
41 // origin admin page a desktop-mode user opens — chromeless or
42 // accidentally classic.
43 if ( desktop_mode_is_enabled() ) {
44 wp_enqueue_script( 'desktop-mode-iframe-bridge' );
45
46 // Block Editor cross-window drop receiver. Listens for
47 // `desktop-mode-drop` postMessages from the parent shell and
48 // inserts the matching block. Only enqueue inside the
49 // post-edit Block Editor screens — every other admin page
50 // would be paying for a bundle it never uses.
51 //
52 // `site-editor.php` (full-site editor) deliberately omitted:
53 // the FSE doesn't expose `wp.data.dispatch('core/block-editor')`
54 // until the user opens a template in the canvas iframe, so
55 // drops arriving before that point would silently time out
56 // after the receiver's 5 s `waitForEditor()` poll. Re-enable
57 // once we have a reliable readiness signal in that context.
58 global $hook_suffix;
59 if ( 'post.php' === $hook_suffix || 'post-new.php' === $hook_suffix ) {
60 wp_enqueue_script( 'desktop-mode-gutenberg-drop-receiver' );
61 }
62 }
63
64 // Chromeless requests (iframes) need chromeless styles and overrides.
65 if ( desktop_mode_is_chromeless_request() ) {
66 wp_enqueue_style( 'desktop-mode' );
67 wp_enqueue_style( 'desktop-mode-chromeless' );
68
69 /**
70 * Fires when chromeless styles are enqueued inside a desktop mode iframe.
71 *
72 * Plugin and theme authors can hook here to enqueue their own CSS
73 * overrides for legacy pages rendered in chromeless mode. Use the
74 * `.desktop-mode-chromeless` body class to scope your rules.
75 *
76 * @since 0.1.0
77 */
78 do_action( 'desktop_mode_chromeless_styles' );
79 return;
80 }
81
82 if ( ! desktop_mode_is_enabled() || desktop_mode_is_classic_request() ) {
83 return;
84 }
85
86 // CSS.
87 wp_enqueue_style( 'desktop-mode' );
88 wp_enqueue_style( 'desktop-mode-windows' );
89 wp_enqueue_style( 'desktop-mode-dock' );
90 wp_enqueue_style( 'desktop-mode-dock-peek' );
91 wp_enqueue_style( 'desktop-mode-ai-assistant' );
92 wp_enqueue_style( 'desktop-mode-bug-report' );
93 wp_enqueue_style( 'desktop-mode-files' );
94
95 // JS.
96 wp_enqueue_script( 'desktop-mode' );
97
98 // `wp_enqueue_command_palette_assets()` (WP 6.9+) enqueues the
99 // `wp-commands` store package, the `wp-core-commands` script that
100 // registers the WordPress-wide baseline (Add new post, Manage
101 // plugins, Switch theme, Browse patterns, …) AND — critically —
102 // the inline `wp.coreCommands.initializeCommandPalette( … )` call
103 // that actually populates the `core/commands` data store with the
104 // admin-menu commands. Without that inline init, the script loads
105 // but the store stays empty and `src/commands/shell-harvester.ts`
106 // finds nothing to publish.
107 //
108 // WP normally only calls this on screens that opt in to the native
109 // palette; the shell needs it on every admin URL it might wrap.
110 // `function_exists` guard for pre-6.9 sites — the harvester gracefully
111 // no-ops when the store is missing.
112 if ( function_exists( 'wp_enqueue_command_palette_assets' ) ) {
113 // `wp_enqueue_command_palette_assets()` calls
114 // `array_key_exists( $menu_slug, $submenu )` without guarding
115 // the global, so an unset `$submenu` (test contexts, edge-case
116 // admin requests where the menu wasn't built yet) blows up
117 // with a TypeError. Initialize defensively before calling.
118 global $menu, $submenu;
119 if ( ! isset( $submenu ) || ! is_array( $submenu ) ) {
120 $submenu = array();
121 }
122 if ( ! isset( $menu ) || ! is_array( $menu ) ) {
123 $menu = array();
124 }
125 wp_enqueue_command_palette_assets();
126
127 // Expose the same menu-commands array WP serializes into
128 // `wp.coreCommands.initializeCommandPalette(...)` on a window
129 // slot the shell harvester can read. Built in PHP from `$menu`
130 // / `$submenu` here (we already guarded that they're arrays
131 // above), then injected as a `before` inline on our own bundle
132 // — that runs synchronously before `desktop.min.js` boots the
133 // shell harvester, so the lookup is guaranteed populated by
134 // the time `src/commands/shell-harvester.ts` classifies any
135 // command. Decoupled from WP's command-palette mount timing
136 // (which fires from a core-registered hook we can't reorder).
137 $menu_map = desktop_mode_build_command_menu_map();
138 wp_add_inline_script(
139 'desktop-mode',
140 'window.__desktopModeMenuCommands = ' . wp_json_encode( $menu_map ) . ';',
141 'before'
142 );
143 }
144
145 // Pass configuration to JavaScript.
146 global $title, $pagenow, $parent_file, $menu;
147
148 $menu_icon = 'dashicons-admin-generic';
149 if ( ! empty( $parent_file ) && ! empty( $menu ) ) {
150 foreach ( $menu as $item ) {
151 if ( ! empty( $item[2] ) && $item[2] === $parent_file && ! empty( $item[6] ) ) {
152 $menu_icon = $item[6];
153 break;
154 }
155 }
156 }
157
158 // Build dock items from the admin menu. Core pages are ordered
159 // first (Dashboard, Posts, Plugins, Users, Settings, …), then
160 // plugin-contributed top-level routes. `desktop_mode_dock_placement`
161 // is the per-item filter escape hatch for hiding. Shared with the
162 // REST menu endpoint so live refreshes (post plugin-activation)
163 // produce the same ordering as the boot payload.
164 $menu_payload = desktop_mode_build_menu_payload();
165 $dock_items = $menu_payload['dockItems'];
166 $native_windows = isset( $menu_payload['nativeWindows'] )
167 ? $menu_payload['nativeWindows']
168 : array();
169 $server_widgets = isset( $menu_payload['serverWidgets'] )
170 ? $menu_payload['serverWidgets']
171 : array();
172 $server_wallpapers = isset( $menu_payload['serverWallpapers'] )
173 ? $menu_payload['serverWallpapers']
174 : array();
175 $server_command_scripts = isset( $menu_payload['serverCommandScripts'] )
176 ? $menu_payload['serverCommandScripts']
177 : array();
178 $server_commands = isset( $menu_payload['serverCommands'] )
179 ? $menu_payload['serverCommands']
180 : array();
181 $server_settings_tab_scripts = isset( $menu_payload['serverSettingsTabScripts'] )
182 ? $menu_payload['serverSettingsTabScripts']
183 : array();
184 $server_settings_tabs = isset( $menu_payload['serverSettingsTabs'] )
185 ? $menu_payload['serverSettingsTabs']
186 : array();
187 $server_dock_rail_renderer_scripts = isset( $menu_payload['serverDockRailRendererScripts'] )
188 ? $menu_payload['serverDockRailRendererScripts']
189 : array();
190 $server_titlebar_button_scripts = isset( $menu_payload['serverTitleBarButtonScripts'] )
191 ? $menu_payload['serverTitleBarButtonScripts']
192 : array();
193 $server_window_theme_scripts = isset( $menu_payload['serverWindowThemeScripts'] )
194 ? $menu_payload['serverWindowThemeScripts']
195 : array();
196 $server_window_themes = isset( $menu_payload['serverWindowThemes'] )
197 ? $menu_payload['serverWindowThemes']
198 : array();
199 $server_window_control_scripts = isset( $menu_payload['serverWindowControlScripts'] )
200 ? $menu_payload['serverWindowControlScripts']
201 : array();
202 $server_window_controls = isset( $menu_payload['serverWindowControls'] )
203 ? $menu_payload['serverWindowControls']
204 : array();
205 $server_window_slot_scripts = isset( $menu_payload['serverWindowSlotScripts'] )
206 ? $menu_payload['serverWindowSlotScripts']
207 : array();
208 $server_window_slots = isset( $menu_payload['serverWindowSlots'] )
209 ? $menu_payload['serverWindowSlots']
210 : array();
211 $server_window_chrome_scripts = isset( $menu_payload['serverWindowChromeScripts'] )
212 ? $menu_payload['serverWindowChromeScripts']
213 : array();
214 $server_window_chromes = isset( $menu_payload['serverWindowChromes'] )
215 ? $menu_payload['serverWindowChromes']
216 : array();
217 $server_window_notices = isset( $menu_payload['serverWindowNotices'] )
218 ? $menu_payload['serverWindowNotices']
219 : array();
220 $desktop_icons = isset( $menu_payload['desktopIcons'] )
221 ? $menu_payload['desktopIcons']
222 : array();
223
224 // Files-on-the-Desktop payload (Phase 0+1). Plugin-registered
225 // file types and openers ship as metadata only; the JS side
226 // holds the executable handlers and resolves on double-click.
227 $server_file_types = function_exists( 'desktop_mode_build_file_types_payload' )
228 ? desktop_mode_build_file_types_payload()
229 : array();
230 $server_file_openers = function_exists( 'desktop_mode_build_file_openers_payload' )
231 ? desktop_mode_build_file_openers_payload()
232 : array();
233 $user_file_associations = function_exists( 'desktop_mode_get_user_file_associations' )
234 ? desktop_mode_get_user_file_associations( get_current_user_id() )
235 : array();
236 $server_wallpaper_menu_items = function_exists( 'desktop_mode_build_wallpaper_menu_items' )
237 ? desktop_mode_build_wallpaper_menu_items()
238 : array();
239
240 /*
241 * OS-file drop config — what the browser drop manager will
242 * accept when the user drags a file from their native desktop
243 * onto any surface inside Desktop Mode (wallpaper, a folder,
244 * a window, or a chromeless iframe). The allowed-mimes list is
245 * the user-scoped `get_allowed_mime_types()` (already capability
246 * gated by WordPress); the size cap is `wp_max_upload_size()`.
247 *
248 * Both are filterable so plugins can narrow or widen the set —
249 * e.g. a media-only plugin can restrict drops to images, or a
250 * docs plugin can opt PDFs in for a specific role.
251 */
252 $drop_allowed_mimes_map = current_user_can( 'upload_files' )
253 ? get_allowed_mime_types( get_current_user_id() )
254 : array();
255 /**
256 * Filter the allowed-mime map used by the OS-file drop manager.
257 *
258 * @since 0.30.0
259 *
260 * @param array<string,string> $mimes_map `ext => mime-type` map (same shape `get_allowed_mime_types()` returns).
261 * @param int $user_id The current user id.
262 */
263 $drop_allowed_mimes_map = apply_filters( 'desktop_mode_drop_allowed_mimes', $drop_allowed_mimes_map, get_current_user_id() );
264 $drop_allowed_mimes_map = is_array( $drop_allowed_mimes_map ) ? $drop_allowed_mimes_map : array();
265 $drop_allowed_mimes = array_values( array_unique( array_values( $drop_allowed_mimes_map ) ) );
266
267 $drop_max_size = (int) wp_max_upload_size();
268 /**
269 * Filter the per-file size cap (in bytes) used by the OS-file
270 * drop manager. Returning `0` disables the client-side cap —
271 * the server still enforces its own.
272 *
273 * @since 0.30.0
274 *
275 * @param int $max_size Default `wp_max_upload_size()`.
276 * @param int $user_id The current user id.
277 */
278 $drop_max_size = (int) apply_filters( 'desktop_mode_drop_max_size', $drop_max_size, get_current_user_id() );
279
280 /**
281 * Filter the master OS-file drop enable gate. Lets plugins
282 * disable the drop manager by role / capability beyond the
283 * default `upload_files` check (e.g. only for admins, or
284 * only on specific multisite blogs).
285 *
286 * @since 0.30.0
287 *
288 * @param bool $enabled Default — `current_user_can( 'upload_files' )`.
289 * @param int $user_id The current user id.
290 */
291 $drop_enabled = (bool) apply_filters(
292 'desktop_mode_drop_enabled',
293 current_user_can( 'upload_files' ),
294 get_current_user_id()
295 );
296
297 $drop_config = array(
298 'enabled' => $drop_enabled,
299 'allowedMimes' => $drop_allowed_mimes,
300 'extToMime' => $drop_allowed_mimes_map,
301 'maxSize' => $drop_max_size,
302 );
303
304 // Lazy-bundle URL builder. Each lazy-loaded bundle (AI Assistant,
305 // About-scene, OS Settings panel, shell-overlays, window-system)
306 // is `<script>`-injected by the main bundle on demand — they don't
307 // go through `wp_register_script`, so they don't pick up WordPress's
308 // usual `?ver=<filemtime>` cache-buster. Without one, the browser
309 // happily serves a stale cached copy across plugin updates that
310 // don't bump `DESKTOP_MODE_VERSION`, and the main bundle's loader
311 // fires a `<script>`-loaded event for a file that's missing the
312 // fresh `window.desktopMode*` factory the new code expects.
313 //
314 // Mirror the `$built_version( … )` helper in `includes/assets.php`:
315 // prefer the on-disk mtime of the actual file, fall back to the
316 // plugin version when the file is missing (dev environments where
317 // the bundle hasn't been built yet).
318 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
319 $lazy_bundle_url = static function ( $base ) use ( $suffix ) {
320 $path = DESKTOP_MODE_DIR . 'assets/js/' . $base . $suffix . '.js';
321 $ver = file_exists( $path )
322 ? (string) filemtime( $path )
323 : DESKTOP_MODE_VERSION;
324 return esc_url_raw(
325 DESKTOP_MODE_URL . 'assets/js/' . $base . $suffix . '.js?ver=' . $ver
326 );
327 };
328
329 // Build the current page URL from $pagenow + $_GET. Strip the portal
330 // markers so the derived window ID matches what the dock would produce
331 // for the same page — otherwise auto-opening the entry window and
332 // clicking the same dock icon would create a duplicate.
333 $current_query = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
334 unset( $current_query[ DESKTOP_MODE_PORTAL_FLAG ], $current_query[ DESKTOP_MODE_PORTAL_INTENT_FLAG ] );
335 $current_page = admin_url( $pagenow ) . ( ! empty( $current_query ) ? '?' . http_build_query( $current_query ) : '' );
336
337 $from_portal = ! empty( $_GET[ DESKTOP_MODE_PORTAL_FLAG ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
338 $from_portal_intent = ! empty( $_GET[ DESKTOP_MODE_PORTAL_INTENT_FLAG ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
339
340 /**
341 * Filters the desktop shell configuration passed to JavaScript.
342 *
343 * @since 0.1.0
344 *
345 * @param array $config {
346 * Desktop shell configuration.
347 *
348 * @type string $currentPage The current admin page URL.
349 * @type string $currentTitle The current page title.
350 * @type string $currentIcon Dashicon class for the current page.
351 * @type string $adminUrl The base admin URL.
352 * @type string $colorScheme The active admin color scheme.
353 * @type array $dockItems Dock items derived from the admin menu. Core WordPress pages (Dashboard, Posts, Plugins, Users, Settings, CPTs…) are ordered first; plugin-contributed top-level routes (admin.php?page=*) follow. Items hidden via `desktop_mode_dock_placement` are omitted.
354 * @type array $nativeWindows Server-declared native windows (via `desktop_mode_register_window`). Shell registers + syncs tiles based on this list — activation/deactivation is a diff without shell reload.
355 * @type array $serverWidgets Server-declared right-column widgets (via `desktop_mode_register_widget`). Shell syncs the widget registry + dynamically loads plugin scripts so widgets appear in the picker without a shell reload.
356 * @type array $serverWallpapers Server-declared wallpapers (via `desktop_mode_register_wallpaper`). Same lifecycle — shell loads the plugin's JS, reads the full `WallpaperDef` from `window.desktopModeWallpapers[id]`, and registers / unregisters as plugins activate / deactivate.
357 * @type array $serverCommandScripts Script handles opted-in via `desktop_mode_register_command_script`. Shell injects each URL on activation so commands registered by `wp.desktop.registerCommand` appear in the palette live. Deactivation unregisters any commands whose `owner` matches the departing handle.
358 * @type array $serverCommands Server-declared command metadata (via `desktop_mode_register_command`). Advisory today — reserved for future pre-registration shims.
359 * @type array $serverSettingsTabScripts Script handles opted-in via `desktop_mode_register_settings_tab_script`. Shell injects each URL on activation so tabs registered by `wp.desktop.registerSettingsTab` appear in the OS Settings window live. Deactivation unregisters tabs attributable to the departing handle.
360 * @type array $serverSettingsTabs Server-declared settings-tab metadata (via `desktop_mode_register_settings_tab`). Enables live unregistration on plugin deactivation without requiring JS to set `owner`.
361 * @type array $desktopIcons Server-declared desktop icons (via `desktop_mode_register_icon`). Rendered on the wallpaper as clickable shortcut tiles.
362 * @type array $accentColors Swatch list for the OS Settings accent picker. Filterable via `desktop_mode_accent_colors`.
363 * @type array $toastTypes Toast-notification type map. Filterable via `desktop_mode_toast_types`.
364 * @type string $defaultWallpaper Wallpaper slug applied on first boot. Filterable via `desktop_mode_default_wallpaper`.
365 * @type array $session Saved session (windows, focused, updated).
366 * @type string $sessionUrl REST endpoint for saving the session.
367 * @type string $mediaUrl REST endpoint for media uploads (wp/v2/media).
368 * @type string $restUrl REST API root from rest_url(), safe for pretty and plain permalink installs.
369 * @type string $defaultWindowUrl REST endpoint for saving the default-window preference.
370 * @type array $defaultWindow { enabled: bool, url: string } — current default-window preference.
371 * @type bool $canUpload Whether the user holds the `upload_files` capability.
372 * @type string $pluginUrl Plugin base URL (no trailing slash). Used by the shell to locate vendor assets and by plugins to build asset URLs.
373 * @type string $pluginVersion Plugin semver string. Surfaced in the OS Settings → About tab; plugins can read it to gate features by version.
374 * @type string $restNonce Nonce for the session REST endpoint.
375 * @type string $portalUrl Canonical `/desktop-mode/` URL.
376 * @type bool $fromPortal Whether the shell was reached via the portal.
377 * @type bool $fromPortalIntent Whether the portal redirect resolved from an explicit `?target=…` (user navigation intent) rather than the session's focused window or the default-window fallback. Distinguishes a bare `/desktop-mode/` visit from a portal-redirected admin-bar click so the shell can honour the URL the user actually asked for.
378 * @type array $seenIntros Slugs of one-time intro dialogs the user has dismissed (e.g. `['posts']`). Native windows gate their first-open intro on this list.
379 * @type string $seenIntrosUrl REST endpoint for the seen-intros surface — POST `/seen` to mark, DELETE the base to reset.
380 * }
381 */
382 $config = apply_filters(
383 'desktop_mode_shell_config',
384 array(
385 'currentPage' => esc_url( $current_page ),
386 'currentTitle' => wp_strip_all_tags( $title ),
387 'currentIcon' => sanitize_html_class( $menu_icon ),
388 'adminUrl' => esc_url( admin_url() ),
389 'colorScheme' => sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' ),
390 'dockItems' => $dock_items,
391 'nativeWindows' => $native_windows,
392 'serverWidgets' => $server_widgets,
393 'serverWallpapers' => $server_wallpapers,
394 'serverCommandScripts' => $server_command_scripts,
395 'serverCommands' => $server_commands,
396 'serverSettingsTabScripts' => $server_settings_tab_scripts,
397 'serverSettingsTabs' => $server_settings_tabs,
398 'serverDockRailRendererScripts' => $server_dock_rail_renderer_scripts,
399 'serverTitleBarButtonScripts' => $server_titlebar_button_scripts,
400 'serverWindowThemeScripts' => $server_window_theme_scripts,
401 'serverWindowThemes' => $server_window_themes,
402 'serverWindowControlScripts' => $server_window_control_scripts,
403 'serverWindowControls' => $server_window_controls,
404 'serverWindowSlotScripts' => $server_window_slot_scripts,
405 'serverWindowSlots' => $server_window_slots,
406 'serverWindowChromeScripts' => $server_window_chrome_scripts,
407 'serverWindowChromes' => $server_window_chromes,
408 'serverWindowNotices' => $server_window_notices,
409 'desktopIcons' => $desktop_icons,
410 'serverFileTypes' => $server_file_types,
411 'serverFileOpeners' => $server_file_openers,
412 'userFileAssociations' => $user_file_associations,
413 'filesUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/files' ) ),
414 'serverWallpaperMenuItems' => $server_wallpaper_menu_items,
415 'accentColors' => desktop_mode_get_accent_colors(),
416 'toastTypes' => desktop_mode_get_toast_types(),
417 'defaultWallpaper' => desktop_mode_get_default_wallpaper(),
418 'session' => desktop_mode_get_session( get_current_user_id() ),
419 'sessionUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/session' ) ),
420 'restUrl' => esc_url_raw( rest_url() ),
421 'mediaUrl' => esc_url_raw( rest_url( 'wp/v2/media' ) ),
422 'dropConfig' => $drop_config,
423 'defaultWindowUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/default-window' ) ),
424 'defaultWindow' => desktop_mode_get_default_window( get_current_user_id() ),
425 'canUpload' => current_user_can( 'upload_files' ),
426 'pluginUrl' => esc_url_raw( untrailingslashit( DESKTOP_MODE_URL ) ),
427 'pluginVersion' => DESKTOP_MODE_VERSION,
428 'iframeBridgeUrl' => $lazy_bundle_url( 'iframe-bridge' ),
429 // URL of the AI Assistant lazy bundle. The main bundle
430 // ships a stub matching the public `wp.desktop.ai` API; the
431 // stub `<script>`-injects this URL the first time the user
432 // opens the assistant. Picking `.js` vs `.min.js` here keeps
433 // the SCRIPT_DEBUG gate server-side, matching iframeBridgeUrl.
434 'aiAssistantBundleUrl' => $lazy_bundle_url( 'ai-assistant' ),
435 // URL of the About-scene lazy bundle. The OS Settings →
436 // About tab loads this on first mount; ~25 kB PixiJS
437 // particle scene that would otherwise ship in the main
438 // bundle for every shell load.
439 'aboutSceneBundleUrl' => $lazy_bundle_url( 'about-scene' ),
440 // URL of the OS Settings panel lazy bundle. Injected by
441 // the main bundle's `OsSettings.renderPanel()` stub on
442 // the user's first Settings open. Holds every section
443 // renderer + the `<wpd-*>` components only the panel
444 // uses, so nothing about Settings ships in
445 // `desktop.min.js` for users who never open it.
446 'osSettingsPanelBundleUrl' => $lazy_bundle_url( 'os-settings-panel' ),
447 // URL of the shell-overlays lazy bundle. Pre-loaded by
448 // the main bundle after first paint so action-triggered
449 // overlays (toast, confirm dialog, context menus) feel
450 // instant the first time they fire.
451 'shellOverlaysBundleUrl' => $lazy_bundle_url( 'shell-overlays' ),
452 // URL of the lazy window-system bundle (Stage 11).
453 // Holds the `Window` class and its DOM / pointer / tab /
454 // chrome helpers — the single largest module in the pre-
455 // 0.8.4 main bundle. Loaded on first `windowManager.open()`
456 // / `openNew()` call (both async since 0.8.4); pre-loaded
457 // by the shell after first paint when no session is being
458 // restored and no `openCurrentPage` will fire.
459 'windowSystemBundleUrl' => $lazy_bundle_url( 'window-system' ),
460 'restNonce' => wp_create_nonce( 'wp_rest' ),
461 'osSettings' => desktop_mode_get_os_settings( get_current_user_id() ),
462 'osSettingsUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/os-settings' ) ),
463 'seenIntros' => desktop_mode_get_seen_intros( get_current_user_id() ),
464 'seenIntrosUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/intros' ) ),
465 'aiSearchUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/ai/search' ) ),
466 'aiSearchStreamUrl' => esc_url_raw( add_query_arg( 'action', 'desktop_mode_ai_search_stream', admin_url( 'admin-ajax.php' ) ) ),
467 'aiPlatformSettings' => current_user_can( 'manage_options' ) ? desktop_mode_ai_get_platform_settings() : null,
468 'aiPlatformSettingsUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/ai/platform-settings' ) ),
469 'aiProviders' => desktop_mode_ai_get_providers_for_config(),
470 'extendedOptions' => current_user_can( 'manage_options' ) ? desktop_mode_get_extended_options() : null,
471 'extendedOptionsUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/extended-options' ) ),
472 // Comments-window AI moderation toggle — surfaced at the
473 // shell level so the OS Settings → Features tab can render
474 // the toggle without depending on the Comments window
475 // being registered for this user. URL is the same
476 // endpoint the comments-window config exposes; state is
477 // `null` for non-admins (the UI hides the row entirely).
478 'commentsAiUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/comments/ai-settings' ) ),
479 'commentsAi' => current_user_can( 'manage_options' )
480 ? array(
481 'enabled' => function_exists( 'desktop_mode_comments_ai_is_enabled' )
482 ? desktop_mode_comments_ai_is_enabled()
483 : false,
484 'providerConfigured' => function_exists( 'desktop_mode_comments_ai_provider_configured' )
485 ? desktop_mode_comments_ai_provider_configured()
486 : false,
487 )
488 : null,
489 'currentUserIsAdmin' => current_user_can( 'manage_options' ),
490 'portalUrl' => esc_url( desktop_mode_portal_url() ),
491 'fromPortal' => $from_portal,
492 'fromPortalIntent' => $from_portal_intent,
493 'pwa' => array(
494 'manifestUrl' => esc_url_raw( desktop_mode_pwa_manifest_url() ),
495 'swUrl' => esc_url_raw( desktop_mode_pwa_sw_url() ),
496 'stateUrl' => esc_url_raw( rest_url( 'desktop-mode/v1/pwa-state' ) ),
497 'state' => desktop_mode_pwa_get_user_state( get_current_user_id() ),
498 // Mirrors the manifest's `name` field — used by the
499 // install pill so the button reads "Install <site>"
500 // rather than "Install <current page>" (which would
501 // be misleading: we install the whole site as an
502 // app, not the dashboard window the user happens to
503 // be viewing).
504 'appName' => get_bloginfo( 'name' ),
505 // Operators set the `desktop_mode_pwa_force_replace_sw`
506 // filter to `true` when another root-scope service
507 // worker on the origin is blocking desktop-mode
508 // installability (foreign-SW guard in
509 // `src/pwa/sw-register.ts`). Default `false` preserves
510 // the polite behaviour where we yield to existing PWAs.
511 'forceReplaceSw' => desktop_mode_pwa_force_replace_sw(),
512 ),
513 )
514 );
515
516 wp_localize_script( 'desktop-mode', 'desktopModeConfig', $config );
517
518 /**
519 * Fires when desktop mode assets are enqueued.
520 *
521 * @since 0.1.0
522 */
523 do_action( 'desktop_mode_mode_init' );
524 }
525 add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_assets' );
526
527 /**
528 * Emits `<link rel="preload">` hints for the shell's critical-path
529 * assets so the browser starts fetching them as soon as it parses
530 * the document `<head>`.
531 *
532 * Without this, the browser doesn't discover the main `desktop.min.js`
533 * bundle URL until it parses the footer `<script>` tag — typically
534 * ~1 RTT after the rest of the page has started loading. For a 464 KB
535 * bundle on a midrange phone that's a measurable FCP delay; on a
536 * slow connection it dominates first paint entirely.
537 *
538 * Hooked at `admin_print_styles @ 1` so the preload tags land in
539 * `<head>` BEFORE the regular `<link rel="stylesheet">` tags (which
540 * default to priority 10) and well before the footer `<script>`
541 * tag. The `wp_resource_hints` filter is frontend-only (`wp_head`-
542 * driven) and isn't invoked in admin context, so we emit our own
543 * tags.
544 *
545 * Four targets by default:
546 * - `desktop[.min].js` — the shell bundle (biggest win).
547 * - `desktop.css` — shell base CSS, needed for first paint.
548 * - `window-system[.min].js` — lazy bundle preloaded by JS after
549 * first paint; hinting in HTML lets the browser start the fetch
550 * in parallel with the main bundle's parse instead of waiting.
551 * - `shell-overlays[.min].js` — same rationale.
552 *
553 * Plugins can extend the hint list via the `desktop_mode_preload_hints`
554 * filter — e.g. a settings tab whose bundle the user opens on every
555 * visit can opt its own URL into the preload phase.
556 *
557 * Same-origin resources only — no `crossorigin` attribute. CDN hosts
558 * that serve `wp-content/plugins/` from a different origin should
559 * supply absolute URLs through the filter; in that case the consumer
560 * is responsible for the `crossorigin` semantics.
561 *
562 * @since 0.8.9
563 */
564 function desktop_mode_print_preload_hints() {
565 if (
566 ! is_admin()
567 || ! desktop_mode_is_enabled()
568 || desktop_mode_is_chromeless_request()
569 || desktop_mode_is_classic_request()
570 ) {
571 return;
572 }
573
574 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
575
576 $build_url = static function ( $relative ) {
577 $path = DESKTOP_MODE_DIR . $relative;
578 $ver = file_exists( $path ) ? (string) filemtime( $path ) : DESKTOP_MODE_VERSION;
579 return DESKTOP_MODE_URL . $relative . '?ver=' . $ver;
580 };
581
582 $hints = array(
583 array(
584 'href' => $build_url( 'assets/js/desktop' . $suffix . '.js' ),
585 'as' => 'script',
586 ),
587 array(
588 'href' => $build_url( 'assets/css/desktop.css' ),
589 'as' => 'style',
590 ),
591 array(
592 'href' => $build_url( 'assets/js/window-system' . $suffix . '.js' ),
593 'as' => 'script',
594 ),
595 array(
596 'href' => $build_url( 'assets/js/shell-overlays' . $suffix . '.js' ),
597 'as' => 'script',
598 ),
599 );
600
601 /**
602 * Filters the list of resource preload hints emitted in `<head>`.
603 *
604 * Each entry is a `{ 'href' => string, 'as' => string }` array
605 * rendered as `<link rel="preload" as="<as>" href="<href>">`.
606 * Unrecognized entries are silently skipped — keep the contract
607 * permissive so a misconfigured plugin can't tank first paint.
608 *
609 * @since 0.8.9
610 *
611 * @param array $hints Default hints (main bundle, base CSS,
612 * window-system, shell-overlays).
613 */
614 $hints = apply_filters( 'desktop_mode_preload_hints', $hints );
615
616 if ( ! is_array( $hints ) ) {
617 return;
618 }
619
620 foreach ( $hints as $hint ) {
621 if ( ! is_array( $hint ) ) {
622 continue;
623 }
624 $href = isset( $hint['href'] ) ? (string) $hint['href'] : '';
625 $as = isset( $hint['as'] ) ? (string) $hint['as'] : '';
626 if ( '' === $href || '' === $as ) {
627 continue;
628 }
629 printf(
630 '<link rel="preload" as="%s" href="%s" />' . "\n",
631 esc_attr( $as ),
632 esc_url( $href )
633 );
634 }
635 }
636 add_action( 'admin_print_styles', 'desktop_mode_print_preload_hints', 1 );
637
638 /**
639 * Defers loading of non-critical desktop-mode stylesheets so they
640 * don't block first paint.
641 *
642 * Three stylesheets in the default enqueue list are only needed
643 * after a user interaction — `dock-peek` (mouseover a dock tile),
644 * `ai-assistant` (Cmd+K palette), `bug-report` (Report-a-bug
645 * window). With the normal `<link rel="stylesheet">` tag they sit
646 * on the critical path and the browser blocks first paint waiting
647 * for them, even though nothing on screen needs them yet.
648 *
649 * The well-known mitigation is the `media="print" onload="…"`
650 * pattern:
651 *
652 * <link rel="stylesheet" media="print"
653 * onload="this.media='all'; this.onload=null" href="…">
654 * <noscript><link rel="stylesheet" href="…"></noscript>
655 *
656 * `media="print"` makes the browser treat the sheet as
657 * non-applicable to the current display, so it downloads with
658 * low priority and doesn't block render. The `onload` handler
659 * swaps `media` to the original value once the bytes arrive
660 * (within ms of page load), making the styles take effect long
661 * before the user clicks anything that needs them. The
662 * `<noscript>` fallback restores critical-path behavior for JS-off
663 * browsers, so accessibility isn't degraded.
664 *
665 * Filterable via `desktop_mode_deferred_styles` so plugins can opt
666 * their own non-critical stylesheets in (or pull a built-in out).
667 * Chromeless iframes are skipped — their CSS pipeline is separate.
668 *
669 * @since 0.8.9
670 *
671 * @param string $html The original <link> tag HTML.
672 * @param string $handle The stylesheet handle WP is printing.
673 * @param string $href The full URL of the stylesheet.
674 * @param string $media The media attribute value WP resolved.
675 * @return string Possibly-rewritten tag.
676 */
677 function desktop_mode_defer_non_critical_styles( $html, $handle, $href, $media ) {
678 // Cheap gates first — `style_loader_tag` fires once per enqueued
679 // stylesheet on EVERY admin page (frontend doesn't go through
680 // this filter, but admin does, including pages where desktop mode
681 // is disabled). The deferred handles only ship when desktop mode
682 // is active, so the in_array check below would always miss on
683 // classic-only admin pages — but the `apply_filters` call still
684 // builds an array and walks subscribers per stylesheet. Short-
685 // circuit on the cheap helper checks (`is_admin` / enabled /
686 // chromeless) so non-desktop-mode users pay nothing.
687 if ( ! desktop_mode_is_enabled() ) {
688 return $html;
689 }
690 if ( desktop_mode_is_chromeless_request() ) {
691 return $html;
692 }
693
694 /**
695 * Filters the list of stylesheet handles that should be loaded
696 * deferred via the media-print-onload pattern. Plugins can add
697 * their own non-critical stylesheets here, or pull a built-in
698 * out (e.g. a plugin that surfaces the AI assistant on every
699 * page might want to keep its CSS critical-path).
700 *
701 * @since 0.8.9
702 *
703 * @param string[] $handles Default deferred handles.
704 */
705 $deferred = apply_filters(
706 'desktop_mode_deferred_styles',
707 array(
708 'desktop-mode-dock-peek',
709 'desktop-mode-ai-assistant',
710 'desktop-mode-bug-report',
711 )
712 );
713
714 if ( ! in_array( $handle, (array) $deferred, true ) ) {
715 return $html;
716 }
717
718 $resolved_media = $media ? $media : 'all';
719 $id = $handle . '-css';
720
721 // Two contexts, two escapers for the same `$resolved_media` value:
722 //
723 // - `%3$s` lands inside a JS string literal inside the HTML
724 // `onload="…"` attribute (`this.media='%3$s'`). `esc_attr`
725 // escapes `"` and `&` but NOT single quotes, so a media
726 // value containing `'` would break out of the JS string.
727 // `esc_js` is the correct escaper for "string literal inside
728 // an event-handler attribute" — escapes single quotes, double
729 // quotes, backslashes, newlines. Today `$resolved_media`
730 // comes from `wp_enqueue_style()`'s `$media` parameter (always
731 // a CSS media type / query produced by WordPress core), so
732 // this is pure defense-in-depth, but the cost is one extra
733 // function call.
734 //
735 // - `%4$s` lands inside an HTML attribute in the `<noscript>`
736 // fallback (`media='%4$s'`). That's standard `esc_attr`.
737 //
738 // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- This filter rewrites a tag WordPress is in the process of emitting for an already-registered+enqueued stylesheet handle; the linter doesn't trace the `style_loader_tag` filter context, so the raw <link rel="stylesheet"> output is a false-positive.
739 $markup = sprintf(
740 '<link rel=\'stylesheet\' id=\'%1$s\' href=\'%2$s\' media=\'print\' onload="this.media=\'%3$s\'; this.onload=null;" />' . "\n" .
741 '<noscript><link rel=\'stylesheet\' id=\'%1$s-noscript\' href=\'%2$s\' media=\'%4$s\' /></noscript>' . "\n",
742 esc_attr( $id ),
743 esc_url( $href ),
744 esc_js( $resolved_media ),
745 esc_attr( $resolved_media )
746 );
747 // phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
748
749 return $markup;
750 }
751 add_filter( 'style_loader_tag', 'desktop_mode_defer_non_critical_styles', 10, 4 );
752
753 /**
754 * Build the admin-menu command map (name → URL) and expose it on
755 * `window.__desktopModeMenuCommands`. The shell command harvester
756 * (`src/commands/shell-harvester.ts`) reads this slot to resolve URLs
757 * for "Go to: …" commands whose JS callbacks
758 * (`document.location = menuCommand.url`) close over a variable URL
759 * we can't extract from source. Without this map those commands
760 * either get skipped (no URL recoverable) or — if the location
761 * shadow misses — navigate the SHELL out of desktop mode.
762 *
763 * Mirrors what WordPress core's `wp_enqueue_command_palette_assets()`
764 * builds for `wp.coreCommands.initializeCommandPalette(...)`. We
765 * duplicate the logic here (instead of monkey-patching the JS init
766 * which is timing-sensitive — WP registers its hook during core load,
767 * so it always emits its inline before any plugin-added inline on the
768 * same handle) and ship the result through `wp_add_inline_script` on
769 * our own bundle handle. That decouples us entirely from WP's command-
770 * palette mount timing.
771 *
772 * @since 0.8.4
773 *
774 * @global array $menu
775 * @global array $submenu
776 * @return array<int, array{label:string, url:string, name:string}>
777 */
778 function desktop_mode_build_command_menu_map() {
779 global $menu, $submenu;
780 if ( ! is_array( $menu ) ) {
781 return array();
782 }
783 $out = array();
784
785 $extract_root_text = static function ( $label ) {
786 if ( '' === $label || ! is_string( $label ) ) {
787 return '';
788 }
789 if ( class_exists( 'WP_HTML_Tag_Processor' ) ) {
790 $processor = new WP_HTML_Tag_Processor( $label );
791 $text = '';
792 $depth = 0;
793 while ( $processor->next_token() ) {
794 $token_type = $processor->get_token_type();
795 if ( '#text' === $token_type && 0 === $depth ) {
796 $text .= $processor->get_modifiable_text();
797 }
798 if ( '#tag' === $token_type ) {
799 if ( $processor->is_tag_closer() ) {
800 if ( $depth > 0 ) {
801 --$depth;
802 }
803 continue;
804 }
805 $name = $processor->get_tag();
806 if ( $name && ! ( class_exists( 'WP_HTML_Processor' ) && WP_HTML_Processor::is_void( $name ) ) ) {
807 ++$depth;
808 }
809 }
810 }
811 return trim( $text );
812 }
813 return trim( wp_strip_all_tags( $label ) );
814 };
815
816 foreach ( $menu as $menu_item ) {
817 if ( empty( $menu_item[0] ) || ! is_string( $menu_item[0] ) ) {
818 continue;
819 }
820 if ( ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
821 continue;
822 }
823 $menu_label = $extract_root_text( $menu_item[0] );
824 $menu_slug = $menu_item[2];
825 $menu_url = '';
826 if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
827 $menu_url = $menu_slug;
828 } elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
829 $menu_url = menu_page_url( $menu_slug, false );
830 }
831 if ( '' !== $menu_url ) {
832 $out[] = array(
833 'label' => $menu_label,
834 'url' => $menu_url,
835 'name' => $menu_slug,
836 );
837 }
838 if ( ! empty( $submenu ) && is_array( $submenu ) && array_key_exists( $menu_slug, $submenu ) ) {
839 foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
840 if ( empty( $submenu_item[0] ) ) {
841 continue;
842 }
843 if ( ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
844 continue;
845 }
846 $submenu_label = $extract_root_text( $submenu_item[0] );
847 $submenu_slug = $submenu_item[2];
848 $submenu_url = '';
849 if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
850 $submenu_url = $submenu_slug;
851 } elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
852 $submenu_url = menu_page_url( $submenu_slug, false );
853 }
854 if ( '' === $submenu_url ) {
855 continue;
856 }
857 $out[] = array(
858 'label' => sprintf(
859 /* translators: 1: parent menu label, 2: submenu label */
860 __( '%1$s > %2$s', 'desktop-mode' ),
861 $menu_label,
862 $submenu_label
863 ),
864 'url' => $submenu_url,
865 'name' => $menu_slug . '-' . $submenu_item[2],
866 );
867 }
868 }
869 }
870 return $out;
871 }
872