| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — Widgets registry. |
| 4 |
* |
| 5 |
* Server-side registration API + payload builder + asset enqueue |
| 6 |
* for the right-column widget layer. Plugin-side JS publishes |
| 7 |
* the full widget def on `window.desktopModeWidgets[ id ]`; this |
| 8 |
* module is the PHP side that announces them and ships their |
| 9 |
* script handles into the boot payload. |
| 10 |
* |
| 11 |
* Extracted from `components.php` during the architecture-0.8.1 |
| 12 |
* PHP slicing (phase 6). |
| 13 |
* |
| 14 |
* @package Desktop_Mode |
| 15 |
* @since 0.8.1 |
| 16 |
*/ |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Register a server-side desktop widget. Symmetric to |
| 22 |
* {@see desktop_mode_register_window()} for the right-column widget |
| 23 |
* layer: plugin declares the widget's metadata + script handle in |
| 24 |
* PHP; shell syncs its registry from the live payload so |
| 25 |
* activation / deactivation map to picker add / remove without a |
| 26 |
* browser reload. |
| 27 |
* |
| 28 |
* The mount callback still lives in JS — not serializable across |
| 29 |
* the wire. Plugins register it on |
| 30 |
* `window.desktopModeWidgets[ <id> ]` as a `(container, ctx) => |
| 31 |
* teardown` function. The shell reads that global once the |
| 32 |
* declared script loads and wraps it into a WidgetDef. |
| 33 |
* |
| 34 |
* Example: |
| 35 |
* |
| 36 |
* ```php |
| 37 |
* desktop_mode_register_widget( 'myplugin/stats', array( |
| 38 |
* 'label' => __( 'Stats', 'my-plugin' ), |
| 39 |
* 'description' => __( 'Live analytics rollup', 'my-plugin' ), |
| 40 |
* 'icon' => 'dashicons-chart-bar', |
| 41 |
* 'script' => 'my-plugin-desktop-widgets', |
| 42 |
* 'movable' => true, |
| 43 |
* 'resizable' => true, |
| 44 |
* 'default_width' => 280, |
| 45 |
* 'default_height' => 180, |
| 46 |
* ) ); |
| 47 |
* ``` |
| 48 |
* |
| 49 |
* ```js |
| 50 |
* // Inside my-plugin-desktop-widgets.js: |
| 51 |
* window.desktopModeWidgets = window.desktopModeWidgets || {}; |
| 52 |
* window.desktopModeWidgets[ 'myplugin/stats' ] = function ( container, ctx ) { |
| 53 |
* container.append( buildDOM() ); |
| 54 |
* return function teardown() { }; |
| 55 |
* }; |
| 56 |
* ``` |
| 57 |
* |
| 58 |
* @since 0.10.0 |
| 59 |
* @since 0.11.0 Returns `WP_Error` on validation failure instead of |
| 60 |
* silent `false`. Legacy `if ( $result )` callers remain |
| 61 |
* correct because `WP_Error` is truthy. |
| 62 |
* |
| 63 |
* @param string $id Widget id. Must match the key the JS side |
| 64 |
* uses on `window.desktopModeWidgets[ … ]`. |
| 65 |
* @param array $args { |
| 66 |
* @type string $label Human-readable picker label. Required. |
| 67 |
* @type string $description Picker subtitle. Default empty. |
| 68 |
* @type string $icon Dashicons class for the picker. Required. |
| 69 |
* @type string $script Enqueued script handle that owns |
| 70 |
* the mount callback. Required. |
| 71 |
* @type bool $movable Allow drag out of the right column. |
| 72 |
* @type bool $resizable Allow user resize. |
| 73 |
* @type int $min_width |
| 74 |
* @type int $min_height |
| 75 |
* @type int $max_width |
| 76 |
* @type int $max_height |
| 77 |
* @type int $default_width First-mount floating width. |
| 78 |
* @type int $default_height First-mount floating height. |
| 79 |
* @type string[] $capabilities Gate: ALL caps must match. Any |
| 80 |
* missed cap returns |
| 81 |
* `WP_Error desktop_mode_capability_denied`. |
| 82 |
* } |
| 83 |
* @return true|WP_Error `true` on success; `WP_Error` otherwise. |
| 84 |
*/ |
| 85 |
function desktop_mode_register_widget( $id, $args = array() ) { |
| 86 |
$id = (string) $id; |
| 87 |
if ( '' === $id ) { |
| 88 |
return desktop_mode_registration_error( |
| 89 |
'desktop_mode_missing_id', |
| 90 |
__( 'Widget id is required.', 'desktop-mode' ) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
$defaults = array( |
| 95 |
'label' => '', |
| 96 |
'description' => '', |
| 97 |
'icon' => 'dashicons-admin-generic', |
| 98 |
'script' => '', |
| 99 |
'movable' => false, |
| 100 |
'resizable' => false, |
| 101 |
'min_width' => 0, |
| 102 |
'min_height' => 0, |
| 103 |
'max_width' => 0, |
| 104 |
'max_height' => 0, |
| 105 |
'default_width' => 0, |
| 106 |
'default_height' => 0, |
| 107 |
'capabilities' => array(), |
| 108 |
); |
| 109 |
$args = wp_parse_args( $args, $defaults ); |
| 110 |
|
| 111 |
foreach ( (array) $args['capabilities'] as $cap ) { |
| 112 |
if ( ! current_user_can( (string) $cap ) ) { |
| 113 |
return desktop_mode_registration_error( |
| 114 |
'desktop_mode_capability_denied', |
| 115 |
sprintf( |
| 116 |
/* translators: %s: capability slug. */ |
| 117 |
__( 'Current user lacks the %s capability required to register this widget.', 'desktop-mode' ), |
| 118 |
(string) $cap |
| 119 |
), |
| 120 |
array( 'capability' => (string) $cap, 'id' => $id ) |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Required fields. The script handle isn't strictly required — |
| 126 |
// a plugin could register a widget whose mount callback is |
| 127 |
// declared on the shell page's own JS (edge case; still valid). |
| 128 |
if ( '' === (string) $args['label'] ) { |
| 129 |
return desktop_mode_registration_error( |
| 130 |
'desktop_mode_missing_label', |
| 131 |
__( 'Widget registration requires a non-empty `label`.', 'desktop-mode' ), |
| 132 |
array( 'id' => $id ) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
$entry = array( |
| 137 |
'id' => $id, |
| 138 |
'label' => (string) $args['label'], |
| 139 |
'description' => (string) $args['description'], |
| 140 |
'icon' => (string) $args['icon'], |
| 141 |
'script' => (string) $args['script'], |
| 142 |
'movable' => (bool) $args['movable'], |
| 143 |
'resizable' => (bool) $args['resizable'], |
| 144 |
'min_width' => (int) $args['min_width'], |
| 145 |
'min_height' => (int) $args['min_height'], |
| 146 |
'max_width' => (int) $args['max_width'], |
| 147 |
'max_height' => (int) $args['max_height'], |
| 148 |
'default_width' => (int) $args['default_width'], |
| 149 |
'default_height' => (int) $args['default_height'], |
| 150 |
); |
| 151 |
desktop_mode_desktop_widget_registry( $id, $entry ); |
| 152 |
|
| 153 |
/** |
| 154 |
* Fires after a desktop widget is successfully registered. |
| 155 |
* |
| 156 |
* Does NOT fire when `desktop_mode_register_widget()` returns a |
| 157 |
* `WP_Error`. |
| 158 |
* |
| 159 |
* @since 0.11.0 |
| 160 |
* |
| 161 |
* @param string $id The widget id. |
| 162 |
* @param array $entry The stored registry entry. |
| 163 |
*/ |
| 164 |
do_action( 'desktop_mode_widget_registered', $id, $entry ); |
| 165 |
|
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Internal module-level registry for widgets registered via |
| 171 |
* {@see desktop_mode_register_widget()}. Same pattern as |
| 172 |
* {@see desktop_mode_native_window_registry()}. |
| 173 |
* |
| 174 |
* @since 0.10.0 |
| 175 |
* @internal |
| 176 |
*/ |
| 177 |
function desktop_mode_desktop_widget_registry( $id = '', $entry = null ) { |
| 178 |
static $store = array(); |
| 179 |
|
| 180 |
if ( '' === (string) $id ) { |
| 181 |
return $store; |
| 182 |
} |
| 183 |
if ( null !== $entry ) { |
| 184 |
$store[ $id ] = $entry; |
| 185 |
} |
| 186 |
return isset( $store[ $id ] ) ? $store[ $id ] : null; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Build the widget list for the shell payload. Runs through |
| 191 |
* every entry registered via `desktop_mode_register_widget()` and |
| 192 |
* attaches the resolved script URL (`wp_scripts()` lookup) so |
| 193 |
* the shell can dynamically inject the script on mid-session |
| 194 |
* plugin activation. |
| 195 |
* |
| 196 |
* @since 0.10.0 |
| 197 |
* |
| 198 |
* @return array[] |
| 199 |
*/ |
| 200 |
function desktop_mode_build_desktop_widgets_payload() { |
| 201 |
$registry = desktop_mode_desktop_widget_registry(); |
| 202 |
if ( ! is_array( $registry ) || empty( $registry ) ) { |
| 203 |
return array(); |
| 204 |
} |
| 205 |
|
| 206 |
$out = array(); |
| 207 |
foreach ( $registry as $entry ) { |
| 208 |
$script_payload = desktop_mode_resolve_script_payload( $entry['script'] ); |
| 209 |
|
| 210 |
$out[] = array( |
| 211 |
'id' => $entry['id'], |
| 212 |
'label' => $entry['label'], |
| 213 |
'description' => $entry['description'], |
| 214 |
'icon' => $entry['icon'], |
| 215 |
'movable' => $entry['movable'], |
| 216 |
'resizable' => $entry['resizable'], |
| 217 |
'minWidth' => $entry['min_width'], |
| 218 |
'minHeight' => $entry['min_height'], |
| 219 |
'maxWidth' => $entry['max_width'], |
| 220 |
'maxHeight' => $entry['max_height'], |
| 221 |
'defaultWidth' => $entry['default_width'], |
| 222 |
'defaultHeight' => $entry['default_height'], |
| 223 |
'scriptUrl' => $script_payload['url'], |
| 224 |
'scriptHandle' => $entry['script'], |
| 225 |
'scriptBefore' => $script_payload['before'], |
| 226 |
'scriptAfter' => $script_payload['after'], |
| 227 |
'scriptL10n' => $script_payload['l10n'], |
| 228 |
'scriptTranslations' => $script_payload['translations'], |
| 229 |
); |
| 230 |
} |
| 231 |
return $out; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Enqueue plugin-registered widget scripts on the shell page so |
| 236 |
* widgets active at boot time have their mount callbacks |
| 237 |
* available without any dynamic-load roundtrip. |
| 238 |
* |
| 239 |
* @since 0.10.0 |
| 240 |
*/ |
| 241 |
function desktop_mode_enqueue_desktop_widget_scripts() { |
| 242 |
if ( ! desktop_mode_is_enabled() || desktop_mode_is_chromeless_request() || desktop_mode_is_classic_request() ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
$registry = desktop_mode_desktop_widget_registry(); |
| 246 |
if ( ! is_array( $registry ) ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
foreach ( $registry as $entry ) { |
| 250 |
if ( ! empty( $entry['script'] ) ) { |
| 251 |
wp_enqueue_script( $entry['script'] ); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_desktop_widget_scripts', 20 ); |
| 256 |
|