login_support = $login_support; $this->user_storage = $login_support->get_user_storage(); } /** * @param null|WP_Error|WP_User $user * * @return bool */ abstract public function supports( $user ); /** * @param null|WP_Error|WP_User $user * * @return bool|JSON_Response|Redirection_Response|View_Response */ abstract protected function handle( $user ); /** * @param null|WP_Error|WP_User $user * * @return bool|JSON_Response|Redirection_Response|View_Response If false is returned, it means that a login request could not be handled by any of the login handlers. */ public function authenticate( $user ) { if ( $this->user_storage->is_wp_user_set() ) { $this->user_storage->reset_wp_user(); } if ( $this->is_wp_user( $user ) ) { $this->user_storage->set_wp_user( $user ); } return $this->supports( $user ) ? $this->handle( $user ) : $this->fallback( $user ); } /** * @param Login_Handler $successor * * @return Login_Handler */ public function then( Login_Handler $successor ) { return $this->successor = $successor; } /** * @param null|WP_Error|WP_User $user * * @return bool|JSON_Response|Redirection_Response|View_Response */ protected function fallback( $user ) { if ( $this->successor ) { return $this->successor->authenticate( $user ); } return false; } /** * @param array $body * @param int $status_code * * @return JSON_Response */ protected function json( array $body, $status_code ) { return new JSON_Response( $body, $status_code ); } /** * @param string $message * @param int $status_code * * @return JSON_Response */ protected function json_error( $message, $status_code ) { return $this->json( array( 'error' => $message ), $status_code ); } /** * @param string $template * @param array $data * * @return View_Response */ protected function view( $template, array $data = array() ) { return new View_Response( $template, $data ); } /** * @param string $url * * @return Redirection_Response */ protected function redirection( $url ) { return new Redirection_Response( '', '', $url ); } /** * @param Exception $e */ protected function capture_exception( Exception $e ) { $this->login_support->capture_exception( $e ); } /** * @return WP_User * * @throws User_Not_Found_Exception */ protected function get_wp_user() { return $this->login_support->get_wp_user(); } /** * @return int * * @throws User_Not_Found_Exception */ protected function get_user_id() { return $this->get_wp_user()->ID; } /** * @return array * * @throws User_Not_Found_Exception */ protected function get_roles() { return $this->get_wp_user()->roles; } /** * @return null|IntegrationUser * * @throws API_Exception * @throws User_Not_Found_Exception */ protected function get_integration_user() { return $this->login_support->get_integration_user( $this->get_user_id() ); } /** * @param null|WP_Error|WP_User $user * * @return bool */ protected function is_wp_user( $user ) { return $user instanceof WP_User; } }