-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtexts.php
293 lines (256 loc) · 9.25 KB
/
texts.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* WebMan Metabox Generator
* Form Fields Renderers and Validators
*
* Text and color elements
*
* @package WebMan Amplifier
* @subpackage Metabox
*
* @since 1.0
* @version 1.1.6
*/
/**
* TEXT (AND COLOR) FIELDS
*/
/**
* Text input (used also as color and password input)
*
* @package WebMan Amplifier
* @subpackage Metabox
*
* @since 1.0
* @version 1.1.6
*/
if ( ! function_exists( 'wma_field_text' ) ) {
function wma_field_text( $field, $page_template = null ) {
//Field definition array
$field = wp_parse_args( (array) $field, array(
//DEFAULTS:
//* = Required setting
'type' => 'text', //Field type name *
'id' => 'id', //Field ID (form field name) *
'label' => '', //Field label
'description' => '', //Field description
'default' => '', //Default value
'class' => '', //Additional CSS class
'attributes' => '', //Additional HTML attributes applied on input field
'validate' => '', //Field output validation
'empty' => false, //Can be empty or should there always be default value applied?
'repeater' => false, //Special values when used in Repeater field
'conditional' => '', //Conditional display setup
) );
if ( 'color' == $field['type'] ) {
$field['type'] = 'text';
$field['validate'] = 'color';
}
//Value processing
if (
$field['repeater']
&& is_array( $field['repeater'] )
&& isset( $field['repeater']['value'] )
) {
$value = $field['repeater']['value'];
} else {
$value = wma_meta_option( $field['id'] );
}
if ( $field['validate'] ) {
switch ( $field['validate'] ) {
case 'url':
$value = esc_url( $value );
break;
case 'absint':
$value = absint( $value );
$field['type'] = 'number';
break;
case 'int':
$value = intval( $value );
$field['type'] = 'number';
break;
case 'email':
$field['type'] = 'email';
break;
case 'color':
$value = preg_replace( '/[^a-fA-F0-9\#]/', '', $value );
$field['class'] = 'color-wrap ' . $field['class'];
break;
default:
break;
}
}
$value = apply_filters( 'wmhook_metabox_' . 'wma_field_text' . '_sanitize', wp_kses_post( $value ), $field, $page_template );
if ( $value || $field['empty'] ) {
$value = esc_html( $value );
} else {
$value = esc_html( $field['default'] );
}
//Field ID setup
if (
$field['repeater']
&& is_array( $field['repeater'] )
&& isset( $field['repeater']['id'] )
) {
$field['id'] = $field['repeater']['id'];
} else {
$field['id'] = WM_METABOX_FIELD_PREFIX . $field['id'];
}
//Output
$output = "\r\n\t" . '<tr class="option text-wrap option-' . trim( sanitize_html_class( $field['id'] ) . ' ' . $field['class'] ) . '" data-option="' . $field['id'] . '"><th>';
//Label
$output .= "\r\n\t\t" . '<label for="' . $field['id'] . '">' . trim( strip_tags( $field['label'], WM_METABOX_LABEL_HTML ) ) . '</label>';
$output .= "\r\n\t" . '</th><td>';
//Input field
$output .= "\r\n\t\t" . '<input type="' . $field['type'] . '" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $value . '" class="fieldtype-text" ' . trim( $field['attributes'] . ' /' ) . '>';
//Description
if ( trim( $field['description'] ) ) {
$output .= "\r\n\t\t" . '<p class="description">' . trim( $field['description'] ) . '</p>';
}
//Reset default value button
if ( trim( $field['default'] ) ) {
$output .= "\r\n\t\t" . '<a data-option="' . $field['id'] . '" class="button-default-value" title="' . __( 'Use a default value', 'webman-amplifier' ) . '"><span>' . $field['default'] . '</span></a>';
}
echo $output;
//Conditional display
do_action( 'wmhook_metabox_' . 'conditional', $field, $field['id'] );
echo "\r\n\t" . '</td></tr>';
}
} // /wma_field_text
add_action( 'wmhook_metabox_' . 'render_' . 'color', 'wma_field_text', 10, 2 );
add_action( 'wmhook_metabox_' . 'render_' . 'password', 'wma_field_text', 10, 2 );
add_action( 'wmhook_metabox_' . 'render_' . 'text', 'wma_field_text', 10, 2 );
/**
* Text input validation
*
* @since 1.0
* @package WebMan Amplifier
* @subpackage Metabox
* @author WebMan
* @version 1.0
*/
if ( ! function_exists( 'wma_field_text_validation' ) ) {
function wma_field_text_validation( $new, $field, $post_id ) {
$new = wp_kses_post( $new );
if ( isset( $field['validate'] ) && $field['validate'] ) {
switch ( $field['validate'] ) {
case 'url':
$new = esc_url( $new );
break;
case 'absint':
$new = absint( $new );
break;
case 'int':
$new = intval( $new );
break;
case 'email':
$new = ( is_email( $new ) ) ? ( $new ) : ( '' );
break;
case 'color':
$new = preg_replace( '/[^a-fA-F0-9\#]/', '', $new );
break;
default:
break;
}
}
return $new;
}
} // /wma_field_text_validation
add_action( 'wmhook_metabox_' . 'saving_' . 'color', 'wma_field_text_validation', 10, 3 );
add_action( 'wmhook_metabox_' . 'saving_' . 'password', 'wma_field_text_validation', 10, 3 );
add_action( 'wmhook_metabox_' . 'saving_' . 'text', 'wma_field_text_validation', 10, 3 );
/**
* Textarea (used also as visual editor)
*
* @package WebMan Amplifier
* @subpackage Metabox
*
* @since 1.0
* @version 1.1.6
*/
if ( ! function_exists( 'wma_field_textarea' ) ) {
function wma_field_textarea( $field, $page_template = null ) {
//Field definition array
$field = wp_parse_args( (array) $field, array(
//DEFAULTS:
//* = Required setting
'type' => 'textarea', //Field type name *
'id' => 'id', //Field ID (form field name) *
'label' => '', //Field label
'description' => '', //Field description
'default' => '', //Default value
'class' => '', //Additional CSS class
'attributes' => 'rows="3"', //Additional HTML attributes applied on input field
'empty' => false, //Can be empty or should there always be default value applied?
'editor' => false, //Display visual editor?
'repeater' => false, //Special values when used in Repeater field
'conditional' => '', //Conditional display setup
) );
//Value processing
if (
$field['repeater']
&& is_array( $field['repeater'] )
&& isset( $field['repeater']['value'] )
) {
$value = $field['repeater']['value'];
} else {
$value = wma_meta_option( $field['id'] );
}
if ( $value || $field['empty'] ) {
$value = ( ! $field['editor'] ) ? ( esc_textarea( $value ) ) : ( $value );
} else {
$value = ( ! $field['editor'] ) ? ( esc_textarea( $field['default'] ) ) : ( $field['default'] );
}
//Field ID setup
if (
$field['repeater']
&& is_array( $field['repeater'] )
&& isset( $field['repeater']['id'] )
) {
$field['id'] = $field['repeater']['id'];
} else {
$field['id'] = WM_METABOX_FIELD_PREFIX . $field['id'];
}
//Output
$output = "\r\n\t" . '<tr class="option textarea-wrap option-' . trim( sanitize_html_class( $field['id'] ) . ' ' . $field['class'] ) . '" data-option="' . $field['id'] . '"><th>';
//Label
$output .= "\r\n\t\t" . '<label for="' . $field['id'] . '">' . trim( strip_tags( $field['label'], WM_METABOX_LABEL_HTML ) ) . '</label>';
$output .= "\r\n\t" . '</th><td>';
//Input field
if ( ! $field['editor'] ) {
$output .= "\r\n\t\t" . '<textarea name="' . $field['id'] . '" id="' . $field['id'] . '" class="fieldtype-textarea" ' . trim( $field['attributes'] ) . '>' . $value . '</textarea>';
} else {
echo $output;
wp_editor( $value, $field['id'] );
$output = '';
}
//Description
if ( trim( $field['description'] ) ) {
$output .= "\r\n\t\t" . '<p class="description">' . trim( $field['description'] ) . '</p>';
}
//Reset default value button
if ( trim( $field['default'] ) && ! $field['editor'] ) {
$output .= "\r\n\t\t" . '<a data-option="' . $field['id'] . '" class="button-default-value" title="' . __( 'Use a default value', 'webman-amplifier' ) . '"><span>' . $field['default'] . '</span></a>';
}
echo $output;
//Conditional display
do_action( 'wmhook_metabox_' . 'conditional', $field, $field['id'] );
echo "\r\n\t" . '</td></tr>';
}
} // /wma_field_textarea
add_action( 'wmhook_metabox_' . 'render_' . 'textarea', 'wma_field_textarea', 10, 2 );
/**
* Textarea validation
*
* @since 1.0
* @package WebMan Amplifier
* @subpackage Metabox
* @author WebMan
* @version 1.0
*/
if ( ! function_exists( 'wma_field_textarea_validation' ) ) {
function wma_field_textarea_validation( $new, $field, $post_id ) {
$new = wp_kses_post( $new );
return $new;
}
} // /wma_field_textarea_validation
add_action( 'wmhook_metabox_' . 'saving_' . 'textarea', 'wma_field_textarea_validation', 10, 3 );