-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.php
348 lines (305 loc) · 7.7 KB
/
index.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
346
347
348
<?php
/**
* Post Meta Box (UI + form handler) manager class.
*
* Handles the construction of the form container (here a meta box), and
* bridges the gap between the form submission and saving the fields'
* data.
*
* Business logic specific to the object type is defined by the class by default,
* but can easily be overridden at the field object level (in a subclass).
*
*/
class WP_Post_Meta_Box_Manager {
/**
* Related fields
*
* @var array
*/
var $fields = array();
/**
* Metabox title
*
* @var string
*/
var $title = '';
/**
* Unique identifier
*
* @var string
*/
var $name = '';
function __construct( $args = array() ) {
$keys = array_keys( get_class_vars( __CLASS__ ) );
foreach ( $keys as $key ) {
if ( isset( $args[ $key ] ) )
$this->$key = $args[ $key ];
}
if ( empty( $this->name ) )
$this->name = sanitize_title( $this->title );
// Register object-specific UI
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
// Register data-submission handler
add_action( 'save_post', array( $this, 'save' ), 10, 2 );
}
/**
* Register the meta box.
*/
function add_meta_box() {
add_meta_box(
$this->name, // ID
$this->title, // title
array( $this, 'metabox_callback' ), // render callback
'post', // screen
'advanced', // context
'default', // priority
null // callback_args
);
}
/**
* Output the contents of the meta box.
*/
function metabox_callback() {
$this->setup_object_data( get_post() );
$this->render_form();
}
/**
* The object's data is stored within this UI/form handler object.
* This keeps all business logic flowing through the manager by default,
* and doesn't require customizing a field for each different object
* type. Business logic can easily be overridden at the Field object level.
*
* @param WP_Post $object
*/
function setup_object_data( $object ) {
$this->object_data = $object;
}
/**
* Render fields' input elements.
*/
function render_form() {
wp_nonce_field( $this->name, $this->name );
foreach ( $this->fields() as $field ) {
$field->render_input_element();
}
}
/**
* save_post handler
*/
function save( $post_id, $post ) {
// Setup object data
$this->setup_object_data( $post );
if ( wp_is_post_revision( $post_id ) )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
foreach ( $this->fields() as $field ) {
$field->save_field( $field );
}
}
/**
* Default save method specific to a post type.
*/
function save_field( $field ) {
if ( ! $field->authorization( $field ) ) {
return false;
}
$value = $field->sanitize( $field->post_value() );
update_post_meta( $this->object_data->ID, $field->name, $value );
}
/**
* Relate a field to the post meta box.
* @param string $field_name
*/
function add_field( $field_name ) {
$field = WP_Field_Manager::get_field( 'post', $field_name );
$field->container = $this;
$this->fields[] = $field;
}
/**
* Get field objects related to the post meta box.
*/
function fields() {
return $this->fields;
}
/**
* Default get method specific to a post type.
*
* @param string $field_name
*
* @return mixed
*/
function value( $field_name ) {
return get_post_meta( $this->object_data->ID, $field_name, true );
}
/**
* Default authorization callback for post meta.
*
* @param Field $field Field object.
*
* @return bool Authorization yay or nay.
*/
function authorization( $field ) {
return current_user_can( 'edit_post_meta', $this->object_data->ID, $field->name );
}
}
/**
* A field class defines a general field type. It holds business logic
* that may be specific to that field type, as well as view rendering.
*
* An instance of a field class defines a singular field, which relates
* to one or many object types (post types, users, etc.).
*
* Business logic can be overridden at the object level, but by default
* defers to the form object.
*/
class Field {
/**
* Unique identifier
*
* @var string
*/
var $name;
/**
* Label
*
* @var string
*/
var $label;
/**
* Type of object this field relates to.
*
* @var string
*/
var $object_type;
/**
* Form object.
*
* @var TBD
*/
var $container;
function __construct( $args = array() ) {
$keys = array_keys( get_class_vars( __CLASS__ ) );
foreach ( $keys as $key ) {
if ( isset( $args[ $key ] ) )
$this->$key = $args[ $key ];
}
}
/**
*
*/
function sanitize( $value ) {
return $value;
}
/**
* Authorization
* @return [type] [description]
*/
function authorization() {
return $this->container->authorization( $this );
}
function value() {
return $this->container->value( $this->name );
}
/**
* Retrieve a user-submitted value for a field.
*
* This may need to call the field object/class, depending how
* $_POST variable names are built.
*/
function post_value() {
return $_POST[$this->name];
}
function save_field() {
return $this->container->save_field( $this );
}
}
/**
* A textfield class.
*/
class Field_text extends Field {
function render_input_element() {
?>
<label>
<span class="field-title"><?php echo esc_html( $this->label ); ?></span>
<input type="text"
name="<?php echo $this->name ?>"
value="<?php echo $this->value() ?>">
</label> <?php
}
}
/**
* A manager for registering fields.
*/
class WP_Field_Manager {
/**
* [$fields description]
* @var [type]
*/
static $fields;
/**
* Field registration.
*
* Abstracted from the UI view class, so that the field's business logic
* and data describing it can be accessed independently.
*
* Data about fields is stored in a nested array.
*/
public static function add_field( $args ) {
$defaults = array(
'name' => null, // unique identifier
'object_types' => array(), // WP objects (post, page, user, etc.) this field applies to
'form_object_type' => 'text'
);
$args = wp_parse_args( $args, $defaults );
self::_set_field( $args );
}
public static function get_field( $object_type, $field_name ) {
if ( isset( self::$fields[$object_type][$field_name] ) )
return self::$fields[$object_type][$field_name];
}
public static function get_fields( $object_type ) {
if ( ! empty( self::$fields[$object_type] ) )
return self::$fields[$object_type];
}
/**
* Internally store field data.
*
* @param array $args {
* Field options.
*
* @type string $name Unique identifier.
* @type array $object_types WordPress objects the field applies to.
* @type string $form_object_type Form object type.
* @type callback $authorization_callback
* @type callback $sanitization_callback
* }
*/
protected static function _set_field( $args ) {
foreach ( $args['object_types'] as $object_type ) {
$classname = sprintf( "Field_%s", $args['form_object_type'] );
self::$fields[$object_type][$args['name']] = new $classname( $args );
}
}
}
/**
* Example Developer Usage
*/
add_action( 'init', 'register_custom_fields_and_form_containers' );
/**
* Register custom fields and an example form contianer.
*/
function register_custom_fields_and_form_containers() {
WP_Field_Manager::add_field( array(
'object_types' => array( 'post', 'page' ),
'name' => 'background_color',
'type' => 'text', // Relates to a pre-defined class.
'label' => 'Background Color'
) );
// Insantiate a container.
$container = new WP_Post_Meta_Box_Manager( array(
'title' => 'Post Details'
) );
// Relate a registered field to the container.
$container->add_field( 'background_color' );
}