settings = Plugin::get_settings( 'users' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( isset( $this->settings['auto_logout_user'] ) && true === $this->settings['auto_logout_user'] && !isset( $this->settings['auto_logout_user_roles'] ) ) { if( isset( $this->settings['auto_logout_user_time_other'] ) ) { $this->timeout = (int) $this->settings['auto_logout_user_time_other']; } else if( isset( $this->settings['auto_logout_user_time'] ) ) { $this->timeout = (int) $this->settings['auto_logout_user_time'] ?? 3600; } add_filter( 'auth_cookie_expiration', [ $this, 'auth_cookie_expiration' ] ); } } public function adminease_settings_fields( array $fields ): array { $fields['users']['fields'][] = [ 'type' => 'switch', 'id' => 'auto-logout-user', 'name' => 'adminease[users][auto_logout_user]', 'value' => $this->settings['auto_logout_user'] ?? '', 'label_class' => 'adminease-switch', 'input_class' => 'form-control toggle-field', 'label' => __( 'Auto-logout user', 'adminease' ), 'description' => __( "The Login Session Timeout setting controls how long a user stays logged in before WordPress automatically logs them out. For example, if it’s set to 30 minutes, a user will need to log in again after that time—even if they’re still active. This helps improve your site's security by limiting how long login sessions remain open. You can choose a shorter timeout for better protection or a longer one for convenience.", 'adminease' ), 'child_fields' => [ [ 'type' => 'select', 'id' => 'auto-logout-user-time', 'name' => 'adminease[users][auto_logout_user_time]', 'value' => $this->settings['auto_logout_user_time'] ?? '', 'options' => [ '' => __( 'Select', 'adminease' ), '0' => __( 'Never log out', 'adminease' ), '300' => '5 minutes', '600' => '10 minutes', '900' => '15 minutes', '1200' => '20 minutes', '1800' => '30 minutes', '2700' => '45 minutes', '3600' => '1 hour', '7200' => '2 hours', '14400' => '4 hours', '28800' => '8 hours', '43200' => '12 hours', '86400' => '1 day', '604800' => '7 days', '1209600' => '14 days', '2419200' => '28 days', 'other' => __( 'Other', 'adminease' ), ], 'label_class' => 'adminease-label', 'input_class' => 'form-control toggle-field', 'wrapper_class' => 'form-group-child', 'label' => __( 'Auto-logout user after X seconds', 'adminease' ), 'field_description' => __( 'Select the login session limit.', 'adminease' ), 'attributes' => [ 'data-parent' => 'auto-logout-user', ], ], [ 'type' => 'number', 'id' => 'auto-logout-user-time-other', 'name' => 'adminease[users][auto_logout_user_time_other]', 'value' => $this->settings['auto_logout_user_time_other'] ?? '', 'label_class' => 'adminease-label', 'input_class' => 'form-control', 'wrapper_class' => 'form-group-child', 'label' => __( 'Custom time', 'adminease' ), 'field_description' => __( 'Set login session limit in seconds.', 'adminease' ), 'attributes' => [ 'min' => 0, 'data-parent' => 'auto-logout-user-time', ], ], ], ]; return $fields; } /** * Filters the expiration time of an authentication cookie based on user roles and the "remember me" setting. * * @param int $expiration The default expiration time of the authentication cookie in seconds. * @param int $user_id The ID of the user for whom the cookie expiration is being modified. * @param bool $remember Whether the "remember me" checkbox was checked when the user logged in. * * @return int The modified or original expiration time of the authentication cookie in seconds. */ public function auth_cookie_expiration( int $expiration, int $user_id, bool $remember ): int { if( $remember ) { return $this->timeout; } return $expiration; } }