| @@ -1,15 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Desktop-icons registry. | |
| 3 | + * OpenStation — Desktop-icons registry. | |
| 4 | 4 | * |
| 5 | 5 | * Owns the registration API + payload builder for the wallpaper- |
| 6 | - * surface app icons (`wp.desktop.icons`). The dock has its own | |
| 6 | + * surface app icons (`wp.os.icons`). The dock has its own | |
| 7 | 7 | * rail; this is the second surface — clickable shortcuts that |
| 8 | 8 | * sit on the desktop wallpaper itself, registered via |
| 9 | - * `desktop_mode_register_icon()` and rendered by | |
| 9 | + * `openstation_register_icon()` and rendered by | |
| 10 | 10 | * `src/desktop-icons.ts`. |
| 11 | 11 | * |
| 12 | + * Not to be confused with `includes/wp-icon-registry.php`, which hands our | |
| 13 | + * eleven pieces of artwork to WordPress's own icon registry for use with | |
| 14 | + * `wp_get_icon()`. This file is about a surface of the desktop. | |
| 15 | + * | |
| 12 | 16 | * Extracted from the 2,101-LOC `components.php` during the |
| 13 | 17 | * architecture-0.8.1 PHP slicing (phase 6). Behaviour is |
| 14 | 18 | * unchanged: every function name, every WP filter, every error |
| 15 | 19 | * code is identical. PHP looks function references up by name at |
| @@ -14,10 +18,9 @@ | ||
| 14 | 18 | * unchanged: every function name, every WP filter, every error |
| 15 | 19 | * code is identical. PHP looks function references up by name at |
| 16 | 20 | * hook-fire time, so existing call sites resolve from any module. |
| 17 | 21 | * |
| 18 | - * @package Desktop_Mode | |
| 19 | - * @since 0.8.1 | |
| 22 | + * @package OpenStation | |
| 20 | 23 | */ |
| 21 | 24 | |
| 22 | 25 | defined( 'ABSPATH' ) || exit; |
| 23 | 26 | |
| @@ -35,10 +38,10 @@ | ||
| 35 | 38 | * |
| 36 | 39 | * Example — the classic Jorvy recipe: |
| 37 | 40 | * |
| 38 | 41 | * ```php |
| 39 | - * desktop_mode_register_window( 'jorvy', array( …window args… ) ); | |
| 40 | - * desktop_mode_register_icon( 'jorvy', array( | |
| 42 | + * openstation_register_window( 'jorvy', array( …window args… ) ); | |
| 43 | + * openstation_register_icon( 'jorvy', array( | |
| 41 | 44 | * 'title' => __( 'Jorvy', 'jorvy' ), |
| 42 | 45 | * 'icon' => 'dashicons-star-filled', |
| 43 | 46 | * 'window' => 'jorvy', |
| 44 | 47 | * 'position' => 10, |
| @@ -44,10 +47,8 @@ | ||
| 44 | 47 | * 'position' => 10, |
| 45 | 48 | * ) ); |
| 46 | 49 | * ``` |
| 47 | 50 | * |
| 48 | - * @since 0.11.0 | |
| 49 | - * | |
| 50 | 51 | * @param string $id Icon id. Must be a kebab-case-ish slug. |
| 51 | 52 | * @param array $args { |
| 52 | 53 | * Icon registration options. |
| 53 | 54 | * |
| @@ -65,10 +66,9 @@ | ||
| 65 | 66 | * URI and routes the result through the |
| 66 | 67 | * same sanitizer as `icon`. Wins over |
| 67 | 68 | * `icon` when both are supplied. Markup |
| 68 | 69 | * containing a `<script>` tag is rejected |
| 69 | - * with `desktop_mode_invalid_icon_svg`. | |
| 70 | - * @since 0.8.2 | |
| 70 | + * with `openstation_invalid_icon_svg`. | |
| 71 | 71 | * @type string $window Id of a registered native window to |
| 72 | 72 | * open on click. Mutually exclusive |
| 73 | 73 | * with `url`. |
| 74 | 74 | * @type string $url URL to open on click (admin URLs |
| @@ -81,20 +81,20 @@ | ||
| 81 | 81 | * any unpinned icon regardless of |
| 82 | 82 | * `position`, and is never user- |
| 83 | 83 | * draggable. Reserved for built-in |
| 84 | 84 | * shortcuts like "My WordPress". |
| 85 | - * Default `false`. @since 0.8.0 | |
| 85 | + * Default `false`. | |
| 86 | 86 | * @type string[] $capabilities Gate: ALL caps must match. Any |
| 87 | 87 | * missed cap returns |
| 88 | - * `WP_Error desktop_mode_capability_denied`. | |
| 88 | + * `WP_Error openstation_capability_denied`. | |
| 89 | 89 | * } |
| 90 | 90 | * @return true|WP_Error `true` on success; `WP_Error` otherwise. |
| 91 | 91 | */ |
| 92 | -function desktop_mode_register_icon( $id, $args = array() ) { | |
| 92 | +function openstation_register_icon( $id, $args = array() ) { | |
| 93 | 93 | $id = sanitize_key( (string) $id ); |
| 94 | 94 | if ( '' === $id ) { |
| 95 | - return desktop_mode_registration_error( | |
| 96 | - 'desktop_mode_missing_id', | |
| 95 | + return openstation_registration_error( | |
| 96 | + 'openstation_missing_id', | |
| 97 | 97 | __( 'Desktop icon id is required and must be a valid slug.', 'desktop-mode' ) |
| 98 | 98 | ); |
| 99 | 99 | } |
| 100 | 100 | |
| @@ -107,9 +107,9 @@ | ||
| 107 | 107 | 'position' => 100, |
| 108 | 108 | 'pinned' => false, |
| 109 | 109 | 'capabilities' => array(), |
| 110 | 110 | ); |
| 111 | - $args = wp_parse_args( $args, $defaults ); | |
| 111 | + $args = wp_parse_args( $args, $defaults ); | |
| 112 | 112 | |
| 113 | 113 | $svg = trim( (string) $args['icon_svg'] ); |
| 114 | 114 | if ( '' !== $svg ) { |
| 115 | 115 | // Reject markup that contains a script tag outright. The data |
| @@ -116,17 +116,17 @@ | ||
| 116 | 116 | // URI is consumed via `<img src=…>` in the browser (which |
| 117 | 117 | // sandboxes scripts inside SVG), but defence-in-depth catches |
| 118 | 118 | // callers who paste an SVG harvested from an untrusted source. |
| 119 | 119 | if ( false !== stripos( $svg, '<script' ) ) { |
| 120 | - return desktop_mode_registration_error( | |
| 121 | - 'desktop_mode_invalid_icon_svg', | |
| 120 | + return openstation_registration_error( | |
| 121 | + 'openstation_invalid_icon_svg', | |
| 122 | 122 | __( 'Desktop icon `icon_svg` must not contain a <script> tag.', 'desktop-mode' ), |
| 123 | 123 | array( 'id' => $id ) |
| 124 | 124 | ); |
| 125 | 125 | } |
| 126 | 126 | if ( 0 !== stripos( ltrim( $svg ), '<svg' ) ) { |
| 127 | - return desktop_mode_registration_error( | |
| 128 | - 'desktop_mode_invalid_icon_svg', | |
| 127 | + return openstation_registration_error( | |
| 128 | + 'openstation_invalid_icon_svg', | |
| 129 | 129 | __( 'Desktop icon `icon_svg` must start with a <svg> root element.', 'desktop-mode' ), |
| 130 | 130 | array( 'id' => $id ) |
| 131 | 131 | ); |
| 132 | 132 | } |
| @@ -134,23 +134,26 @@ | ||
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | foreach ( (array) $args['capabilities'] as $cap ) { |
| 137 | 137 | if ( ! current_user_can( (string) $cap ) ) { |
| 138 | - return desktop_mode_registration_error( | |
| 139 | - 'desktop_mode_capability_denied', | |
| 138 | + return openstation_registration_error( | |
| 139 | + 'openstation_capability_denied', | |
| 140 | 140 | sprintf( |
| 141 | 141 | /* translators: %s: capability slug. */ |
| 142 | 142 | __( 'Current user lacks the %s capability required to register this desktop icon.', 'desktop-mode' ), |
| 143 | 143 | (string) $cap |
| 144 | 144 | ), |
| 145 | - array( 'capability' => (string) $cap, 'id' => $id ) | |
| 145 | + array( | |
| 146 | + 'capability' => (string) $cap, | |
| 147 | + 'id' => $id, | |
| 148 | + ) | |
| 146 | 149 | ); |
| 147 | 150 | } |
| 148 | 151 | } |
| 149 | 152 | |
| 150 | 153 | if ( '' === (string) $args['title'] ) { |
| 151 | - return desktop_mode_registration_error( | |
| 152 | - 'desktop_mode_missing_title', | |
| 154 | + return openstation_registration_error( | |
| 155 | + 'openstation_missing_title', | |
| 153 | 156 | __( 'Desktop icon registration requires a non-empty `title`.', 'desktop-mode' ), |
| 154 | 157 | array( 'id' => $id ) |
| 155 | 158 | ); |
| 156 | 159 | } |
| @@ -157,17 +160,17 @@ | ||
| 157 | 160 | |
| 158 | 161 | $window = sanitize_key( (string) $args['window'] ); |
| 159 | 162 | $url = (string) $args['url']; |
| 160 | 163 | if ( '' !== $window && '' !== $url ) { |
| 161 | - return desktop_mode_registration_error( | |
| 162 | - 'desktop_mode_conflicting_target', | |
| 164 | + return openstation_registration_error( | |
| 165 | + 'openstation_conflicting_target', | |
| 163 | 166 | __( 'Desktop icon cannot declare both `window` and `url`; pick one target.', 'desktop-mode' ), |
| 164 | 167 | array( 'id' => $id ) |
| 165 | 168 | ); |
| 166 | 169 | } |
| 167 | 170 | if ( '' === $window && '' === $url ) { |
| 168 | - return desktop_mode_registration_error( | |
| 169 | - 'desktop_mode_missing_target', | |
| 171 | + return openstation_registration_error( | |
| 172 | + 'openstation_missing_target', | |
| 170 | 173 | __( 'Desktop icon must declare a `window` id or a `url` target.', 'desktop-mode' ), |
| 171 | 174 | array( 'id' => $id ) |
| 172 | 175 | ); |
| 173 | 176 | } |
| @@ -176,10 +179,10 @@ | ||
| 176 | 179 | // desktop window; off-site URLs open in a new browser tab at |
| 177 | 180 | // click time (shell decides). |
| 178 | 181 | $url = esc_url_raw( $url, array( 'http', 'https' ) ); |
| 179 | 182 | if ( '' === $url ) { |
| 180 | - return desktop_mode_registration_error( | |
| 181 | - 'desktop_mode_invalid_url', | |
| 183 | + return openstation_registration_error( | |
| 184 | + 'openstation_invalid_url', | |
| 182 | 185 | __( 'Desktop icon `url` must be a valid http(s) URL.', 'desktop-mode' ), |
| 183 | 186 | array( 'id' => $id ) |
| 184 | 187 | ); |
| 185 | 188 | } |
| @@ -187,29 +190,27 @@ | ||
| 187 | 190 | |
| 188 | 191 | $entry = array( |
| 189 | 192 | 'id' => $id, |
| 190 | 193 | 'title' => (string) $args['title'], |
| 191 | - 'icon' => desktop_mode_sanitize_dock_icon( (string) $args['icon'] ), | |
| 194 | + 'icon' => openstation_sanitize_dock_icon( (string) $args['icon'] ), | |
| 192 | 195 | 'window' => $window, |
| 193 | 196 | 'url' => $url, |
| 194 | 197 | 'position' => (int) $args['position'], |
| 195 | 198 | 'pinned' => (bool) $args['pinned'], |
| 196 | 199 | ); |
| 197 | - desktop_mode_desktop_icon_registry( $id, $entry ); | |
| 200 | + openstation_desktop_icon_registry( $id, $entry ); | |
| 198 | 201 | |
| 199 | 202 | /** |
| 200 | 203 | * Fires after a desktop icon is successfully registered. |
| 201 | 204 | * |
| 202 | - * Does NOT fire when `desktop_mode_register_icon()` returns a | |
| 205 | + * Does NOT fire when `openstation_register_icon()` returns a | |
| 203 | 206 | * `WP_Error`. |
| 204 | 207 | * |
| 205 | - * @since 0.11.0 | |
| 206 | - * | |
| 207 | 208 | * @param string $id The icon id. |
| 208 | 209 | * @param array $entry The stored registry entry (id, title, |
| 209 | - * icon, window, url, position). | |
| 210 | + * icon, window, url, position, pinned). | |
| 210 | 211 | */ |
| 211 | - do_action( 'desktop_mode_icon_registered', $id, $entry ); | |
| 212 | + do_action( 'openstation_icon_registered', $id, $entry ); | |
| 212 | 213 | |
| 213 | 214 | return true; |
| 214 | 215 | } |
| 215 | 216 | |
| @@ -214,15 +215,14 @@ | ||
| 214 | 215 | } |
| 215 | 216 | |
| 216 | 217 | /** |
| 217 | 218 | * Internal module-level registry for desktop icons registered via |
| 218 | - * {@see desktop_mode_register_icon()}. Same static-store pattern as | |
| 219 | + * {@see openstation_register_icon()}. Same static-store pattern as | |
| 219 | 220 | * the widget + native-window + wallpaper registries. |
| 220 | 221 | * |
| 221 | - * @since 0.11.0 | |
| 222 | 222 | * @internal |
| 223 | 223 | */ |
| 224 | -function desktop_mode_desktop_icon_registry( $id = '', $entry = null ) { | |
| 224 | +function openstation_desktop_icon_registry( $id = '', $entry = null ) { | |
| 225 | 225 | static $store = array(); |
| 226 | 226 | |
| 227 | 227 | if ( '' === (string) $id ) { |
| 228 | 228 | return $store; |
| @@ -227,9 +227,9 @@ | ||
| 227 | 227 | if ( '' === (string) $id ) { |
| 228 | 228 | return $store; |
| 229 | 229 | } |
| 230 | 230 | // Sentinel write: passing the literal string `__unset__` removes |
| 231 | - // the entry. Used by `desktop_mode_unregister_icon()` and by | |
| 231 | + // the entry. Used by `openstation_unregister_icon()` and by | |
| 232 | 232 | // PHPUnit teardowns; lets us clear test-only registrations |
| 233 | 233 | // without exposing the static `$store` directly. |
| 234 | 234 | if ( '__unset__' === $entry ) { |
| 235 | 235 | unset( $store[ $id ] ); |
| @@ -242,38 +242,34 @@ | ||
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
| 245 | 245 | * Remove a previously registered desktop icon from the static |
| 246 | - * registry. Mirror of `desktop_mode_register_icon()` — handy for | |
| 246 | + * registry. Mirror of `openstation_register_icon()` — handy for | |
| 247 | 247 | * plugins that register icons conditionally and need to drop them |
| 248 | 248 | * mid-request, and for PHPUnit teardowns that shouldn't leak |
| 249 | 249 | * registrations into other tests. |
| 250 | 250 | * |
| 251 | - * @since 0.8.0 | |
| 252 | - * | |
| 253 | - * @param string $id Icon id passed to `desktop_mode_register_icon()`. | |
| 251 | + * @param string $id Icon id passed to `openstation_register_icon()`. | |
| 254 | 252 | * @return void |
| 255 | 253 | */ |
| 256 | -function desktop_mode_unregister_icon( $id ) { | |
| 254 | +function openstation_unregister_icon( $id ) { | |
| 257 | 255 | $id = sanitize_key( (string) $id ); |
| 258 | 256 | if ( '' === $id ) { |
| 259 | 257 | return; |
| 260 | 258 | } |
| 261 | - desktop_mode_desktop_icon_registry( $id, '__unset__' ); | |
| 259 | + openstation_desktop_icon_registry( $id, '__unset__' ); | |
| 262 | 260 | } |
| 263 | 261 | |
| 264 | 262 | /** |
| 265 | 263 | * Build the desktop-icon list for the shell payload. Applies a |
| 266 | - * `desktop_mode_icons` filter so plugins can hide / reorder / rename | |
| 264 | + * `openstation_icons` filter so plugins can hide / reorder / rename | |
| 267 | 265 | * entries registered by others — mirrors the wallpaper payload |
| 268 | 266 | * builder's filter discipline. |
| 269 | 267 | * |
| 270 | - * @since 0.11.0 | |
| 271 | - * | |
| 272 | 268 | * @return array[] |
| 273 | 269 | */ |
| 274 | -function desktop_mode_build_desktop_icons_payload() { | |
| 275 | - $registry = desktop_mode_desktop_icon_registry(); | |
| 270 | +function openstation_build_desktop_icons_payload() { | |
| 271 | + $registry = openstation_desktop_icon_registry(); | |
| 276 | 272 | if ( ! is_array( $registry ) || empty( $registry ) ) { |
| 277 | 273 | return array(); |
| 278 | 274 | } |
| 279 | 275 | |
| @@ -279,16 +275,14 @@ | ||
| 279 | 275 | |
| 280 | 276 | /** |
| 281 | 277 | * Filters the full desktop-icon registry before it ships to the |
| 282 | 278 | * shell. Each entry is the shape stored by |
| 283 | - * `desktop_mode_register_icon()` (`id`, `title`, `icon`, `window`, | |
| 284 | - * `url`, `position`). Return a reordered / filtered array. | |
| 279 | + * `openstation_register_icon()` (`id`, `title`, `icon`, `window`, | |
| 280 | + * `url`, `position`, `pinned`). Return a reordered / filtered array. | |
| 285 | 281 | * |
| 286 | - * @since 0.11.0 | |
| 287 | - * | |
| 288 | 282 | * @param array[] $registry The registered icon entries. |
| 289 | 283 | */ |
| 290 | - $registry = apply_filters( 'desktop_mode_icons', $registry ); | |
| 284 | + $registry = apply_filters( 'openstation_icons', $registry ); | |
| 291 | 285 | if ( ! is_array( $registry ) ) { |
| 292 | 286 | return array(); |
| 293 | 287 | } |
| 294 | 288 | |
| @@ -308,18 +302,21 @@ | ||
| 308 | 302 | ); |
| 309 | 303 | } |
| 310 | 304 | |
| 311 | 305 | // Pinned icons first; then by position. Ties break on insertion order. |
| 312 | - usort( $out, static function ( $a, $b ) { | |
| 313 | - $ap = ! empty( $a['pinned'] ) ? 0 : 1; | |
| 314 | - $bp = ! empty( $b['pinned'] ) ? 0 : 1; | |
| 315 | - if ( $ap !== $bp ) { | |
| 316 | - return $ap - $bp; | |
| 306 | + usort( | |
| 307 | + $out, | |
| 308 | + static function ( $a, $b ) { | |
| 309 | + $ap = ! empty( $a['pinned'] ) ? 0 : 1; | |
| 310 | + $bp = ! empty( $b['pinned'] ) ? 0 : 1; | |
| 311 | + if ( $ap !== $bp ) { | |
| 312 | + return $ap - $bp; | |
| 313 | + } | |
| 314 | + if ( $a['position'] === $b['position'] ) { | |
| 315 | + return 0; | |
| 316 | + } | |
| 317 | + return $a['position'] < $b['position'] ? -1 : 1; | |
| 317 | 318 | } |
| 318 | - if ( $a['position'] === $b['position'] ) { | |
| 319 | - return 0; | |
| 320 | - } | |
| 321 | - return $a['position'] < $b['position'] ? -1 : 1; | |
| 322 | - } ); | |
| 319 | + ); | |
| 323 | 320 | |
| 324 | 321 | return $out; |
| 325 | 322 | } |