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

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