| 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 |
|