3 class Custom_Image_Header {
4 var $admin_header_callback;
6 function Custom_Image_Header($admin_header_callback) {
7 $this->admin_header_callback = $admin_header_callback;
11 $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
13 add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
14 add_action("admin_head-$page", array(&$this, 'take_action'), 50);
15 add_action("admin_head-$page", array(&$this, 'js'), 50);
16 add_action("admin_head-$page", $this->admin_header_callback, 51);
20 $step = (int) @$_GET['step'];
21 if ( $step < 1 || 3 < $step )
26 function js_includes() {
27 $step = $this->step();
29 wp_enqueue_script('colorpicker');
31 wp_enqueue_script('cropper');
34 function take_action() {
35 if ( isset( $_POST['textcolor'] ) ) {
36 check_admin_referer('custom-header');
37 if ( 'blank' == $_POST['textcolor'] ) {
38 set_theme_mod('header_textcolor', 'blank');
40 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
41 if ( strlen($color) == 6 || strlen($color) == 3 )
42 set_theme_mod('header_textcolor', $color);
45 if ( isset($_POST['resetheader']) ) {
46 check_admin_referer('custom-header');
52 $step = $this->step();
60 <script type="text/javascript">
61 var cp = new ColorPicker();
63 function pickColor(color) {
64 $('name').style.color = color;
65 $('desc').style.color = color;
66 $('textcolor').value = color;
68 function PopupWindow_hidePopup(magicword) {
69 if ( magicword != 'prettyplease' )
71 if (this.divName != null) {
73 document.getElementById(this.divName).style.visibility = "hidden";
75 else if (this.use_css) {
76 document.all[this.divName].style.visibility = "hidden";
78 else if (this.use_layers) {
79 document.layers[this.divName].visibility = "hidden";
83 if (this.popupWindow && !this.popupWindow.closed) {
84 this.popupWindow.close();
85 this.popupWindow = null;
90 function colorSelect(t,p) {
91 if ( cp.p == p && document.getElementById(cp.divName).style.visibility != "hidden" ) {
92 cp.hidePopup('prettyplease');
98 function colorDefault() {
99 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
102 function hide_text() {
103 $('name').style.display = 'none';
104 $('desc').style.display = 'none';
105 $('pickcolor').style.display = 'none';
106 $('defaultcolor').style.display = 'none';
107 $('textcolor').value = 'blank';
108 $('hidetext').value = '<?php _e('Show Text'); ?>';
109 // $('hidetext').onclick = 'show_text()';
110 Event.observe( $('hidetext'), 'click', show_text );
113 function show_text() {
114 $('name').style.display = 'block';
115 $('desc').style.display = 'block';
116 $('pickcolor').style.display = 'inline';
117 $('defaultcolor').style.display = 'inline';
118 $('textcolor').value = '<?php echo HEADER_TEXTCOLOR; ?>';
119 $('hidetext').value = '<?php _e('Hide Text'); ?>';
120 Event.stopObserving( $('hidetext'), 'click', show_text );
121 Event.observe( $('hidetext'), 'click', hide_text );
124 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
125 Event.observe( window, 'load', hide_text );
133 <script type="text/javascript">
134 function onEndCrop( coords, dimensions ) {
135 $( 'x1' ).value = coords.x1;
136 $( 'y1' ).value = coords.y1;
137 $( 'x2' ).value = coords.x2;
138 $( 'y2' ).value = coords.y2;
139 $( 'width' ).value = dimensions.width;
140 $( 'height' ).value = dimensions.height;
143 // with a supplied ratio
148 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
149 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
150 var ratio = xinit / yinit;
151 var ximg = $('upload').width;
152 var yimg = $('upload').height;
153 if ( yimg < yinit || ximg < xinit ) {
154 if ( ximg / yimg > ratio ) {
156 xinit = yinit * ratio;
159 yinit = xinit / ratio;
165 ratioDim: { x: xinit, y: yinit },
177 if ( $_GET['updated'] ) { ?>
178 <div id="message" class="updated fade">
179 <p><?php _e('Header updated.') ?></p>
184 <h2><?php _e('Your Header Image'); ?></h2>
185 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
187 <div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
188 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
189 <div id="desc"><?php bloginfo('description');?></div>
191 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
192 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&updated=true') ?>">
193 <input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
194 <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" />
195 <?php wp_nonce_field('custom-header') ?>
196 <input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
199 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
202 <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>
203 <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>
205 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
206 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
207 <input type="hidden" name="action" value="save" />
208 <?php wp_nonce_field('custom-header') ?>
210 <input type="submit" value="<?php _e('Upload'); ?>" />
216 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
218 <h2><?php _e('Reset Header Image and Color'); ?></h2>
219 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
220 <form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
221 <?php wp_nonce_field('custom-header'); ?>
222 <input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
230 check_admin_referer('custom-header');
231 $overrides = array('test_form' => false);
232 $file = wp_handle_upload($_FILES['import'], $overrides);
234 if ( isset($file['error']) )
235 die( $file['error'] );
238 $type = $file['type'];
239 $file = $file['file'];
240 $filename = basename($file);
242 // Construct the object array
244 'post_title' => $filename,
245 'post_content' => $url,
246 'post_mime_type' => $type,
250 $id = wp_insert_attachment($object, $file);
252 list($width, $height, $type, $attr) = getimagesize( $file );
254 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
256 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
258 set_theme_mod('header_image', clean_url($url));
259 do_action('wp_create_file_in_uploads', $file, $id); // For replication
260 return $this->finished();
261 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
262 $oitar = $width / HEADER_IMAGE_WIDTH;
263 $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
264 $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
266 $url = str_replace(basename($url), basename($image), $url);
267 $width = $width / $oitar;
268 $height = $height / $oitar;
276 <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
278 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
279 <div id="testWrap" style="position: relative">
280 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
284 <input type="hidden" name="x1" id="x1" />
285 <input type="hidden" name="y1" id="y1" />
286 <input type="hidden" name="x2" id="x2" />
287 <input type="hidden" name="y2" id="y2" />
288 <input type="hidden" name="width" id="width" />
289 <input type="hidden" name="height" id="height" />
290 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
291 <input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
292 <?php wp_nonce_field('custom-header') ?>
293 <input type="submit" value="<?php _e('Crop Header'); ?>" />
302 check_admin_referer('custom-header');
303 if ( $_POST['oitar'] > 1 ) {
304 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
305 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
306 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
307 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
310 $original = get_attached_file( $_POST['attachment_id'] );
312 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
313 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
315 $parent = get_post($_POST['attachment_id']);
316 $parent_url = $parent->guid;
317 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
319 // Construct the object array
321 'ID' => $_POST['attachment_id'],
322 'post_title' => basename($cropped),
323 'post_content' => $url,
324 'post_mime_type' => 'image/jpeg',
328 // Update the attachment
329 wp_insert_attachment($object, $cropped);
330 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
332 set_theme_mod('header_image', $url);
335 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
336 @unlink( apply_filters( 'wp_delete_file', $medium ) );
337 @unlink( apply_filters( 'wp_delete_file', $original ) );
339 return $this->finished();
342 function finished() {
345 <h2><?php _e('Header complete!'); ?></h2>
347 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
353 function admin_page() {
354 $step = $this->step();
357 elseif ( 2 == $step )
359 elseif ( 3 == $step )