*/ if ( ! defined( 'ABSPATH' ) ) { exit; } class Group107_AccessibleDocs_Admin_General_Settings { public function __construct() { add_action('admin_init', [$this, 'register_accessible_docs_general_settings']); } /** * Registers the settings for the General Settings page. */ public function register_accessible_docs_general_settings() { register_setting('accessible_docs_settings_group', 'accessible_docs_settings', [ 'sanitize_callback' => [$this, 'accessible_docs_sanitize_settings'] ]); add_settings_section( 'accessible_docs_settings_section', esc_html__('General Settings', 'accessible-docs'), function () { echo '
'; /* translators: %s is the AccessibleDocs signup URL. */ $text = __('Enter your email and password for AccessibleDocs account.', 'accessible-docs'); $text = wp_kses( $text, array( 'a' => array( 'href' => array(), 'target' => array() ) ) ); $message = sprintf( $text, esc_url('https://platform.accessibledocs.ai/sign-up') ); echo wp_kses_post( $message ); echo '
'; }, 'accessible_docs_settings' ); add_settings_field( 'accessible_docs_email', esc_html__('Email', 'accessible-docs'), [$this, 'accessible_docs_render_email_field'], 'accessible_docs_settings', 'accessible_docs_settings_section' ); add_settings_field( 'accessible_docs_password', esc_html__('Password', 'accessible-docs'), [$this, 'accessible_docs_render_password_field'], 'accessible_docs_settings', 'accessible_docs_settings_section' ); } public function accessible_docs_render_email_field() { $options = get_option('accessible_docs_settings'); $email = isset($options['email']) ? $options['email'] : ''; echo ''; } public function accessible_docs_render_password_field() { $options = get_option('accessible_docs_settings'); $has_saved = !empty($options['password']); // The saved password is never printed into the page. Leave the field empty to keep it. printf( '', $has_saved ? esc_attr__('Saved – leave empty to keep the current password', 'accessible-docs') : '' ); } public function accessible_docs_sanitize_settings($input) { $output = []; $previous = get_option('accessible_docs_settings'); $previous = is_array($previous) ? $previous : []; $input = is_array($input) ? $input : []; // Check if email is provided and valid. if (!empty($input['email'])) { if (is_email($input['email'])) { $output['email'] = sanitize_email($input['email']); } else { add_settings_error('accessible_docs_messages', 'invalid_email', esc_html__('Please enter a valid email address.', 'accessible-docs'), 'error'); } } // The password is stored exactly as typed (text sanitizing would change passwords with "<", "%" or spaces). // An empty field keeps the saved password. $password = isset($input['password']) ? str_replace(chr(0), '', (string) $input['password']) : ''; if ('' !== $password) { $output['password'] = $password; } elseif (!empty($previous['password'])) { $output['password'] = $previous['password']; } if (empty($output['email']) && empty($output['password'])) { add_settings_error( 'accessible_docs_messages', 'empty_fields', esc_html__('Please provide both an email and a password.', 'accessible-docs'), 'error' ); return $output; } // Verify the credentials right away, so a typo does not silently break the popup. // (WordPress may call this callback twice on the first save – check only once per request.) static $verified = false; $changed = ($output['email'] ?? '') !== ($previous['email'] ?? '') || ($output['password'] ?? '') !== ($previous['password'] ?? ''); if (!$verified && $changed && !empty($output['email']) && !empty($output['password'])) { $verified = true; $login = Group107_AccessibleDocs_API_Handler::accessible_docs_login_request($output['email'], $output['password']); if (empty($login['data']['accessToken'])) { add_settings_error( 'accessible_docs_messages', 'invalid_credentials', esc_html__('Settings saved, but AccessibleDocs rejected these credentials. Please check the email and password.', 'accessible-docs'), 'warning' ); } else { add_settings_error( 'accessible_docs_messages', 'valid_credentials', esc_html__('Connected to AccessibleDocs successfully.', 'accessible-docs'), 'success' ); } } return $output; } } new Group107_AccessibleDocs_Admin_General_Settings();