login_process = $login_process; $this->plugin_status = $plugin_status; $this->error_handler = $error_handler; $this->twig = $twig; } public function register_hook() { if ( $this->plugin_status->client_completed_registration() && $this->plugin_status->is_plugin_enabled() ) { add_filter( 'authenticate', array( $this, 'authenticate' ), 100 ); } } /** * @param null|WP_User|WP_Error $user * * @return null|WP_User|WP_Error */ public function authenticate( $user ) { $response = $this->login_process->authenticate( $user ); if ( $response instanceof JSON_Response ) { $status_code = $response->get_status_code(); $body = $response->get_body(); if ( 200 === $status_code ) { return new WP_User( $body['user_id'] ); } return $this->error( $body['error'] ); } elseif ( $response instanceof Redirection_Response ) { $response->redirect(); } elseif ( $response instanceof View_Response ) { try { echo $this->twig->try_render( $response->get_template(), $response->get_data() ); exit; } catch ( Exception $e ) { return $this->error_handler->capture_exception( $e )->to_wp_error( $e ); } } else { return $user; } } /** * @param string $message * * @return WP_Error */ private function error( $message ) { return new WP_Error( 'twofas_login_error', $message ); } }