meta = null;
$this->postid = null;
$this->template = null;
}
public function init_meta() {
add_action('admin_init',array(&$this, 'add_meta'));
}
public function add_meta() {
if ($this->meta['template'] == $this->template) {
add_meta_box($this->meta['id'], $this->meta['title'], array(&$this, 'format_meta'), $this->meta['post_type'], $this->meta['context'], $this->meta['priority']);
}
}
public function format_meta() {
global $post;
if (!isset($post)) return false;
if ($this->meta['post_type'] != $post->post_type) return false;
$this->set_postid($post->ID);
// Use nonce for verification
echo '';
foreach ($this->meta['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
if (isset($field['before']) && $field['before'] != "") {
echo '
' . $field['before'] . '
';
}
echo '
';
if (isset($field['desc']) && $field['desc'] != "") {
echo '
' . $field['desc'] . '
';
}
if ( $field['type'] == 'radio' )
echo '
';
switch ($field['type']) {
case 'text':
echo ''. ' ';
break;
case 'textarea':
echo ''. ' ';
break;
case 'select':
echo '';
break;
case 'radio':
foreach ($field['options'] as $option) {
// echo '' . $option['name'] . ' ';
echo '';
}
break;
case 'checkbox':
echo '';
break;
}
if ( $field['type'] == 'radio' )
echo '
';
// echo '';
}
}
public function save_meta_data($post_id) {
global $post;
if (!isset($post->ID)) {
$post = get_post($post_id);
}
$this->set_postid($post->ID);
$this->meta = $this->get_meta();
if ($this->meta['post_type'] != $post->post_type) return $post_id;
$_POST[$this->meta['nonce_name']] = isset($_POST[$this->meta['nonce_name']]) ? $_POST[$this->meta['nonce_name']] : "";
//Verify nonce
if (!wp_verify_nonce($_POST[$this->meta['nonce_name']], basename(__FILE__))) {
return $post_id;
}
//Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
//Check permissions
if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($this->meta['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = isset($_POST[$field['id']]) ? $_POST[$field['id']] : '';
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
public function set_postid($axPostId = 0) {
$postID = null;
$postID2 = null;
if ($axPostId > 0) {
$this->postid = $axPostId;
} else {
if ( isset( $_GET['post'] ) ) {
$postID = filter_var( $_GET['post'], FILTER_VALIDATE_INT );
if ( isset( $_POST['post_ID'] ) ) {
$postID2 = filter_var( $_POST['post_ID'], FILTER_VALIDATE_INT );
} else {
$postID2 = '';
}
$this->postid = $postID ? $postID : $postID2 ;
}
}
}
public function get_postid() {
return $this->postid;
}
public function set_template() {
$this->template = get_post_meta($this->postid,'_wp_page_template',TRUE);
}
public function get_template() {
return $this->template;
}
public function set_meta($axMeta) {
update_post_meta($this->postid, '_arixwp_field_meta', serialize($axMeta));
$this->meta = $axMeta;
}
public function get_meta() {
if (is_null(($this->meta))) {
$this->meta = unserialize(get_post_meta($this->postid,'_arixwp_field_meta',TRUE));
}
return $this->meta;
}
}