PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.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 / desktop-themes / assets.php

assets.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.7, at includes/desktop-themes/assets.php

157 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — Desktop-theme enqueue + body class + shell config.
4 *
5 * **Zero cost when no theme is active.** Every entry point here
6 * early-returns on an empty selection before it reads an option,
7 * touches the filesystem, or registers a handle. A site whose users
8 * all sit on "System default" pays one user-meta read that the OS
9 * settings layer was doing anyway.
10 *
11 * @package OpenStation
12 */
13
14 defined( 'ABSPATH' ) || exit;
15
16 /** Style handle for the compiled desktop-theme stylesheet. */
17 const OPENSTATION_DESKTOP_THEME_STYLE_HANDLE = 'os-desktop-theme';
18
19 /**
20 * Resolve the desktop theme a user has selected, if it still exists.
21 *
22 * Orphans degrade silently: a user whose selected theme was deleted
23 * (or whose registering plugin was deactivated) gets `''` here and
24 * sees the system default, with no user-meta rewrite and no error.
25 *
26 * @param int $user_id User id. Defaults to the current user.
27 * @return string Slug, or `''` for the system default.
28 */
29 function openstation_active_desktop_theme_slug( $user_id = 0 ) {
30 $user_id = (int) $user_id;
31 if ( $user_id <= 0 ) {
32 $user_id = get_current_user_id();
33 }
34 if ( $user_id <= 0 ) {
35 return '';
36 }
37
38 $settings = openstation_get_os_settings( $user_id );
39 $slug = isset( $settings['desktopTheme'] ) ? sanitize_key( (string) $settings['desktopTheme'] ) : '';
40 if ( '' === $slug ) {
41 return '';
42 }
43
44 // Existence check across both sources. This is the safety net
45 // that lets the os-settings sanitizer stay a cheap pattern check
46 // instead of loading the themes option on every settings write.
47 if ( null !== openstation_desktop_theme_get( $slug ) ) {
48 return $slug;
49 }
50 if ( null !== openstation_desktop_theme_registry( $slug ) ) {
51 return $slug;
52 }
53 return '';
54 }
55
56 /**
57 * Whether this request should carry desktop-theme assets at all.
58 *
59 * @internal
60 *
61 * @return bool
62 */
63 function openstation_desktop_theme_request_is_themable() {
64 return openstation_is_shell_request();
65 }
66
67 /**
68 * Enqueue the active theme's compiled stylesheet.
69 *
70 * The `os-variables` dependency is load-bearing, not
71 * decoration: the compiled selectors weigh the same as the
72 * per-admin-color-scheme blocks in `variables.css`, and a
73 * specificity tie is settled by source order. Drop the dependency
74 * and a themed token silently loses to the color scheme.
75 */
76 function openstation_enqueue_desktop_theme_style() {
77 if ( ! openstation_desktop_theme_request_is_themable() ) {
78 return;
79 }
80 $slug = openstation_active_desktop_theme_slug();
81 if ( '' === $slug ) {
82 // The whole point: nothing registered, nothing enqueued, no
83 // extra request, no extra bytes.
84 return;
85 }
86
87 $uploaded = openstation_desktop_theme_get( $slug );
88 if ( is_array( $uploaded ) ) {
89 $version = isset( $uploaded['installedAt'] ) ? (string) (int) $uploaded['installedAt'] : OPENSTATION_VERSION;
90 wp_enqueue_style(
91 OPENSTATION_DESKTOP_THEME_STYLE_HANDLE,
92 openstation_desktop_themes_url( $slug ) . '/theme.css',
93 array( 'os-variables' ),
94 $version
95 );
96 return;
97 }
98
99 $code = openstation_desktop_theme_registry( $slug );
100 if ( is_array( $code ) && ! empty( $code['cssText'] ) ) {
101 // Code themes have no file to link. Register a src-less stub
102 // so `wp_add_inline_style()` has a handle to hang off, and
103 // keep the same dependency so print order is identical.
104 wp_register_style(
105 OPENSTATION_DESKTOP_THEME_STYLE_HANDLE,
106 false,
107 array( 'os-variables' ),
108 OPENSTATION_VERSION
109 );
110 wp_enqueue_style( OPENSTATION_DESKTOP_THEME_STYLE_HANDLE );
111 wp_add_inline_style( OPENSTATION_DESKTOP_THEME_STYLE_HANDLE, (string) $code['cssText'] );
112 }
113 }
114 add_action( 'admin_enqueue_scripts', 'openstation_enqueue_desktop_theme_style', 20 );
115
116 /**
117 * Add `os-desktop-theme-<slug>` to the admin body classes.
118 *
119 * The compiled stylesheet scopes to this class as well as to the
120 * shell root, because toasts / dialogs / tooltips / context menus
121 * mount on `document.body`, outside `#os-shell`.
122 *
123 * `admin_body_class` is a STRING filter — concatenate, never
124 * array-push.
125 *
126 * @param string $classes Space-separated class list.
127 * @return string
128 */
129 function openstation_desktop_theme_body_class( $classes ) {
130 if ( ! openstation_desktop_theme_request_is_themable() ) {
131 return $classes;
132 }
133 $slug = openstation_active_desktop_theme_slug();
134 if ( '' === $slug ) {
135 return $classes;
136 }
137 return trim( $classes . ' os-desktop-theme-' . $slug );
138 }
139 add_filter( 'admin_body_class', 'openstation_desktop_theme_body_class', 20 );
140
141 /**
142 * Inject the desktop-theme bits of the shell config.
143 *
144 * Uses the public `openstation_shell_config` filter rather than
145 * editing the config literal in `includes/render/assets.php`, the
146 * same way the stored-files module contributes `desktopStorage`.
147 *
148 * @param array $config Shell config.
149 * @return array
150 */
151 function openstation_desktop_theme_inject_shell_config( $config ) {
152 $config['canManageDesktopThemes'] = current_user_can( openstation_desktop_theme_upload_capability() );
153 $config['desktopThemesUrl'] = esc_url_raw( rest_url( 'desktop-mode/v1/desktop-themes' ) );
154 return $config;
155 }
156 add_filter( 'openstation_shell_config', 'openstation_desktop_theme_inject_shell_config', 20 );
157