-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfields.php
More file actions
259 lines (205 loc) · 5.06 KB
/
fields.php
File metadata and controls
259 lines (205 loc) · 5.06 KB
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
<?php
if( !class_exists( 'NS_Tabbed_Meta_Field' ) ) :
/**
*
*/
class NS_Tabbed_Meta_Field {
public static function save( $post_id, $name, $post, $args ) {
// dont save blanks by default
if( !empty( $_POST[$name] ) ) {
if( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
$value = call_user_func_array( $args['sanitize_callback'], array( $_POST[$name] ) );
} else {
$value = sanitize_text_field( $_POST[$name] );
}
update_post_meta( $post_id, $name, $value );
} else {
delete_post_meta( $post_id, $name );
}
}
}
/**
*
*/
class NS_Tabbed_Meta_Text_Field extends NS_Tabbed_Meta_Field {
/**
*
*/
public static function render( $args ) {
// this input can have a placeholder value
$placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : '';
// class is customizable
$class = isset( $args['class'] ) ? $args['class'] : 'widefat';
return sprintf(
'<input type="text" name="%s" value="%s" placeholder="%s" class="%s">',
esc_attr( $args['name'] ),
esc_attr( $args['value'] ),
esc_attr( $placeholder ),
esc_attr( $class )
);
}
}
/**
* Builds a data field that uses the jquery date picker
*/
class NS_Tabbed_Meta_Date_Field extends NS_Tabbed_Meta_Text_Field {
/**
* Output the field markup
*/
public static function render( $args ) {
$value = !empty( $args['value'] ) ? $args['value'] : time();
return sprintf(
'<input type="text" name="%s" value="%s">',
esc_attr( $args['name'] ),
esc_attr( date( 'm/d/Y', $value ) )
);
}
/**
* Save the data
*
* @return string
*/
public static function save( $post_id, $name, $post, $args ) {
if( isset( $_POST[$name] ) )
update_post_meta( $post_id, $name, strtotime( $_POST[$name] ) );
else
delete_post_meta( $post_id, $name );
}
}
/**
*
*/
class NS_Tabbed_Meta_Checkbox_Field extends NS_Tabbed_Meta_Field {
public static function render( $args ) {
return sprintf(
'<input type="checkbox" name="%s" value="1" %s>',
esc_attr( $args['name'] ),
checked( 1, $args['value'], 0 )
);
}
/**
* Save either a 1 or 0
*/
public static function save( $post_id, $name, $post, $args ) {
if( isset( $_POST[$name] ) )
update_post_meta( $post_id, $name, 1 );
else
update_post_meta( $post_id, $name, 0 );
}
}
/**
* URL/Link field
*/
class NS_Tabbed_Meta_Link_Field extends NS_Tabbed_Meta_Field {
/**
*
*/
public static function render( $args ) {
$placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : '';
return sprintf(
'<input type="text" name="%s" value="%s" placeholder="%s" class="widefat">',
esc_attr( $args['name'] ),
esc_url( $args['value'] ),
esc_attr( $placeholder )
);
}
/**
* Use esc_url
*/
public static function save( $post_id, $name, $post, $args ) {
$args['sanitize_callback'] = 'esc_url';
parent::save( $post_id, $name, $post, $args );
}
}
/**
* Textarea field
*/
class NS_Tabbed_Meta_Textarea_Field extends NS_Tabbed_Meta_Field {
/**
*
*/
public static function render( $args ) {
// this input can have a placeholder value
$placeholder = isset( $args['placeholder'] ) ? $args['placeholder'] : '';
// class is customizable
$class = isset( $args['class'] ) ? $args['class'] : 'widefat';
return sprintf(
'<textarea name="%s" class="widefat" rows="4">%s</textarea>',
esc_attr( $args['name'] ),
esc_textarea( $args['value'] )
);
}
}
/**
*
*/
class NS_Tabbed_Meta_Select_Field extends NS_Tabbed_Meta_Field {
/**
*
*/
public static function render( $args ) {
$html = '';
if( isset( $args['options'] ) && is_array( $args['options'] ) ) {
foreach( $args['options'] as $key => $value ) {
$html .= sprintf(
'<option value="%s" %s>%s</option>',
esc_attr( $key ),
selected( $key, $args['value'], 0 ),
esc_html( $value )
);
}
}
return sprintf(
'<select name="%s">%s</select>',
esc_attr( $args['name'] ),
$html
);
}
}
/**
*
*/
class NS_Tabbed_Meta_Sorter_Field extends NS_Tabbed_Meta_Field {
/**
*
*/
public static function render( $args ) {
// setup some defaults
$args = wp_parse_args( $args, array(
'items' => array()
));
$html = '<div>';
$items = $args['items'];
// if we have a value, determine the order of the items
if( !empty( $args['value'] ) && is_array( $args['items'] ) ) {
$existing = array();
foreach( explode( ',', $args['value'] ) as $v ) {
$existing[$v] = $args['items'][$v];
}
$items = $existing;
}
$html .= sprintf(
'<input type="hidden" name="%s" value="%s" class="tm-field-sorter-ids">',
esc_attr( $args['name'] ),
!empty( $args['value'] ) ? esc_attr( $args['value'] ) : implode( ',', array_keys( $items ) )
);
// output items in order
if( count( $items ) ) {
$html .= '<ul>';
foreach( $items as $key => $item ) {
$html .= sprintf(
'<li data-id="%s">' .
'<h4>%s</h4>' .
'</li>',
esc_attr( $key ),
esc_html( $item )
);
}
$html .= '</ul>';
}
// close div
$html .= '</div>';
return $html;
}
}
endif;