PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.0
ShopBuilder – WooCommerce Builder For Elementor v3.2.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 3.2.0, at app/Modules/CheckoutEditor/CheckoutEditorBase.php

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