_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();
$this->check_field_color();
$this->check_field_date();
$this->check_field_time();
}
/******************** 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 COLOR PICKER **********************/
// Check field color
function check_field_color() {
if ($this->has_field('color') && $this->is_edit_page()) {
wp_enqueue_style('farbtastic'); // enqueue built-in script and style for color picker
wp_enqueue_script('farbtastic');
add_action('admin_head', array(&$this, 'add_script_color')); // add our custom script for color picker
}
}
// Custom script for color picker
function add_script_color() {
$ids = array();
foreach ($this->_fields as $field) {
if ('color' == $field['type']) {
$ids[] = $field['id'];
}
}
echo '
';
}
/******************** END COLOR PICKER **********************/
/******************** BEGIN DATE PICKER **********************/
// Check field date
function check_field_date() {
if ($this->has_field('date') && $this->is_edit_page()) {
// add style and script, must use jQuery UI 1.7.3 to get rid of confliction with WP admin scripts
wp_enqueue_style('rw-jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/themes/base/jquery-ui.css');
wp_enqueue_script('rw-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/jquery-ui.min.js', array('jquery'));
add_action('admin_head', array(&$this, 'add_script_date'));
}
}
// Custom script for date picker
function add_script_date() {
$dates = array();
foreach ($this->_fields as $field) {
if ('date' == $field['type']) {
$dates[$field['id']] = $field['format'];
}
}
echo '
';
}
/******************** END DATE PICKER **********************/
/******************** BEGIN TIME PICKER **********************/
// Check field time
function check_field_time() {
if ($this->has_field('time') && $this->is_edit_page()) {
// add style and script, must use jQuery UI 1.7.3 to get rid of confliction with WP admin scripts
wp_enqueue_style('rw-jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/themes/base/jquery-ui.css');
wp_enqueue_script('rw-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/jquery-ui.min.js', array('jquery'));
wp_enqueue_script('rw-timepicker', 'https://github.com/trentrichardson/jQuery-Timepicker-Addon/raw/master/jquery-ui-timepicker-addon.js', array('rw-jquery-ui'));
add_action('admin_head', array(&$this, 'add_script_time'));
}
}
// Custom script and style for time picker
function add_script_time() {
// style
echo '
';
// script
$times = array();
foreach ($this->_fields as $field) {
if ('time' == $field['type']) {
$times[$field['id']] = $field['format'];
}
}
echo '
';
}
/******************** END TIME PICKER **********************/
/******************** 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 '
';
// 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 "