Skip to content

Commit f1c2c77

Browse files
Initial commit
0 parents  commit f1c2c77

9 files changed

Lines changed: 500 additions & 0 deletions

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Ultimate Member - Math Captcha in Register forms
2+
3+
Displays Math Captcha field in the registration form. Math Captcha allows you to use simple math problems to test site users to prove their human identity.
4+
5+
## Installation
6+
7+
Follow instructions in this [guide](https://docs.ultimatemember.com/article/1663-download-installation-of-the-basic-extensions) to download and install the extension.
8+
9+
## How to use
10+
11+
Once the plugin is activated, it is automatically enabled. Go to your site's registration page to check and test the Math Captcha.
12+
13+
### Screenshots
14+
15+
Image - Math Captcha field in the registration form
16+
![WP, Ultimate Member, Math Captcha](https://d33v4339jhl8k0.cloudfront.net/docs/assets/561c96629033600a7a36d662/images/61e5444e8200bc052eb7f510/file-NoJi5tjkVR.png)
17+
18+
## License
19+
20+
GNU Version 2 or Any Later Version

includes/class-math-captcha.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/**
4+
* PHP-based Simple Math Captcha
5+
*
6+
* @version 1.0
7+
* @author Nettraction <dev@nettraction.in>
8+
*
9+
*/
10+
Class MathCaptcha {
11+
12+
private $min;
13+
private $max;
14+
private $session_var;
15+
private $result;
16+
private $operand1;
17+
private $operand2;
18+
private $operator;
19+
private $op_symbols = array('+', '-', '*');
20+
21+
/**
22+
*
23+
* @param string $sess_var
24+
* @param int $min_val
25+
* @param int $max_val
26+
*/
27+
function __construct($sess_var = 'math_captcha_result', $min_val = 0, $max_val = 10) {
28+
29+
$this->min = ($min_val <= 0) ? 0 : $min_val;
30+
$this->max = ($max_val <= $this->min) ? 10 : $max_val;
31+
32+
if (!empty($sess_var)) {
33+
$this->session_var = $sess_var;
34+
} else {
35+
$this->session_var = 'math_captcha_result';
36+
}
37+
}
38+
39+
/**
40+
* Generate a new captcha and save the result into a session variable.
41+
*
42+
*/
43+
public function reset_captcha() {
44+
45+
if (!session_id())
46+
session_start();
47+
48+
$this->operand1 = rand($this->min, $this->max);
49+
$this->operand2 = rand($this->min, $this->max);
50+
$this->operator = $this->op_symbols[rand(0, (count($this->op_symbols) - 1))];
51+
52+
$this->compute_result();
53+
54+
// Save to $_SESSION
55+
$_SESSION[$this->session_var] = $this->result;
56+
}
57+
58+
private function compute_result() {
59+
60+
switch ($this->operator) {
61+
case '+':
62+
$this->result = ($this->operand1 + $this->operand2);
63+
break;
64+
65+
case '-':
66+
$this->result = ($this->operand1 - $this->operand2);
67+
break;
68+
69+
case '*':
70+
$this->result = ($this->operand1 * $this->operand2);
71+
break;
72+
}
73+
}
74+
75+
/**
76+
*
77+
* @param int $val Value to be compared to the result in session
78+
* @return boolean TRUE if the value matches; FALSE otherwise
79+
*/
80+
public function validate($val) {
81+
if (!session_id())
82+
session_start();
83+
84+
if ($val == $_SESSION[$this->session_var]) {
85+
return TRUE;
86+
} else {
87+
return FALSE;
88+
}
89+
}
90+
91+
/**
92+
*
93+
* @param string $format sprintf compatible format with text/html e.g "Compute Sum of {operand1} and {operand2}"
94+
* @return type
95+
*/
96+
public function get_captcha_text($format = '{operand1} {operator} {operand2}') {
97+
if (!empty($format)) {
98+
return str_replace(
99+
array('{operand1}', '{operand2}', '{operator}')
100+
, array($this->operand1, $this->operand2, $this->operator)
101+
, $format);
102+
} else {
103+
return sprintf("%d %s %d", $this->operand1, $this->operator, $this->operand2);
104+
}
105+
}
106+
107+
/**
108+
* Makes it sums only!
109+
*/
110+
public function sums_only_please(){
111+
$this->op_symbols = array('+');
112+
}
113+
114+
}

includes/class-um-math-captcha.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Init the extension.
4+
*
5+
* @package um_ext\um_math_captcha
6+
*/
7+
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
exit;
10+
}
11+
12+
/**
13+
* Class UM_Math_Captcha
14+
*/
15+
class UM_Math_Captcha {
16+
17+
/**
18+
* PHP-based Simple Math Captcha
19+
*
20+
* @var \MathCaptcha
21+
*/
22+
public $cpa;
23+
24+
25+
/**
26+
* Math Captcha field's name
27+
*
28+
* @var string
29+
*/
30+
public $key = 'um_math_challenge';
31+
32+
33+
/**
34+
* An instance of the class.
35+
*
36+
* @var UM_Math_Captcha
37+
*/
38+
private static $instance;
39+
40+
41+
/**
42+
* Creates an instance of the class.
43+
*
44+
* @return UM_Math_Captcha
45+
*/
46+
public static function instance() {
47+
if ( is_null( self::$instance ) ) {
48+
self::$instance = new self();
49+
}
50+
return self::$instance;
51+
}
52+
53+
54+
/**
55+
* UM_Math_Captch constructor.
56+
*/
57+
public function __construct() {
58+
if ( ! class_exists( 'MathCaptcha' ) ) {
59+
require_once 'class-math-captcha.php';
60+
}
61+
$this->cpa = new MathCaptcha();
62+
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 );
65+
}
66+
67+
68+
/**
69+
* Add Math Captcha field to the registration form.
70+
*
71+
* @param array $args UM form data.
72+
*/
73+
public function add_field( $args ) {
74+
$this->cpa->reset_captcha();
75+
76+
$field_data = array(
77+
'type' => 'text',
78+
'label' => __( 'Solve this simple Math: ', 'um-math-captcha' ) . $this->cpa->get_captcha_text() . ' = ?',
79+
'name' => $this->key,
80+
'help' => '',
81+
'placeholder' => __( 'Your answer...', 'um-math-captcha' ),
82+
'icon' => '',
83+
'default' => null,
84+
'required' => true,
85+
'editable' => true,
86+
);
87+
88+
$data = apply_filters( "um_get_field__{$this->key}", $field_data );
89+
90+
?>
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>
96+
</div>
97+
<?php
98+
99+
if ( UM()->fields()->is_error( $this->key ) ) {
100+
$text = UM()->fields()->show_error( $this->key );
101+
echo UM()->fields()->field_error( $text, $this->key );
102+
}
103+
}
104+
105+
106+
/**
107+
* Validate Math Captcha field when the registration form has been submitted.
108+
*
109+
* @param array $post_form Submission array.
110+
* @param array $form_data UM form data.
111+
*/
112+
public function validate( $post_form, $form_data ) {
113+
if ( empty( $post_form[ $this->key ] ) ) {
114+
UM()->form()->add_error( $this->key, __( 'Math Captcha is required.', 'um-math-captcha' ) );
115+
} elseif ( ! $this->cpa->validate( $post_form[ $this->key ] ) ) {
116+
UM()->form()->add_error( $this->key, __( 'Incorrect answer. Please try again.', 'um-math-captcha' ) );
117+
}
118+
}
119+
120+
}

languages/um-math-captcha-de_DE.mo

1.39 KB
Binary file not shown.

languages/um-math-captcha-de_DE.po

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: Ultimate Member - Math Captcha in Register form\n"
4+
"Report-Msgid-Bugs-To: \n"
5+
"POT-Creation-Date: 2023-10-28 21:51+0000\n"
6+
"PO-Revision-Date: 2023-10-29 10:20+0000\n"
7+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8+
"Language-Team: Unknown\n"
9+
"Language: zxx\n"
10+
"Plural-Forms: nplurals=2; plural=n!=1;\n"
11+
"MIME-Version: 1.0\n"
12+
"Content-Type: text/plain; charset=UTF-8\n"
13+
"Content-Transfer-Encoding: 8bit\n"
14+
"X-Loco-Source-Locale: de_DE\n"
15+
"X-Generator: Loco https://localise.biz/\n"
16+
"X-Loco-Version: 2.6.6; wp-6.4-RC2\n"
17+
"X-Domain: um-math-captcha\n"
18+
"X-Loco-Parser: loco_parse_po"
19+
20+
#. Author URI of the plugin
21+
msgid "http://ultimatemember.com/"
22+
msgstr ""
23+
24+
#: includes/class-um-math-captcha.php:107
25+
msgid "Incorrect answer. Please try again."
26+
msgstr "Falsche Antwort. Bitte nochmal versuchen!"
27+
28+
#: includes/class-um-math-captcha.php:105
29+
msgid "Math Captcha is required."
30+
msgstr "Mathe-Captcha ist erforderlich."
31+
32+
#: includes/class-um-math-captcha.php:70
33+
msgid "Solve this simple Math: "
34+
msgstr "Lösen Sie diese einfache Rechnung:"
35+
36+
#. %s - plugin name.
37+
#: um-math-captcha.php:39
38+
#, php-format
39+
msgid ""
40+
"The <strong>%s</strong> extension requires the Ultimate Member plugin to be "
41+
"activated to work properly. You can download it <a href=\"https://wordpress."
42+
"org/plugins/ultimate-member\">here</a>"
43+
msgstr ""
44+
"Das Ultimate Member-Plugin muss aktiviert sein, damit die <strong>%s</strong>"
45+
" Erweiterung richtig funktioniert. Du kannst es <a href=\"https://wordpress."
46+
"org/plugins/ultimate-member\">hier</a> herunterladen."
47+
48+
#. Description of the plugin
49+
msgid "This plugin adds math challenge in the Register forms"
50+
msgstr "Dieses Plugin fügt Rechenaufgaben in das Registrier-Formular ein."
51+
52+
#. Author of the plugin
53+
msgid "Ultimate Member"
54+
msgstr ""
55+
56+
#. Name of the plugin
57+
msgid "Ultimate Member - Math Captcha in Register form"
58+
msgstr ""
59+
60+
#: includes/class-um-math-captcha.php:78
61+
msgid "Your answer..."
62+
msgstr "Ihre Antwort..."

languages/um-math-captcha-en_US.mo

532 Bytes
Binary file not shown.

languages/um-math-captcha-en_US.po

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: Ultimate Member - Math Captcha in Register form\n"
4+
"Report-Msgid-Bugs-To: \n"
5+
"POT-Creation-Date: 2023-10-28 21:51+0000\n"
6+
"PO-Revision-Date: 2023-10-28 21:52+0000\n"
7+
"Last-Translator: Denis Baran\n"
8+
"Language-Team: English (United States)\n"
9+
"Language: en_US\n"
10+
"Plural-Forms: nplurals=2; plural=n != 1;\n"
11+
"MIME-Version: 1.0\n"
12+
"Content-Type: text/plain; charset=UTF-8\n"
13+
"Content-Transfer-Encoding: 8bit\n"
14+
"X-Generator: Loco https://localise.biz/\n"
15+
"X-Loco-Version: 2.6.6; wp-6.4-RC2\n"
16+
"X-Domain: um-math-captcha"
17+
18+
#. Author URI of the plugin
19+
msgid "http://ultimatemember.com/"
20+
msgstr ""
21+
22+
#: includes/class-um-math-captcha.php:107
23+
msgid "Incorrect answer. Please try again."
24+
msgstr ""
25+
26+
#: includes/class-um-math-captcha.php:105
27+
msgid "Math Captcha is required."
28+
msgstr ""
29+
30+
#: includes/class-um-math-captcha.php:70
31+
msgid "Solve this simple Math: "
32+
msgstr ""
33+
34+
#. %s - plugin name.
35+
#: um-math-captcha.php:39
36+
#, php-format
37+
msgid ""
38+
"The <strong>%s</strong> extension requires the Ultimate Member plugin to be "
39+
"activated to work properly. You can download it <a href=\"https://wordpress."
40+
"org/plugins/ultimate-member\">here</a>"
41+
msgstr ""
42+
43+
#. Description of the plugin
44+
msgid "This plugin adds math challenge in the Register forms"
45+
msgstr ""
46+
47+
#. Author of the plugin
48+
msgid "Ultimate Member"
49+
msgstr ""
50+
51+
#. Name of the plugin
52+
msgid "Ultimate Member - Math Captcha in Register form"
53+
msgstr ""
54+
55+
#: includes/class-um-math-captcha.php:78
56+
msgid "Your answer..."
57+
msgstr ""

0 commit comments

Comments
 (0)