| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Platform-wide extended options. |
| 4 |
* |
| 5 |
* System-level toggles that enhance the WordPress admin with extra |
| 6 |
* openstation capabilities. Stored in `wp_options` (not per-user) so |
| 7 |
* they affect every user on the site. Admin-only to read or write. |
| 8 |
* |
| 9 |
* Current options: |
| 10 |
* - window_prewarm: preload windows on hover. Defaults to true. |
| 11 |
* - admin_asset_cache: share cached admin assets across windows. |
| 12 |
* Defaults to true. Both performance options apply on shell reload. |
| 13 |
* - media_library_enhanced: when true, enqueues a small JS shim on |
| 14 |
* every admin page that makes Media Library .attachment tiles |
| 15 |
* draggable — users can drag images out of the library into any |
| 16 |
* drop target (text fields, TinyMCE/Gutenberg blocks, custom |
| 17 |
* drop zones) without the library having to be re-built from |
| 18 |
* scratch. **Defaults to `true`** — the enhancement is opt-OUT; |
| 19 |
* sites that want vanilla Media Library behaviour must explicitly |
| 20 |
* toggle it off in OS Settings → Features → Extended options. |
| 21 |
* - games: when true, the games framework loads — Games hub window + |
| 22 |
* desktop icon, built-in games, score/challenge REST routes, the |
| 23 |
* Heartbeat challenge channel, and the schema check. **Defaults to |
| 24 |
* `false`** — games are opt-IN; an admin turns them on in |
| 25 |
* OS Settings → Features → Extended options. While off, |
| 26 |
* `includes/games/bootstrap.php` skips every module file, so the |
| 27 |
* framework consumes no resources at all (see |
| 28 |
* `openstation_games_enabled()`). |
| 29 |
* - agents: when true, the AI Agents framework loads — synthetic |
| 30 |
* agent users, the `/desktop-mode/v1/agents` REST surface, the |
| 31 |
* Agents section in My WordPress, and the Agent chat window. |
| 32 |
* **Defaults to `false`** — agents are opt-IN. While off, |
| 33 |
* `includes/agents/bootstrap.php` skips every module file (see |
| 34 |
* `openstation_agents_enabled()`). |
| 35 |
* - network: when true, the OpenStation Network module loads — the |
| 36 |
* keypair, the identity and list routes, the registry, the Network |
| 37 |
* window, and the hop token that logs a user in on arrival at |
| 38 |
* another install. **Defaults to `false`** — the network is |
| 39 |
* opt-IN. While off, `includes/network/bootstrap.php` skips every |
| 40 |
* module file (see `openstation_network_enabled()`), and a |
| 41 |
* multisite keeps the site switcher it has on its own. |
| 42 |
* |
| 43 |
* @package OpenStation |
| 44 |
*/ |
| 45 |
|
| 46 |
defined( 'ABSPATH' ) || exit; |
| 47 |
|
| 48 |
/** |
| 49 |
* The `wp_options` key for the extended options bundle. |
| 50 |
* |
| 51 |
* The VALUE keeps its pre-rebrand spelling on purpose: it is a |
| 52 |
* persisted or externally-visible identifier, so renaming it would |
| 53 |
* orphan data already written by live installs (or break a live |
| 54 |
* URL). The mismatch between this constant's name and its value is |
| 55 |
* deliberate — it is NOT a half-finished rename. |
| 56 |
*/ |
| 57 |
const OPENSTATION_EXTENDED_OPTIONS_KEY = 'desktop_mode_extended_options'; |
| 58 |
|
| 59 |
// --------------------------------------------------------------------------- |
| 60 |
// Get / save |
| 61 |
// --------------------------------------------------------------------------- |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the extended options with defaults filled in. |
| 65 |
* |
| 66 |
* @return array{ media_library_enhanced: bool, games: bool, agents: bool, network: bool, window_prewarm: bool, admin_asset_cache: bool } |
| 67 |
*/ |
| 68 |
function openstation_get_extended_options() { |
| 69 |
$defaults = array( |
| 70 |
'media_library_enhanced' => true, |
| 71 |
'window_prewarm' => true, |
| 72 |
'admin_asset_cache' => true, |
| 73 |
'games' => false, |
| 74 |
'agents' => false, |
| 75 |
'network' => false, |
| 76 |
); |
| 77 |
$raw = get_option( OPENSTATION_EXTENDED_OPTIONS_KEY, array() ); |
| 78 |
if ( ! is_array( $raw ) ) { |
| 79 |
return $defaults; |
| 80 |
} |
| 81 |
// Fall back to the default only when the key is ABSENT — so an |
| 82 |
// admin who explicitly saved `false` stays opted-out even though |
| 83 |
// the shipped default is now `true`. `! empty()` would wrongly |
| 84 |
// coerce an explicit `false` back to the default. |
| 85 |
$clean = array(); |
| 86 |
foreach ( $defaults as $key => $default ) { |
| 87 |
$clean[ $key ] = array_key_exists( $key, $raw ) |
| 88 |
? (bool) $raw[ $key ] |
| 89 |
: $default; |
| 90 |
} |
| 91 |
return $clean; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Persists extended options to `wp_options`. |
| 96 |
* |
| 97 |
* @param mixed $raw Incoming payload. |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
function openstation_save_extended_options( $raw ) { |
| 101 |
if ( ! is_array( $raw ) ) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
// Merge over the current values so a payload that omits a key (an |
| 105 |
// older client, a partial save) can't silently reset it. |
| 106 |
$clean = openstation_get_extended_options(); |
| 107 |
foreach ( $clean as $key => $current ) { |
| 108 |
if ( array_key_exists( $key, $raw ) ) { |
| 109 |
$clean[ $key ] = ! empty( $raw[ $key ] ); |
| 110 |
} |
| 111 |
} |
| 112 |
return update_option( OPENSTATION_EXTENDED_OPTIONS_KEY, $clean, false ); |
| 113 |
} |
| 114 |
|
| 115 |
// --------------------------------------------------------------------------- |
| 116 |
// REST endpoint — admin only |
| 117 |
// --------------------------------------------------------------------------- |
| 118 |
|
| 119 |
/** |
| 120 |
* Registers the extended options REST route. |
| 121 |
*/ |
| 122 |
function openstation_register_extended_options_rest_routes() { |
| 123 |
register_rest_route( |
| 124 |
'desktop-mode/v1', |
| 125 |
'/extended-options', |
| 126 |
array( |
| 127 |
array( |
| 128 |
'methods' => WP_REST_Server::READABLE, |
| 129 |
'callback' => 'openstation_rest_get_extended_options', |
| 130 |
'permission_callback' => 'openstation_rest_extended_options_permission', |
| 131 |
), |
| 132 |
array( |
| 133 |
'methods' => WP_REST_Server::CREATABLE, |
| 134 |
'callback' => 'openstation_rest_save_extended_options', |
| 135 |
'permission_callback' => 'openstation_rest_extended_options_permission', |
| 136 |
'args' => array( |
| 137 |
'options' => array( |
| 138 |
'required' => true, |
| 139 |
'type' => 'object', |
| 140 |
), |
| 141 |
), |
| 142 |
), |
| 143 |
) |
| 144 |
); |
| 145 |
} |
| 146 |
add_action( 'rest_api_init', 'openstation_register_extended_options_rest_routes' ); |
| 147 |
|
| 148 |
/** |
| 149 |
* Permission: admins only. |
| 150 |
* |
| 151 |
* @return bool|WP_Error |
| 152 |
*/ |
| 153 |
function openstation_rest_extended_options_permission() { |
| 154 |
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 155 |
return new WP_Error( |
| 156 |
'openstation_extended_forbidden', |
| 157 |
'Only administrators can manage extended options.', |
| 158 |
array( 'status' => 403 ) |
| 159 |
); |
| 160 |
} |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* GET /desktop-mode/v1/extended-options |
| 166 |
* |
| 167 |
* @return WP_REST_Response |
| 168 |
*/ |
| 169 |
function openstation_rest_get_extended_options() { |
| 170 |
return rest_ensure_response( openstation_get_extended_options() ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* POST /desktop-mode/v1/extended-options |
| 175 |
* |
| 176 |
* @param WP_REST_Request $request |
| 177 |
* @return WP_REST_Response |
| 178 |
*/ |
| 179 |
function openstation_rest_save_extended_options( WP_REST_Request $request ) { |
| 180 |
$payload = $request->get_param( 'options' ); |
| 181 |
openstation_save_extended_options( $payload ); |
| 182 |
return rest_ensure_response( openstation_get_extended_options() ); |
| 183 |
} |
| 184 |
|
| 185 |
// --------------------------------------------------------------------------- |
| 186 |
// Media Library enhancement enqueue |
| 187 |
// --------------------------------------------------------------------------- |
| 188 |
|
| 189 |
/** |
| 190 |
* Enqueues the media-library enhancement shim when the admin has |
| 191 |
* toggled it on. The script is a no-op on pages where wp.media isn't |
| 192 |
* loaded (it checks at runtime), so there's no harm in enqueuing |
| 193 |
* globally in the admin. |
| 194 |
*/ |
| 195 |
function openstation_enqueue_media_library_enhancement() { |
| 196 |
if ( ! is_admin() || ! is_user_logged_in() ) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
$options = openstation_get_extended_options(); |
| 201 |
if ( empty( $options['media_library_enhanced'] ) ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
wp_enqueue_script( |
| 206 |
'os-media-library-enhanced', |
| 207 |
OPENSTATION_URL . 'assets/js/media-library-enhanced.js', |
| 208 |
array(), |
| 209 |
OPENSTATION_VERSION, |
| 210 |
true |
| 211 |
); |
| 212 |
} |
| 213 |
add_action( 'admin_enqueue_scripts', 'openstation_enqueue_media_library_enhancement', 20 ); |
| 214 |
|