PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.6
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.6
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.6, at includes/widgets/heartbeat.php

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