*/
defined( 'ABSPATH' ) || die;
add_action( 'init', 'do_output_buffer' );
function do_output_buffer() {
ob_start();
}
/**
* Contact Form Frontend Code.
*/
function arcf_frontend() {
?>
', 'antirobot-contact-form' ),
$sender_name,
$sender_email
);
$sender_message .= "\r\n\r\n";
$sender_message .= sanitize_text_field( $_POST['arcf-message'] );
$admin_message = sprintf(
/* translators: 1: Admin E-Mail 2: WordPress URL */
__( 'You successfully sent the following message to %1$s (via %2$s)', 'antirobot-contact-form' ),
$admin_email,
get_bloginfo( 'url' )
);
$admin_message .= "\r\n\r\n";
$admin_message .= sanitize_text_field( $_POST['arcf-message'] );
$admin_subject = __( 'You successfully sent us an E-Mail!', 'antirobot-contact-form' );
$admin_headers[] = "From: $admin_email";
$admin_headers[] = "Reply-To: $admin_email";
$sender_headers[] = "From: $sender_name <$sender_email>";
$sender_headers[] = "Reply-To: $sender_name <$sender_email>";
$privatekey = sanitize_text_field( get_option( 'arcf_privatekey' ) );
$captcha = null;
// Verify that a POST Request was issued to reCAPTCHA ...
! empty( $_POST['g-recaptcha-response'] ) ? $captcha = $_POST['g-recaptcha-response'] : null;
// ... if not, throw an Error ...
if ( empty( $captcha ) ) {
echo '' .
__( 'Please solve the reCAPTCHA before submitting the form.', 'antirobot-contact-form' )
. '
';
} else {
// ... if so, get the response from the reCAPTCHA service ...
$response = wp_remote_fopen( 'https://www.google.com/recaptcha/api/siteverify?secret=' . $privatekey . '&response=' . $captcha . '&remoteip=' . $_SERVER['REMOTE_ADDR'] );
$json = json_decode( $response, true );
// ... if the service did not return true, throw an error ...
if ( true !== $json['success'] ) {
echo '' .
__( 'reCAPTCHA did not authorize your request. Make sure your keys are correct.',
'antirobot-contact-form' )
. '
';
// ... if it did return true, send these mails ...
} else {
// ... and if wp_mail() is not causing troubles ...
if ( wp_mail( $admin_email, $sender_subject, $sender_message, $sender_headers ) &&
wp_mail( $sender_email, $admin_subject, $admin_message, $admin_headers )
) {
// ... and notify the user of that successfully send mail.
if ( get_option( 'arcf_redirect' ) ) {
wp_redirect( get_permalink( get_option( 'arcf_redirect' ) ) );
exit;
}
echo '' .
__( 'Message successfully sent. You will receive an E-Mail confirmation soon.',
'antirobot-contact-form' )
. '
';
// ... but if wp_mail() is troubling, notify the user.
} else {
echo '' .
__( 'Mail Delivery with wp_mail() failed. Is your web server configuration allowing to send mails?',
'antirobot-contact-form' )
. '
';
}
}
}
}
}
/**
* Register the Antirobot Contact Form Shortcode.
*
* @return string
*/
function arcf_shortcode() {
ob_start();
arcf_validation();
arcf_frontend();
return ob_get_clean();
}
add_shortcode( 'antirobot_contact_form', 'arcf_shortcode' );
/**
* Register the backend settings.
*/
add_action( 'admin_menu', 'arcf_setup_menu' );
add_action( 'admin_init', 'arcf_register_settings' );
function arcf_register_settings() {
register_setting( 'arcf-option-group', 'arcf_publickey' );
register_setting( 'arcf-option-group', 'arcf_privatekey' );
register_setting( 'arcf-option-group', 'arcf_mailto' );
register_setting( 'arcf-option-group', 'arcf_subject' );
register_setting( 'arcf-option-group', 'arcf_placeholders' );
register_setting( 'arcf-option-group', 'arcf_invisible' );
register_setting( 'arcf-option-group', 'arcf_redirect' );
}
/**
* Setup the backend menu.
*/
function arcf_setup_menu() {
add_options_page( 'AntiRobot Contact Form', 'AntiRobot Contact Form', 'manage_options', 'antirobot-contact-form',
'arcf_init' );
}
/**
* Backend GUI.
*/
function arcf_init() {
?>
[antirobot_contact_form] on pages or posts to display the contact form.',
'antirobot-contact-form' ); ?>
leave a review and spread the word. If you want to support my coffee addiction, you can tip me on paypal.
',
'antirobot-contact-form' ); ?>