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

Revision 8656, 12.9 kB (checked in by westi, 6 days ago)

More phpdoc updates for wp-adming. See #7496 props santosj.

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