Last active
May 22, 2022 22:34
-
-
Save Exieros/8917f9407bad62b36db5bd07edd0a29f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * Модификация формы авторизации. | |
| * Позволяет добавить капчу и изменить стиль формы авторизации. | |
| * | |
| * !Требуется composer-зависимость Gregwar\Captcha\CaptchaBuilder | |
| */ | |
| namespace Exieros\Utils; | |
| defined( 'ABSPATH' ) || exit; | |
| use Gregwar\Captcha\CaptchaBuilder; | |
| use WP_Error; | |
| class LoginFormMod{ | |
| /* | |
| * Создание сессии. Регистрация хуков. | |
| */ | |
| public function addCaptcha(): self{ | |
| session_start(); | |
| add_action( 'login_form', array('Exieros\Utils\LoginFormMod', 'login_form_hook')); | |
| remove_filter('authenticate', 'wp_authenticate_username_password'); | |
| add_filter('authenticate', array('Exieros\Utils\LoginFormMod', 'authenticate_custom_hook'), 20, 3); | |
| return $this; | |
| } | |
| public function addStyleMod(): self{ | |
| add_action( 'login_enqueue_scripts', array('Exieros\Utils\LoginFormMod', 'login_enqueue_hook') ); | |
| return $this; | |
| } | |
| /* | |
| * Хук добавляет генерирует капчу и добавляет ее в форму | |
| */ | |
| public static function login_form_hook(): void{ | |
| $builder = new CaptchaBuilder; | |
| $builder->setBackgroundColor(255, 255, 255); | |
| $builder->build(); | |
| $_SESSION['captcha'] = $builder->getPhrase(); | |
| echo <<<HTML | |
| <div id="captcha"> | |
| <img src="{$builder->inline()}" alt="captcha"/> | |
| <label for="captcha">Капча</label> | |
| <input type="text" name="captcha" class="captcha__input" size="5" /> | |
| </div> | |
| <style> | |
| #captcha label[for=captcha]{ | |
| display: block; | |
| } | |
| #captcha img{ | |
| display: block; | |
| margin: 0 auto; | |
| } | |
| </style> | |
| HTML; | |
| } | |
| /* | |
| * Хук срабатывает при попытке аутентификации, перед попыткой аутентификации проверяется введеная капча | |
| */ | |
| public static function authenticate_custom_hook($user, $username, $password){ | |
| if($_SERVER['REQUEST_METHOD'] !== 'POST'){ | |
| return $user; | |
| } | |
| if( empty($_POST['captcha']) || empty($_SESSION['captcha']) ){ | |
| return new WP_Error( 'authentication_failed', 'Капча не решена' ); | |
| } | |
| if( strcasecmp( $_POST['captcha'], $_SESSION['captcha'] ) !== 0 ){ | |
| return new WP_Error( 'authentication_failed', 'Капча не решена' ); | |
| } | |
| return wp_authenticate_username_password($user, $username, $password); | |
| } | |
| /* | |
| * Хук добавляет пользовательские стили на страницу авторизации. | |
| * @TODO: Вынести в отд. файл. | |
| */ | |
| public static function login_enqueue_hook(): void{ | |
| ?> | |
| <style type="text/css"> | |
| #login h1{ | |
| display: none; | |
| } | |
| #login .forgetmenot{ | |
| text-align: center; | |
| width: 100%; | |
| margin-bottom: 15px; | |
| } | |
| #login .submit{ | |
| width: 100%; | |
| display: grid; | |
| } | |
| #user_login, #user_pass, #user_email, #captcha .captcha__input{ | |
| box-shadow: 0 0 0px 1000px #f7f7f7 inset; | |
| border: none; | |
| border-radius: 10px; | |
| font-size: 15px; | |
| padding: 10px 15px; | |
| } | |
| #loginform, #registerform, #lostpasswordform, #resetpassform{ | |
| margin-top: 0px; | |
| border-radius: 20px; | |
| padding-bottom: 20px; | |
| border: none; | |
| } | |
| #wp-submit, #login_error .wp-generate-pw{ | |
| border-radius: 10px; | |
| } | |
| button.button.wp-generate-pw{ | |
| border-radius: 10px!important; | |
| } | |
| .submit.reset-pass-submit{ | |
| display: flex!important; | |
| justify-content: center!important; | |
| } | |
| .submit.reset-pass-submit > *{ | |
| width: inherit!important; | |
| } | |
| #nav{ | |
| text-align: center; | |
| } | |
| #loginform label, #registerform label{ | |
| color: #ccc; | |
| } | |
| #backtoblog{ | |
| display: none; | |
| } | |
| #login{ | |
| padding: 0!important; | |
| z-index: 2; | |
| } | |
| body{ | |
| display: flex; | |
| background-image: url(/wp-content/themes/handmade/public/images/login_page_bg.jpeg)!important; | |
| backdrop-filter: blur(5px); | |
| background-size: cover!important; | |
| background-position: center!important; | |
| } | |
| body:before { | |
| content: ''; | |
| width: 100%; | |
| height: 100%; | |
| position: absolute; | |
| background: #fff3; | |
| } | |
| .message, #login_error{ | |
| border-radius: 10px; | |
| } | |
| </style> | |
| <?php | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment