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

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

129 lines 3.9 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 settings helpers.
4 *
5 * Thin wrappers around `desktop_mode_get_os_settings()` scoped to the AI block.
6 * All other copilot modules call these instead of reading user meta
7 * directly so the key path is one place to change.
8 *
9 * @package WPDesktopMode
10 */
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Returns the AI settings block for a given user.
16 *
17 * @since 0.14.0
18 *
19 * @param int $user_id
20 * @return array{ enabled: bool, provider: string, apiKey: string }
21 */
22 function desktop_mode_ai_get_settings( $user_id ) {
23 $os = desktop_mode_get_os_settings( (int) $user_id );
24 $defaults = array(
25 'enabled' => false,
26 'provider' => 'openai',
27 'apiKey' => '',
28 'apiKeys' => array(),
29 );
30 $ai = isset( $os['ai'] ) && is_array( $os['ai'] ) ? $os['ai'] : array();
31 return array_merge( $defaults, $ai );
32 }
33
34 /**
35 * Whether AI processing is active — checks platform settings first,
36 * then falls back to the user's personal settings.
37 *
38 * Priority:
39 * 1. Platform-wide settings (wp_options) — enabled by any admin.
40 * 2. Per-user settings (user meta) — personal override.
41 *
42 * @since 0.14.0
43 *
44 * @param int $user_id
45 * @return bool
46 */
47 function desktop_mode_ai_is_enabled( $user_id ) {
48 $user_id = (int) $user_id;
49 $platform = desktop_mode_ai_get_platform_settings();
50 $active = function_exists( 'desktop_mode_ai_get_active_provider_id' )
51 ? desktop_mode_ai_get_active_provider_id( $user_id )
52 : 'openai';
53
54 // 1. Platform key — works for any user, including anonymous contexts.
55 // Resolve via the per-provider map first, then fall back to the
56 // legacy single `apiKey` field (which is treated as the openai key).
57 if ( ! empty( $platform['enabled'] ) && '' !== desktop_mode_ai_resolve_key_for_provider( $platform, $active ) ) {
58 return true;
59 }
60
61 // 2. Per-user override.
62 $ai = desktop_mode_ai_get_settings( $user_id );
63 if ( empty( $ai['enabled'] ) ) {
64 return false;
65 }
66 if ( '' === desktop_mode_ai_resolve_key_for_provider( $ai, $active ) ) {
67 return false;
68 }
69
70 return true;
71 }
72
73 /**
74 * Resolve which key — from a settings block — applies to a given provider.
75 *
76 * Order: explicit `apiKeys[provider]` → legacy `apiKey` (only when the
77 * provider is `openai`, since that's what the legacy field meant) → ''.
78 *
79 * @since 0.18.0
80 *
81 * @param array $settings `apiKeys` and `apiKey` carrying settings block.
82 * @param string $provider_id Provider id to resolve for.
83 * @return string Trimmed API key, or '' if none.
84 */
85 function desktop_mode_ai_resolve_key_for_provider( array $settings, $provider_id ) {
86 $provider_id = (string) $provider_id;
87 $keys = isset( $settings['apiKeys'] ) && is_array( $settings['apiKeys'] ) ? $settings['apiKeys'] : array();
88
89 if ( isset( $keys[ $provider_id ] ) && is_string( $keys[ $provider_id ] ) && '' !== $keys[ $provider_id ] ) {
90 return (string) $keys[ $provider_id ];
91 }
92
93 if ( 'openai' === $provider_id && isset( $settings['apiKey'] ) && is_string( $settings['apiKey'] ) && '' !== $settings['apiKey'] ) {
94 return (string) $settings['apiKey'];
95 }
96
97 return '';
98 }
99
100 /**
101 * Returns the API key to use for a given user.
102 *
103 * Per-user key takes precedence (personal override); falls back to the
104 * platform-wide key so anonymous contexts (cron, WP-CLI, anonymous
105 * comments) always have a key available when the admin has configured one.
106 *
107 * @since 0.14.0
108 *
109 * @param int $user_id
110 * @return string API key, or empty string if none configured.
111 */
112 function desktop_mode_ai_get_api_key( $user_id ) {
113 $user_id = (int) $user_id;
114 $active = function_exists( 'desktop_mode_ai_get_active_provider_id' )
115 ? desktop_mode_ai_get_active_provider_id( $user_id )
116 : 'openai';
117
118 // Per-user override takes precedence.
119 $ai = desktop_mode_ai_get_settings( $user_id );
120 $key = desktop_mode_ai_resolve_key_for_provider( $ai, $active );
121 if ( '' !== $key ) {
122 return $key;
123 }
124
125 // Fall back to platform key.
126 $platform = desktop_mode_ai_get_platform_settings();
127 return desktop_mode_ai_resolve_key_for_provider( $platform, $active );
128 }
129