PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 / block-editor / blocks / checkout / form-fields / base.php

base.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/block-editor/blocks/checkout/form-fields/base.php

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