Created
August 1, 2011 15:02
-
-
Save mildfuzz/1118296 to your computer and use it in GitHub Desktop.
MetaBox class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $MF_MetaFields; //global defined to store meta ID's | |
| class MetaBox{ | |
| protected $options = array(); //for storing all options | |
| protected $id; //unique string ID for naming field | |
| protected $title; //Box Title | |
| protected $posttype; //Array (list, of, post, types) to attach to | |
| protected $context;//Optional. normal, or side | |
| protected $priority = "low";//optional | |
| protected $prefix;//database prefix to avoid conflict | |
| protected $fields; /* Array of arrays or multiple fields. | |
| * Fields Excepted args | |
| * | |
| * array( | |
| * 'name' => 'Default', | |
| * 'desc' => "<p>this is a default metabox</p>", | |
| * 'id' => $this->prefix . 'date', | |
| * 'type' => 'text') | |
| */ | |
| function __construct($id, array $fields, $prefix, array $posttype = array('post','page'), $title = "Meta Box", $context = "normal",$priority = "low"){ | |
| if(!is_string($id) || !is_string($prefix) || !is_string($title) || !is_string($context) || !is_string($priority)) die ("Field should be string"); | |
| add_action('save_post', array(&$this, 'save')); | |
| $this->id = $id; | |
| $this->fields = $fields; | |
| $this->prefix = $prefix; | |
| $this->posttype = $posttype; | |
| $this->title = $title; | |
| $this->context = $context; | |
| $this->priority = $priority; | |
| $this->set_option(); | |
| $this->get_meta_boxes(); | |
| $this->add(); | |
| do_action("mf_meta_loaded"); | |
| } | |
| function add(){ | |
| foreach ($this->posttype as $posttype){ | |
| add_meta_box($this->id, $this->title, array(&$this, 'show'), $posttype, $this->context, $this->priority); | |
| } | |
| } | |
| function show(){ | |
| global $post; | |
| $echo = '<table class="form-table">'; | |
| foreach($this->fields as $field){ | |
| $echo .= '<tr><th style="width:20%"><label for="'. $field['id'].'">'.$field['name'].'</label></th><td>'; | |
| $meta = get_post_meta($post->ID, $field['id'], true); | |
| switch($field['type']){ | |
| case 'select': | |
| $this->option_check($field); | |
| $echo .= '<select name="'.$field['id'].'" id="'.$field['id'].'">'; | |
| foreach ($field['options'] as $k => $v) { | |
| $echo .= '<option value="'.$k.'" '.($meta == $k ? ' selected="selected"' : '').'>'.$v.'</option>'; | |
| } | |
| $echo .= '</select>'; | |
| break; | |
| case 'text': | |
| $echo .= '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.($meta ? $meta : $field['std']). '" size="30" style="width:15%" /><p>'.$field['desc'].'</p>'; | |
| break; | |
| case 'wide-text': | |
| $echo .= '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.($meta ? $meta : $field['std']). '" size="30" style="width:85%" /><p>'.$field['desc'].'</p>'; | |
| break; | |
| case 'toggle': | |
| $echo .= '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'"'.($meta ? ' checked="checked"' : '').' />'; | |
| break; | |
| case 'checkbox': | |
| $this->option_check($field); | |
| if (!$meta){ | |
| $meta = array(); | |
| } | |
| foreach ($field['options'] as $k=>$v){ | |
| $echo .= '<label for="'.$v.'">'.$v.'</label>'; | |
| $echo .= '<input type="checkbox" name="'.$field['id'].'[]"value="'.$k.'" id="'.$k.'"'.(in_array($k,$meta) ? ' checked="checked"' : '').' /><br />'; | |
| } | |
| break; | |
| case 'radio': | |
| $this->option_check($field); | |
| foreach ($field['options'] as $option) { | |
| $echo .= '<input type="radio" name="'.$field['id'].'" value="'.$option['value'].'"'.($meta == $option['value'] ? ' checked="checked"' : '').' />'.$option['name']; | |
| } | |
| break; | |
| $echo .= '<td></tr>'; | |
| } | |
| } | |
| $echo .= '</table>'; | |
| echo $echo; | |
| } | |
| protected function option_check($field, $message = '$field array must contain option array to use '){ | |
| if(!is_array($field['options']) || !isset($field['options']) || !$field['options']) die ($message.$field['type']); | |
| return true; | |
| } | |
| function save($post_id) { | |
| // check autosave | |
| if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
| return $post_id; | |
| } | |
| // check permissions | |
| if ('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->fields as $field) { | |
| $old = get_post_meta($post_id, $field['id'], true); | |
| $new = $_POST[$field['id']]; | |
| if($field['type'] == "checkbox"){ | |
| foreach($field['options'] as $k=>$v){ | |
| update_post_meta($post_id, $field['id'], $new); | |
| } | |
| return; | |
| } | |
| if ($new && $new != $old) { | |
| update_post_meta($post_id, $field['id'], $new); | |
| } elseif ('' == $new && $old) { | |
| delete_post_meta($post_id, $field['id'], $old); | |
| } | |
| } | |
| } | |
| static function connect_to_post_type($type, $alt_query = false){ | |
| if(!is_string($type)) die ("type must be string"); | |
| global $wpdb; | |
| $meta_array = array(); | |
| if($alt_query && is_string($alt_query)){ | |
| $query = $alt_query; | |
| } elseif($alt_query === false){ | |
| $query = "SELECT post_title, ID FROM $wpdb->posts WHERE post_type='$type' AND post_status ='publish'"; | |
| } else { | |
| die ("Query must be a string"); | |
| } | |
| $meta_query= $wpdb->get_results($query); | |
| foreach ($meta_query as $object){ | |
| $i = get_object_vars($object); | |
| $meta_array[$i['ID']]=$i['post_title']; | |
| } | |
| if (count($meta_array)<1) $meta_array[0]='No '.$type.' Found!';//*/ | |
| return $meta_array; | |
| } | |
| protected function set_option(){ | |
| $options = get_option('mf_meta_fields'); | |
| foreach($this->posttype as $type){//create options list | |
| foreach($this->fields as $field){ | |
| $this->options[$this->id][$type][] = $field['id']; | |
| } | |
| } | |
| if($options){ | |
| $this->options = array_merge($this->options, $options); | |
| update_option('mf_meta_fields',$this->options); | |
| } else { | |
| add_option('mf_meta_fields',$this->options); | |
| } | |
| } | |
| static function get_meta_boxes(){ | |
| global $MF_MetaFields; | |
| $MF_MetaFields = get_option('mf_meta_fields'); | |
| return $MF_MetaFields; | |
| } | |
| } | |
| function demo_define_meta_box(){ | |
| $id="program_meta";// | |
| $title = "Program Meta";//Box Title | |
| $page = array('Program');//Section to attach metbox to (page, post or custom) | |
| $context = 'side'; | |
| $priority = 'high'; | |
| $prefix = 'mf_SALF_meta_'; | |
| $fields = array( | |
| array( | |
| 'name' => 'Featured', | |
| 'id' => $prefix . 'featured', | |
| 'type' => 'toggle', | |
| 'desc' => "<p>Place in the Carousel. Limited spaces.</p>" | |
| ), | |
| array( | |
| 'name' => 'Price', | |
| 'id' => $prefix . 'price', | |
| 'type' => 'text' | |
| ), | |
| array( | |
| 'name' => 'Venue', | |
| 'id' => $prefix . 'venue', | |
| 'type' => 'select', | |
| 'options' => MetaBox::connect_to_post_type('post') | |
| ), | |
| array( | |
| 'name' => 'Event Type', | |
| 'id' => $prefix . 'type', | |
| 'many' => true, | |
| 'type' => 'select', | |
| 'options' => MetaBox::connect_to_post_type('Events') | |
| ), | |
| array( | |
| 'name' => 'Ticket ID', | |
| 'desc' => '<p>Enter Ticket ID</p>', | |
| 'id' => $prefix . 'ticket_ID', | |
| 'type' => 'wide-text' | |
| ), | |
| array( | |
| 'name' => 'Sponsor', | |
| 'id' => $prefix . 'sponsor', | |
| 'type' => 'checkbox', | |
| 'options' => MetaBox::connect_to_post_type('Sponsors') | |
| ) | |
| );//*/ | |
| $program_metabox = new MetaBox($id,$fields,$prefix); | |
| } | |
| function create_new_meta_boxes(){ | |
| //demo_define_meta_box(); | |
| } | |
| add_action('init',array('MetaBox','get_meta_boxes'));//load meta ID's and store in global array | |
| add_action('admin_menu','create_new_meta_boxes'); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment