-
Notifications
You must be signed in to change notification settings - Fork 56
/
PostMetabox.php
346 lines (292 loc) · 6.91 KB
/
PostMetabox.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Class that creates metaboxes on the post editing page.
*/
class scbPostMetabox {
/**
* Metabox ID.
* @var string
*/
private $id;
/**
* Title.
* @var string
*/
private $title;
/**
* Post types.
* @var array
*/
private $post_types;
/**
* Post meta data.
* @var array
*/
private $post_data = array();
/**
* Action hooks.
* @var array
*/
protected $actions = array( 'admin_enqueue_scripts', 'post_updated_messages' );
/**
* Sets up metabox.
*
* @param string $id
* @param string $title
* @param array $args (optional)
*
* @return void
*/
public function __construct( $id, $title, $args = array() ) {
$this->id = $id;
$this->title = $title;
$args = wp_parse_args( $args, array(
'post_type' => 'post',
'context' => 'advanced',
'priority' => 'default',
) );
if ( is_string( $args['post_type'] ) ) {
$args['post_type'] = array( $args['post_type'] );
}
$this->post_types = $args['post_type'];
$this->context = $args['context'];
$this->priority = $args['priority'];
add_action( 'load-post.php', array( $this, 'pre_register' ) );
add_action( 'load-post-new.php', array( $this, 'pre_register' ) );
}
/**
* Pre register the metabox.
*
* @return void
*/
final public function pre_register() {
if ( ! in_array( get_current_screen()->post_type, $this->post_types ) ) {
return;
}
if ( ! $this->condition() ) {
return;
}
if ( isset( $_GET['post'] ) ) {
$this->post_data = $this->get_meta( intval( $_GET['post'] ) );
}
add_action( 'add_meta_boxes', array( $this, 'register' ) );
add_action( 'save_post', array( $this, '_save_post' ), 10, 2 );
foreach ( $this->actions as $action ) {
if ( method_exists( $this, $action ) ) {
add_action( $action, array( $this, $action ) );
}
}
}
/**
* Additional checks before registering the metabox.
*
* @return bool
*/
protected function condition() {
return true;
}
/**
* Registers the metabox.
*
* @return void
*/
final public function register() {
add_meta_box( $this->id, $this->title, array( $this, 'display' ), null, $this->context, $this->priority );
}
/**
* Filter data before display.
*
* @param array $form_data
* @param object $post
*
* @return array
*/
public function before_display( $form_data, $post ) {
return $form_data;
}
/**
* Displays metabox content.
*
* @param object $post
*
* @return void
*/
public function display( $post ) {
$form_fields = $this->form_fields();
if ( ! $form_fields ) {
return;
}
$form_data = $this->post_data;
$error_fields = array();
if ( isset( $form_data['_error_data_' . $this->id ] ) ) {
$data = maybe_unserialize( $form_data['_error_data_' . $this->id ] );
$error_fields = $data['fields'];
$form_data = $data['data'];
$this->display_notices( $data['messages'], 'error' );
}
$form_data = $this->before_display( $form_data, $post );
$this->before_form( $post );
echo $this->table( $form_fields, $form_data, $error_fields );
$this->after_form( $post );
delete_post_meta( $post->ID, '_error_data_' . $this->id );
}
/**
* Returns table.
*
* @param array $rows
* @param array $formdata
* @param array $errors (optional)
*
* @return string
*/
public function table( $rows, $formdata, $errors = array() ) {
$output = '';
foreach ( $rows as $row ) {
$output .= $this->table_row( $row, $formdata, $errors );
}
$output = scbForms::table_wrap( $output );
return $output;
}
/**
* Returns table row.
*
* @param array $row
* @param array $formdata
* @param array $errors (optional)
*
* @return string
*/
public function table_row( $row, $formdata, $errors = array() ) {
$input = scbForms::input( $row, $formdata );
// If row has an error, highlight it
$style = ( in_array( $row['name'], $errors ) ) ? 'style="background-color: #FFCCCC"' : '';
return html( 'tr',
html( "th $style scope='row'", $row['title'] ),
html( "td $style", $input )
);
}
/**
* Displays notices.
*
* @param array|string $notices
* @param string $class (optional)
*
* @return void
*/
public function display_notices( $notices, $class = 'updated' ) {
// add inline class so the notices stays in metabox
$class .= ' inline';
foreach ( (array) $notices as $notice ) {
echo scb_admin_notice( $notice, $class );
}
}
/**
* Display some extra HTML before the form.
*
* @param object $post
*
* @return void
*/
public function before_form( $post ) { }
/**
* Return an array of form fields.
*
* @return array
*/
public function form_fields() {
return array();
}
/**
* Display some extra HTML after the form.
*
* @param object $post
*
* @return void
*/
public function after_form( $post ) { }
/**
* Makes sure that the saving occurs only for the post being edited.
*
* @param int $post_id
* @param object $post
*
* @return void
*/
final public function _save_post( $post_id, $post ) {
if ( ! isset( $_POST['action'] ) || $_POST['action'] != 'editpost' ) {
return;
}
if ( ! isset( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
return;
}
if ( ! in_array( $post->post_type, $this->post_types ) ) {
return;
}
$this->save( $post->ID );
}
/**
* Saves metabox form data.
*
* @param int $post_id
*
* @return void
*/
protected function save( $post_id ) {
$form_fields = $this->form_fields();
$to_update = scbForms::validate_post_data( $form_fields );
// Filter data
$to_update = $this->before_save( $to_update, $post_id );
// Validate dataset
$is_valid = $this->validate_post_data( $to_update, $post_id );
if ( $is_valid instanceof WP_Error && $is_valid->get_error_codes() ) {
$error_data = array(
'fields' => $is_valid->get_error_codes(),
'messages' => $is_valid->get_error_messages(),
'data' => $to_update,
);
update_post_meta( $post_id, '_error_data_' . $this->id, $error_data );
$location = add_query_arg( 'message', 1, get_edit_post_link( $post_id, 'url' ) );
wp_redirect( esc_url_raw( apply_filters( 'redirect_post_location', $location, $post_id ) ) );
exit;
}
foreach ( $to_update as $key => $value ) {
update_post_meta( $post_id, $key, $value );
}
}
/**
* Filter data before save.
*
* @param array $post_data
* @param int $post_id
*
* @return array
*/
protected function before_save( $post_data, $post_id ) {
return $post_data;
}
/**
* Validate posted data.
*
* @param array $post_data
* @param int $post_id
*
* @return bool|object A WP_Error object if posted data are invalid.
*/
protected function validate_post_data( $post_data, $post_id ) {
return false;
}
/**
* Returns an array of post meta.
*
* @param int $post_id
*
* @return array
*/
private function get_meta( $post_id ) {
$meta = get_post_custom( $post_id );
foreach ( $meta as $key => $values ) {
$meta[ $key ] = maybe_unserialize( $meta[ $key ][0] );
}
return $meta;
}
}