PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.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 / ai-copilot / platform-settings.php

platform-settings.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.8.7, at includes/ai-copilot/platform-settings.php

204 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — AI Copilot platform-wide settings.
4 *
5 * Stores site-level AI configuration in `wp_options` so every user and
6 * every background job (hooks, cron, WP-CLI) can use the same API key
7 * without requiring each individual user to configure one.
8 *
9 * Priority when resolving which key to use:
10 * 1. Per-user key (user meta) — personal override, takes precedence.
11 * 2. Platform key (wp_options) — site-wide fallback.
12 *
13 * The REST endpoint is gated behind `manage_options` so only admins
14 * can read or update the platform key.
15 *
16 * @package WPDesktopMode
17 */
18
19 defined( 'ABSPATH' ) || exit;
20
21 /** wp_options key for the platform-wide AI settings. */
22 const DESKTOP_MODE_AI_PLATFORM_OPTION = 'desktop_mode_ai_platform';
23
24 // ---------------------------------------------------------------------------
25 // Get / save
26 // ---------------------------------------------------------------------------
27
28 /**
29 * Returns the platform AI settings, with defaults merged in.
30 *
31 * @since 0.14.0
32 *
33 * @return array{ enabled: bool, provider: string, apiKey: string }
34 */
35 function desktop_mode_ai_get_platform_settings() {
36 $defaults = array(
37 'enabled' => false,
38 'provider' => 'openai',
39 'apiKey' => '', // Legacy — kept for backwards compat with 0.14–0.17 callers.
40 'apiKeys' => array(), // Per-provider keys.
41 );
42
43 $raw = get_option( DESKTOP_MODE_AI_PLATFORM_OPTION, array() );
44 if ( ! is_array( $raw ) ) {
45 return $defaults;
46 }
47
48 $provider = $defaults['provider'];
49 if ( isset( $raw['provider'] ) && is_string( $raw['provider'] ) ) {
50 $slug = sanitize_key( $raw['provider'] );
51 if ( '' !== $slug ) {
52 $provider = $slug;
53 }
54 }
55
56 $keys = array();
57 if ( isset( $raw['apiKeys'] ) && is_array( $raw['apiKeys'] ) ) {
58 foreach ( $raw['apiKeys'] as $pid => $val ) {
59 if ( count( $keys ) >= 32 ) {
60 break;
61 }
62 $slug = sanitize_key( (string) $pid );
63 if ( '' === $slug || ! is_string( $val ) ) {
64 continue;
65 }
66 $keys[ $slug ] = $val;
67 }
68 }
69
70 return array(
71 'enabled' => ! empty( $raw['enabled'] ),
72 'provider' => $provider,
73 'apiKey' => ( isset( $raw['apiKey'] ) && is_string( $raw['apiKey'] ) )
74 ? $raw['apiKey']
75 : $defaults['apiKey'],
76 'apiKeys' => $keys,
77 );
78 }
79
80 /**
81 * Sanitizes and saves platform AI settings to `wp_options`.
82 *
83 * @since 0.14.0
84 *
85 * @param mixed $raw Incoming settings payload (from REST or direct call).
86 * @return bool True on success.
87 */
88 function desktop_mode_ai_save_platform_settings( $raw ) {
89 if ( ! is_array( $raw ) ) {
90 return false;
91 }
92
93 $provider = 'openai';
94 if ( isset( $raw['provider'] ) && is_string( $raw['provider'] ) ) {
95 $slug = sanitize_key( $raw['provider'] );
96 if ( '' !== $slug ) {
97 $provider = $slug;
98 }
99 }
100
101 $keys = array();
102 if ( isset( $raw['apiKeys'] ) && is_array( $raw['apiKeys'] ) ) {
103 foreach ( $raw['apiKeys'] as $pid => $val ) {
104 if ( count( $keys ) >= 32 ) {
105 break;
106 }
107 $slug = sanitize_key( (string) $pid );
108 if ( '' === $slug || ! is_string( $val ) ) {
109 continue;
110 }
111 $keys[ $slug ] = substr( sanitize_text_field( $val ), 0, 512 );
112 }
113 }
114
115 $clean = array(
116 'enabled' => ! empty( $raw['enabled'] ),
117 'provider' => $provider,
118 'apiKey' => ( isset( $raw['apiKey'] ) && is_string( $raw['apiKey'] ) )
119 ? substr( sanitize_text_field( $raw['apiKey'] ), 0, 512 )
120 : '',
121 'apiKeys' => $keys,
122 );
123
124 return update_option( DESKTOP_MODE_AI_PLATFORM_OPTION, $clean, false );
125 }
126
127 // ---------------------------------------------------------------------------
128 // REST endpoint
129 // ---------------------------------------------------------------------------
130
131 /**
132 * Registers the platform settings REST route.
133 *
134 * @since 0.14.0
135 */
136 function desktop_mode_register_ai_platform_settings_rest_route() {
137 register_rest_route(
138 'desktop-mode/v1',
139 '/ai/platform-settings',
140 array(
141 array(
142 'methods' => WP_REST_Server::READABLE,
143 'callback' => 'desktop_mode_rest_get_ai_platform_settings',
144 'permission_callback' => 'desktop_mode_rest_ai_platform_permission',
145 ),
146 array(
147 'methods' => WP_REST_Server::CREATABLE,
148 'callback' => 'desktop_mode_rest_save_ai_platform_settings',
149 'permission_callback' => 'desktop_mode_rest_ai_platform_permission',
150 'args' => array(
151 'settings' => array(
152 'required' => true,
153 'type' => 'object',
154 ),
155 ),
156 ),
157 )
158 );
159 }
160 add_action( 'rest_api_init', 'desktop_mode_register_ai_platform_settings_rest_route' );
161
162 /**
163 * Permission: administrators only.
164 *
165 * @since 0.14.0
166 *
167 * @return bool|WP_Error
168 */
169 function desktop_mode_rest_ai_platform_permission() {
170 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
171 return new WP_Error(
172 'desktop_mode_ai_forbidden',
173 'Only administrators can manage platform AI settings.',
174 array( 'status' => 403 )
175 );
176 }
177 return true;
178 }
179
180 /**
181 * GET /desktop-mode/v1/ai/platform-settings
182 *
183 * @since 0.14.0
184 *
185 * @return WP_REST_Response
186 */
187 function desktop_mode_rest_get_ai_platform_settings() {
188 return rest_ensure_response( desktop_mode_ai_get_platform_settings() );
189 }
190
191 /**
192 * POST /desktop-mode/v1/ai/platform-settings
193 *
194 * @since 0.14.0
195 *
196 * @param WP_REST_Request $request
197 * @return WP_REST_Response
198 */
199 function desktop_mode_rest_save_ai_platform_settings( WP_REST_Request $request ) {
200 $payload = $request->get_param( 'settings' );
201 desktop_mode_ai_save_platform_settings( $payload );
202 return rest_ensure_response( desktop_mode_ai_get_platform_settings() );
203 }
204