blackimpala

formulario basico validacion

Mar 20th, 2021 (edited)
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.99 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Contact form functionality
  4.  * Used within the loop
  5.  * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
  6.  * @link https://codex.wordpress.org/Class_Reference/WP_Error
  7.  * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
  8.  * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
  9.  * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
  10.  * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
  11.  */
  12.  
  13.  
  14. defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
  15.  
  16. class gymclub_contact_form {
  17.        
  18. private $reg_errors = new WP_Error;
  19.  
  20. static public function form() { ?>
  21.  
  22. <?php get_header(); ?>  
  23.  
  24. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  25.  
  26. <article class="container">
  27.   <?php if (have_posts()):
  28.   while (have_posts()): the_post();  ?>
  29.   <div class="row contact">
  30.     <div class="col-md-6">
  31.       <div class="form-area">
  32.             <div class="text-center contact-h"><?php the_title();?></div>
  33.            
  34.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  35.                   <div class="group form-group">
  36.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  37.                       <span class="highlight"></span>
  38.                       <span class="bar"></span>
  39.                       <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
  40.                   </div><!-- end div group form-group -->
  41.                   <div class="group form-group">
  42.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  43.                       <span class="highlight"></span>
  44.                       <span class="bar"></span>
  45.                       <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
  46.                   </div><!-- end div group form-group -->
  47.                   <div class="group form-group">
  48.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  49.                       <span class="highlight"></span>
  50.                       <span class="bar"></span>
  51.                       <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
  52.                   </div><!-- end div group form-group -->
  53.                   <div class="group form-group">
  54.                       <div class="text-group">
  55.                           <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>
  56.                           <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
  57.                           <i class="bar"></i>
  58.                       </div><!-- end div text-group -->
  59.                   </div><!-- end div group form-group -->
  60.                   <p class="message_success" id="message_success">Hola desde el formulario</p>
  61.                   <p class="message_error" id="message_error">Hola desde el formulario</p>
  62.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  63.                   <!--<input type="hidden" name="submitted" value="custom_action">-->
  64.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  65.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
  66.             </form><!-- end form -->
  67.       </div><!--end respond -->
  68.    </div><!-- end div col-md-6 -->  
  69.  
  70.     <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
  71.         <h3><?php echo __('Dates', 'gymclub'); ?></h3>
  72.         <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
  73.         <div  itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
  74.           <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>
  75.            <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
  76.            <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
  77.            <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>
  78.         </div><!-- end div itemprop -->
  79.      </div><!--end div col-md-6 div itemscope -->
  80.  
  81.  </div><!-- end div contact -->
  82.    
  83.   <?php
  84.   endwhile;
  85.   endif; ?>    
  86.  
  87. </article><!-- end section -->
  88.  
  89. <?php }
  90.  
  91.     public function validate_form( $name, $email, $phone, $message, $response ) {
  92.  
  93.               $not_human  = "Human verification incorrect.";
  94.               $failed_connect = "An error has occurred while validating the recaptcha";
  95.               $email_invalid   = "Email Address Invalid.";
  96.               $name_error   = "Name should be at least 4 characters";
  97.               $missing_content = "Please supply all information.";
  98.  
  99.  
  100.               $response = wp_safe_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
  101.                 'method' => 'POST',
  102.                 'timeout' => 45,
  103.                 'redirection' => 5,
  104.                 'httpversion' => '1.0',
  105.                 'blocking' => true,
  106.                 'headers' => array(),
  107.                 'body' => array(
  108.                   'secret' => "tu-clave-secreta",
  109.                   'response' => esc_attr($_POST['g-recaptcha-response'])),
  110.                 'cookies' => array()
  111.                 )
  112.               );
  113.              
  114.            
  115.           // If any field is left empty, add the error message to the error array
  116.             if ( empty($name) || empty($email) || empty($phone) || empty($message) || empty($response) ) {
  117.                 $this->reg_errors->add( esc_html__( $missing_content, 'gymclub') );
  118.             }
  119.  
  120.             // if the name field isn't alphabetic, add the error message
  121.             if ( strlen($name) < 4 ) {
  122.                 $this->reg_errors->add( esc_html__( $name_error, 'gymclub') );
  123.             }
  124.  
  125.             // Check if the email is valid
  126.             if ( !is_email($email) ) {
  127.                 $this->reg_errors->add( esc_html__( $email_invalid, 'gymclub') );
  128.             }
  129.  
  130.             //Check if we have some kind of error in the connection with google
  131.             if (is_wp_error( $response )) {
  132.                 $this->reg_errors->add( esc_html__( $failed_connect, 'gymclub' ));
  133.             } else{
  134.               //If we have successfully connected to google, we check if the answer is true or false
  135.                $response = json_decode($response['body']);
  136.             }if ($response->success == false) {
  137.                 $this->reg_errors->add( esc_html__( $not_human, 'gymclub' ));
  138.             }
  139.     }
  140.  
  141.     public function send_email( $name, $email, $phone, $message, $response ) {
  142.  
  143.             $message_sent    = "Thanks! Your message has been sent.";
  144.  
  145.  
  146.          if (count( $this->reg_errors )  < 1 ) {
  147.                  
  148.                      
  149.                   $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  150.                   $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  151.                   $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  152.                   $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  153.  
  154.                   $to = get_option('gym_contact_admin_email');
  155.  
  156.                   $headers[] = 'From: '. $name . ' <' . $email . '>';
  157.  
  158.                   if ( wp_mail($to, $subject, $message, $headers)) { ?>
  159.                       <div class="row margin-button-small">
  160.                           <div class="col-md-12 alert alert-success">
  161.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  162.                                   <span aria-hidden="true">&times;</span>
  163.                               </button>
  164.                               <p class="message"><?php echo __( $message_sent, 'gymclub'); ?></p>
  165.                           </div>
  166.                      </div> <!-- end row -->
  167.                   <?php}
  168.  
  169.                  
  170.               }
  171.  
  172.     }
  173.  
  174.   public function process_functions() {
  175.  
  176.        if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
  177.  
  178.               return;
  179.  
  180.            } //end isset
  181.      
  182.        if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
  183.  
  184.               return;
  185.  
  186.            }// end verify nonce
  187.  
  188.          $url = wp_get_referer();
  189.          
  190.          $this->validate_form($_POST['message_name'], $_POST['message_email'], $_POST[''], $_POST['message_phone'], $_POST['message_text']), ($_POST['g-recaptcha-response']);
  191.          
  192.           if (is_wp_error( $this->reg_errors) && count( $this->reg_errors->get_error_messages() ) > 0 ) {
  193.                    foreach ( $this->reg_errors->get_error_messages() as $error ) {?>
  194.                        <div class="row margin-button-small">
  195.                           <div class="col-md-12 alert alert-success">
  196.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  197.                                   <span aria-hidden="true">&times;</span>
  198.                               </button>
  199.                               <p class="message"><?php echo __( $error, 'gymclub'); ?></p>
  200.                           </div>
  201.                        </div> <!-- end row -->
  202.                   <?php }  //end reg_errors
  203.                
  204.             } // end is_wp_error
  205.  
  206.             wp_safe_redirect( $url );
  207.             exit();
  208.          
  209.         } //end public function
  210.  
  211.  
  212.  
  213. }
  214.  
  215.  
  216. // WordPress Ajax
  217. add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
  218. add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
  219.  
  220.  
  221. // Ajax insert data contact entry
  222. function gym_contact_create_entry($name, $email, $phone, $message ) {
  223.   global $wpdb;
  224.   $table_name = $wpdb->prefix . 'contact';
  225.  
  226.   $wpdb->insert(
  227.         $table_name,
  228.         array(
  229.             'name' => $name,
  230.             'email' => $email,
  231.             'phone' => $phone,
  232.             'message' => $message,
  233.             'time' => current_time( 'mysql' )
  234.         )
  235.     );
  236. }
  237.  
  238.  
  239.  ?>
  240.              
  241. <?php get_footer(); ?>
  242.  
  243.  
Add Comment
Please, Sign In to add comment