config = new Two_Factor_Config(); $this->settings_page = new Settings_Page(); $this->repository = new User_Settings_Repository(); $this->otp_manager = new OTP_Manager(); $this->recovery_codes = new Recovery_Codes(); } /** * Register WordPress hooks. * * @return void */ public function register_hooks(): void { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Register the REST routes. * * @return void */ public function register_routes(): void { register_rest_route( self::REST_NAMESPACE, '/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( self::REST_NAMESPACE, '/users', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_users' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'role' => array( 'type' => 'string', 'description' => __( 'Restrict the list to a single role slug.', 'robotstxt-2fa' ), 'sanitize_callback' => 'sanitize_key', ), 'status' => array( 'type' => 'string', 'description' => __( 'Filter by 2FA status: enabled, required, or disabled.', 'robotstxt-2fa' ), 'enum' => array( 'enabled', 'required', 'disabled' ), 'sanitize_callback' => 'sanitize_key', ), ), ) ); } /** * Permission callback — every endpoint requires the admin capability. * * @return bool|\WP_Error */ public function check_permission() { if ( ! current_user_can( self::CAPABILITY ) ) { return new \WP_Error( 'robotstxt_2fa_rest_forbidden', __( 'You are not allowed to manage 2FA settings.', 'robotstxt-2fa' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Retrieve the full 2FA settings object. * * @param WP_REST_Request $request Incoming request. * * @return WP_REST_Response */ public function get_settings( WP_REST_Request $request ): WP_REST_Response { unset( $request ); return rest_ensure_response( $this->settings_page->get_settings() ); } /** * Update the 2FA settings from a JSON body. * * Reuses {@see Settings_Page::sanitize_settings()} so the REST write path * applies the exact same validation as the admin settings form. * * @param WP_REST_Request $request Incoming request with a JSON settings body. * * @return WP_REST_Response|\WP_Error */ public function update_settings( WP_REST_Request $request ) { $sanitized = $this->settings_page->sanitize_settings( $request->get_json_params() ); update_option( Settings_Page::OPTION_NAME, $sanitized, false ); if ( is_multisite() ) { update_site_option( Settings_Page::OPTION_NAME, $sanitized ); } return rest_ensure_response( $this->settings_page->get_settings() ); } /** * List users and their 2FA status, optionally filtered by role and status. * * @param WP_REST_Request $request Incoming request. * * @return WP_REST_Response */ public function get_users( WP_REST_Request $request ): WP_REST_Response { $query_args = array( 'number' => -1, 'fields' => 'all', ); $role = $request->get_param( 'role' ); if ( is_string( $role ) && '' !== $role ) { $query_args['role'] = sanitize_key( $role ); } $status_filter = $request->get_param( 'status' ); if ( is_string( $status_filter ) && '' === $status_filter ) { $status_filter = ''; } $users = get_users( $query_args ); $out = array(); foreach ( $users as $user ) { if ( ! $user instanceof \WP_User ) { continue; } $entry = $this->build_user_entry( $user ); if ( is_string( $status_filter ) && '' !== $status_filter && $entry['status'] !== $status_filter ) { continue; } $out[] = $entry; } return rest_ensure_response( $out ); } /** * Build the 2FA status payload for a single user. * * @param \WP_User $user User to describe. * * @return array */ private function build_user_entry( \WP_User $user ): array { $settings = $this->repository->get_user_settings( $user->ID ); $required = $this->config->get_required_methods_for_user( $user ); $forced = ! empty( $required ); $enabled = $settings['enabled']; return array( 'id' => $user->ID, 'login' => $user->user_login, 'email' => $user->user_email, 'roles' => $user->roles, 'enabled' => $enabled, 'forced' => $forced, 'status' => $enabled ? 'enabled' : ( $forced ? 'required' : 'disabled' ), 'methods' => $settings['methods'], 'required_methods' => $required, 'frequency' => $settings['frequency'], 'preferred_method' => $settings['preferred_method'], 'has_otp' => '' !== $this->otp_manager->get_secret( $user->ID ), 'unused_recovery_codes' => $this->recovery_codes->count_unused_codes( $user->ID ), ); } }