3 * The custom header image script.
6 * @subpackage Administration
10 * The custom header image class.
14 * @subpackage Administration
16 class Custom_Image_Header {
19 * Callback for administration header.
25 var $admin_header_callback;
28 * PHP4 Constructor - Register administration header callback.
31 * @param callback $admin_header_callback
32 * @return Custom_Image_Header
34 function Custom_Image_Header($admin_header_callback) {
35 $this->admin_header_callback = $admin_header_callback;
39 * Setup the hooks for the Custom Header admin page.
44 $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
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);
54 * Get the current step.
58 * @return int Current step
61 if ( ! isset( $_GET['step'] ) )
64 $step = (int) $_GET['step'];
65 if ( $step < 1 || 3 < $step )
72 * Setup the enqueue for the JavaScript files.
76 function js_includes() {
77 $step = $this->step();
80 wp_enqueue_script('farbtastic');
82 wp_enqueue_script('cropper');
86 * Setup the enqueue for the CSS files
90 function css_includes() {
91 $step = $this->step();
94 wp_enqueue_style('farbtastic');
99 * Execute custom header modification.
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');
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);
114 if ( isset($_POST['resetheader']) ) {
115 check_admin_referer('custom-header');
121 * Execute Javascript depending on step.
126 $step = $this->step();
129 elseif ( 2 == $step )
134 * Display Javascript based on Step 1.
139 <script type="text/javascript">
140 var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
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);
150 jQuery(document).ready(function() {
151 jQuery('#pickcolor').click(function() {
152 jQuery('#colorPickerDiv').show();
155 jQuery('#hidetext').click(function() {
159 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
160 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
162 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
167 jQuery(document).mousedown(function(){
168 // Make the picker disappear, since we're using it in an independant div
172 function colorDefault() {
173 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
176 function hide_picker(what) {
178 jQuery('#colorPickerDiv').each(function(){
179 var id = jQuery(this).attr('id');
183 var display = jQuery(this).css('display');
184 if (display == 'block') {
185 jQuery(this).fadeOut(2);
190 function toggle_text(force) {
191 if(jQuery('#textcolor').val() == 'blank') {
193 jQuery( buttons.toString() ).show();
194 jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
195 jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
199 jQuery( buttons.toString() ).hide();
200 jQuery('#textcolor').val('blank');
201 jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
212 * Display Javascript based on Step 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);
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 ) {
237 xinit = yinit * ratio;
240 yinit = xinit / ratio;
246 ratioDim: { x: xinit, y: yinit },
257 * Display first step of custom header image page.
262 if ( $_GET['updated'] ) { ?>
263 <div id="message" class="updated fade">
264 <p><?php _e('Header updated.') ?></p>
269 <?php screen_icon(); ?>
270 <h2><?php _e('Your Header Image'); ?></h2>
271 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
273 <div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
274 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
275 <div id="desc"><?php bloginfo('description');?></div>
277 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
278 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&updated=true') ?>">
279 <input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
280 <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" />
281 <?php wp_nonce_field('custom-header') ?>
282 <input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
285 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
288 <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>
289 <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>
291 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
292 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
293 <input type="hidden" name="action" value="save" />
294 <?php wp_nonce_field('custom-header') ?>
296 <input type="submit" value="<?php _e('Upload'); ?>" />
302 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
304 <h2><?php _e('Reset Header Image and Color'); ?></h2>
305 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
306 <form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
307 <?php wp_nonce_field('custom-header'); ?>
308 <input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
316 * Display second step of custom header image page.
321 check_admin_referer('custom-header');
322 $overrides = array('test_form' => false);
323 $file = wp_handle_upload($_FILES['import'], $overrides);
325 if ( isset($file['error']) )
326 die( $file['error'] );
329 $type = $file['type'];
330 $file = $file['file'];
331 $filename = basename($file);
333 // Construct the object array
335 'post_title' => $filename,
336 'post_content' => $url,
337 'post_mime_type' => $type,
341 $id = wp_insert_attachment($object, $file);
343 list($width, $height, $type, $attr) = getimagesize( $file );
345 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
347 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
349 set_theme_mod('header_image', clean_url($url));
350 do_action('wp_create_file_in_uploads', $file, $id); // For replication
351 return $this->finished();
352 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
353 $oitar = $width / HEADER_IMAGE_WIDTH;
354 $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
355 $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
357 $url = str_replace(basename($url), basename($image), $url);
358 $width = $width / $oitar;
359 $height = $height / $oitar;
367 <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
369 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
370 <div id="testWrap" style="position: relative">
371 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
375 <input type="hidden" name="x1" id="x1" />
376 <input type="hidden" name="y1" id="y1" />
377 <input type="hidden" name="x2" id="x2" />
378 <input type="hidden" name="y2" id="y2" />
379 <input type="hidden" name="width" id="width" />
380 <input type="hidden" name="height" id="height" />
381 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
382 <input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
383 <?php wp_nonce_field('custom-header') ?>
384 <input type="submit" value="<?php _e('Crop Header'); ?>" />
393 * Display third step of custom header image page.
398 check_admin_referer('custom-header');
399 if ( $_POST['oitar'] > 1 ) {
400 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
401 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
402 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
403 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
406 $original = get_attached_file( $_POST['attachment_id'] );
408 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
409 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
411 $parent = get_post($_POST['attachment_id']);
412 $parent_url = $parent->guid;
413 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
415 // Construct the object array
417 'ID' => $_POST['attachment_id'],
418 'post_title' => basename($cropped),
419 'post_content' => $url,
420 'post_mime_type' => 'image/jpeg',
424 // Update the attachment
425 wp_insert_attachment($object, $cropped);
426 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
428 set_theme_mod('header_image', $url);
431 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
432 @unlink( apply_filters( 'wp_delete_file', $medium ) );
433 @unlink( apply_filters( 'wp_delete_file', $original ) );
435 return $this->finished();
439 * Display last step of custom header image page.
443 function finished() {
446 <h2><?php _e('Header complete!'); ?></h2>
448 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
455 * Display the page based on the current step.
459 function admin_page() {
460 $step = $this->step();
463 elseif ( 2 == $step )
465 elseif ( 3 == $step )