diff --git a/contact-form-7-email-validator.php b/contact-form-7-email-validator.php index bd2c2dd3f9f92d0737f580edf0717b9b79293568..685b38d60de04405a750302186eba8fac088586b 100644 --- a/contact-form-7-email-validator.php +++ b/contact-form-7-email-validator.php @@ -1,3 +1,4 @@ +<?php /* Plugin Name: Contact Form 7 Email Validator Plugin URI: https://wordpress.org/plugins/contact-form-7-email-validator @@ -8,23 +9,200 @@ Author URI: https://ant.sr License: MIT */ +define('API_LINK', '<a href="https://mailboxlayer.com" target="_blank">mailboxlayer</a>'); +define('API_KEY_LINK', '<a href="https://mailboxlayer.com/documentation#access_keys" target="_blank">mailboxlayer API key</a>'); + add_filter('wpcf7_validate_email*', 'wpcf7ev_validate_email', 20, 2); function wpcf7ev_validate_email($result, $tag) { $tag = new WPCF7_FormTag($tag); $email = isset($_POST[$tag->name]) ? trim($_POST[$tag->name]) : ''; - $mailboxlayer_access_key = 'YOUR_ACCESS_KEY'; + $options = get_option('wpcf7ev_options'); + + $mailboxlayer_access_key = $options['wpcf7ev_field_api_key']; $ch = curl_init('http://apilayer.net/api/check?access_key='.$mailboxlayer_access_key.'&email='.$email.''); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); - $validationResult = json_decode($json, true); + $response = json_decode($json, true); - if (!$validationResult['smtp_check']) { - $result->invalidate($tag, "This email address could not be verified!"); + if ($response['error']) { + $error = $response['error']['info']; + switch ($options['wpcf7ev_field_on_error']) { + case 'email': + $body = "The mailboxlayer API returned an error.\n\nEmail: $email\n\nError: $error"; + wp_mail(get_option('admin_email'), 'CF7 Email Validation Error', $body); + error_log("Sent mail to " . get_option('admin_email') . " about mailboxlayer API error."); + error_log("mailboxlayer API error when validating '" . $email . "': " . $error); + break; + case 'fail': + $result->invalidate($tag, "The email validation service returned an error.<br><small>Error: " . $error . "</small>"); + error_log("mailboxlayer API error when validating '" . $email . "': " . $error); + break; + default: + error_log("mailboxlayer API error when validating '" . $email . "': " . $error); + } + } else { + if (!$response['smtp_check']) { + $result->invalidate($tag, "This email address could not be verified!"); + } } return $result; } + +/* Settings Page */ + +function wpcf7ev_settings_init() { + // register a new setting for "wpcf7ev" page + register_setting( 'wpcf7ev', 'wpcf7ev_options' ); + + // register a new section in the "wpcf7ev" page + add_settings_section( + 'wpcf7ev_section_api', + __( 'Set up email validation for Contact Form 7 email fields here.', 'wpcf7ev' ), + 'wpcf7ev_section_api_cb', + 'wpcf7ev' + ); + + // register a new field in the "wpcf7ev_section_api" section, inside the "wpcf7ev" page + add_settings_field( + 'wpcf7ev_field_api_key', + __( 'API Key', 'wpcf7ev' ), + 'wpcf7ev_field_api_key_cb', + 'wpcf7ev', + 'wpcf7ev_section_api', + [ + 'label_for' => 'wpcf7ev_field_api_key', + 'class' => 'wpcf7ev_row', + ] + ); + + // register a new field in the "wpcf7ev_section_api" section, inside the "wpcf7ev" page + add_settings_field( + 'wpcf7ev_field_on_error', + __( 'On Error', 'wpcf7ev' ), + 'wpcf7ev_field_on_error_cb', + 'wpcf7ev', + 'wpcf7ev_section_api', + [ + 'label_for' => 'wpcf7ev_field_on_error', + 'class' => 'wpcf7ev_row', + ] + ); +} + +/** + * register our wpcf7ev_settings_init to the admin_init action hook + */ +add_action( 'admin_init', 'wpcf7ev_settings_init' ); + +/** + * callback functions + */ + +// api section cb + +// section callbacks can accept an $args parameter, which is an array. +// $args have the following keys defined: title, id, callback. +// the values are defined at the add_settings_section() function. +function wpcf7ev_section_api_cb( $args ) { + ?> + <p id="<?php echo esc_attr( $args['id'] ); ?>"><?php _e( 'This plugin requires a ' . API_LINK . ' account.', 'wpcf7ev' ); ?></p> + <?php +} + + +// api_key field cb +function wpcf7ev_field_api_key_cb( $args ) { + $options = get_option( 'wpcf7ev_options' ); + ?> + <input id="<?php echo esc_attr( $args['label_for'] ); ?>" + type="text" + size=36 + value="<?php echo esc_attr( $options[$args['label_for']] ); ?>" + name="wpcf7ev_options[<?php echo esc_attr( $args['label_for'] ); ?>]" + > + <p class="description"> + <?php _e( 'Enter your ' . API_KEY_LINK . '.', 'wpcf7ev' ); ?> + </p> + <?php +} + +// on_error field cb +function wpcf7ev_field_on_error_cb( $args ) { + $options = get_option( 'wpcf7ev_options' ); + $selected = $options['wpcf7ev_field_on_error']; + ?> + <select id="<?php echo esc_attr( $args['label_for'] ); ?>" + value="<?php echo esc_attr( $options[$args['label_for']] ); ?>" + name="wpcf7ev_options[<?php echo esc_attr( $args['label_for'] ); ?>]" + > +<option value="email" <?php if ($selected == 'email') echo 'selected=selected'; ?>>Send email to site admin</option> +<option value="fail" <?php if ($selected == 'fail') echo 'selected=selected'; ?>>Fail validation</option> +<option value="log" <?php if ($selected == 'log') echo 'selected=selected'; ?>>Only generate a PHP error</option> +</select> + <p class="description"> + <?php _e( 'What to do when the API returns an error.', 'wpcf7ev' ); ?> + </p> + <?php +} + +/** + * top level menu + */ +function wpcf7ev_options_page() { + // add top level menu page + add_menu_page( + 'Email Validation', + 'Email Validation Options', + 'manage_options', + 'wpcf7ev', + 'wpcf7ev_options_page_html' + ); +} + +/** + * register our wpcf7ev_options_page to the admin_menu action hook + */ +add_action( 'admin_menu', 'wpcf7ev_options_page' ); + +/** + * top level menu: + * callback functions + */ +function wpcf7ev_options_page_html() { + // check user capabilities + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + // add error/update messages + + // check if the user have submitted the settings + // wordpress will add the "settings-updated" $_GET parameter to the url + if ( isset( $_GET['settings-updated'] ) ) { + // add settings saved message with the class of "updated" + add_settings_error( 'wpcf7ev_messages', 'wpcf7ev_message', __( 'Settings Saved', 'wpcf7ev' ), 'updated' ); + } + + // show error/update messages + settings_errors( 'wpcf7ev_messages' ); + ?> + <div class="wrap"> + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> + <form action="options.php" method="post"> + <?php + settings_fields( 'wpcf7ev' ); + do_settings_sections( 'wpcf7ev' ); + submit_button( 'Save Settings' ); + ?> + </form> + </div> + <?php +} + + +?>