]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/custom-header.php
8d605655df0287022e0e30f4ad81a340e781ec7a
[autoinstalls/wordpress.git] / wp-admin / custom-header.php
1 <?php
2
3 class Custom_Image_Header {
4         var $admin_header_callback;
5
6         function Custom_Image_Header($admin_header_callback) {
7                 $this->admin_header_callback = $admin_header_callback;
8         }
9
10         function init() {
11                 $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
12
13                 add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
14                 add_action("admin_head-$page", array(&$this, 'js'), 50);
15                 add_action("admin_head-$page", $this->admin_header_callback, 51);
16         }
17
18         function js_includes() {
19                 wp_enqueue_script('cropper');
20                 wp_enqueue_script('colorpicker');
21         }
22
23         function js() {
24
25                 if ( isset( $_POST['textcolor'] ) ) {
26                         check_admin_referer('custom-header');
27                         if ( 'blank' == $_POST['textcolor'] ) {
28                                 set_theme_mod('header_textcolor', 'blank');
29                         } else {
30                                 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
31                                 if ( strlen($color) == 6 || strlen($color) == 3 )
32                                         set_theme_mod('header_textcolor', $color);
33                         }
34                 }
35                 if ( isset($_POST['resetheader']) ) {
36                         check_admin_referer('custom-header');
37                         remove_theme_mods();
38                 }
39         ?>
40 <script type="text/javascript">
41
42         function onEndCrop( coords, dimensions ) {
43                 $( 'x1' ).value = coords.x1;
44                 $( 'y1' ).value = coords.y1;
45                 $( 'x2' ).value = coords.x2;
46                 $( 'y2' ).value = coords.y2;
47                 $( 'width' ).value = dimensions.width;
48                 $( 'height' ).value = dimensions.height;
49         }
50
51         // with a supplied ratio
52         Event.observe(
53                 window,
54                 'load',
55                 function() {
56                         var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
57                         var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
58                         var ratio = xinit / yinit;
59                         var ximg = $('upload').width;
60                         var yimg = $('upload').height;
61                         if ( yimg < yinit || ximg < xinit ) {
62                                 if ( ximg / yimg > ratio ) {
63                                         yinit = yimg;
64                                         xinit = yinit * ratio;
65                                 } else {
66                                         xinit = ximg;
67                                         yinit = xinit / ratio;
68                                 }
69                         }
70                         new Cropper.Img(
71                                 'upload',
72                                 {
73                                         ratioDim: { x: xinit, y: yinit },
74                                         displayOnInit: true,
75                                         onEndCrop: onEndCrop
76                                 }
77                         )
78                 }
79         );
80
81         var cp = new ColorPicker();
82
83         function pickColor(color) {
84                 $('name').style.color = color;
85                 $('desc').style.color = color;
86                 $('textcolor').value = color;
87         }
88         function PopupWindow_hidePopup(magicword) {
89                 if ( magicword != 'prettyplease' )
90                         return false;
91                 if (this.divName != null) {
92                         if (this.use_gebi) {
93                                 document.getElementById(this.divName).style.visibility = "hidden";
94                         }
95                         else if (this.use_css) {
96                                 document.all[this.divName].style.visibility = "hidden";
97                         }
98                         else if (this.use_layers) {
99                                 document.layers[this.divName].visibility = "hidden";
100                         }
101                 }
102                 else {
103                         if (this.popupWindow && !this.popupWindow.closed) {
104                                 this.popupWindow.close();
105                                 this.popupWindow = null;
106                         }
107                 }
108                 return false;
109         }
110         function colorSelect(t,p) {
111                 if ( cp.p == p && document.getElementById(cp.divName).style.visibility != "hidden" ) {
112                         cp.hidePopup('prettyplease');
113                 } else {
114                         cp.p = p;
115                         cp.select(t,p);
116                 }
117         }
118         function colorDefault() {
119                 pickColor('<?php echo HEADER_TEXTCOLOR; ?>');
120         }
121
122         function hide_text() {
123                 $('name').style.display = 'none';
124                 $('desc').style.display = 'none';
125                 $('pickcolor').style.display = 'none';
126                 $('defaultcolor').style.display = 'none';
127                 $('textcolor').value = 'blank';
128                 $('hidetext').value = '<?php _e('Show Text'); ?>';
129 //              $('hidetext').onclick = 'show_text()';
130                 Event.observe( $('hidetext'), 'click', show_text );
131         }
132
133         function show_text() {
134                 $('name').style.display = 'block';
135                 $('desc').style.display = 'block';
136                 $('pickcolor').style.display = 'inline';
137                 $('defaultcolor').style.display = 'inline';
138                 $('textcolor').value = '<?php echo HEADER_TEXTCOLOR; ?>';
139                 $('hidetext').value = '<?php _e('Hide Text'); ?>';
140                 Event.stopObserving( $('hidetext'), 'click', show_text );
141                 Event.observe( $('hidetext'), 'click', hide_text );
142         }
143
144         <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
145 Event.observe( window, 'load', hide_text );
146         <?php } ?>
147
148 </script>
149 <?php
150         }
151
152         function step_1() {
153                 if ( $_GET['updated'] ) { ?>
154 <div id="message" class="updated fade">
155 <p><?php _e('Header updated.') ?></p>
156 </div>
157                 <?php } ?>
158
159 <div class="wrap">
160 <h2><?php _e('Your Header Image'); ?></h2>
161 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
162
163 <div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
164 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
165 <div id="desc"><?php bloginfo('description');?></div>
166 </div>
167 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
168 <form method="post" action="<?php echo get_option('siteurl') ?>/wp-admin/themes.php?page=custom-header&amp;updated=true">
169 <input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
170 <input type="button" value="<?php _e('Select a Text Color'); ?>" onclick="colorSelect($('textcolor'), 'pickcolor')" id="pickcolor" /><input type="button" value="<?php _e('Use Original Color'); ?>" onclick="colorDefault()" id="defaultcolor" />
171 <?php wp_nonce_field('custom-header') ?>
172 <input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes &raquo;'); ?>" /></form>
173 <?php } ?>
174
175 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
176 </div>
177 <div class="wrap">
178 <h2><?php _e('Upload New Header Image'); ?></h2><p><?php _e('Here you can upload a custom header image to be shown at the top of your blog instead of the default one. On the next screen you will be able to crop the image.'); ?></p>
179 <p><?php printf(__('Images of exactly <strong>%1$d x %2$d pixels</strong> will be used as-is.'), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT); ?></p>
180
181 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
182 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
183 <input type="hidden" name="action" value="save" />
184 <?php wp_nonce_field('custom-header') ?>
185 <p class="submit">
186 <input type="submit" value="<?php _e('Upload &raquo;'); ?>" />
187 </p>
188 </form>
189
190 </div>
191
192                 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
193 <div class="wrap">
194 <h2><?php _e('Reset Header Image and Color'); ?></h2>
195 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
196 <form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
197 <?php wp_nonce_field('custom-header'); ?>
198 <input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
199 </form>
200 </div>
201                 <?php endif;
202
203         }
204
205         function step_2() {
206                 check_admin_referer('custom-header');
207                 $overrides = array('test_form' => false);
208                 $file = wp_handle_upload($_FILES['import'], $overrides);
209
210                 if ( isset($file['error']) )
211                 die( $file['error'] );
212
213                 $url = $file['url'];
214                 $file = $file['file'];
215                 $filename = basename($file);
216
217                 // Construct the object array
218                 $object = array(
219                 'post_title' => $filename,
220                 'post_content' => $url,
221                 'post_mime_type' => 'import',
222                 'guid' => $url);
223
224                 // Save the data
225                 $id = wp_insert_attachment($object, $file);
226
227                 $upload = array('file' => $file, 'id' => $id);
228
229                 list($width, $height, $type, $attr) = getimagesize( $file );
230
231                 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
232                         set_theme_mod('header_image', clean_url($url));
233                         $header = apply_filters('wp_create_file_in_uploads', $file, $id); // For replication
234                         return $this->finished();
235                 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
236                         $oitar = $width / HEADER_IMAGE_WIDTH;
237                         $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
238                         $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
239
240                         $url = str_replace(basename($url), basename($image), $url);
241                         $width = $width / $oitar;
242                         $height = $height / $oitar;
243                 } else {
244                         $oitar = 1;
245                 }
246                 ?>
247
248 <div class="wrap">
249
250 <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
251
252 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
253 <div id="testWrap">
254 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
255 </div>
256
257 <p class="submit">
258 <input type="hidden" name="x1" id="x1" />
259 <input type="hidden" name="y1" id="y1" />
260 <input type="hidden" name="x2" id="x2" />
261 <input type="hidden" name="y2" id="y2" />
262 <input type="hidden" name="width" id="width" />
263 <input type="hidden" name="height" id="height" />
264 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
265 <input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
266 <?php wp_nonce_field('custom-header') ?>
267 <input type="submit" value="<?php _e('Crop Header &raquo;'); ?>" />
268 </p>
269
270 </form>
271 </div>
272                 <?php
273         }
274
275         function step_3() {
276                 check_admin_referer('custom-header');
277                 if ( $_POST['oitar'] > 1 ) {
278                         $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
279                         $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
280                         $_POST['width'] = $_POST['width'] * $_POST['oitar'];
281                         $_POST['height'] = $_POST['height'] * $_POST['oitar'];
282                 }
283
284                 $header = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
285                 $header = apply_filters('wp_create_file_in_uploads', $header); // For replication
286
287                 $parent = get_post($_POST['attachment_id']);
288
289                 $parent_url = $parent->guid;
290
291                 $url = str_replace(basename($parent_url), basename($header), $parent_url);
292
293                 set_theme_mod('header_image', $url);
294
295                 // cleanup
296                 $file = get_attached_file( $_POST['attachment_id'] );
297                 $medium = str_replace(basename($file), 'midsize-'.basename($file), $file);
298                 @unlink( apply_filters( 'wp_delete_file', $medium ) );
299                 wp_delete_attachment( $_POST['attachment_id'] );
300
301                 return $this->finished();
302         }
303
304         function finished() {
305                 ?>
306 <div class="wrap">
307 <h2><?php _e('Header complete!'); ?></h2>
308
309 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
310
311 </div>
312                 <?php
313         }
314
315         function admin_page() {
316                 if ( !isset( $_GET['step'] ) )
317                         $step = 1;
318                 else
319                         $step = (int) $_GET['step'];
320
321                 if ( 1 == $step ) {
322                         $this->step_1();
323                 } elseif ( 2 == $step ) {
324                         $this->step_2();
325                 } elseif ( 3 == $step ) {
326                         $this->step_3();
327                 }
328
329         }
330
331 }
332 ?>