PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.5
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.5
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 / extended-options.php

extended-options.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.5, at includes/extended-options.php

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