PostMetabox.php
4.71 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
<?php
class scbPostMetabox {
private $id, $title;
private $post_types;
private $post_data = array();
protected $actions = array( 'admin_enqueue_scripts', 'post_updated_messages' );
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' ) );
}
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
protected function condition() {
return true;
}
final public function register() {
add_meta_box( $this->id, $this->title, array( $this, 'display' ), null, $this->context, $this->priority );
}
public function before_display( $form_data, $post ) {
return $form_data;
}
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 = unserialize( $form_data[ '_error_data_' . $this->id ] );
$error_fields = $data['fields'];
$form_data = $data['data'];
}
$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 );
badgeos_utilities::del_post_meta( $post->ID, '_error_data_' . $this->id );
}
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;
}
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 )
);
}
// Display some extra HTML before the form
public function before_form( $post ) { }
// Return the list of form fields
public function form_fields() {
return array();
}
// Display some extra HTML after the form
public function after_form( $post ) { }
// Makes sure that the saving occurs only for the post being edited
final public function _save_post( $post_id, $post ) {
if ( ! isset( $_POST['action'] ) || $_POST['action'] != 'editpost' ) {
return;
}
if ( $post_id != $_POST['post_ID'] ) {
return;
}
if ( ! in_array( $post->post_type, $this->post_types ) ) {
return;
}
$this->save( $post->ID );
}
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(),
'data' => $to_update,
);
badgeos_utilities::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( apply_filters( 'redirect_post_location', $location, $post_id ) );
exit;
}
foreach ( $to_update as $key => $value ) {
badgeos_utilities::update_post_meta( $post_id, $key, $value );
}
}
protected function before_save( $post_data, $post_id ) {
return $post_data;
}
protected function validate_post_data( $post_data ) {
return false;
}
private function get_meta( $post_id ) {
$meta = get_post_custom( $post_id );
foreach ( $meta as $key => $values ) {
$meta[ $key ] = $meta[ $key ][0];
}
return $meta;
}
}