PluginProbe ʕ •ᴥ•ʔ
Email Validator for Contact Form 7 / 1.1.7
Email Validator for Contact Form 7 v1.1.7
1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.8.0 1.8.1 1.8.2 1.8.3 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.5.0 1.5.1
email-validator-for-contact-form-7 / email-validator-for-contact-form-7.php
email-validator-for-contact-form-7 Last commit date
assets 5 years ago email-validator-for-contact-form-7.php 5 years ago license.txt 5 years ago readme.txt 5 years ago uninstall.php 5 years ago
email-validator-for-contact-form-7.php
491 lines
1 <?php
2
3 /*
4
5 Plugin Name: Email Validator for Contact Form 7
6
7 Plugin URI: https://developer.wordpress.org/plugins/email-validator-for-contact-form-7
8
9 Description: Enables Contact Form 7 users to validate their client’s email address before accepting their messages for sending using MailboxValidator. <strong>Before get started, install and activate the Contact Form 7 plugin first.</strong>
10
11 Version: 1.1.8
12
13 Author: MailboxValidator
14
15 Author URI: https://mailboxvalidator.com
16
17 License: GNU General Public License (GPL) version 3
18
19 License URI: https://www.gnu.org/licenses/gpl-2.0.html
20
21 */
22
23
24
25 // Check if Contact Form 7 is been installed or not
26
27 add_action('admin_init', 'mbv_wpcf7_check_wpcf7_installed');
28
29 function mbv_wpcf7_check_wpcf7_installed () {
30
31 if ( is_admin() && current_user_can( "activate_plugins" ) && ! is_plugin_active( "contact-form-7/wp-contact-form-7.php" ) ) {
32
33 add_action( 'admin_notices', 'mbv_wpcf7_nocf7_notice' );
34
35 deactivate_plugins( plugin_basename( __FILE__ ) );
36
37 remove_submenu_page( 'options-general.php', 'email-validator-for-contact-form-7' );
38
39 $flag = (int) $_GET['activate'];
40
41 if ( isset( $flag ) ) {
42
43 unset( $_GET['activate'] );
44
45 }
46
47 }
48
49 }
50
51 function mbv_wpcf7_nocf7_notice () {
52
53 $plugin_url = esc_url( admin_url( 'plugin-install.php?tab=search&s=contact+form+7' ) );
54
55 echo '<div class="notice notice-warning is-dismissible">
56
57 <p>Contact Form 7 must be installed and activated before using the MailboxValidator plugin. You can click <a href="' . $plugin_url . '">here</a> to install the Contact Form 7 plugin.</p>
58
59 </div>';
60
61 }
62
63 // Show notice if the MBV API key not been saved yet
64
65 function mbv_wpcf7_general_admin_notice(){
66
67 global $pagenow;
68
69 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
70
71 if ( $options['api_key'] == '' || $options['api_key'] == ' ' ) {
72
73 echo '<div class="notice notice-warning is-dismissible">
74
75 <p>Please get your MailboxValidator API key from <a href="https://www.mailboxvalidator.com/plans#api">here</a> and save in <a href="options-general.php?page=email-validator-for-contact-form-7">setting page</a>.</p>
76
77 </div>';
78
79 }
80
81 // else if ( $pagenow == 'plugins.php' && ( $options['api_key'] == '' || $options['api_key'] == ' ' ) ) {
82
83 // echo '<div class="notice notice-warning is-dismissible">
84
85 // <p>Please get your MailboxValidator API key from <a href="https://www.mailboxvalidator.com/plans#api">here</a> and save in <a href="options-general.php?page=email-validator-for-contact-form-7">setting page</a>.</p>
86
87 // </div>';
88
89 // }
90
91 }
92
93 add_action( 'admin_notices', 'mbv_wpcf7_general_admin_notice' );
94
95 // add the admin options page
96
97 add_action( 'admin_menu', 'mbv_wpcf7_plugin_admin_add_page' );
98
99 function mbv_wpcf7_plugin_admin_add_page() {
100
101 add_options_page( 'Email Validator for Contact Form 7', 'Email Validator for Contact Form 7', 'manage_options', 'email-validator-for-contact-form-7', 'mbv_wpcf7_plugin_options_page' );
102
103 wp_register_script( 'mailboxvalidator_email_validator_script', plugins_url( '/assets/js/mbv.js', __FILE__ ), array( 'jquery' ) );
104 wp_enqueue_script( 'mailboxvalidator_email_validator_script' );
105
106 }
107
108 // display the admin options page
109
110 function mbv_wpcf7_plugin_options_page() {
111
112 ?>
113
114 <div>
115
116 <h2>Email Validator for Contact Form 7 by MailboxValidator</h2>
117
118 <p>This plugin enables Contact Form 7 users to validate their client’s email address before accepting their messages for sending. It uses MailboxValidator service for email validation. Please get your free MailboxValidator API key from <a href="https://www.mailboxvalidator.com/plans#api">here</a>. Once you save the settings by clicking "Save Changes", your settings will be saved and Contact Form 7 will automatically detect the changes.</p>
119
120 <!--<p>At here you can edit your MailboxValidator API key and switch on or off disposable or free email validator.</p>-->
121 <?php
122 $mbv_options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
123 $api_key = isset( $mbv_options['api_key'] ) ? $mbv_options['api_key'] : '';
124 if ( $api_key != '' ) {
125 $url = 'https://api.mailboxvalidator.com/plan?key=' . $api_key;
126 $results = wp_remote_get( $url );
127 if ( !is_wp_error( $results ) ) {
128 $body = wp_remote_retrieve_body( $results );
129
130 // Decode the return json results and return the data.
131 $data = json_decode( $body, true );
132 // Now print the plan info
133 echo '<h2>Plan Information</h2><table class="form-table">';
134 echo '<tr><th scope="row"><label>Plan Name</label></th><td><p>' . $data['plan_name'] . '</p></td></tr>';
135 echo '<tr><th scope="row"><label>Credits Available</label></th><td><p>' . $data['credits_available'] . '</p></td></tr>';
136 echo '<tr><th scope="row"><label>Next Renewal Date</label></th><td><p>' . $data['next_renewal_date'] . '</p></td></tr>';
137 echo '</table>';
138 }
139 }
140 ?>
141
142 <form action="options.php" method="post">
143
144 <?php settings_fields( 'mbv_wpcf7_email_validator_for_contact_form_7' ); ?>
145
146 <?php do_settings_sections( 'mbv_wpcf7_plugin' ); ?>
147
148 <input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
149
150 </form>
151
152 </div>
153
154
155
156 <?php
157
158 }
159
160 // add the admin settings and such
161
162 add_action( 'admin_init', 'mbv_wpcf7_plugin_admin_init' );
163
164 function mbv_wpcf7_plugin_admin_init(){
165
166 register_setting( 'mbv_wpcf7_email_validator_for_contact_form_7', 'mbv_wpcf7_email_validator_for_contact_form_7' );
167
168 add_settings_section( 'mbv_wpcf7_plugin_main', 'Main Settings', 'mbv_wpcf7_plugin_section_text', 'mbv_wpcf7_plugin' );
169
170 add_settings_field( 'mbv_wpcf7_api_key', 'MailboxValidator API Key', 'mbv_wpcf7_api_key_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
171
172 add_settings_field( 'mbv_wpcf7_disposable_option', 'Disposable Email Validation', 'mbv_wpcf7_disposable_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
173
174 add_settings_field( 'mbv_wpcf7_disposable_error_message', 'Error Message for Disposable Email', 'mbv_wpcf7_disposable_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
175
176 add_settings_field( 'mbv_wpcf7_free_option', 'Free Email Validation', 'mbv_wpcf7_free_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
177
178 add_settings_field( 'mbv_wpcf7_free_error_message', 'Error Message for Free Email', 'mbv_wpcf7_free_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
179
180 }
181
182 function mbv_wpcf7_plugin_section_text() {
183
184 echo '<p>At here you can edit your MailboxValidator API key, switch on or off disposable or free email validator and design custom error message for disposable and free email.</p>';
185
186 }
187
188 function mbv_wpcf7_api_key_setting() {
189
190 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
191
192 $api_key = $options['api_key'] ?? ' ';
193
194 echo '<input id="api_key" name="mbv_wpcf7_email_validator_for_contact_form_7[api_key]" size="40" type="text" value="' . esc_attr( $api_key ). '" />';
195
196 }
197
198 function mbv_wpcf7_disposable_setting_option() {
199
200 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
201
202 $disposable_on_off = $options['disposable_on_off'] ?? 'on';
203
204 echo '<div id="radio1"><label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[disposable_on_off]" id="disposable_option" value="on"' . ( ( $disposable_on_off == 'on' ) ? ' checked' : '' ) . ' /> On</label>
205 <label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[disposable_on_off]" id="disposable_option" value="off"' . ( ( $disposable_on_off == 'off' ) ? ' checked' : '' ) . ' /> Off</label></div><br />';
206
207 }
208
209 function mbv_wpcf7_free_setting_option() {
210
211 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
212
213 $free_on_off = $options['free_on_off'] ?? 'on';
214
215 echo '<div id="radio2"><label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[free_on_off]" id="free_option" value="on"' . ( ( $free_on_off == 'on' ) ? ' checked' : '' ) . ' /> On</label>
216 <label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[free_on_off]" id="free_option" value="off"' . ( ( $free_on_off == 'off' ) ? ' checked' : '' ) . ' /> Off</label></div><br />';
217
218 }
219
220 function mbv_wpcf7_disposable_error_message_setting() {
221
222 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
223
224 $disposable_error_message = $options['disposable_error_message'] ?? 'Invalid email address. Please enter a non-disposable email address.';
225
226 $disposable_on_off = $options['disposable_on_off'] ?? 'on';
227
228 echo '<input id="disposable_error_message" name="mbv_wpcf7_email_validator_for_contact_form_7[disposable_error_message]" style="width:100%" type="text" value="' . $disposable_error_message . '" ' . ( $disposable_on_off == 'off' ? 'disabled="disabled" ' : '' ) . '/>';
229
230 }
231
232 function mbv_wpcf7_free_error_message_setting() {
233
234 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
235
236 $free_error_message = $options['free_error_message'] ?? 'Invalid email address. Please enter a non-free email address.';
237
238 $free_on_off = $options['free_on_off'] ?? 'on';
239
240 echo '<input id="free_error_message" name="mbv_wpcf7_email_validator_for_contact_form_7[free_error_message]" style="width:100%" type="text" value="' . $free_error_message . '" ' . ( $free_on_off == 'off' ? 'disabled="disabled" ' : '' ) . '/>';
241
242 }
243
244 // function mbv_wpcf7_string_length( $string ){
245
246 // return mb_strlen( $string );
247
248 // }
249
250 add_action( 'admin_enqueue_scripts', 'plugin_enqueues' );
251
252 add_action( 'wp_ajax_email_validator_for_contact_form_7_submit_feedback', 'submit_feedback' );
253 add_action( 'admin_footer_text', 'admin_footer_text' );
254
255 // Enqueue the script.
256 function plugin_enqueues( $hook ) {
257
258 // if ( $hook == 'options-general.php' ) {
259 // wp_enqueue_script( 'email_validator_for_contact_form_7_admin_setting_script', plugins_url( '/assets/js/feedback.js', __FILE__ ), ['jquery'], null, true );
260 // }
261
262 if ( $hook == 'plugins.php' ) {
263 // Add in required libraries for feedback modal
264 wp_enqueue_script( 'jquery-ui-dialog' );
265 wp_enqueue_style( 'wp-jquery-ui-dialog' );
266
267 wp_enqueue_script( 'email_validator_for_contact_form_7_admin_script', plugins_url( '/assets/js/feedback.js', __FILE__ ), ['jquery'], null, true );
268 }
269 }
270
271 function admin_footer_text( $footer_text ) {
272 // $plugin_name = substr( basename( __FILE__ ), 0, strpos( basename( __FILE__ ), '.' ) );
273 $plugin_name = 'email-validator-for-contact-form-7';
274 $current_screen = get_current_screen();
275
276 if ( ( $current_screen && strpos( $current_screen->id, $plugin_name ) !== false ) ) {
277 $footer_text .= sprintf(
278 __( 'Enjoyed %1$s? Please leave us a %2$s rating. A huge thanks in advance!', $plugin_name ),
279 '<strong>' . __( 'MailboxValidator Email Validator', $plugin_name ) . '</strong>',
280 '<a href="https://wordpress.org/support/plugin/' . $plugin_name . '/reviews/?filter=5/#new-post" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
281 );
282 }
283
284 if ( $current_screen->id == 'plugins' ) {
285 return $footer_text . '
286 <div id="email-validator-for-contact-form-7-feedback-modal" class="hidden" style="max-width:800px">
287 <span id="email-validator-for-contact-form-7-feedback-response"></span>
288 <p>
289 <strong>Would you mind sharing with us the reason to deactivate the plugin?</strong>
290 </p>
291 <p>
292 <label>
293 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="1"> I no longer need the plugin
294 </label>
295 </p>
296 <p>
297 <label>
298 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="2"> I couldn\'t get the plugin to work
299 </label>
300 </p>
301 <p>
302 <label>
303 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="3"> The plugin doesn\'t meet my requirements
304 </label>
305 </p>
306 <p>
307 <label>
308 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="4"> Other concerns
309 <br><br>
310 <textarea id="email-validator-for-contact-form-7-feedback-other" style="display:none;width:100%"></textarea>
311 </label>
312 </p>
313 <p>
314 <div style="float:left">
315 <input type="button" id="email-validator-for-contact-form-7-submit-feedback-button" class="button button-danger" value="Submit & Deactivate" />
316 </div>
317 <div style="float:right">
318 <a href="#">Skip & Deactivate</a>
319 </div>
320 </p>
321 </div>';
322 }
323
324 return $footer_text;
325 }
326
327 function submit_feedback() {
328 $feedback = ( isset( $_POST['feedback'] ) ) ? $_POST['feedback'] : '';
329 $others = sanitize_text_field (( isset( $_POST['others'] ) ) ? $_POST['others'] : '' );
330
331 $options = [
332 1 => 'I no longer need the plugin',
333 2 => 'I couldn\'t get the plugin to work',
334 3 => 'The plugin doesn\'t meet my requirements',
335 4 => 'Other concerns' . ( ( $others ) ? ( ' - ' . $others ) : '' ),
336 ];
337
338 $url = 'https://www.mailboxvalidator.com/wp-plugin-feedback?' . http_build_query( ['name' => 'email-validator-for-contact-form-7', 'message' => $options[$feedback],] );
339 // file_put_contents ( __DIR__ . '/mbv_plugin_logs.log' , $url . PHP_EOL, FILE_APPEND );
340
341 if ( isset( $options[$feedback] ) ) {
342 if ( !class_exists( 'WP_Http' ) ) {
343 include_once ABSPATH . WPINC . '/class-http.php';
344 }
345
346 $request = new WP_Http();
347 $response = $request->request( 'https://www.mailboxvalidator.com/wp-plugin-feedback?' . http_build_query( [
348 'name' => 'email-validator-for-contact-form-7',
349 'message' => $options[$feedback],
350 ] ), ['timeout' => 5] );
351 }
352 }
353
354 function mbv_wpcf7_validate_email_check_free( $emailAddress, $api_key ) {
355
356 // Now we need to send the data to MBV API Key and return back the result.
357
358 $url = 'https://api.mailboxvalidator.com/v1/email/free?key=' . str_replace( ' ', '', $api_key ) . '&email=' . str_replace( ' ', '', $emailAddress ) . '&source=wordpress_7';
359
360 $results = file_get_contents( $url );
361
362
363
364 // Decode the return json results and return the data.
365
366 $data = json_decode( $results, true );
367
368 mbv_wpcf7_mailboxvalidator_api_log( 'free', $data['email_address'], $data['is_free'], $data['credits_available'], $data['error_code'], $data['error_message'] );
369
370 if ( $data['error_message'] == '' ) {
371
372 if ( $data['is_free'] == 'False' ) {
373
374 return false;
375
376 } else {
377
378 return true;
379
380 }
381
382 }
383
384 }
385
386 function mbv_wpcf7_validate_email_check_disposable( $emailAddress, $api_key ) {
387
388 // Now we need to send the data to MBV API Key and return back the result.
389
390 $url = 'https://api.mailboxvalidator.com/v1/email/disposable?key=' . str_replace( ' ', '', $api_key ) . '&email=' . str_replace( ' ', '', $emailAddress ) . '&source=wordpress_7';
391
392 $results = file_get_contents( $url );
393
394
395
396 // Decode the return json results and return the data.
397
398 $data = json_decode( $results, true );
399
400 mbv_wpcf7_mailboxvalidator_api_log( 'disposable', $data['email_address'], $data['is_disposable'], $data['credits_available'], $data['error_code'], $data['error_message'] );
401
402 if ( $data['error_message'] == '' ) {
403
404 if ( $data['is_disposable'] == 'False' ) {
405
406 return false;
407
408 } else {
409
410 return true;
411
412 }
413
414 }
415
416 }
417
418 function mbv_wpcf7_custom_email_validator_filter( $result, $tags ) {
419
420 // Get option settings to know which validator is been called
421
422 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
423
424 $tags = new WPCF7_FormTag( $tags );
425
426 $type = $tags->type;
427
428 $name = $tags->name;
429
430 if ( ( 'email' == $type || 'email*' == $type ) && !( $options['api_key'] == '' ) ) {
431
432 // do validation for disposable and free
433
434 if( $options['disposable_on_off'] == 'on' ){
435
436 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'mbv_wpcf7_validate_email_check_disposable result: ' . mbv_wpcf7_validate_email_check_disposable( sanitize_email ( $_POST[$name] ), $options['api_key'] ) . PHP_EOL, FILE_APPEND );
437 if( ( mbv_wpcf7_validate_email_check_disposable( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true){
438
439 $result->invalidate( $tags, __( $options['disposable_error_message'] ?? 'Invalid email address. Please enter a non-disposable email address.', 'email-validator-for-contact-form-7' ) );
440
441 }elseif ( $options['free_on_off'] == 'on' ){
442
443 if( ( mbv_wpcf7_validate_email_check_free( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true ){
444
445 $result->invalidate( $tags, __( $options['free_error_message'] ?? 'Invalid email address. Please enter a non-free email address.', 'email-validator-for-contact-form-7' ) );
446
447 }
448
449 }
450
451 } elseif ( $options['free_on_off'] == 'on' ){
452
453 if( ( mbv_wpcf7_validate_email_check_free( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true ){
454
455 $result->invalidate( $tags, __( $options['free_error_message'] ?? 'Invalid email address. Please enter a non-free email address.', 'email-validator-for-contact-form-7' ) );
456
457 }
458
459 }
460
461 }
462
463 return $result;
464
465 }
466
467 add_filter( 'wpcf7_validate_email', 'mbv_wpcf7_custom_email_validator_filter', 20, 2 ); // Email field
468
469 add_filter( 'wpcf7_validate_email*', 'mbv_wpcf7_custom_email_validator_filter', 20, 2 ); // Req. Email field
470
471 function mbv_wpcf7_mailboxvalidator_api_log ( $mode, $api_result1, $api_result2, $api_result3, $api_result4, $api_result5 ) {
472
473 // Log all the MBV api usage to the log file
474
475 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Email Address: ' . $api_result1 . PHP_EOL, FILE_APPEND );
476
477 if ( $mode == 'disposable' ) {
478 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'is_disposable: ' . $api_result2 . PHP_EOL, FILE_APPEND );
479 } else {
480 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'is_free: ' . $api_result2 . PHP_EOL, FILE_APPEND );
481 }
482
483 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Credits Available: ' . $api_result3 . PHP_EOL, FILE_APPEND );
484
485 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Error Code: ' . $api_result4 == '' ?: '-' . PHP_EOL, FILE_APPEND );
486
487 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Error Message: ' . $api_result5 == '' ?: '-' . PHP_EOL, FILE_APPEND );
488
489 file_put_contents( __DIR__ . '/mbv_api_logs.log', str_repeat( '-', 120 ) . PHP_EOL, FILE_APPEND );
490
491 }