-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-meta-boxes.php
More file actions
269 lines (218 loc) · 8.89 KB
/
Copy pathclass-meta-boxes.php
File metadata and controls
269 lines (218 loc) · 8.89 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
260
261
262
263
264
265
266
267
268
269
<?php
if ( ! class_exists( 'Helios_Add_Meta_Box' ) ) :
class Helios_Add_Meta_Box {
private $boxes;
/**
* Hook into the appropriate actions when the class is constructed.
*/
public function __construct( $meta_box_data ) {
$this->boxes = $meta_box_data;
add_action( 'add_meta_boxes_' . $this->boxes['post_type'], array( $this, 'add_meta_box' ) );
add_action( 'save_post_' . $this->boxes['post_type'], array( $this, 'save_meta_box' ) );
add_filter( 'helios_check_template', array( $this, 'check_template' ), 10, 2 );
}
/**
* Descendant category checking utility function.
*/
public function check_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category' );
if ( $descendants && in_category( $descendants, $_post ) ) {
return true;
}
}
return false;
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post ) {
//$post_types = array('post', 'page'); //limit meta box to certain post types
//render_var($post_type);
$box = $this->boxes;
if ( ! empty( $box['parent_category'] ) ) {
$category = get_term_by( 'name', $box['parent_category'], 'category' );
if ( ! in_category( $box['parent_category'] ) && ! $this->check_descendant_category( $category->term_id ) ) {
return;
}
}
if ( ! empty( $box['template'] ) && ! apply_filters( 'helios_check_template', true, $this->boxes ) )
return;
add_meta_box(
$box['id'],
$box['headline'],
array( $this, 'render_meta_box' ),
$post->post_type,
$box['context'],
$box['priority'],
array(
'description' => isset( $box['description'] ) ? $box['description'] : '',
'fields' => $box['fields']
)
);
}
public function check_template( $display, $box ) {
if ( empty( $box['template'] ) )
return false;
if ( isset( $_GET['post'] ) ) {
$post_id = $_GET['post'];
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = $_POST['post_ID'];
}
if ( ! ( isset( $post_id ) || is_page() ) ) return false;
$templ = get_post_meta( $post_id, '_wp_page_template', true );
$box['template'] = ! is_array( $box['template'] ) ? array( $box['template'] ) : $box['template'];
if ( in_array( $templ, $box['template'] ) ) {
return true;
} else {
return false;
}
}
/**
* Save the meta when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public function save_meta_box( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['helios_inner_custom_box_nonce'] ) )
return $post_id;
$nonce = $_POST['helios_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'helios_inner_custom_box' ) )
return $post_id;
// If this is an autosave, our form has not been submitted,
// so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
foreach( $this->boxes['fields'] as $field ) {
$key = $field->id;
// Check to make sure the key exists.
// if ( ! isset( $_POST[ $key ] ) )
// continue;
if ( $field->type == 'output' ) {
continue;
}
// Sanitize the user input.
if ( $field->type == 'checkbox-group' ) {
$unprocessed = isset( $_POST[ $key ] ) ? $_POST[ $key ] : array();
$value = array();
if ( is_array( $unprocessed ) && count( $unprocessed ) > 1) {
foreach( $unprocessed as $val ) { $value[] = (int) $val; }
$value = implode(',', $value);
} else {
$value = isset( $_POST[ $key ][0] ) ? $_POST[ $key ][0] : null;
}
// $value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : null;
} elseif ( $field->type == 'text' ) {
$value = isset( $_POST[ $key ] ) ? sanitize_text_field( $_POST[ $key ] ) : null;
if ( ! empty( $field->date_format ) ) {
$value = date( $field->date_format, strtotime( $value ) );
}
} else {
$value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : null;
}
// Update the meta field.
update_post_meta( $post_id, $key, $value );
}
}
/**
* Render Meta Box content.
*
* @param WP_Post $post The post object.
*/
public function render_meta_box( $post, $args ) {
$fields = $args['args']['fields'];
if ( ! empty( $args['args']['description'] ) ) {
echo $args['args']['description'];
}
// Add an nonce field so we can check for it later.
wp_nonce_field( 'helios_inner_custom_box', 'helios_inner_custom_box_nonce' );
foreach( $fields as $field ) {
// Use get_post_meta to retrieve an existing value from the database.
$post_value = get_post_meta( $post->ID, $field->id, true );
$default = isset( $field->default ) ? $field->default : '';
$value = empty( $post_value ) && $post_value != 0 ? $default : $post_value;
if ( ! empty( $value ) && ! empty( $field->date_format ) ) {
$value = date('Y-m-d', strtotime( $value ) );
}
$label = ! empty( $field->label ) ? sprintf( '<label for="%s" style="width: 200px; display: inline-block;">%s </label>', $field->id, $field->label ) : '';
$classes = isset( $field->class ) ? ' class="' . implode( ',', $field->class ) . '"' : '';
echo '<p' . $classes . '>';
switch( $field->type ) {
case 'text':
printf('%2$s<input type="text" id="%1$s" name="%1$s" value="%3$s" size="%4$s"%5$s>%6$s',
$field->id,
$label,
esc_attr( $value ),
! empty( $field->size ) ? $field->size : 25,
! empty( $field->placeholder) ? ' placeholder="' . $field->placeholder . '"' : '',
! empty( $field->after ) ? ' <span class="meta-after">' . $field->after . '</span>' : ''
);
break;
case 'select':
echo $label;
echo '<select name="' . $field->id . '" id="' . $field->id . '">';
foreach( $field->choices as $k => $v ) {
$selected = selected( $value, $k, false );//$value == $k ? 'selected' : '';
echo '<option value="' . $k . '" ' . $selected . '>' . $v . '</option>';
}
echo '</select>';
break;
case 'editor':
wp_editor( $value, $field->id );
break;
case 'checkbox':
printf('%1$s<input type="checkbox" id="%2$s" value="1" name="%2$s" %3$s>%4$s',
$label,
$field->id,
checked( $value, 1, false ),
! empty( $field->after ) ? ' <span class="meta-after">' . $field->after . '</span>' : ''
);
break;
case 'checkbox-group':
$value = explode(',', $value);
printf( '<p><label for="%s">%s </label></p>', $field->id, $field->label );
foreach( $field->choices as $k => $v ) {
printf('<input type="checkbox" id="%1$s" value="%1$s" name="%2$s" %3$s> %4$s<br>',
$k,
$field->id . '[]',
checked( in_array( $k, $value ), true, false ),
$v
);
}
break;
case 'textarea':
printf('%1$s<textarea name="%2$s" id="%2$s" rows="%4$s" cols="%5$s" style="max-width:%6$s;">%3$s</textarea><br>%7$s',
! empty( $field->label) ? '<p>' . $label . '</p>' : $label,
$field->id,
esc_attr( $value ),
! empty( $field->rows ) ? $field->rows : '',
! empty( $field->columns) ? $field->columns : '',
'100%',
! empty( $field->after ) ? ' <span class="meta-after">' . $field->after . '</span>' : ''
);
break;
case 'output':
printf('<pre>%s</pre>', get_post_meta( $post->ID, $field->id, true ) );
break;
}
echo '</p>';
}
}
}
endif;