PluginProbe ʕ •ᴥ•ʔ
Checkout Field Editor (Checkout Manager) for WooCommerce / 1.4.8
Checkout Field Editor (Checkout Manager) for WooCommerce v1.4.8
1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 trunk 1.0.9 1.2.0 1.2.5 1.2.8 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8
woo-checkout-field-editor-pro / public / class-thwcfd-public-checkout.php
woo-checkout-field-editor-pro / public Last commit date
assets 4 years ago class-thwcfd-public-checkout.php 4 years ago
class-thwcfd-public-checkout.php
505 lines
1 <?php
2 /**
3 * Woo Checkout Field Editor Public
4 *
5 * @link https://themehigh.com
6 * @since 1.3.6
7 *
8 * @package woo-checkout-field-editor-pro
9 * @subpackage woo-checkout-field-editor-pro/public
10 */
11
12 defined( 'ABSPATH' ) || exit;
13
14 if(!class_exists('THWCFD_Public_Checkout')) :
15
16 class THWCFD_Public_Checkout {
17 public function __construct() {
18
19 }
20
21 public function enqueue_styles_and_scripts() {
22 if(is_checkout()){
23 $in_footer = apply_filters( 'thwcfd_enqueue_script_in_footer', true );
24 $deps = array('jquery', 'selectWoo');
25
26 $debug_mode = apply_filters('thwcfd_debug_mode', false);
27 $suffix = $debug_mode ? '' : '.min';
28 wp_register_script('thwcfd-checkout-script', THWCFD_ASSETS_URL_PUBLIC.'js/thwcfd-public' . $suffix . '.js', $deps, THWCFD_VERSION, $in_footer);
29 wp_enqueue_script('thwcfd-checkout-script');
30 wp_enqueue_style('thwcfd-checkout-style', THWCFD_ASSETS_URL_PUBLIC . 'css/thwcfd-public' . $suffix . '.css', THWCFD_VERSION);
31
32 $wcfd_var = array(
33 'is_override_required' => $this->is_override_required_prop(),
34 );
35 wp_localize_script('thwcfd-checkout-script', 'thwcfd_public_var', $wcfd_var);
36 }
37 }
38
39 public function define_public_hooks(){
40 $hp_billing_fields = apply_filters('thwcfd_billing_fields_priority', 1000);
41 $hp_shipping_fields = apply_filters('thwcfd_shipping_fields_priority', 1000);
42 $hp_checkout_fields = apply_filters('thwcfd_checkout_fields_priority', 1000);
43
44 add_filter('woocommerce_enable_order_notes_field', array($this, 'enable_order_notes_field'), 1000);
45
46 add_filter('woocommerce_get_country_locale_default', array($this, 'prepare_country_locale'));
47 add_filter('woocommerce_get_country_locale_base', array($this, 'prepare_country_locale'));
48 add_filter('woocommerce_get_country_locale', array($this, 'get_country_locale'));
49
50 add_filter('woocommerce_billing_fields', array($this, 'billing_fields'), $hp_billing_fields, 2);
51 add_filter('woocommerce_shipping_fields', array($this, 'shipping_fields'), $hp_shipping_fields, 2);
52 add_filter('woocommerce_checkout_fields', array($this, 'checkout_fields'), $hp_checkout_fields);
53
54 add_action('woocommerce_after_checkout_validation', array($this, 'checkout_fields_validation'), 10, 2);
55 add_action('woocommerce_checkout_update_order_meta', array($this, 'checkout_update_order_meta'), 10, 2);
56
57 add_filter('woocommerce_email_order_meta_fields', array($this, 'display_custom_fields_in_emails'), 10, 3);
58 add_action('woocommerce_order_details_after_order_table', array($this, 'order_details_after_customer_details'), 20, 1);
59 }
60
61 /**
62 * Hide Additional Fields title if no fields available.
63 */
64 public function enable_order_notes_field() {
65 $additional_fields = get_option('wc_fields_additional');
66 if(is_array($additional_fields)){
67 $enabled = 0;
68 foreach($additional_fields as $field){
69 if($field['enabled']){
70 $enabled++;
71 }
72 }
73 return $enabled > 0 ? true : false;
74 }
75 return true;
76 }
77
78 private function get_locale_override_value($key, $settings=false, $default=false){
79 $value = '';
80
81 if($settings){
82 $value = THWCFD_Utils::get_setting_value($settings, $key);
83 }else{
84 $value = THWCFD_Utils::get_settings($key);
85 }
86
87 return $value === 'undefined' ? $default : $value;
88 }
89
90 public function is_override_label($settings=false){
91 $override_label = $this->get_locale_override_value('enable_label_override', $settings, true);
92 $override_label = $override_label ? true : false;
93 return apply_filters('thwcfd_address_field_override_label', $override_label);
94 }
95
96 public function is_override_placeholder($settings=false){
97 $override_ph = $this->get_locale_override_value('enable_placeholder_override', $settings, true);
98 $override_ph = $override_ph ? true : false;
99 return apply_filters('thwcfd_address_field_override_placeholder', $override_ph);
100 }
101
102 public function is_override_class($settings=false){
103 $override_class = $this->get_locale_override_value('enable_class_override', $settings, false);
104 $override_class = $override_class ? true : false;
105 return apply_filters('thwcfd_address_field_override_class', $override_class);
106 }
107
108 public function is_override_priority($settings=false){
109 $override_priority = $this->get_locale_override_value('enable_priority_override', $settings, true);
110 $override_priority = $override_priority ? true : false;
111 return apply_filters('thwcfd_address_field_override_priority', $override_priority);
112 }
113
114 public function is_override_required_prop($settings=false){
115 $override_required = $this->get_locale_override_value('enable_required_override', $settings, false);
116 $override_required = $override_required ? true : false;
117 return apply_filters('thwcfd_address_field_override_required', $override_required);
118 }
119
120 public function prepare_country_locale($fields) {
121 if(is_array($fields)){
122 $settings = THWCFD_Utils::get_advanced_settings();
123
124 $override_label = $this->is_override_label($settings);
125 $override_ph = $this->is_override_placeholder($settings);
126 $override_class = $this->is_override_class($settings);
127 $override_priority = $this->is_override_priority($settings);
128
129 foreach($fields as $key => $props){
130 if($override_label && isset($props['label'])){
131 unset($fields[$key]['label']);
132 }
133
134 if($override_ph && isset($props['placeholder'])){
135 unset($fields[$key]['placeholder']);
136 }
137
138 if($override_class && isset($props['class'])){
139 unset($fields[$key]['class']);
140 }
141
142 if($override_priority && isset($props['priority'])){
143 unset($fields[$key]['priority']);
144 }
145 }
146 }
147 return $fields;
148 }
149
150 public function get_country_locale($locale) {
151 $countries = array_merge( WC()->countries->get_allowed_countries(), WC()->countries->get_shipping_countries() );
152 $countries = array_keys($countries);
153
154 if(is_array($locale) && is_array($countries)){
155 foreach($countries as $country){
156 if(isset($locale[$country])){
157 $locale[$country] = $this->prepare_country_locale($locale[$country]);
158 }
159 }
160 }
161 return $locale;
162 }
163
164 public function billing_fields($fields, $country){
165 if(is_wc_endpoint_url('edit-address')){
166 return $fields;
167 }else{
168 return $this->prepare_address_fields(get_option('wc_fields_billing'), $fields, 'billing', $country);
169 }
170 }
171
172 public function shipping_fields($fields, $country){
173 if(is_wc_endpoint_url('edit-address')){
174 return $fields;
175 }else{
176 return $this->prepare_address_fields(get_option('wc_fields_shipping'), $fields, 'shipping', $country);
177 }
178 }
179
180 public function checkout_fields($fields) {
181 $additional_fields = get_option('wc_fields_additional');
182
183 if(is_array($additional_fields)){
184 if(isset($fields['order']) && is_array($fields['order'])){
185 $fields['order'] = $additional_fields + $fields['order'];
186 }
187
188 // check if order_comments is enabled/disabled
189 if(isset($additional_fields['order_comments']['enabled']) && !$additional_fields['order_comments']['enabled']){
190 unset($fields['order']['order_comments']);
191 }
192 }
193
194 if(isset($fields['order']) && is_array($fields['order'])){
195 $fields['order'] = $this->prepare_checkout_fields($fields['order'], false);
196 }
197
198 if(isset($fields['order']) && !is_array($fields['order'])){
199 unset($fields['order']);
200 }
201
202 return $fields;
203 }
204
205 public function prepare_address_fields($fieldset, $original_fieldset = false, $sname = 'billing', $country){
206 if(is_array($fieldset) && !empty($fieldset)) {
207 $locale = WC()->countries->get_country_locale();
208
209 if(isset($locale[ $country ]) && is_array($locale[ $country ])) {
210 $override_required_prop = $this->is_override_required_prop();
211 $states = WC()->countries->get_states( $country );
212
213 foreach($locale[ $country ] as $key => $value){
214 $fname = $sname.'_'.$key;
215
216 if(is_array($value) && isset($fieldset[$fname])){
217 if(!$override_required_prop && isset($value['required'])){
218 $fieldset[$fname]['required'] = $value['required'];
219 }
220
221 if($key === 'state'){
222 if(is_array($states) && empty($states)){
223 $fieldset[$fname]['hidden'] = true;
224 }
225 }else{
226 if(isset($value['hidden'])){
227 $fieldset[$fname]['hidden'] = $value['hidden'];
228 }
229 }
230 }
231 }
232 }
233
234 $fieldset = $this->prepare_checkout_fields($fieldset, $original_fieldset);
235 return $fieldset;
236 }else {
237 return $original_fieldset;
238 }
239 }
240
241 public function prepare_checkout_fields($fields, $original_fields) {
242 if(is_array($fields) && !empty($fields)) {
243 $override_required_prop = $this->is_override_required_prop();
244
245 foreach($fields as $name => $field) {
246 if(THWCFD_Utils::is_enabled($field)) {
247 $new_field = false;
248 $allow_override = apply_filters('thwcfd_allow_default_field_override_'.$name, false);
249
250 if($original_fields && isset($original_fields[$name]) && !$allow_override){
251 $new_field = $original_fields[$name];
252
253 $class = isset($field['class']) && is_array($field['class']) ? $field['class'] : array();
254 $required = isset($field['required']) ? $field['required'] : 0;
255 $is_hidden = isset($field['hidden']) && $field['hidden'] ? true : false;
256
257 if($is_hidden){
258 $new_field['hidden'] = $field['hidden'];
259 $new_field['required'] = false;
260 }else{
261 if($override_required_prop){
262 $new_field['required'] = $required;
263 }
264 }
265
266 if($override_required_prop){
267 if($required){
268 $class[] = 'thwcfd-required';
269 }else{
270 $class[] = 'thwcfd-optional';
271 }
272 }
273
274 $new_field['label'] = isset($field['label']) ? $field['label'] : '';
275 $new_field['default'] = isset($field['default']) ? $field['default'] : '';
276 $new_field['placeholder'] = isset($field['placeholder']) ? $field['placeholder'] : '';
277 $new_field['class'] = $class;
278 $new_field['label_class'] = isset($field['label_class']) && is_array($field['label_class']) ? $field['label_class'] : array();
279 $new_field['validate'] = isset($field['validate']) && is_array($field['validate']) ? $field['validate'] : array();
280 $new_field['priority'] = isset($field['priority']) ? $field['priority'] : '';
281 } else {
282 $new_field = $field;
283 }
284
285 $type = isset($new_field['type']) ? $new_field['type'] : 'text';
286
287 $new_field['class'][] = 'thwcfd-field-wrapper';
288 $new_field['class'][] = 'thwcfd-field-'.$type;
289
290 if($type === 'select' || $type === 'radio'){
291 if(isset($new_field['options'])){
292 $options_arr = THWCFD_Utils::prepare_field_options($new_field['options']);
293 $options = array();
294 foreach($options_arr as $key => $value) {
295 $options[$key] = __($value, 'woo-checkout-field-editor-pro');
296 }
297 $new_field['options'] = $options;
298 }
299 }
300
301 if($type === 'select' && apply_filters('thwcfd_enable_select2_for_select_fields', true)){
302 $new_field['input_class'][] = 'thwcfd-enhanced-select';
303 }
304
305 if(isset($new_field['label'])){
306 $new_field['label'] = THWCFD_Utils::t($new_field['label']);
307 }
308
309 if(isset($new_field['placeholder'])){
310 $new_field['placeholder'] = THWCFD_Utils::t($new_field['placeholder']);
311 }
312
313 $fields[$name] = $new_field;
314 }else{
315 unset($fields[$name]);
316 }
317 }
318 return $fields;
319 }else {
320 return $original_fields;
321 }
322 }
323
324 /*************************************
325 ----- Validate & Update - START ------
326 *************************************/
327 public function checkout_fields_validation($posted, $errors){
328 $checkout_fields = WC()->checkout->checkout_fields;
329
330 foreach($checkout_fields as $fieldset_key => $fieldset){
331 if($this->maybe_skip_fieldset($fieldset_key, $posted)){
332 continue;
333 }
334
335 foreach($fieldset as $key => $field) {
336 if(isset($posted[$key]) && !THWCFD_Utils::is_blank($posted[$key])){
337 $this->validate_custom_field($field, $posted, $errors);
338 }
339 }
340 }
341 }
342
343 public function validate_custom_field($field, $posted, $errors=false, $return=false){
344 $err_msgs = array();
345 $key = isset($field['name']) ? $field['name'] : false;
346
347 if($key){
348 $value = isset($posted[$key]) ? $posted[$key] : '';
349 $validators = isset($field['validate']) ? $field['validate'] : '';
350
351 if($value && is_array($validators) && !empty($validators)){
352 foreach($validators as $vname){
353 $err_msg = '';
354 $flabel = isset($field['label']) ? THWCFD_Utils::t($field['label']) : $key;
355
356 if($vname === 'number'){
357 if(!is_numeric($value)){
358 $err_msg = '<strong>'. $flabel .'</strong> '. THWCFD_Utils::t('is not a valid number.');
359 }
360 }
361
362 if($err_msg){
363 if($errors || !$return){
364 $this->add_validation_error($err_msg, $errors);
365 }
366 $err_msgs[] = $err_msg;
367 }
368 }
369 }
370 }
371 return !empty($err_msgs) ? $err_msgs : false;
372 }
373
374 public function add_validation_error($msg, $errors=false){
375 if($errors){
376 $errors->add('validation', $msg);
377 }else if(THWCFD_Utils::woo_version_check('2.3.0')){
378 wc_add_notice($msg, 'error');
379 } else {
380 WC()->add_error($msg);
381 }
382 }
383
384 public function checkout_update_order_meta($order_id, $posted){
385 $types = array('billing', 'shipping', 'additional');
386
387 foreach($types as $type){
388 if($this->maybe_skip_fieldset($type, $posted)){
389 continue;
390 }
391
392 $fields = THWCFD_Utils::get_fields($type);
393
394 foreach($fields as $name => $field){
395 if(THWCFD_Utils::is_active_custom_field($field) && isset($posted[$name])){
396 $value = wc_clean($posted[$name]);
397 if($value){
398 update_post_meta($order_id, $name, $value);
399 }
400 }
401 }
402 }
403 }
404
405 private function maybe_skip_fieldset( $fieldset_key, $data ) {
406 $ship_to_different_address = isset($data['ship_to_different_address']) ? $data['ship_to_different_address'] : false;
407 $ship_to_destination = get_option( 'woocommerce_ship_to_destination' );
408
409 if ( 'shipping' === $fieldset_key && ( ! $ship_to_different_address || ! WC()->cart->needs_shipping_address() ) ) {
410 return $ship_to_destination != 'billing_only' ? true : false;
411 }
412 return false;
413 }
414
415 /****************************************
416 ----- Display Field Values - START ------
417 *****************************************/
418 /**
419 * Display custom fields in emails
420 */
421 public function display_custom_fields_in_emails($ofields, $sent_to_admin, $order){
422 $custom_fields = array();
423 $fields = THWCFD_Utils::get_checkout_fields();
424
425 // Loop through all custom fields to see if it should be added
426 foreach( $fields as $key => $field ) {
427 if(isset($field['show_in_email']) && $field['show_in_email']){
428 $order_id = THWCFD_Utils::get_order_id($order);
429 $value = get_post_meta( $order_id, $key, true );
430
431 if($value){
432 $label = isset($field['label']) && $field['label'] ? $field['label'] : $key;
433 $label = esc_attr($label);
434 $value = THWCFD_Utils::get_option_text($field, $value);
435
436 $custom_field = array();
437 $custom_field['label'] = THWCFD_Utils::t($label);
438 $custom_field['value'] = $value;
439
440 $custom_fields[$key] = $custom_field;
441 }
442 }
443 }
444
445 return array_merge($ofields, $custom_fields);
446 }
447
448 /**
449 * Display custom checkout fields on view order pages
450 */
451 public function order_details_after_customer_details($order){
452 $order_id = THWCFD_Utils::get_order_id($order);
453 $fields = THWCFD_Utils::get_checkout_fields($order);
454
455 if(is_array($fields) && !empty($fields)){
456 $fields_html = '';
457 // Loop through all custom fields to see if it should be added
458 foreach($fields as $key => $field){
459 if(THWCFD_Utils::is_active_custom_field($field) && isset($field['show_in_order']) && $field['show_in_order']){
460 $value = get_post_meta( $order_id, $key, true );
461
462 if($value){
463 $label = isset($field['label']) && $field['label'] ? THWCFD_Utils::t($field['label']) : $key;
464
465 $label = esc_attr($label);
466 //$value = wptexturize($value);
467 $value = THWCFD_Utils::get_option_text($field, $value);
468
469 if(is_account_page()){
470 if(apply_filters( 'thwcfd_view_order_customer_details_table_view', true )){
471 $fields_html .= '<tr><th>'. $label .':</th><td>'. $value .'</td></tr>';
472 }else{
473 $fields_html .= '<br/><dt>'. $label .':</dt><dd>'. $value .'</dd>';
474 }
475 }else{
476 if(apply_filters( 'thwcfd_thankyou_customer_details_table_view', true )){
477 $fields_html .= '<tr><th>'. $label .':</th><td>'. $value .'</td></tr>';
478 }else{
479 $fields_html .= '<br/><dt>'. $label .':</dt><dd>'. $value .'</dd>';
480 }
481 }
482 }
483 }
484 }
485
486 if($fields_html){
487 do_action( 'thwcfd_order_details_before_custom_fields_table', $order );
488 ?>
489 <table class="woocommerce-table woocommerce-table--custom-fields shop_table custom-fields">
490 <?php
491 echo $fields_html;
492 ?>
493 </table>
494 <?php
495 do_action( 'thwcfd_order_details_after_custom_fields_table', $order );
496 }
497 }
498 }
499 /*****************************************
500 ----- Display Field Values - END --------
501 *****************************************/
502 }
503
504 endif;
505