PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
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
← All changes | includes/registries/widgets.php +76 -65 0.9.81.1.10 View file →
@@ -1,11 +1,11 @@
1 1 <?php
2 2 /**
3 - * Desktop Mode — Widgets registry.
3 + * OpenStation — Widgets registry.
4 4 *
5 5 * Server-side registration API + payload builder + asset enqueue
6 6 * for the right-column widget layer. Plugin-side JS publishes
7 - * the full widget def on `window.desktopModeWidgets[ id ]`; this
7 + * the full widget def on `window.openStationWidgets[ id ]`; this
8 8 * module is the PHP side that announces them and ships their
9 9 * script handles into the boot payload.
10 10 *
11 11 * Extracted from `components.php` during the architecture-0.8.1
@@ -10,9 +10,9 @@
10 10 *
11 11 * Extracted from `components.php` during the architecture-0.8.1
12 12 * PHP slicing (phase 6).
13 13 *
14 - * @package Desktop_Mode
14 + * @package OpenStation
15 15 */
16 16
17 17 defined( 'ABSPATH' ) || exit;
18 18
@@ -17,9 +17,9 @@
17 17 defined( 'ABSPATH' ) || exit;
18 18
19 19 /**
20 20 * Register a server-side desktop widget. Symmetric to
21 - * {@see desktop_mode_register_window()} for the right-column widget
21 + * {@see openstation_register_window()} for the right-column widget
22 22 * layer: plugin declares the widget's metadata + script handle in
23 23 * PHP; shell syncs its registry from the live payload so
24 24 * activation / deactivation map to picker add / remove without a
25 25 * browser reload.
@@ -25,9 +25,9 @@
25 25 * browser reload.
26 26 *
27 27 * The mount callback still lives in JS — not serializable across
28 28 * the wire. Plugins register it on
29 - * `window.desktopModeWidgets[ <id> ]` as a `(container, ctx) =>
29 + * `window.openStationWidgets[ <id> ]` as a `(container, ctx) =>
30 30 * teardown` function. The shell reads that global once the
31 31 * declared script loads and wraps it into a WidgetDef.
32 32 *
33 33 * Example:
@@ -32,9 +32,9 @@
32 32 *
33 33 * Example:
34 34 *
35 35 * ```php
36 - * desktop_mode_register_widget( 'myplugin/stats', array(
36 + * openstation_register_widget( 'myplugin/stats', array(
37 37 * 'label' => __( 'Stats', 'my-plugin' ),
38 38 * 'description' => __( 'Live analytics rollup', 'my-plugin' ),
39 39 * 'icon' => 'dashicons-chart-bar',
40 40 * 'script' => 'my-plugin-desktop-widgets',
@@ -46,10 +46,10 @@
46 46 * ```
47 47 *
48 48 * ```js
49 49 * // Inside my-plugin-desktop-widgets.js:
50 - * window.desktopModeWidgets = window.desktopModeWidgets || {};
51 - * window.desktopModeWidgets[ 'myplugin/stats' ] = function ( container, ctx ) {
50 + * window.openStationWidgets = window.openStationWidgets || {};
51 + * window.openStationWidgets[ 'myplugin/stats' ] = function ( container, ctx ) {
52 52 * container.append( buildDOM() );
53 53 * return function teardown() { };
54 54 * };
55 55 * ```
@@ -54,9 +54,9 @@
54 54 * };
55 55 * ```
56 56 *
57 57 * @param string $id Widget id. Must match the key the JS side
58 - * uses on `window.desktopModeWidgets[ … ]`.
58 + * uses on `window.openStationWidgets[ … ]`.
59 59 * @param array $args {
60 60 * @type string $label Human-readable picker label. Required.
61 61 * @type string $description Picker subtitle. Default empty.
62 62 * @type string $icon Dashicons class for the picker.
@@ -75,17 +75,17 @@
75 75 * @type int $default_width First-mount floating width.
76 76 * @type int $default_height First-mount floating height.
77 77 * @type string[] $capabilities Gate: ALL caps must match. Any
78 78 * missed cap returns
79 - * `WP_Error desktop_mode_capability_denied`.
79 + * `WP_Error openstation_capability_denied`.
80 80 * }
81 81 * @return true|WP_Error `true` on success; `WP_Error` otherwise.
82 82 */
83 -function desktop_mode_register_widget( $id, $args = array() ) {
83 +function openstation_register_widget( $id, $args = array() ) {
84 84 $id = (string) $id;
85 85 if ( '' === $id ) {
86 - return desktop_mode_registration_error(
87 - 'desktop_mode_missing_id',
86 + return openstation_registration_error(
87 + 'openstation_missing_id',
88 88 __( 'Widget id is required.', 'desktop-mode' )
89 89 );
90 90 }
91 91
@@ -103,20 +103,23 @@
103 103 'default_width' => 0,
104 104 'default_height' => 0,
105 105 'capabilities' => array(),
106 106 );
107 - $args = wp_parse_args( $args, $defaults );
107 + $args = wp_parse_args( $args, $defaults );
108 108
109 109 foreach ( (array) $args['capabilities'] as $cap ) {
110 110 if ( ! current_user_can( (string) $cap ) ) {
111 - return desktop_mode_registration_error(
112 - 'desktop_mode_capability_denied',
111 + return openstation_registration_error(
112 + 'openstation_capability_denied',
113 113 sprintf(
114 114 /* translators: %s: capability slug. */
115 115 __( 'Current user lacks the %s capability required to register this widget.', 'desktop-mode' ),
116 116 (string) $cap
117 117 ),
118 - array( 'capability' => (string) $cap, 'id' => $id )
118 + array(
119 + 'capability' => (string) $cap,
120 + 'id' => $id,
121 + )
119 122 );
120 123 }
121 124 }
122 125
@@ -123,10 +126,10 @@
123 126 // Required fields. The script handle isn't strictly required —
124 127 // a plugin could register a widget whose mount callback is
125 128 // declared on the shell page's own JS (edge case; still valid).
126 129 if ( '' === (string) $args['label'] ) {
127 - return desktop_mode_registration_error(
128 - 'desktop_mode_missing_label',
130 + return openstation_registration_error(
131 + 'openstation_missing_label',
129 132 __( 'Widget registration requires a non-empty `label`.', 'desktop-mode' ),
130 133 array( 'id' => $id )
131 134 );
132 135 }
@@ -145,20 +148,20 @@
145 148 'max_height' => (int) $args['max_height'],
146 149 'default_width' => (int) $args['default_width'],
147 150 'default_height' => (int) $args['default_height'],
148 151 );
149 - desktop_mode_desktop_widget_registry( $id, $entry );
152 + openstation_desktop_widget_registry( $id, $entry );
150 153
151 154 /**
152 155 * Fires after a desktop widget is successfully registered.
153 156 *
154 - * Does NOT fire when `desktop_mode_register_widget()` returns a
157 + * Does NOT fire when `openstation_register_widget()` returns a
155 158 * `WP_Error`.
156 159 *
157 160 * @param string $id The widget id.
158 161 * @param array $entry The stored registry entry.
159 162 */
160 - do_action( 'desktop_mode_widget_registered', $id, $entry );
163 + do_action( 'openstation_widget_registered', $id, $entry );
161 164
162 165 return true;
163 166 }
164 167
@@ -163,14 +166,14 @@
163 166 }
164 167
165 168 /**
166 169 * Internal module-level registry for widgets registered via
167 - * {@see desktop_mode_register_widget()}. Same pattern as
168 - * {@see desktop_mode_native_window_registry()}.
170 + * {@see openstation_register_widget()}. Same pattern as
171 + * {@see openstation_native_window_registry()}.
169 172 *
170 173 * @internal
171 174 */
172 -function desktop_mode_desktop_widget_registry( $id = '', $entry = null ) {
175 +function openstation_desktop_widget_registry( $id = '', $entry = null ) {
173 176 static $store = array();
174 177
175 178 if ( '' === (string) $id ) {
176 179 return $store;
@@ -182,9 +185,9 @@
182 185 }
183 186
184 187 /**
185 188 * Build the widget list for the shell payload. Runs through
186 - * every entry registered via `desktop_mode_register_widget()` and
189 + * every entry registered via `openstation_register_widget()` and
187 190 * attaches the resolved script URL (`wp_scripts()` lookup) so
188 191 * the shell can dynamically inject the script on mid-session
189 192 * plugin activation.
190 193 *
@@ -189,10 +192,10 @@
189 192 * plugin activation.
190 193 *
191 194 * @return array[]
192 195 */
193 -function desktop_mode_build_desktop_widgets_payload() {
194 - $registry = desktop_mode_desktop_widget_registry();
196 +function openstation_build_desktop_widgets_payload() {
197 + $registry = openstation_desktop_widget_registry();
195 198 if ( ! is_array( $registry ) || empty( $registry ) ) {
196 199 return array();
197 200 }
198 201
@@ -197,28 +200,35 @@
197 200 }
198 201
199 202 $out = array();
200 203 foreach ( $registry as $entry ) {
201 - $script_payload = desktop_mode_resolve_script_payload( $entry['script'] );
204 + $script_payload = openstation_resolve_script_payload( $entry['script'] );
202 205
203 206 $out[] = array(
204 - 'id' => $entry['id'],
205 - 'label' => $entry['label'],
206 - 'description' => $entry['description'],
207 - 'icon' => $entry['icon'],
208 - 'movable' => $entry['movable'],
209 - 'resizable' => $entry['resizable'],
210 - 'minWidth' => $entry['min_width'],
211 - 'minHeight' => $entry['min_height'],
212 - 'maxWidth' => $entry['max_width'],
213 - 'maxHeight' => $entry['max_height'],
214 - 'defaultWidth' => $entry['default_width'],
215 - 'defaultHeight' => $entry['default_height'],
216 - 'scriptUrl' => $script_payload['url'],
217 - 'scriptHandle' => $entry['script'],
218 - 'scriptBefore' => $script_payload['before'],
219 - 'scriptAfter' => $script_payload['after'],
220 - 'scriptL10n' => $script_payload['l10n'],
207 + 'id' => $entry['id'],
208 + 'label' => $entry['label'],
209 + 'description' => $entry['description'],
210 + 'icon' => $entry['icon'],
211 + 'movable' => $entry['movable'],
212 + 'resizable' => $entry['resizable'],
213 + 'minWidth' => $entry['min_width'],
214 + 'minHeight' => $entry['min_height'],
215 + 'maxWidth' => $entry['max_width'],
216 + 'maxHeight' => $entry['max_height'],
217 + 'defaultWidth' => $entry['default_width'],
218 + 'defaultHeight' => $entry['default_height'],
219 + 'scriptUrl' => $script_payload['url'],
220 + // The packages this widget declares, in load order.
221 + // WordPress resolves a script's dependencies when it
222 + // enqueues it; a widget bundle is delivered lazily and
223 + // never goes through that, so a widget declaring
224 + // `wp-api-fetch` found `wp.apiFetch` undefined at mount.
225 + // See docs/migration-wp-package-globals.md.
226 + 'scriptDeps' => openstation_resolve_script_dependencies( $entry['script'] ),
227 + 'scriptHandle' => $entry['script'],
228 + 'scriptBefore' => $script_payload['before'],
229 + 'scriptAfter' => $script_payload['after'],
230 + 'scriptL10n' => $script_payload['l10n'],
221 231 'scriptTranslations' => $script_payload['translations'],
222 232 );
223 233 }
224 234 return $out;
@@ -223,24 +233,25 @@
223 233 }
224 234 return $out;
225 235 }
226 236
227 -/**
228 - * Enqueue plugin-registered widget scripts on the shell page so
229 - * widgets active at boot time have their mount callbacks
230 - * available without any dynamic-load roundtrip.
237 +/*
238 + * Widget scripts are NOT enqueued here, and that is deliberate.
239 + *
240 + * Everything the widget picker shows — label, description, icon,
241 + * size constraints — is metadata declared right here in PHP and
242 + * shipped in the boot payload. The only thing a plugin's bundle
243 + * contributes is the `mount` callback, so the shell assembles the
244 + * whole def from the payload and loads the script the first time
245 + * the widget is actually mounted.
246 + *
247 + * A widget the user has never enabled therefore costs a row in the
248 + * picker and nothing else. This file used to `wp_enqueue_script()`
249 + * every registered one on every admin page, which meant the nine
250 + * built-in widget bundles — Drafts at 46 KB, Focus Timer at 41 KB,
251 + * Notes at 31 KB, and the rest — were downloaded and parsed by
252 + * every user whether or not a single widget was on their desktop.
253 + *
254 + * See `src/widgets/server-sync.ts`. `scriptUrl` in the payload
255 + * (built above) is what makes that possible; nothing else on the
256 + * PHP side is involved.
231 257 */
232 -function desktop_mode_enqueue_desktop_widget_scripts() {
233 - if ( ! desktop_mode_is_enabled() || desktop_mode_is_chromeless_request() || desktop_mode_is_classic_request() ) {
234 - return;
235 - }
236 - $registry = desktop_mode_desktop_widget_registry();
237 - if ( ! is_array( $registry ) ) {
238 - return;
239 - }
240 - foreach ( $registry as $entry ) {
241 - if ( ! empty( $entry['script'] ) ) {
242 - wp_enqueue_script( $entry['script'] );
243 - }
244 - }
245 -}
246 -add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_desktop_widget_scripts', 20 );