From a34be2e4aab60bd85ab7451040afd0fa61e073aa Mon Sep 17 00:00:00 2001 From: kagg-design Date: Sun, 21 Jul 2024 18:03:38 +0300 Subject: [PATCH] Add integration with WPS Hide Login plugin. --- readme.txt | 1 + src/php/WP/Base.php | 68 +++++++++++++++++++++++++++++++++++++ src/php/WP/LostPassword.php | 21 ++---------- src/php/WP/Register.php | 31 +++++------------ 4 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 src/php/WP/Base.php diff --git a/readme.txt b/readme.txt index a2360d81..d50bec51 100644 --- a/readme.txt +++ b/readme.txt @@ -562,6 +562,7 @@ Instructions for popular native integrations are below: == Changelog == = 4.4.0 = +* Added integration with WPS Hide Login plugin. * Fixed conflict with Ninja Forms Upload field. * Fixed Ninja Forms Ajax processing. * Fixed error in cron with Matomo Analytics. diff --git a/src/php/WP/Base.php b/src/php/WP/Base.php new file mode 100644 index 00000000..ec5e2af8 --- /dev/null +++ b/src/php/WP/Base.php @@ -0,0 +1,68 @@ +new_login_url(), PHP_URL_PATH ); + } + + return '/wp-login.php'; + } + + /** + * Check if the current request is the login URL. + * + * @return bool + */ + private function is_login_url(): bool { + $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? + filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : + ''; + + $request_uri = wp_parse_url( $request_uri, PHP_URL_PATH ); + + return false !== strpos( $request_uri, $this->get_login_url() ); + } + + /** + * Get action. + * + * @return string + */ + private function get_action(): string { + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; + } + + /** + * Whether the current request is the login action. + * + * @return bool + */ + private function is_login_action(): bool { + return self::WP_LOGIN_ACTION === $this->get_action(); + } +} diff --git a/src/php/WP/LostPassword.php b/src/php/WP/LostPassword.php index cf4ec043..72838357 100644 --- a/src/php/WP/LostPassword.php +++ b/src/php/WP/LostPassword.php @@ -13,6 +13,7 @@ * Class LostPassword */ class LostPassword extends LostPasswordBase { + use Base; /** * Nonce action. @@ -39,11 +40,6 @@ class LostPassword extends LostPasswordBase { */ protected const POST_VALUE = null; - /** - * WP login URL. - */ - private const WP_LOGIN_URL = '/wp-login.php'; - /** * WP login action. */ @@ -55,20 +51,7 @@ class LostPassword extends LostPasswordBase { * @return void */ public function add_captcha(): void { - $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? - filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : - ''; - - $request_uri = wp_parse_url( $request_uri, PHP_URL_PATH ); - - if ( false === strpos( $request_uri, self::WP_LOGIN_URL ) ) { - return; - } - - // phpcs:ignore WordPress.Security.NonceVerification.Recommended - $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; - - if ( self::WP_LOGIN_ACTION !== $action ) { + if ( ! $this->is_login_url() || ! $this->is_login_action() ) { return; } diff --git a/src/php/WP/Register.php b/src/php/WP/Register.php index e79fa2f2..4ee70501 100644 --- a/src/php/WP/Register.php +++ b/src/php/WP/Register.php @@ -14,11 +14,7 @@ * Class Register */ class Register { - - /** - * WP login URL. - */ - private const WP_LOGIN_URL = '/wp-login.php'; + use Base; /** * Nonce action. @@ -30,6 +26,11 @@ class Register { */ private const NONCE = 'hcaptcha_registration_nonce'; + /** + * WP login action. + */ + private const WP_LOGIN_ACTION = 'register'; + /** * Constructor. */ @@ -53,20 +54,7 @@ private function init_hooks(): void { * @return void */ public function add_captcha(): void { - $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? - filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : - ''; - - $request_uri = wp_parse_url( $request_uri, PHP_URL_PATH ); - - if ( false === strpos( $request_uri, self::WP_LOGIN_URL ) ) { - return; - } - - // phpcs:ignore WordPress.Security.NonceVerification.Recommended - $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; - - if ( 'register' !== $action ) { + if ( ! $this->is_login_url() || ! $this->is_login_action() ) { return; } @@ -94,10 +82,7 @@ public function add_captcha(): void { * @noinspection PhpUnusedParameterInspection */ public function verify( $errors, string $sanitized_user_login, string $user_email ) { - // phpcs:ignore WordPress.Security.NonceVerification.Recommended - $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; - - if ( 'register' !== $action ) { + if ( ! $this->is_login_action() ) { return $errors; }