'1', 'notify_categories' => array( 'estimate', 'consult', 'recruit' ), 'stale_days' => 3, 'stale_email' => '', 'spam_url_threshold' => 3, 'spam_latin_only' => '1', 'spam_honeypot' => '1', 'store_ip' => '1', 'delete_on_uninstall' => '0', 'rules' => acscf_get_default_rules(), ); } /** * Read the effective settings. * * @return array */ function acscf_get_settings() { $stored = get_option( 'acscf_settings', array() ); $defaults = acscf_get_default_settings(); $settings = wp_parse_args( is_array( $stored ) ? $stored : array(), $defaults ); $settings['rules'] = wp_parse_args( isset( $settings['rules'] ) && is_array( $settings['rules'] ) ? $settings['rules'] : array(), $defaults['rules'] ); if ( ! is_array( $settings['notify_categories'] ) ) { $settings['notify_categories'] = $defaults['notify_categories']; } $settings['stale_days'] = (int) $settings['stale_days']; $settings['spam_url_threshold'] = (int) $settings['spam_url_threshold']; return $settings; } /** * Default AI settings. * * The provider and its credentials are not stored by this plugin. They belong * to the WordPress core AI Client, which the site owner configures under * Settings > Connectors. * * @return array */ function acscf_get_default_ai_settings() { return array( 'tone' => 'polite', 'sender_name' => '', 'auto_draft' => '0', ); } /** * Read the effective AI settings. * * @return array */ function acscf_get_ai_settings() { $stored = get_option( 'acscf_ai_settings', array() ); $defaults = acscf_get_default_ai_settings(); $stored = is_array( $stored ) ? $stored : array(); // Only the keys this plugin still owns. Settings left over from the // versions that talked to a provider directly are ignored. return array_merge( $defaults, array_intersect_key( $stored, $defaults ) ); } /** * URL of the core screen where AI providers are connected. * * @return string */ function acscf_ai_connectors_url() { return admin_url( 'options-connectors.php' ); } /** * Whether the WordPress core AI Client is present and allowed in this environment. * * @return bool */ function acscf_ai_client_available() { return function_exists( 'wp_ai_client_prompt' ) && function_exists( 'wp_supports_ai' ) && wp_supports_ai(); } /** * Whether the AI reply draft feature is usable. * * True only when the core AI Client can generate text, which means the site * owner has connected a provider. This plugin never holds an API key and never * contacts a provider itself. * * @return bool */ function acscf_ai_is_enabled() { if ( ! acscf_ai_client_available() ) { return false; } // A deterministic capability lookup in the provider registry. No request is sent. return (bool) wp_ai_client_prompt( 'ping' )->is_supported_for_text_generation(); } /** * Register the settings. * * @return void */ function acscf_register_settings() { register_setting( 'acscf_settings_group', 'acscf_settings', array( 'type' => 'array', 'sanitize_callback' => 'acscf_sanitize_settings', 'default' => acscf_get_default_settings(), ) ); register_setting( 'acscf_ai_settings_group', 'acscf_ai_settings', array( 'type' => 'array', 'sanitize_callback' => 'acscf_sanitize_ai_settings', 'default' => acscf_get_default_ai_settings(), ) ); } add_action( 'admin_init', 'acscf_register_settings' ); /** * Sanitize the general settings. * * @param mixed $input Raw input. * @return array */ function acscf_sanitize_settings( $input ) { $defaults = acscf_get_default_settings(); $current = acscf_get_settings(); if ( ! is_array( $input ) ) { return $current; } $clean = $current; $clean['save_entries'] = ! empty( $input['save_entries'] ) ? '1' : '0'; $clean['spam_latin_only'] = ! empty( $input['spam_latin_only'] ) ? '1' : '0'; $clean['spam_honeypot'] = ! empty( $input['spam_honeypot'] ) ? '1' : '0'; $clean['store_ip'] = ! empty( $input['store_ip'] ) ? '1' : '0'; $clean['delete_on_uninstall'] = ! empty( $input['delete_on_uninstall'] ) ? '1' : '0'; $stale_days = isset( $input['stale_days'] ) ? (int) $input['stale_days'] : $defaults['stale_days']; $clean['stale_days'] = max( 0, min( 365, $stale_days ) ); $threshold = isset( $input['spam_url_threshold'] ) ? (int) $input['spam_url_threshold'] : $defaults['spam_url_threshold']; $clean['spam_url_threshold'] = max( 0, min( 50, $threshold ) ); $stale_email = isset( $input['stale_email'] ) ? sanitize_email( $input['stale_email'] ) : ''; $clean['stale_email'] = is_email( $stale_email ) ? $stale_email : ''; $notify = array(); if ( isset( $input['notify_categories'] ) && is_array( $input['notify_categories'] ) ) { foreach ( $input['notify_categories'] as $category ) { $category = sanitize_key( $category ); if ( array_key_exists( $category, acscf_get_categories() ) ) { $notify[] = $category; } } } $clean['notify_categories'] = array_values( array_unique( $notify ) ); $rules = array(); foreach ( array_keys( acscf_get_default_rules() ) as $category ) { $raw = isset( $input['rules'][ $category ] ) ? (string) $input['rules'][ $category ] : ''; $rules[ $category ] = sanitize_textarea_field( $raw ); } $clean['rules'] = $rules; acscf_schedule_stale_alert( $clean['stale_days'] ); return $clean; } /** * Sanitize the AI settings. * * @param mixed $input Raw input. * @return array */ function acscf_sanitize_ai_settings( $input ) { $current = acscf_get_ai_settings(); if ( ! is_array( $input ) ) { return $current; } $clean = $current; $clean['sender_name'] = isset( $input['sender_name'] ) ? sanitize_text_field( $input['sender_name'] ) : ''; $clean['auto_draft'] = ! empty( $input['auto_draft'] ) ? '1' : '0'; $tone = isset( $input['tone'] ) ? sanitize_key( $input['tone'] ) : 'polite'; $clean['tone'] = in_array( $tone, array( 'polite', 'friendly', 'brief' ), true ) ? $tone : 'polite'; if ( ! acscf_ai_is_enabled() ) { $clean['auto_draft'] = '0'; } return $clean; } /** * Render the settings page. * * @return void */ function acscf_render_settings_page() { if ( ! current_user_can( acscf_manage_capability() ) ) { wp_die( esc_html__( 'You do not have permission to view this screen.', 'acs-contact-form' ) ); } $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read only tab switch. if ( ! in_array( $tab, array( 'general', 'ai' ), true ) ) { $tab = 'general'; } $settings = acscf_get_settings(); $ai = acscf_get_ai_settings(); $ai_ready = acscf_ai_is_enabled(); $categories = acscf_get_categories(); $base_url = admin_url( 'edit.php?post_type=acscf_form&page=acscf-settings' ); ?>

$unused_default ) : ?>