* @author Matt Wiebe
* @author Modern Tribe, Inc.
* @link http://www.deluxeblogtips.com/p/meta-box-script-for-wordpress.html
* @example meta-box-usage.php Sample declaration and usage of meta boxes
* @version: 3.2.2
*
* @license GNU General Public License v2.0
*/
/**
* Meta Box class
*/
class Tribe_Meta_Box {
protected $_meta_box;
protected $_fields;
protected $textdomain = 'tribe-apm';
// Create meta box based on given data
public function __construct($meta_box) {
// run script only in admin area
if (!is_admin()) return;
// overrideable textdomain
$this->textdomain = apply_filters( 'tribe_apm_textdomain', $this->textdomain );
// assign meta box values to local variables and add it's missed values
$this->_meta_box = $meta_box;
// Cast pages to array
$this->_meta_box['pages'] = (array) $meta_box['pages'];
$this->_fields = $this->_meta_box['fields'];
$this->add_missed_values();
$this->register_scripts_and_styles();
add_action('add_meta_boxes', array($this, 'add')); // add meta box, using 'add_meta_boxes' for WP 3.0+
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();
$this->check_field_wysiwyg();
// load common js, css files
// must enqueue for all pages as we need js for the media upload, too
add_action('admin_enqueue_scripts', array(__CLASS__, 'js_css'));
}
function register_scripts_and_styles() {
// change '\' to '/' in case using Windows
$content_dir = str_replace('\\', '/', WP_CONTENT_DIR);
$script_dir = str_replace('\\', '/', dirname(__FILE__));
// get URL of the directory of current file, this works in both theme or plugin
$base_url = trailingslashit( str_replace($content_dir, WP_CONTENT_URL, $script_dir) );
$resources_url = apply_filters( 'tribe_apm_resources_url', $base_url . 'resources' );
$resources_url = trailingslashit($resources_url);
wp_register_style( 'tribe-meta-box', $resources_url . 'meta-box.css');
wp_register_script('tribe-meta-box', $resources_url . 'meta-box.js', array('jquery'), null, true);
wp_register_style('tribe-jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/' . self::get_jqueryui_ver() . '/themes/base/jquery-ui.css');
wp_register_script('tribe-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/' . self::get_jqueryui_ver() . '/jquery-ui.min.js', array('jquery'));
wp_register_script('tribe-timepicker', 'https://github.com/trentrichardson/jQuery-Timepicker-Addon/raw/master/jquery-ui-timepicker-addon.js', array('tribe-jquery-ui'));
}
// Load common js, css files for the script
static function js_css() {
wp_enqueue_script( 'tribe-meta-box' );
wp_enqueue_style( 'tribe-meta-box' );
}
/******************** BEGIN UPLOAD **********************/
// Check field upload and add needed actions
function check_field_upload() {
if (!$this->has_field('image') && !$this->has_field('file')) return;
add_action('post_edit_form_tag', array($this, 'add_enctype')); // add data encoding type for file uploading
// make upload feature works even when custom post type doesn't support 'editor'
wp_enqueue_script('media-upload');
add_thickbox();
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-sortable');
add_filter('media_upload_gallery', array($this, 'insert_images')); // process adding multiple images to image meta field
add_filter('media_upload_library', array($this, 'insert_images'));
add_filter('media_upload_image', array($this, 'insert_images'));
// add_action('delete_post', array($this, 'delete_attachments')); // delete all attachments when delete post
add_action('wp_ajax_tribe_delete_file', array($this, 'delete_file')); // ajax delete files
add_action('wp_ajax_tribe_reorder_images', array($this, 'reorder_images')); // ajax reorder images
}
// Add data encoding type for file uploading
function add_enctype() {
echo ' enctype="multipart/form-data"';
}
// Process adding images to image meta field, modifiy from 'Faster image insert' plugin
function insert_images() {
if (!isset($_POST['tribe-insert']) || empty($_POST['attachments'])) return;
check_admin_referer('media-form');
$nonce = wp_create_nonce('tribe_ajax_delete');
$post_id = $_POST['post_id'];
$id = $_POST['field_id'];
// modify the insertion string
$html = '';
foreach ($_POST['attachments'] as $attachment_id => $attachment) {
$attachment = stripslashes_deep($attachment);
if (empty($attachment['selected']) || empty($attachment['url'])) continue;
$li = "
";
$html .= $li;
}
media_send_to_editor($html);
}
// 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);
}
}
}
// Ajax callback for deleting files. Modified from a function used by "Verve Meta Boxes" plugin (http://goo.gl/LzYSq)
function delete_file() {
if (!isset($_POST['data'])) die();
list($nonce, $post_id, $key, $attach_id) = explode('|', $_POST['data']);
if (!wp_verify_nonce($nonce, 'tribe_ajax_delete')) die('1');
// wp_delete_attachment($attach_id);
delete_post_meta($post_id, $key, $attach_id);
die('0');
}
// Ajax callback for reordering images
function reorder_images() {
if (!isset($_POST['data'])) die();
list($order, $post_id, $key, $nonce) = explode('|',$_POST['data']);
if (!wp_verify_nonce($nonce, 'tribe_ajax_reorder')) die('1');
parse_str($order, $items);
$items = $items['item'];
$order = 1;
foreach ($items as $item) {
wp_update_post(array(
'ID' => $item,
'post_parent' => $post_id,
'menu_order' => $order
));
$order++;
}
die('0');
}
/******************** END UPLOAD **********************/
/******************** BEGIN OTHER FIELDS **********************/
// Check field color
function check_field_color() {
if ($this->has_field('color') && self::is_edit_page()) {
wp_enqueue_style('farbtastic'); // enqueue built-in script and style for color picker
wp_enqueue_script('farbtastic');
}
}
// Check field date
function check_field_date() {
if ($this->has_field('date') && self::is_edit_page()) {
wp_enqueue_style('tribe-jquery-ui-css');
wp_enqueue_script('tribe-jquery-ui');
}
}
// Check field time
function check_field_time() {
if ($this->has_field('time') && self::is_edit_page()) {
// add style and script, use proper jQuery UI version
wp_enqueue_style('tribe-jquery-ui-css');
wp_enqueue_script('tribe-jquery-ui');
wp_enqueue_script('tribe-timepicker');
}
}
// Check field WYSIWYG
function check_field_wysiwyg() {
if ($this->has_field('wysiwyg') && self::is_edit_page()) {
add_action('admin_print_footer_scripts', 'wp_tiny_mce', 25);
}
}
/******************** END OTHER FIELDS **********************/
/******************** BEGIN META BOX PAGE **********************/
// Add meta box for multiple post types
function add() {
foreach ( (array) $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__), 'tribe_meta_box_nonce');
echo '
";
// re-arrange images with 'menu_order', thanks Onur
if (!empty($meta)) {
$meta = implode(',', $meta);
$images = $wpdb->get_col("
SELECT ID FROM $wpdb->posts
WHERE post_type = 'attachment'
AND ID in ($meta)
ORDER BY menu_order ASC
");
foreach ($images as $image) {
$src = wp_get_attachment_image_src($image);
$src = $src[0];
echo "