-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSmartMetaBox.php
More file actions
126 lines (109 loc) · 3.6 KB
/
SmartMetaBox.php
File metadata and controls
126 lines (109 loc) · 3.6 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
<?php
/**
* Meta box generator for WordPress
* Compatible with custom post types
*
* Support input types: text, textarea, checkbox, select, radio
*
* @author: Nikolay Yordanov <[email protected]> http://nyordanov.com
* @version: 1.0
*
*/
class SmartMetaBox {
protected $meta_box;
protected $id;
static $prefix = '_smartmeta_';
// create meta box based on given data
public function __construct($id, $opts) {
if (!is_admin()) return;
$this->meta_box = $opts;
$this->id = $id;
add_action('add_meta_boxes', array(&$this,
'add'
));
add_action('save_post', array(&$this,
'save'
));
}
// Add meta box for multiple post types
public function add() {
foreach ($this->meta_box['pages'] as $page) {
add_meta_box($this->id, $this->meta_box['title'], array(&$this,
'show'
) , $page, $this->meta_box['context'], $this->meta_box['priority']);
}
}
// Callback function to show fields in meta box
public function show($post) {
// Use nonce for verification
echo '<input type="hidden" name="' . $this->id . '_meta_box_nonce" value="', wp_create_nonce('smartmetabox' . $this->id) , '" />';
echo '<table class="form-table">';
foreach ($this->meta_box['fields'] as $field) {
extract($field);
$id = self::$prefix . $id;
$value = self::get($field['id']);
if (empty($value) && !sizeof(self::get($field['id'], false))) {
$value = isset($field['default']) ? $default : '';
}
echo '<tr>', '<th style="width:20%"><label for="', $id, '">', $name, '</label></th>', '<td>';
include "smart_meta_fields/$type.php";
if (isset($desc)) {
echo ' <span class="description">' . $desc . '</span>';
}
echo '</td></tr>';
}
echo '</table>';
}
// Save data from meta box
public function save($post_id) {
// verify nonce
if (!isset($_POST[$this->id . '_meta_box_nonce']) || !wp_verify_nonce($_POST[$this->id . '_meta_box_nonce'], 'smartmetabox' . $this->id)) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('post' == $_POST['post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
foreach ($this->meta_box['fields'] as $field) {
$name = self::$prefix . $field['id'];
$sanitize_callback = (isset($field['sanitize_callback'])) ? $field['sanitize_callback'] : '';
if (isset($_POST[$name]) || isset($_FILES[$name])) {
$old = self::get($field['id'], true, $post_id);
$new = $_POST[$name];
if ($new != $old) {
self::set($field['id'], $new, $post_id, $sanitize_callback);
}
} elseif ($field['type'] == 'checkbox') {
self::set($field['id'], 'false', $post_id, $sanitize_callback);
} else {
self::delete($field['id'], $name);
}
}
}
static function get($name, $single = true, $post_id = null) {
global $post;
return get_post_meta(isset($post_id) ? $post_id : $post->ID, self::$prefix . $name, $single);
}
static function set($name, $new, $post_id = null, $sanitize_callback = '') {
global $post;
$id = (isset($post_id)) ? $post_id : $post->ID;
$meta_key = self::$prefix . $name;
$new = ($sanitize_callback != '' && is_callable($sanitize_callback)) ? call_user_func($sanitize_callback, $new, $meta_key, $id) : $new;
return update_post_meta($id, $meta_key, $new);
}
static function delete($name, $post_id = null) {
global $post;
return delete_post_meta(isset($post_id) ? $post_id : $post->ID, self::$prefix . $name);
}
};
function add_smart_meta_box($id, $opts) {
new SmartMetaBox($id, $opts);
}