PluginProbe ʕ •ᴥ•ʔ
Email Validator for Contact Form 7 / 1.5.0
Email Validator for Contact Form 7 v1.5.0
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 languages 5 years ago vendor 5 years ago email-validator-for-contact-form-7.php 5 years ago license.txt 7 years ago readme.txt 5 years ago uninstall.php 7 years ago
email-validator-for-contact-form-7.php
696 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.5.0
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 Text Domain: email-validator-for-contact-form-7
22
23 */
24
25 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
26
27 // Check if Contact Form 7 is been installed or not
28
29 add_action('admin_init', 'mbv_wpcf7_check_wpcf7_installed');
30
31 function mbv_wpcf7_check_wpcf7_installed () {
32
33 if ( is_admin() && current_user_can( "activate_plugins" ) && ! is_plugin_active( "contact-form-7/wp-contact-form-7.php" ) ) {
34
35 add_action( 'admin_notices', 'mbv_wpcf7_nocf7_notice' );
36
37 deactivate_plugins( plugin_basename( __FILE__ ) );
38
39 remove_submenu_page( 'options-general.php', 'email-validator-for-contact-form-7' );
40
41 $flag = (int) $_GET['activate'];
42
43 if ( isset( $flag ) ) {
44
45 unset( $_GET['activate'] );
46
47 }
48
49 }
50
51 }
52
53 /*
54 * Localization
55 */
56 function mbv_localization()
57 {
58 // load_plugin_textdomain( 'email-validator-for-contact-form-7', false, dirname( plugin_basename( __FILE__ ) ) . '/langs' );
59 load_plugin_textdomain( 'email-validator-for-contact-form-7', false, plugins_url( '/languages' ) );
60 }
61
62 // add_action ( 'init', 'mbv_localization');
63 add_action ( 'admin_init', 'mbv_localization');
64
65 function mbv_wpcf7_nocf7_notice () {
66
67 $plugin_url = esc_url( admin_url( 'plugin-install.php?tab=search&s=contact+form+7' ) );
68
69 echo '<div class="notice notice-warning is-dismissible"><p>';
70 printf( __( 'You must <a href="%s">install</a> and activate the Contact Form 7 before using the MailboxValidator plugin.', 'email-validator-for-contact-form-7' ), $plugin_url );
71 echo '</p></div>';
72
73 }
74
75 // Show notice if the MBV API key not been saved yet
76
77 function mbv_wpcf7_general_admin_notice(){
78
79 global $pagenow;
80
81 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
82
83 if ( $options['api_key'] == '' || $options['api_key'] == ' ' ) {
84
85 echo '<div class="notice notice-warning is-dismissible"><p>';
86
87 printf( __( 'Please sign up for a <a href="%1$s">free MailboxValidator API key</a> to enable the email blocking.', 'email-validator-for-contact-form-7' ), 'https://www.mailboxvalidator.com/plans#api' );
88
89 echo '</p></div>';
90
91 }
92
93 // else if ( $pagenow == 'plugins.php' && ( $options['api_key'] == '' || $options['api_key'] == ' ' ) ) {
94
95 // echo '<div class="notice notice-warning is-dismissible">
96
97 // <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>
98
99 // </div>';
100
101 // }
102
103 }
104
105 add_action( 'admin_notices', 'mbv_wpcf7_general_admin_notice' );
106
107 // add the admin options page
108
109 add_action( 'admin_menu', 'mbv_wpcf7_plugin_admin_add_page' );
110
111 function mbv_wpcf7_plugin_admin_add_page() {
112
113 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' );
114
115 wp_register_script( 'mailboxvalidator_email_validator_script', plugins_url( '/assets/js/mbv.js', __FILE__ ), array( 'jquery' ) );
116 wp_enqueue_script( 'mailboxvalidator_email_validator_script' );
117
118 }
119
120 // display the admin options page
121
122 function mbv_wpcf7_plugin_options_page() {
123
124 ?>
125
126 <div>
127
128 <h2>Email Validator for Contact Form 7 by MailboxValidator</h2>
129
130 <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>
131
132 <!--<p>At here you can edit your MailboxValidator API key and switch on or off disposable or free email validator.</p>-->
133 <?php
134 $mbv_options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
135 $api_key = isset( $mbv_options['api_key'] ) ? $mbv_options['api_key'] : '';
136 if ( $api_key != '' ) {
137 $url = 'https://api.mailboxvalidator.com/plan?key=' . $api_key;
138 $results = wp_remote_get( $url );
139 if ( !is_wp_error( $results ) ) {
140 $body = wp_remote_retrieve_body( $results );
141
142 // Decode the return json results and return the data.
143 $data = json_decode( $body, true );
144
145 if ( $data['plan_name'] != '' ) {
146 if ( $data['plan_name'] == 'API-FREE' ) {
147 $is_low_credit = ( $data['credits_available'] < 100 ) ? true : false ;
148 } else {
149 $is_low_credit = ( $data['credits_available'] < ( $data['credits_limit'] * 0.1 ) ) ? true : false ;
150 }
151 // Now print the plan info
152 echo '<h2>'. __('Plan Information', 'email-validator-for-contact-form-7' ) . '</h2><table class="form-table">';
153 echo '<tr><th scope="row"><label>'. __('Plan Name', 'email-validator-for-contact-form-7' ) . '</label></th><td><p>' . $data['plan_name'] . '</p></td></tr>';
154 // echo '<tr><th scope="row"><label>Credits Available</label></th><td><p>' . $data['credits_available'] . ' ' . ( $is_low_credit ? '<button class="button"><a href="https://www.mailboxvalidator.com/plans#api" target="_blank">Get More Credits</a></button>' : '' ) . '</p></td></tr>';
155 echo '<tr><th scope="row"><label>'. __('Credits Available', 'email-validator-for-contact-form-7' ) . '</label></th><td><p>' . $data['credits_available'] . '<span style="margin-left: 20px"></span>' . ( $is_low_credit ? '<a href="https://www.mailboxvalidator.com/plans#api" target="_blank" class="button">Get More Credits</a>' : '' ) . '</p></td></tr>';
156 echo '<tr><th scope="row"><label>'. __('Next Renewal Date', 'email-validator-for-contact-form-7' ) . '</label></th><td><p>' . $data['next_renewal_date'] . '</p></td></tr>';
157 echo '</table>';
158 }
159 }
160 }
161 ?>
162
163 <form action="options.php" method="post">
164
165 <?php settings_fields( 'mbv_wpcf7_email_validator_for_contact_form_7' ); ?>
166
167 <?php do_settings_sections( 'mbv_wpcf7_plugin' ); ?>
168
169 <input name="Submit" type="submit" value="<?php esc_attr_e( 'Save Changes' ); ?>" />
170
171 </form>
172
173 </div>
174
175
176
177 <?php
178
179 }
180
181 // add the admin settings and such
182
183 add_action( 'admin_init', 'mbv_wpcf7_plugin_admin_init' );
184
185 function mbv_wpcf7_plugin_admin_init(){
186
187 register_setting( 'mbv_wpcf7_email_validator_for_contact_form_7', 'mbv_wpcf7_email_validator_for_contact_form_7' );
188
189 add_settings_section( 'mbv_wpcf7_plugin_main', __('Main Settings', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_plugin_section_text', 'mbv_wpcf7_plugin' );
190
191 add_settings_field( 'mbv_wpcf7_api_key', __('MailboxValidator API Key', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_api_key_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
192
193 add_settings_field( 'mbv_wpcf7_invalid_option', __('Block Invalid Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_invalid_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
194
195 add_settings_field( 'mbv_wpcf7_invalid_error_message', __('Error Message for Invalid Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_invalid_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
196
197 add_settings_field( 'mbv_wpcf7_disposable_option', __('Block Disposable Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_disposable_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
198
199 add_settings_field( 'mbv_wpcf7_disposable_error_message', __('Error Message for Disposable Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_disposable_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
200
201 add_settings_field( 'mbv_wpcf7_free_option', __('Block Free Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_free_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
202
203 add_settings_field( 'mbv_wpcf7_free_error_message', __('Error Message for Free Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_free_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
204
205 add_settings_field( 'mbv_wpcf7_role_option', __('Block Role-Based Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_role_setting_option', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
206
207 add_settings_field( 'mbv_wpcf7_role_error_message', __('Error Message for Role-based Email', 'email-validator-for-contact-form-7' ), 'mbv_wpcf7_role_error_message_setting', 'mbv_wpcf7_plugin', 'mbv_wpcf7_plugin_main' );
208
209 }
210
211 function mbv_wpcf7_plugin_section_text() {
212
213 echo '<p>'. __('Please enter a MailboxValidator API key to enable the email blocking.', 'email-validator-for-contact-form-7' ) . '</p>';
214
215 }
216
217 function mbv_wpcf7_api_key_setting() {
218
219 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
220
221 $api_key = $options['api_key'] ?? ' ';
222
223 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 ). '" />';
224
225 }
226
227 function mbv_wpcf7_invalid_setting_option() {
228
229 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
230
231 $invalid_on_off = $options['invalid_on_off'] ?? 'on';
232
233 echo '<div id="radio1"><label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[invalid_on_off]" id="invalid_option" value="on"' . ( ( $invalid_on_off == 'on' ) ? ' checked' : '' ) . ' /> '. __('On', 'email-validator-for-contact-form-7' ) . '</label>
234 <label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[invalid_on_off]" id="invalid_option" value="off"' . ( ( $invalid_on_off == 'off' ) ? ' checked' : '' ) . ' /> '. __('Off', 'email-validator-for-contact-form-7' ) . '</label></div><br />';
235
236 }
237
238 function mbv_wpcf7_disposable_setting_option() {
239
240 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
241
242 $disposable_on_off = $options['disposable_on_off'] ?? 'on';
243
244 echo '<div id="radio2"><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', 'email-validator-for-contact-form-7' ) . '</label>
245 <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', 'email-validator-for-contact-form-7' ) . '</label></div><br />';
246
247 }
248
249 function mbv_wpcf7_free_setting_option() {
250
251 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
252
253 $free_on_off = $options['free_on_off'] ?? 'on';
254
255 echo '<div id="radio3"><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', 'email-validator-for-contact-form-7' ) . '</label>
256 <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', 'email-validator-for-contact-form-7' ) . '</label></div><br />';
257
258 }
259
260 function mbv_wpcf7_role_setting_option() {
261
262 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
263
264 $role_on_off = $options['role_on_off'] ?? 'on';
265
266 echo '<div id="radio3"><label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[role_on_off]" id="free_option" value="on"' . ( ( $role_on_off == 'on' ) ? ' checked' : '' ) . ' /> '. __('On', 'email-validator-for-contact-form-7' ) . '</label>
267 <label><input type="radio" name="mbv_wpcf7_email_validator_for_contact_form_7[role_on_off]" id="free_option" value="off"' . ( ( $role_on_off == 'off' ) ? ' checked' : '' ) . ' /> '. __('Off', 'email-validator-for-contact-form-7' ) . '</label></div><br />';
268
269 }
270
271 function mbv_wpcf7_invalid_error_message_setting() {
272
273 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
274
275 $invalid_error_message = $options['invalid_error_message'] ?? __('Please enter a valid email address.', 'email-validator-for-contact-form-7' );
276
277 $invalid_on_off = $options['invalid_on_off'] ?? 'on';
278
279 echo '<input id="invalid_error_message" name="mbv_wpcf7_email_validator_for_contact_form_7[invalid_error_message]" style="width:100%" type="text" value="' . $invalid_error_message . '" ' . ( $invalid_on_off == 'off' ? 'disabled="disabled" ' : '' ) . '/>';
280
281 }
282
283 function mbv_wpcf7_disposable_error_message_setting() {
284
285 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
286
287 $disposable_error_message = $options['disposable_error_message'] ?? __('Please enter a non-disposable email address.', 'email-validator-for-contact-form-7' );
288
289 $disposable_on_off = $options['disposable_on_off'] ?? 'on';
290
291 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" ' : '' ) . '/>';
292
293 }
294
295 function mbv_wpcf7_free_error_message_setting() {
296
297 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
298
299 $free_error_message = $options['free_error_message'] ?? __('Please enter a non-free email address.', 'email-validator-for-contact-form-7' );
300
301 $free_on_off = $options['free_on_off'] ?? 'on';
302
303 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" ' : '' ) . '/>';
304
305 }
306
307 function mbv_wpcf7_role_error_message_setting() {
308
309 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
310
311 $role_error_message = $options['role_error_message'] ?? __('Please enter a non-role based email address.', 'email-validator-for-contact-form-7' );
312
313 $role_on_off = $options['role_on_off'] ?? 'on';
314
315 echo '<input id="role_error_message" name="mbv_wpcf7_email_validator_for_contact_form_7[role_error_message]" style="width:100%" type="text" value="' . $role_error_message . '" ' . ( $role_on_off == 'off' ? 'disabled="disabled" ' : '' ) . '/>';
316
317 }
318
319 // function mbv_wpcf7_string_length( $string ){
320
321 // return mb_strlen( $string );
322
323 // }
324
325 add_action( 'admin_enqueue_scripts', 'plugin_enqueues' );
326
327 add_action( 'wp_ajax_email_validator_for_contact_form_7_submit_feedback', 'submit_feedback' );
328 add_action( 'admin_footer_text', 'admin_footer_text' );
329
330 // Enqueue the script.
331 function plugin_enqueues( $hook ) {
332
333 // if ( $hook == 'options-general.php' ) {
334 // wp_enqueue_script( 'email_validator_for_contact_form_7_admin_setting_script', plugins_url( '/assets/js/feedback.js', __FILE__ ), ['jquery'], null, true );
335 // }
336
337 if ( $hook == 'plugins.php' ) {
338 // Add in required libraries for feedback modal
339 wp_enqueue_script( 'jquery-ui-dialog' );
340 wp_enqueue_style( 'wp-jquery-ui-dialog' );
341
342 wp_enqueue_script( 'email_validator_for_contact_form_7_admin_script', plugins_url( '/assets/js/feedback.js', __FILE__ ), ['jquery'], null, true );
343 }
344 }
345
346 function admin_footer_text( $footer_text ) {
347 // $plugin_name = substr( basename( __FILE__ ), 0, strpos( basename( __FILE__ ), '.' ) );
348 $plugin_name = 'email-validator-for-contact-form-7';
349 $current_screen = get_current_screen();
350
351 if ( ( $current_screen && strpos( $current_screen->id, $plugin_name ) !== false ) ) {
352 $footer_text .= sprintf(
353 __( 'Love our plugin? Please leave us a %1$s rating. A huge thanks in advance!', $plugin_name ),
354 '<a href="https://wordpress.org/support/plugin/' . $plugin_name . '/reviews/?filter=5/#new-post" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
355 );
356 }
357
358 if ( $current_screen->id == 'plugins' ) {
359 return $footer_text . '
360 <div id="email-validator-for-contact-form-7-feedback-modal" class="hidden" style="max-width:800px">
361 <span id="email-validator-for-contact-form-7-feedback-response"></span>
362 <p>
363 <strong>'. __('Would you mind sharing with us the reason to deactivate the plugin', 'email-validator-for-contact-form-7' ) . '?</strong>
364 </p>
365 <p>
366 <label>
367 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="1"> '. __('I no longer need the plugin', 'email-validator-for-contact-form-7' ) . '
368 </label>
369 </p>
370 <p>
371 <label>
372 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="2"> '. __('I couldn\'t get the plugin to work', 'email-validator-for-contact-form-7' ) . '
373 </label>
374 </p>
375 <p>
376 <label>
377 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="3"> '. __('The plugin doesn\'t meet my requirements', 'email-validator-for-contact-form-7' ) . '
378 </label>
379 </p>
380 <p>
381 <label>
382 <input type="radio" name="email-validator-for-contact-form-7-feedback" value="4"> '. __('Other concerns', 'email-validator-for-contact-form-7' ) . '
383 <br><br>
384 <textarea id="email-validator-for-contact-form-7-feedback-other" style="display:none;width:100%"></textarea>
385 </label>
386 </p>
387 <p>
388 <div style="float:left">
389 <input type="button" id="email-validator-for-contact-form-7-submit-feedback-button" class="button button-danger" value="'. esc_attr__('Submit & Deactivate', 'email-validator-for-contact-form-7' ) . '" />
390 </div>
391 <div style="float:right">
392 <a href="#">'. __('Skip & Deactivate', 'email-validator-for-contact-form-7' ) . '</a>
393 </div>
394 </p>
395 </div>';
396 }
397
398 return $footer_text;
399 }
400
401 function submit_feedback() {
402
403 $feedback = ( isset( $_POST['feedback'] ) ) ? $_POST['feedback'] : '';
404 $others = sanitize_text_field (( isset( $_POST['others'] ) ) ? $_POST['others'] : '' );
405
406 $options = [
407 1 => 'I no longer need the plugin',
408 2 => 'I couldn\'t get the plugin to work',
409 3 => 'The plugin doesn\'t meet my requirements',
410 4 => 'Other concerns' . ( ( $others ) ? ( ' - ' . $others ) : '' ),
411 ];
412
413 $url = 'https://www.mailboxvalidator.com/wp-plugin-feedback?' . http_build_query( ['name' => 'email-validator-for-contact-form-7', 'message' => $options[$feedback],] );
414 // file_put_contents ( __DIR__ . '/mbv_plugin_logs.log' , $url . PHP_EOL, FILE_APPEND );
415
416 if ( isset( $options[$feedback] ) ) {
417 if ( !class_exists( 'WP_Http' ) ) {
418 include_once ABSPATH . WPINC . '/class-http.php';
419 }
420
421 $request = new WP_Http();
422 $response = $request->request( 'https://www.mailboxvalidator.com/wp-plugin-feedback?' . http_build_query( [
423 'name' => 'email-validator-for-contact-form-7',
424 'message' => $options[$feedback],
425 ] ), ['timeout' => 5] );
426 }
427 }
428
429 function mbv_wpcf7_single( $emailAddress, $api_key ) {
430
431 try{
432 // Now we need to send the data to MBV API Key and return back the result.
433 // $url = 'https://api.mailboxvalidator.com/v1/validation/single?key=' . str_replace( ' ', '', $api_key ) . '&email=' . str_replace( ' ', '', $emailAddress ) . '&source=wordpress';
434
435 // Now we used WordPress custom HTTP API method to get the result from MBV API.
436 // $results = wp_remote_get( $url
437
438 $mbv = new \MailboxValidator\EmailValidation ($api_key);
439 $results = $mbv->validateEmail(str_replace( ' ', '', $emailAddress ));
440
441 if ($results != null) {
442 foreach ($results as $key => $value) {
443 $data[$key] = $value;
444 }
445 return $data;
446 } else {
447 return true;
448 }
449
450 // if ( !is_wp_error( $results ) ) {
451 // $body = wp_remote_retrieve_body( $results );
452
453 // Decode the return json results and return the data.
454 // $data = json_decode( $body, true );
455
456 // update remaining_credits
457 // $mbv_options = get_option( 'mbv_email_validator' );
458 // $mbv_options['remaining_credits'] = $data['credits_available'];
459 // update_option('mbv_email_validator', $mbv_options );
460
461 // return $data;
462 // } else {
463 // if connection error, let it pass
464 // return true;
465 // }
466 }
467 catch( Exception $e ) {
468 return true;
469 }
470 }
471
472 function mbv_wpcf7_is_valid_email( $api_result ) {
473
474 if ( $api_result != '' ) {
475 // mbv_wpcf7_mailboxvalidator_api_log( 'is_valid', $api_result['email_address'], $api_result['status'], $api_result['credits_available'], $api_result['error_code'], $api_result['error_message'] );
476 if ( $api_result['error_message'] === '' ) {
477 if ( $api_result['status'] === 'False' ) {
478 return false;
479 } else {
480 return true;
481 }
482 } else {
483 // If error message occured, let it pass first.
484 return true;
485 }
486 } else {
487 // If error message occured, let it pass first.
488 return true;
489 }
490 }
491
492 function mbv_wpcf7_is_role_email( $api_result ) {
493
494 if ( $api_result != '' ) {
495 // mbv_wpcf7_mailboxvalidator_api_log( 'is_valid', $api_result['email_address'], $api_result['status'], $api_result['credits_available'], $api_result['error_code'], $api_result['error_message'] );
496 if ( $api_result['error_message'] === '' ) {
497 if ( $api_result['is_role'] === 'False' ) {
498 return false;
499 } else {
500 return true;
501 }
502 } else {
503 // If error message occured, let it pass first.
504 return true;
505 }
506 } else {
507 // If error message occured, let it pass first.
508 return true;
509 }
510 }
511
512 function mbv_wpcf7_validate_email_check_free( $emailAddress, $api_key ) {
513
514 // Now we need to send the data to MBV API Key and return back the result.
515
516 // $url = 'https://api.mailboxvalidator.com/v1/email/free?key=' . str_replace( ' ', '', $api_key ) . '&email=' . str_replace( ' ', '', $emailAddress ) . '&source=wordpress_7';
517
518 // $results = file_get_contents( $url );
519
520
521
522 // Decode the return json results and return the data.
523
524 // $data = json_decode( $results, true );
525
526 $mbv = new \MailboxValidator\EmailValidation ($api_key);
527 $results = $mbv->isFreeEmail(str_replace( ' ', '', $emailAddress ));
528
529 // mbv_wpcf7_mailboxvalidator_api_log( 'is_free', $data['email_address'], $data['is_free'], $data['credits_available'], $data['error_code'], $data['error_message'] );
530
531 // if ( $data['error_message'] == '' ) {
532 if ($results != null) {
533 if ( trim( $results->error_code ) === '' ) {
534
535 // if ( $data['is_free'] == 'False' ) {
536 if ( $results->is_free === 'False' ) {
537
538 return false;
539
540 } else {
541
542 return true;
543
544 }
545
546 } else {
547 return false;
548 }
549 } else {
550 return false;
551 }
552
553 }
554
555 function mbv_wpcf7_validate_email_check_disposable( $emailAddress, $api_key ) {
556
557 // Now we need to send the data to MBV API Key and return back the result.
558
559 // $url = 'https://api.mailboxvalidator.com/v1/email/disposable?key=' . str_replace( ' ', '', $api_key ) . '&email=' . str_replace( ' ', '', $emailAddress ) . '&source=wordpress_7';
560
561 // $results = file_get_contents( $url );
562
563
564
565 // Decode the return json results and return the data.
566
567 // $data = json_decode( $results, true );
568
569 // mbv_wpcf7_mailboxvalidator_api_log( 'is_disposable', $data['email_address'], $data['is_disposable'], $data['credits_available'], $data['error_code'], $data['error_message'] );
570
571 $mbv = new \MailboxValidator\EmailValidation ($api_key);
572 $results = $mbv->isDisposableEmail(str_replace( ' ', '', $emailAddress ));
573
574 // if ( $data['error_message'] == '' ) {
575 if ($results != null) {
576 if ( trim( $results->error_code ) === '' ) {
577
578 // if ( $data['is_disposable'] == 'False' ) {
579 if ( $results->is_disposable === 'False' ) {
580
581 return false;
582
583 } else {
584
585 return true;
586
587 }
588
589 } else {
590 return false;
591 }
592 } else {
593 return false;
594 }
595
596 }
597
598 function mbv_wpcf7_custom_email_validator_filter( $result, $tags ) {
599
600 // Get option settings to know which validator is been called
601
602 $options = get_option( 'mbv_wpcf7_email_validator_for_contact_form_7' );
603
604 $tags = new WPCF7_FormTag( $tags );
605
606 $type = $tags->type;
607
608 $name = $tags->name;
609
610 if ( ( 'email' === $type || 'email*' === $type ) && !( $options['api_key'] === '' ) ) {
611
612 // do validation for disposable and free
613
614 /*
615 if( $options['disposable_on_off'] == 'on' ){
616
617 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 );
618 if( ( mbv_wpcf7_validate_email_check_disposable( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true){
619
620 $result->invalidate( $tags, __( $options['disposable_error_message'] ?? 'Please enter a non-disposable email address.', 'email-validator-for-contact-form-7' ) );
621
622 }elseif ( $options['free_on_off'] == 'on' ){
623
624 if( ( mbv_wpcf7_validate_email_check_free( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true ){
625
626 $result->invalidate( $tags, __( $options['free_error_message'] ?? 'Please enter a non-free email address.', 'email-validator-for-contact-form-7' ) );
627
628 }
629
630 }
631
632 } elseif ( $options['free_on_off'] == 'on' ){
633
634 if( ( mbv_wpcf7_validate_email_check_free( sanitize_email ( $_POST[$name] ), $options['api_key'] ) ) == true ){
635
636 $result->invalidate( $tags, __( $options['free_error_message'] ?? 'Please enter a non-free email address.', 'email-validator-for-contact-form-7' ) );
637
638 }
639
640 }*/
641 $single_result = $options['invalid_on_off'] == 'on' || $options['role_on_off'] == 'on' ? mbv_wpcf7_single( $_POST[$name], $options['api_key'] ) : '';
642 $is_valid_email = $options['invalid_on_off'] == 'on' && $single_result != '' ? mbv_wpcf7_is_valid_email( $single_result ) : true;
643 $is_role = $options['role_on_off'] == 'on' && $single_result != '' ? mbv_wpcf7_is_role_email( $single_result ) : false;
644 $is_disposable = $options['disposable_on_off'] == 'on' ? mbv_wpcf7_validate_email_check_disposable( $_POST[$name] , $options['api_key'] ) : false;
645 $is_free = $options['free_on_off'] == 'on' ? mbv_wpcf7_validate_email_check_free( $_POST[$name] , $options['api_key'] ) : false;
646
647 if( $is_valid_email === false ){
648 if ( ($options['disposable_on_off'] === 'on') && ( $is_disposable === true ) ) {
649 $result->invalidate( $tags, __( $options['disposable_error_message'] ?? 'Please enter a non-disposable email address.', 'email-validator-for-contact-form-7' ) );
650 } else {
651 $result->invalidate( $tags, __( $options['invalid_error_message'] ?? 'Please enter a valid email address.', 'email-validator-for-contact-form-7' ) );
652 }
653 } elseif( $is_disposable === true ){
654 $result->invalidate( $tags, __( $options['disposable_error_message'] ?? 'Please enter a non-disposable email address.', 'email-validator-for-contact-form-7' ) );
655 } elseif( $is_free === true ){
656 // $mbv_validation_result['status'] = false;
657 $result->invalidate( $tags, __( $options['free_error_message'] ?? 'Please enter a non-free email address.', 'email-validator-for-contact-form-7' ) );
658 } elseif( $is_role === true ){
659 $result->invalidate( $tags, __( $options['role_error_message'] ?? 'Please enter a non-role email address.', 'email-validator-for-contact-form-7' ) );
660 }
661
662 }
663
664 return $result;
665
666 }
667
668 add_filter( 'wpcf7_validate_email', 'mbv_wpcf7_custom_email_validator_filter', 20, 2 ); // Email field
669
670 add_filter( 'wpcf7_validate_email*', 'mbv_wpcf7_custom_email_validator_filter', 20, 2 ); // Req. Email field
671
672 function mbv_wpcf7_mailboxvalidator_api_log ( $mode, $api_result1, $api_result2, $api_result3, $api_result4, $api_result5 ) {
673
674 // Log all the MBV api usage to the log file
675
676 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Email Address: ' . $api_result1 . PHP_EOL, FILE_APPEND );
677
678 /*
679 if ( $mode == 'disposable' ) {
680 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'is_disposable: ' . $api_result2 . PHP_EOL, FILE_APPEND );
681 } else {
682 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'is_free: ' . $api_result2 . PHP_EOL, FILE_APPEND );
683 }
684 */
685
686 file_put_contents( __DIR__ . '/mbv_api_logs.log', $mode . ': ' . $api_result2 . PHP_EOL, FILE_APPEND );
687
688 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Credits Available: ' . $api_result3 . PHP_EOL, FILE_APPEND );
689
690 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Error Code: ' . $api_result4 == '' ?: '-' . PHP_EOL, FILE_APPEND );
691
692 file_put_contents( __DIR__ . '/mbv_api_logs.log', 'Error Message: ' . $api_result5 == '' ?: '-' . PHP_EOL, FILE_APPEND );
693
694 file_put_contents( __DIR__ . '/mbv_api_logs.log', str_repeat( '-', 120 ) . PHP_EOL, FILE_APPEND );
695
696 }