$page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
+ add_action("admin_head-$page", array(&$this, 'take_action'), 50);
add_action("admin_head-$page", array(&$this, 'js'), 50);
add_action("admin_head-$page", $this->admin_header_callback, 51);
}
- function js_includes() {
- wp_enqueue_script('cropper');
- wp_enqueue_script('colorpicker');
+ function step() {
+ $step = (int) @$_GET['step'];
+ if ( $step < 1 || 3 < $step )
+ $step = 1;
+ return $step;
}
- function js() {
+ function js_includes() {
+ $step = $this->step();
+ if ( 1 == $step )
+ wp_enqueue_script('colorpicker');
+ elseif ( 2 == $step )
+ wp_enqueue_script('cropper');
+ }
+ function take_action() {
if ( isset( $_POST['textcolor'] ) ) {
check_admin_referer('custom-header');
if ( 'blank' == $_POST['textcolor'] ) {
check_admin_referer('custom-header');
remove_theme_mods();
}
- ?>
-<script type="text/javascript">
-
- function onEndCrop( coords, dimensions ) {
- $( 'x1' ).value = coords.x1;
- $( 'y1' ).value = coords.y1;
- $( 'x2' ).value = coords.x2;
- $( 'y2' ).value = coords.y2;
- $( 'width' ).value = dimensions.width;
- $( 'height' ).value = dimensions.height;
}
- // with a supplied ratio
- Event.observe(
- window,
- 'load',
- function() {
- var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
- var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
- var ratio = xinit / yinit;
- var ximg = $('upload').width;
- var yimg = $('upload').height;
- if ( yimg < yinit || ximg < xinit ) {
- if ( ximg / yimg > ratio ) {
- yinit = yimg;
- xinit = yinit * ratio;
- } else {
- xinit = ximg;
- yinit = xinit / ratio;
- }
- }
- new Cropper.Img(
- 'upload',
- {
- ratioDim: { x: xinit, y: yinit },
- displayOnInit: true,
- onEndCrop: onEndCrop
- }
- )
- }
- );
+ function js() {
+ $step = $this->step();
+ if ( 1 == $step )
+ $this->js_1();
+ elseif ( 2 == $step )
+ $this->js_2();
+ }
+ function js_1() { ?>
+<script type="text/javascript">
var cp = new ColorPicker();
function pickColor(color) {
}
}
function colorDefault() {
- pickColor('<?php echo HEADER_TEXTCOLOR; ?>');
+ pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
}
function hide_text() {
<?php
}
+ function js_2() { ?>
+<script type="text/javascript">
+ function onEndCrop( coords, dimensions ) {
+ $( 'x1' ).value = coords.x1;
+ $( 'y1' ).value = coords.y1;
+ $( 'x2' ).value = coords.x2;
+ $( 'y2' ).value = coords.y2;
+ $( 'width' ).value = dimensions.width;
+ $( 'height' ).value = dimensions.height;
+ }
+
+ // with a supplied ratio
+ Event.observe(
+ window,
+ 'load',
+ function() {
+ var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
+ var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
+ var ratio = xinit / yinit;
+ var ximg = $('upload').width;
+ var yimg = $('upload').height;
+ if ( yimg < yinit || ximg < xinit ) {
+ if ( ximg / yimg > ratio ) {
+ yinit = yimg;
+ xinit = yinit * ratio;
+ } else {
+ xinit = ximg;
+ yinit = xinit / ratio;
+ }
+ }
+ new Cropper.Img(
+ 'upload',
+ {
+ ratioDim: { x: xinit, y: yinit },
+ displayOnInit: true,
+ onEndCrop: onEndCrop
+ }
+ )
+ }
+ );
+</script>
+<?php
+ }
+
function step_1() {
if ( $_GET['updated'] ) { ?>
<div id="message" class="updated fade">
<input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
<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" />
<?php wp_nonce_field('custom-header') ?>
-<input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes »'); ?>" /></form>
+<input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
<?php } ?>
<div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
<input type="hidden" name="action" value="save" />
<?php wp_nonce_field('custom-header') ?>
<p class="submit">
-<input type="submit" value="<?php _e('Upload »'); ?>" />
+<input type="submit" value="<?php _e('Upload'); ?>" />
</p>
</form>
die( $file['error'] );
$url = $file['url'];
+ $type = $file['type'];
$file = $file['file'];
$filename = basename($file);
$object = array(
'post_title' => $filename,
'post_content' => $url,
- 'post_mime_type' => 'import',
+ 'post_mime_type' => $type,
'guid' => $url);
// Save the data
$id = wp_insert_attachment($object, $file);
- $upload = array('file' => $file, 'id' => $id);
-
list($width, $height, $type, $attr) = getimagesize( $file );
if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
+ // Add the meta-data
+ wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
+
set_theme_mod('header_image', clean_url($url));
- $header = apply_filters('wp_create_file_in_uploads', $file, $id); // For replication
+ do_action('wp_create_file_in_uploads', $file, $id); // For replication
return $this->finished();
} elseif ( $width > HEADER_IMAGE_WIDTH ) {
$oitar = $width / HEADER_IMAGE_WIDTH;
<form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
<p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
-<div id="testWrap">
+<div id="testWrap" style="position: relative">
<img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
</div>
<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
<input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
<?php wp_nonce_field('custom-header') ?>
-<input type="submit" value="<?php _e('Crop Header »'); ?>" />
+<input type="submit" value="<?php _e('Crop Header'); ?>" />
</p>
</form>
$_POST['height'] = $_POST['height'] * $_POST['oitar'];
}
- $header = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
- $header = apply_filters('wp_create_file_in_uploads', $header); // For replication
+ $original = get_attached_file( $_POST['attachment_id'] );
- $parent = get_post($_POST['attachment_id']);
+ $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
+ $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
+ $parent = get_post($_POST['attachment_id']);
$parent_url = $parent->guid;
+ $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
+
+ // Construct the object array
+ $object = array(
+ 'ID' => $_POST['attachment_id'],
+ 'post_title' => basename($cropped),
+ 'post_content' => $url,
+ 'post_mime_type' => 'image/jpeg',
+ 'guid' => $url
+ );
- $url = str_replace(basename($parent_url), basename($header), $parent_url);
+ // Update the attachment
+ wp_insert_attachment($object, $cropped);
+ wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
set_theme_mod('header_image', $url);
// cleanup
- $file = get_attached_file( $_POST['attachment_id'] );
- $medium = str_replace(basename($file), 'midsize-'.basename($file), $file);
+ $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
@unlink( apply_filters( 'wp_delete_file', $medium ) );
- wp_delete_attachment( $_POST['attachment_id'] );
+ @unlink( apply_filters( 'wp_delete_file', $original ) );
return $this->finished();
}
}
function admin_page() {
- if ( !isset( $_GET['step'] ) )
- $step = 1;
- else
- $step = (int) $_GET['step'];
-
- if ( 1 == $step ) {
+ $step = $this->step();
+ if ( 1 == $step )
$this->step_1();
- } elseif ( 2 == $step ) {
+ elseif ( 2 == $step )
$this->step_2();
- } elseif ( 3 == $step ) {
+ elseif ( 3 == $step )
$this->step_3();
- }
-
}
}