'admon_sanitize_settings', 'default' => array( 'session_timeout' => '', 'custom_timeout_hours' => '', 'apply_to_admins' => 0, 'override_remember_me' => 0, 'allowed_users' => '', 'custom_redirect' => '', ), ) ); // Add main settings section. add_settings_section( 'admin_only_main_section', '', 'admon_settings_section_callback', 'admin_only_settings' ); // Add session settings section. add_settings_section( 'admin_only_session_section', '', 'admon_session_section_callback', 'admin_only_settings' ); // Session timeout field. add_settings_field( 'session_timeout', __( 'Auto-Logout After', 'admin-only' ), 'admon_session_timeout_callback', 'admin_only_settings', 'admin_only_session_section' ); // Apply to admins field. add_settings_field( 'apply_to_admins', __( 'Include Administrators', 'admin-only' ), 'admon_apply_to_admins_callback', 'admin_only_settings', 'admin_only_session_section' ); // Override Remember Me field. add_settings_field( 'override_remember_me', __( 'Override "Remember Me"', 'admin-only' ), 'admon_override_remember_me_callback', 'admin_only_settings', 'admin_only_session_section' ); // Allowed users field. add_settings_field( 'allowed_users', __( 'Allowed Users', 'admin-only' ), 'admon_allowed_users_callback', 'admin_only_settings', 'admin_only_main_section' ); // Custom redirect field. add_settings_field( 'custom_redirect', __( 'Custom Redirect URL', 'admin-only' ), 'admon_custom_redirect_callback', 'admin_only_settings', 'admin_only_main_section' ); } add_action( 'admin_init', 'admon_settings_init' ); /** * Enqueue admin scripts for the settings page. * * @param string $hook The current admin page. */ function admon_enqueue_settings_scripts( $hook ) { // Only enqueue on our specific settings page. if ( 'settings_page_admin-only-settings' !== $hook ) { return; } wp_enqueue_script( 'admon-settings-js', ADMON_PLUGIN_URL . 'admin/js/settings.js', array(), ADMON_VERSION, true // Load in footer ); wp_enqueue_style( 'admon-settings-css', ADMON_PLUGIN_URL . 'admin/css/settings.css', array(), ADMON_VERSION ); } add_action( 'admin_enqueue_scripts', 'admon_enqueue_settings_scripts' ); /** * Sanitize settings * * @param array $input Raw settings input. * @return array Sanitized settings. */ function admon_sanitize_settings( $input ) { $sanitized = array(); // Sanitize session timeout. if ( isset( $input['session_timeout'] ) ) { $timeout = $input['session_timeout']; if ( 'custom' === $timeout ) { $sanitized['session_timeout'] = 'custom'; } else { $timeout_int = absint( $timeout ); $allowed_timeouts = array( 1, 2, 4, 8, 12, 24 ); $sanitized['session_timeout'] = in_array( $timeout_int, $allowed_timeouts, true ) ? $timeout_int : ''; } } else { $sanitized['session_timeout'] = ''; } // Sanitize custom timeout hours. if ( isset( $input['custom_timeout_hours'] ) ) { $custom_hours = absint( $input['custom_timeout_hours'] ); // Allow 1-168 hours (1 week maximum). if ( $custom_hours >= 1 && $custom_hours <= 168 ) { $sanitized['custom_timeout_hours'] = $custom_hours; } else { $sanitized['custom_timeout_hours'] = ''; if ( ! empty( $input['custom_timeout_hours'] ) ) { add_settings_error( 'admin_only_settings', 'invalid_custom_timeout', __( 'Custom timeout must be between 1 and 168 hours.', 'admin-only' ), 'error' ); } } } else { $sanitized['custom_timeout_hours'] = ''; } // Sanitize apply to admins. $sanitized['apply_to_admins'] = ! empty( $input['apply_to_admins'] ) ? 1 : 0; // Sanitize override remember me. $sanitized['override_remember_me'] = ! empty( $input['override_remember_me'] ) ? 1 : 0; // Sanitize and validate allowed users. if ( isset( $input['allowed_users'] ) ) { $raw_usernames = sanitize_text_field( $input['allowed_users'] ); $validation_result = admon_validate_usernames_with_feedback( $raw_usernames ); $sanitized['allowed_users'] = $validation_result['valid_usernames']; // Show error message for invalid usernames (count only to prevent enumeration). if ( ! empty( $validation_result['invalid_usernames'] ) ) { $invalid_count = count( $validation_result['invalid_usernames'] ); add_settings_error( 'admin_only_settings', 'invalid_usernames', sprintf( /* translators: %d: Number of invalid usernames. */ _n( '%d username was not found and has been removed.', '%d usernames were not found and have been removed.', $invalid_count, 'admin-only' ), $invalid_count ), 'warning' ); } } else { $sanitized['allowed_users'] = ''; } // Sanitize custom redirect. if ( isset( $input['custom_redirect'] ) ) { $url = trim( $input['custom_redirect'] ); if ( ! empty( $url ) ) { // Validate that URL is within the same WordPress installation $sanitized_url = admon_validate_same_site_url( $url ); $sanitized['custom_redirect'] = ! empty( $sanitized_url ) ? $sanitized_url : ''; // Show error if URL is external if ( empty( $sanitized_url ) && ! empty( $url ) ) { add_settings_error( 'admin_only_settings', 'invalid_redirect_url', __( 'Custom redirect URL must be within this WordPress site.', 'admin-only' ), 'error' ); } } else { $sanitized['custom_redirect'] = ''; } } else { $sanitized['custom_redirect'] = ''; } return $sanitized; } /** * Main settings section callback */ function admon_settings_section_callback() { echo '

' . esc_html__( 'General Settings', 'admin-only' ) . '

'; echo '

' . esc_html__( 'Control which users can access the WordPress dashboard.', 'admin-only' ) . '

'; } /** * Session settings section callback */ function admon_session_section_callback() { echo '

' . esc_html__( 'Session Management', 'admin-only' ) . '

'; echo '

' . esc_html__( 'Automatically log out users after periods of inactivity for enhanced security.', 'admin-only' ) . '

'; } /** * Session timeout field callback */ function admon_session_timeout_callback() { $settings = get_option( 'admin_only_settings' ); $current_timeout = $settings['session_timeout'] ?? ''; $custom_hours = $settings['custom_timeout_hours'] ?? ''; $timeout_options = array( '' => __( 'Default WordPress', 'admin-only' ), 1 => __( '1 hour', 'admin-only' ), 2 => __( '2 hours', 'admin-only' ), 4 => __( '4 hours', 'admin-only' ), 8 => __( '8 hours', 'admin-only' ), 12 => __( '12 hours', 'admin-only' ), 24 => __( '24 hours', 'admin-only' ), 'custom' => __( 'Custom', 'admin-only' ), ); ?>


'', 'custom_timeout_hours' => '', 'apply_to_admins' => 0, 'override_remember_me' => 0, 'allowed_users' => '', 'custom_redirect' => '', ); update_option( 'admin_only_settings', $default_settings ); // Add success message add_settings_error( 'admin_only_settings', 'settings_reset', __( 'All settings have been reset to their default values.', 'admin-only' ), 'updated' ); } } add_action( 'admin_init', 'admon_handle_reset_settings' ); /** * Add contextual help tabs */ function admon_add_help_tabs() { $screen = get_current_screen(); if ( ! $screen ) { return; } // Overview Tab. $screen->add_help_tab( array( 'id' => 'admon_overview', 'title' => __( 'Overview', 'admin-only' ), 'content' => '

' . __( 'UserFlow allows you to restrict access to the WordPress dashboard to administrators only. You can optionally whitelist specific users or capabilities.', 'admin-only' ) . '

', ) ); // General Settings Tab. $screen->add_help_tab( array( 'id' => 'admon_access_control', 'title' => __( 'General Settings', 'admin-only' ), 'content' => '

' . __( 'Allowed Users: Enter a comma-separated list of usernames (e.g., john_doe, jane_doe) to allow specific non-admin users to access the dashboard.', 'admin-only' ) . '

' . '

' . __( 'Custom Redirect URL: By default, blocked users are redirected to the homepage. You can enter a custom URL here (must be within the same site) to redirect them to a specific page, such as a custom login or error page.', 'admin-only' ) . '

' . '

' . __( 'Note: WordPress AJAX requests (admin-ajax.php) are not blocked. Ensure any AJAX handlers perform their own capability checks.', 'admin-only' ) . '

', ) ); // Session Management Tab. $screen->add_help_tab( array( 'id' => 'admon_session', 'title' => __( 'Session Management', 'admin-only' ), 'content' => '

' . __( 'Auto-Logout: Automatically log users out after a specific period of inactivity. This improves security, especially for shared computers.', 'admin-only' ) . '

' . '

' . __( 'Include Administrators: By default, administrators are exempt from auto-logout. Check this option to enforce the timeout for admins as well.', 'admin-only' ) . '

' . '

' . __( 'Override "Remember Me": If checked, the session timeout will apply even if the user checked "Remember Me" during login. Otherwise, "Remember Me" sessions will last for 14 days.', 'admin-only' ) . '

', ) ); // Troubleshooting Tab. $screen->add_help_tab( array( 'id' => 'admon_troubleshooting', 'title' => __( 'Troubleshooting', 'admin-only' ), 'content' => '

' . __( 'Locked Out? If you accidentally lock yourself out, you can regain access by:', 'admin-only' ) . '

' . '
    ' . '
  1. ' . __( 'Accessing your site files via FTP or Hosting File Manager.', 'admin-only' ) . '
  2. ' . '
  3. ' . __( 'Navigating to /wp-content/plugins/.', 'admin-only' ) . '
  4. ' . '
  5. ' . __( 'Renaming the admin-only folder to admin-only-disabled.', 'admin-only' ) . '
  6. ' . '
  7. ' . __( 'This will automatically deactivate the plugin, allowing you to log in.', 'admin-only' ) . '
  8. ' . '
', ) ); // Help Sidebar. $screen->set_help_sidebar( '

' . __( 'For more information:', 'admin-only' ) . '

' . '

' . __( 'Plugin Page', 'admin-only' ) . '

' ); }