Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Contact form functionality
- * Used within the loop
- * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
- * @link https://codex.wordpress.org/Class_Reference/WP_Error
- * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
- * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
- * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
- * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
- */
- defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
- class gymclub_contact_form {
- private $reg_errors = new WP_Error;
- static public function form() { ?>
- <?php get_header(); ?>
- <script src="https://www.google.com/recaptcha/api.js" async defer></script>
- <article class="container">
- <?php if (have_posts()):
- while (have_posts()): the_post(); ?>
- <div class="row contact">
- <div class="col-md-6">
- <div class="form-area">
- <div class="text-center contact-h"><?php the_title();?></div>
- <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
- <div class="group form-group">
- <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <input class="form-control" id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <input class="form-control" id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <div class="text-group">
- <textarea class="form-control" id="message" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
- <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
- <i class="bar"></i>
- </div><!-- end div text-group -->
- </div><!-- end div group form-group -->
- <p class="message_success" id="message_success">Hola desde el formulario</p>
- <p class="message_error" id="message_error">Hola desde el formulario</p>
- <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
- <!--<input type="hidden" name="submitted" value="custom_action">-->
- <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
- <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
- </form><!-- end form -->
- </div><!--end respond -->
- </div><!-- end div col-md-6 -->
- <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
- <h3><?php echo __('Dates', 'gymclub'); ?></h3>
- <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
- <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
- <span class="dates_contact" itemprop="streetAddress"><i class="fas fa-map-marked-alt"></i><?php echo esc_attr( get_option('gym_contact_address_company') ); ?></span>
- <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
- <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
- <span class="dates_contact" itemprop="postalCode"><i class="fas fa-mail-bulk"></i><?php echo esc_attr( get_option('gym_contact_code_postal_company') ); ?></span>
- </div><!-- end div itemprop -->
- </div><!--end div col-md-6 div itemscope -->
- </div><!-- end div contact -->
- <?php
- endwhile;
- endif; ?>
- </article><!-- end section -->
- <?php }
- public function validate_form( $name, $email, $phone, $message, $response ) {
- $not_human = "Human verification incorrect.";
- $failed_connect = "An error has occurred while validating the recaptcha";
- $email_invalid = "Email Address Invalid.";
- $name_error = "Name should be at least 4 characters";
- $missing_content = "Please supply all information.";
- $response = wp_safe_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
- 'method' => 'POST',
- 'timeout' => 45,
- 'redirection' => 5,
- 'httpversion' => '1.0',
- 'blocking' => true,
- 'headers' => array(),
- 'body' => array(
- 'secret' => "tu-clave-secreta",
- 'response' => esc_attr($_POST['g-recaptcha-response'])),
- 'cookies' => array()
- )
- );
- // If any field is left empty, add the error message to the error array
- if ( empty($name) || empty($email) || empty($phone) || empty($message) || empty($response) ) {
- $this->reg_errors->add( esc_html__( $missing_content, 'gymclub') );
- }
- // if the name field isn't alphabetic, add the error message
- if ( strlen($name) < 4 ) {
- $this->reg_errors->add( esc_html__( $name_error, 'gymclub') );
- }
- // Check if the email is valid
- if ( !is_email($email) ) {
- $this->reg_errors->add( esc_html__( $email_invalid, 'gymclub') );
- }
- //Check if we have some kind of error in the connection with google
- if (is_wp_error( $response )) {
- $this->reg_errors->add( esc_html__( $failed_connect, 'gymclub' ));
- } else{
- //If we have successfully connected to google, we check if the answer is true or false
- $response = json_decode($response['body']);
- }if ($response->success == false) {
- $this->reg_errors->add( esc_html__( $not_human, 'gymclub' ));
- }
- }
- public function send_email( $name, $email, $phone, $message, $response ) {
- $message_sent = "Thanks! Your message has been sent.";
- if (count( $this->reg_errors ) < 1 ) {
- $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
- $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
- $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
- $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
- $to = get_option('gym_contact_admin_email');
- $headers[] = 'From: '. $name . ' <' . $email . '>';
- if ( wp_mail($to, $subject, $message, $headers)) { ?>
- <div class="row margin-button-small">
- <div class="col-md-12 alert alert-success">
- <button type="button" class="close" data-dismiss="alert" aria-label="close">
- <span aria-hidden="true">×</span>
- </button>
- <p class="message"><?php echo __( $message_sent, 'gymclub'); ?></p>
- </div>
- </div> <!-- end row -->
- <?php}
- }
- }
- public function process_functions() {
- if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
- return;
- } //end isset
- if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
- return;
- }// end verify nonce
- $url = wp_get_referer();
- $this->validate_form($_POST['message_name'], $_POST['message_email'], $_POST[''], $_POST['message_phone'], $_POST['message_text']), ($_POST['g-recaptcha-response']);
- if (is_wp_error( $this->reg_errors) && count( $this->reg_errors->get_error_messages() ) > 0 ) {
- foreach ( $this->reg_errors->get_error_messages() as $error ) {?>
- <div class="row margin-button-small">
- <div class="col-md-12 alert alert-success">
- <button type="button" class="close" data-dismiss="alert" aria-label="close">
- <span aria-hidden="true">×</span>
- </button>
- <p class="message"><?php echo __( $error, 'gymclub'); ?></p>
- </div>
- </div> <!-- end row -->
- <?php } //end reg_errors
- } // end is_wp_error
- wp_safe_redirect( $url );
- exit();
- } //end public function
- }
- // WordPress Ajax
- add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
- add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
- // Ajax insert data contact entry
- function gym_contact_create_entry($name, $email, $phone, $message ) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'contact';
- $wpdb->insert(
- $table_name,
- array(
- 'name' => $name,
- 'email' => $email,
- 'phone' => $phone,
- 'message' => $message,
- 'time' => current_time( 'mysql' )
- )
- );
- }
- ?>
- <?php get_footer(); ?>
Add Comment
Please, Sign In to add comment