This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleMetabox.class.php
272 lines (240 loc) · 7.74 KB
/
SimpleMetabox.class.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
<?php
/**
* Created by PhpStorm.
* User: etessore
* Date: 24/07/2014
* Time: 15:35
*/
class SimpleMetabox {
public $metabox;
public $fields;
public $metaname;
/**
* Builds the object
* @param string $metaname the post meta name where WordPress will store the infos
* @param array $metabox metabox properties array('id'=>'','title'=>'', 'post_type'=>'', 'context'=>'', 'priority'=>'')
* @param array $fields list of fields array of array('id'=>'changeme', 'label'=>'Change Me', 'type'=>'')
*/
public function __construct($metaname, $metabox, $fields) {
$this->metabox = array_merge(
array(
'id' => $metaname,
'title' => '',
'post_type' => '',
'context' => 'normal',
'priority' => 'high',
), $metabox
);
$this->fields = $fields;
$this->metaname = $metaname;
wp_register_script(
'wpu-media-manager-2', get_template_directory_uri() . '/js/media-manager-2.js', array(
'jquery',
'media-editor'
), '1.0.0', true
);
add_action(
'admin_print_scripts', array(
__CLASS__,
'enqueue_assets'
)
);
}
public static function enqueue_assets() {
wp_enqueue_script('wpu-media-manager-2');
}
/**
* Prints the metabox markup
* @param $post the current post
* TODO: support for other input types and select :D
*/
public function metabox_html($post) {
wp_nonce_field($this->metaname, $this->metaname . '_nonce');
$values = $this->get_meta($post->ID);
$rows = '';
foreach ($this->fields as $field) {
$th = HtmlHelper::standard_tag('th', $field['label'], array('class' => ''));
switch ($field['type']) {
case 'callback':
$input = $this->_markup_callback($field, $values, $post);
break;
case 'select':
$input = $this->_markup_select($field, $values);
break;
case 'single-attachment':
$input = $this->_markup_single_attachment($field, $values);
break;
case 'chebox-list':
$input = $this->_markup_checkbox_list($field, $values);
break;
case 'checkbox':
$input = $this->_markup_checkbox($field, $values);
break;
case 'text':
default:
$input = $this->_markup_text($field, $values);
break;
}
if (!empty($field['description'])) {
$input .= HtmlHelper::br() . HtmlHelper::paragraph(
$field['description'], array('class' => 'description')
);
}
$td = HtmlHelper::standard_tag('td', $input, array('class' => ''));
$tr = HtmlHelper::standard_tag('tr', $th . "\n" . $td);
$rows .= $tr;
}
echo HtmlHelper::standard_tag(
'table', HtmlHelper::standard_tag('tbody', $rows), array('class' => 'form-table')
);
}
public function _markup_callback($field, $values, $post){
if (is_callable($field['options']['callback'])) {
$box = isset($field['options']['box']) ? $field['options']['box'] : array();
$input = call_user_func_array(
$field['options']['callback'],
array($post, $box, $values)
);
}
return $input;
}
public function _markup_select($field, $values){
return HtmlHelper::select(
$this->metaname . '[' . $field['id'] . ']', $field['options'],
array('value' => $values[$field['id']])
);
}
public function _markup_single_attachment($field, $values){
$thumb = '';
if (is_numeric($values[$field['id']])) {
$thumb = wp_get_attachment_link($values[$field['id']], 'thumbnail', false, true, false);
}
$button_text = isset($field['parms']['data-button-text']) ? $field['parms']['data-button-text'] : __('Select from Media');
$input = $thumb . HtmlHelper::input(
'', 'button', array_merge(
array(
'value' => $button_text,
'class' => 'media-single-attachment',
'data-target-id' => $this->metaname . '-' . $field['id'],
'data-thumb-id' => $this->metaname . '-' . $field['id'] . '-thumb'
), (array)$field['parms']
)
) . HtmlHelper::input(
$this->metaname . '[' . $field['id'] . ']', 'hidden', //todo: change to hidden?
array_merge(
array(
'id' => $this->metaname . '-' . $field['id'],
'value' => $values[$field['id']],
'class' => 'large-text'
), (array)$field['parms']
)
);
return $input;
}
public function _markup_checkbox_list($field, $values){
$input = '';
if (count($field['options'])) {
foreach ($field['options'] as $key => $option) {
$input .= HtmlHelper::input(
$this->metaname . '[' . $field['id'] . '][' . $key . ']',
'checkbox',
array(
'id' => 'chebox-list-' . $field['id'] . '-' . $key,
'class' => '',
'checked' => empty($values[$field['id']][$key]) ? '' : 'checked'
)
) . PHP_EOL . HtmlHelper::label(
$option, 'chebox-list-' . $field['id'] . '-' . $key
) . PHP_EOL . HtmlHelper::br();
}
}
return $input;
}
public function _markup_checkbox($field, $values){
return HtmlHelper::input(
$this->metaname . '[' . $field['id'] . ']',
$field['type'],
array(
'class' => '',
'checked' => empty($values[$field['id']]) ? '' : 'checked'
)
);
}
public function _markup_text($field, $values){
return HtmlHelper::input(
$this->metaname . '[' . $field['id'] . ']',
$field['type'],
array_merge(
array(
'value' => $values[$field['id']],
'class' => 'large-text'
),
(array)$field['parms']
)
);
}
/**
* Registers the metabox on WordPress
*/
public function register_metaboxes() {
add_meta_box(
$this->metabox['id'], $this->metabox['title'], array(
&$this,
'metabox_html'
), $this->metabox['post_type'], $this->metabox['context'], $this->metabox['priority']
);
}
/**
* Saves the metabox data while saving the page
*/
public function save_metabox_data($post_id) {
if (!isset($post_id))
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if (!isset($_POST['post_type']))
return $post_id;
// First we need to check if the current user is authorised to do this action.
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return;
}
else {
if (!current_user_can('edit_post', $post_id))
return;
}
// Secondly we need to check if the user intended to change this value.
wp_verify_nonce($_POST[$this->metaname . '_nonce'], $this->metaname);
// TODO: understand how to make this check working :|
//check_admin_referer($this->metaname, $this->metaname . '_nonce');
array_walk_recursive(
$_POST[$this->metaname], array(
__CLASS__,
'sanitize_r'
)
);
update_post_meta($post_id, $this->metaname, $_POST[$this->metaname]);
}
public static function sanitize_r(&$val, $key) {
$val = sanitize_text_field($val);
}
/**
* Retrieves the post meta content.
* If a field is not set or empty, the value of the array
* element with such key will be an empty string,
* just to avoid warnings on non illegal offsets
* @param $post_id
* @return array unserialized values for the meta
*/
public function get_meta($post_id = null) {
if (is_null($post_id))
$post_id = get_the_ID();
$values = get_post_meta($post_id, $this->metaname, true);
foreach ($this->fields as $field) {
if (empty($values[$field['id']])) {
$values[$field['id']] = '';
}
}
return $values;
}
}