PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.9
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.9
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.2.9, at includes/elementor/modules/checkout/fields/base.php

267 lines 5.7 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 <div style="position:relative">
67 <span class="mini-title">
68 <?php echo $args['label']; ?>
69 </span>
70 </div>
71 <?php
72 }
73
74 /**
75 * Print out field label for those fields without placeholder attribute.
76 *
77 * @param array $args checkout fields options.
78 * @return void
79 * @since 1.1.0
80 */
81 protected function label( $args ) {
82 if ( ! in_array( $args['type'], $this->field_without_placeholder(), true ) ) {
83 $this->placeholder( $args );
84 return;
85 }
86
87 ?>
88 <label class="free_label"><?php echo $args['label']; ?></label>
89 <?php
90 }
91
92 /**
93 * Display error if required field is empty.
94 *
95 * @param array $args checkout fields options.
96 * @return void
97 * @since 1.1.0
98 */
99 protected function required_validation( $args ) {
100 if ( ! array_key_exists( 'required', $args ) || false === $args['required'] || 0 === $args['required'] ) {
101 return;
102 }
103
104 ?>
105 <div class="sellkit-required-validation">
106 <span class="required-alarm">
107 <?php echo __( 'This field is required.', 'sellkit' ); ?>
108 </span>
109 </div>
110 <?php
111 }
112
113 /**
114 * Empty element, is used to display postcode, phone etc validation errors.
115 *
116 * @param bool $enable enable or disable this element per field.
117 * @return void
118 * @since 1.1.0
119 */
120 protected function global_errors( $enable ) {
121 if ( false === $enable ) {
122 return;
123 }
124 ?>
125 <div
126 style="position:relative"
127 class="sellkit-checkout-widget-d-none sellkit-checkout-field-global-errors"
128 >
129 </div>
130 <?php
131 }
132
133
134 /**
135 * Adds additional class for fields if required
136 *
137 * @param array $args checkout fields options.
138 * @param string $key field key.
139 * @return array
140 * @since 1.1.0
141 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
142 */
143 protected function additional_class( $args, $key ) {
144 return $args;
145 }
146
147 /**
148 * Adds required validation class for fields.
149 *
150 * @param array $args checkout fields options.
151 * @return array
152 * @since 1.1.0
153 */
154 private function set_required_class( $args ) {
155 if ( array_key_exists( 'required', $args ) && ( true === $args['required'] || 1 === $args['required'] ) ) {
156 $args['class'][] = 'validate-required';
157 }
158
159 return $args;
160 }
161
162 /**
163 * We make sure our fields basic class exists in class array, in order to not have any style issue.
164 *
165 * @param array $args checkout field option.
166 * @since 1.1.0
167 * @return array
168 */
169 private function make_sure_sellkit_native_classes_exists( $args ) {
170 $classes = $args['class'];
171 $defaults = [ 'sellkit-widget-checkout-fields', 'sellkit-checkout-fields-wrapper' ];
172 $new_classes = [];
173 $is_width = true;
174
175 foreach ( $defaults as $def ) {
176 if ( ! in_array( $def, $classes, true ) ) {
177 $new_classes[] = $def;
178 }
179 }
180
181 // Assign w-100 class to those fields that have not width class.
182 foreach ( $classes as $class ) {
183 if ( 'form-row-wide' === $class ) {
184 continue;
185 }
186
187 if ( false !== strpos( $class, 'w-' ) ) {
188 $is_width = false;
189 }
190
191 $new_classes[] = $class;
192 }
193
194 if ( true === $is_width ) {
195 $new_classes[] = 'w-100';
196 }
197
198 $args['class'] = $new_classes;
199
200 return $args;
201 }
202
203 /**
204 * Gets fields default value from database if exists.
205 *
206 * @param array $args checkout fields options.
207 * @param string $key checkout field key.
208 * @since 1.1.0
209 * @return array
210 */
211 private function try_to_set_default_value( $args, $key ) {
212 $user = get_current_user_id();
213
214 $default = get_user_meta( $user, $key, true );
215
216 if ( ! empty( $default ) ) {
217 $args['default'] = $default;
218 }
219
220 return $args;
221 }
222
223 /**
224 * Customized html per field.
225 *
226 * @param string $field field html string.
227 * @param array $args checkout fields options.
228 * @param string $key key of field.
229 * @return void
230 * @since 1.1.0
231 */
232 abstract public function field( $field, $args, $key );
233
234 /**
235 * Field final html structure.
236 *
237 * @param string $field html string.
238 * @param array $args checkout fields options.
239 * @param string $key key of field.
240 * @return void
241 * @since 1.1.0
242 */
243 public function final_html_structure( $field, $args, $key ) {
244 $args = $this->additional_class( $args, $key );
245 $args = $this->set_required_class( $args );
246 $args = $this->make_sure_sellkit_native_classes_exists( $args );
247 $args = $this->try_to_set_default_value( $args, $key );
248 $class = implode( ' ', $args['class'] );
249
250 // Remove all classes of fields.
251 $field = preg_replace( '/class=".*?"/', '', $field, 1 );
252 // Remove fields label.
253 $field = preg_replace( '~<label(.*?)</label>~Usi', '', $field );
254
255 ?>
256 <div class="<?php echo $class; ?> sellkit-checkout-fields-wrapper" id="wrapper-<?php echo $key; ?>">
257 <?php
258 $this->label( $args );
259 $this->field( $field, $args, $key );
260 $this->required_validation( $args );
261 $this->global_errors( true );
262 ?>
263 </div>
264 <?php
265 }
266 }
267