Math Captcha - Version 1.2.7

Version Description

  • Fix: Contact form 7 compatibility fix
  • Tweak: Removed local translation files in favor of WP repository translations.
Download this release

Release Info

Developer dfactory
Plugin Icon 128x128 Math Captcha
Version 1.2.7
Comparing to
See all releases

Code changes from version 1.2.6 to 1.2.7

css/admin.css CHANGED
@@ -1,20 +1,20 @@
1
- /* Math Captcha settings */
2
- .df-credits { float: right; width: 280px; background: #fff; margin: 20px -300px 20px 20px; position: relative; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05); }
3
- .df-credits .inner { padding-left: 10px; padding-right: 10px; }
4
- .df-credits h3 { font-size: 14px; line-height: 1.4; margin: 0; padding: 8px 12px; border-bottom: 1px solid #eee; }
5
- .df-credits .df-link { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; font-style:italic; padding-bottom: 10px; margin: 0; }
6
- .df-credits .df-link a { vertical-align: middle; padding: 5px 0 0 4px; }
7
- .math-captcha-settings .df-credits form { min-width: 260px; margin-bottom: 1em; }
8
- .math-captcha-settings .df-credits form input { margin: 0; padding: 0; }
9
- .math-captcha-settings { margin-right: 300px; }
10
- .math-captcha-settings hr, .df-credits hr { border: solid #eee; border-width: 1px 0 0; clear: both; height: 0; }
11
- .math-captcha-settings form { float: left; min-width: 463px; width: auto; }
12
- .math-captcha-settings fieldset span.description { margin-bottom: 8px; margin-top: 4px; display: block; }
13
- .math-captcha-settings .ui-button { margin-bottom: 5px; }
14
- .math-captcha-settings .description { font-size: 13px; margin-bottom: 8px; }
15
-
16
- /* All Mobile Sizes (devices and browser) */
17
- @media only screen and (max-width: 959px) {
18
- .df-credits { width: 100%; float: none; margin: 20px 0; }
19
- .math-captcha-settings { margin-right: 0; }
20
  }
1
+ /* Math Captcha settings */
2
+ .df-credits { float: right; width: 280px; background: #fff; margin: 20px -300px 20px 20px; position: relative; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05); }
3
+ .df-credits .inner { padding-left: 10px; padding-right: 10px; }
4
+ .df-credits h3 { font-size: 14px; line-height: 1.4; margin: 0; padding: 8px 12px; border-bottom: 1px solid #eee; }
5
+ .df-credits .df-link { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; font-style:italic; padding-bottom: 10px; margin: 0; }
6
+ .df-credits .df-link a { vertical-align: middle; padding: 5px 0 0 4px; }
7
+ .math-captcha-settings .df-credits form { min-width: 260px; margin-bottom: 1em; }
8
+ .math-captcha-settings .df-credits form input { margin: 0; padding: 0; }
9
+ .math-captcha-settings { margin-right: 300px; }
10
+ .math-captcha-settings hr, .df-credits hr { border: solid #eee; border-width: 1px 0 0; clear: both; height: 0; }
11
+ .math-captcha-settings form { float: left; min-width: 463px; width: auto; }
12
+ .math-captcha-settings fieldset span.description { margin-bottom: 8px; margin-top: 4px; display: block; }
13
+ .math-captcha-settings .ui-button { margin-bottom: 5px; }
14
+ .math-captcha-settings .description { font-size: 13px; margin-bottom: 8px; }
15
+
16
+ /* All Mobile Sizes (devices and browser) */
17
+ @media only screen and (max-width: 959px) {
18
+ .df-credits { width: 100%; float: none; margin: 20px 0; }
19
+ .math-captcha-settings { margin-right: 0; }
20
  }
css/frontend.css CHANGED
@@ -1,16 +1,16 @@
1
- .math-captcha-form {
2
- display:block;
3
- float:none;
4
- clear:both;
5
- }
6
-
7
- .login form .math-captcha-form span {
8
- display: inline-block;
9
- margin-bottom: 16px;
10
- }
11
- input[type="text"].mc-input {
12
- display: inline-block;
13
- width: 60px!important;
14
- vertical-align: middle;
15
- margin-bottom: 0;
16
- }
1
+ .math-captcha-form {
2
+ display:block;
3
+ float:none;
4
+ clear:both;
5
+ }
6
+
7
+ .login form .math-captcha-form span {
8
+ display: inline-block;
9
+ margin-bottom: 16px;
10
+ }
11
+ input[type="text"].mc-input {
12
+ display: inline-block;
13
+ width: 60px!important;
14
+ vertical-align: middle;
15
+ margin-bottom: 0;
16
+ }
includes/class-cookie-session.php CHANGED
@@ -1,67 +1,71 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) )
3
- exit;
4
-
5
- new Math_Captcha_Cookie_Session();
6
-
7
- class Math_Captcha_Cookie_Session {
8
-
9
- public $session_ids;
10
-
11
- public function __construct() {
12
- // sets instance
13
- Math_Captcha()->cookie_session = $this;
14
-
15
- // actions
16
- add_action( 'plugins_loaded', array( &$this, 'init_session' ), 1 );
17
- }
18
-
19
- /**
20
- * Initializes cookie-session
21
- */
22
- public function init_session() {
23
- if ( is_admin() )
24
- return;
25
-
26
- if ( isset( $_COOKIE['mc_session_ids'] ) )
27
- $this->session_ids = $_COOKIE['mc_session_ids'];
28
- else {
29
- foreach ( array( 'default', 'multi' ) as $place ) {
30
- switch ( $place ) {
31
- case 'multi':
32
- for ( $i = 0; $i < 5; $i ++ ) {
33
- $this->session_ids[$place][$i] = sha1( $this->generate_password() );
34
- }
35
- break;
36
-
37
- case 'default':
38
- $this->session_ids[$place] = sha1( $this->generate_password() );
39
- break;
40
- }
41
- }
42
- }
43
-
44
- if ( ! isset( $_COOKIE['mc_session_ids'] ) ) {
45
- setcookie( 'mc_session_ids[default]', $this->session_ids['default'], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN, (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ? true : false ), true );
46
-
47
- for ( $i = 0; $i < 5; $i ++ ) {
48
- setcookie( 'mc_session_ids[multi][' . $i . ']', $this->session_ids['multi'][$i], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN );
49
- }
50
- }
51
- }
52
-
53
- /**
54
- * Generate password helper, without wp_rand() call
55
- */
56
- private function generate_password( $length = 64 ) {
57
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
58
- $password = '';
59
-
60
- for ( $i = 0; $i < $length; $i ++ ) {
61
- $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
62
- }
63
-
64
- return $password;
65
- }
66
-
 
 
 
 
67
  }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ new Math_Captcha_Cookie_Session();
7
+
8
+ class Math_Captcha_Cookie_Session {
9
+
10
+ public $session_ids;
11
+
12
+ public function __construct() {
13
+ // set instance
14
+ Math_Captcha()->cookie_session = $this;
15
+
16
+ // actions
17
+ add_action( 'plugins_loaded', array( &$this, 'init_session' ), 1 );
18
+ }
19
+
20
+ /**
21
+ * Initialize cookie-session.
22
+ */
23
+ public function init_session() {
24
+ if ( is_admin() )
25
+ return;
26
+
27
+ if ( isset( $_COOKIE['mc_session_ids'] ) )
28
+ $this->session_ids = $_COOKIE['mc_session_ids'];
29
+ else {
30
+ foreach ( array( 'default', 'multi' ) as $place ) {
31
+ switch ( $place ) {
32
+ case 'multi':
33
+ for ( $i = 0; $i < 5; $i ++ ) {
34
+ $this->session_ids[$place][$i] = sha1( $this->generate_password() );
35
+ }
36
+ break;
37
+
38
+ case 'default':
39
+ $this->session_ids[$place] = sha1( $this->generate_password() );
40
+ break;
41
+ }
42
+ }
43
+ }
44
+
45
+ if ( ! isset( $_COOKIE['mc_session_ids'] ) ) {
46
+ setcookie( 'mc_session_ids[default]', $this->session_ids['default'], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN, (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ? true : false ), true );
47
+
48
+ for ( $i = 0; $i < 5; $i ++ ) {
49
+ setcookie( 'mc_session_ids[multi][' . $i . ']', $this->session_ids['multi'][$i], current_time( 'timestamp', true ) + apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ), COOKIEPATH, COOKIE_DOMAIN );
50
+ }
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Generate password helper, without wp_rand() call
56
+ *
57
+ * @param int $length
58
+ * @return string
59
+ */
60
+ private function generate_password( $length = 64 ) {
61
+ $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
62
+ $password = '';
63
+
64
+ for ( $i = 0; $i < $length; $i ++ ) {
65
+ $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
66
+ }
67
+
68
+ return $password;
69
+ }
70
+
71
  }
includes/class-core.php CHANGED
@@ -1,633 +1,679 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) )
3
- exit;
4
-
5
- new Math_Captcha_Core();
6
-
7
- class Math_Captcha_Core {
8
-
9
- public $session_number = 0;
10
- public $login_failed = false;
11
- public $error_messages;
12
- public $errors;
13
-
14
- /**
15
- *
16
- */
17
- public function __construct() {
18
- // sets instance
19
- Math_Captcha()->core = $this;
20
-
21
- // actions
22
- add_action( 'init', array( &$this, 'load_actions_filters' ), 1 );
23
- add_action( 'plugins_loaded', array( &$this, 'load_defaults' ) );
24
- add_action( 'admin_init', array( &$this, 'flush_rewrites' ) );
25
-
26
- // filters
27
- add_filter( 'shake_error_codes', array( &$this, 'add_shake_error_codes' ), 1 );
28
- add_filter( 'mod_rewrite_rules', array( &$this, 'block_direct_comments' ) );
29
- }
30
-
31
- /**
32
- * Load defaults
33
- */
34
- public function load_defaults() {
35
- $this->error_messages = array(
36
- 'fill' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Please enter captcha value.', 'math-captcha' ),
37
- 'wrong' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Invalid captcha value.', 'math-captcha' ),
38
- 'time' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Captcha time expired.', 'math-captcha' )
39
- );
40
- }
41
-
42
- /**
43
- * Loads required filters
44
- */
45
- public function load_actions_filters() {
46
- // Contact Form 7
47
- if ( Math_Captcha()->options['general']['enable_for']['contact_form_7'] && class_exists( 'WPCF7_ContactForm' ) )
48
- include_once(MATH_CAPTCHA_PATH . 'includes/integrations/contact-form-7.php');
49
-
50
- if ( is_admin() )
51
- return;
52
-
53
- $action = (isset( $_GET['action'] ) && $_GET['action'] !== '' ? $_GET['action'] : null);
54
-
55
- // comments
56
- if ( Math_Captcha()->options['general']['enable_for']['comment_form'] ) {
57
- if ( ! is_user_logged_in() )
58
- add_action( 'comment_form_after_fields', array( &$this, 'add_captcha_form' ) );
59
- elseif ( ! Math_Captcha()->options['general']['hide_for_logged_users'] )
60
- add_action( 'comment_form_logged_in_after', array( &$this, 'add_captcha_form' ) );
61
-
62
- add_filter( 'preprocess_comment', array( &$this, 'add_comment_with_captcha' ) );
63
- }
64
-
65
- // registration
66
- if ( Math_Captcha()->options['general']['enable_for']['registration_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'register' ) {
67
- add_action( 'register_form', array( &$this, 'add_captcha_form' ) );
68
- add_action( 'register_post', array( &$this, 'add_user_with_captcha' ), 10, 3 );
69
- add_action( 'signup_extra_fields', array( &$this, 'add_captcha_form' ) );
70
- add_filter( 'wpmu_validate_user_signup', array( &$this, 'validate_user_with_captcha' ) );
71
- }
72
-
73
- // lost password
74
- if ( Math_Captcha()->options['general']['enable_for']['reset_password_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'lostpassword' ) {
75
- add_action( 'lostpassword_form', array( &$this, 'add_captcha_form' ) );
76
- add_action( 'lostpassword_post', array( &$this, 'check_lost_password_with_captcha' ) );
77
- }
78
-
79
- // login
80
- if ( Math_Captcha()->options['general']['enable_for']['login_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === null ) {
81
- add_action( 'login_form', array( &$this, 'add_captcha_form' ) );
82
- add_filter( 'login_redirect', array( &$this, 'redirect_login_with_captcha' ), 10, 3 );
83
- add_filter( 'authenticate', array( &$this, 'authenticate_user' ), 1000, 3 );
84
- }
85
-
86
- // bbPress
87
- if ( Math_Captcha()->options['general']['enable_for']['bbpress'] && class_exists( 'bbPress' ) && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) ) {
88
- add_action( 'bbp_theme_after_reply_form_content', array( &$this, 'add_bbp_captcha_form' ) );
89
- add_action( 'bbp_theme_after_topic_form_content', array( &$this, 'add_bbp_captcha_form' ) );
90
- add_action( 'bbp_new_reply_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
91
- add_action( 'bbp_new_topic_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
92
- }
93
- }
94
-
95
- /**
96
- * Adds lost password errors
97
- */
98
- public function add_lostpassword_captcha_message( $errors ) {
99
- return $errors . $this->errors->errors['math-captcha-error'][0];
100
- }
101
-
102
- /**
103
- * Adds lost password errors (special way)
104
- */
105
- public function add_lostpassword_wp_message() {
106
- return $this->errors;
107
- }
108
-
109
- /**
110
- * Validates lost password form
111
- */
112
- public function check_lost_password_with_captcha() {
113
- $this->errors = new WP_Error();
114
- $user_error = false;
115
- $user_data = null;
116
-
117
- // checks captcha
118
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
119
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
120
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
121
- $this->errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
122
- } else
123
- $this->errors->add( 'math-captcha-error', $this->error_messages['time'] );
124
- } else
125
- $this->errors->add( 'math-captcha-error', $this->error_messages['fill'] );
126
-
127
- // checks user_login (from wp-login.php)
128
- if ( empty( $_POST['user_login'] ) )
129
- $user_error = true;
130
- elseif ( strpos( $_POST['user_login'], '@' ) ) {
131
- $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
132
-
133
- if ( empty( $user_data ) )
134
- $user_error = true;
135
- } else
136
- $user_data = get_user_by( 'login', trim( $_POST['user_login'] ) );
137
-
138
- if ( ! $user_data )
139
- $user_error = true;
140
-
141
- // something went wrong?
142
- if ( ! empty( $this->errors->errors ) ) {
143
- // nasty hack (captcha is wrong but user_login is fine)
144
- if ( $user_error === false )
145
- add_filter( 'allow_password_reset', array( &$this, 'add_lostpassword_wp_message' ) );
146
- else
147
- add_filter( 'login_errors', array( &$this, 'add_lostpassword_captcha_message' ) );
148
- }
149
- }
150
-
151
- /**
152
- * Validates register form
153
- */
154
- public function add_user_with_captcha( $login, $email, $errors ) {
155
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
156
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
157
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
158
- $errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
159
- } else
160
- $errors->add( 'math-captcha-error', $this->error_messages['time'] );
161
- } else
162
- $errors->add( 'math-captcha-error', $this->error_messages['fill'] );
163
-
164
- return $errors;
165
- }
166
-
167
- /**
168
- * Validates register form
169
- */
170
- public function validate_user_with_captcha( $result ) {
171
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
172
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
173
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
174
- $result['errors']->add( 'math-captcha-error', $this->error_messages['wrong'] );
175
- } else
176
- $result['errors']->add( 'math-captcha-error', $this->error_messages['time'] );
177
- } else
178
- $result['errors']->add( 'math-captcha-error', $this->error_messages['fill'] );
179
-
180
- return $result;
181
- }
182
-
183
- /**
184
- * Posts login form
185
- */
186
- public function redirect_login_with_captcha( $redirect, $bool, $errors ) {
187
- if ( $this->login_failed === false && ! empty( $_POST ) ) {
188
- $error = '';
189
-
190
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
191
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
192
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
193
- $error = 'wrong';
194
- } else
195
- $error = 'time';
196
- } else
197
- $error = 'fill';
198
-
199
- if ( is_wp_error( $errors ) && ! empty( $error ) )
200
- $errors->add( 'math-captcha-error', $this->error_messages[$error] );
201
- }
202
-
203
- return $redirect;
204
- }
205
-
206
- /**
207
- * Authenticate user
208
- */
209
- public function authenticate_user( $user, $username, $password ) {
210
- // user gave us valid login and password
211
- if ( ! is_wp_error( $user ) ) {
212
- if ( ! empty( $_POST ) ) {
213
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
214
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
215
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
216
- $error = 'wrong';
217
- } else
218
- $error = 'time';
219
- } else
220
- $error = 'fill';
221
- }
222
-
223
- if ( ! empty( $error ) ) {
224
- // destroy cookie
225
- wp_clear_auth_cookie();
226
-
227
- $user = new WP_Error();
228
- $user->add( 'math-captcha-error', $this->error_messages[$error] );
229
-
230
- // inform redirect function that we failed to login
231
- $this->login_failed = true;
232
- }
233
- }
234
-
235
- return $user;
236
- }
237
-
238
- /**
239
- * Adds shake
240
- */
241
- public function add_shake_error_codes( $codes ) {
242
- $codes[] = 'math-captcha-error';
243
-
244
- return $codes;
245
- }
246
-
247
- /**
248
- * Adds captcha to comment form
249
- */
250
- public function add_comment_with_captcha( $comment ) {
251
- if ( isset( $_POST['mc-value'] ) && ( ! is_admin() || DOING_AJAX) && ($comment['comment_type'] === '' || $comment['comment_type'] === 'comment') ) {
252
- if ( $_POST['mc-value'] !== '' ) {
253
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
254
- if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) === 0 )
255
- return $comment;
256
- else
257
- wp_die( $this->error_messages['wrong'] );
258
- } else
259
- wp_die( $this->error_messages['time'] );
260
- } else
261
- wp_die( $this->error_messages['fill'] );
262
- } else
263
- return $comment;
264
- }
265
-
266
- /**
267
- * Shows and generates captcha
268
- */
269
- public function add_captcha_form() {
270
- if ( is_admin() )
271
- return;
272
-
273
- $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
274
-
275
- echo '
276
- <p class="math-captcha-form">';
277
-
278
- if ( ! empty( $captcha_title ) )
279
- echo '
280
- <label>' . $captcha_title . '<br/></label>';
281
-
282
- echo '
283
- <span>' . $this->generate_captcha_phrase( 'default' ) . '</span>
284
- </p>';
285
- }
286
-
287
- /**
288
- * Shows and generates captcha for bbPress
289
- */
290
- public function add_bbp_captcha_form() {
291
- if ( is_admin() )
292
- return;
293
-
294
- $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
295
-
296
- echo '
297
- <p class="math-captcha-form">';
298
-
299
- if ( ! empty( $captcha_title ) )
300
- echo '
301
- <label>' . $captcha_title . '<br/></label>';
302
-
303
- echo '
304
- <span>' . $this->generate_captcha_phrase( 'bbpress' ) . '</span>
305
- </p>';
306
- }
307
-
308
- /**
309
- * Validates bbpress topics and replies
310
- */
311
- public function check_bbpress_captcha() {
312
- if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
313
- if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
314
- if ( strcmp( get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
315
- bbp_add_error( 'math-captcha-wrong', $this->error_messages['wrong'] );
316
- } else
317
- bbp_add_error( 'math-captcha-wrong', $this->error_messages['time'] );
318
- } else
319
- bbp_add_error( 'math-captcha-wrong', $this->error_messages['fill'] );
320
- }
321
-
322
- /**
323
- * Encodes chars
324
- */
325
- private function encode_operation( $string ) {
326
- $chars = str_split( $string );
327
- $seed = mt_rand( 0, (int) abs( crc32( $string ) / strlen( $string ) ) );
328
-
329
- foreach ( $chars as $key => $char ) {
330
- $ord = ord( $char );
331
-
332
- // ignore non-ascii chars
333
- if ( $ord < 128 ) {
334
- // pseudo "random function"
335
- $r = ($seed * (1 + $key)) % 100;
336
-
337
- if ( $r > 60 && $char !== '@' ) {
338
-
339
- } // plain character (not encoded), if not @-sign
340
- elseif ( $r < 45 )
341
- $chars[$key] = '&#x' . dechex( $ord ) . ';'; // hexadecimal
342
- else
343
- $chars[$key] = '&#' . $ord . ';'; // decimal (ascii)
344
- }
345
- }
346
-
347
- return implode( '', $chars );
348
- }
349
-
350
- /**
351
- * Converts numbers to words
352
- */
353
- private function numberToWords( $number ) {
354
- $words = array(
355
- 1 => __( 'one', 'math-captcha' ),
356
- 2 => __( 'two', 'math-captcha' ),
357
- 3 => __( 'three', 'math-captcha' ),
358
- 4 => __( 'four', 'math-captcha' ),
359
- 5 => __( 'five', 'math-captcha' ),
360
- 6 => __( 'six', 'math-captcha' ),
361
- 7 => __( 'seven', 'math-captcha' ),
362
- 8 => __( 'eight', 'math-captcha' ),
363
- 9 => __( 'nine', 'math-captcha' ),
364
- 10 => __( 'ten', 'math-captcha' ),
365
- 11 => __( 'eleven', 'math-captcha' ),
366
- 12 => __( 'twelve', 'math-captcha' ),
367
- 13 => __( 'thirteen', 'math-captcha' ),
368
- 14 => __( 'fourteen', 'math-captcha' ),
369
- 15 => __( 'fifteen', 'math-captcha' ),
370
- 16 => __( 'sixteen', 'math-captcha' ),
371
- 17 => __( 'seventeen', 'math-captcha' ),
372
- 18 => __( 'eighteen', 'math-captcha' ),
373
- 19 => __( 'nineteen', 'math-captcha' ),
374
- 20 => __( 'twenty', 'math-captcha' ),
375
- 30 => __( 'thirty', 'math-captcha' ),
376
- 40 => __( 'forty', 'math-captcha' ),
377
- 50 => __( 'fifty', 'math-captcha' ),
378
- 60 => __( 'sixty', 'math-captcha' ),
379
- 70 => __( 'seventy', 'math-captcha' ),
380
- 80 => __( 'eighty', 'math-captcha' ),
381
- 90 => __( 'ninety', 'math-captcha' )
382
- );
383
-
384
- if ( isset( $words[$number] ) )
385
- return $words[$number];
386
- else {
387
- $reverse = false;
388
-
389
- switch ( get_bloginfo( 'language' ) ) {
390
- case 'de-DE':
391
- $spacer = 'und';
392
- $reverse = true;
393
- break;
394
-
395
- case 'nl-NL':
396
- $spacer = 'en';
397
- $reverse = true;
398
- break;
399
-
400
- case 'ru-RU':
401
- case 'pl-PL':
402
- case 'en-EN':
403
- default:
404
- $spacer = ' ';
405
- }
406
-
407
- $first = (int) (substr( $number, 0, 1 ) * 10);
408
- $second = (int) substr( $number, -1 );
409
-
410
- return ($reverse === false ? $words[$first] . $spacer . $words[$second] : $words[$second] . $spacer . $words[$first]);
411
- }
412
- }
413
-
414
- /**
415
- * Generates captcha
416
- */
417
- public function generate_captcha_phrase( $form = '' ) {
418
- $ops = array(
419
- 'addition' => '+',
420
- 'subtraction' => '&#8722;',
421
- 'multiplication' => '&#215;',
422
- 'division' => '&#247;',
423
- );
424
-
425
- $operations = $groups = array();
426
- $input = '<input type="text" size="2" length="2" id="mc-input" class="mc-input" name="mc-value" value="" aria-required="true"/>';
427
-
428
- // available operations
429
- foreach ( Math_Captcha()->options['general']['mathematical_operations'] as $operation => $enable ) {
430
- if ( $enable === true )
431
- $operations[] = $operation;
432
- }
433
-
434
- // available groups
435
- foreach ( Math_Captcha()->options['general']['groups'] as $group => $enable ) {
436
- if ( $enable === true )
437
- $groups[] = $group;
438
- }
439
-
440
- // number of groups
441
- $ao = count( $groups );
442
-
443
- // operation
444
- $rnd_op = $operations[mt_rand( 0, count( $operations ) - 1 )];
445
- $number[3] = $ops[$rnd_op];
446
-
447
- // place where to put empty input
448
- $rnd_input = mt_rand( 0, 2 );
449
-
450
- // which random operation
451
- switch ( $rnd_op ) {
452
- case 'addition':
453
- if ( $rnd_input === 0 ) {
454
- $number[0] = mt_rand( 1, 10 );
455
- $number[1] = mt_rand( 1, 89 );
456
- } elseif ( $rnd_input === 1 ) {
457
- $number[0] = mt_rand( 1, 89 );
458
- $number[1] = mt_rand( 1, 10 );
459
- } elseif ( $rnd_input === 2 ) {
460
- $number[0] = mt_rand( 1, 9 );
461
- $number[1] = mt_rand( 1, 10 - $number[0] );
462
- }
463
-
464
- $number[2] = $number[0] + $number[1];
465
- break;
466
-
467
- case 'subtraction':
468
- if ( $rnd_input === 0 ) {
469
- $number[0] = mt_rand( 2, 10 );
470
- $number[1] = mt_rand( 1, $number[0] - 1 );
471
- } elseif ( $rnd_input === 1 ) {
472
- $number[0] = mt_rand( 11, 99 );
473
- $number[1] = mt_rand( 1, 10 );
474
- } elseif ( $rnd_input === 2 ) {
475
- $number[0] = mt_rand( 11, 99 );
476
- $number[1] = mt_rand( $number[0] - 10, $number[0] - 1 );
477
- }
478
-
479
- $number[2] = $number[0] - $number[1];
480
- break;
481
-
482
- case 'multiplication':
483
- if ( $rnd_input === 0 ) {
484
- $number[0] = mt_rand( 1, 10 );
485
- $number[1] = mt_rand( 1, 9 );
486
- } elseif ( $rnd_input === 1 ) {
487
- $number[0] = mt_rand( 1, 9 );
488
- $number[1] = mt_rand( 1, 10 );
489
- } elseif ( $rnd_input === 2 ) {
490
- $number[0] = mt_rand( 1, 10 );
491
- $number[1] = ($number[0] > 5 ? 1 : ($number[0] === 4 && $number[0] === 5 ? mt_rand( 1, 2 ) : ($number[0] === 3 ? mt_rand( 1, 3 ) : ($number[0] === 2 ? mt_rand( 1, 5 ) : mt_rand( 1, 10 )))));
492
- }
493
-
494
- $number[2] = $number[0] * $number[1];
495
- break;
496
-
497
- case 'division':
498
- $divide = array( 1 => 99, 2 => 49, 3 => 33, 4 => 24, 5 => 19, 6 => 16, 7 => 14, 8 => 12, 9 => 11, 10 => 9 );
499
-
500
- if ( $rnd_input === 0 ) {
501
- $divide = array( 2 => array( 1, 2 ), 3 => array( 1, 3 ), 4 => array( 1, 2, 4 ), 5 => array( 1, 5 ), 6 => array( 1, 2, 3, 6 ), 7 => array( 1, 7 ), 8 => array( 1, 2, 4, 8 ), 9 => array( 1, 3, 9 ), 10 => array( 1, 2, 5, 10 ) );
502
- $number[0] = mt_rand( 2, 10 );
503
- $number[1] = $divide[$number[0]][mt_rand( 0, count( $divide[$number[0]] ) - 1 )];
504
- } elseif ( $rnd_input === 1 ) {
505
- $number[1] = mt_rand( 1, 10 );
506
- $number[0] = $number[1] * mt_rand( 1, $divide[$number[1]] );
507
- } elseif ( $rnd_input === 2 ) {
508
- $number[2] = mt_rand( 1, 10 );
509
- $number[0] = $number[2] * mt_rand( 1, $divide[$number[2]] );
510
- $number[1] = (int) ($number[0] / $number[2]);
511
- }
512
-
513
- if ( ! isset( $number[2] ) )
514
- $number[2] = (int) ($number[0] / $number[1]);
515
-
516
- break;
517
- }
518
-
519
- // words
520
- if ( $ao === 1 && $groups[0] === 'words' ) {
521
- if ( $rnd_input === 0 ) {
522
- $number[1] = $this->numberToWords( $number[1] );
523
- $number[2] = $this->numberToWords( $number[2] );
524
- } elseif ( $rnd_input === 1 ) {
525
- $number[0] = $this->numberToWords( $number[0] );
526
- $number[2] = $this->numberToWords( $number[2] );
527
- } elseif ( $rnd_input === 2 ) {
528
- $number[0] = $this->numberToWords( $number[0] );
529
- $number[1] = $this->numberToWords( $number[1] );
530
- }
531
- }
532
- // numbers and words
533
- elseif ( $ao === 2 ) {
534
- if ( $rnd_input === 0 ) {
535
- if ( mt_rand( 1, 2 ) === 2 ) {
536
- $number[1] = $this->numberToWords( $number[1] );
537
- $number[2] = $this->numberToWords( $number[2] );
538
- } else
539
- $number[$tmp = mt_rand( 1, 2 )] = $this->numberToWords( $number[$tmp] );
540
- }
541
- elseif ( $rnd_input === 1 ) {
542
- if ( mt_rand( 1, 2 ) === 2 ) {
543
- $number[0] = $this->numberToWords( $number[0] );
544
- $number[2] = $this->numberToWords( $number[2] );
545
- } else
546
- $number[$tmp = array_rand( array( 0 => 0, 2 => 2 ), 1 )] = $this->numberToWords( $number[$tmp] );
547
- }
548
- elseif ( $rnd_input === 2 ) {
549
- if ( mt_rand( 1, 2 ) === 2 ) {
550
- $number[0] = $this->numberToWords( $number[0] );
551
- $number[1] = $this->numberToWords( $number[1] );
552
- } else
553
- $number[$tmp = mt_rand( 0, 1 )] = $this->numberToWords( $number[$tmp] );
554
- }
555
- }
556
-
557
- if ( in_array( $form, array( 'default', 'bbpress' ), true ) ) {
558
- // position of empty input
559
- if ( $rnd_input === 0 )
560
- $return = $input . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $this->encode_operation( $number[2] );
561
- elseif ( $rnd_input === 1 )
562
- $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $input . ' = ' . $this->encode_operation( $number[2] );
563
- elseif ( $rnd_input === 2 )
564
- $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $input;
565
-
566
- $transient_name = ($form === 'bbpress' ? 'bbp' : 'mc');
567
- $session_id = Math_Captcha()->cookie_session->session_ids['default'];
568
- }
569
- elseif ( $form === 'cf7' ) {
570
- $return = array();
571
-
572
- if ( $rnd_input === 0 ) {
573
- $return['input'] = 1;
574
- $return[2] = ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ';
575
- $return[3] = $this->encode_operation( $number[2] );
576
- } elseif ( $rnd_input === 1 ) {
577
- $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
578
- $return['input'] = 2;
579
- $return[3] = ' = ' . $this->encode_operation( $number[2] );
580
- } elseif ( $rnd_input === 2 ) {
581
- $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
582
- $return[2] = $this->encode_operation( $number[1] ) . ' = ';
583
- $return['input'] = 3;
584
- }
585
-
586
- $transient_name = 'cf7';
587
- $session_id = Math_Captcha()->cookie_session->session_ids['multi'][$this->session_number ++];
588
- }
589
-
590
- set_transient( $transient_name . '_' . $session_id, sha1( AUTH_KEY . $number[$rnd_input] . $session_id, false ), apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ) );
591
-
592
- return $return;
593
- }
594
-
595
- /**
596
- *
597
- */
598
- public function flush_rewrites() {
599
- if ( Math_Captcha()->options['general']['flush_rules'] ) {
600
- global $wp_rewrite;
601
-
602
- $wp_rewrite->flush_rules();
603
-
604
- Math_Captcha()->options['general']['flush_rules'] = false;
605
- update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
606
- }
607
- }
608
-
609
- /**
610
- *
611
- */
612
- public function block_direct_comments( $rules ) {
613
- if ( Math_Captcha()->options['general']['block_direct_comments'] ) {
614
- $new_rules = <<<EOT
615
- \n# BEGIN Math Captcha
616
- <IfModule mod_rewrite.c>
617
- RewriteEngine On
618
- RewriteCond %{REQUEST_METHOD} POST
619
- RewriteCond %{REQUEST_URI} .wp-comments-post.php*
620
- RewriteCond %{HTTP_REFERER} !.*{$_SERVER['HTTP_HOST']}.* [OR]
621
- RewriteCond %{HTTP_USER_AGENT} ^$
622
- RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
623
- </IfModule>
624
- # END Math Captcha\n\n
625
- EOT;
626
-
627
- return $new_rules . $rules;
628
- }
629
-
630
- return $rules;
631
- }
632
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
  }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ new Math_Captcha_Core();
7
+
8
+ class Math_Captcha_Core {
9
+
10
+ public $session_number = 0;
11
+ public $login_failed = false;
12
+ public $error_messages;
13
+ public $errors;
14
+
15
+ /**
16
+ *
17
+ */
18
+ public function __construct() {
19
+ // set instance
20
+ Math_Captcha()->core = $this;
21
+
22
+ // actions
23
+ add_action( 'init', array( &$this, 'load_actions_filters' ), 1 );
24
+ add_action( 'plugins_loaded', array( &$this, 'load_defaults' ) );
25
+ add_action( 'admin_init', array( &$this, 'flush_rewrites' ) );
26
+
27
+ // filters
28
+ add_filter( 'shake_error_codes', array( &$this, 'add_shake_error_codes' ), 1 );
29
+ add_filter( 'mod_rewrite_rules', array( &$this, 'block_direct_comments' ) );
30
+ }
31
+
32
+ /**
33
+ * Load defaults.
34
+ */
35
+ public function load_defaults() {
36
+ $this->error_messages = array(
37
+ 'fill' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Please enter captcha value.', 'math-captcha' ),
38
+ 'wrong' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Invalid captcha value.', 'math-captcha' ),
39
+ 'time' => '<strong>' . __( 'ERROR', 'math-captcha' ) . '</strong>: ' . __( 'Captcha time expired.', 'math-captcha' )
40
+ );
41
+ }
42
+
43
+ /**
44
+ * Load required filters.
45
+ */
46
+ public function load_actions_filters() {
47
+ // Contact Form 7
48
+ if ( Math_Captcha()->options['general']['enable_for']['contact_form_7'] && class_exists( 'WPCF7_ContactForm' ) )
49
+ include_once(MATH_CAPTCHA_PATH . 'includes/integrations/contact-form-7.php');
50
+
51
+ if ( is_admin() )
52
+ return;
53
+
54
+ $action = (isset( $_GET['action'] ) && $_GET['action'] !== '' ? $_GET['action'] : null);
55
+
56
+ // comments
57
+ if ( Math_Captcha()->options['general']['enable_for']['comment_form'] ) {
58
+ if ( ! is_user_logged_in() )
59
+ add_action( 'comment_form_after_fields', array( &$this, 'add_captcha_form' ) );
60
+ elseif ( ! Math_Captcha()->options['general']['hide_for_logged_users'] )
61
+ add_action( 'comment_form_logged_in_after', array( &$this, 'add_captcha_form' ) );
62
+
63
+ add_filter( 'preprocess_comment', array( &$this, 'add_comment_with_captcha' ) );
64
+ }
65
+
66
+ // registration
67
+ if ( Math_Captcha()->options['general']['enable_for']['registration_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'register' ) {
68
+ add_action( 'register_form', array( &$this, 'add_captcha_form' ) );
69
+ add_action( 'register_post', array( &$this, 'add_user_with_captcha' ), 10, 3 );
70
+ add_action( 'signup_extra_fields', array( &$this, 'add_captcha_form' ) );
71
+ add_filter( 'wpmu_validate_user_signup', array( &$this, 'validate_user_with_captcha' ) );
72
+ }
73
+
74
+ // lost password
75
+ if ( Math_Captcha()->options['general']['enable_for']['reset_password_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === 'lostpassword' ) {
76
+ add_action( 'lostpassword_form', array( &$this, 'add_captcha_form' ) );
77
+ add_action( 'lostpassword_post', array( &$this, 'check_lost_password_with_captcha' ) );
78
+ }
79
+
80
+ // login
81
+ if ( Math_Captcha()->options['general']['enable_for']['login_form'] && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) && $action === null ) {
82
+ add_action( 'login_form', array( &$this, 'add_captcha_form' ) );
83
+ add_filter( 'login_redirect', array( &$this, 'redirect_login_with_captcha' ), 10, 3 );
84
+ add_filter( 'authenticate', array( &$this, 'authenticate_user' ), 1000, 3 );
85
+ }
86
+
87
+ // bbPress
88
+ if ( Math_Captcha()->options['general']['enable_for']['bbpress'] && class_exists( 'bbPress' ) && ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users'])) ) {
89
+ add_action( 'bbp_theme_after_reply_form_content', array( &$this, 'add_bbp_captcha_form' ) );
90
+ add_action( 'bbp_theme_after_topic_form_content', array( &$this, 'add_bbp_captcha_form' ) );
91
+ add_action( 'bbp_new_reply_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
92
+ add_action( 'bbp_new_topic_pre_extras', array( &$this, 'check_bbpress_captcha' ) );
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Add lost password errors.
98
+ *
99
+ * @param array $errors
100
+ * @return array
101
+ */
102
+ public function add_lostpassword_captcha_message( $errors ) {
103
+ return $errors . $this->errors->errors['math-captcha-error'][0];
104
+ }
105
+
106
+ /**
107
+ * Add lost password errors (special way)
108
+ *
109
+ * @return array
110
+ */
111
+ public function add_lostpassword_wp_message() {
112
+ return $this->errors;
113
+ }
114
+
115
+ /**
116
+ * Validate lost password form.
117
+ */
118
+ public function check_lost_password_with_captcha() {
119
+ $this->errors = new WP_Error();
120
+ $user_error = false;
121
+ $user_data = null;
122
+
123
+ // checks captcha
124
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
125
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
126
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
127
+ $this->errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
128
+ } else
129
+ $this->errors->add( 'math-captcha-error', $this->error_messages['time'] );
130
+ } else
131
+ $this->errors->add( 'math-captcha-error', $this->error_messages['fill'] );
132
+
133
+ // checks user_login (from wp-login.php)
134
+ if ( empty( $_POST['user_login'] ) )
135
+ $user_error = true;
136
+ elseif ( strpos( $_POST['user_login'], '@' ) ) {
137
+ $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
138
+
139
+ if ( empty( $user_data ) )
140
+ $user_error = true;
141
+ } else
142
+ $user_data = get_user_by( 'login', trim( $_POST['user_login'] ) );
143
+
144
+ if ( ! $user_data )
145
+ $user_error = true;
146
+
147
+ // something went wrong?
148
+ if ( ! empty( $this->errors->errors ) ) {
149
+ // nasty hack (captcha is invalid but user_login is fine)
150
+ if ( $user_error === false )
151
+ add_filter( 'allow_password_reset', array( &$this, 'add_lostpassword_wp_message' ) );
152
+ else
153
+ add_filter( 'login_errors', array( &$this, 'add_lostpassword_captcha_message' ) );
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Validate registration form.
159
+ *
160
+ * @param string $login
161
+ * @param string $email
162
+ * @param array $errors
163
+ * @return array
164
+ */
165
+ public function add_user_with_captcha( $login, $email, $errors ) {
166
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
167
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
168
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
169
+ $errors->add( 'math-captcha-error', $this->error_messages['wrong'] );
170
+ } else
171
+ $errors->add( 'math-captcha-error', $this->error_messages['time'] );
172
+ } else
173
+ $errors->add( 'math-captcha-error', $this->error_messages['fill'] );
174
+
175
+ return $errors;
176
+ }
177
+
178
+ /**
179
+ * Validate registration form.
180
+ *
181
+ * @param array $result
182
+ * @return array
183
+ */
184
+ public function validate_user_with_captcha( $result ) {
185
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
186
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
187
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
188
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['wrong'] );
189
+ } else
190
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['time'] );
191
+ } else
192
+ $result['errors']->add( 'math-captcha-error', $this->error_messages['fill'] );
193
+
194
+ return $result;
195
+ }
196
+
197
+ /**
198
+ * Posts login form
199
+ *
200
+ * @param string $redirect
201
+ * @param bool $bool
202
+ * @param array $errors
203
+ * @return array
204
+ */
205
+ public function redirect_login_with_captcha( $redirect, $bool, $errors ) {
206
+ if ( $this->login_failed === false && ! empty( $_POST ) ) {
207
+ $error = '';
208
+
209
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
210
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
211
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
212
+ $error = 'wrong';
213
+ } else
214
+ $error = 'time';
215
+ } else
216
+ $error = 'fill';
217
+
218
+ if ( is_wp_error( $errors ) && ! empty( $error ) )
219
+ $errors->add( 'math-captcha-error', $this->error_messages[$error] );
220
+ }
221
+
222
+ return $redirect;
223
+ }
224
+
225
+ /**
226
+ * Authenticate user.
227
+ *
228
+ * @param WP_Error $user
229
+ * @param string $username
230
+ * @param string $password
231
+ * @return \WP_Error
232
+ */
233
+ public function authenticate_user( $user, $username, $password ) {
234
+ // user gave us valid login and password
235
+ if ( ! is_wp_error( $user ) ) {
236
+ if ( ! empty( $_POST ) ) {
237
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
238
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
239
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
240
+ $error = 'wrong';
241
+ } else
242
+ $error = 'time';
243
+ } else
244
+ $error = 'fill';
245
+ }
246
+
247
+ if ( ! empty( $error ) ) {
248
+ // destroy cookie
249
+ wp_clear_auth_cookie();
250
+
251
+ $user = new WP_Error();
252
+ $user->add( 'math-captcha-error', $this->error_messages[$error] );
253
+
254
+ // inform redirect function that we failed to login
255
+ $this->login_failed = true;
256
+ }
257
+ }
258
+
259
+ return $user;
260
+ }
261
+
262
+ /**
263
+ * Add shake.
264
+ *
265
+ * @param array $codes
266
+ * @return array
267
+ */
268
+ public function add_shake_error_codes( $codes ) {
269
+ $codes[] = 'math-captcha-error';
270
+
271
+ return $codes;
272
+ }
273
+
274
+ /**
275
+ * Add captcha to comment form.
276
+ *
277
+ * @param array $comment
278
+ * @return array
279
+ */
280
+ public function add_comment_with_captcha( $comment ) {
281
+ if ( isset( $_POST['mc-value'] ) && ( ! is_admin() || DOING_AJAX) && ($comment['comment_type'] === '' || $comment['comment_type'] === 'comment') ) {
282
+ if ( $_POST['mc-value'] !== '' ) {
283
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
284
+ if ( strcmp( get_transient( 'mc_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) === 0 )
285
+ return $comment;
286
+ else
287
+ wp_die( $this->error_messages['wrong'] );
288
+ } else
289
+ wp_die( $this->error_messages['time'] );
290
+ } else
291
+ wp_die( $this->error_messages['fill'] );
292
+ } else
293
+ return $comment;
294
+ }
295
+
296
+ /**
297
+ * Display and generate captcha.
298
+ *
299
+ * @return mixed
300
+ */
301
+ public function add_captcha_form() {
302
+ if ( is_admin() )
303
+ return;
304
+
305
+ $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
306
+
307
+ echo '
308
+ <p class="math-captcha-form">';
309
+
310
+ if ( ! empty( $captcha_title ) )
311
+ echo '
312
+ <label>' . $captcha_title . '<br/></label>';
313
+
314
+ echo '
315
+ <span>' . $this->generate_captcha_phrase( 'default' ) . '</span>
316
+ </p>';
317
+ }
318
+
319
+ /**
320
+ * Display and generate captcha for bbPress forms.
321
+ *
322
+ * @return mixed
323
+ */
324
+ public function add_bbp_captcha_form() {
325
+ if ( is_admin() )
326
+ return;
327
+
328
+ $captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
329
+
330
+ echo '
331
+ <p class="math-captcha-form">';
332
+
333
+ if ( ! empty( $captcha_title ) )
334
+ echo '
335
+ <label>' . $captcha_title . '<br/></label>';
336
+
337
+ echo '
338
+ <span>' . $this->generate_captcha_phrase( 'bbpress' ) . '</span>
339
+ </p>';
340
+ }
341
+
342
+ /**
343
+ * Validate bbpress topics and replies.
344
+ */
345
+ public function check_bbpress_captcha() {
346
+ if ( isset( $_POST['mc-value'] ) && $_POST['mc-value'] !== '' ) {
347
+ if ( Math_Captcha()->cookie_session->session_ids['default'] !== '' && get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ) !== false ) {
348
+ if ( strcmp( get_transient( 'bbp_' . Math_Captcha()->cookie_session->session_ids['default'] ), sha1( AUTH_KEY . $_POST['mc-value'] . Math_Captcha()->cookie_session->session_ids['default'], false ) ) !== 0 )
349
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['wrong'] );
350
+ } else
351
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['time'] );
352
+ } else
353
+ bbp_add_error( 'math-captcha-wrong', $this->error_messages['fill'] );
354
+ }
355
+
356
+ /**
357
+ * Encode chars.
358
+ *
359
+ * @param string $string
360
+ * @return string
361
+ */
362
+ private function encode_operation( $string ) {
363
+ $chars = str_split( $string );
364
+ $seed = mt_rand( 0, (int) abs( crc32( $string ) / strlen( $string ) ) );
365
+
366
+ foreach ( $chars as $key => $char ) {
367
+ $ord = ord( $char );
368
+
369
+ // ignore non-ascii chars
370
+ if ( $ord < 128 ) {
371
+ // pseudo "random function"
372
+ $r = ($seed * (1 + $key)) % 100;
373
+
374
+ if ( $r > 60 && $char !== '@' ) {
375
+
376
+ } // plain character (not encoded), if not @-sign
377
+ elseif ( $r < 45 )
378
+ $chars[$key] = '&#x' . dechex( $ord ) . ';'; // hexadecimal
379
+ else
380
+ $chars[$key] = '&#' . $ord . ';'; // decimal (ascii)
381
+ }
382
+ }
383
+
384
+ return implode( '', $chars );
385
+ }
386
+
387
+ /**
388
+ * Convert numbers to words.
389
+ *
390
+ * @param int $number
391
+ * @return string
392
+ */
393
+ private function numberToWords( $number ) {
394
+ $words = array(
395
+ 1 => __( 'one', 'math-captcha' ),
396
+ 2 => __( 'two', 'math-captcha' ),
397
+ 3 => __( 'three', 'math-captcha' ),
398
+ 4 => __( 'four', 'math-captcha' ),
399
+ 5 => __( 'five', 'math-captcha' ),
400
+ 6 => __( 'six', 'math-captcha' ),
401
+ 7 => __( 'seven', 'math-captcha' ),
402
+ 8 => __( 'eight', 'math-captcha' ),
403
+ 9 => __( 'nine', 'math-captcha' ),
404
+ 10 => __( 'ten', 'math-captcha' ),
405
+ 11 => __( 'eleven', 'math-captcha' ),
406
+ 12 => __( 'twelve', 'math-captcha' ),
407
+ 13 => __( 'thirteen', 'math-captcha' ),
408
+ 14 => __( 'fourteen', 'math-captcha' ),
409
+ 15 => __( 'fifteen', 'math-captcha' ),
410
+ 16 => __( 'sixteen', 'math-captcha' ),
411
+ 17 => __( 'seventeen', 'math-captcha' ),
412
+ 18 => __( 'eighteen', 'math-captcha' ),
413
+ 19 => __( 'nineteen', 'math-captcha' ),
414
+ 20 => __( 'twenty', 'math-captcha' ),
415
+ 30 => __( 'thirty', 'math-captcha' ),
416
+ 40 => __( 'forty', 'math-captcha' ),
417
+ 50 => __( 'fifty', 'math-captcha' ),
418
+ 60 => __( 'sixty', 'math-captcha' ),
419
+ 70 => __( 'seventy', 'math-captcha' ),
420
+ 80 => __( 'eighty', 'math-captcha' ),
421
+ 90 => __( 'ninety', 'math-captcha' )
422
+ );
423
+
424
+ if ( isset( $words[$number] ) )
425
+ return $words[$number];
426
+ else {
427
+ $reverse = false;
428
+
429
+ switch ( get_bloginfo( 'language' ) ) {
430
+ case 'de-DE':
431
+ $spacer = 'und';
432
+ $reverse = true;
433
+ break;
434
+
435
+ case 'nl-NL':
436
+ $spacer = 'en';
437
+ $reverse = true;
438
+ break;
439
+
440
+ case 'ru-RU':
441
+ case 'pl-PL':
442
+ case 'en-EN':
443
+ default:
444
+ $spacer = ' ';
445
+ }
446
+
447
+ $first = (int) (substr( $number, 0, 1 ) * 10);
448
+ $second = (int) substr( $number, -1 );
449
+
450
+ return ($reverse === false ? $words[$first] . $spacer . $words[$second] : $words[$second] . $spacer . $words[$first]);
451
+ }
452
+ }
453
+
454
+ /**
455
+ * Generate captcha phrase.
456
+ *
457
+ * @param string $form
458
+ * @return array
459
+ */
460
+ public function generate_captcha_phrase( $form = '' ) {
461
+ $ops = array(
462
+ 'addition' => '+',
463
+ 'subtraction' => '&#8722;',
464
+ 'multiplication' => '&#215;',
465
+ 'division' => '&#247;',
466
+ );
467
+
468
+ $operations = $groups = array();
469
+ $input = '<input type="text" size="2" length="2" id="mc-input" class="mc-input" name="mc-value" value="" aria-required="true"/>';
470
+
471
+ // available operations
472
+ foreach ( Math_Captcha()->options['general']['mathematical_operations'] as $operation => $enable ) {
473
+ if ( $enable === true )
474
+ $operations[] = $operation;
475
+ }
476
+
477
+ // available groups
478
+ foreach ( Math_Captcha()->options['general']['groups'] as $group => $enable ) {
479
+ if ( $enable === true )
480
+ $groups[] = $group;
481
+ }
482
+
483
+ // number of groups
484
+ $ao = count( $groups );
485
+
486
+ // operation
487
+ $rnd_op = $operations[mt_rand( 0, count( $operations ) - 1 )];
488
+ $number[3] = $ops[$rnd_op];
489
+
490
+ // place where to put empty input
491
+ $rnd_input = mt_rand( 0, 2 );
492
+
493
+ // which random operation
494
+ switch ( $rnd_op ) {
495
+ case 'addition':
496
+ if ( $rnd_input === 0 ) {
497
+ $number[0] = mt_rand( 1, 10 );
498
+ $number[1] = mt_rand( 1, 89 );
499
+ } elseif ( $rnd_input === 1 ) {
500
+ $number[0] = mt_rand( 1, 89 );
501
+ $number[1] = mt_rand( 1, 10 );
502
+ } elseif ( $rnd_input === 2 ) {
503
+ $number[0] = mt_rand( 1, 9 );
504
+ $number[1] = mt_rand( 1, 10 - $number[0] );
505
+ }
506
+
507
+ $number[2] = $number[0] + $number[1];
508
+ break;
509
+
510
+ case 'subtraction':
511
+ if ( $rnd_input === 0 ) {
512
+ $number[0] = mt_rand( 2, 10 );
513
+ $number[1] = mt_rand( 1, $number[0] - 1 );
514
+ } elseif ( $rnd_input === 1 ) {
515
+ $number[0] = mt_rand( 11, 99 );
516
+ $number[1] = mt_rand( 1, 10 );
517
+ } elseif ( $rnd_input === 2 ) {
518
+ $number[0] = mt_rand( 11, 99 );
519
+ $number[1] = mt_rand( $number[0] - 10, $number[0] - 1 );
520
+ }
521
+
522
+ $number[2] = $number[0] - $number[1];
523
+ break;
524
+
525
+ case 'multiplication':
526
+ if ( $rnd_input === 0 ) {
527
+ $number[0] = mt_rand( 1, 10 );
528
+ $number[1] = mt_rand( 1, 9 );
529
+ } elseif ( $rnd_input === 1 ) {
530
+ $number[0] = mt_rand( 1, 9 );
531
+ $number[1] = mt_rand( 1, 10 );
532
+ } elseif ( $rnd_input === 2 ) {
533
+ $number[0] = mt_rand( 1, 10 );
534
+ $number[1] = ($number[0] > 5 ? 1 : ($number[0] === 4 && $number[0] === 5 ? mt_rand( 1, 2 ) : ($number[0] === 3 ? mt_rand( 1, 3 ) : ($number[0] === 2 ? mt_rand( 1, 5 ) : mt_rand( 1, 10 )))));
535
+ }
536
+
537
+ $number[2] = $number[0] * $number[1];
538
+ break;
539
+
540
+ case 'division':
541
+ $divide = array( 1 => 99, 2 => 49, 3 => 33, 4 => 24, 5 => 19, 6 => 16, 7 => 14, 8 => 12, 9 => 11, 10 => 9 );
542
+
543
+ if ( $rnd_input === 0 ) {
544
+ $divide = array( 2 => array( 1, 2 ), 3 => array( 1, 3 ), 4 => array( 1, 2, 4 ), 5 => array( 1, 5 ), 6 => array( 1, 2, 3, 6 ), 7 => array( 1, 7 ), 8 => array( 1, 2, 4, 8 ), 9 => array( 1, 3, 9 ), 10 => array( 1, 2, 5, 10 ) );
545
+ $number[0] = mt_rand( 2, 10 );
546
+ $number[1] = $divide[$number[0]][mt_rand( 0, count( $divide[$number[0]] ) - 1 )];
547
+ } elseif ( $rnd_input === 1 ) {
548
+ $number[1] = mt_rand( 1, 10 );
549
+ $number[0] = $number[1] * mt_rand( 1, $divide[$number[1]] );
550
+ } elseif ( $rnd_input === 2 ) {
551
+ $number[2] = mt_rand( 1, 10 );
552
+ $number[0] = $number[2] * mt_rand( 1, $divide[$number[2]] );
553
+ $number[1] = (int) ($number[0] / $number[2]);
554
+ }
555
+
556
+ if ( ! isset( $number[2] ) )
557
+ $number[2] = (int) ($number[0] / $number[1]);
558
+
559
+ break;
560
+ }
561
+
562
+ // words
563
+ if ( $ao === 1 && $groups[0] === 'words' ) {
564
+ if ( $rnd_input === 0 ) {
565
+ $number[1] = $this->numberToWords( $number[1] );
566
+ $number[2] = $this->numberToWords( $number[2] );
567
+ } elseif ( $rnd_input === 1 ) {
568
+ $number[0] = $this->numberToWords( $number[0] );
569
+ $number[2] = $this->numberToWords( $number[2] );
570
+ } elseif ( $rnd_input === 2 ) {
571
+ $number[0] = $this->numberToWords( $number[0] );
572
+ $number[1] = $this->numberToWords( $number[1] );
573
+ }
574
+ }
575
+ // numbers and words
576
+ elseif ( $ao === 2 ) {
577
+ if ( $rnd_input === 0 ) {
578
+ if ( mt_rand( 1, 2 ) === 2 ) {
579
+ $number[1] = $this->numberToWords( $number[1] );
580
+ $number[2] = $this->numberToWords( $number[2] );
581
+ } else
582
+ $number[$tmp = mt_rand( 1, 2 )] = $this->numberToWords( $number[$tmp] );
583
+ }
584
+ elseif ( $rnd_input === 1 ) {
585
+ if ( mt_rand( 1, 2 ) === 2 ) {
586
+ $number[0] = $this->numberToWords( $number[0] );
587
+ $number[2] = $this->numberToWords( $number[2] );
588
+ } else
589
+ $number[$tmp = array_rand( array( 0 => 0, 2 => 2 ), 1 )] = $this->numberToWords( $number[$tmp] );
590
+ }
591
+ elseif ( $rnd_input === 2 ) {
592
+ if ( mt_rand( 1, 2 ) === 2 ) {
593
+ $number[0] = $this->numberToWords( $number[0] );
594
+ $number[1] = $this->numberToWords( $number[1] );
595
+ } else
596
+ $number[$tmp = mt_rand( 0, 1 )] = $this->numberToWords( $number[$tmp] );
597
+ }
598
+ }
599
+
600
+ if ( in_array( $form, array( 'default', 'bbpress' ), true ) ) {
601
+ // position of empty input
602
+ if ( $rnd_input === 0 )
603
+ $return = $input . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $this->encode_operation( $number[2] );
604
+ elseif ( $rnd_input === 1 )
605
+ $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $input . ' = ' . $this->encode_operation( $number[2] );
606
+ elseif ( $rnd_input === 2 )
607
+ $return = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ' . $input;
608
+
609
+ $transient_name = ($form === 'bbpress' ? 'bbp' : 'mc');
610
+ $session_id = Math_Captcha()->cookie_session->session_ids['default'];
611
+ }
612
+ elseif ( $form === 'cf7' ) {
613
+ $return = array();
614
+
615
+ if ( $rnd_input === 0 ) {
616
+ $return['input'] = 1;
617
+ $return[2] = ' ' . $number[3] . ' ' . $this->encode_operation( $number[1] ) . ' = ';
618
+ $return[3] = $this->encode_operation( $number[2] );
619
+ } elseif ( $rnd_input === 1 ) {
620
+ $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
621
+ $return['input'] = 2;
622
+ $return[3] = ' = ' . $this->encode_operation( $number[2] );
623
+ } elseif ( $rnd_input === 2 ) {
624
+ $return[1] = $this->encode_operation( $number[0] ) . ' ' . $number[3] . ' ';
625
+ $return[2] = $this->encode_operation( $number[1] ) . ' = ';
626
+ $return['input'] = 3;
627
+ }
628
+
629
+ $transient_name = 'cf7';
630
+ $session_id = Math_Captcha()->cookie_session->session_ids['multi'][$this->session_number ++];
631
+ }
632
+
633
+ set_transient( $transient_name . '_' . $session_id, sha1( AUTH_KEY . $number[$rnd_input] . $session_id, false ), apply_filters( 'math_captcha_time', Math_Captcha()->options['general']['time'] ) );
634
+
635
+ return $return;
636
+ }
637
+
638
+ /**
639
+ * FLush rewrite rules.
640
+ */
641
+ public function flush_rewrites() {
642
+ if ( Math_Captcha()->options['general']['flush_rules'] ) {
643
+ global $wp_rewrite;
644
+
645
+ $wp_rewrite->flush_rules();
646
+
647
+ Math_Captcha()->options['general']['flush_rules'] = false;
648
+ update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
649
+ }
650
+ }
651
+
652
+ /**
653
+ * Block direct comments.
654
+ *
655
+ * @param string $rules
656
+ * @return string
657
+ */
658
+ public function block_direct_comments( $rules ) {
659
+ if ( Math_Captcha()->options['general']['block_direct_comments'] ) {
660
+ $new_rules = <<<EOT
661
+ \n# BEGIN Math Captcha
662
+ <IfModule mod_rewrite.c>
663
+ RewriteEngine On
664
+ RewriteCond %{REQUEST_METHOD} POST
665
+ RewriteCond %{REQUEST_URI} .wp-comments-post.php*
666
+ RewriteCond %{HTTP_REFERER} !.*{$_SERVER['HTTP_HOST']}.* [OR]
667
+ RewriteCond %{HTTP_USER_AGENT} ^$
668
+ RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
669
+ </IfModule>
670
+ # END Math Captcha\n\n
671
+ EOT;
672
+
673
+ return $new_rules . $rules;
674
+ }
675
+
676
+ return $rules;
677
+ }
678
+
679
  }
includes/class-settings.php CHANGED
@@ -1,343 +1,316 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) )
3
- exit;
4
-
5
- new Math_Captcha_Settings();
6
-
7
- class Math_Captcha_Settings {
8
-
9
- public $mathematical_operations;
10
- public $groups;
11
- public $forms;
12
-
13
- /**
14
- *
15
- */
16
- public function __construct() {
17
- // actions
18
- add_action( 'init', array( &$this, 'load_defaults' ) );
19
- add_action( 'admin_init', array( &$this, 'register_settings' ) );
20
- add_action( 'admin_menu', array( &$this, 'admin_menu_options' ) );
21
- }
22
-
23
- /**
24
- *
25
- */
26
- public function load_defaults() {
27
- if ( ! is_admin() )
28
- return;
29
-
30
- $this->forms = array(
31
- 'login_form' => __( 'login form', 'math-captcha' ),
32
- 'registration_form' => __( 'registration form', 'math-captcha' ),
33
- 'reset_password_form' => __( 'reset password form', 'math-captcha' ),
34
- 'comment_form' => __( 'comment form', 'math-captcha' ),
35
- 'bbpress' => __( 'bbpress', 'math-captcha' ),
36
- 'contact_form_7' => __( 'contact form 7', 'math-captcha' )
37
- );
38
-
39
- $this->mathematical_operations = array(
40
- 'addition' => __( 'addition (+)', 'math-captcha' ),
41
- 'subtraction' => __( 'subtraction (-)', 'math-captcha' ),
42
- 'multiplication' => __( 'multiplication (&#215;)', 'math-captcha' ),
43
- 'division' => __( 'division (&#247;)', 'math-captcha' )
44
- );
45
-
46
- $this->groups = array(
47
- 'numbers' => __( 'numbers', 'math-captcha' ),
48
- 'words' => __( 'words', 'math-captcha' )
49
- );
50
- }
51
-
52
- /**
53
- * Adds options menu
54
- */
55
- public function admin_menu_options() {
56
- add_options_page(
57
- __( 'Math Captcha', 'math-captcha' ), __( 'Math Captcha', 'math-captcha' ), 'manage_options', 'math-captcha', array( &$this, 'options_page' )
58
- );
59
- }
60
-
61
- /**
62
- * Shows options page
63
- */
64
- public function options_page() {
65
- echo '
66
- <div class="wrap">
67
- <h2>' . __( 'Math Captcha', 'math-captcha' ) . '</h2>
68
- <div class="math-captcha-settings">
69
- <div class="df-credits">
70
- <h3 class="hndle">' . __( 'Math Captcha', 'math-captcha' ) . ' ' . Math_Captcha()->defaults['version'] . '</h3>
71
- <div class="inside">
72
- <h4 class="inner">' . __( 'Need support?', 'math-captcha' ) . '</h4>
73
- <p class="inner">' . __( 'If you are having problems with this plugin, please talk about them in the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/support/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=support" target="_blank" title="' . __( 'Support forum', 'math-captcha' ) . '">' . __( 'Support forum', 'math-captcha' ) . '</a></p>
74
- <hr/>
75
- <h4 class="inner">' . __( 'Do you like this plugin?', 'math-captcha' ) . '</h4>
76
- <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank" class="inner">
77
- <input type="hidden" name="cmd" value="_s-xclick">
78
- <input type="hidden" name="hosted_button_id" value="BJSHR9GS5QJTC">
79
- <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
80
- <img alt="" border="0" src="https://www.paypalobjects.com/pl_PL/i/scr/pixel.gif" width="1" height="1">
81
- </form>
82
- <p class="inner"><a href="http://wordpress.org/support/view/plugin-reviews/wp-math-captcha" target="_blank" title="' . __( 'Rate it 5', 'math-captcha' ) . '">' . __( 'Rate it 5', 'math-captcha' ) . '</a> ' . __( 'on WordPress.org', 'math-captcha' ) . '<br/>' .
83
- __( 'Blog about it & link to the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/math-captcha/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=blog-about" target="_blank" title="' . __( 'plugin page', 'math-captcha' ) . '">' . __( 'plugin page', 'math-captcha' ) . '</a><br/>' .
84
- __( 'Check out our other', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=other-plugins" target="_blank" title="' . __( 'WordPress plugins', 'math-captcha' ) . '">' . __( 'WordPress plugins', 'math-captcha' ) . '</a>
85
- </p>
86
- <hr/>
87
- <p class="df-link inner">Created by <a href="http://www.dfactory.eu/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="dFactory - Quality plugins for WordPress"><img src="' . MATH_CAPTCHA_URL . '/images/logo-dfactory.png" title="dFactory - Quality plugins for WordPress" alt="dFactory - Quality plugins for WordPress"/></a></p>
88
- </div>
89
- </div>
90
- <form action="options.php" method="post">';
91
-
92
- wp_nonce_field( 'update-options' );
93
- settings_fields( 'math_captcha_options' );
94
- do_settings_sections( 'math_captcha_options' );
95
-
96
- echo '
97
- <p class="submit">';
98
-
99
- submit_button( '', 'primary', 'save_mc_general', false );
100
-
101
- echo ' ';
102
-
103
- submit_button( __( 'Reset to defaults', 'math-captcha' ), 'secondary reset_mc_settings', 'reset_mc_general', false );
104
-
105
- echo '
106
- </p>
107
- </form>
108
- </div>
109
- <div class="clear"></div>
110
- </div>';
111
- }
112
-
113
- /**
114
- *
115
- */
116
- public function register_settings() {
117
- // general settings
118
- register_setting( 'math_captcha_options', 'math_captcha_options', array( &$this, 'validate_settings' ) );
119
- add_settings_section( 'math_captcha_settings', __( 'Math Captcha settings', 'math-captcha' ), '', 'math_captcha_options' );
120
- add_settings_field( 'mc_general_enable_captcha_for', __( 'Enable Math Captcha for', 'math-captcha' ), array( &$this, 'mc_general_enable_captcha_for' ), 'math_captcha_options', 'math_captcha_settings' );
121
- add_settings_field( 'mc_general_hide_for_logged_users', __( 'Hide for logged in users', 'math-captcha' ), array( &$this, 'mc_general_hide_for_logged_users' ), 'math_captcha_options', 'math_captcha_settings' );
122
- add_settings_field( 'mc_general_mathematical_operations', __( 'Mathematical operations', 'math-captcha' ), array( &$this, 'mc_general_mathematical_operations' ), 'math_captcha_options', 'math_captcha_settings' );
123
- add_settings_field( 'mc_general_groups', __( 'Display captcha as', 'math-captcha' ), array( &$this, 'mc_general_groups' ), 'math_captcha_options', 'math_captcha_settings' );
124
- add_settings_field( 'mc_general_title', __( 'Captcha field title', 'math-captcha' ), array( &$this, 'mc_general_title' ), 'math_captcha_options', 'math_captcha_settings' );
125
- add_settings_field( 'mc_general_time', __( 'Captcha time', 'math-captcha' ), array( &$this, 'mc_general_time' ), 'math_captcha_options', 'math_captcha_settings' );
126
- add_settings_field( 'mc_general_block_direct_comments', __( 'Block Direct Comments', 'math-captcha' ), array( &$this, 'mc_general_block_direct_comments' ), 'math_captcha_options', 'math_captcha_settings' );
127
- add_settings_field( 'mc_general_deactivation_delete', __( 'Deactivation', 'math-captcha' ), array( &$this, 'mc_general_deactivation_delete' ), 'math_captcha_options', 'math_captcha_settings' );
128
- }
129
-
130
- /**
131
- *
132
- */
133
- public function mc_general_enable_captcha_for() {
134
- echo '
135
- <div id="mc_general_enable_captcha_for">
136
- <fieldset>';
137
-
138
- foreach ( $this->forms as $val => $trans ) {
139
- echo '
140
- <input id="mc-general-enable-captcha-for-' . $val . '" type="checkbox" name="math_captcha_options[enable_for][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['enable_for'][$val], false ) . ' ' . disabled( (($val === 'contact_form_7' && ! class_exists( 'WPCF7_ContactForm' )) || ($val === 'bbpress' && ! class_exists( 'bbPress' )) ), true, false ) . '/><label for="mc-general-enable-captcha-for-' . $val . '">' . esc_html( $trans ) . '</label>';
141
- }
142
-
143
- echo '
144
- <br/>
145
- <span class="description">' . __( 'Select where you\'d like to use Math Captcha.', 'math-captcha' ) . '</span>
146
- </fieldset>
147
- </div>';
148
- }
149
-
150
- /**
151
- *
152
- */
153
- public function mc_general_hide_for_logged_users() {
154
- echo '
155
- <div id="mc_general_hide_for_logged_users">
156
- <fieldset>
157
- <input id="mc-general-hide-for-logged" type="checkbox" name="math_captcha_options[hide_for_logged_users]" ' . checked( true, Math_Captcha()->options['general']['hide_for_logged_users'], false ) . '/><label for="mc-general-hide-for-logged">' . __( 'Enable to hide captcha for logged in users.', 'math-captcha' ) . '</label>
158
- <br/>
159
- <span class="description">' . __( 'Would you like to hide captcha for logged in users?', 'math-captcha' ) . '</span>
160
- </fieldset>
161
- </div>';
162
- }
163
-
164
- /**
165
- *
166
- */
167
- public function mc_general_mathematical_operations() {
168
- echo '
169
- <div id="mc_general_mathematical_operations">
170
- <fieldset>';
171
-
172
- foreach ( $this->mathematical_operations as $val => $trans ) {
173
- echo '
174
- <input id="mc-general-mathematical-operations-' . $val . '" type="checkbox" name="math_captcha_options[mathematical_operations][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['mathematical_operations'][$val], false ) . '/><label for="mc-general-mathematical-operations-' . $val . '">' . esc_html( $trans ) . '</label>';
175
- }
176
-
177
- echo '
178
- <br/>
179
- <span class="description">' . __( 'Select which mathematical operations to use in your captcha.', 'math-captcha' ) . '</span>
180
- </fieldset>
181
- </div>';
182
- }
183
-
184
- /**
185
- *
186
- */
187
- public function mc_general_groups() {
188
- echo '
189
- <div id="mc_general_groups">
190
- <fieldset>';
191
-
192
- foreach ( $this->groups as $val => $trans ) {
193
- echo '
194
- <input id="mc-general-groups-' . $val . '" type="checkbox" name="math_captcha_options[groups][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['groups'][$val], false ) . '/><label for="mc-general-groups-' . $val . '">' . esc_html( $trans ) . '</label>';
195
- }
196
-
197
- echo '
198
- <br/>
199
- <span class="description">' . __( 'Select how you\'d like to display you captcha.', 'math-captcha' ) . '</span>
200
- </fieldset>
201
- </div>';
202
- }
203
-
204
- /**
205
- *
206
- */
207
- public function mc_general_title() {
208
- echo '
209
- <div id="mc_general_title">
210
- <fieldset>
211
- <input type="text" name="math_captcha_options[title]" value="' . Math_Captcha()->options['general']['title'] . '"/>
212
- <br/>
213
- <span class="description">' . __( 'How to entitle field with captcha?', 'math-captcha' ) . '</span>
214
- </fieldset>
215
- </div>';
216
- }
217
-
218
- /**
219
- *
220
- */
221
- public function mc_general_time() {
222
- echo '
223
- <div id="mc_general_time">
224
- <fieldset>
225
- <input type="text" name="math_captcha_options[time]" value="' . Math_Captcha()->options['general']['time'] . '"/>
226
- <br/>
227
- <span class="description">' . __( 'Enter the time (in seconds) a user has to enter captcha value.', 'math-captcha' ) . '</span>
228
- </fieldset>
229
- </div>';
230
- }
231
-
232
- /**
233
- *
234
- */
235
- public function mc_general_block_direct_comments() {
236
- echo '
237
- <div id="mc_general_block_direct_comments">
238
- <fieldset>
239
- <input id="mc-general-block-direct-comments" type="checkbox" name="math_captcha_options[block_direct_comments]" ' . checked( true, Math_Captcha()->options['general']['block_direct_comments'], false ) . '/><label for="mc-general-block-direct-comments">' . __( 'Block direct access to wp-comments-post.php.', 'math-captcha' ) . '</label>
240
- <br/>
241
- <span class="description">' . __( 'Enable this to prevent spambots from posting to Wordpress via a URL.', 'math-captcha' ) . '</span>
242
- </fieldset>
243
- </div>';
244
- }
245
-
246
- /**
247
- *
248
- */
249
- public function mc_general_deactivation_delete() {
250
- echo '
251
- <div id="mc_general_deactivation_delete">
252
- <fieldset>
253
- <input id="mc-general-deactivation-delete" type="checkbox" name="math_captcha_options[deactivation_delete]" ' . checked( true, Math_Captcha()->options['general']['deactivation_delete'], false ) . '/><label for="mc-general-deactivation-delete">' . __( 'Delete settings on plugin deactivation.', 'math-captcha' ) . '</label>
254
- <br/>
255
- <span class="description">' . __( 'Delete settings on plugin deactivation', 'math-captcha' ) . '</span>
256
- </fieldset>
257
- </div>';
258
- }
259
-
260
- /**
261
- * Validates settings
262
- */
263
- public function validate_settings( $input ) {
264
- if ( isset( $_POST['save_mc_general'] ) ) {
265
- // enable captcha forms
266
- $enable_for = array();
267
-
268
- if ( empty( $input['enable_for'] ) ) {
269
- foreach ( Math_Captcha()->defaults['general']['enable_for'] as $enable => $bool ) {
270
- $input['enable_for'][$enable] = false;
271
- }
272
- } else {
273
- foreach ( $this->forms as $enable => $trans ) {
274
- $enable_for[$enable] = (in_array( $enable, $input['enable_for'] ) ? true : false);
275
- }
276
-
277
- $input['enable_for'] = $enable_for;
278
- }
279
-
280
- if ( ! class_exists( 'WPCF7_ContactForm' ) && Math_Captcha()->options['general']['enable_for']['contact_form_7'] )
281
- $input['enable_for']['contact_form_7'] = true;
282
-
283
- if ( ! class_exists( 'bbPress' ) && Math_Captcha()->options['general']['enable_for']['bbpress'] )
284
- $input['enable_for']['bbpress'] = true;
285
-
286
- // enable mathematical operations
287
- $mathematical_operations = array();
288
-
289
- if ( empty( $input['mathematical_operations'] ) ) {
290
- add_settings_error( 'empty-operations', 'settings_updated', __( 'You need to check at least one mathematical operation. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
291
-
292
- $input['mathematical_operations'] = Math_Captcha()->defaults['general']['mathematical_operations'];
293
- } else {
294
- foreach ( $this->mathematical_operations as $operation => $trans ) {
295
- $mathematical_operations[$operation] = (in_array( $operation, $input['mathematical_operations'] ) ? true : false);
296
- }
297
-
298
- $input['mathematical_operations'] = $mathematical_operations;
299
- }
300
-
301
- // enable groups
302
- $groups = array();
303
-
304
- if ( empty( $input['groups'] ) ) {
305
- add_settings_error( 'empty-groups', 'settings_updated', __( 'You need to check at least one group. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
306
-
307
- $input['groups'] = Math_Captcha()->defaults['general']['groups'];
308
- } else {
309
- foreach ( $this->groups as $group => $trans ) {
310
- $groups[$group] = (in_array( $group, $input['groups'] ) ? true : false);
311
- }
312
-
313
- $input['groups'] = $groups;
314
- }
315
-
316
- // hide for logged in users
317
- $input['hide_for_logged_users'] = isset( $input['hide_for_logged_users'] );
318
-
319
- // block direct comments access
320
- $input['block_direct_comments'] = isset( $input['block_direct_comments'] );
321
-
322
- // deactivation delete
323
- $input['deactivation_delete'] = isset( $input['deactivation_delete'] );
324
-
325
- // captcha title
326
- $input['title'] = trim( $input['title'] );
327
-
328
- // captcha time
329
- $time = (int) $input['time'];
330
- $input['time'] = ($time < 0 ? Math_Captcha()->defaults['general']['time'] : $time);
331
-
332
- // flush rules
333
- $input['flush_rules'] = true;
334
- } elseif ( isset( $_POST['reset_mc_general'] ) ) {
335
- $input = Math_Captcha()->defaults['general'];
336
-
337
- add_settings_error( 'settings', 'settings_reset', __( 'Settings restored to defaults.', 'math-captcha' ), 'updated' );
338
- }
339
-
340
- return $input;
341
- }
342
-
343
  }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ new Math_Captcha_Settings();
7
+
8
+ class Math_Captcha_Settings {
9
+
10
+ public $mathematical_operations;
11
+ public $groups;
12
+ public $forms;
13
+
14
+ public function __construct() {
15
+ // actions
16
+ add_action( 'init', array( &$this, 'load_defaults' ) );
17
+ add_action( 'admin_init', array( &$this, 'register_settings' ) );
18
+ add_action( 'admin_menu', array( &$this, 'admin_menu_options' ) );
19
+ }
20
+
21
+ /**
22
+ * Load defaults.
23
+ */
24
+ public function load_defaults() {
25
+ if ( ! is_admin() )
26
+ return;
27
+
28
+ $this->forms = array(
29
+ 'login_form' => __( 'login form', 'math-captcha' ),
30
+ 'registration_form' => __( 'registration form', 'math-captcha' ),
31
+ 'reset_password_form' => __( 'reset password form', 'math-captcha' ),
32
+ 'comment_form' => __( 'comment form', 'math-captcha' ),
33
+ 'bbpress' => __( 'bbpress', 'math-captcha' ),
34
+ 'contact_form_7' => __( 'contact form 7', 'math-captcha' )
35
+ );
36
+
37
+ $this->mathematical_operations = array(
38
+ 'addition' => __( 'addition (+)', 'math-captcha' ),
39
+ 'subtraction' => __( 'subtraction (-)', 'math-captcha' ),
40
+ 'multiplication' => __( 'multiplication (&#215;)', 'math-captcha' ),
41
+ 'division' => __( 'division (&#247;)', 'math-captcha' )
42
+ );
43
+
44
+ $this->groups = array(
45
+ 'numbers' => __( 'numbers', 'math-captcha' ),
46
+ 'words' => __( 'words', 'math-captcha' )
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Add options menu.
52
+ */
53
+ public function admin_menu_options() {
54
+ add_options_page(
55
+ __( 'Math Captcha', 'math-captcha' ), __( 'Math Captcha', 'math-captcha' ), 'manage_options', 'math-captcha', array( &$this, 'options_page' )
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Render options page.
61
+ *
62
+ * @return mixed
63
+ */
64
+ public function options_page() {
65
+ echo '
66
+ <div class="wrap">
67
+ <h2>' . __( 'Math Captcha', 'math-captcha' ) . '</h2>
68
+ <div class="math-captcha-settings">
69
+ <div class="df-credits">
70
+ <h3 class="hndle">' . __( 'Math Captcha', 'math-captcha' ) . ' ' . Math_Captcha()->defaults['version'] . '</h3>
71
+ <div class="inside">
72
+ <h4 class="inner">' . __( 'Need support?', 'math-captcha' ) . '</h4>
73
+ <p class="inner">' . __( 'If you are having problems with this plugin, please talk about them in the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/support/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=support" target="_blank" title="' . __( 'Support forum', 'math-captcha' ) . '">' . __( 'Support forum', 'math-captcha' ) . '</a></p>
74
+ <hr/>
75
+ <h4 class="inner">' . __( 'Do you like this plugin?', 'math-captcha' ) . '</h4>
76
+ <p class="inner"><a href="http://wordpress.org/support/view/plugin-reviews/wp-math-captcha" target="_blank" title="' . __( 'Rate it 5', 'math-captcha' ) . '">' . __( 'Rate it 5', 'math-captcha' ) . '</a> ' . __( 'on WordPress.org', 'math-captcha' ) . '<br/>' .
77
+ __( 'Blog about it & link to the', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/math-captcha/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=blog-about" target="_blank" title="' . __( 'plugin page', 'math-captcha' ) . '">' . __( 'plugin page', 'math-captcha' ) . '</a><br/>' .
78
+ __( 'Check out our other', 'math-captcha' ) . ' <a href="http://www.dfactory.eu/plugins/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=other-plugins" target="_blank" title="' . __( 'WordPress plugins', 'math-captcha' ) . '">' . __( 'WordPress plugins', 'math-captcha' ) . '</a>
79
+ </p>
80
+ <hr/>
81
+ <p class="df-link inner">Created by <a href="http://www.dfactory.eu/?utm_source=math-captcha-settings&utm_medium=link&utm_campaign=created-by" target="_blank" title="dFactory - Quality plugins for WordPress"><img src="' . MATH_CAPTCHA_URL . '/images/logo-dfactory.png" title="dFactory - Quality plugins for WordPress" alt="dFactory - Quality plugins for WordPress"/></a></p>
82
+ </div>
83
+ </div>
84
+ <form action="options.php" method="post">';
85
+
86
+ wp_nonce_field( 'update-options' );
87
+ settings_fields( 'math_captcha_options' );
88
+ do_settings_sections( 'math_captcha_options' );
89
+
90
+ echo '
91
+ <p class="submit">';
92
+
93
+ submit_button( '', 'primary', 'save_mc_general', false );
94
+
95
+ echo ' ';
96
+
97
+ submit_button( __( 'Reset to defaults', 'math-captcha' ), 'secondary reset_mc_settings', 'reset_mc_general', false );
98
+
99
+ echo '
100
+ </p>
101
+ </form>
102
+ </div>
103
+ <div class="clear"></div>
104
+ </div>';
105
+ }
106
+
107
+ /**
108
+ * Register settings.
109
+ */
110
+ public function register_settings() {
111
+ // general settings
112
+ register_setting( 'math_captcha_options', 'math_captcha_options', array( &$this, 'validate_settings' ) );
113
+ add_settings_section( 'math_captcha_settings', __( 'Math Captcha settings', 'math-captcha' ), '', 'math_captcha_options' );
114
+ add_settings_field( 'mc_general_enable_captcha_for', __( 'Enable Math Captcha for', 'math-captcha' ), array( &$this, 'mc_general_enable_captcha_for' ), 'math_captcha_options', 'math_captcha_settings' );
115
+ add_settings_field( 'mc_general_hide_for_logged_users', __( 'Hide for logged in users', 'math-captcha' ), array( &$this, 'mc_general_hide_for_logged_users' ), 'math_captcha_options', 'math_captcha_settings' );
116
+ add_settings_field( 'mc_general_mathematical_operations', __( 'Mathematical operations', 'math-captcha' ), array( &$this, 'mc_general_mathematical_operations' ), 'math_captcha_options', 'math_captcha_settings' );
117
+ add_settings_field( 'mc_general_groups', __( 'Display captcha as', 'math-captcha' ), array( &$this, 'mc_general_groups' ), 'math_captcha_options', 'math_captcha_settings' );
118
+ add_settings_field( 'mc_general_title', __( 'Captcha field title', 'math-captcha' ), array( &$this, 'mc_general_title' ), 'math_captcha_options', 'math_captcha_settings' );
119
+ add_settings_field( 'mc_general_time', __( 'Captcha time', 'math-captcha' ), array( &$this, 'mc_general_time' ), 'math_captcha_options', 'math_captcha_settings' );
120
+ add_settings_field( 'mc_general_block_direct_comments', __( 'Block Direct Comments', 'math-captcha' ), array( &$this, 'mc_general_block_direct_comments' ), 'math_captcha_options', 'math_captcha_settings' );
121
+ add_settings_field( 'mc_general_deactivation_delete', __( 'Deactivation', 'math-captcha' ), array( &$this, 'mc_general_deactivation_delete' ), 'math_captcha_options', 'math_captcha_settings' );
122
+ }
123
+
124
+ public function mc_general_enable_captcha_for() {
125
+ echo '
126
+ <div id="mc_general_enable_captcha_for">
127
+ <fieldset>';
128
+
129
+ foreach ( $this->forms as $val => $trans ) {
130
+ echo '
131
+ <input id="mc-general-enable-captcha-for-' . $val . '" type="checkbox" name="math_captcha_options[enable_for][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['enable_for'][$val], false ) . ' ' . disabled( (($val === 'contact_form_7' && ! class_exists( 'WPCF7_ContactForm' )) || ($val === 'bbpress' && ! class_exists( 'bbPress' )) ), true, false ) . '/><label for="mc-general-enable-captcha-for-' . $val . '">' . esc_html( $trans ) . '</label>';
132
+ }
133
+
134
+ echo '
135
+ <br/>
136
+ <span class="description">' . __( 'Select where you\'d like to use Math Captcha.', 'math-captcha' ) . '</span>
137
+ </fieldset>
138
+ </div>';
139
+ }
140
+
141
+ public function mc_general_hide_for_logged_users() {
142
+ echo '
143
+ <div id="mc_general_hide_for_logged_users">
144
+ <fieldset>
145
+ <input id="mc-general-hide-for-logged" type="checkbox" name="math_captcha_options[hide_for_logged_users]" ' . checked( true, Math_Captcha()->options['general']['hide_for_logged_users'], false ) . '/><label for="mc-general-hide-for-logged">' . __( 'Enable to hide captcha for logged in users.', 'math-captcha' ) . '</label>
146
+ <br/>
147
+ <span class="description">' . __( 'Would you like to hide captcha for logged in users?', 'math-captcha' ) . '</span>
148
+ </fieldset>
149
+ </div>';
150
+ }
151
+
152
+ public function mc_general_mathematical_operations() {
153
+ echo '
154
+ <div id="mc_general_mathematical_operations">
155
+ <fieldset>';
156
+
157
+ foreach ( $this->mathematical_operations as $val => $trans ) {
158
+ echo '
159
+ <input id="mc-general-mathematical-operations-' . $val . '" type="checkbox" name="math_captcha_options[mathematical_operations][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['mathematical_operations'][$val], false ) . '/><label for="mc-general-mathematical-operations-' . $val . '">' . esc_html( $trans ) . '</label>';
160
+ }
161
+
162
+ echo '
163
+ <br/>
164
+ <span class="description">' . __( 'Select which mathematical operations to use in your captcha.', 'math-captcha' ) . '</span>
165
+ </fieldset>
166
+ </div>';
167
+ }
168
+
169
+ public function mc_general_groups() {
170
+ echo '
171
+ <div id="mc_general_groups">
172
+ <fieldset>';
173
+
174
+ foreach ( $this->groups as $val => $trans ) {
175
+ echo '
176
+ <input id="mc-general-groups-' . $val . '" type="checkbox" name="math_captcha_options[groups][]" value="' . $val . '" ' . checked( true, Math_Captcha()->options['general']['groups'][$val], false ) . '/><label for="mc-general-groups-' . $val . '">' . esc_html( $trans ) . '</label>';
177
+ }
178
+
179
+ echo '
180
+ <br/>
181
+ <span class="description">' . __( 'Select how you\'d like to display you captcha.', 'math-captcha' ) . '</span>
182
+ </fieldset>
183
+ </div>';
184
+ }
185
+
186
+ public function mc_general_title() {
187
+ echo '
188
+ <div id="mc_general_title">
189
+ <fieldset>
190
+ <input type="text" name="math_captcha_options[title]" value="' . Math_Captcha()->options['general']['title'] . '"/>
191
+ <br/>
192
+ <span class="description">' . __( 'How to entitle field with captcha?', 'math-captcha' ) . '</span>
193
+ </fieldset>
194
+ </div>';
195
+ }
196
+
197
+ public function mc_general_time() {
198
+ echo '
199
+ <div id="mc_general_time">
200
+ <fieldset>
201
+ <input type="text" name="math_captcha_options[time]" value="' . Math_Captcha()->options['general']['time'] . '"/>
202
+ <br/>
203
+ <span class="description">' . __( 'Enter the time (in seconds) a user has to enter captcha value.', 'math-captcha' ) . '</span>
204
+ </fieldset>
205
+ </div>';
206
+ }
207
+
208
+ public function mc_general_block_direct_comments() {
209
+ echo '
210
+ <div id="mc_general_block_direct_comments">
211
+ <fieldset>
212
+ <input id="mc-general-block-direct-comments" type="checkbox" name="math_captcha_options[block_direct_comments]" ' . checked( true, Math_Captcha()->options['general']['block_direct_comments'], false ) . '/><label for="mc-general-block-direct-comments">' . __( 'Block direct access to wp-comments-post.php.', 'math-captcha' ) . '</label>
213
+ <br/>
214
+ <span class="description">' . __( 'Enable this to prevent spambots from posting to Wordpress via a URL.', 'math-captcha' ) . '</span>
215
+ </fieldset>
216
+ </div>';
217
+ }
218
+
219
+ public function mc_general_deactivation_delete() {
220
+ echo '
221
+ <div id="mc_general_deactivation_delete">
222
+ <fieldset>
223
+ <input id="mc-general-deactivation-delete" type="checkbox" name="math_captcha_options[deactivation_delete]" ' . checked( true, Math_Captcha()->options['general']['deactivation_delete'], false ) . '/><label for="mc-general-deactivation-delete">' . __( 'Delete settings on plugin deactivation.', 'math-captcha' ) . '</label>
224
+ <br/>
225
+ <span class="description">' . __( 'Delete settings on plugin deactivation', 'math-captcha' ) . '</span>
226
+ </fieldset>
227
+ </div>';
228
+ }
229
+
230
+ /**
231
+ * Validate settings.
232
+ *
233
+ * @param array $input
234
+ * @return array
235
+ */
236
+ public function validate_settings( $input ) {
237
+ if ( isset( $_POST['save_mc_general'] ) ) {
238
+ // enable captcha forms
239
+ $enable_for = array();
240
+
241
+ if ( empty( $input['enable_for'] ) ) {
242
+ foreach ( Math_Captcha()->defaults['general']['enable_for'] as $enable => $bool ) {
243
+ $input['enable_for'][$enable] = false;
244
+ }
245
+ } else {
246
+ foreach ( $this->forms as $enable => $trans ) {
247
+ $enable_for[$enable] = (in_array( $enable, $input['enable_for'] ) ? true : false);
248
+ }
249
+
250
+ $input['enable_for'] = $enable_for;
251
+ }
252
+
253
+ if ( ! class_exists( 'WPCF7_ContactForm' ) && Math_Captcha()->options['general']['enable_for']['contact_form_7'] )
254
+ $input['enable_for']['contact_form_7'] = true;
255
+
256
+ if ( ! class_exists( 'bbPress' ) && Math_Captcha()->options['general']['enable_for']['bbpress'] )
257
+ $input['enable_for']['bbpress'] = true;
258
+
259
+ // enable mathematical operations
260
+ $mathematical_operations = array();
261
+
262
+ if ( empty( $input['mathematical_operations'] ) ) {
263
+ add_settings_error( 'empty-operations', 'settings_updated', __( 'You need to check at least one mathematical operation. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
264
+
265
+ $input['mathematical_operations'] = Math_Captcha()->defaults['general']['mathematical_operations'];
266
+ } else {
267
+ foreach ( $this->mathematical_operations as $operation => $trans ) {
268
+ $mathematical_operations[$operation] = (in_array( $operation, $input['mathematical_operations'] ) ? true : false);
269
+ }
270
+
271
+ $input['mathematical_operations'] = $mathematical_operations;
272
+ }
273
+
274
+ // enable groups
275
+ $groups = array();
276
+
277
+ if ( empty( $input['groups'] ) ) {
278
+ add_settings_error( 'empty-groups', 'settings_updated', __( 'You need to check at least one group. Defaults settings of this option restored.', 'math-captcha' ), 'error' );
279
+
280
+ $input['groups'] = Math_Captcha()->defaults['general']['groups'];
281
+ } else {
282
+ foreach ( $this->groups as $group => $trans ) {
283
+ $groups[$group] = (in_array( $group, $input['groups'] ) ? true : false);
284
+ }
285
+
286
+ $input['groups'] = $groups;
287
+ }
288
+
289
+ // hide for logged in users
290
+ $input['hide_for_logged_users'] = isset( $input['hide_for_logged_users'] );
291
+
292
+ // block direct comments access
293
+ $input['block_direct_comments'] = isset( $input['block_direct_comments'] );
294
+
295
+ // deactivation delete
296
+ $input['deactivation_delete'] = isset( $input['deactivation_delete'] );
297
+
298
+ // captcha title
299
+ $input['title'] = trim( $input['title'] );
300
+
301
+ // captcha time
302
+ $time = (int) $input['time'];
303
+ $input['time'] = ($time < 0 ? Math_Captcha()->defaults['general']['time'] : $time);
304
+
305
+ // flush rules
306
+ $input['flush_rules'] = true;
307
+ } elseif ( isset( $_POST['reset_mc_general'] ) ) {
308
+ $input = Math_Captcha()->defaults['general'];
309
+
310
+ add_settings_error( 'settings', 'settings_reset', __( 'Settings restored to defaults.', 'math-captcha' ), 'updated' );
311
+ }
312
+
313
+ return $input;
314
+ }
315
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  }
includes/class-update.php CHANGED
@@ -1,39 +1,37 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) )
3
- exit;
4
-
5
- new Math_Captcha_Update();
6
-
7
- class Math_Captcha_Update {
8
-
9
- /**
10
- *
11
- */
12
- public function __construct() {
13
- // actions
14
- add_action( 'init', array( &$this, 'check_update' ) );
15
- }
16
-
17
- /**
18
- *
19
- */
20
- public function check_update() {
21
- if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
22
- return;
23
-
24
- // gets current database version
25
- $current_db_version = get_option( 'math_captcha_version', '1.0.0' );
26
-
27
- // new version?
28
- if ( version_compare( $current_db_version, Math_Captcha()->defaults['version'], '<' ) ) {
29
- if ( version_compare( $current_db_version, '1.0.9', '<' ) ) {
30
- update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
31
- delete_option( 'mc_options' );
32
- }
33
-
34
- // updates plugin version
35
- update_option( 'math_captcha_version', Math_Captcha()->defaults['version'] );
36
- }
37
- }
38
-
39
  }
1
+ <?php
2
+ // exit if accessed directly
3
+ if ( ! defined( 'ABSPATH' ) )
4
+ exit;
5
+
6
+ new Math_Captcha_Update();
7
+
8
+ class Math_Captcha_Update {
9
+
10
+ public function __construct() {
11
+ // actions
12
+ add_action( 'init', array( &$this, 'check_update' ) );
13
+ }
14
+
15
+ /**
16
+ * Check update.
17
+ */
18
+ public function check_update() {
19
+ if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
20
+ return;
21
+
22
+ // gets current database version
23
+ $current_db_version = get_option( 'math_captcha_version', '1.0.0' );
24
+
25
+ // new version?
26
+ if ( version_compare( $current_db_version, Math_Captcha()->defaults['version'], '<' ) ) {
27
+ if ( version_compare( $current_db_version, '1.0.9', '<' ) ) {
28
+ update_option( 'math_captcha_options', Math_Captcha()->options['general'] );
29
+ delete_option( 'mc_options' );
30
+ }
31
+
32
+ // updates plugin version
33
+ update_option( 'math_captcha_version', Math_Captcha()->defaults['version'] );
34
+ }
35
+ }
36
+
 
 
37
  }
includes/integrations/contact-form-7.php CHANGED
@@ -1,179 +1,179 @@
1
- <?php
2
- /**
3
- * A module for [mathcaptcha]
4
- */
5
-
6
- // shortcode handler
7
- add_action( 'init', 'wpcf7_add_shortcode_mathcaptcha', 5 );
8
-
9
- function wpcf7_add_shortcode_mathcaptcha() {
10
- wpcf7_add_shortcode( 'mathcaptcha', 'wpcf7_mathcaptcha_shortcode_handler', true );
11
- }
12
-
13
- function wpcf7_mathcaptcha_shortcode_handler( $tag ) {
14
- if ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users']) ) {
15
- $tag = new WPCF7_Shortcode( $tag );
16
-
17
- if ( empty( $tag->name ) )
18
- return '';
19
-
20
- $validation_error = wpcf7_get_validation_error( $tag->name );
21
- $class = wpcf7_form_controls_class( $tag->type );
22
-
23
- if ( $validation_error )
24
- $class .= ' wpcf7-not-valid';
25
-
26
- $atts = array();
27
- $atts['size'] = 2;
28
- $atts['maxlength'] = 2;
29
- $atts['class'] = $tag->get_class_option( $class );
30
- $atts['id'] = $tag->get_option( 'id', 'id', true );
31
- $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
32
- $atts['aria-required'] = 'true';
33
- $atts['type'] = 'text';
34
- $atts['name'] = $tag->name;
35
- $atts['value'] = '';
36
- $atts = wpcf7_format_atts( $atts );
37
-
38
- $mc_form = Math_Captcha()->core->generate_captcha_phrase( 'cf7' );
39
- $mc_form[$mc_form['input']] = '<input %2$s />';
40
-
41
- $math_captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
42
-
43
- return sprintf( ((empty( $math_captcha_title )) ? '' : $math_captcha_title) . '<span class="wpcf7-form-control-wrap %1$s">' . $mc_form[1] . $mc_form[2] . $mc_form[3] . '%3$s</span><input type="hidden" value="' . (Math_Captcha()->core->session_number - 1) . '" name="' . $tag->name . '-sn" />', $tag->name, $atts, $validation_error );
44
- }
45
- }
46
-
47
- // validation
48
- add_filter( 'wpcf7_validate_mathcaptcha', 'wpcf7_mathcaptcha_validation_filter', 10, 2 );
49
-
50
- function wpcf7_mathcaptcha_validation_filter( $result, $tag ) {
51
- $tag = new WPCF7_Shortcode( $tag );
52
- $name = $tag->name;
53
-
54
- if ( ! is_admin() && isset( $_POST[$name] ) ) {
55
- $cf7_version = get_option( 'wpcf7', '1.0.0' );
56
-
57
- if ( is_array( $cf7_version ) && isset( $cf7_version['version'] ) )
58
- $cf7_version = $cf7_version['version'];
59
-
60
- if ( $_POST[$name] !== '' ) {
61
- $session_id = (isset( $_POST[$name . '-sn'] ) && $_POST[$name . '-sn'] !== '' ? Math_Captcha()->cookie_session->session_ids['multi'][$_POST[$name . '-sn']] : '');
62
-
63
- if ( $session_id !== '' && get_transient( 'cf7_' . $session_id ) !== false ) {
64
- if ( strcmp( get_transient( 'cf7_' . $session_id ), sha1( AUTH_KEY . $_POST[$name] . $session_id, false ) ) !== 0 ) {
65
- if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
66
- $result->invalidate( $tag, wpcf7_get_message( 'wrong_mathcaptcha' ) );
67
- else {
68
- $result['valid'] = false;
69
- $result['reason'][$name] = wpcf7_get_message( 'wrong_mathcaptcha' );
70
- }
71
- }
72
- } else {
73
- if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
74
- $result->invalidate( $tag, wpcf7_get_message( 'time_mathcaptcha' ) );
75
- else {
76
- $result['valid'] = false;
77
- $result['reason'][$name] = wpcf7_get_message( 'time_mathcaptcha' );
78
- }
79
- }
80
- } else {
81
- if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
82
- $result->invalidate( $tag, wpcf7_get_message( 'fill_mathcaptcha' ) );
83
- else {
84
- $result['valid'] = false;
85
- $result['reason'][$name] = wpcf7_get_message( 'fill_mathcaptcha' );
86
- }
87
- }
88
- }
89
-
90
- return $result;
91
- }
92
-
93
- // messages
94
- add_filter( 'wpcf7_messages', 'wpcf7_mathcaptcha_messages' );
95
-
96
- function wpcf7_mathcaptcha_messages( $messages ) {
97
- return array_merge(
98
- $messages, array(
99
- 'wrong_mathcaptcha' => array(
100
- 'description' => __( 'Invalid captcha value.', 'math-captcha' ),
101
- 'default' => Math_Captcha()->core->error_messages['wrong']
102
- ),
103
- 'fill_mathcaptcha' => array(
104
- 'description' => __( 'Please enter captcha value.', 'math-captcha' ),
105
- 'default' => Math_Captcha()->core->error_messages['fill']
106
- ),
107
- 'time_mathcaptcha' => array(
108
- 'description' => __( 'Captcha time expired.', 'math-captcha' ),
109
- 'default' => Math_Captcha()->core->error_messages['time']
110
- )
111
- )
112
- );
113
- }
114
-
115
- // warning message
116
- add_action( 'wpcf7_admin_notices', 'wpcf7_mathcaptcha_display_warning_message' );
117
-
118
- function wpcf7_mathcaptcha_display_warning_message() {
119
- if ( empty( $_GET['post'] ) || ! ($contact_form = wpcf7_contact_form( $_GET['post'] )) )
120
- return;
121
-
122
- $has_tags = (bool) $contact_form->form_scan_shortcode( array( 'type' => array( 'mathcaptcha' ) ) );
123
-
124
- if ( ! $has_tags )
125
- return;
126
- }
127
-
128
- // tag generator
129
- add_action( 'admin_init', 'wpcf7_add_tag_generator_mathcaptcha', 45 );
130
-
131
- function wpcf7_add_tag_generator_mathcaptcha() {
132
- if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
133
- return;
134
-
135
- wpcf7_add_tag_generator( 'mathcaptcha', __( 'Math Captcha', 'math-captcha' ), 'wpcf7-mathcaptcha', 'wpcf7_tg_pane_mathcaptcha' );
136
- }
137
-
138
- function wpcf7_tg_pane_mathcaptcha( $contact_form ) {
139
- echo '
140
- <div class="control-box">
141
- <fieldset>
142
- <table class="form-table">
143
- <tbody>
144
- <tr>
145
- <th scope="row">
146
- <label for="tag-generator-panel-mathcaptcha-name">' . esc_html__( 'Name', 'contact-form-7' ) . '</label>
147
- </th>
148
- <td>
149
- <input type="text" name="name" class="tg-name oneline" id="tag-generator-panel-mathcaptcha-name" />
150
- </td>
151
- </tr>
152
- <tr>
153
- <th scope="row">
154
- <label for="tag-generator-panel-mathcaptcha-id">' . esc_html__( 'Id attribute', 'contact-form-7' ) . '</label>
155
- </th>
156
- <td>
157
- <input type="text" name="id" class="idvalue oneline option" id="tag-generator-panel-mathcaptcha-id" />
158
- </td>
159
- </tr>
160
- <tr>
161
- <th scope="row">
162
- <label for="tag-generator-panel-mathcaptcha-class">' . esc_html__( 'Class attribute', 'contact-form-7' ) . '</label>
163
- </th>
164
- <td>
165
- <input type="text" name="class" class="classvalue oneline option" id="tag-generator-panel-mathcaptcha-class" />
166
- </td>
167
- </tr>
168
- </tbody>
169
- </table>
170
- </fieldset>
171
- </div>
172
- <div class="insert-box">
173
- <input type="text" name="mathcaptcha" class="tag code" readonly="readonly" onfocus="this.select();">
174
- <div class="submitbox">
175
- <input type="button" class="button button-primary insert-tag" value="' . esc_attr__( 'Insert Tag', 'contact-form-7' ) . '">
176
- </div>
177
- <br class="clear">
178
- </div>';
179
  }
1
+ <?php
2
+ /**
3
+ * A module for [mathcaptcha]
4
+ */
5
+
6
+ // shortcode handler
7
+ add_action( 'init', 'wpcf7_add_shortcode_mathcaptcha', 5 );
8
+
9
+ function wpcf7_add_shortcode_mathcaptcha() {
10
+ wpcf7_add_form_tag( 'mathcaptcha', 'wpcf7_mathcaptcha_shortcode_handler', true );
11
+ }
12
+
13
+ function wpcf7_mathcaptcha_shortcode_handler( $tag ) {
14
+ if ( ! is_user_logged_in() || (is_user_logged_in() && ! Math_Captcha()->options['general']['hide_for_logged_users']) ) {
15
+ $tag = new WPCF7_FormTag( $tag );
16
+
17
+ if ( empty( $tag->name ) )
18
+ return '';
19
+
20
+ $validation_error = wpcf7_get_validation_error( $tag->name );
21
+ $class = wpcf7_form_controls_class( $tag->type );
22
+
23
+ if ( $validation_error )
24
+ $class .= ' wpcf7-not-valid';
25
+
26
+ $atts = array();
27
+ $atts['size'] = 2;
28
+ $atts['maxlength'] = 2;
29
+ $atts['class'] = $tag->get_class_option( $class );
30
+ $atts['id'] = $tag->get_option( 'id', 'id', true );
31
+ $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
32
+ $atts['aria-required'] = 'true';
33
+ $atts['type'] = 'text';
34
+ $atts['name'] = $tag->name;
35
+ $atts['value'] = '';
36
+ $atts = wpcf7_format_atts( $atts );
37
+
38
+ $mc_form = Math_Captcha()->core->generate_captcha_phrase( 'cf7' );
39
+ $mc_form[$mc_form['input']] = '<input %2$s />';
40
+
41
+ $math_captcha_title = apply_filters( 'math_captcha_title', Math_Captcha()->options['general']['title'] );
42
+
43
+ return sprintf( ((empty( $math_captcha_title )) ? '' : $math_captcha_title) . '<span class="wpcf7-form-control-wrap %1$s">' . $mc_form[1] . $mc_form[2] . $mc_form[3] . '%3$s</span><input type="hidden" value="' . (Math_Captcha()->core->session_number - 1) . '" name="' . $tag->name . '-sn" />', $tag->name, $atts, $validation_error );
44
+ }
45
+ }
46
+
47
+ // validation
48
+ add_filter( 'wpcf7_validate_mathcaptcha', 'wpcf7_mathcaptcha_validation_filter', 10, 2 );
49
+
50
+ function wpcf7_mathcaptcha_validation_filter( $result, $tag ) {
51
+ $tag = new WPCF7_FormTag( $tag );
52
+ $name = $tag->name;
53
+
54
+ if ( ! is_admin() && isset( $_POST[$name] ) ) {
55
+ $cf7_version = get_option( 'wpcf7', '1.0.0' );
56
+
57
+ if ( is_array( $cf7_version ) && isset( $cf7_version['version'] ) )
58
+ $cf7_version = $cf7_version['version'];
59
+
60
+ if ( $_POST[$name] !== '' ) {
61
+ $session_id = (isset( $_POST[$name . '-sn'] ) && $_POST[$name . '-sn'] !== '' ? Math_Captcha()->cookie_session->session_ids['multi'][$_POST[$name . '-sn']] : '');
62
+
63
+ if ( $session_id !== '' && get_transient( 'cf7_' . $session_id ) !== false ) {
64
+ if ( strcmp( get_transient( 'cf7_' . $session_id ), sha1( AUTH_KEY . $_POST[$name] . $session_id, false ) ) !== 0 ) {
65
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
66
+ $result->invalidate( $tag, wpcf7_get_message( 'wrong_mathcaptcha' ) );
67
+ else {
68
+ $result['valid'] = false;
69
+ $result['reason'][$name] = wpcf7_get_message( 'wrong_mathcaptcha' );
70
+ }
71
+ }
72
+ } else {
73
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
74
+ $result->invalidate( $tag, wpcf7_get_message( 'time_mathcaptcha' ) );
75
+ else {
76
+ $result['valid'] = false;
77
+ $result['reason'][$name] = wpcf7_get_message( 'time_mathcaptcha' );
78
+ }
79
+ }
80
+ } else {
81
+ if ( version_compare( $cf7_version, '4.1.0', '>=' ) )
82
+ $result->invalidate( $tag, wpcf7_get_message( 'fill_mathcaptcha' ) );
83
+ else {
84
+ $result['valid'] = false;
85
+ $result['reason'][$name] = wpcf7_get_message( 'fill_mathcaptcha' );
86
+ }
87
+ }
88
+ }
89
+
90
+ return $result;
91
+ }
92
+
93
+ // messages
94
+ add_filter( 'wpcf7_messages', 'wpcf7_mathcaptcha_messages' );
95
+
96
+ function wpcf7_mathcaptcha_messages( $messages ) {
97
+ return array_merge(
98
+ $messages, array(
99
+ 'wrong_mathcaptcha' => array(
100
+ 'description' => __( 'Invalid captcha value.', 'math-captcha' ),
101
+ 'default' => Math_Captcha()->core->error_messages['wrong']
102
+ ),
103
+ 'fill_mathcaptcha' => array(
104
+ 'description' => __( 'Please enter captcha value.', 'math-captcha' ),
105
+ 'default' => Math_Captcha()->core->error_messages['fill']
106
+ ),
107
+ 'time_mathcaptcha' => array(
108
+ 'description' => __( 'Captcha time expired.', 'math-captcha' ),
109
+ 'default' => Math_Captcha()->core->error_messages['time']
110
+ )
111
+ )
112
+ );
113
+ }
114
+
115
+ // warning message
116
+ add_action( 'wpcf7_admin_notices', 'wpcf7_mathcaptcha_display_warning_message' );
117
+
118
+ function wpcf7_mathcaptcha_display_warning_message() {
119
+ if ( empty( $_GET['post'] ) || ! ($contact_form = wpcf7_contact_form( $_GET['post'] )) )
120
+ return;
121
+
122
+ $has_tags = (bool) $contact_form->form_scan_shortcode( array( 'type' => array( 'mathcaptcha' ) ) );
123
+
124
+ if ( ! $has_tags )
125
+ return;
126
+ }
127
+
128
+ // tag generator
129
+ add_action( 'admin_init', 'wpcf7_add_tag_generator_mathcaptcha', 45 );
130
+
131
+ function wpcf7_add_tag_generator_mathcaptcha() {
132
+ if ( ! function_exists( 'wpcf7_add_tag_generator' ) )
133
+ return;
134
+
135
+ wpcf7_add_tag_generator( 'mathcaptcha', __( 'Math Captcha', 'math-captcha' ), 'wpcf7-mathcaptcha', 'wpcf7_tg_pane_mathcaptcha' );
136
+ }
137
+
138
+ function wpcf7_tg_pane_mathcaptcha( $contact_form ) {
139
+ echo '
140
+ <div class="control-box">
141
+ <fieldset>
142
+ <table class="form-table">
143
+ <tbody>
144
+ <tr>
145
+ <th scope="row">
146
+ <label for="tag-generator-panel-mathcaptcha-name">' . esc_html__( 'Name', 'contact-form-7' ) . '</label>
147
+ </th>
148
+ <td>
149
+ <input type="text" name="name" class="tg-name oneline" id="tag-generator-panel-mathcaptcha-name" />
150
+ </td>
151
+ </tr>
152
+ <tr>
153
+ <th scope="row">
154
+ <label for="tag-generator-panel-mathcaptcha-id">' . esc_html__( 'Id attribute', 'contact-form-7' ) . '</label>
155
+ </th>
156
+ <td>
157
+ <input type="text" name="id" class="idvalue oneline option" id="tag-generator-panel-mathcaptcha-id" />
158
+ </td>
159
+ </tr>
160
+ <tr>
161
+ <th scope="row">
162
+ <label for="tag-generator-panel-mathcaptcha-class">' . esc_html__( 'Class attribute', 'contact-form-7' ) . '</label>
163
+ </th>
164
+ <td>
165
+ <input type="text" name="class" class="classvalue oneline option" id="tag-generator-panel-mathcaptcha-class" />
166
+ </td>
167
+ </tr>
168
+ </tbody>
169
+ </table>
170
+ </fieldset>
171
+ </div>
172
+ <div class="insert-box">
173
+ <input type="text" name="mathcaptcha" class="tag code" readonly="readonly" onfocus="this.select();">
174
+ <div class="submitbox">
175
+ <input type="button" class="button button-primary insert-tag" value="' . esc_attr__( 'Insert Tag', 'contact-form-7' ) . '">
176
+ </div>
177
+ <br class="clear">
178
+ </div>';
179
  }
index.php CHANGED
@@ -1,2 +1,2 @@
1
- <?php
2
  // Silence is golden.
1
+ <?php
2
  // Silence is golden.
js/admin-settings.js CHANGED
@@ -1,7 +1,11 @@
1
- jQuery(document).ready(function($) {
2
-
3
- // resets options to defaults if needed
4
- $(document).on('click', '.reset_mc_settings', function() {
5
- return confirm(mcArgsSettings.resetToDefaults);
6
- });
7
- });
 
 
 
 
1
+ ( function ( $ ) {
2
+
3
+ $( document ).ready( function () {
4
+ // reset options to defaults if needed
5
+ $( document ).on( 'click', '.reset_mc_settings', function () {
6
+ return confirm( mcArgsSettings.resetToDefaults );
7
+ } );
8
+
9
+ } );
10
+
11
+ } )( jQuery );
languages/math-captcha-de_DE.mo DELETED
Binary file
languages/math-captcha-de_DE.po DELETED
@@ -1,349 +0,0 @@
1
- msgid ""
2
- msgstr ""
3
- "Project-Id-Version: Math Captcha\n"
4
- "POT-Creation-Date: 2013-08-08 08:01+0100\n"
5
- "PO-Revision-Date: 2013-08-08 08:01+0100\n"
6
- "Last-Translator: Stephan Hilchenbach <info@elcats.de>\n"
7
- "Language-Team: \n"
8
- "Language: de-DE\n"
9
- "MIME-Version: 1.0\n"
10
- "Content-Type: text/plain; charset=U