Skip to content

Commit edd8a4b

Browse files
Refactor Math Captcha plugin and fix dependency handling.
Removed unnecessary parameters and extra spaces for cleaner code. Improved dependency check logic to ensure compatibility with Ultimate Member and fixed admin notice HTML sanitization. Updated plugin version to 1.1.2.
1 parent 62ea498 commit edd8a4b

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

includes/class-um-math-captcha.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,20 @@ class UM_Math_Captcha {
2121
*/
2222
public $cpa;
2323

24-
2524
/**
2625
* Math Captcha field's name
2726
*
2827
* @var string
2928
*/
3029
public $key = 'um_math_challenge';
3130

32-
3331
/**
3432
* An instance of the class.
3533
*
3634
* @var UM_Math_Captcha
3735
*/
3836
private static $instance;
3937

40-
4138
/**
4239
* Creates an instance of the class.
4340
*
@@ -50,7 +47,6 @@ public static function instance() {
5047
return self::$instance;
5148
}
5249

53-
5450
/**
5551
* UM_Math_Captch constructor.
5652
*/
@@ -60,17 +56,14 @@ public function __construct() {
6056
}
6157
$this->cpa = new MathCaptcha();
6258

63-
add_action( 'um_after_register_fields', array( $this, 'add_field' ), 10, 1 );
64-
add_action( 'um_submit_form_errors_hook__registration', array( $this, 'validate' ), 10, 2 );
59+
add_action( 'um_after_register_fields', array( $this, 'add_field' ) );
60+
add_action( 'um_submit_form_errors_hook__registration', array( $this, 'validate' ) );
6561
}
6662

67-
6863
/**
6964
* Add Math Captcha field to the registration form.
70-
*
71-
* @param array $args UM form data.
7265
*/
73-
public function add_field( $args ) {
66+
public function add_field() {
7467
$this->cpa->reset_captcha();
7568

7669
$field_data = array(
@@ -86,35 +79,30 @@ public function add_field( $args ) {
8679
);
8780

8881
$data = apply_filters( "um_get_field__{$this->key}", $field_data );
89-
9082
?>
91-
<div class="um-field um-field-text">
92-
<?php echo UM()->fields()->field_label( $data['label'], $this->key, $data ); ?>
93-
<div class="um-field-area">
94-
<input type="text" name="<?php echo esc_attr( $data['name'] ); ?>" value="" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" />
95-
</div>
83+
<div class="um-field um-field-text">
84+
<?php echo UM()->fields()->field_label( $data['label'], $this->key, $data ); ?>
85+
<div class="um-field-area">
86+
<input type="text" name="<?php echo esc_attr( $data['name'] ); ?>" value="" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" />
9687
</div>
88+
</div>
9789
<?php
98-
9990
if ( UM()->fields()->is_error( $this->key ) ) {
10091
$text = UM()->fields()->show_error( $this->key );
10192
echo UM()->fields()->field_error( $text, $this->key );
10293
}
10394
}
10495

105-
10696
/**
10797
* Validate Math Captcha field when the registration form has been submitted.
10898
*
10999
* @param array $post_form Submission array.
110-
* @param array $form_data UM form data.
111100
*/
112-
public function validate( $post_form, $form_data ) {
101+
public function validate( $post_form ) {
113102
if ( empty( $post_form[ $this->key ] ) ) {
114103
UM()->form()->add_error( $this->key, __( 'Math Captcha is required.', 'um-math-captcha' ) );
115104
} elseif ( ! $this->cpa->validate( $post_form[ $this->key ] ) ) {
116105
UM()->form()->add_error( $this->key, __( 'Incorrect answer. Please try again.', 'um-math-captcha' ) );
117106
}
118107
}
119-
120-
}
108+
}

um-math-captcha.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* Requires at least: 6.5
1212
* Requires PHP: 7.4
1313
* UM version: 2.9.0
14-
* Version: 1.1.1
15-
*
14+
* Version: 1.1.2
15+
* Requires Plugins: ultimate-member
1616
* @package um_ext\um_math_captcha
1717
*/
1818

@@ -37,13 +37,19 @@
3737
function um_math_captcha_check_dependencies() {
3838
if ( ! defined( 'um_path' ) || ! function_exists( 'UM' ) || ! UM()->dependencies()->ultimatemember_active_check() ) {
3939
// Ultimate Member is not active.
40-
add_action(
41-
'admin_notices',
42-
function () {
43-
// translators: %s - plugin name.
44-
echo '<div class="error"><p>' . wp_kses_post( sprintf( __( 'The <strong>%s</strong> extension requires the Ultimate Member plugin to be activated to work properly. You can download it <a href="https://wordpress.org/plugins/ultimate-member">here</a>', 'um-math-captcha' ), um_math_captcha_extension ) ) . '</p></div>';
45-
}
46-
);
40+
function um_math_captcha_dependencies() {
41+
$allowed_html = array(
42+
'a' => array(
43+
'href' => array(),
44+
'target' => true,
45+
),
46+
'strong' => array(),
47+
'br' => array(),
48+
);
49+
// translators: %s - plugin name.
50+
echo '<div class="error"><p>' . wp_kses( sprintf( __( 'The <strong>%s</strong> extension requires the Ultimate Member plugin to be activated to work properly. You can download it <a href="https://wordpress.org/plugins/ultimate-member">here</a>', 'um-math-captcha' ), um_math_captcha_extension ), $allowed_html ) . '</p></div>';
51+
}
52+
add_action( 'admin_notices', 'um_math_captcha_dependencies' );
4753
} else {
4854
require_once 'includes/class-um-math-captcha.php';
4955
UM()->set_class( 'Math_Captcha', true );

0 commit comments

Comments
 (0)