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

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