Skip to content

Commit

Permalink
added select input type support and some fixes to nonces verification…
Browse files Browse the repository at this point in the history
… system
  • Loading branch information
Emanuele Tessore committed Sep 24, 2014
1 parent 1078003 commit c2ab7b1
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions SimpleMetabox.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class SimpleMetabox {
public function __construct($metaname, $metabox, $fields) {
$this->metabox = array_merge(
array(
'id' => $metaname,
'title' => '',
'id' => $metaname,
'title' => '',
'post_type' => '',
'context' => 'advanced',
'priority' => 'default',
'context' => 'normal',
'priority' => 'high',
),
$metabox
);
Expand All @@ -38,14 +38,23 @@ public function __construct($metaname, $metabox, $fields) {
* TODO: support for other input types and select :D
*/
public function metabox_html($post) {
wp_nonce_field(__FILE__, $this->metaname . '_nonce');
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 'select':
$input = HtmlHelper::select(
$this->metaname . '[' . $field['id'] . ']',
$field['options'],
array('value' => $values[$field['id']])
);
break;

case 'chebox-list':
$input = '';
if (count($field['options'])) {
Expand All @@ -54,8 +63,8 @@ public function metabox_html($post) {
$this->metaname . '[' . $field['id'] . '][' . $key . ']',
'checkbox',
array(
'id' => 'chebox-list-' . $field['id'] . '-' . $key,
'class' => '',
'id' => 'chebox-list-' . $field['id'] . '-' . $key,
'class' => '',
'checked' => empty($values[$field['id']][$key]) ? '' : 'checked'
)
) . PHP_EOL
Expand All @@ -70,7 +79,7 @@ public function metabox_html($post) {
$this->metaname . '[' . $field['id'] . ']',
$field['type'],
array(
'class' => '',
'class' => '',
'checked' => empty($values[$field['id']]) ? '' : 'checked'
)
);
Expand Down Expand Up @@ -130,12 +139,9 @@ public function save_metabox_data($post_id) {
}

// Secondly we need to check if the user intended to change this value.
if (
!isset($_POST[$this->metaname . '_nonce'])
|| !wp_verify_nonce($_POST[$this->metaname . '_nonce'], __FILE__)
) {
return;
}
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'));

Expand Down Expand Up @@ -166,8 +172,8 @@ public function get_meta($post_id = null) {

return $values;

v($this->fields);
v(get_post_meta($post_id, $this->metaname, true));
//v($this->fields);
//v(get_post_meta($post_id, $this->metaname, true));
return array_merge($this->fields, (array)get_post_meta($post_id, $this->metaname, true));
}
}

0 comments on commit c2ab7b1

Please sign in to comment.