Skip to content

Commit

Permalink
Add integration with WPS Hide Login plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 21, 2024
1 parent 88b9bac commit a34be2e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 42 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions src/php/WP/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Base trait file.
*
* @package hcaptcha-wp
*/

// phpcs:disable Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
// phpcs:enable Generic.Commenting.DocComment.MissingShort

namespace HCaptcha\WP;

use WPS\WPS_Hide_Login\Plugin;

/**
* Base trait.
*/
trait Base {
/**
* Get login URL.
*
* @return string
*/
private function get_login_url(): string {
if ( class_exists( Plugin::class ) ) {
// Integration with WPS Hide Login plugin.
return wp_parse_url( Plugin::get_instance()->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();
}
}
21 changes: 2 additions & 19 deletions src/php/WP/LostPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Class LostPassword
*/
class LostPassword extends LostPasswordBase {
use Base;

/**
* Nonce action.
Expand All @@ -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.
*/
Expand All @@ -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;
}

Expand Down
31 changes: 8 additions & 23 deletions src/php/WP/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
* Class Register
*/
class Register {

/**
* WP login URL.
*/
private const WP_LOGIN_URL = '/wp-login.php';
use Base;

/**
* Nonce action.
Expand All @@ -30,6 +26,11 @@ class Register {
*/
private const NONCE = 'hcaptcha_registration_nonce';

/**
* WP login action.
*/
private const WP_LOGIN_ACTION = 'register';

/**
* Constructor.
*/
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a34be2e

Please sign in to comment.