image-widget.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | Plugin Name: Image Widget |
| 5 | Plugin URI: http://www.shaneandpeter.com/wordpress |
| 6 | Description: This widget accepts a title, a link and an image and displays them. The admin panel is separated from the widget to offer independant control |
| 7 | Author: Shane & Peter, Inc. |
| 8 | Version: 1.0 |
| 9 | Author URI: http://www.shaneandpeter.com |
| 10 | */ |
| 11 | |
| 12 | class spImageWidget { |
| 13 | |
| 14 | var $id = "image-widget"; |
| 15 | var $name = "Image Widget"; |
| 16 | var $classname = "spImageWidget"; |
| 17 | var $optionsname = "spImageWidget_option"; |
| 18 | var $description = 'This widget accepts a title, a link and an image and displays them. The admin panel is separated from the widget to offer independant control'; |
| 19 | var $thispage = 'sp-image-widget'; |
| 20 | |
| 21 | // Display the widget |
| 22 | function widget($args) { |
| 23 | extract($args); |
| 24 | $options = get_option($this->optionsname); |
| 25 | echo $before_widget; |
| 26 | if ($options[$this->id]['link']) { |
| 27 | if ($options[$this->id]['title']) |
| 28 | echo $before_title.'<a href="'.$options[$this->id]['link'].'">'.$options[$this->id]['title'].'</a>'.$after_title; |
| 29 | if ($options[$this->id]['image']) |
| 30 | echo '<a href="'.$options[$this->id]['link'].'"><img src="'.$options[$this->id]['image'].'" /></a>'; |
| 31 | } else { |
| 32 | if ($options[$this->id]['title']) |
| 33 | echo $before_title.$options[$this->id]['title'].$after_title; |
| 34 | if ($options[$this->id]['image']) |
| 35 | echo '<img src="'.$options[$this->id]['image'].'" />'; |
| 36 | } |
| 37 | if ($options[$this->id]['description']) |
| 38 | echo '<p>'.$options[$this->id]['description'].'</p>'; |
| 39 | echo $after_widget; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | // Controller for modifying the Widget |
| 44 | function control() { |
| 45 | $options = get_option($this->optionsname); |
| 46 | if ( !is_array($options) ) { |
| 47 | $options = array(); |
| 48 | $options[$this->id] = array( |
| 49 | 'title' => &$this->name, |
| 50 | 'link' => '', |
| 51 | 'description' => '', |
| 52 | 'image' => '' |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | $title = $this->id.'-title'; |
| 57 | $link = $this->id.'-link'; |
| 58 | $description = $this->id.'-description'; |
| 59 | $import = $this->id.'-import'; |
| 60 | $submit = $this->id.'-submit'; |
| 61 | |
| 62 | if ( $_POST[$submit] ) { |
| 63 | check_admin_referer($this->classname); |
| 64 | |
| 65 | $options[$this->id]['title'] = htmlentities(stripslashes($_POST[$title])); |
| 66 | $options[$this->id]['description'] = htmlentities(stripslashes($_POST[$description])); |
| 67 | $options[$this->id]['link'] = htmlentities(stripslashes($_POST[$link])); |
| 68 | |
| 69 | // Image Upload |
| 70 | if ($_FILES[$import]['size'] > 0) { |
| 71 | $file = wp_handle_upload($_FILES[$import], array('test_form' => false)); |
| 72 | |
| 73 | if ( isset($file['error']) ) |
| 74 | die( $file['error'] ); |
| 75 | |
| 76 | $url = $file['url']; |
| 77 | $type = $file['type']; |
| 78 | $file = $file['file']; |
| 79 | $filename = basename($file); |
| 80 | |
| 81 | // Construct the object array |
| 82 | $object = array( |
| 83 | 'post_title' => $filename, |
| 84 | 'post_content' => $url, |
| 85 | 'post_mime_type' => $type, |
| 86 | 'guid' => $url); |
| 87 | |
| 88 | $id = wp_insert_attachment($object, $file); |
| 89 | list($width, $height, $type, $attr) = getimagesize( $file ); |
| 90 | |
| 91 | // Add the meta-data |
| 92 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
| 93 | |
| 94 | do_action('wp_create_file_in_uploads', $file, $id); // For replication |
| 95 | |
| 96 | $options[$this->id]['image'] = $url; |
| 97 | } |
| 98 | |
| 99 | update_option($this->optionsname, $options); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | echo '<div class="wrap">'; |
| 104 | echo '<h2>'; |
| 105 | echo $this->name; |
| 106 | echo '</h2>'; |
| 107 | echo '<form name="form1" method="post" action="' . str_replace( '%7E', '~', $_SERVER['REQUEST_URI']) . '" enctype="multipart/form-data">'; |
| 108 | echo '<p>'; |
| 109 | _e('Title:'); |
| 110 | echo ' <br /><input type="text" name="'.$title.'" value="' . htmlspecialchars($options[$this->id]['title'], ENT_QUOTES) . '" size="20">'; |
| 111 | echo '</p>'; |
| 112 | echo '<p>'; |
| 113 | _e('Link:'); |
| 114 | echo ' <br /><input type="text" name="'.$link.'" value="' . htmlspecialchars($options[$this->id]['link'], ENT_QUOTES) . '" size="20">'; |
| 115 | echo '</p>'; |
| 116 | echo '<p>'; |
| 117 | _e('Description:'); |
| 118 | echo ' <br /><textarea type="text" name="'.$description.'">' . htmlspecialchars($options[$this->id]['description'], ENT_QUOTES) . '</textarea>'; |
| 119 | echo '</p>'; |
| 120 | echo '<p>'; |
| 121 | _e('Image:'); |
| 122 | echo ' <input type="file" id="upload" name="'.$import.'" />'; |
| 123 | if ($options[$this->id]['image']) { |
| 124 | echo "<br/><img src=\"".$options[$this->id]['image']."\" />"; |
| 125 | } |
| 126 | echo '</p>'; |
| 127 | echo '<p class="submit"><input type="submit" name="'.$submit.'" value="Save" /></p>'; |
| 128 | wp_nonce_field($this->classname); |
| 129 | echo '</form>'; |
| 130 | echo '</div>'; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | // Initialize the widget |
| 135 | function init() { |
| 136 | wp_register_sidebar_widget( |
| 137 | $this->id, |
| 138 | $this->name, |
| 139 | array(&$this, 'widget'), |
| 140 | array('classname' => $this->classname, 'description' => $this->description), |
| 141 | array( 'number' => -1 ) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | // Admin menu placement |
| 146 | function admin() { |
| 147 | $options = get_option($this->optionsname); |
| 148 | add_management_page($this->name, $this->name, 5, $this->thispage, array(&$this, 'control')); |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | $o = new spImageWidget(); |
| 154 | add_action("plugins_loaded", array($o,"init")); |
| 155 | add_action('admin_menu', array($o,"admin")); |
| 156 | |
| 157 | ?> |