request = $request; $this->flash = $flash; $this->twig = $twig; $this->error_handler = $error_handler; } /** * @param JSON_Response|Redirect_Response|View_Response|Not_Handled_Response|null $response * * @return WP_Error|WP_User|void */ public function process( $response ) { if ( $this->should_response_be_changed( $response ) ) { $this->flash->add_message( 'error', $response->get_body()['error'] ); $response = $this->safe_redirect(); } if ( $response instanceof JSON_Response ) { $status_code = $response->get_status_code(); $body = $response->get_body(); if ( Code::OK === $status_code ) { $this->response = new WP_User( $body['user_id'] ); } else { $this->response = $this->error( $body['error'] ); } } if ( $response instanceof Redirect_Response ) { $response->redirect(); } if ( $response instanceof View_Response ) { try { echo $this->twig->try_render( $response->get_template(), $response->get_data() ); Plugin::terminate(); } catch ( Exception $e ) { return $this->error_handler->capture_exception( $e )->to_wp_error( $e ); } } } public function should_response_be_returned(): bool { return ! is_null( $this->response ); } /** * @return WP_Error|WP_User|null */ public function get_response() { return $this->response; } private function should_response_be_changed( $response ): bool { return $this->is_jetpack_sso_login() && $response instanceof JSON_Response && Code::OK !== $response->get_status_code(); } private function is_jetpack_sso_login(): bool { return did_action( 'jetpack_sso_handle_login' ) > 0; } private function safe_redirect(): Safe_Redirect_Response { $login_url = wp_login_url(); $interim_login = $this->request->request( 'interim-login' ); if ( $interim_login ) { $login_url = add_query_arg( 'interim-login', '1', $login_url ); } return new Safe_Redirect_Response( new Direct_URL ( $login_url ) ); } private function error( string $message ): WP_Error { return new WP_Error( 'twofas_light_login_error', $message ); } }