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

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