Repository navigation
Expand file tree
/
Copy pathmetabox.class.php
More file actions
429 lines (321 loc) · 12.5 KB
/
Copy pathmetabox.class.php
File metadata and controls
429 lines (321 loc) · 12.5 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
class MetaBox
{
// Stores all metabox info
private static $boxs = array();
// List of unique names and ids
private static $ids = array();
private static $names = array();
// Used to retrieve all stored metainformation
private static $meta_name = 'MetaBox';
// The ID of the metabox attached to this instance of the class
private $box;
// Temporary option storage. Associative array.
// All instanced helper methods access this variable.
private $opts;
function __construct( $id, $type = 'page', $location = 'side', $priority = 'high' ){
$this->opts = array(
'title' => $id,
'type' => $type,
'location' => $location,
'priority' => $priority,
'fields' => array()
);
if( !$this->extend($id) )
$this->opts['id'] = self::slug($id);
$this->addBox();
}
// OBJECT HELPERS
// Method used to extend the options array with user
// defined parameters.
private function extend( $map ){
// Exit if parameter is a string or nonexistent
if( is_string( $map ) || !$map )
return false;
// Overrite the default options
foreach( $map as $key => $val )
$this->opts[$key] = $val;
return true;
}
// Validate metabox info and add to array
private function addBox(){
$this->valid('id');
$this->unique('id');
$this->box = $this->opts['id'];
self::$boxs[$this->box] = $this->opts;
}
// Validate field info and add to array
private function addField(){
$this->valid('id');
$this->unique('id');
$this->valid('name');
$this->unique('name');
self::$boxs[$this->box]['fields'][] = $this->opts;
}
// Validate slug and alert user if correction needs to be made
private function valid( $param ){
$value = $this->opts[$param];
$slug = self::slug( $value );
if( $value != $slug )
die( "'{$value}' contains illegal characters.<br>\nTry '{$slug}' instead." );
}
// Validate the uniqueness of a name and alert user if a correction needs to be made
private function unique( $param ){
eval('$list = self::$'.$param.'s;');
$value = $this->opts[$param];
foreach( $list as $entry )
if( $value == $entry ) die( "The ID '{$value}' already exists." );
eval('self::$'.$param.'s[] = $value;');
}
// CLASS HELPER FUNCTIONS
// Creates a valid slug
private static function slug( $string ){
return strtolower( preg_replace( '/[^A-Za-z0-9-]+/', '-', $string ) );
}
// CLASS METHODS
// Jump starts the MetaBox class by adding appropriate actions
public static function init(){
foreach( self::$boxs as $id => $box ){
add_meta_box($box['id'], $box['title'], 'MetaBox::display', $box['type'], $box['location'], $box['priority'], $box['location']);
}
add_action('save_post', 'MetaBox::save');
add_action('admin_head', 'MetaBox::loadScripts');
}
// Method called by Wordpress to display the corresponding metabox
public static function display( $post = false, $box = false ){
$id = $box['id'];
$location = $box['args'];
$fields = self::$boxs[$id]['fields'];
$nonce = wp_create_nonce( basename(__FILE__) );
echo "<input type='hidden' name='{$id}_nonce' value='{$nonce}' />";
echo "<table class='form-table'>";
$meta = get_post_meta( $post->ID, self::$meta_name, true );
foreach( $fields as $field ){
$value = $meta[ $field['name'] ];
$call_show_func = 'self::show'.$field['type'];
echo "<tr><th style='padding:10px 0 2px'><label for='{$field['id']}'>{$field['title']}</label><div style='padding: 4px 0 0 8px;font-size:10px'>{$field['desc']}</div></th></tr><tr><td style='padding:0'>";
call_user_func( $call_show_func, $field, $value );
echo "</td></tr>";
}
echo "</table>";
}
// Method called by Wordpress upon post save
public static function save( $post_id = false, $post = false ){
// check autosave
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// check permissions
/* ADD CODE HERE */
$meta = get_post_meta( $post_id, self::$meta_name, true );
foreach( self::$boxs as $id => $box ){
if( $_POST['post_type'] == $box['type'] ) {
foreach( $box['fields'] as $field ){
$name = $field['name'];
$old = $meta[$name];
$new = $_POST[$name];
if( $new && $new != $old )
$meta[$name] = $new;
}
}
}
update_post_meta( $post_id, self::$meta_name, $meta );
return $post_id;
}
public static function loadScripts(){
?>
<script type='text/javascript'>
(function($){
$(function(){
$sortable = $('#sortable');
$('input.metabox-upload').click( function(){
var id = $(this).data('target');
tb_show('', 'media-upload.php?type=image&TB_iframe=1');
window.send_to_editor = function(html){
var $img = $('img', html),
url = $img.attr('src'),
$clone = $sortable.find('li').first().clone();
$clone.find('img').attr({src: url});
$sortable.append( $clone );
$clone.find('input').val( url );
$(id).val(url);
tb_remove();
};
});
$sortable.sortable({
placeholder: 'ui-state-highlight',
forcePlaceholderSize: true
});
});
})(jQuery);
</script>
<style>
#sortable li {width:100px;float:left;background:#ddd;padding:2px;margin:5px 5px;border:3px solid #ddd}
#sortable img {width:100%;height:auto}
.metabox-image-url {width:100px;border:none;margin:0}
.fix {height:1px;clear:both}
</style>
<?php
}
//
// ADD SUPPORTED FIELDS HERE
//
// ----------------------------
// TEXT FIELD
public function text( $id, $desc = '' ){
$this->opts = array(
'type' => 'text',
'title' => $id,
'desc' => $desc,
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showText( $field, $meta ){
echo "<input id='{$field['id']}' name='{$field['name']}' type='text' value='{$meta}' style='width:97%' />";
}
// CHECKBOX FIELD
public function checkbox( $id, $desc = '' ){
$this->opts = array(
'type' => 'checkbox',
'title' => $id,
'desc' => $desc
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showCheckbox( $field, $meta ){
$checked = $meta ? "checked" : "";
echo "<input id='{$field['id']}' name='{$field['name']}' type='checkbox' {$checked}>";
}
// EDITOR FIELD
public function editor( $id, $desc = '' ){
$this->opts = array(
'type' => 'editor',
'title' => $id,
'desc' => $desc,
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showEditor( $field, $meta ){
echo "<div style='width:97%;border:1px solid #DFDFDF;background:#fff'><textarea id='{$field['id']}' name='{$field['name']}' class='theEditor' style='width:97%'>{$meta}</textarea></div>";
}
// TEXTAREA FIELD
public function textarea( $id, $desc = '' ){
$this->opts = array(
'type' => 'textarea',
'title' => $id,
'desc' => $desc,
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showTextarea( $field, $meta ){
echo "<div style='width:100%'><textarea id='{$field['id']}' name='{$field['name']}' style='width:97%'>{$meta}</textarea></div>";
}
// UPLOAD FIELD
public function upload( $id, $desc = '' ){
$this->opts = array(
'type' => 'upload',
'title' => $id,
'desc' => $desc,
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showUpload( $field, $meta ){
$len = count($meta);
echo "<ul id='sortable'>";
for( $i = 0; $i < $len; $i++ ){
if( $i != $len && $meta[$i] == null )
unset( $meta[$i] );
else
echo "<li><img src='{$meta[$i]}'><br><input type='text' name='{$field['name']}[]' class='metabox-image-url' value='{$meta[$i]}'></li>";
}
echo "</ul>";
echo "<div class='fix'></div>";
echo "<input id='{$field['id']}' name='{$field['name']}[]' style='width:50%;margin-bottom:5px' />";
echo "<script>document.write(\"<input type='button' class='button-primary metabox-upload' data-target='#{$field['id']}' value='Upload'/>\");</script>";
}
// RADIO FIELD
public function radio( $id, $options = array(), $desc = '' ){
$this->opts = array(
'type' => 'radio',
'title' => $id,
'desc' => $desc,
'options' => $options
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showRadio( $field, $meta ){
echo "<div id='{$field['id']}'>";
foreach( $field['options'] as $option => $value ){
$checked = $meta == $value ? 'checked' : '';
echo "<input type='radio' name='{$field['name']}' value='{$value}' {$checked}>{$option}<br>";
}
echo "</div>";
}
// SELECT FIELD
public function select( $id, $options = array(), $selected = array(), $desc = '' ){
$this->opts = array(
'type' => 'select',
'title' => $id,
'desc' => $desc,
'options' => $options,
'selected' => $selected,
'size' => 1,
'multiple' => count( $selected ),
'disabled' => array(),
'tabindex' => false
);
if( !$this->extend($id) ) {
$this->opts['id'] = self::slug($id);
$this->opts['name'] = self::slug($id);
}
$this->addField();
}
private static function showSelect( $field, $meta ){
$multiple = $field['multiple'] ? 'multiple' : '';
echo "<select id='{$field['id']}' name='{$field['name']}' size='{$field['size']}' {$multiple}>";
foreach( $field['options'] as $option ){
$selected = $option == $meta ? 'selected' : '';
foreach( $field['selected'] as $select ){
if( $select == $option )
$selected = 'selected';
}
echo "<option {$selected}>{$option}</option>";
}
echo "</select>";
}
}
add_action('admin_menu', 'MetaBox::init');
/*
$meep = new MetaBox('Test Box', 'page', 'advanced');
$meep->text('My Text');
$meep->editor('My Editor');
$meep->textarea('My Textarea');
$meep->radio('My Radio', array( 'test' => 'test', 'blah' => 'blah' ) );
$meep->checkbox('My Checkbox');
$meep->select('My Select', array( 'ford', 'toyota', 'chevy' ) );
$meep->upload('My Upload');
*/
?>