PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.4.0
ShopBuilder – WooCommerce Builder For Elementor v2.4.0
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / CheckoutEditor / CheckoutEditorBase.php

CheckoutEditorBase.php in ShopBuilder – WooCommerce Builder For Elementor 2.4.0, at app/Modules/CheckoutEditor/CheckoutEditorBase.php

222 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Modules\CheckoutEditor;
4
5 // Do not allow directly accessing this file.
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit( 'This script cannot be accessed directly.' );
8 }
9
10 /**
11 * CheckoutEditorInit
12 */
13 class CheckoutEditorBase {
14
15 /**
16 * @var array
17 */
18 protected $cache = [];
19
20 /**
21 * Billing Fields Settings
22 */
23 protected $options = [];
24
25 /**
26 * Billing Fields Settings
27 */
28 protected $billing_settings = [];
29
30 /**
31 * All Custom Fields Here
32 */
33 protected $checkout_custom_fields = [];
34
35 /**
36 * Billing Custom Fields
37 */
38 protected $billing_custom_field = [];
39
40 /**
41 * Billing Custom Fields
42 */
43 protected $shipping_custom_field = [];
44
45 /**
46 * Shippping Field Settings
47 */
48 protected $shipping_settings = [];
49
50 /**
51 * Additional Field Settings
52 */
53 protected $additional_settings = [];
54 /**
55 * Billing Custom Fields
56 */
57 protected $additional_custom_field = [];
58
59 /**
60 * Notifications hooks.
61 */
62 public function __construct() {
63 global $checkout_editor_settings; // Define global variable.
64 $this->options = $checkout_editor_settings;
65 // If the cached result doesn't exist, fetch it from the database.
66 $this->billing_settings = $this->fields_generator( 'billing' );
67 $this->shipping_settings = $this->fields_generator( 'shipping' );
68 $this->additional_settings = $this->fields_generator( 'additional' );
69 }
70 /**
71 * @param array $settings Field generator settings.
72 * @param array $address_fields Previous Field.
73 * @return array
74 */
75 protected function render_fields( $settings, $address_fields ) {
76 $modified_fields = [];
77 if ( empty( $settings ) || empty( $address_fields ) ) {
78 return $address_fields;
79 }
80 $item = 1;
81 foreach ( $settings as $key => $field ) {
82 $field['priority'] = absint( $item ) * 10;
83 if ( ! empty( $address_fields[ $key ] ) ) {
84 $modified_fields[ $key ] = wp_parse_args( $field, $address_fields[ $key ] );
85 } else {
86 $modified_fields[ $key ] = $field;
87 }
88 $item++;
89 }
90 return $modified_fields;
91 }
92 /**
93 * Billing Custom Field.
94 *
95 * @return array|mixed
96 */
97 private function get_multiselect_fields_value( $data ) {
98 if ( empty( $data ) ) {
99 return [];
100 }
101 return array_column( $data, 'value' );
102 }
103 /**
104 * @param string $group group name.
105 * @return array
106 */
107 private function fields_generator( $group ) {
108 $cache_key = 'checkout_' . $group . '_fields';
109 if ( isset( $this->cache[ $cache_key ] ) ) {
110 return $this->cache[ $cache_key ];
111 }
112 $customize_fields = $this->options[ 'checkout_' . $group . '_fields' ] ?? [];
113 $fields = [];
114 if ( ! empty( $customize_fields ) ) {
115 $fields = json_decode( stripslashes( $customize_fields ), true );
116 }
117 $generated_fields = [];
118 $user = wp_get_current_user();
119 foreach ( $fields as $field ) {
120 if ( empty( $field['name'] ) || 'on' !== ( $field['isEnable'] ?? '' ) ) {
121 continue;
122 }
123 if ( rtsb()->has_pro() && 'specific' === ( $field['fieldVisibility'] ?? '' ) && ! empty( $field['fieldVisibilityRoles'] ) ) {
124 $roles = $this->get_multiselect_fields_value( $field['fieldVisibilityRoles'] );
125 if ( ! array_intersect( $roles, $user->roles ) ) {
126 continue;
127 }
128 }
129
130 $field['required'] = 'on' === ( $field['isRequired'] ?? '' );
131 $field['class'] = ! empty( $field['custom_class'] ) ? explode( ',', $field['custom_class'] ) : [];
132 $field['class'][] = 'rtsb-input-field';
133 if ( ! empty( $field['validation'] ) && 'default' !== $field['validation'] ) {
134 $field['validate'] = [ $field['validation'] ];
135 }
136 if ( 'phone' === $field['name'] ) {
137 $field['type'] = 'tel';
138 } elseif ( 'country' === $field['name'] ) {
139 $field['type'] = 'country';
140 $field['value'] = $field['default'] ?? null;
141 } elseif ( 'state' === $field['name'] ) {
142 $field['type'] = 'state';
143 } elseif ( 'email' === $field['name'] ) {
144 $field['type'] = 'email';
145 }
146
147 if ( rtsb()->has_pro() && 'custom_field' === $field['name'] ) {
148 $field_name = $group . '_' . ( $field['custom_key'] ?? 'custom_field_' . $field['id'] );
149 $selected_value = [];
150 if ( in_array( $field['type'], [ 'select', 'radio', 'checkboxgroup', 'multiselect' ], true ) ) {
151 $options = $field['options'] ?? [];
152 unset( $field['options'] );
153 $is_multiple = in_array( $field['type'], [ 'checkboxgroup', 'multiselect' ], true );
154 foreach ( $options as $option ) {
155 $field['options'][ $option['value'] ] = $option['label'];
156 if ( $is_multiple ) {
157 if ( 'on' === ( $option['isDefault'] ?? 'off' ) ) {
158 $selected_value[] = $option['value'];
159 }
160 } elseif ( empty( $selected_value ) ) {
161 $selected_value = 'on' === ( $option['isDefault'] ?? 'off' ) ? $option['value'] : '';
162 }
163 }
164 $field['default'] = $selected_value;
165 }
166 if ( in_array( $field['type'], [ 'checkbox' ], true ) ) {
167 $field['checked_value'] = $field['default'] ?? null;
168 $field['value'] = 'on' === $field['isChecked'] ? $field['checked_value'] : 'N/A';
169 if ( 'off' === $field['isChecked'] ) {
170 $field['default'] = 'N/A';
171 }
172 }
173 if ( in_array( $field['type'], [ 'heading' ], true ) ) {
174 unset(
175 $field['options'],
176 $field['isRequired'],
177 $field['required'],
178 );
179 }
180
181 if ( 'billing' === $group ) {
182 $this->billing_custom_field[ $field_name ] = $this->remeove_unused_attr( $field );
183 } elseif ( 'shipping' === $group ) {
184 $this->shipping_custom_field[ $field_name ] = $this->remeove_unused_attr( $field );
185 } elseif ( 'additional' === $group ) {
186 $this->additional_custom_field[ $field_name ] = $this->remeove_unused_attr( $field );
187 }
188 $this->checkout_custom_fields[ $field_name ] = $this->remeove_unused_attr( $field );
189
190 } else {
191 $prefix = 'additional' === $group ? 'order' : $group;
192 $field_name = $prefix . '_' . $field['name'];
193 unset(
194 $field['options']
195 );
196 }
197 $generated_fields[ $field_name ] = $this->remeove_unused_attr( $field );
198 }
199 $this->cache[ $cache_key ] = $generated_fields;
200 return $generated_fields;
201 }
202
203 /**
204 * Billing Custom Field.
205 *
206 * @return array|mixed
207 */
208 private function remeove_unused_attr( $field ) {
209 unset(
210 $field['isEnable'],
211 $field['id'],
212 $field['deletable'],
213 $field['validation'],
214 $field['custom_key'],
215 $field['custom_class'],
216 $field['isRequired'],
217 $field['name'],
218 );
219 return $field;
220 }
221 }
222