options_cache[ $key ] ) ) { $this->options_cache[ $key ] = get_option( $key, $default ); } if ( $key === 'aipt_model' ) { $provider = $this->get( 'aipt_api_provider', Activator::get_default_value( 'api_provider' ) ); $this->options_cache[ $key ] = $this->normalizeModelSetting( $provider, $this->options_cache[ $key ] ); } return $this->options_cache[ $key ]; } /** * Clear all caches * * Clears options cache and settings cache. */ public function clear(): void { $this->options_cache = array(); $this->advanced_settings_cache = null; $this->settings_cache = null; $this->primed = false; } /** * Whether non-autoloaded options have been primed for this request. * * @var bool */ private $primed = false; /** * Prime non-autoloaded options with a single DB query. * * After autoload optimization, prompt templates and admin-only options * are no longer in the alloptions cache. This method batch-loads them * before the settings arrays are built, replacing N individual queries * with one SELECT ... WHERE option_name IN (...). * * Requires WP 6.4+ (wp_prime_option_caches). On older versions, each * option is fetched individually — still correct, just not batched. */ private function primeNonAutoloadedOptions(): void { if ( $this->primed ) { return; } $this->primed = true; if ( ! function_exists( 'wp_prime_option_caches' ) ) { return; } \wp_prime_option_caches( array( 'aipt_short_custom_prompt', 'aipt_full_custom_prompt', 'aipt_title_additional_prompt', 'aipt_faq_additional_prompt', 'aipt_seo_meta_additional_prompt', 'aipt_taxonomy_additional_prompt', 'aipt_image_alt_additional_prompt', 'aipt_brand_context', 'aipt_custom_variables', 'aipt_enable_tooltip', 'aipt_metabox_hidden', 'aipt_editor_clear_ai_mark_on_manual_edit', ) ); } /** * Get cached settings array * * @param string $type Settings type: 'advanced' or 'general'. * @return array Settings data. */ public function getSettingsArray( string $type = 'general' ): array { switch ( $type ) { case 'advanced': return $this->getAdvancedSettingsArray(); case 'general': default: return $this->getGeneralSettingsArray(); } } /** * Get cached advanced settings array * * For Advanced Bulk Generator. * * @return array Advanced settings. */ private function getAdvancedSettingsArray(): array { if ( $this->advanced_settings_cache === null ) { $this->primeNonAutoloadedOptions(); $openai_key = ''; $gemini_key = ''; $openrouter_key = ''; $claude_key = ''; try { $openai_key = \AIPT\Utils\ApiKeyManager::get_openai_key(); } catch ( \Exception $e ) { } try { $gemini_key = \AIPT\Utils\ApiKeyManager::get_gemini_key(); } catch ( \Exception $e ) { } try { $openrouter_key = \AIPT\Utils\ApiKeyManager::get_openrouter_key(); } catch ( \Exception $e ) { } try { $claude_key = \AIPT\Utils\ApiKeyManager::get_claude_key(); } catch ( \Exception $e ) { } $this->advanced_settings_cache = array( 'openai_api_key' => $openai_key, 'gemini_api_key' => $gemini_key, 'openrouter_api_key' => $openrouter_key, 'claude_api_key' => $claude_key, 'api_provider' => $this->get( 'aipt_api_provider', Activator::get_default_value( 'api_provider' ) ), 'short_custom_prompt' => CustomPrompts::stored( CustomPrompts::SHORT ), 'full_custom_prompt' => CustomPrompts::stored( CustomPrompts::FULL ), 'language' => $this->get( 'aipt_language', Activator::get_default_value( 'language' ) ), 'writing_style' => $this->get( 'aipt_writing_style', Activator::get_default_value( 'writing_style' ) ), 'content_length_preset' => $this->get( 'aipt_content_length_preset', Activator::get_default_value( 'content_length_preset' ) ), 'short_length_preset' => $this->get( 'aipt_short_length_preset', Activator::get_default_value( 'short_length_preset' ) ), 'max_tags_count' => $this->get( 'aipt_max_tags_count', Activator::get_default_value( 'max_tags_count' ) ), 'model' => $this->get( 'aipt_model', Activator::get_default_value( 'model' ) ), 'enable_tooltip' => rest_sanitize_boolean( $this->get( 'aipt_enable_tooltip', Activator::get_default_value( 'enable_tooltip' ) ) ), ); $this->advanced_settings_cache['model'] = $this->normalizeModelSetting( $this->advanced_settings_cache['api_provider'], $this->advanced_settings_cache['model'] ); } return $this->advanced_settings_cache; } /** * Get cached general settings array * * For Settings page and general use. * * @return array General settings. */ private function getGeneralSettingsArray(): array { if ( $this->settings_cache === null ) { $this->primeNonAutoloadedOptions(); $openai_key = ''; $gemini_key = ''; $openrouter_key = ''; $claude_key = ''; try { $openai_key = \AIPT\Utils\ApiKeyManager::get_openai_key(); } catch ( \Exception $e ) { } try { $gemini_key = \AIPT\Utils\ApiKeyManager::get_gemini_key(); } catch ( \Exception $e ) { } try { $openrouter_key = \AIPT\Utils\ApiKeyManager::get_openrouter_key(); } catch ( \Exception $e ) { } try { $claude_key = \AIPT\Utils\ApiKeyManager::get_claude_key(); } catch ( \Exception $e ) { } $this->settings_cache = array( 'openai_api_key' => $openai_key, 'gemini_api_key' => $gemini_key, 'openrouter_api_key' => $openrouter_key, 'claude_api_key' => $claude_key, 'claude_workspace_id' => $this->get( 'aipt_claude_workspace_id', Activator::get_default_value( 'claude_workspace_id' ) ), 'api_provider' => $this->get( 'aipt_api_provider', Activator::get_default_value( 'api_provider' ) ), 'short_custom_prompt' => CustomPrompts::stored( CustomPrompts::SHORT ), 'full_custom_prompt' => CustomPrompts::stored( CustomPrompts::FULL ), 'title_additional_prompt' => $this->get( 'aipt_title_additional_prompt', Activator::get_default_value( 'title_additional_prompt' ) ), 'faq_additional_prompt' => $this->get( 'aipt_faq_additional_prompt', Activator::get_default_value( 'faq_additional_prompt' ) ), 'seo_meta_additional_prompt' => $this->get( 'aipt_seo_meta_additional_prompt', Activator::get_default_value( 'seo_meta_additional_prompt' ) ), 'taxonomy_additional_prompt' => $this->get( 'aipt_taxonomy_additional_prompt', Activator::get_default_value( 'taxonomy_additional_prompt' ) ), 'image_alt_additional_prompt' => $this->get( 'aipt_image_alt_additional_prompt', Activator::get_default_value( 'image_alt_additional_prompt' ) ), 'brand_context' => $this->get( 'aipt_brand_context', Activator::get_default_value( 'brand_context' ) ), 'language' => $this->get( 'aipt_language', Activator::get_default_value( 'language' ) ), 'writing_style' => $this->get( 'aipt_writing_style', Activator::get_default_value( 'writing_style' ) ), 'content_length_preset' => $this->get( 'aipt_content_length_preset', Activator::get_default_value( 'content_length_preset' ) ), 'short_length_preset' => $this->get( 'aipt_short_length_preset', Activator::get_default_value( 'short_length_preset' ) ), 'max_tags_count' => $this->get( 'aipt_max_tags_count', Activator::get_default_value( 'max_tags_count' ) ), 'model' => $this->get( 'aipt_model', Activator::get_default_value( 'model' ) ), 'enable_tooltip' => rest_sanitize_boolean( $this->get( 'aipt_enable_tooltip', Activator::get_default_value( 'enable_tooltip' ) ) ), 'metabox_hidden' => rest_sanitize_boolean( $this->get( 'aipt_metabox_hidden', Activator::get_default_value( 'metabox_hidden' ) ) ), 'preserve_content_images' => rest_sanitize_boolean( $this->get( 'aipt_preserve_content_images', Activator::get_default_value( 'preserve_content_images' ) ) ), 'editor_clear_ai_mark_on_manual_edit' => rest_sanitize_boolean( $this->get( 'aipt_editor_clear_ai_mark_on_manual_edit', Activator::get_default_value( 'editor_clear_ai_mark_on_manual_edit' ) ) ), 'content_history_enabled' => rest_sanitize_boolean( $this->get( 'aipt_content_history_enabled', Activator::get_default_value( 'content_history_enabled' ) ) ), 'content_history_retention' => \AIPT\Core\ContentSnapshots\SnapshotStore::retentionCap(), 'can_manage_content_history' => (bool) current_user_can( 'manage_aipt_settings' ), ); $missing_option = new \stdClass(); foreach ( array( 'allowed_html_tags', 'disallowed_html_tags' ) as $field ) { $option_key = 'aipt_' . $field; $option_value = $this->get( $option_key, $missing_option ); if ( $option_value === $missing_option ) { unset( $this->options_cache[ $option_key ] ); continue; } if ( is_string( $option_value ) && ! empty( $option_value ) ) { $this->settings_cache[ $field ] = array_filter( array_map( 'trim', explode( ',', $option_value ) ) ); } elseif ( is_array( $option_value ) ) { $this->settings_cache[ $field ] = array_map( 'sanitize_text_field', $option_value ); } else { $this->settings_cache[ $field ] = array(); } } $this->settings_cache['model'] = $this->normalizeModelSetting( $this->settings_cache['api_provider'], $this->settings_cache['model'] ); $premium_fields = (array) apply_filters( 'aipt_settings_premium_fields', array() ); foreach ( $premium_fields as $field ) { if ( is_string( $field ) && array_key_exists( $field, $this->settings_cache ) && ! apply_filters( 'aipt_settings_can_read_field', false, $field ) ) { unset( $this->settings_cache[ $field ] ); } } } return $this->settings_cache; } /** * Normalize model setting based on provider * * @param string $provider API provider. * @param string $model Model name. * @return string Normalized model name. */ private function normalizeModelSetting( string $provider, string $model ): string { $provider = sanitize_text_field( $provider ); $model = sanitize_text_field( $model ); $normalized = \AIPT\Api\ModelCatalog::normalize_model( $provider, $model ); if ( $model !== $normalized ) { $this->persistModelOption( $normalized ); } return $normalized; } /** * Persist model option to database * * @param string $value Model value to persist. */ private function persistModelOption( string $value ): void { $sanitized_value = sanitize_text_field( (string) $value ); $this->options_cache['aipt_model'] = $sanitized_value; update_option( 'aipt_model', $sanitized_value ); } }