| 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 tour_checkout_data() |
| 20 |
{ |
| 21 |
$tour_cart = yatra_get_session('tour_cart'); |
| 22 |
|
| 23 |
return $tour_cart; |
| 24 |
} |
| 25 |
|
| 26 |
public function chekcout_form_fields() |
| 27 |
{ |
| 28 |
$tour_cart_data = $this->tour_checkout_data(); |
| 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' => 'tour_meta', |
| 42 |
'row_start' => true, |
| 43 |
), 'email' => array( |
| 44 |
'name' => 'email', |
| 45 |
'title' => __('Email', 'yatra'), |
| 46 |
'type' => 'email', |
| 47 |
'value' => '', |
| 48 |
'group_id' => 'tour_meta', |
| 49 |
'wrap_class' => 'yatra-left', |
| 50 |
'extra_attributes' => array( |
| 51 |
'placeholder' => __('Email address', 'yatra'), |
| 52 |
'required' => 'required' |
| 53 |
), |
| 54 |
'row_start' => true, |
| 55 |
), 'tour_package' => array( |
| 56 |
'name' => 'tour_package', |
| 57 |
'title' => __('Tour Package', 'yatra'), |
| 58 |
'type' => 'text', |
| 59 |
'group_id' => 'tour_meta', |
| 60 |
'value' => isset($tour_cart_data->post_title) ? $tour_cart_data->post_title . ' (' . $tour_cart_data->ID . ') ' : '', |
| 61 |
'wrap_class' => 'yatra-left', |
| 62 |
'extra_attributes' => array( |
| 63 |
'placeholder' => __('Your full name', 'yatra'), |
| 64 |
'readonly' => 'readonly', |
| 65 |
'required' => 'required' |
| 66 |
), |
| 67 |
'row_start' => true, |
| 68 |
), 'phone_number' => array( |
| 69 |
'name' => 'phone_number', |
| 70 |
'title' => __('Phone Number', 'yatra'), |
| 71 |
'type' => 'text', |
| 72 |
'group_id' => 'tour_meta', |
| 73 |
'value' => '', |
| 74 |
'wrap_class' => 'yatra-left', |
| 75 |
'extra_attributes' => array( |
| 76 |
'placeholder' => __('Your contact number', 'yatra'), |
| 77 |
), |
| 78 |
'row_start' => true, |
| 79 |
) |
| 80 |
) |
| 81 |
); |
| 82 |
return $form_fields; |
| 83 |
} |
| 84 |
|
| 85 |
public function get_valid_form_data($data = array()) |
| 86 |
{ |
| 87 |
$form_fields_all = $this->chekcout_form_fields(); |
| 88 |
|
| 89 |
$form_fields = array_keys($form_fields_all); |
| 90 |
|
| 91 |
$valid_data = array(); |
| 92 |
|
| 93 |
foreach ($form_fields as $field) { |
| 94 |
|
| 95 |
if (isset($form_fields_all[$field]['group_id'])) { |
| 96 |
|
| 97 |
if (isset($data[$form_fields_all[$field]['group_id']][$field])) { |
| 98 |
|
| 99 |
$valid_data[$form_fields_all[$field]['group_id']][$field] = $this->sanitization($data[$form_fields_all[$field]['group_id']][$field]); |
| 100 |
} |
| 101 |
|
| 102 |
} else { |
| 103 |
|
| 104 |
if (isset($data[$field])) { |
| 105 |
|
| 106 |
$valid_data[$field] = $this->sanitization($data[$field]); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
return $valid_data; |
| 113 |
} |
| 114 |
|
| 115 |
public function sanitization($data) |
| 116 |
{ |
| 117 |
return $data; // Need to write sanitization code. |
| 118 |
} |
| 119 |
|
| 120 |
public function tour_checkout_form() |
| 121 |
{ |
| 122 |
|
| 123 |
$form_fields = $this->chekcout_form_fields(); |
| 124 |
|
| 125 |
foreach ($form_fields as $field) { |
| 126 |
|
| 127 |
$this->form_html($field); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private function form_html($field = array()) |
| 132 |
{ |
| 133 |
$extra_attributes = array(); |
| 134 |
|
| 135 |
if (!isset($field['name']) || !isset($field['type'])) { |
| 136 |
return; |
| 137 |
} |
| 138 |
if (empty($field['name'])) { |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$field_key = $field['name']; |
| 144 |
|
| 145 |
$value = isset($field['value']) ? $field['value'] : ''; |
| 146 |
|
| 147 |
$extra_attributes = isset($field['extra_attributes']) ? $field['extra_attributes'] : array(); |
| 148 |
|
| 149 |
$extra_attribute_text = ''; |
| 150 |
|
| 151 |
foreach ($extra_attributes as $attribute_key => $attribute_value) { |
| 152 |
|
| 153 |
$extra_attribute_text .= ' ' . esc_html($attribute_key) . '="' . esc_attr($attribute_value) . '"'; |
| 154 |
} |
| 155 |
|
| 156 |
$field_type = $field['type']; |
| 157 |
|
| 158 |
$wrap_class = isset($field['wrap_class']) ? $field['wrap_class'] : ''; |
| 159 |
|
| 160 |
$row_start = isset($field['row_start']) ? (boolean)$field['row_start'] : false; |
| 161 |
|
| 162 |
if ($row_start) { |
| 163 |
|
| 164 |
echo '<div class="yatra-field-row">'; |
| 165 |
} |
| 166 |
|
| 167 |
$name = $field_key; |
| 168 |
|
| 169 |
if (isset($field['group_id'])) { |
| 170 |
|
| 171 |
$name = $field['group_id'] . '[' . $field_key . ']'; |
| 172 |
} |
| 173 |
echo '<div class="yatra-field-wrap ' . esc_attr($wrap_class) . '">'; |
| 174 |
|
| 175 |
switch ($field['type']) { |
| 176 |
case "text": |
| 177 |
case "number": |
| 178 |
case "hidden": |
| 179 |
case "email": |
| 180 |
if ($field['type'] != "hidden") { |
| 181 |
?> |
| 182 |
<p> |
| 183 |
<label |
| 184 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 185 |
:</label> |
| 186 |
<?php } |
| 187 |
?> |
| 188 |
<input class="yatra_field" |
| 189 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 190 |
name="<?php echo esc_attr(($name)); ?>" |
| 191 |
type="<?php echo esc_attr($field_type) ?>" |
| 192 |
value="<?php echo esc_attr($value); ?>" <?php echo $extra_attribute_text; ?>/> |
| 193 |
<?php if ($field['type'] != "hidden") { |
| 194 |
?> |
| 195 |
</p> |
| 196 |
<?php |
| 197 |
} |
| 198 |
break; |
| 199 |
case "textarea": |
| 200 |
|
| 201 |
$editor = isset($field['editor']) ? (boolean)$field['editor'] : false; |
| 202 |
|
| 203 |
$editor_settings = isset($field['editor_settings']) ? $field['editor_settings'] : array(); |
| 204 |
|
| 205 |
$editor_height = isset($editor_settings['editor_height']) ? (int)$field['editor_height'] : 350; |
| 206 |
|
| 207 |
$editor_default_settings = array( |
| 208 |
'textarea_name' => $field_key, |
| 209 |
'tinymce' => array( |
| 210 |
'init_instance_callback ' => 'function(inst) { |
| 211 |
$("#" + inst.id + "_ifr").css({minHeight: "' . $editor_height . 'px"}); |
| 212 |
}' |
| 213 |
), |
| 214 |
|
| 215 |
|
| 216 |
); |
| 217 |
|
| 218 |
|
| 219 |
$editor_settings = wp_parse_args($editor_default_settings, $editor_settings); |
| 220 |
|
| 221 |
?> |
| 222 |
<p> |
| 223 |
<label |
| 224 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 225 |
:</label> |
| 226 |
<?php |
| 227 |
if ($editor) { |
| 228 |
echo '</p>'; |
| 229 |
wp_editor($value, $field_key, $editor_settings); |
| 230 |
} else { |
| 231 |
?> |
| 232 |
<textarea class="yatra_field" |
| 233 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 234 |
name="<?php echo esc_attr(($field_key)); ?>" |
| 235 |
<?php echo $extra_attribute_text; ?> |
| 236 |
|
| 237 |
><?php echo esc_html($value); ?></textarea> |
| 238 |
|
| 239 |
|
| 240 |
</p> |
| 241 |
<?php } |
| 242 |
break; |
| 243 |
case "select": |
| 244 |
?> |
| 245 |
<p> |
| 246 |
<label |
| 247 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 248 |
:</label> |
| 249 |
<?php |
| 250 |
$options = isset($field['options']) ? $field['options'] : array(); |
| 251 |
$is_multi_select = isset($field['is_multiple']) ? (boolean)$field['is_multiple'] : false; |
| 252 |
$is_select2 = isset($field['select2']) ? (boolean)$field['select2'] : false; |
| 253 |
if ($is_multi_select) { |
| 254 |
$extra_attribute_text .= ' multiple="multiple"'; |
| 255 |
} |
| 256 |
$select_class = 'yatra_field'; |
| 257 |
$select_class .= $is_select2 ? ' yatra-select2' : ''; |
| 258 |
?> |
| 259 |
|
| 260 |
<select class="<?php echo esc_attr($select_class); ?>" |
| 261 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 262 |
name="<?php echo esc_attr(($name)); |
| 263 |
echo $is_multi_select ? '[]' : ''; ?>" |
| 264 |
<?php echo $extra_attribute_text; ?>> |
| 265 |
<?php foreach ($options as $option_key => $option_value) { |
| 266 |
|
| 267 |
if (!$is_multi_select) { |
| 268 |
if (is_array($value)) { |
| 269 |
$value = $value[0]; |
| 270 |
} |
| 271 |
$selected = $option_key == $value ? true : false; |
| 272 |
} else { |
| 273 |
if (!is_array($value)) { |
| 274 |
$value = array($value); |
| 275 |
} |
| 276 |
$selected = in_array($option_key, $value) ? true : false; |
| 277 |
} |
| 278 |
|
| 279 |
?> |
| 280 |
<option <?php echo $selected ? 'selected="selected"' : ''; ?> |
| 281 |
value="<?php echo esc_attr($option_key); ?>"><?php echo esc_html($option_value) ?></option><?php |
| 282 |
} |
| 283 |
?> |
| 284 |
</select> |
| 285 |
|
| 286 |
|
| 287 |
</p> |
| 288 |
<?php |
| 289 |
break; |
| 290 |
case "image": |
| 291 |
?> |
| 292 |
<p><label |
| 293 |
for="<?php echo esc_attr(($field_key)); ?>"><?php echo esc_html($field['title']); ?> |
| 294 |
:</label> |
| 295 |
<div class="media-uploader" id="<?php echo('background_image'); ?>"> |
| 296 |
<div class="custom_media_preview"> |
| 297 |
<img style="<?php echo empty($value) ? 'display:none;' : '' ?>max-width:100%;" |
| 298 |
class="media_preview_image" |
| 299 |
src="<?php echo esc_url($value); ?>" alt=""/> |
| 300 |
|
| 301 |
<input class="yatra_field custom_media_input" type="hidden" |
| 302 |
id="<?php echo esc_attr(($field_key)); ?>" |
| 303 |
name="<?php echo esc_attr(($name)); ?>" |
| 304 |
<?php echo $extra_attribute_text; ?> |
| 305 |
type="text" value="<?php echo esc_html($value); ?>"/> |
| 306 |
<button class="media_upload button" |
| 307 |
id="<?php echo('background_image'); ?>" |
| 308 |
data-choose="<?php esc_attr_e('Choose an image', 'yatra'); ?>" |
| 309 |
data-update="<?php esc_attr_e('Use image', 'yatra'); ?>" |
| 310 |
style="width:100%;margin-top:6px;margin-right:30px;"><?php esc_html_e('Select an Image', 'yatra'); ?></button> |
| 311 |
</div> |
| 312 |
|
| 313 |
</div> |
| 314 |
</p> |
| 315 |
<?php |
| 316 |
break; |
| 317 |
|
| 318 |
} |
| 319 |
echo "</div>"; |
| 320 |
|
| 321 |
$row_end = isset($field['row_end']) ? (boolean)$field['row_end'] : false; |
| 322 |
|
| 323 |
if ($row_end) { |
| 324 |
|
| 325 |
echo '</div>'; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|