Advertisement
Guest User

WordPress Uploads Metabox

a guest
May 29th, 2012
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.27 KB | None | 0 0
  1. /**
  2.  * Meta Box class
  3.  */
  4. class RW_Meta_Box {
  5.  
  6.     protected $_meta_box;
  7.     protected $_fields;
  8.  
  9.     // Create meta box based on given data
  10.     function __construct($meta_box) {
  11.         if (!is_admin()) return;
  12.  
  13.         // assign meta box values to local variables and add it's missed values
  14.         $this->_meta_box = $meta_box;
  15.         $this->_fields = & $this->_meta_box['fields'];
  16.         $this->add_missed_values();
  17.  
  18.         add_action('admin_menu', array(&$this, 'add')); // add meta box
  19.         add_action('save_post', array(&$this, 'save')); // save meta box's data
  20.  
  21.         // check for some special fields and add needed actions for them
  22.         $this->check_field_upload();
  23.         $this->check_field_color();
  24.         $this->check_field_date();
  25.         $this->check_field_time();
  26.     }
  27.  
  28.     /******************** BEGIN UPLOAD **********************/
  29.  
  30.     // Check field upload and add needed actions
  31.     function check_field_upload() {
  32.         if ($this->has_field('image') || $this->has_field('file')) {
  33.             add_action('post_edit_form_tag', array(&$this, 'add_enctype'));             // add data encoding type for file uploading
  34.  
  35.             wp_enqueue_script('jquery-ui-core');
  36.             wp_enqueue_script('jquery-ui-sortable');
  37.             add_action('admin_head-post.php', array(&$this, 'add_script_upload'));      // add scripts for handling add/delete images
  38.             add_action('admin_head-post-new.php', array(&$this, 'add_script_upload'));
  39.            
  40.             add_action('delete_post', array(&$this, 'delete_attachments'));             // delete all attachments when delete post
  41.             add_action('wp_ajax_rw_delete_file', array(&$this, 'delete_file'));         // ajax delete files
  42.             add_action('wp_ajax_rw_reorder_images', array(&$this, 'reorder_images'));   // ajax reorder images
  43.         }
  44.     }
  45.  
  46.     // Add data encoding type for file uploading
  47.     function add_enctype() {
  48.         echo ' enctype="multipart/form-data"';
  49.     }
  50.  
  51.     // Add scripts for handling add/delete images
  52.     function add_script_upload() {
  53.         global $post;
  54.         echo '
  55.         <style type="text/css">
  56.         .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}
  57.         .rw-images img {width: 150px; height: 100px}
  58.         .rw-images a {position: absolute; bottom: 0; right: 0; color: #fff; background: #000; font-weight: bold; padding: 5px}
  59.         </style>
  60.         ';
  61.        
  62.         echo '
  63.         <script type="text/javascript">
  64.         jQuery(document).ready(function($) {
  65.         ';
  66.        
  67.         echo '
  68.             // add more file
  69.             $(".rw-add-file").click(function(){
  70.                 var $first = $(this).parent().find(".file-input:first");
  71.                 $first.clone().insertAfter($first).show();
  72.                 return false;
  73.             });
  74.         ';
  75.        
  76.         echo '
  77.             // delete file
  78.             $(".rw-delete-file").click(function(){
  79.                 var $parent = $(this).parent(),
  80.                     data = $(this).attr("rel");
  81.                 $.post(ajaxurl, {action: \'rw_delete_file\', data: data}, function(response){
  82.                     if (response == "0") {
  83.                         alert("' . __('File has been successfully deleted.') . '");
  84.                         $parent.remove();
  85.                     }
  86.                     if (response == "1") {
  87.                         alert("' . __("You don't have permission to delete this file.") . '");
  88.                     }
  89.                 });
  90.                 return false;
  91.             });
  92.         ';
  93.        
  94.         foreach ($this->_fields as $field) {
  95.             if ('image' != $field['type']) continue;
  96.            
  97.             $id = $field['id'];
  98.             $nonce_delete = wp_create_nonce('rw_ajax_delete_file');
  99.             echo "
  100.             // thickbox upload
  101.             $('#rw_upload_$id').click(function(){
  102.                 backup = window.send_to_editor;
  103.                 window.send_to_editor = function(html) {
  104.                     var el = $(html).is('a') ? $('img', html) : $(html),
  105.                         img_url = el.attr('src'),
  106.                         img_id = el.attr('class');
  107.                    
  108.                     img_id = img_id.slice((img_id.search(/wp-image-/) + 9));
  109.                    
  110.                     html = '<li id=\"item_' + img_id + '\">';
  111.                     html += '<img src=\"' + img_url + '\" />';
  112.                     html += '<a title=\"" . __('Delete this image') . "\" class=\"rw-delete-file\" href=\"#\" rel=\"{$post->ID}!$id!' + img_id + '!$nonce_delete\">" . __('Delete') . "</a>';
  113.                     html += '<input type=\"hidden\" name=\"{$id}[]\" value=\"' + img_id + '\" />';
  114.                     html += '</li>';
  115.                    
  116.                     $('#rw-images-$id').append($(html));
  117.                    
  118.                     tb_remove();
  119.                     window.send_to_editor = backup;
  120.                 }
  121.                 tb_show('', 'media-upload.php?post_id={$post->ID}; ?>&type=image&TB_iframe=true');
  122.                
  123.                 return false;
  124.             });
  125.             ";
  126.            
  127.             echo "
  128.             // sort
  129.             $('#rw-images-$id').sortable({
  130.                 placeholder: 'ui-state-highlight',
  131.                 update: function (){
  132.                     var order = $('#rw-images-$id').sortable('serialize'),
  133.                         data = order + '!' + $('#rw-data-$id').val();
  134.                     $.post(ajaxurl, {action: 'rw_reorder_images', data: data}, function(response){
  135.                         if (response == '0') {
  136.                             alert('" . __('Order saved.') . "');
  137.                         }
  138.                         if (response == '1') {
  139.                             alert(\"" . __("You don't have permission to reorder images.") . "\");
  140.                         }
  141.                     });
  142.                 }
  143.             });
  144.             ";
  145.         }
  146.        
  147.         echo '});
  148.         </script>
  149.         ';
  150.     }
  151.  
  152.     // Delete all attachments when delete post
  153.     function delete_attachments($post_id) {
  154.         $attachments = get_posts(array(
  155.             'numberposts' => -1,
  156.             'post_type' => 'attachment',
  157.             'post_parent' => $post_id
  158.         ));
  159.         if (!empty($attachments)) {
  160.             foreach ($attachments as $att) {
  161.                 wp_delete_attachment($att->ID);
  162.             }
  163.         }
  164.     }
  165.  
  166.     // Ajax callback for deleting files. Modified from a function used by "Verve Meta Boxes" plugin (http://goo.gl/LzYSq)
  167.     function delete_file() {
  168.         if (!isset($_POST['data'])) die();
  169.  
  170.         list($post_id, $key, $attach_id, $nonce) = explode('!', $_POST['data']);
  171.  
  172.         if (!wp_verify_nonce($nonce, 'rw_ajax_delete_file')) {
  173.             die('1');
  174.         }
  175.  
  176.         wp_delete_attachment($attach_id);
  177.         delete_post_meta($post_id, $key, $attach_id);
  178.  
  179.         die('0');
  180.     }
  181.    
  182.     // Ajax callback for reordering images
  183.     function reorder_images() {
  184.         if (!isset($_POST['data'])) die();
  185.  
  186.         list($order, $post_id, $key, $nonce) = explode('!',$_POST['data']);
  187.  
  188.         if (!wp_verify_nonce($nonce, 'rw_ajax_sort_file')) {
  189.             die('1');
  190.         }
  191.  
  192.         parse_str($order, $items);
  193.         $items = $items['item'];
  194.         $order = 0;
  195.         $meta = array();
  196.         foreach ($items as $item) {
  197.             wp_update_post(array(
  198.                 'ID' => $item,
  199.                 'post_parent' => $post_id,
  200.                 'menu_order' => $order
  201.             ));
  202.             $order++;
  203.             $meta[] = $item;
  204.         }
  205.         delete_post_meta($post_id, $key);
  206.         foreach ($meta as $value) {
  207.             add_post_meta($post_id, $key, $value);
  208.         }
  209.  
  210.         die('0');
  211.     }
  212.  
  213.     /******************** END UPLOAD **********************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement