| 1 |
<?php |
| 2 |
defined('ABSPATH') || exit; |
| 3 |
|
| 4 |
class Yatra_Forms |
| 5 |
{ |
| 6 |
private static $instance; |
| 7 |
|
| 8 |
|
| 9 |
public static function get_instance() |
| 10 |
{ |
| 11 |
if (empty(self::$instance)) { |
| 12 |
|
| 13 |
self::$instance = new self(); |
| 14 |
} |
| 15 |
return self::$instance; |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
public function chekcout_form_fields() |
| 20 |
{ |
| 21 |
$country_list = yatra_get_countries(); |
| 22 |
$countries = array_merge( |
| 23 |
array( |
| 24 |
'0' => __('Select Country', 'yatra') |
| 25 |
), |
| 26 |
$country_list |
| 27 |
); |
| 28 |
|
| 29 |
|
| 30 |
$form_fields = apply_filters('tour_checkout_form_fields', array( |
| 31 |
'fullname' => array( |
| 32 |
'name' => 'fullname', |
| 33 |
'title' => __('Your full name', 'yatra'), |
| 34 |
'type' => 'text', |
| 35 |
'value' => '', |
| 36 |
'wrap_class' => 'yatra-left', |
| 37 |
'extra_attributes' => array( |
| 38 |
'placeholder' => __('Your full name', 'yatra'), |
| 39 |
'required' => 'required' |
| 40 |
), |
| 41 |
'group_id' => 'yatra_tour_customer_info', |
| 42 |
'row_start' => true, |
| 43 |
), 'email' => array( |
| 44 |
'name' => 'email', |
| 45 |
'title' => __('Email', 'yatra'), |
| 46 |
'type' => 'email', |
| 47 |
'value' => '', |
| 48 |
'group_id' => 'yatra_tour_customer_info', |
| 49 |
'wrap_class' => 'yatra-left', |
| 50 |
'extra_attributes' => array( |
| 51 |
'placeholder' => __('Email address', 'yatra'), |
| 52 |
'required' => 'required' |
| 53 |
), |
| 54 |
'row_start' => true, |
| 55 |
), 'country' => array( |
| 56 |
'name' => 'country', |
| 57 |
'title' => __('Country', 'yatra'), |
| 58 |
'type' => 'select', |
| 59 |
'group_id' => 'yatra_tour_customer_info', |
| 60 |
'options' => $countries, |
| 61 |
'wrap_class' => 'yatra-left', |
| 62 |
'row_start' => true, |
| 63 |
'select2' => true |
| 64 |
), 'phone_number' => array( |
| 65 |
'name' => 'phone_number', |
| 66 |
'title' => __('Phone Number', 'yatra'), |
| 67 |
'type' => 'text', |
| 68 |
'group_id' => 'yatra_tour_customer_info', |
| 69 |
'value' => '', |
| 70 |
'wrap_class' => 'yatra-left', |
| 71 |
'extra_attributes' => array( |
| 72 |
'placeholder' => __('Your contact number', 'yatra'), |
| 73 |
), |
| 74 |
'row_start' => true, |
| 75 |
) |
| 76 |
) |
| 77 |
); |
| 78 |
return $form_fields; |
| 79 |
} |
| 80 |
|
| 81 |
public function get_valid_form_data($data = array()) |
| 82 |
{ |
| 83 |
$form_fields_all = $this->chekcout_form_fields(); |
| 84 |
|
| 85 |
$valid_data = array(); |
| 86 |
|
| 87 |
foreach ($form_fields_all as $field => $field_option) { |
| 88 |
|
| 89 |
if (isset($form_fields_all[$field]['group_id'])) { |
| 90 |
|
| 91 |
if (isset($data[$form_fields_all[$field]['group_id']][$field])) { |
| 92 |
|
| 93 |
$valid_data[$form_fields_all[$field]['group_id']][$field] = $this->sanitization($data[$form_fields_all[$field]['group_id']][$field], $field_option); |
| 94 |
} |
| 95 |
|
| 96 |
} else { |
| 97 |
|
| 98 |
if (isset($data[$field])) { |
| 99 |
|
| 100 |
$valid_data[$field] = $this->sanitization($data[$field], $field_option); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
return $valid_data; |
| 106 |
} |
| 107 |
|
| 108 |
public function sanitization($field_value, $field_option) |
| 109 |
{ |
| 110 |
$type = isset($field_option['type']) ? $field_option['type'] : ''; |
| 111 |
if (empty($type)) { |
| 112 |
return ''; |
| 113 |
} |
| 114 |
$updated_value = ''; |
| 115 |
|
| 116 |
switch ($type) { |
| 117 |
// Allow only integers in number fields |
| 118 |
case 'number': |
| 119 |
$updated_value = absint($field_value); |
| 120 |
break; |
| 121 |
|
| 122 |
case 'text': |
| 123 |
$updated_value = sanitize_text_field($field_value); |
| 124 |
break; |
| 125 |
|
| 126 |
case 'email': |
| 127 |
$updated_value = sanitize_email($field_value); |
| 128 |
break; |
| 129 |
|
| 130 |
// Allow some tags in textareas |
| 131 |
case 'textarea': |
| 132 |
|
| 133 |
if (isset($field_option['allowed_tags'])) { |
| 134 |
|
| 135 |
$allowed_tags = $field_option['allowed_tags']; |
| 136 |
|
| 137 |
} else { |
| 138 |
|
| 139 |
$allowed_tags = array( |
| 140 |
'p' => array(), |
| 141 |
'em' => array(), |
| 142 |
'strong' => array(), |
| 143 |
'img' => array( |
| 144 |
'src' => array() |
| 145 |
), |
| 146 |
'ul' => array(), |
| 147 |
'ol' => array(), |
| 148 |
'li' => array(), |
| 149 |
'a' => array( |
| 150 |
'href' => array(), |
| 151 |
), |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
$updated_value = wp_kses($field_value, $allowed_tags); |
| 156 |
|
| 157 |
|
| 158 |
break; |
| 159 |
// No allowed tags for all other fields |
| 160 |
case 'url': |
| 161 |
$updated_value = esc_url_raw($field_value); |
| 162 |
break; |
| 163 |
case 'select': |
| 164 |
$is_multiple = isset($meta_field['is_multiple']) && (boolean)$field_option['is_multiple'] ? true : false; |
| 165 |
|
| 166 |
if ($is_multiple) { |
| 167 |
$field_value = empty($field_value) ? array() : $field_value; |
| 168 |
$array = array_map('sanitize_text_field', wp_unslash($field_value)); |
| 169 |
|
| 170 |
$updated_value = array_map('wp_kses_post', $array); |
| 171 |
|
| 172 |
} else { |
| 173 |
|
| 174 |
$updated_value = wp_kses_post(sanitize_text_field($field_value)); |
| 175 |
} |
| 176 |
break; |
| 177 |
default: |
| 178 |
$updated_value = wp_kses_post(sanitize_text_field($field_value)); |
| 179 |
break; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
return $updated_value; |
| 185 |
} |
| 186 |
|
| 187 |
public function tour_checkout_form() |
| 188 |
{ |
| 189 |
|
| 190 |
$form_fields = $this->chekcout_form_fields(); |
| 191 |
|
| 192 |
foreach ($form_fields as $field) { |
| 193 |
|
| 194 |
$this->form_html($field); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
private function form_html($field = array()) |
| 199 |
{ |
| 200 |
$extra_attributes = array(); |
| 201 |
|
| 202 |
if (!isset($field['name']) || !isset($field['type'])) { |
| 203 |
return; |
| 204 |
} |
| 205 |
if (empty($field['name'])) { |
| 206 |
|
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$field_key = $field['name']; |
| 211 |
|
| 212 |
$value = isset($field['value']) ? $field['value'] : ''; |
| 213 |
|
| 214 |
$extra_attributes = isset($field['extra_attributes']) ? $field['extra_attributes'] : array(); |
| 215 |
|
| 216 |
$extra_attribute_text = ''; |
| 217 |
|
| 218 |
foreach ($extra_attributes as $attribute_key => $attribute_value) { |
| 219 |
|
| 220 |
$extra_attribute_text .= ' ' . esc_html($attribute_key) . '="' . esc_attr($attribute_value) . '"'; |
| 221 |
} |
| 222 |
|
| 223 |
$field_type = $field['type']; |
| 224 |
|
| 225 |
$wrap_class = isset($field['wrap_class']) ? $field['wrap_class'] : ''; |
| 226 |
|
| 227 |
$row_start = isset($field['row_start']) ? (boolean)$field['row_start'] : false; |
| 228 |
|
| 229 |
if ($row_start) { |
| 230 |
|
| 231 |
echo '<div class="yatra-field-row">'; |
| 232 |
} |
| 233 |
|
| 234 |
$name = $field_key; |
| 235 |
|
| 236 |
if (isset($field['group_id'])) { |
| 237 |
|
| 238 |
$name = $field['group_id'] . '[' . $field_key . ']'; |
| 239 |
} |
| 240 |
echo '<div class="yatra-field-wrap ' . esc_attr($wrap_class) . '">'; |
| 241 |
|
| 242 |
switch ($field['type']) { |
| 243 |
case "text": |
| 244 |
case "number": |
| 245 |
case "hidden": |
| 246 |
case "email": |
| 247 |
if ($field['type'] != "hidden") { |
| 248 |
?> |
| 249 |
<p> |
| 250 |
<label |
| 251 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 252 |
:</label> |
| 253 |
<?php } |
| 254 |
?> |
| 255 |
<input class="yatra_field" |
| 256 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 257 |
name="<?php echo esc_attr(($name)); ?>" |
| 258 |
type="<?php echo esc_attr($field_type) ?>" |
| 259 |
value="<?php echo esc_attr($value); ?>" <?php echo $extra_attribute_text; ?>/> |
| 260 |
<?php if ($field['type'] != "hidden") { |
| 261 |
?> |
| 262 |
</p> |
| 263 |
<?php |
| 264 |
} |
| 265 |
break; |
| 266 |
case "textarea": |
| 267 |
|
| 268 |
$editor = isset($field['editor']) ? (boolean)$field['editor'] : false; |
| 269 |
|
| 270 |
$editor_settings = isset($field['editor_settings']) ? $field['editor_settings'] : array(); |
| 271 |
|
| 272 |
$editor_height = isset($editor_settings['editor_height']) ? (int)$field['editor_height'] : 350; |
| 273 |
|
| 274 |
$editor_default_settings = array( |
| 275 |
'textarea_name' => $field_key, |
| 276 |
'tinymce' => array( |
| 277 |
'init_instance_callback ' => 'function(inst) { |
| 278 |
$("#" + inst.id + "_ifr").css({minHeight: "' . $editor_height . 'px"}); |
| 279 |
}' |
| 280 |
), |
| 281 |
|
| 282 |
|
| 283 |
); |
| 284 |
|
| 285 |
|
| 286 |
$editor_settings = wp_parse_args($editor_default_settings, $editor_settings); |
| 287 |
|
| 288 |
?> |
| 289 |
<p> |
| 290 |
<label |
| 291 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 292 |
:</label> |
| 293 |
<?php |
| 294 |
if ($editor) { |
| 295 |
echo '</p>'; |
| 296 |
wp_editor($value, $field_key, $editor_settings); |
| 297 |
} else { |
| 298 |
?> |
| 299 |
<textarea class="yatra_field" |
| 300 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 301 |
name="<?php echo esc_attr(($field_key)); ?>" |
| 302 |
<?php echo $extra_attribute_text; ?> |
| 303 |
|
| 304 |
><?php echo esc_html($value); ?></textarea> |
| 305 |
|
| 306 |
|
| 307 |
</p> |
| 308 |
<?php } |
| 309 |
break; |
| 310 |
case "select": |
| 311 |
?> |
| 312 |
<p> |
| 313 |
<label |
| 314 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 315 |
:</label> |
| 316 |
<?php |
| 317 |
$options = isset($field['options']) ? $field['options'] : array(); |
| 318 |
$is_multi_select = isset($field['is_multiple']) ? (boolean)$field['is_multiple'] : false; |
| 319 |
$is_select2 = isset($field['select2']) ? (boolean)$field['select2'] : false; |
| 320 |
if ($is_multi_select) { |
| 321 |
$extra_attribute_text .= ' multiple="multiple"'; |
| 322 |
} |
| 323 |
$select_class = 'yatra_field'; |
| 324 |
$select_class .= $is_select2 ? ' yatra-select2' : ''; |
| 325 |
?> |
| 326 |
|
| 327 |
<select class="<?php echo esc_attr($select_class); ?>" |
| 328 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 329 |
name="<?php echo esc_attr(($name)); |
| 330 |
echo $is_multi_select ? '[]' : ''; ?>" |
| 331 |
<?php echo $extra_attribute_text; ?>> |
| 332 |
<?php foreach ($options as $option_key => $option_value) { |
| 333 |
|
| 334 |
if (!$is_multi_select) { |
| 335 |
if (is_array($value)) { |
| 336 |
$value = $value[0]; |
| 337 |
} |
| 338 |
$selected = $option_key == $value ? true : false; |
| 339 |
} else { |
| 340 |
if (!is_array($value)) { |
| 341 |
$value = array($value); |
| 342 |
} |
| 343 |
$selected = in_array($option_key, $value) ? true : false; |
| 344 |
} |
| 345 |
|
| 346 |
?> |
| 347 |
<option <?php echo $selected ? 'selected="selected"' : ''; ?> |
| 348 |
value="<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value) ?></option><?php |
| 349 |
} |
| 350 |
?> |
| 351 |
</select> |
| 352 |
|
| 353 |
|
| 354 |
</p> |
| 355 |
<?php |
| 356 |
break; |
| 357 |
case "image": |
| 358 |
?> |
| 359 |
<p><label |
| 360 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 361 |
:</label> |
| 362 |
<div class="media-uploader" id="<?php echo('background_image'); ?>"> |
| 363 |
<div class="custom_media_preview"> |
| 364 |
<img style="<?php echo empty($value) ? 'display:none;' : '' ?>max-width:100%;" |
| 365 |
class="media_preview_image" |
| 366 |
src="<?php echo esc_url($value); ?>" alt=""/> |
| 367 |
|
| 368 |
<input class="yatra_field custom_media_input" type="hidden" |
| 369 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 370 |
name="<?php echo esc_attr(($name)); ?>" |
| 371 |
<?php echo $extra_attribute_text; ?> |
| 372 |
type="text" value="<?php echo esc_html($value); ?>"/> |
| 373 |
<button class="media_upload button" |
| 374 |
id="<?php echo('background_image'); ?>" |
| 375 |
data-choose="<?php esc_attr_e('Choose an image', 'yatra'); ?>" |
| 376 |
data-update="<?php esc_attr_e('Use image', 'yatra'); ?>" |
| 377 |
style="width:100%;margin-top:6px;margin-right:30px;"><?php esc_html_e('Select an Image', 'yatra'); ?></button> |
| 378 |
</div> |
| 379 |
|
| 380 |
</div> |
| 381 |
</p> |
| 382 |
<?php |
| 383 |
break; |
| 384 |
|
| 385 |
} |
| 386 |
echo "</div>"; |
| 387 |
|
| 388 |
$row_end = isset($field['row_end']) ? (boolean)$field['row_end'] : false; |
| 389 |
|
| 390 |
if ($row_end) { |
| 391 |
|
| 392 |
echo '</div>'; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|