Advertisement
Guest User

Untitled

a guest
May 31st, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. ini_set("display_errors", 1);
  5.  
  6. /*
  7.     Plugin Name: Image Gallery Uploader
  8.     Plugin URI: https://github.com/kschat
  9.     Description: This plugin allows visitors of your WordPress site to upload images to a gallery.
  10.     Version: 0.1
  11.     License: GPL2
  12. */
  13.  
  14. include_once 'Debug.php';
  15.  
  16. define('PLUGIN_PATH', plugin_dir_url(__FILE__));
  17.  
  18. class UploaderWidget extends WP_Widget {
  19.    
  20.     // Constructor
  21.     function __construct() {
  22.         $widget_ops = array(
  23.         'classname' => 'image-uploader',
  24.         'description' => 'A widget used to upload photos to a gallery.');
  25.        
  26.         $control_ops = array('width'=> '200', 'height'=> '350', 'id_base' => 'image-uploader-id');
  27.        
  28.         parent::__construct('image-uploader-id', 'Uploader Widget', $widget_ops, $control_ops);
  29.     }
  30.    
  31.     function widget($args, $instance) {
  32.         extract($args);
  33.        
  34.         $title = apply_filters('widget_title', $instance['title']);
  35.        
  36.         echo $before_widget;
  37.        
  38.         if($title) {
  39.             echo $before_title . $title . $after_title;
  40.         }
  41.         ?>
  42.         <img src="<?php echo PLUGIN_PATH;?>images/defaultImage.gif"><br />
  43.         <p>
  44.         <label for="upload-button" style="color:black;">Upload new Image:</label>
  45.         <input type="file" name="upload-button" />
  46.         </p>
  47.         <?php
  48.        
  49.         echo $after_widget;
  50.     }
  51.    
  52.     function update($new_instance, $old_instance) {
  53.         $instance = $old_instance;
  54.        
  55.         $instance['title'] = $new_instance['title'];
  56.         $instance['imgDir'] = $new_instance['imgDir'];
  57.         //$instance['categories'][0]['selected'] = 1;
  58.         //$instance['categories'][0]['label'] = '';
  59.         //$instance['newCategory'] = '';
  60.        
  61.        
  62.        
  63.         $instance['categories'] = $this->parseAndCombineCategories((get_option('UploaderWidget_categories')), $new_instance['newCategory']);
  64.        
  65.         for($i=0; $i<sizeof($instance['categories']); $i++) {
  66.             $instance['categories'][$i]['selected'] = (int)$new_instance['categories'][$i]['selected'];
  67.         }
  68.        
  69.         //$instance['categories'][0]['selected'] = 1;
  70.         //$instance['categories'][0]['label'] = 'label';
  71.        
  72.         update_option('UploaderWidget_categories', $instance['categories']);
  73.        
  74.         return $instance;
  75.     }
  76.    
  77.     function form($instance) {
  78.         $defaults = array('title'       => 'Uploader Widget',
  79.                           'imgDir'      => PLUGIN_PATH.'images/',
  80.                           'categories'  => array(0 => array('selected'   => 0,
  81.                                                             'label'      => 'option1'),
  82.                                                  1 => array('selected'   => 0,
  83.                                                             'label'      => 'option2'),
  84.                                                  2 => array('selected'   => 0,
  85.                                                             'label'      => 'option3')));
  86.        
  87.         $instance = wp_parse_args((array)$instance, $defaults);
  88.         //$instance['categories'] = null;
  89.         //$instance['newCategory'] = null;
  90.         ?>
  91.         <p>
  92.             <label for="<?php echo $this->get_field_id('title');?>">Title:</label><br />
  93.             <input type= "text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>"
  94.                    value="<?php echo $instance['title'];?>" />
  95.         </p>
  96.        
  97.         <p>
  98.             <label for="<?php echo $this->get_field_id('imgDir');?>">Saved Images Directory:</label>
  99.             <input type="text" id="<?php echo $this->get_field_id('imgDir');?>" name="<?php echo $this->get_field_name('imgDir');?>"
  100.                     value="<?php echo $instance['imgDir'];?>" readonly="readonly" />
  101.             <input type="file" />
  102.         </p>
  103.        
  104.         <p>
  105.             Categories:<br />
  106.             <?php
  107.             $this->displayCategories($instance['categories']);
  108.             $this->displayNewCategoriesInput('newCategory');
  109.             ?>
  110.         </p>
  111.         <?php
  112.     }
  113.    
  114.     function parseAndCombineCategories($categories, $newCategories) {
  115.         //unset($categories);
  116.         if(trim($newCategories) != "") {
  117.             unset($categories['NoCategories']);
  118.             $newCategories = split(',', $newCategories);
  119.             $newSize = sizeof($categories) + sizeof($newCategories);
  120.             $oldSize = sizeof($categories);
  121.            
  122.             for($i=sizeof($categories); $i<$newSize; $i++) {
  123.                 $categories[$i]['selected'] = 0;
  124.                 $categories[$i]['label'] = $newCategories[$i - $oldSize];
  125.             }
  126.         }
  127.         else if(empty($categories)) {
  128.             return $categories = array('NoCategories' => '<b>No categories have been made yet.</b>');
  129.         }
  130.         return (array)$categories;
  131.     }
  132.    
  133.     function displayCategories($categories) {
  134.         if(array_key_exists('NoCategories', $categories)) {
  135.             ?> <p> <?php
  136.             echo $categories['NoCategories'];
  137.             ?> </p> <?php
  138.             return;
  139.         }
  140.         ?> <table class="form-table" id="image-gallery-form-table"> <?php
  141.         for($i=0; $i<sizeof($categories); $i++) {
  142.             echo ($i % 2 == 0) ? "<tr><td>": "<td>"; ?>
  143.             <input class="checkboxes" type="checkbox" <?php checked((int)$categories[$i]['selected'], 1); ?> id="<?php echo $this->get_field_id($categories[$i]['label']); ?>"
  144.                    name="<?php echo $this->get_field_name($categories[$i]['label']);?>" />
  145.                 <label for="<?php echo $this->get_field_id($categories[$i]['label']); ?>"><?php echo $categories[$i]['label']; ?></label>
  146.                 <?php echo ($i % 2 == 0) ? "</td>": "</td></tr>";
  147.         }
  148.         ?> </table> <?php echo Debug::printArray($categories, false);
  149.     }
  150.    
  151.     function displayNewCategoriesInput($field) {
  152.         ?>
  153.         <label for="<?php echo $this->get_field_id($field); ?>">Add a new category
  154.             (seperate names with a ',' to add multiple categories):</label>
  155.             <input type="text" name="<?php echo $this->get_field_name($field); ?>"
  156.                    id="<?php echo $this->get_field_id($field); ?>" />
  157.         <?php
  158.     }
  159. }
  160.  
  161. function registerUploaderWidget() {
  162.     register_widget('UploaderWidget');
  163. }
  164.  
  165. add_action('widgets_init', 'registerUploaderWidget');
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement