Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Meta Box class
- */
- class RW_Meta_Box {
- protected $_meta_box;
- protected $_fields;
- // Create meta box based on given data
- function __construct($meta_box) {
- if (!is_admin()) return;
- // assign meta box values to local variables and add it's missed values
- $this->_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
- wp_enqueue_script('jquery-ui-core');
- wp_enqueue_script('jquery-ui-sortable');
- 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_action('wp_ajax_rw_delete_file', array(&$this, 'delete_file')); // ajax delete files
- add_action('wp_ajax_rw_reorder_images', array(&$this, 'reorder_images')); // ajax reorder images
- }
- }
- // 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() {
- global $post;
- echo '
- <style type="text/css">
- .rw-images li {margin: 0 10px 10px 0; float: left; width: 150px; height: 100px; text-align: center; border: 3px solid #ccc; cursor: move; position: relative}
- .rw-images img {width: 150px; height: 100px}
- .rw-images a {position: absolute; bottom: 0; right: 0; color: #fff; background: #000; font-weight: bold; padding: 5px}
- </style>
- ';
- echo '
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- ';
- echo '
- // add more file
- $(".rw-add-file").click(function(){
- var $first = $(this).parent().find(".file-input:first");
- $first.clone().insertAfter($first).show();
- return false;
- });
- ';
- echo '
- // delete file
- $(".rw-delete-file").click(function(){
- var $parent = $(this).parent(),
- data = $(this).attr("rel");
- $.post(ajaxurl, {action: \'rw_delete_file\', data: data}, function(response){
- if (response == "0") {
- alert("' . __('File has been successfully deleted.') . '");
- $parent.remove();
- }
- if (response == "1") {
- alert("' . __("You don't have permission to delete this file.") . '");
- }
- });
- return false;
- });
- ';
- foreach ($this->_fields as $field) {
- if ('image' != $field['type']) continue;
- $id = $field['id'];
- $nonce_delete = wp_create_nonce('rw_ajax_delete_file');
- echo "
- // thickbox upload
- $('#rw_upload_$id').click(function(){
- backup = window.send_to_editor;
- window.send_to_editor = function(html) {
- var el = $(html).is('a') ? $('img', html) : $(html),
- img_url = el.attr('src'),
- img_id = el.attr('class');
- img_id = img_id.slice((img_id.search(/wp-image-/) + 9));
- html = '<li id=\"item_' + img_id + '\">';
- html += '<img src=\"' + img_url + '\" />';
- html += '<a title=\"" . __('Delete this image') . "\" class=\"rw-delete-file\" href=\"#\" rel=\"{$post->ID}!$id!' + img_id + '!$nonce_delete\">" . __('Delete') . "</a>';
- html += '<input type=\"hidden\" name=\"{$id}[]\" value=\"' + img_id + '\" />';
- html += '</li>';
- $('#rw-images-$id').append($(html));
- tb_remove();
- window.send_to_editor = backup;
- }
- tb_show('', 'media-upload.php?post_id={$post->ID}; ?>&type=image&TB_iframe=true');
- return false;
- });
- ";
- echo "
- // sort
- $('#rw-images-$id').sortable({
- placeholder: 'ui-state-highlight',
- update: function (){
- var order = $('#rw-images-$id').sortable('serialize'),
- data = order + '!' + $('#rw-data-$id').val();
- $.post(ajaxurl, {action: 'rw_reorder_images', data: data}, function(response){
- if (response == '0') {
- alert('" . __('Order saved.') . "');
- }
- if (response == '1') {
- alert(\"" . __("You don't have permission to reorder images.") . "\");
- }
- });
- }
- });
- ";
- }
- echo '});
- </script>
- ';
- }
- // 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($post_id, $key, $attach_id, $nonce) = explode('!', $_POST['data']);
- if (!wp_verify_nonce($nonce, 'rw_ajax_delete_file')) {
- 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, 'rw_ajax_sort_file')) {
- die('1');
- }
- parse_str($order, $items);
- $items = $items['item'];
- $order = 0;
- $meta = array();
- foreach ($items as $item) {
- wp_update_post(array(
- 'ID' => $item,
- 'post_parent' => $post_id,
- 'menu_order' => $order
- ));
- $order++;
- $meta[] = $item;
- }
- delete_post_meta($post_id, $key);
- foreach ($meta as $value) {
- add_post_meta($post_id, $key, $value);
- }
- die('0');
- }
- /******************** END UPLOAD **********************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement