PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.7.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.7.2
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / elementor / modules / checkout / fields / base.php

base.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.7.2, at includes/elementor/modules/checkout/fields/base.php

308 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Elementor\Modules\Checkout\Fields;
4
5 defined( 'ABSPATH' ) || die();
6
7 /**
8 * Class base.
9 *
10 * @since 1.1.0
11 */
12 abstract class Base {
13 /**
14 * Type of supported fields.
15 *
16 * @return array
17 * @since 1.1.0
18 */
19 public function supported_fields() {
20 return [
21 'text',
22 'select',
23 'multiselect',
24 'checkbox',
25 'radio',
26 'textarea',
27 'tel',
28 'hidden',
29 'email',
30 ];
31 }
32
33 /**
34 * Return type of field.
35 *
36 * @return string
37 * @since 1.1.0
38 */
39 abstract public function type();
40
41 /**
42 * Type of fields that don't get / don't need placeholder as label.
43 *
44 * @return array
45 * @since 1.1.0
46 */
47 protected function field_without_placeholder() {
48 return [
49 'radio',
50 'checkbox',
51 ];
52 }
53
54 /**
55 * Print out field label using placeholder argument.
56 *
57 * @param array $args checkout fields options.
58 * @return void
59 * @since 1.1.0
60 */
61 protected function placeholder( $args ) {
62 if ( ! array_key_exists( 'label', $args ) ) {
63 return;
64 }
65
66 $label = $args['label'];
67
68 if ( $this->placeholder_required_value( $args ) && 'multiselect' === $args['type'] ) {
69 $label .= ' *';
70 }
71
72 ?>
73 <div style="position:relative">
74 <span class="mini-title">
75 <?php echo esc_html( $label ); ?>
76 </span>
77 </div>
78 <?php
79 }
80
81 /**
82 * Print out field label for those fields without placeholder attribute.
83 *
84 * @param array $args checkout fields options.
85 * @return void
86 * @since 1.1.0
87 */
88 protected function label( $args ) {
89 if ( ! in_array( $args['type'], $this->field_without_placeholder(), true ) ) {
90 $this->placeholder( $args );
91 return;
92 }
93
94 $label = $this->placeholder_required_value( $args );
95
96 ?>
97 <label class="free_label"><?php echo esc_html( $label ); ?></label>
98 <?php
99 }
100
101 /**
102 * Display error if required field is empty.
103 *
104 * @param array $args checkout fields options.
105 * @return void
106 * @since 1.1.0
107 */
108 protected function required_validation( $args ) {
109 if ( ! array_key_exists( 'required', $args ) || false === $args['required'] || 0 === $args['required'] ) {
110 return;
111 }
112
113 ?>
114 <div class="sellkit-required-validation">
115 <span class="required-alarm">
116 <?php echo __( 'This field is required.', 'sellkit' ); ?>
117 </span>
118 </div>
119 <?php
120 }
121
122 /**
123 * Empty element, is used to display postcode, phone etc validation errors.
124 *
125 * @param bool $enable enable or disable this element per field.
126 * @return void
127 * @since 1.1.0
128 */
129 protected function global_errors( $enable ) {
130 if ( false === $enable ) {
131 return;
132 }
133 ?>
134 <div
135 style="position:relative"
136 class="sellkit-checkout-widget-d-none sellkit-checkout-field-global-errors"
137 >
138 </div>
139 <?php
140 }
141
142
143 /**
144 * Adds additional class for fields if required
145 *
146 * @param array $args checkout fields options.
147 * @param string $key field key.
148 * @return array
149 * @since 1.1.0
150 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
151 */
152 protected function additional_class( $args, $key ) {
153 return $args;
154 }
155
156 /**
157 * Adds required validation class for fields.
158 *
159 * @param array $args checkout fields options.
160 * @return array
161 * @since 1.1.0
162 */
163 private function set_required_class( $args ) {
164 if ( array_key_exists( 'required', $args ) && ( true === $args['required'] || 1 === $args['required'] ) ) {
165 $args['class'][] = 'validate-required';
166 }
167
168 return $args;
169 }
170
171 /**
172 * Check if this field is required.
173 *
174 * @param array $args field arguments.
175 * @since 1.3.1
176 * @return boolean
177 */
178 protected function is_this_required( $args ) {
179 if ( array_key_exists( 'required', $args ) && ( true === $args['required'] || 1 === $args['required'] ) ) {
180 return true;
181 }
182
183 return false;
184 }
185
186 /**
187 * Prepare placeholder value and add * to it for required fields.
188 *
189 * @param array $args fields arguments.
190 * @since 1.3.1
191 * @return string
192 */
193 protected function placeholder_required_value( $args ) {
194 $placeholder = ( array_key_exists( 'label', $args ) ) ? $args['label'] : '';
195
196 if ( $this->is_this_required( $args ) ) {
197 $placeholder .= ' *';
198 }
199
200 return $placeholder;
201 }
202
203 /**
204 * We make sure our fields basic class exists in class array, in order to not have any style issue.
205 *
206 * @param array $args checkout field option.
207 * @since 1.1.0
208 * @return array
209 */
210 private function make_sure_sellkit_native_classes_exists( $args ) {
211 $classes = $args['class'];
212 $defaults = [ 'sellkit-widget-checkout-fields', 'sellkit-checkout-fields-wrapper' ];
213 $new_classes = [];
214 $is_width = true;
215
216 foreach ( $defaults as $def ) {
217 if ( ! in_array( $def, $classes, true ) ) {
218 $new_classes[] = $def;
219 }
220 }
221
222 // Assign w-100 class to those fields that have not width class.
223 foreach ( $classes as $class ) {
224 if ( 'form-row-wide' === $class ) {
225 continue;
226 }
227
228 if ( false !== strpos( $class, 'w-' ) ) {
229 $is_width = false;
230 }
231
232 $new_classes[] = $class;
233 }
234
235 if ( true === $is_width ) {
236 $new_classes[] = 'w-100';
237 }
238
239 $args['class'] = $new_classes;
240
241 return $args;
242 }
243
244 /**
245 * Gets fields default value from database if exists.
246 *
247 * @param array $args checkout fields options.
248 * @param string $key checkout field key.
249 * @since 1.1.0
250 * @return array
251 */
252 private function try_to_set_default_value( $args, $key ) {
253 $user = get_current_user_id();
254
255 $default = get_user_meta( $user, $key, true );
256
257 if ( ! empty( $default ) ) {
258 $args['default'] = $default;
259 }
260
261 return $args;
262 }
263
264 /**
265 * Customized html per field.
266 *
267 * @param string $field field html string.
268 * @param array $args checkout fields options.
269 * @param string $key key of field.
270 * @return void
271 * @since 1.1.0
272 */
273 abstract public function field( $field, $args, $key );
274
275 /**
276 * Field final html structure.
277 *
278 * @param string $field html string.
279 * @param array $args checkout fields options.
280 * @param string $key key of field.
281 * @return void
282 * @since 1.1.0
283 */
284 public function final_html_structure( $field, $args, $key ) {
285 $args = $this->additional_class( $args, $key );
286 $args = $this->set_required_class( $args );
287 $args = $this->make_sure_sellkit_native_classes_exists( $args );
288 $args = $this->try_to_set_default_value( $args, $key );
289 $class = implode( ' ', $args['class'] );
290
291 // Remove all classes of fields.
292 $field = preg_replace( '/class=".*?"/', '', $field, 1 );
293 // Remove fields label.
294 $field = preg_replace( '~<label(.*?)</label>~Usi', '', $field );
295
296 ?>
297 <div class="<?php echo $class; ?> sellkit-checkout-fields-wrapper" id="wrapper-<?php echo $key; ?>">
298 <?php
299 $this->label( $args );
300 $this->field( $field, $args, $key );
301 $this->required_validation( $args );
302 $this->global_errors( true );
303 ?>
304 </div>
305 <?php
306 }
307 }
308