PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.8
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.8
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 0.9.8, at includes/extended-options.php

193 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — Platform-wide extended options.
4 *
5 * System-level toggles that enhance the WordPress admin with extra
6 * desktop-mode 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 * `desktop_mode_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 * `desktop_mode_agents_enabled()`).
32 *
33 * @package WPDesktopMode
34 */
35
36 defined( 'ABSPATH' ) || exit;
37
38 /** wp_options key for the extended options bundle. */
39 const DESKTOP_MODE_EXTENDED_OPTIONS_KEY = 'desktop_mode_extended_options';
40
41 // ---------------------------------------------------------------------------
42 // Get / save
43 // ---------------------------------------------------------------------------
44
45 /**
46 * Returns the extended options with defaults filled in.
47 *
48 * @return array{ media_library_enhanced: bool, games: bool, agents: bool }
49 */
50 function desktop_mode_get_extended_options() {
51 $defaults = array(
52 'media_library_enhanced' => true,
53 'games' => false,
54 'agents' => false,
55 );
56 $raw = get_option( DESKTOP_MODE_EXTENDED_OPTIONS_KEY, array() );
57 if ( ! is_array( $raw ) ) {
58 return $defaults;
59 }
60 // Fall back to the default only when the key is ABSENT — so an
61 // admin who explicitly saved `false` stays opted-out even though
62 // the shipped default is now `true`. `! empty()` would wrongly
63 // coerce an explicit `false` back to the default.
64 $clean = array();
65 foreach ( $defaults as $key => $default ) {
66 $clean[ $key ] = array_key_exists( $key, $raw )
67 ? (bool) $raw[ $key ]
68 : $default;
69 }
70 return $clean;
71 }
72
73 /**
74 * Persists extended options to `wp_options`.
75 *
76 * @param mixed $raw Incoming payload.
77 * @return bool
78 */
79 function desktop_mode_save_extended_options( $raw ) {
80 if ( ! is_array( $raw ) ) {
81 return false;
82 }
83 // Merge over the current values so a payload that omits a key (an
84 // older client, a partial save) can't silently reset it.
85 $clean = desktop_mode_get_extended_options();
86 foreach ( $clean as $key => $current ) {
87 if ( array_key_exists( $key, $raw ) ) {
88 $clean[ $key ] = ! empty( $raw[ $key ] );
89 }
90 }
91 return update_option( DESKTOP_MODE_EXTENDED_OPTIONS_KEY, $clean, false );
92 }
93
94 // ---------------------------------------------------------------------------
95 // REST endpoint — admin only
96 // ---------------------------------------------------------------------------
97
98 /**
99 * Registers the extended options REST route.
100 */
101 function desktop_mode_register_extended_options_rest_routes() {
102 register_rest_route(
103 'desktop-mode/v1',
104 '/extended-options',
105 array(
106 array(
107 'methods' => WP_REST_Server::READABLE,
108 'callback' => 'desktop_mode_rest_get_extended_options',
109 'permission_callback' => 'desktop_mode_rest_extended_options_permission',
110 ),
111 array(
112 'methods' => WP_REST_Server::CREATABLE,
113 'callback' => 'desktop_mode_rest_save_extended_options',
114 'permission_callback' => 'desktop_mode_rest_extended_options_permission',
115 'args' => array(
116 'options' => array(
117 'required' => true,
118 'type' => 'object',
119 ),
120 ),
121 ),
122 )
123 );
124 }
125 add_action( 'rest_api_init', 'desktop_mode_register_extended_options_rest_routes' );
126
127 /**
128 * Permission: admins only.
129 *
130 * @return bool|WP_Error
131 */
132 function desktop_mode_rest_extended_options_permission() {
133 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
134 return new WP_Error(
135 'desktop_mode_extended_forbidden',
136 'Only administrators can manage extended options.',
137 array( 'status' => 403 )
138 );
139 }
140 return true;
141 }
142
143 /**
144 * GET /desktop-mode/v1/extended-options
145 *
146 * @return WP_REST_Response
147 */
148 function desktop_mode_rest_get_extended_options() {
149 return rest_ensure_response( desktop_mode_get_extended_options() );
150 }
151
152 /**
153 * POST /desktop-mode/v1/extended-options
154 *
155 * @param WP_REST_Request $request
156 * @return WP_REST_Response
157 */
158 function desktop_mode_rest_save_extended_options( WP_REST_Request $request ) {
159 $payload = $request->get_param( 'options' );
160 desktop_mode_save_extended_options( $payload );
161 return rest_ensure_response( desktop_mode_get_extended_options() );
162 }
163
164 // ---------------------------------------------------------------------------
165 // Media Library enhancement enqueue
166 // ---------------------------------------------------------------------------
167
168 /**
169 * Enqueues the media-library enhancement shim when the admin has
170 * toggled it on. The script is a no-op on pages where wp.media isn't
171 * loaded (it checks at runtime), so there's no harm in enqueuing
172 * globally in the admin.
173 */
174 function desktop_mode_enqueue_media_library_enhancement() {
175 if ( ! is_admin() || ! is_user_logged_in() ) {
176 return;
177 }
178
179 $options = desktop_mode_get_extended_options();
180 if ( empty( $options['media_library_enhanced'] ) ) {
181 return;
182 }
183
184 wp_enqueue_script(
185 'desktop-mode-media-library-enhanced',
186 DESKTOP_MODE_URL . 'assets/js/media-library-enhanced.js',
187 array(),
188 DESKTOP_MODE_VERSION,
189 true
190 );
191 }
192 add_action( 'admin_enqueue_scripts', 'desktop_mode_enqueue_media_library_enhancement', 20 );
193