| 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 esc_html__( '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 |
if ( ! is_array( $classes ) ) { |
| 217 |
$classes = []; |
| 218 |
} |
| 219 |
|
| 220 |
foreach ( $defaults as $def ) { |
| 221 |
if ( ! in_array( $def, $classes, true ) ) { |
| 222 |
$new_classes[] = $def; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Assign w-100 class to those fields that have not width class. |
| 227 |
foreach ( $classes as $class ) { |
| 228 |
if ( 'form-row-wide' === $class ) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
if ( false !== strpos( $class, 'w-' ) ) { |
| 233 |
$is_width = false; |
| 234 |
} |
| 235 |
|
| 236 |
$new_classes[] = $class; |
| 237 |
} |
| 238 |
|
| 239 |
if ( true === $is_width ) { |
| 240 |
$new_classes[] = 'w-100'; |
| 241 |
} |
| 242 |
|
| 243 |
$args['class'] = $new_classes; |
| 244 |
|
| 245 |
return $args; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Gets fields default value from database if exists. |
| 250 |
* |
| 251 |
* @param array $args checkout fields options. |
| 252 |
* @param string $key checkout field key. |
| 253 |
* @since 1.1.0 |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
private function try_to_set_default_value( $args, $key ) { |
| 257 |
$user = get_current_user_id(); |
| 258 |
|
| 259 |
$default = get_user_meta( $user, $key, true ); |
| 260 |
|
| 261 |
if ( ! empty( $default ) ) { |
| 262 |
$args['default'] = $default; |
| 263 |
} |
| 264 |
|
| 265 |
return $args; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Customized html per field. |
| 270 |
* |
| 271 |
* @param string $field field html string. |
| 272 |
* @param array $args checkout fields options. |
| 273 |
* @param string $key key of field. |
| 274 |
* @return void |
| 275 |
* @since 1.1.0 |
| 276 |
*/ |
| 277 |
abstract public function field( $field, $args, $key ); |
| 278 |
|
| 279 |
/** |
| 280 |
* Field final html structure. |
| 281 |
* |
| 282 |
* @param string $field html string. |
| 283 |
* @param array $args checkout fields options. |
| 284 |
* @param string $key key of field. |
| 285 |
* @param string $value value of field. |
| 286 |
* @return void |
| 287 |
* @since 1.1.0 |
| 288 |
*/ |
| 289 |
public function final_html_structure( $field, $args, $key, $value = null ) { |
| 290 |
$args = $this->additional_class( $args, $key ); |
| 291 |
$args = $this->set_required_class( $args ); |
| 292 |
$args = $this->make_sure_sellkit_native_classes_exists( $args ); |
| 293 |
$args = $this->try_to_set_default_value( $args, $key ); |
| 294 |
$class = implode( ' ', $args['class'] ); |
| 295 |
|
| 296 |
if ( empty( $args['default'] ) && ! empty( $value ) ) { |
| 297 |
$args['default'] = $value; |
| 298 |
} |
| 299 |
|
| 300 |
// Remove all classes of fields. |
| 301 |
$field = preg_replace( '/class=".*?"/', '', $field, 1 ); |
| 302 |
// Remove fields label. |
| 303 |
$field = preg_replace( '~<label(.*?)</label>~Usi', '', $field ); |
| 304 |
|
| 305 |
?> |
| 306 |
<div class="<?php echo esc_attr( $class ); ?> sellkit-checkout-fields-wrapper" id="wrapper-<?php echo esc_attr( $key ); ?>"> |
| 307 |
<?php |
| 308 |
$this->label( $args ); |
| 309 |
$this->field( $field, $args, $key ); |
| 310 |
$this->required_validation( $args ); |
| 311 |
$this->global_errors( true ); |
| 312 |
?> |
| 313 |
</div> |
| 314 |
<?php |
| 315 |
} |
| 316 |
} |
| 317 |
|