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 / widgets / heartbeat.php

heartbeat.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.8, at includes/widgets/heartbeat.php

134 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — Heartbeat widget (built-in, lazy-loaded).
4 *
5 * Dogfoods the public `desktop_mode_register_widget()` API for a
6 * built-in widget: the metadata + script handle live here, the
7 * JS + CSS ship as their own Vite bundle
8 * (`assets/js/widget-heartbeat[.min].js` and matching `.css`).
9 * The shell's widgets `server-sync` only loads them when the user
10 * adds the widget or the picker is opened — main bundle keeps
11 * none of the heart's code.
12 *
13 * @package WPDesktopMode
14 */
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Register the JS bundle as a script handle so it can be loaded
20 * lazily via `wp_register_script()` / its URL. The CSS file
21 * emitted by Vite alongside the JS is registered as its own
22 * style handle and eagerly enqueued by
23 * desktop_mode_enqueue_heartbeat_widget_styles() below — the
24 * widget server-sync only injects the JS, so the stylesheet must
25 * ship ahead of time for the chrome to paint with the JS.
26 */
27 function desktop_mode_register_heartbeat_widget_assets() {
28 $suffix = desktop_mode_asset_suffix();
29 $version = defined( 'DESKTOP_MODE_VERSION' ) ? DESKTOP_MODE_VERSION : '0';
30
31 $js_path = DESKTOP_MODE_DIR . 'assets/js/widget-heartbeat' . $suffix . '.js';
32 $css_path = DESKTOP_MODE_DIR . 'assets/js/widget-heartbeat' . $suffix . '.css';
33
34 wp_register_style(
35 'desktop-mode-heartbeat-widget',
36 DESKTOP_MODE_URL . 'assets/js/widget-heartbeat' . $suffix . '.css',
37 array(),
38 file_exists( $css_path ) ? (string) filemtime( $css_path ) : $version
39 );
40 wp_register_script(
41 'desktop-mode-heartbeat-widget',
42 DESKTOP_MODE_URL . 'assets/js/widget-heartbeat' . $suffix . '.js',
43 array( 'wp-hooks' ),
44 file_exists( $js_path ) ? (string) filemtime( $js_path ) : $version,
45 true
46 );
47 }
48 add_action( 'init', 'desktop_mode_register_heartbeat_widget_assets', 5 );
49
50 /**
51 * Register the widget itself. Sizing constraints + chrome metadata
52 * (label / description / icon) live here so the framework knows
53 * the widget exists at picker-render time, before the JS bundle
54 * is even fetched.
55 */
56 function desktop_mode_register_heartbeat_widget() {
57 if ( ! function_exists( 'desktop_mode_register_widget' ) ) {
58 return;
59 }
60 desktop_mode_register_widget( 'desktop-mode/heartbeat', array(
61 'label' => __( 'Heartbeat', 'desktop-mode' ),
62 'description' => __(
63 'A gently beating heart that pulses with the WordPress Heartbeat. The bar fills as the next tick approaches.',
64 'desktop-mode'
65 ),
66 'icon' => 'dashicons-heart',
67 'script' => 'desktop-mode-heartbeat-widget',
68 'movable' => true,
69 'resizable' => false,
70 'min_width' => 310,
71 'max_width' => 310,
72 'min_height' => 230,
73 'max_height' => 230,
74 'default_width' => 310,
75 'default_height' => 230,
76 ) );
77 }
78 add_action( 'init', 'desktop_mode_register_heartbeat_widget', 6 );
79
80 /**
81 * Eagerly enqueue the widget's CSS handle when the current
82 * request runs in Desktop Mode and is not a chromeless iframe
83 * load. The JS bundle stays lazy and loads via the widget
84 * server-sync the first time the picker opens or the widget
85 * mounts.
86 *
87 * Why eager (on shell pages): the shell injects a `<script>` for
88 * the widget at runtime, but there is no matching auto-load for
89 * the stylesheet. A pure-JS CSS injection creates a flash of
90 * unstyled content while the link's stylesheet is still in
91 * flight — long enough for the widget frame's children to render
92 * past the card boundary before the stylesheet's flex layout
93 * kicks in. 1.9 KB ungzipped is small enough to live in the
94 * shell's always-loaded set without measurable cost; the
95 * heavier JS (9 KB + PIXI) stays lazy.
96 *
97 * Why NOT eager in chromeless iframes: they never mount widgets
98 * themselves — sending the stylesheet there is dead weight. Note
99 * that classic-override pages (`?desktop_mode_classic=1`) are
100 * not excluded: they skip the shell but still receive the
101 * (1.9 KB) stylesheet. The
102 * `desktop_mode_heartbeat_widget_eager_css` filter lets a
103 * site owner opt out entirely without forking the plugin.
104 */
105 function desktop_mode_enqueue_heartbeat_widget_styles() {
106 if ( function_exists( 'desktop_mode_is_enabled' ) && ! desktop_mode_is_enabled() ) {
107 return;
108 }
109 // Chromeless requests render content inside an iframe owned
110 // by a shell elsewhere — they never mount widgets themselves.
111 if (
112 function_exists( 'desktop_mode_is_chromeless_request' )
113 && desktop_mode_is_chromeless_request()
114 ) {
115 return;
116 }
117 /**
118 * Whether to enqueue the heartbeat widget's stylesheet on
119 * this request. Defaults to `true` for shell requests in
120 * Desktop Mode. Sites that never plan to ship the heartbeat
121 * widget can return `false` and save the ~0.66 KB gzipped
122 * stylesheet roundtrip.
123 *
124 * @param bool $eager Default `true` once the chromeless +
125 * desktop-mode gates above have passed.
126 */
127 $eager = (bool) apply_filters( 'desktop_mode_heartbeat_widget_eager_css', true );
128 if ( ! $eager ) {
129 return;
130 }
131 wp_enqueue_style( 'desktop-mode-heartbeat-widget' );
132 }
133 add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_heartbeat_widget_styles', 20 );
134