| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — My WordPress: window + pinned icon registration. |
| 4 |
* |
| 5 |
* Native window with id `desktop-mode-my-wordpress`, opened from a |
| 6 |
* pinned desktop icon that always sits in the top-left of the grid |
| 7 |
* (`pinned: true`, `position: -1`). The bundle renders a two-pane |
| 8 |
* file-explorer UI with breadcrumb navigation: root shows Posts, |
| 9 |
* Pages, Users, and Media folder tiles, and clicking one drills into |
| 10 |
* an infinite-scroll list of entities with a per-kind preview pane. |
| 11 |
* |
| 12 |
* **The app is called WP Explorer; the module is called my-wordpress.** |
| 13 |
* The display name is the only thing that changed — the window id, the |
| 14 |
* icon id, the script and style handles, the function prefix and every |
| 15 |
* `openstation_my_wordpress_*` filter are the names plugins and stored |
| 16 |
* desktop layouts already reference, so they stay put. See the frozen- |
| 17 |
* values rule in AGENTS.md; renaming any of them here would orphan a |
| 18 |
* user's pinned tile rather than move it. |
| 19 |
* |
| 20 |
* Filterable surface (mirrors the recycle-bin / posts-window modules): |
| 21 |
* |
| 22 |
* - `openstation_my_wordpress_window_args` |
| 23 |
* - `openstation_my_wordpress_icon_args` |
| 24 |
* - `openstation_my_wordpress_user_can_use` |
| 25 |
* - `openstation_my_wordpress_entities` |
| 26 |
* - `openstation_my_wordpress_template_html` |
| 27 |
* |
| 28 |
* @package OpenStation |
| 29 |
*/ |
| 30 |
|
| 31 |
defined( 'ABSPATH' ) || exit; |
| 32 |
|
| 33 |
/** |
| 34 |
* Display name of the app, for the window title and the pinned icon. |
| 35 |
* |
| 36 |
* A file explorer, named after what it explores. The window used to |
| 37 |
* carry the site title instead, on the reasoning that the desktop holds |
| 38 |
* objects rather than a mention of the software you are already standing |
| 39 |
* in — but a site title is what the *root folder* is called, not what |
| 40 |
* the app is called, and the breadcrumb still says it. The two are no |
| 41 |
* longer the same string. |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
function openstation_my_wordpress_app_title() { |
| 46 |
return __( 'WP Explorer', 'desktop-mode' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* The app's icon: a folder wearing the WordPress mark. |
| 51 |
* |
| 52 |
* The window used to wear `dashicons-wordpress`, the double-ringed |
| 53 |
* wp.org mark, which said "WordPress" and nothing about what the app |
| 54 |
* does — every other window here is WordPress too. A folder says |
| 55 |
* "browse", and the single-ringed mark inside it says whose files. |
| 56 |
* |
| 57 |
* Drawn in `currentColor` so both painters mask it: the dock masks |
| 58 |
* image icons to the rail's own glyph colour so a plugin's brand |
| 59 |
* colours cannot break the monochrome rail, and `renderIcon()` masks it |
| 60 |
* to the title bar's text colour. A literal fill would silently turn it |
| 61 |
* back into a background image in one and a solid blob in the other. |
| 62 |
* |
| 63 |
* The folder is outlined rather than filled so the mark inside it stays |
| 64 |
* a mark: on a filled folder, a same-colour mark is invisible. Hand- |
| 65 |
* placed at 64×64, the established canvas for the custom icons here, |
| 66 |
* and held to two shapes because it renders as small as 20px in the |
| 67 |
* dock. The mark's own path is the upstream single-ringed logo, a |
| 68 |
* filled disc with the W knocked out by the nonzero fill rule. |
| 69 |
* |
| 70 |
* On the sizing: the front panel runs `y 19.1 → 52.1` on the |
| 71 |
* centreline, so at stroke 3 the clear height inside it is 30 units. |
| 72 |
* The mark takes 20 of those at scale 1, centred on 35.6, which leaves |
| 73 |
* five units above and below. It used to be scaled 1.15× for 23 units |
| 74 |
* and barely three units of clearance, less than one stroke width, and |
| 75 |
* a disc that close to the panel edge reads as crammed in rather than |
| 76 |
* placed. Horizontal clearance was never the constraint: the panel is |
| 77 |
* 50 units wide against the mark's 20. |
| 78 |
* |
| 79 |
* @return string Raw `<svg>` markup. |
| 80 |
*/ |
| 81 |
function openstation_my_wordpress_icon_svg() { |
| 82 |
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">' |
| 83 |
// The folder: back tab, notch, front panel, all one outline. |
| 84 |
. '<path d="M11 13h12.2a3 3 0 0 1 2.4 1.2l2.8 3.7a3 3 0 0 0 2.4 1.2H53a4 4 0 0 1 4 4v25a4 4 0 0 1-4 4H11a4 4 0 0 1-4-4V17a4 4 0 0 1 4-4z"' |
| 85 |
. ' fill="none" stroke="currentColor" stroke-width="3" stroke-linejoin="round"/>' |
| 86 |
// The WordPress mark, centred on the front panel at r=10. |
| 87 |
. '<g transform="translate(22 25.6) scale(1)">' |
| 88 |
. '<path fill="currentColor" d="M20 10c0-5.51-4.49-10-10-10C4.48 0 0 4.49 0 10c0 5.52 4.48 10 10 10 5.51 0 10-4.48 10-10zM7.78 15.37L4.37 6.22c.55-.02 1.17-.08 1.17-.08.5-.06.44-1.13-.06-1.11 0 0-1.45.11-2.37.11-.18 0-.37 0-.58-.01C4.12 2.69 6.87 1.11 10 1.11c2.33 0 4.45.87 6.05 2.34-.68-.11-1.65.39-1.65 1.58 0 .74.45 1.36.9 2.1.35.61.55 1.36.55 2.46 0 1.49-1.4 5-1.4 5l-3.03-8.37c.54-.02.82-.17.82-.17.5-.05.44-1.25-.06-1.22 0 0-1.44.12-2.38.12-.87 0-2.33-.12-2.33-.12-.5-.03-.56 1.2-.06 1.22l.92.08 1.26 3.41zM17.41 10c.24-.64.74-1.87.43-4.25.7 1.29 1.05 2.71 1.05 4.25 0 3.29-1.73 6.24-4.4 7.78.97-2.59 1.94-5.2 2.92-7.78zM6.1 18.09C3.12 16.65 1.11 13.53 1.11 10c0-1.3.23-2.48.72-3.59C3.25 10.3 4.67 14.2 6.1 18.09zm4.03-6.63l2.58 6.98c-.86.29-1.76.45-2.71.45-.79 0-1.57-.11-2.29-.33.81-2.38 1.62-4.74 2.42-7.1z"/>' |
| 89 |
. '</g>' |
| 90 |
. '</svg>'; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Whether the current user should see My WordPress. |
| 95 |
* |
| 96 |
* Mirrors the recycle-bin gate — anyone who can edit posts can |
| 97 |
* browse posts and pages. |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
function openstation_my_wordpress_user_can_use() { |
| 102 |
$can = current_user_can( 'edit_posts' ); |
| 103 |
|
| 104 |
/** |
| 105 |
* Filter whether the current user can see the My WordPress |
| 106 |
* pinned icon and window. |
| 107 |
* |
| 108 |
* @param bool $can Default: edit_posts capability. |
| 109 |
*/ |
| 110 |
return (bool) apply_filters( 'openstation_my_wordpress_user_can_use', $can ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Build the entity list shipped to the bundle. Posts, Pages, |
| 115 |
* Users, and Media. Future phases add |
| 116 |
* Comments, Tags, Categories, Themes, and Plugins. |
| 117 |
* |
| 118 |
* The optional `kind` field tells the bundle how to render entries |
| 119 |
* of this entity: `'post'` (default) renders the standard |
| 120 |
* title/excerpt/featured-image tile and the rendered-HTML preview; |
| 121 |
* `'user'` renders an avatar + display-name tile and routes to the |
| 122 |
* user dossier preview; `'media'` renders a thumbnail-grid tile and |
| 123 |
* routes to the media preview pane. Plugins extending the entity |
| 124 |
* list with a post-shaped collection can omit the field; user- and |
| 125 |
* media-shaped collections must set `'user'` / `'media'`. |
| 126 |
* The optional `post_type` field specifies the WP post-type |
| 127 |
* slug used for `os.<slug>.changed` cross-window broadcasts. |
| 128 |
* |
| 129 |
* @return array[] Each entry is `array( 'id', 'label', 'icon', |
| 130 |
* 'restPath', 'kind', 'post_type' )`. `restPath` is appended to |
| 131 |
* the `restRoot` config to derive the list URL. |
| 132 |
*/ |
| 133 |
function openstation_my_wordpress_entities() { |
| 134 |
$entities = array( |
| 135 |
array( |
| 136 |
'id' => 'posts', |
| 137 |
'label' => __( 'Posts', 'desktop-mode' ), |
| 138 |
'icon' => 'dashicons-admin-post', |
| 139 |
'restPath' => 'wp/v2/posts', |
| 140 |
'kind' => 'post', |
| 141 |
'post_type' => 'post', |
| 142 |
), |
| 143 |
array( |
| 144 |
'id' => 'pages', |
| 145 |
'label' => __( 'Pages', 'desktop-mode' ), |
| 146 |
'icon' => 'dashicons-admin-page', |
| 147 |
'restPath' => 'wp/v2/pages', |
| 148 |
'kind' => 'post', |
| 149 |
'post_type' => 'page', |
| 150 |
), |
| 151 |
array( |
| 152 |
'id' => 'users', |
| 153 |
'label' => __( 'Users', 'desktop-mode' ), |
| 154 |
'icon' => 'dashicons-admin-users', |
| 155 |
'restPath' => 'wp/v2/users', |
| 156 |
'kind' => 'user', |
| 157 |
), |
| 158 |
array( |
| 159 |
'id' => 'media', |
| 160 |
'label' => __( 'Media', 'desktop-mode' ), |
| 161 |
'icon' => 'dashicons-admin-media', |
| 162 |
'restPath' => 'wp/v2/media', |
| 163 |
'kind' => 'media', |
| 164 |
'post_type' => 'attachment', |
| 165 |
), |
| 166 |
); |
| 167 |
|
| 168 |
/** |
| 169 |
* Filter the list of entity types shown inside the My WordPress |
| 170 |
* window. Each entry must declare `id`, `label`, `icon`, and |
| 171 |
* `restPath`. Returning a reordered or extended array shows up |
| 172 |
* in the bundle on the next render. |
| 173 |
* |
| 174 |
* **Status: Experimental** — the entity descriptor shape may |
| 175 |
* gain fields as new entity kinds land (Comments, Tags, |
| 176 |
* Categories, Themes, Plugins). Stable id/label/icon/restPath |
| 177 |
* fields will continue to work; new optional fields will not |
| 178 |
* break existing consumers. The `kind` field is optional and |
| 179 |
* defaults to `'post'` for back-compat. |
| 180 |
* |
| 181 |
* Optional fields: |
| 182 |
* - `kind` — render strategy (`'post'` default, `'user'`, `'media'`). |
| 183 |
* - `post_type` — WP post-type slug for cross-window broadcast topic `os.<slug>.changed`. |
| 184 |
* - `thumbnails` — set false to keep the section icon on every tile |
| 185 |
* instead of the entity's featured image. |
| 186 |
* - `editAction` — who edits this section's rows. A preview-action id |
| 187 |
* (see `openstation_my_wordpress_preview_actions`) |
| 188 |
* replaces "Open in editor" everywhere — pane button, |
| 189 |
* context-menu open entry, tile double-click. `false` |
| 190 |
* removes every edit affordance (double-click falls |
| 191 |
* back to the detail dossier; the bulk "Edit…" modal |
| 192 |
* is suppressed). Omit for the classic editor. |
| 193 |
* - `group` — folder id this section nests under at the root |
| 194 |
* (null / omitted renders it loose at the root). |
| 195 |
* - `groupLabel` — folder label. `groupIcon`, `groupOrder` follow. |
| 196 |
* |
| 197 |
* @param array[] $entities Default entities. |
| 198 |
*/ |
| 199 |
$filtered = apply_filters( 'openstation_my_wordpress_entities', $entities ); |
| 200 |
return is_array( $filtered ) ? array_values( $filtered ) : $entities; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Render the My WordPress window's static template body. The bundle |
| 205 |
* mounts its UI into `[data-os-my-wordpress-root]`. |
| 206 |
*/ |
| 207 |
function openstation_my_wordpress_render_template() { |
| 208 |
ob_start(); |
| 209 |
?> |
| 210 |
<div class="desktop-mode-my-wordpress" data-os-my-wordpress-root> |
| 211 |
<header data-os-my-wordpress-breadcrumbs></header> |
| 212 |
<div class="os-my-wordpress__body" data-os-my-wordpress-body> |
| 213 |
<div class="os-my-wordpress__loading" data-os-my-wordpress-loading hidden> |
| 214 |
<os-spinner></os-spinner> |
| 215 |
</div> |
| 216 |
</div> |
| 217 |
<div class="os-folder-status-bar" data-os-my-wordpress-status></div> |
| 218 |
</div> |
| 219 |
<?php |
| 220 |
$html = (string) ob_get_clean(); |
| 221 |
|
| 222 |
/** |
| 223 |
* Filter the My WordPress window's template HTML. |
| 224 |
* |
| 225 |
* @param string $html Default template HTML. |
| 226 |
*/ |
| 227 |
$filtered = (string) apply_filters( 'openstation_my_wordpress_template_html', $html ); |
| 228 |
|
| 229 |
$allowed_html = function_exists( 'openstation_native_window_allowed_html' ) |
| 230 |
? openstation_native_window_allowed_html() |
| 231 |
: wp_kses_allowed_html( 'post' ); |
| 232 |
|
| 233 |
echo wp_kses( $filtered, $allowed_html ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Register the native window + the pinned wallpaper icon on `init`, |
| 238 |
* priority 99 — after `components.php` boots the registry, and late |
| 239 |
* enough that every plugin's `register_post_type()` call has already |
| 240 |
* run. The entity list is frozen into the window config here and only |
| 241 |
* emitted later on `admin_enqueue_scripts`, so a CPT registered after |
| 242 |
* this point would never reach the bundle. `previewActions` is the |
| 243 |
* exception: it is re-collected at emit time via |
| 244 |
* `openstation_my_wordpress_refresh_window_config()` below, so the |
| 245 |
* value snapshotted here is only the fallback. |
| 246 |
*/ |
| 247 |
function openstation_my_wordpress_register_window() { |
| 248 |
if ( ! openstation_my_wordpress_user_can_use() ) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
$site_title = openstation_site_title(); |
| 253 |
$app_title = openstation_my_wordpress_app_title(); |
| 254 |
$icon_uri = 'data:image/svg+xml;base64,' . base64_encode( openstation_my_wordpress_icon_svg() ); |
| 255 |
|
| 256 |
$entities = openstation_my_wordpress_entities(); |
| 257 |
|
| 258 |
$window_args = array( |
| 259 |
'title' => $app_title, |
| 260 |
'icon' => $icon_uri, |
| 261 |
'template' => 'openstation_my_wordpress_render_template', |
| 262 |
'script' => 'desktop-mode-my-wordpress', |
| 263 |
// `styles` (companion), not `style`: loads on first open |
| 264 |
// rather than at every shell boot. Declared FIRST so the |
| 265 |
// WooCommerce integration's sheet — appended to this array by |
| 266 |
// `openstation_my_wordpress_woo_window_args()` — lands after |
| 267 |
// it in the head and keeps winning their equal-specificity |
| 268 |
// overrides by source order. |
| 269 |
'styles' => array( 'desktop-mode-my-wordpress' ), |
| 270 |
'width' => 960, |
| 271 |
'height' => 640, |
| 272 |
'min_width' => 640, |
| 273 |
'min_height' => 420, |
| 274 |
'placement' => 'none', |
| 275 |
'config' => array( |
| 276 |
'restRoot' => esc_url_raw( rest_url() ), |
| 277 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 278 |
'editPostUrlBase' => esc_url_raw( admin_url( 'post.php' ) ), |
| 279 |
'editUserUrlBase' => esc_url_raw( admin_url( 'user-edit.php' ) ), |
| 280 |
'siteName' => $site_title, |
| 281 |
'entities' => $entities, |
| 282 |
'groups' => function_exists( 'openstation_my_wordpress_collect_groups' ) |
| 283 |
? openstation_my_wordpress_collect_groups( $entities ) |
| 284 |
: array(), |
| 285 |
'perPage' => 24, |
| 286 |
'mediaPerPage' => 48, |
| 287 |
'previewActions' => function_exists( 'openstation_my_wordpress_collect_preview_actions' ) |
| 288 |
? openstation_my_wordpress_collect_preview_actions() |
| 289 |
: array(), |
| 290 |
), |
| 291 |
); |
| 292 |
|
| 293 |
/** |
| 294 |
* Filter the args used to register the My WordPress native window. |
| 295 |
* |
| 296 |
* @param array $window_args Args passed to `openstation_register_window()`. |
| 297 |
*/ |
| 298 |
$window_args = (array) apply_filters( 'openstation_my_wordpress_window_args', $window_args ); |
| 299 |
|
| 300 |
$registered = openstation_register_window( 'desktop-mode-my-wordpress', $window_args ); |
| 301 |
if ( is_wp_error( $registered ) ) { |
| 302 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 303 |
error_log( '[openstation] My WordPress window registration failed: ' . $registered->get_error_message() ); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
$icon_args = array( |
| 308 |
'title' => $app_title, |
| 309 |
'icon' => $icon_uri, |
| 310 |
'window' => 'desktop-mode-my-wordpress', |
| 311 |
'pinned' => true, |
| 312 |
'position' => -1, |
| 313 |
); |
| 314 |
|
| 315 |
/** |
| 316 |
* Filter the args used to register the My WordPress pinned icon. |
| 317 |
* |
| 318 |
* Removing `pinned` here lets the icon participate in normal |
| 319 |
* sort order — useful for sites that want the shortcut to feel |
| 320 |
* like any other plugin icon. |
| 321 |
* |
| 322 |
* @param array $icon_args Args passed to `openstation_register_icon()`. |
| 323 |
*/ |
| 324 |
$icon_args = (array) apply_filters( 'openstation_my_wordpress_icon_args', $icon_args ); |
| 325 |
|
| 326 |
openstation_register_icon( 'desktop-mode-my-wordpress', $icon_args ); |
| 327 |
} |
| 328 |
add_action( 'init', 'openstation_my_wordpress_register_window', 99 ); |
| 329 |
|
| 330 |
/** |
| 331 |
* Re-collect the preview-action descriptors when the window config is |
| 332 |
* serialized for the browser, replacing the `init` 99 snapshot taken |
| 333 |
* at registration time. |
| 334 |
* |
| 335 |
* The action SCRIPTS were always collected late (`admin_enqueue_scripts` |
| 336 |
* 40, `openstation_my_wordpress_enqueue_preview_action_scripts()`), so |
| 337 |
* a plugin registering its `openstation_my_wordpress_preview_actions` |
| 338 |
* callback after `init` 99 used to get its JS delivered with no |
| 339 |
* descriptor to attach to — the button simply never rendered. Emitting |
| 340 |
* both legs from the same late collection closes that trap: any |
| 341 |
* registration during a normal bootstrap now ships both. |
| 342 |
* |
| 343 |
* The entity list deliberately stays frozen at `init` 99 — see the |
| 344 |
* registration docblock above. |
| 345 |
* |
| 346 |
* @param array $config Window config blob about to be emitted. |
| 347 |
* @param string $window_id Native window id. |
| 348 |
* @return array Config with fresh `previewActions` for our window. |
| 349 |
*/ |
| 350 |
function openstation_my_wordpress_refresh_window_config( $config, $window_id ) { |
| 351 |
if ( 'desktop-mode-my-wordpress' !== $window_id || ! is_array( $config ) ) { |
| 352 |
return $config; |
| 353 |
} |
| 354 |
if ( function_exists( 'openstation_my_wordpress_collect_preview_actions' ) ) { |
| 355 |
$config['previewActions'] = openstation_my_wordpress_collect_preview_actions(); |
| 356 |
} |
| 357 |
return $config; |
| 358 |
} |
| 359 |
add_filter( 'openstation_native_window_config', 'openstation_my_wordpress_refresh_window_config', 10, 2 ); |
| 360 |
|