css
1 year ago
img
1 year ago
js
1 year ago
partials
1 year ago
class-email.php
1 year ago
class-eqw-advance.php
1 year ago
class-eqw-enquiry-cart.php
1 year ago
class-eqw-enquiry-shortcode.php
1 year ago
class-eqw-product.php
1 year ago
class-eqw-save-enquiry.php
1 year ago
class-pisol-enquiry-quotation-woocommerce-public.php
1 year ago
class-pisol-form.php
1 year ago
class-webhook.php
1 year ago
index.php
1 year ago
class-pisol-form.php
245 lines
| 1 | <?php |
| 2 | |
| 3 | class class_pisol_form{ |
| 4 | |
| 5 | public $items; |
| 6 | public $template; |
| 7 | public $errors; |
| 8 | |
| 9 | function __construct($items){ |
| 10 | $this->items = $items; |
| 11 | $this->template = 'template1'; |
| 12 | $this->errors = array(); |
| 13 | if($this->enquiryPresent()){ |
| 14 | $this->form_page(); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | function enquiryPresent(){ |
| 19 | return true; //(class_eqw_enquiry_cart::isThereProductsInEnquirySession()); |
| 20 | } |
| 21 | |
| 22 | function form_page(){ |
| 23 | if(!empty($_POST) && count($_POST) > 0 ){ |
| 24 | $this->validation(); |
| 25 | $this->error(); |
| 26 | if($this->submit_form()){ |
| 27 | $this->success_msg(); |
| 28 | }else{ |
| 29 | $this->form(); |
| 30 | } |
| 31 | |
| 32 | }else{ |
| 33 | $this->form(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function form(){ |
| 38 | echo '<form method="post" id="pi-eqw-enquiry-form">'; |
| 39 | $this->items(); |
| 40 | echo '</form>'; |
| 41 | } |
| 42 | |
| 43 | function success_msg(){ |
| 44 | echo '<div class="woocommerce-notices-wrapper">'; |
| 45 | echo '<div class="woocommerce-message"><span id="pi-form-submitted-success"></span>'; |
| 46 | echo esc_html__('Enquiry submitted','pisol-enquiry-quotation-woocommerce'); |
| 47 | echo '</div>'; |
| 48 | echo '</div>'; |
| 49 | } |
| 50 | |
| 51 | function validation(){ |
| 52 | foreach($this->items as $item){ |
| 53 | $this->required($item); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function required($item){ |
| 58 | $enable_honey_pot = get_option('pi_eqw_enable_honeypot', 1); |
| 59 | |
| 60 | if(!empty($enable_honey_pot) && $item['type'] == 'honeypot'){ |
| 61 | if(isset($_POST[$item['name']]) && $_POST[$item['name']] != ""){ |
| 62 | $this->errors[] = array( |
| 63 | 'error'=> sprintf(__('Form submitted','pisol-enquiry-quotation-woocommerce')) |
| 64 | ); |
| 65 | $this->clearSession(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if($item['type'] == 'captcha' && PISOL_ENQ_CaptchaGenerator::captcha_enabled()){ |
| 70 | if(isset($_POST['captcha_field']) && !empty($_POST['captcha_field'])){ |
| 71 | if( !PISOL_ENQ_CaptchaGenerator::validateCaptcha($_POST['captcha_field']) ){ |
| 72 | $this->errors['captcha-error'] = array( |
| 73 | 'error'=> __('Captcha code does not match','pisol-enquiry-quotation-woocommerce') |
| 74 | ); |
| 75 | } |
| 76 | }else{ |
| 77 | $this->errors['captcha-error'] = array( |
| 78 | 'error'=> __('Captcha cant be left empty','pisol-enquiry-quotation-woocommerce') |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if(isset($item['required']) && $item['required'] == 'required'){ |
| 84 | if(isset($_POST[$item['name']]) && $_POST[$item['name']] != ""){ |
| 85 | return true; |
| 86 | }else{ |
| 87 | $this->errors[] = array( |
| 88 | 'error'=> sprintf(__('Cant leave %s empty','pisol-enquiry-quotation-woocommerce'), $item['placeholder']) |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | function submit_form(){ |
| 96 | if( count($this->errors) <= 0 && !empty($_POST)){ |
| 97 | $save = $this->saveEnquiry(); |
| 98 | if($save !== false){ |
| 99 | $email = $this->sendEmail($save); |
| 100 | $clear_product = $this->clearSession(); |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | function saveEnquiry(){ |
| 108 | $obj = new class_eqw_save_enquiry($this->items); |
| 109 | return $obj->save(); |
| 110 | } |
| 111 | |
| 112 | function sendEmail($enq_id){ |
| 113 | $email_obj = new class_pisol_eqw_email($this->items, $enq_id); |
| 114 | $email_obj->sendEmail(); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | function clearSession(){ |
| 119 | class_eqw_enquiry_cart::deleteProductsFromEnquirySession(); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | function error(){ |
| 124 | $error_msg = ""; |
| 125 | if(is_array($this->errors) && count($this->errors) > 0){ |
| 126 | foreach($this->errors as $key => $error){ |
| 127 | $error_msg .= '<li data-error-id="'.esc_attr($key).'">'.esc_html($error['error']).'</li>'; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if($error_msg != ""){ |
| 132 | echo '<div class="woocommerce-notices-wrapper">'; |
| 133 | echo '<div class="woocommerce-error">'; |
| 134 | echo wp_kses_post( $error_msg ); |
| 135 | echo '</div>'; |
| 136 | echo '</div>'; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | function items(){ |
| 141 | foreach($this->items as $item){ |
| 142 | $this->item($item); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | function item($item){ |
| 147 | if(method_exists($this,$item['type'])){ |
| 148 | $this->{$item['type']}($item); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | function errorMsgLabel($item){ |
| 153 | $error_label = ''; |
| 154 | if($item['name'] === 'pi_phone'){ |
| 155 | $error_label .= ' data-msg-digits="'.esc_attr__('Please enter only digits.','pisol-enquiry-quotation-woocommerce').'" '; |
| 156 | } |
| 157 | |
| 158 | if(isset($item['required']) && $item['required'] === 'required'){ |
| 159 | $error_label .= ' data-msg-required="'.esc_attr__('This field is required.','pisol-enquiry-quotation-woocommerce').'" '; |
| 160 | } |
| 161 | |
| 162 | if($item['name'] === 'pi_email'){ |
| 163 | $error_label .= ' data-msg-email="'.esc_attr__('Please enter a valid email address','pisol-enquiry-quotation-woocommerce').'" '; |
| 164 | } |
| 165 | |
| 166 | return $error_label; |
| 167 | } |
| 168 | |
| 169 | function text($item){ |
| 170 | |
| 171 | if($item['name'] === 'pi_phone'){ |
| 172 | $rule = ' data-rule-digits '; |
| 173 | } |
| 174 | |
| 175 | $error_label = $this->errorMsgLabel($item); |
| 176 | |
| 177 | if($item['required'] == 'required'){ |
| 178 | $placeholder = $item['placeholder'].' *'; |
| 179 | }else{ |
| 180 | $placeholder = $item['placeholder']; |
| 181 | } |
| 182 | |
| 183 | $field = '<input type="text" name="'.esc_attr($item['name']).'" '.($item['required'] == 'required' ? 'required' : '').' placeholder="'.esc_attr($placeholder).'" class="%s" '.(isset($rule) ? $rule : '').' '.$error_label.'/>'; |
| 184 | |
| 185 | $this->{$this->template}($field, $item); |
| 186 | } |
| 187 | |
| 188 | function email($item){ |
| 189 | $error_label = $this->errorMsgLabel($item); |
| 190 | |
| 191 | if($item['required'] == 'required'){ |
| 192 | $placeholder = $item['placeholder'].' *'; |
| 193 | }else{ |
| 194 | $placeholder = $item['placeholder']; |
| 195 | } |
| 196 | |
| 197 | $field = '<input type="email" name="'.$item['name'].'" '.($item['required'] == 'required' ? 'required' : '').' placeholder="'.esc_attr($placeholder).'" class="%s" '.$error_label.'/>'; |
| 198 | |
| 199 | $this->{$this->template}($field, $item); |
| 200 | } |
| 201 | |
| 202 | function submit($item){ |
| 203 | $field = '<input type="submit" value="'.$item['value'].'" class="%s" />'; |
| 204 | |
| 205 | $this->{$this->template}($field, $item); |
| 206 | } |
| 207 | |
| 208 | function textarea($item){ |
| 209 | $error_label = $this->errorMsgLabel($item); |
| 210 | |
| 211 | if($item['required'] == 'required'){ |
| 212 | $placeholder = $item['placeholder'].' *'; |
| 213 | }else{ |
| 214 | $placeholder = $item['placeholder']; |
| 215 | } |
| 216 | |
| 217 | $field = '<textarea name="'.$item['name'].'" '.($item['required'] == 'required' ? 'required' : '').' placeholder="'.esc_attr($placeholder).'" class="%s" '.$error_label.'></textarea>'; |
| 218 | |
| 219 | $this->{$this->template}($field, $item); |
| 220 | } |
| 221 | |
| 222 | function template1($field, $item){ |
| 223 | $class_name = isset($item['type']) ? "type-".$item['type'] : "no-type"; |
| 224 | $id = isset($item['name']) ? 'field-container-'.$item['name'] : ""; |
| 225 | echo '<div class="pi-row '.esc_attr($class_name).'" id="'.esc_attr($id).'">'; |
| 226 | echo '<div class="pi-col-12">'; |
| 227 | if($item['type'] == 'submit'){ |
| 228 | printf($field, 'pi-btn pi-btn-primary pi-submit-enq-button'); |
| 229 | }else{ |
| 230 | printf($field, 'pi-form-control'); |
| 231 | } |
| 232 | echo '</div>'; |
| 233 | echo '</div>'; |
| 234 | } |
| 235 | |
| 236 | function honeypot($item){ |
| 237 | $field = '<span style="display:none; visibility:hidden;"><input type="text" name="'.esc_attr($item['name']).'" style="display:none;"/></span>'; |
| 238 | |
| 239 | echo $field; |
| 240 | } |
| 241 | |
| 242 | function captcha(){ |
| 243 | do_action('pi_eqw_add_captcha_field'); |
| 244 | } |
| 245 | } |