allowed_styles = array_fill_keys( array_keys( $style_descriptions ), true ); $this->callbacks = array_replace( array( 'get_option' => 'get_option', 'update_option' => 'update_option', 'current_user_can' => 'current_user_can', 'normalize_model' => static function ( string $provider, string $model ): string { return \AIPT\Api\ModelCatalog::normalize_model( $provider, $model ); }, ), $callbacks ); } public function get(): array { $provider_raw = call_user_func( $this->callbacks['get_option'], 'aipt_api_provider', self::DEFAULT_PROVIDER ); $provider = $this->is_valid_provider( $provider_raw ) ? $provider_raw : self::DEFAULT_PROVIDER; $model_raw = call_user_func( $this->callbacks['get_option'], 'aipt_model', self::DEFAULT_MODEL ); $language_raw = call_user_func( $this->callbacks['get_option'], 'aipt_language', self::DEFAULT_LANGUAGE ); $writing_style_raw = call_user_func( $this->callbacks['get_option'], 'aipt_writing_style', self::DEFAULT_WRITING_STYLE ); $brand_context_raw = call_user_func( $this->callbacks['get_option'], 'aipt_brand_context', '' ); $preserve_raw = call_user_func( $this->callbacks['get_option'], 'aipt_preserve_content_images', true ); $model = $this->normalize_model( $provider, $model_raw ); if ( ( ! is_string( $model_raw ) || $model_raw !== $model ) && $this->can_update() ) { call_user_func( $this->callbacks['update_option'], 'aipt_model', $model ); } return array( 'provider' => $provider, 'model' => $model, 'language' => $this->is_valid_language( $language_raw ) ? $language_raw : self::DEFAULT_LANGUAGE, 'writing_style' => $this->is_valid_writing_style( $writing_style_raw ) ? $writing_style_raw : self::DEFAULT_WRITING_STYLE, 'brand_context' => is_string( $brand_context_raw ) ? \AIPT\Api\Services\SettingsService::normalizePromptText( $brand_context_raw ) : '', 'preserveContentImages' => $this->normalize_preserve_content_images( $preserve_raw ), ); } /** * Whether a value is an accepted language. Single source shared by get() and * update() so a stored value can never bypass the allow-list. * * @param mixed $value Raw stored/incoming value. */ private function is_valid_language( $value ): bool { return is_string( $value ) && in_array( $value, self::SUPPORTED_LANGUAGES, true ); } /** * Whether a value is an accepted writing style (keys of the descriptions map). * * @param mixed $value Raw stored/incoming value. */ private function is_valid_writing_style( $value ): bool { return is_string( $value ) && isset( $this->allowed_styles[ $value ] ); } private function is_valid_provider( $value ): bool { return is_string( $value ) && in_array( $value, self::SUPPORTED_PROVIDERS, true ); } private function normalize_model( string $provider, $model ): string { $source = is_string( $model ) ? $model : self::DEFAULT_MODEL; return (string) call_user_func( $this->callbacks['normalize_model'], $provider, $source ); } private function normalize_preserve_content_images( $value ): bool { if ( is_string( $value ) ) { return in_array( strtolower( $value ), array( '1', 'true', 'yes', 'on' ), true ); } return (bool) $value; } public function update( array $params ) { $updates = array(); $current = $this->get(); $provider_changed = array_key_exists( 'provider', $params ); $model_changed = array_key_exists( 'model', $params ); $provider = $current['provider']; if ( $provider_changed ) { $requested_provider = $params['provider']; if ( 'default' === $requested_provider ) { $requested_provider = self::DEFAULT_PROVIDER; } if ( ! $this->is_valid_provider( $requested_provider ) ) { return new \WP_Error( 'invalid_provider', __( 'Invalid API provider selection', 'ai-product-tools' ), array( 'status' => 400 ) ); } $provider = $requested_provider; $updates['aipt_api_provider'] = $provider; } if ( $provider_changed || $model_changed ) { $model = $model_changed ? $params['model'] : ''; if ( 'default' === $model ) { $model = ''; } if ( ! is_string( $model ) ) { return new \WP_Error( 'invalid_model', __( 'Invalid model selection', 'ai-product-tools' ), array( 'status' => 400 ) ); } $normalized_model = $this->normalize_model( $provider, $model ); if ( '' !== $model ) { $compatibility_error = \AIPT\Api\Services\SettingsService::validateModelProviderCompatibility( $provider, $model ); if ( \is_wp_error( $compatibility_error ) ) { return $compatibility_error; } if ( 'openrouter' !== $provider && $model !== $normalized_model ) { return new \WP_Error( 'invalid_model', __( 'Invalid model selection', 'ai-product-tools' ), array( 'status' => 400 ) ); } } $updates['aipt_model'] = $normalized_model; } if ( array_key_exists( 'language', $params ) ) { $language = $params['language']; if ( ! $this->is_valid_language( $language ) ) { return new \WP_Error( 'invalid_language', __( 'Invalid language selection', 'ai-product-tools' ), array( 'status' => 400 ) ); } $updates['aipt_language'] = $language; } if ( array_key_exists( 'writing_style', $params ) ) { $writing_style = $params['writing_style']; if ( ! $this->is_valid_writing_style( $writing_style ) ) { return new \WP_Error( 'invalid_style', __( 'Invalid writing style selection', 'ai-product-tools' ), array( 'status' => 400 ) ); } $updates['aipt_writing_style'] = $writing_style; } if ( array_key_exists( 'brand_context', $params ) ) { $brand_context = $params['brand_context']; if ( ! is_string( $brand_context ) ) { return new \WP_Error( 'invalid_prompt_type', __( 'Brand context must be a string.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $brand_context = \AIPT\Api\Services\SettingsService::normalizePromptText( $brand_context ); if ( strlen( $brand_context ) > self::MAX_PROMPT_BYTES ) { return new \WP_Error( 'prompt_too_long', __( 'Brand context exceeds the maximum allowed length.', 'ai-product-tools' ), array( 'status' => 400 ) ); } $updates['aipt_brand_context'] = $brand_context; } if ( array_key_exists( 'preserveContentImages', $params ) ) { $updates['aipt_preserve_content_images'] = (int) $this->normalize_preserve_content_images( $params['preserveContentImages'] ); } $applied = array(); foreach ( $updates as $option => $value ) { $current = call_user_func( $this->callbacks['get_option'], $option, '' ); if ( (string) $current === (string) $value ) { continue; } if ( false === call_user_func( $this->callbacks['update_option'], $option, $value ) ) { $rollback_failed = array(); foreach ( $applied as $applied_option => $original ) { if ( false === call_user_func( $this->callbacks['update_option'], $applied_option, $original ) ) { $rollback_failed[] = $applied_option; } } $error_data = array( 'status' => 500 ); if ( ! empty( $rollback_failed ) ) { $error_data['rollback_failed'] = $rollback_failed; } return new \WP_Error( 'update_failed', __( 'Failed to save content defaults.', 'ai-product-tools' ), $error_data ); } $applied[ $option ] = $current; } return $this->get(); } public function can_update(): bool { return (bool) call_user_func( $this->callbacks['current_user_can'], 'manage_aipt_settings' ); } }