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 Header'), __('Custom 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('jcrop');
86 * Setup the enqueue for the CSS files
90 function css_includes() {
91 $step = $this->step();
94 wp_enqueue_style('farbtastic');
96 wp_enqueue_style('jcrop');
100 * Execute custom header modification.
104 function take_action() {
105 if ( isset( $_POST['textcolor'] ) ) {
106 check_admin_referer('custom-header');
107 if ( 'blank' == $_POST['textcolor'] ) {
108 set_theme_mod('header_textcolor', 'blank');
110 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
111 if ( strlen($color) == 6 || strlen($color) == 3 )
112 set_theme_mod('header_textcolor', $color);
115 if ( isset($_POST['resetheader']) ) {
116 check_admin_referer('custom-header');
122 * Execute Javascript depending on step.
127 $step = $this->step();
130 elseif ( 2 == $step )
135 * Display Javascript based on Step 1.
140 <script type="text/javascript">
141 var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
144 function pickColor(color) {
145 jQuery('#name').css('color', color);
146 jQuery('#desc').css('color', color);
147 jQuery('#textcolor').val(color);
148 farbtastic.setColor(color);
151 jQuery(document).ready(function() {
152 jQuery('#pickcolor').click(function() {
153 jQuery('#colorPickerDiv').show();
156 jQuery('#hidetext').click(function() {
160 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
161 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
163 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
168 jQuery(document).mousedown(function(){
169 // Make the picker disappear, since we're using it in an independant div
173 function colorDefault() {
174 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
177 function hide_picker(what) {
179 jQuery('#colorPickerDiv').each(function(){
180 var id = jQuery(this).attr('id');
184 var display = jQuery(this).css('display');
185 if (display == 'block') {
186 jQuery(this).fadeOut(2);
191 function toggle_text(force) {
192 if(jQuery('#textcolor').val() == 'blank') {
194 jQuery( buttons.toString() ).show();
195 jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
196 jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
200 jQuery( buttons.toString() ).hide();
201 jQuery('#textcolor').val('blank');
202 jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
213 * Display Javascript based on Step 2.
218 <script type="text/javascript">
219 function onEndCrop( coords ) {
220 jQuery( '#x1' ).val(coords.x);
221 jQuery( '#y1' ).val(coords.y);
222 jQuery( '#x2' ).val(coords.x2);
223 jQuery( '#y2' ).val(coords.y2);
224 jQuery( '#width' ).val(coords.w);
225 jQuery( '#height' ).val(coords.h);
228 // with a supplied ratio
229 jQuery(document).ready(function() {
230 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
231 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
232 var ratio = xinit / yinit;
233 var ximg = jQuery('#upload').width();
234 var yimg = jQuery('#upload').height();
236 //set up default values
237 jQuery( '#x1' ).val(0);
238 jQuery( '#y1' ).val(0);
239 jQuery( '#x2' ).val(xinit);
240 jQuery( '#y2' ).val(yinit);
241 jQuery( '#width' ).val(xinit);
242 jQuery( '#height' ).val(yinit);
244 if ( yimg < yinit || ximg < xinit ) {
245 if ( ximg / yimg > ratio ) {
247 xinit = yinit * ratio;
250 yinit = xinit / ratio;
254 jQuery('#upload').Jcrop({
256 setSelect: [ 0, 0, xinit, yinit ],
265 * Display first step of custom header image page.
270 if ( $_GET['updated'] ) { ?>
271 <div id="message" class="updated fade">
272 <p><?php _e('Header updated.') ?></p>
277 <?php screen_icon(); ?>
278 <h2><?php _e('Your Header Image'); ?></h2>
279 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
281 <div id="headimg" style="background-image: url(<?php esc_url(header_image()) ?>);">
282 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
283 <div id="desc"><?php bloginfo('description');?></div>
285 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
286 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&updated=true') ?>">
287 <input type="button" class="button" value="<?php esc_attr_e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
288 <input type="button" class="button" value="<?php esc_attr_e('Select a Text Color'); ?>" id="pickcolor" /><input type="button" class="button" value="<?php esc_attr_e('Use Original Color'); ?>" onclick="colorDefault()" id="defaultcolor" />
289 <?php wp_nonce_field('custom-header') ?>
290 <input type="hidden" name="textcolor" id="textcolor" value="#<?php esc_attr(header_textcolor()) ?>" /><input name="submit" type="submit" class="button" value="<?php esc_attr_e('Save Changes'); ?>" /></form>
293 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
296 <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>
297 <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>
299 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo esc_attr(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
300 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
301 <input type="hidden" name="action" value="save" />
302 <?php wp_nonce_field('custom-header') ?>
304 <input type="submit" value="<?php esc_attr_e('Upload'); ?>" />
310 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
312 <h2><?php _e('Reset Header Image and Color'); ?></h2>
313 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
314 <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)) ?>">
315 <?php wp_nonce_field('custom-header'); ?>
316 <input type="submit" class="button" name="resetheader" value="<?php esc_attr_e('Restore Original Header'); ?>" />
324 * Display second step of custom header image page.
329 check_admin_referer('custom-header');
330 $overrides = array('test_form' => false);
331 $file = wp_handle_upload($_FILES['import'], $overrides);
333 if ( isset($file['error']) )
334 die( $file['error'] );
337 $type = $file['type'];
338 $file = $file['file'];
339 $filename = basename($file);
341 // Construct the object array
343 'post_title' => $filename,
344 'post_content' => $url,
345 'post_mime_type' => $type,
349 $id = wp_insert_attachment($object, $file);
351 list($width, $height, $type, $attr) = getimagesize( $file );
353 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
355 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
357 set_theme_mod('header_image', esc_url($url));
358 do_action('wp_create_file_in_uploads', $file, $id); // For replication
359 return $this->finished();
360 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
361 $oitar = $width / HEADER_IMAGE_WIDTH;
362 $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
363 $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
365 $url = str_replace(basename($url), basename($image), $url);
366 $width = $width / $oitar;
367 $height = $height / $oitar;
375 <form method="POST" action="<?php echo esc_attr(add_query_arg('step', 3)) ?>">
377 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
378 <div id="testWrap" style="position: relative">
379 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
383 <input type="hidden" name="x1" id="x1" />
384 <input type="hidden" name="y1" id="y1" />
385 <input type="hidden" name="x2" id="x2" />
386 <input type="hidden" name="y2" id="y2" />
387 <input type="hidden" name="width" id="width" />
388 <input type="hidden" name="height" id="height" />
389 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr($id); ?>" />
390 <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr($oitar); ?>" />
391 <?php wp_nonce_field('custom-header') ?>
392 <input type="submit" value="<?php esc_attr_e('Crop Header'); ?>" />
401 * Display third step of custom header image page.
406 check_admin_referer('custom-header');
407 if ( $_POST['oitar'] > 1 ) {
408 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
409 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
410 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
411 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
414 $original = get_attached_file( $_POST['attachment_id'] );
416 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
417 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
419 $parent = get_post($_POST['attachment_id']);
420 $parent_url = $parent->guid;
421 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
423 // Construct the object array
425 'ID' => $_POST['attachment_id'],
426 'post_title' => basename($cropped),
427 'post_content' => $url,
428 'post_mime_type' => 'image/jpeg',
432 // Update the attachment
433 wp_insert_attachment($object, $cropped);
434 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
436 set_theme_mod('header_image', $url);
439 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
440 @unlink( apply_filters( 'wp_delete_file', $medium ) );
441 @unlink( apply_filters( 'wp_delete_file', $original ) );
443 return $this->finished();
447 * Display last step of custom header image page.
451 function finished() {
454 <h2><?php _e('Header complete!'); ?></h2>
456 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
463 * Display the page based on the current step.
467 function admin_page() {
468 $step = $this->step();
471 elseif ( 2 == $step )
473 elseif ( 3 == $step )