_meta_box = $meta_box; $this->_fields = & $this->_meta_box['fields']; $this->add_missed_values(); add_action('admin_menu', array(&$this, 'add')); // add meta box add_action('save_post', array(&$this, 'save')); // save meta box's data // check for some special fields and add needed actions for them $this->check_field_upload(); } /******************** BEGIN UPLOAD **********************/ // Check field upload and add needed actions function check_field_upload() { if ($this->has_field('image') || $this->has_field('file')) { add_action('post_edit_form_tag', array(&$this, 'add_enctype')); // add data encoding type for file uploading add_action('admin_head-post.php', array(&$this, 'add_script_upload')); // add scripts for handling add/delete images add_action('admin_head-post-new.php', array(&$this, 'add_script_upload')); add_action('delete_post', array(&$this, 'delete_attachments')); // delete all attachments when delete post } } // Add data encoding type for file uploading function add_enctype() { echo ' enctype="multipart/form-data"'; } // Add scripts for handling add/delete images function add_script_upload() { echo ' '; } // Delete all attachments when delete post function delete_attachments($post_id) { $attachments = get_posts(array( 'numberposts' => -1, 'post_type' => 'attachment', 'post_parent' => $post_id )); if (!empty($attachments)) { foreach ($attachments as $att) { wp_delete_attachment($att->ID); } } } /******************** END UPLOAD **********************/ /******************** BEGIN META BOX PAGE **********************/ // Add meta box for multiple post types function add() { foreach ($this->_meta_box['pages'] as $page) { add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']); } } // Callback function to show fields in meta box function show() { global $post; wp_nonce_field(basename(__FILE__), 'rw_meta_box_nonce'); echo ''; foreach ($this->_fields as $field) { $meta = get_post_meta($post->ID, $field['id'], !$field['multiple']); $meta = !empty($meta) ? $meta : $field['std']; echo ''; // call separated methods for displaying each type of field call_user_func(array(&$this, 'show_field_' . $field['type']), $field, $meta); echo ''; } echo '
'; } /******************** END META BOX PAGE **********************/ /******************** BEGIN META BOX FIELDS **********************/ function show_field_begin($field, $meta) { echo ""; } function show_field_end($field, $meta) { echo "
{$field['desc']}"; } function show_field_text($field, $meta) { $this->show_field_begin($field, $meta); echo ""; $this->show_field_end($field, $meta); } function show_field_textarea($field, $meta) { $this->show_field_begin($field, $meta); echo ""; $this->show_field_end($field, $meta); } function show_field_select($field, $meta) { if (!is_array($meta)) $meta = (array) $meta; $this->show_field_begin($field, $meta); echo ""; $this->show_field_end($field, $meta); } function show_field_radio($field, $meta) { $this->show_field_begin($field, $meta); foreach ($field['options'] as $key => $value) { echo " $value "; } $this->show_field_end($field, $meta); } function show_field_checkbox($field, $meta) { $this->show_field_begin($field, $meta); echo " {$field['desc']}"; } function show_field_wysiwyg($field, $meta) { $this->show_field_begin($field, $meta); echo ""; $this->show_field_end($field, $meta); } function show_field_file($field, $meta) { global $post; if (!is_array($meta)) $meta = (array) $meta; $this->show_field_begin($field, $meta); echo "{$field['desc']}
"; if (!empty($meta)) { // show attached files $attachs = get_posts(array( 'numberposts' => -1, 'post_type' => 'attachment', 'post_parent' => $post->ID )); $nonce = wp_create_nonce('rw_ajax_delete_file'); echo '
' . _('Uploaded files') . '
'; echo '
    '; foreach ($attachs as $att) { if (wp_attachment_is_image($att->ID)) continue; // what's image uploader for? $src = wp_get_attachment_url($att->ID); if (in_array($src, $meta)) { echo "
  1. " . wp_get_attachment_link($att->ID) . " (ID}!{$src}!{$nonce}'>Delete)
  2. "; } } echo '
'; } // show form upload echo "
" . _('Upload new files') . "
" . _('Add more file') . "
"; } function show_field_image($field, $meta) { global $post; if (!is_array($meta)) $meta = (array) $meta; $this->show_field_begin($field, $meta); echo "{$field['desc']}
"; if (!empty($meta)) { // show attached images $attachs = get_posts(array( 'numberposts' => -1, 'post_type' => 'attachment', 'post_parent' => $post->ID, 'post_mime_type' => 'image', // get attached images only 'output' => ARRAY_A )); $nonce = wp_create_nonce('rw_ajax_delete_file'); echo '
' . _('Uploaded images') . '
'; foreach ($attachs as $att) { $src = wp_get_attachment_image_src($att->ID, 'full'); $src = $src[0]; if (in_array($src, $meta)) { echo "

ID}!{$src}!{$nonce}'>Delete
"; } } } // show form upload echo "
" . _('Upload new images (Make sure to publish the post to save)') . "
"; } function show_field_color($field, $meta) { if (empty($meta)) $meta = '#'; $this->show_field_begin($field, $meta); echo " " . _('Select a color') . " "; $this->show_field_end($field, $meta); } function show_field_checkbox_list($field, $meta) { if (!is_array($meta)) $meta = (array) $meta; $this->show_field_begin($field, $meta); $html = array(); foreach ($field['options'] as $key => $value) { $html[] = " $value"; } echo implode('
', $html); $this->show_field_end($field, $meta); } function show_field_date($field, $meta) { $this->show_field_text($field, $meta); } function show_field_time($field, $meta) { $this->show_field_text($field, $meta); } /******************** END META BOX FIELDS **********************/ /******************** BEGIN META BOX SAVE **********************/ // Save data from meta box function save($post_id) { if (isset($_POST['post_type'])) { $post_type = $_POST['post_type']; } else { $post_type = 'null'; } $post_type_object = get_post_type_object($post_type); if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) // check autosave || (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID']) // check revision || (!in_array($_POST['post_type'], $this->_meta_box['pages'])) // check if current post type is supported || (!check_admin_referer(basename(__FILE__), 'rw_meta_box_nonce')) // verify nonce || (!current_user_can($post_type_object->cap->edit_post, $post_id))) { // check permission return $post_id; } foreach ($this->_fields as $field) { $name = $field['id']; $type = $field['type']; $old = get_post_meta($post_id, $name, !$field['multiple']); $new = isset($_POST[$name]) ? $_POST[$name] : ($field['multiple'] ? array() : ''); // validate meta value if (class_exists('RW_Meta_Box_Validate') && method_exists('RW_Meta_Box_Validate', $field['validate_func'])) { $new = call_user_func(array('RW_Meta_Box_Validate', $field['validate_func']), $new); } // call defined method to save meta value, if there's no methods, call common one $save_func = 'save_field_' . $type; if (method_exists($this, $save_func)) { call_user_func(array(&$this, 'save_field_' . $type), $post_id, $field, $old, $new); } else { $this->save_field($post_id, $field, $old, $new); } } } // Common functions for saving field function save_field($post_id, $field, $old, $new) { $name = $field['id']; // single value if (!$field['multiple']) { if ('' != $new && $new != $old) { update_post_meta($post_id, $name, $new); } elseif ('' == $new) { delete_post_meta($post_id, $name, $old); } return; } // multiple values // get new values that need to add and get old values that need to delete $add = array_diff($new, $old); $delete = array_diff($old, $new); foreach ($add as $add_new) { add_post_meta($post_id, $name, $add_new, false); } foreach ($delete as $delete_old) { delete_post_meta($post_id, $name, $delete_old); } } function save_field_textarea($post_id, $field, $old, $new) { $new = htmlspecialchars($new); $this->save_field($post_id, $field, $old, $new); } function save_field_wysiwyg($post_id, $field, $old, $new) { $new = wpautop($new); $this->save_field($post_id, $field, $old, $new); } function save_field_file($post_id, $field, $old, $new) { $name = $field['id']; if (empty($_FILES[$name])) return; $this->fix_file_array($_FILES[$name]); foreach ($_FILES[$name] as $position => $fileitem) { $file = wp_handle_upload($fileitem, array('test_form' => false)); if (empty($file['file'])) continue; $filename = $file['file']; $attachment = array( 'post_mime_type' => $file['type'], 'guid' => $file['url'], 'post_parent' => $post_id, 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_content' => '' ); $id = wp_insert_attachment($attachment, $filename, $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename)); add_post_meta($post_id, $name, $file['url'], false); // save file's url in meta fields } } } // Save images, call save_field_file, cause they use the same mechanism function save_field_image($post_id, $field, $old, $new) { $this->save_field_file($post_id, $field, $old, $new); } /******************** END META BOX SAVE **********************/ /******************** BEGIN HELPER FUNCTIONS **********************/ // Add missed values for meta box function add_missed_values() { // default values for meta box $this->_meta_box = array_merge(array( 'context' => 'normal', 'priority' => 'high', 'pages' => array('if_custom_slides') ), $this->_meta_box); // default values for fields foreach ($this->_fields as $key => $field) { $multiple = in_array($field['type'], array('checkbox_list', 'file', 'image')) ? true : false; $std = $multiple ? array() : ''; $format = 'date' == $field['type'] ? 'yy-mm-dd' : ('time' == $field['type'] ? 'hh:mm' : ''); $this->_fields[$key] = array_merge(array( 'multiple' => $multiple, 'std' => $std, 'desc' => '', 'format' => $format, 'validate_func' => '' ), $field); } } // Check if field with $type exists function has_field($type) { foreach ($this->_fields as $field) { if ($type == $field['type']) return true; } return false; } // Check if current page is edit page function is_edit_page() { global $pagenow; if (in_array($pagenow, array('post.php', 'post-new.php'))) return true; return false; } /** * Fixes the odd indexing of multiple file uploads from the format: * $_FILES['field']['key']['index'] * To the more standard and appropriate: * $_FILES['field']['index']['key'] */ function fix_file_array(&$files) { $output = array(); foreach ($files as $key => $list) { foreach ($list as $index => $value) { $output[$index][$key] = $value; } } $files = $output; } /******************** END HELPER FUNCTIONS **********************/ } ?> _meta_box['fields'] as $key => $field) { if ('taxonomy' == $field['type'] && 'checkbox_list' == $field['options']['type']) { $this->_meta_box['fields'][$key]['multiple'] = true; } } } // show taxonomy list function show_field_taxonomy($field, $meta) { global $post; if (!is_array($meta)) $meta = (array) $meta; $this->show_field_begin($field, $meta); $options = $field['options']; $terms = get_terms($options['taxonomy'], $options['args']); // checkbox_list if ('checkbox_list' == $options['type']) { foreach ($terms as $term) { echo "slug, $meta), true, false) . " /> $term->name
"; } } // select else { echo ""; } $this->show_field_end($field, $meta); } } /********************* END EXTENDING CLASS ***********************/ /********************* BEGIN DEFINITION OF META BOXES ***********************/ // prefix of meta keys, optional // use underscore (_) at the beginning to make keys hidden, for example $prefix = '_rw_'; // you also can make prefix empty to disable it $prefix = 'slider_'; $meta_boxes = array(); $meta_boxes[] = array( 'id' => 'feature', 'title' => 'bandtheme Slider Options', 'pages' => array('post'), 'fields' => array( array( 'name' => 'bandtheme Slider Image', 'desc' => 'Upload your image here:', 'id' => $prefix . 'post_image', 'type' => 'image', 'std' => '' ), array( 'name' => 'bandtheme Slider Text', 'desc' => 'Enter your slider text here (optional):', 'id' => $prefix . 'text', 'type' => 'text', 'std' => '' ), ) ); $meta_boxes[] = array( 'id' => 'slides', 'title' => 'bandtheme Page Options', 'pages' => array('page'), 'fields' => array( array( 'name' => 'Hide page title', 'desc' => 'Check this box to hide your page title', 'id' => 'hide_title', 'type' => 'checkbox', 'std' => '' ), array( 'name' => 'SEO Title', 'desc' => 'Enter your page title', 'id' => 'seo_title', 'type' => 'text', 'std' => '' ), array( 'name' => 'SEO Description', 'desc' => 'Enter your page description', 'id' => 'seo_description', 'type' => 'textarea', 'std' => '' ), array( 'name' => 'SEO Keywords', 'desc' => 'Enter your page keywords', 'id' => 'seo_keywords', 'type' => 'text', 'std' => '' ), ) ); foreach ($meta_boxes as $meta_box) { $my_box = new RW_Meta_Box_Taxonomy($meta_box); } /********************* END DEFINITION OF META BOXES ***********************/ ?>