-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomescript_fields.php
248 lines (204 loc) · 9.6 KB
/
homescript_fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
if (!defined('ABSPATH')) {
die();
}
/**
* Generate appropriate fields for meta and page in WordPress.
*
* @param string $key Key.
* @param mixed $args Arguments.
* @param string $value (default: null).
* @return string
*/
if (!function_exists('homescript_input_fields')) {
function homescript_input_fields($key, $args, $value = null)
{
$defaults = array(
'type' => 'text',
'label' => '',
'description' => '',
'placeholder' => '',
'maxlength' => false,
'required' => false,
'autocomplete' => false,
'id' => $key,
'class' => array(),
'label_class' => array(),
'input_class' => array(),
'return' => false,
'options' => array(),
'custom_attributes' => array(),
'validate' => array(),
'default' => '',
'autofocus' => '',
'priority' => '',
);
$args = wp_parse_args($args, $defaults);
$args = apply_filters('homescript_form_field_args', $args, $key, $value);
if ($args['required']) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__('required', 'homescript') . '">*</abbr>';
} else {
$required = ' <span class="optional">(' . esc_html__('optional', 'homescript') . ')</span>';
}
if (is_string($args['label_class'])) {
$args['label_class'] = array($args['label_class']);
}
if (is_null($value)) {
$value = $args['default'];
}
// Custom attribute handling.
$custom_attributes = array();
$args['custom_attributes'] = array_filter((array) $args['custom_attributes'], 'strlen');
if ($args['maxlength']) {
$args['custom_attributes']['maxlength'] = absint($args['maxlength']);
}
if (!empty($args['autocomplete'])) {
$args['custom_attributes']['autocomplete'] = $args['autocomplete'];
}
if (true === $args['autofocus']) {
$args['custom_attributes']['autofocus'] = 'autofocus';
}
if ($args['description']) {
$args['custom_attributes']['aria-describedby'] = $args['id'] . '-description';
}
if (!empty($args['custom_attributes']) && is_array($args['custom_attributes'])) {
foreach ($args['custom_attributes'] as $attribute => $attribute_value) {
$custom_attributes[] = esc_attr($attribute) . '="' . esc_attr($attribute_value) . '"';
}
}
if (!empty($args['validate'])) {
foreach ($args['validate'] as $validate) {
$args['class'][] = 'validate-' . $validate;
}
}
$field = '';
$label_id = $args['id'];
$sort = $args['priority'] ? $args['priority'] : '';
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr($sort) . '">%3$s</p>';
switch ($args['type']) {
case 'textarea':
$field .= '<textarea name="' . esc_attr($key) . '" class="input-text ' . esc_attr(implode(' ', $args['input_class'])) . '" id="' . esc_attr($args['id']) . '" placeholder="' . esc_attr($args['placeholder']) . '" ' . (empty($args['custom_attributes']['rows']) ? ' rows="2"' : '') . (empty($args['custom_attributes']['cols']) ? ' cols="5"' : '') . implode(' ', $custom_attributes) . '>' . esc_textarea($value) . '</textarea>';
break;
case 'checkbox':
$field = '<label class="checkbox ' . implode(' ', $args['label_class']) . '" ' . implode(' ', $custom_attributes) . '>
<input type="' . esc_attr($args['type']) . '" class="input-checkbox ' . esc_attr(implode(' ', $args['input_class'])) . '" name="' . esc_attr($key) . '" id="' . esc_attr($args['id']) . '" value="1" ' . checked($value, 1, false) . ' /> ' . $args['label'] . $required . '</label>';
break;
case 'text':
case 'password':
case 'datetime':
case 'datetime-local':
case 'date':
case 'month':
case 'time':
case 'week':
case 'number':
case 'email':
case 'url':
case 'tel':
$field .= '<input type="' . esc_attr($args['type']) . '" class="input-text ' . esc_attr(implode(' ', $args['input_class'])) . '" name="' . esc_attr($key) . '" id="' . esc_attr($args['id']) . '" placeholder="' . esc_attr($args['placeholder']) . '" value="' . esc_attr($value) . '" ' . implode(' ', $custom_attributes) . ' />';
break;
case 'select':
$field = '';
$options = '';
if (!empty($args['options'])) {
foreach ($args['options'] as $option_key => $option_text) {
if ('' === $option_key) {
// If we have a blank option, select2 needs a placeholder.
if (empty($args['placeholder'])) {
$args['placeholder'] = $option_text ? $option_text : __('Choose an option', 'homescript');
}
$custom_attributes[] = 'data-allow_clear="true"';
}
$options .= '<option value="' . esc_attr($option_key) . '" ' . selected($value, $option_key, false) . '>' . esc_attr($option_text) . '</option>';
}
$field .= '<select name="' . esc_attr($key) . '" id="' . esc_attr($args['id']) . '" class="select ' . esc_attr(implode(' ', $args['input_class'])) . '" ' . implode(' ', $custom_attributes) . ' data-placeholder="' . esc_attr($args['placeholder']) . '">
' . $options . '
</select>';
}
break;
case 'radio':
$label_id .= '_' . current(array_keys($args['options']));
if (!empty($args['options'])) {
foreach ($args['options'] as $option_key => $option_text) {
$field .= '<input type="radio" class="input-radio ' . esc_attr(implode(' ', $args['input_class'])) . '" value="' . esc_attr($option_key) . '" name="' . esc_attr($key) . '" ' . implode(' ', $custom_attributes) . ' id="' . esc_attr($args['id']) . '_' . esc_attr($option_key) . '"' . checked($value, $option_key, false) . ' />';
$field .= '<label for="' . esc_attr($args['id']) . '_' . esc_attr($option_key) . '" class="radio ' . implode(' ', $args['label_class']) . '">' . $option_text . '</label>';
}
}
break;
}
if (!empty($field)) {
$field_html = '';
if ($args['label'] && 'checkbox' !== $args['type']) {
$field_html .= '<label for="' . esc_attr($label_id) . '" class="' . esc_attr(implode(' ', $args['label_class'])) . '">' . $args['label'] . $required . '</label>';
}
$field_html .= '<span class="homescript-input-wrapper">' . $field;
if ($args['description']) {
$field_html .= '<span class="description" id="' . esc_attr($args['id']) . '-description" aria-hidden="true">' . wp_kses_post($args['description']) . '</span>';
}
$field_html .= '</span>';
$container_class = esc_attr(implode(' ', $args['class']));
$container_id = esc_attr($args['id']) . '_field';
$field = sprintf($field_container, $container_class, $container_id, $field_html);
}
/**
* Filter by type.
*/
$field = apply_filters('homescript_form_field_' . $args['type'], $field, $key, $args, $value);
/**
* General filter on form fields.
*
* @since 3.4.0
*/
$field = apply_filters('homescript_form_field', $field, $key, $args, $value);
if ($args['return']) {
return $field;
} else {
echo $field; // WPCS: XSS ok.
}
}
}
/**
* Build a button.
*
* @param mixed $key
* @param array $args
* @param string $button_text
*
* @return void
*/
if (!function_exists('homescript_button')) {
function homescript_button($key, $args, $button_text = null)
{
$defaults = array(
'id' => $key,
'class' => array(),
'label_class' =>array(),
'label' => '',
);
$args = wp_parse_args($args, $defaults);
$args = apply_filters('homescript_button_args', $args, $key, $button_text);
if ($args['id']) {
$id = $args['id'];
}
if (is_array($args['class'])) {
$button_class = '';
foreach ($args['class'] as $class) {
$button_class .= ' '. $class;
}
}
if ($args['value']) {
$value = $args['value'];
}
if (isset($args['label'])){
$label = $args['label'];
}
if (is_array($args['label_class'])) {
$label_class = '';
foreach ($args['label_class'] as $class) {
$label_class .= ' '. $class;
}
}
echo "<label class=".$label_class." >".$label."</label>   <button id='" . $id . "' class='" . $button_class . "'>" . $value . "</button>";
}
}