root/trunk/wp-admin/custom-header.php

Revision 1519, 12.8 kB (checked in by donncha, 6 days ago)

WP Merge

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * The custom header image script.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * The custom header image class.
11  *
12  * @since unknown
13  * @package WordPress
14  * @subpackage Administration
15  */
16 class Custom_Image_Header {
17
18     /**
19      * Callback for administration header.
20      *
21      * @var callback
22      * @since unknown
23      * @access private
24      */
25     var $admin_header_callback;
26
27     /**
28      * PHP4 Constructor - Register administration header callback.
29      *
30      * @since unknown
31      * @param callback $admin_header_callback
32      * @return Custom_Image_Header
33      */
34     function Custom_Image_Header($admin_header_callback) {
35         $this->admin_header_callback = $admin_header_callback;
36     }
37
38     /**
39      * Setup the hooks for the Custom Header admin page.
40      *
41      * @since unknown
42      */
43     function init() {
44         $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
45
46         add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
47         add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
48         add_action("admin_head-$page", array(&$this, 'take_action'), 50);
49         add_action("admin_head-$page", array(&$this, 'js'), 50);
50         add_action("admin_head-$page", $this->admin_header_callback, 51);
51     }
52
53     /**
54      * Get the current step.
55      *
56      * @since unknown
57      *
58      * @return int Current step
59      */
60     function step() {
61         if ( ! isset( $_GET['step'] ) )
62             return 1;
63
64         $step = (int) $_GET['step'];
65         if ( $step < 1 || 3 < $step )
66             $step = 1;
67
68         return $step;
69     }
70
71     /**
72      * Setup the enqueue for the JavaScript files.
73      *
74      * @since unknown
75      */
76     function js_includes() {
77         $step = $this->step();
78
79         if ( 1 == $step )
80             wp_enqueue_script('farbtastic');
81         elseif ( 2 == $step )
82             wp_enqueue_script('cropper');
83     }
84
85     /**
86      * Setup the enqueue for the CSS files
87      *
88      * @since 2.7
89      */
90     function css_includes() {
91         $step = $this->step();
92
93         if ( 1 == $step ) {
94             wp_enqueue_style('farbtastic');
95         }
96     }
97
98     /**
99      * Execute custom header modification.
100      *
101      * @since unknown
102      */
103     function take_action() {
104         if ( isset( $_POST['textcolor'] ) ) {
105             check_admin_referer('custom-header');
106             if ( 'blank' == $_POST['textcolor'] ) {
107                 set_theme_mod('header_textcolor', 'blank');
108             } else {
109                 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
110                 if ( strlen($color) == 6 || strlen($color) == 3 )
111                     set_theme_mod('header_textcolor', $color);
112             }
113         }
114         if ( isset($_POST['resetheader']) ) {
115             check_admin_referer('custom-header');
116             remove_theme_mods();
117         }
118     }
119
120     /**
121      * Execute Javascript depending on step.
122      *
123      * @since unknown
124      */
125     function js() {
126         $step = $this->step();
127         if ( 1 == $step )
128             $this->js_1();
129         elseif ( 2 == $step )
130             $this->js_2();
131     }
132
133     /**
134      * Display Javascript based on Step 1.
135      *
136      * @since unknown
137      */
138     function js_1() { ?>
139 <script type="text/javascript">
140     var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
141     var farbtastic;
142
143     function pickColor(color) {
144         jQuery('#name').css('color', color);
145         jQuery('#desc').css('color', color);
146         jQuery('#textcolor').val(color);
147         farbtastic.setColor(color);
148     }
149    
150     jQuery(document).ready(function() {
151         jQuery('#pickcolor').click(function() {
152             jQuery('#colorPickerDiv').show();
153         });
154        
155         jQuery('#hidetext').click(function() {
156             toggle_text();
157         });
158        
159         farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
160         pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
161        
162         <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
163         toggle_text();
164         <?php } ?>
165     });
166    
167     jQuery(document).mousedown(function(){
168         // Make the picker disappear, since we're using it in an independant div
169         hide_picker();
170     });
171
172     function colorDefault() {
173         pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
174     }
175
176     function hide_picker(what) {
177         var update = false;
178         jQuery('#colorPickerDiv').each(function(){
179             var id = jQuery(this).attr('id');
180             if (id == what) {
181                 return;
182             }
183             var display = jQuery(this).css('display');
184             if (display == 'block') {
185                 jQuery(this).fadeOut(2);
186             }
187         });
188     }
189
190     function toggle_text(force) {
191         if(jQuery('#textcolor').val() == 'blank') {
192             //Show text
193             jQuery( buttons.toString() ).show();
194             jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
195             jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
196         }
197         else {
198             //Hide text
199             jQuery( buttons.toString() ).hide();
200             jQuery('#textcolor').val('blank');
201             jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
202         }
203     }
204
205
206
207 </script>
208 <?php
209     }
210
211     /**
212      * Display Javascript based on Step 2.
213      *
214      * @since unknown
215      */
216     function js_2() { ?>
217 <script type="text/javascript">
218     function onEndCrop( coords, dimensions ) {
219         jQuery( '#x1' ).val(coords.x1);
220         jQuery( '#y1' ).val(coords.y1);
221         jQuery( '#x2' ).val(coords.x2);
222         jQuery( '#y2' ).val(coords.y2);
223         jQuery( '#width' ).val(dimensions.width);
224         jQuery( '#height' ).val(dimensions.height);
225     }
226
227     // with a supplied ratio
228     jQuery(document).ready(function() {
229         var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
230         var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
231         var ratio = xinit / yinit;
232         var ximg = jQuery('#upload').width();
233         var yimg = jQuery('#upload').height();
234         if ( yimg < yinit || ximg < xinit ) {
235             if ( ximg / yimg > ratio ) {
236                 yinit = yimg;
237                 xinit = yinit * ratio;
238             } else {
239                 xinit = ximg;
240                 yinit = xinit / ratio;
241             }
242         }
243         new Cropper.Img(
244             'upload',
245             {
246                 ratioDim: { x: xinit, y: yinit },
247                 displayOnInit: true,
248                 onEndCrop: onEndCrop
249             }
250         )
251     });
252 </script>
253 <?php
254     }
255
256     /**
257      * Display first step of custom header image page.
258      *
259      * @since unknown
260      */
261     function step_1() {
262         if ( $_GET['updated'] ) { ?>
263 <div id="message" class="updated fade">
264 <p><?php _e('Header updated.') ?></p>
265 </div>
266         <?php } ?>
267
268 <div class="wrap">
269 <h2><?php _e('Your Header Image'); ?></h2>
270 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
271
272 <div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
273 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
274 <div id="desc"><?php bloginfo('description');?></div>
275 </div>
276 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
277 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&amp;updated=true') ?>">
278 <input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
279 <input type="button" value="<?php _e('Select a Text Color'); ?>" id="pickcolor" /><input type="button" value="<?php _e('Use Original Color'); ?>" onclick="colorDefault()" id="defaultcolor" />
280 <?php wp_nonce_field('custom-header') ?>
281 <input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
282 <?php } ?>
283
284 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
285 </div>
286 <div class="wrap">
287 <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>
288 <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>
289
290 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
291 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
292 <input type="hidden" name="action" value="save" />
293 <?php wp_nonce_field('custom-header') ?>
294 <p class="submit">
295 <input type="submit" value="<?php _e('Upload'); ?>" />
296 </p>
297 </form>
298
299 </div>
300
301         <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
302 <div class="wrap">
303 <h2><?php _e('Reset Header Image and Color'); ?></h2>
304 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
305 <form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
306 <?php wp_nonce_field('custom-header'); ?>
307 <input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
308 </form>
309 </div>
310         <?php endif;
311
312     }
313
314     /**
315      * Display second step of custom header image page.
316      *
317      * @since unknown
318      */
319     function step_2() {
320         check_admin_referer('custom-header');
321         $overrides = array('test_form' => false);
322         $file = wp_handle_upload($_FILES['import'], $overrides);
323
324         if ( isset($file['error']) )
325         die( $file['error'] );
326
327         $url = $file['url'];
328         $type = $file['type'];
329         $file = $file['file'];
330         $filename = basename($file);
331
332         // Construct the object array
333         $object = array(
334         'post_title' => $filename,
335         'post_content' => $url,
336         'post_mime_type' => $type,
337         'guid' => $url);
338
339         // Save the data
340         $id = wp_insert_attachment($object, $file);
341
342         list($width, $height, $type, $attr) = getimagesize( $file );
343
344         if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
345             // Add the meta-data
346             wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
347
348             set_theme_mod('header_image', clean_url($url));
349             do_action('wp_create_file_in_uploads', $file, $id); // For replication
350             return $this->finished();
351         } elseif ( $width > HEADER_IMAGE_WIDTH ) {
352             $oitar = $width / HEADER_IMAGE_WIDTH;
353             $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
354             $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
355
356             $url = str_replace(basename($url), basename($image), $url);
357             $width = $width / $oitar;
358             $height = $height / $oitar;
359         } else {
360             $oitar = 1;
361         }
362         ?>
363
364 <div class="wrap">
365
366 <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
367
368 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
369 <div id="testWrap" style="position: relative">
370 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
371 </div>
372
373 <p class="submit">
374 <input type="hidden" name="x1" id="x1" />
375 <input type="hidden" name="y1" id="y1" />
376 <input type="hidden" name="x2" id="x2" />
377 <input type="hidden" name="y2" id="y2" />
378 <input type="hidden" name="width" id="width" />
379 <input type="hidden" name="height" id="height" />
380 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
381 <input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
382 <?php wp_nonce_field('custom-header') ?>
383 <input type="submit" value="<?php _e('Crop Header'); ?>" />
384 </p>
385
386 </form>
387 </div>
388         <?php
389     }
390
391     /**
392      * Display third step of custom header image page.
393      *
394      * @since unknown
395      */
396     function step_3() {
397         check_admin_referer('custom-header');
398         if ( $_POST['oitar'] > 1 ) {
399             $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
400             $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
401             $_POST['width'] = $_POST['width'] * $_POST['oitar'];
402             $_POST['height'] = $_POST['height'] * $_POST['oitar'];
403         }
404
405         $original = get_attached_file( $_POST['attachment_id'] );
406
407         $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
408         $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
409
410         $parent = get_post($_POST['attachment_id']);
411         $parent_url = $parent->guid;
412         $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
413
414         // Construct the object array
415         $object = array(
416             'ID' => $_POST['attachment_id'],
417             'post_title' => basename($cropped),
418             'post_content' => $url,
419             'post_mime_type' => 'image/jpeg',
420             'guid' => $url
421         );
422
423         // Update the attachment
424         wp_insert_attachment($object, $cropped);
425         wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
426
427         set_theme_mod('header_image', $url);
428
429         // cleanup
430         $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
431         @unlink( apply_filters( 'wp_delete_file', $medium ) );
432         @unlink( apply_filters( 'wp_delete_file', $original ) );
433
434         return $this->finished();
435     }
436
437     /**
438      * Display last step of custom header image page.
439      *
440      * @since unknown
441      */
442     function finished() {
443         ?>
444 <div class="wrap">
445 <h2><?php _e('Header complete!'); ?></h2>
446
447 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
448
449 </div>
450         <?php
451     }
452
453     /**
454      * Display the page based on the current step.
455      *
456      * @since unknown
457      */
458     function admin_page() {
459         $step = $this->step();
460         if ( 1 == $step )
461             $this->step_1();
462         elseif ( 2 == $step )
463             $this->step_2();
464         elseif ( 3 == $step )
465             $this->step_3();
466     }
467
468 }
469 ?>
470
Note: See TracBrowser for help on using the browser.