Here is the full widget class:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
/*
Plugin Name: Image Gallery Uploader
Plugin URI: https://github.com/kschat
Description: This plugin allows visitors of your WordPress site to upload images to a gallery.
Version: 0.1
License: GPL2
*/
include_once 'Debug.php';
define('PLUGIN_PATH', plugin_dir_url(__FILE__));
class UploaderWidget extends WP_Widget {
// Constructor
function __construct() {
$widget_ops = array(
'classname' => 'image-uploader',
'description' => 'A widget used to upload photos to a gallery.');
$control_ops = array('width'=> '200', 'height'=> '350', 'id_base' => 'image-uploader-id');
parent::__construct('image-uploader-id', 'Uploader Widget', $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title) {
echo $before_title . $title . $after_title;
}
?>
<img src="<?php echo PLUGIN_PATH;?>images/defaultImage.gif"><br />
<p>
<label for="upload-button" style="color:black;">Upload new Image:</label>
<input type="file" name="upload-button" />
</p>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['imgDir'] = $new_instance['imgDir'];
//$instance['categories'][0]['selected'] = 1;
//$instance['categories'][0]['label'] = '';
//$instance['newCategory'] = '';
$instance['categories'] = $this->parseAndCombineCategories((get_option('UploaderWidget_categories')), $new_instance['newCategory']);
for($i=0; $i<sizeof($instance['categories']); $i++) {
$instance['categories'][$i]['selected'] = (int)$new_instance['categories'][$i]['selected'];
}
//$instance['categories'][0]['selected'] = 1;
//$instance['categories'][0]['label'] = 'label';
update_option('UploaderWidget_categories', $instance['categories']);
return $instance;
}
function form($instance) {
$defaults = array('title' => 'Uploader Widget',
'imgDir' => PLUGIN_PATH.'images/',
'categories' => array(0 => array('selected' => 0,
'label' => 'option1'),
1 => array('selected' => 0,
'label' => 'option2'),
2 => array('selected' => 0,
'label' => 'option3')));
$instance = wp_parse_args((array)$instance, $defaults);
//$instance['categories'] = null;
//$instance['newCategory'] = null;
?>
<p>
<label for="<?php echo $this->get_field_id('title');?>">Title:</label><br />
<input type= "text" id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>"
value="<?php echo $instance['title'];?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('imgDir');?>">Saved Images Directory:</label>
<input type="text" id="<?php echo $this->get_field_id('imgDir');?>" name="<?php echo $this->get_field_name('imgDir');?>"
value="<?php echo $instance['imgDir'];?>" readonly="readonly" />
<input type="file" />
</p>
<p>
Categories:<br />
<?php
$this->displayCategories($instance['categories']);
$this->displayNewCategoriesInput('newCategory');
?>
</p>
<?php
}
function parseAndCombineCategories($categories, $newCategories) {
//unset($categories);
if(trim($newCategories) != "") {
unset($categories['NoCategories']);
$newCategories = split(',', $newCategories);
$newSize = sizeof($categories) + sizeof($newCategories);
$oldSize = sizeof($categories);
for($i=sizeof($categories); $i<$newSize; $i++) {
$categories[$i]['selected'] = 0;
$categories[$i]['label'] = $newCategories[$i - $oldSize];
}
}
else if(empty($categories)) {
return $categories = array('NoCategories' => '<b>No categories have been made yet.</b>');
}
return (array)$categories;
}
function displayCategories($categories) {
if(array_key_exists('NoCategories', $categories)) {
?> <p> <?php
echo $categories['NoCategories'];
?> </p> <?php
return;
}
?> <table class="form-table" id="image-gallery-form-table"> <?php
for($i=0; $i<sizeof($categories); $i++) {
echo ($i % 2 == 0) ? "<tr><td>": "<td>"; ?>
<input class="checkboxes" type="checkbox" <?php checked((int)$categories[$i]['selected'], 1); ?> id="<?php echo $this->get_field_id($categories[$i]['label']); ?>"
name="<?php echo $this->get_field_name($categories[$i]['label']);?>" />
<label for="<?php echo $this->get_field_id($categories[$i]['label']); ?>"><?php echo $categories[$i]['label']; ?></label>
<?php echo ($i % 2 == 0) ? "</td>": "</td></tr>";
}
?> </table> <?php echo Debug::printArray($categories, false);
}
function displayNewCategoriesInput($field) {
?>
<label for="<?php echo $this->get_field_id($field); ?>">Add a new category
(seperate names with a ',' to add multiple categories):</label>
<input type="text" name="<?php echo $this->get_field_name($field); ?>"
id="<?php echo $this->get_field_id($field); ?>" />
<?php
}
}
function registerUploaderWidget() {
register_widget('UploaderWidget');
}
add_action('widgets_init', 'registerUploaderWidget');
?>
It should be saving the state in the update method, but for some reason it's not. I tried placing the for loop both before and after the call to parseAndCombineCategories() but it didn't make a difference. If I hard code the option (i.e. $instance['categories'][0]['selected'] = 1) it works, but if I try to set instance equal to new_instance like I do in the for loop nothing is saved in the selected node of the array. Something I actually noticed too is if there was already a 0 saved in the selected node and it runs through that loop 0 is no longer there, it's replaced with nothing.
This post has been edited by giggly kisses: 31 May 2012 - 12:14 PM

New Topic/Question
Reply


MultiQuote



|