Advertisement
blackimpala

Modulo 6 POO Avanzado

Apr 3rd, 2021
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.31 KB | None | 0 0
  1. class gymclub_contact_form {
  2.  
  3.     private $reg_errors = array();
  4.  
  5.     static public function load_form() {
  6.  
  7.       require_once plugin_dir_path( dirname(__FILE__) ).'templates/contact_form.php';
  8.  
  9.      }
  10.  
  11.     public function validate_form( $name, $email, $phone, $message, $response ) {
  12.  
  13.               $not_human  = "Human verification incorrect.";
  14.               $failed_connect = "An error has occurred while validating the recaptcha";
  15.               $email_invalid   = "Email Address Invalid.";
  16.               $name_error   = "Name should be at least 4 characters";
  17.               $missing_content = "Please supply all information.";
  18.  
  19.  
  20.               $response = wp_safe_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
  21.                 'method' => 'POST',
  22.                 'timeout' => 45,
  23.                 'redirection' => 5,
  24.                 'httpversion' => '1.0',
  25.                 'blocking' => true,
  26.                 'headers' => array(),
  27.                 'body' => array(
  28.                   'secret' => "6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj",
  29.                   'response' => esc_attr($_POST['g-recaptcha-response'])),
  30.                 'cookies' => array()
  31.                 )
  32.               );
  33.  
  34.  
  35.           // If any field is left empty, add the error message to the error array
  36.             if ( empty($name) || empty($email) || empty($phone) || empty($message) || empty($response) ) {
  37.                 $this->reg_errors->add( esc_html__( $missing_content, 'gymclub') );
  38.             }
  39.  
  40.             // if the name field isn't alphabetic, add the error message
  41.             if ( strlen($name) < 4 ) {
  42.                 $this->reg_errors->add( esc_html__( $name_error, 'gymclub') );
  43.             }
  44.  
  45.             // Check if the email is valid
  46.             if ( !is_email($email) ) {
  47.                 $this->reg_errors->add( esc_html__( $email_invalid, 'gymclub') );
  48.             }
  49.  
  50.             //Check if we have some kind of error in the connection with google
  51.             if (is_wp_error( $response )) {
  52.                 $this->reg_errors->add( esc_html__( $failed_connect, 'gymclub' ));
  53.             } else{
  54.               //If we have successfully connected to google, we check if the answer is true or false
  55.                $response = json_decode($response['body']);
  56.             }if ($response->success == false) {
  57.                 $this->reg_errors->add( esc_html__( $not_human, 'gymclub' ));
  58.             }
  59.     }
  60.  
  61.     public function send_email( $name, $email, $phone, $message, $response ) {
  62.  
  63.             $message_sent    = "Thanks! Your message has been sent.";
  64.  
  65.  
  66.          if (count( $this->reg_errors )  < 1 ) {
  67.  
  68.  
  69.                   $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  70.                   $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  71.                   $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  72.                   $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  73.  
  74.                   $to = get_option('gym_contact_admin_email');
  75.  
  76.                   $headers[] = 'From: '. $name . ' <' . $email . '>';
  77.  
  78.                   if ( wp_mail($to, $subject, $message, $headers)) { ?>
  79.                       <div class="row margin-button-small">
  80.                           <div class="col-md-12 alert alert-success">
  81.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  82.                                   <span aria-hidden="true">&times;</span>
  83.                               </button>
  84.                               <p class="message"><?php echo __( $message_sent, 'gymclub'); ?></p>
  85.                           </div>
  86.                      </div> <!-- end row -->
  87.                   <?php}
  88.  
  89.  
  90.               }
  91.  
  92.     }
  93.  
  94.   public function process_functions() {
  95.  
  96.        if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
  97.  
  98.               return;
  99.  
  100.            } //end isset
  101.  
  102.        if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
  103.  
  104.               return;
  105.  
  106.            }// end verify nonce
  107.  
  108.          $url = wp_get_referer();
  109.  
  110.          $this->validate_form($_POST['message_name'], $_POST['message_email'], $_POST[''], $_POST['message_phone'], $_POST['message_text']), ($_POST['g-recaptcha-response']);
  111.  
  112.           if (is_wp_error( $this->reg_errors) && count( $this->reg_errors->get_error_messages() ) > 0 ) {
  113.                    foreach ( $this->reg_errors->get_error_messages() as $error ) {?>
  114.                        <div class="row margin-button-small">
  115.                           <div class="col-md-12 alert alert-success">
  116.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  117.                                   <span aria-hidden="true">&times;</span>
  118.                               </button>
  119.                               <p class="message"><?php echo __( $error, 'gymclub'); ?></p>
  120.                           </div>
  121.                        </div> <!-- end row -->
  122.                   <?php }  //end reg_errors
  123.  
  124.             } // end is_wp_error
  125.  
  126.             wp_safe_redirect( $url );
  127.             exit();
  128.  
  129.         } //end public function
  130.  
  131.  
  132.  
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement