-
Notifications
You must be signed in to change notification settings - Fork 1
/
Censeo_Post_Meta.php
191 lines (149 loc) · 5.32 KB
/
Censeo_Post_Meta.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
<?php
# TODO: add documentation
require_once(CENSEO_LIB . 'Censeo_Page.php');
require_once(CENSEO_LIB . 'Censeo_Fields.php');
class Censeo_Post_Meta {
protected $id;
protected $title;
protected $post_type;
protected $post_id;
protected $fields = array();
public function get_id() {
return $this->id;
}
public function get_title() {
return $this->title;
}
public function get_post_type() {
return $this->post_type;
}
public function get_post_id() {
return $this->post_id;
}
public function add_field(Censeo_Field $field) {
$this->fields[] = $field;
}
public function add_fields(array $fields) {
foreach ($fields as $field) {
$this->add_field($field);
}
}
public function set_fields(array $fields) {
$this->fields = array();
$this->add_fields($fields);
}
public function user_can_update() {
$user_can_update = true;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
$user_can_update = false;
}
if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $this->post_id)) {
$user_can_update = false;
}
} else {
if (!current_user_can('edit_post', $this->post_id)) {
$user_can_update = false;
}
}
return $user_can_update;
}
public function __construct($id, $title, $post_type = 'post') {
$this->id = sanitize_title_with_dashes($id);
$this->title = $title;
$this->post_type = $post_type;
add_action('add_meta_boxes', array(&$this, 'init'));
add_action('save_post', array(&$this, 'save_field_values'), 999);
add_action('censeo_post_meta_' . $this->get_id() . '_render', array(&$this, 'render_meta_fields'));
add_action('censeo_post_meta_' . $this->get_id() . '_before_fields_render', array(&$this, 'load_field_values'));
// If the action is removed then custom implementation of a nonce field should be added
// in order to avoid errors from the check_admin_referer() call
add_action('censeo_post_meta_' . $this->get_id() . '_hidden_fields', array(&$this, 'nonce'));
}
public function init() {
do_action('censeo_post_meta_before_init');
do_action('censeo_post_meta_' . $this->get_post_type() . '_before_init');
do_action('censeo_post_meta_' . $this->get_id() . '_before_init');
add_meta_box($this->get_id(), $this->get_title(), array(&$this, 'render'), $this->get_post_type());
do_action('censeo_post_meta_after_init');
do_action('censeo_post_meta_' . $this->get_post_type() . '_after_init');
do_action('censeo_post_meta_' . $this->get_id() . '_after_init');
}
public function nonce() {
wp_nonce_field($this->get_nonce_action(), $this->get_nonce_name());
}
public function get_nonce_action() {
return apply_filters('censeo_post_meta_nonce_action', 'save_' . $this->get_id() . '_meta', $this->get_id());
}
public function get_nonce_name() {
return apply_filters('censeo_post_meta_nonce_name', $this->get_id() . '_nonce', $this->get_id());
}
public function get_field_value(&$field, $post_id, $meta_key, $single = false) {
$field_value = '';
$post_id = absint($post_id);
$raw_meta = get_post_meta($post_id);
if (!isset($raw_meta[$meta_key])) {
$field_value = $field->get_default_value();
} else {
$field_value = get_post_meta($post_id, $meta_key, $single);
}
return $field_value;
}
public function load_field_values() {
foreach ($this->fields as &$field) {
$value = $this->get_field_value($field, $this->post_id, $field->get_name(), true);
$field->set_value($value);
}
}
public function render($post) {
$this->post_id = $post->ID;
do_action('censeo_post_meta_render');
do_action('censeo_post_meta_' . $this->get_post_type() . '_render');
do_action('censeo_post_meta_' . $this->get_id() . '_render');
}
public function save_field_values($post_id) {
$this->post_id = $post_id;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && check_admin_referer($this->get_nonce_action(), $this->get_nonce_name())) {
if ($this->user_can_update()) {
foreach ($this->fields as &$field) {
$field->load_value();
$key = $field->get_name();
$value = $field->get_value();
update_post_meta($this->get_post_id(), $key, $value);
if ($field->get_multiple_values() && !is_scalar($value)) {
foreach ($value as $prop => $prop_value) {
update_post_meta($this->get_post_id(), $key . '_' . $prop, $prop_value);
}
}
}
// wp_redirect(add_query_arg('censeo-updated', 1));
// exit;
}
}
}
public function render_meta_fields() {
do_action('censeo_post_meta_before_fields_render');
do_action('censeo_post_meta_' . $this->get_post_type() . '_before_fields_render');
do_action('censeo_post_meta_' . $this->get_id() . '_before_fields_render');
?>
<div class="censeo-form censeo-post-meta-form">
<?php
foreach ($this->fields as $field) {
echo $field->render();
}
?>
<div class="censeo-field-row">
<?php
do_action('censeo_post_meta_hidden_fields');
do_action('censeo_post_meta_' . $this->get_post_type() . '_hidden_fields');
do_action('censeo_post_meta_' . $this->get_id() . '_hidden_fields');
?>
</div>
</form>
<?php
do_action('censeo_post_meta_after_fields_render');
do_action('censeo_post_meta_' . $this->get_post_type() . '_after_fields_render');
do_action('censeo_post_meta_' . $this->get_id() . '_after_fields_render');
}
}
?>