'array', 'default' => array(), 'sanitize_callback' => array( $this, 'sanitize_settings' ), ) ); add_settings_section( self::SECTION_ID, '', '__return_empty_string', self::PAGE_SLUG ); add_settings_field( self::OPTION_NAME . '_host', __( 'Open WebUI URL', 'ai-provider-for-open-webui' ), array( $this, 'render_host_field' ), self::PAGE_SLUG, self::SECTION_ID, array( 'label_for' => self::OPTION_NAME . '-host' ) ); add_settings_field( self::OPTION_NAME . '_api_key', __( 'API Key', 'ai-provider-for-open-webui' ), array( $this, 'render_api_key_field' ), self::PAGE_SLUG, self::SECTION_ID, array( 'label_for' => self::OPTION_NAME . '-api-key' ) ); add_settings_field( self::OPTION_NAME . '_models', __( 'Model', 'ai-provider-for-open-webui' ), array( $this, 'render_model_field' ), self::PAGE_SLUG, self::SECTION_ID, array( 'label_for' => self::OPTION_NAME . '-model' ) ); } /** * Registers the settings screen. * * @since 1.0.0 */ public function register_settings_screen(): void { add_options_page( __( 'Open WebUI Settings', 'ai-provider-for-open-webui' ), __( 'Open WebUI', 'ai-provider-for-open-webui' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_screen' ) ); } /** * Sanitizes the settings array. * * @since 1.0.0 * * @param mixed $value The input value. * @return array The sanitized settings. */ public function sanitize_settings( $value ): array { if ( ! is_array( $value ) ) { return array(); } $host = isset( $value['host'] ) ? trim( (string) $value['host'] ) : ''; if ( '' !== $host ) { $host = rtrim( esc_url_raw( $host ), '/' ); } $api_key = isset( $value['api_key'] ) ? trim( (string) $value['api_key'] ) : ''; if ( '' !== $api_key ) { $api_key = sanitize_text_field( $api_key ); } $current_settings = (array) get_option( self::OPTION_NAME, array() ); $selected_model = ''; if ( isset( $value['model'] ) ) { $selected_model = self::sanitize_model_value( $value['model'] ); } // Backward compatibility: migrate previous capability-specific model fields. if ( '' === $selected_model ) { foreach ( array( 'model_text', 'model_image', 'model_vision' ) as $legacy_key ) { if ( isset( $value[ $legacy_key ] ) ) { $selected_model = self::sanitize_model_value( $value[ $legacy_key ] ); if ( '' !== $selected_model ) { break; } } } } if ( '' === $selected_model ) { $selected_model = isset( $current_settings['model'] ) ? self::sanitize_model_value( $current_settings['model'] ) : ''; } self::sync_connector_api_key( $api_key ); return array( 'host' => $host, 'api_key' => $api_key, 'model' => $selected_model, ); } /** * Sanitizes a model setting value. * * @since 1.1.0 * * @param mixed $value Model value. * @return string Sanitized model value. */ private static function sanitize_model_value( $value ): string { $model = trim( (string) $value ); if ( '' === $model ) { return ''; } return sanitize_text_field( $model ); } /** * Renders the settings screen. * * @since 1.0.0 */ public function render_screen(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>

Connectors%2$s and can be created in Open WebUI under Settings > Account.', 'ai-provider-for-open-webui' ), '', '' ); ?>

', '' ); ?>

', '' ); ?>

Account). This field is synchronized with %1$sSettings > Connectors%2$s.', 'ai-provider-for-open-webui' ), '', '' ); ?>


esc_url( admin_url( 'admin-ajax.php' ) . '?action=' . self::AJAX_ACTION . '&_wpnonce=' . wp_create_nonce( self::NONCE_ACTION ) ), 'modelFields' => array( array( 'fieldId' => self::OPTION_NAME . '-model', 'modelSuggestionsId' => 'obenweb-openwebui-provider-model-suggestions-all', 'capability' => 'any', ), ), ) ); } /** * Handles the AJAX request to list available OpenWebUI models. * * @since 1.0.0 */ public function ajax_list_models(): void { check_ajax_referer( self::NONCE_ACTION ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Insufficient permissions.', 'ai-provider-for-open-webui' ), 403 ); } if ( ! class_exists( AiClient::class ) ) { wp_send_json_error( __( 'WordPress AI Client is not available.', 'ai-provider-for-open-webui' ), 500 ); } $provider_id = 'openwebui'; $registry = AiClient::defaultRegistry(); if ( ! $registry->hasProvider( $provider_id ) ) { wp_send_json_error( __( 'AI provider not found.', 'ai-provider-for-open-webui' ), 404 ); } $provider_classname = $registry->getProviderClassName( $provider_id ); try { // phpcs:ignore Generic.Commenting.DocComment.MissingShort $model_metadata_directory = $provider_classname::modelMetadataDirectory(); $model_metadata_objects = $model_metadata_directory->listModelMetadata(); wp_send_json_success( $model_metadata_objects ); } catch ( \Throwable $e ) { /* translators: %s: Error message. */ wp_send_json_error( sprintf( __( 'Could not list models from Open WebUI. Check URL/API key. Error: %s', 'ai-provider-for-open-webui' ), $e->getMessage() ), 500 ); } } /** * Gets the settings from the WordPress option. * * @since 1.0.0 * * @return array The settings. */ public static function get_settings(): array { $settings = (array) get_option( self::OPTION_NAME, array() ); $host = ''; if ( isset( $settings['host'] ) && '' !== (string) $settings['host'] ) { $host = trim( (string) $settings['host'] ); } $legacy_api_key = ''; if ( isset( $settings['api_key'] ) && '' !== (string) $settings['api_key'] ) { $legacy_api_key = trim( (string) $settings['api_key'] ); } $selected_model = ''; if ( isset( $settings['model'] ) && '' !== (string) $settings['model'] ) { $selected_model = trim( (string) $settings['model'] ); } // Backward compatibility: use previous capability-specific model values if needed. if ( '' === $selected_model ) { foreach ( array( 'model_text', 'model_image', 'model_vision' ) as $legacy_key ) { if ( isset( $settings[ $legacy_key ] ) && '' !== (string) $settings[ $legacy_key ] ) { $selected_model = trim( (string) $settings[ $legacy_key ] ); break; } } } $connector_api_key = self::get_connector_api_key(); if ( '' === $connector_api_key && '' !== $legacy_api_key ) { self::sync_connector_api_key( $legacy_api_key ); $connector_api_key = $legacy_api_key; } return array( 'host' => $host, 'api_key' => '' !== $connector_api_key ? $connector_api_key : $legacy_api_key, 'model' => $selected_model, ); } /** * Gets the Open WebUI API key from the connector option. * * @since 1.0.0 * * @return string The API key or an empty string. */ private static function get_connector_api_key(): string { $value = get_option( self::CONNECTOR_API_KEY_OPTION, '' ); if ( ! is_string( $value ) ) { return ''; } return trim( $value ); } /** * Synchronizes the plugin API key with the Connector API key option. * * @since 1.0.0 * * @param string $api_key The sanitized API key. */ private static function sync_connector_api_key( string $api_key ): void { $current_connector_key = self::get_connector_api_key(); if ( $current_connector_key === $api_key ) { return; } update_option( self::CONNECTOR_API_KEY_OPTION, $api_key ); } }