]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/custom-header.php
Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-admin / custom-header.php
index 8d605655df0287022e0e30f4ad81a340e781ec7a..b1f583d144071bfad226299221ea01b41e45f602 100644 (file)
@@ -11,17 +11,27 @@ class Custom_Image_Header {
                $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'));
                $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);
        }
 
                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'] ) {
                if ( isset( $_POST['textcolor'] ) ) {
                        check_admin_referer('custom-header');
                        if ( 'blank' == $_POST['textcolor'] ) {
@@ -36,48 +46,18 @@ class Custom_Image_Header {
                        check_admin_referer('custom-header');
                        remove_theme_mods();
                }
                        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) {
        var cp = new ColorPicker();
 
        function pickColor(color) {
@@ -116,7 +96,7 @@ class Custom_Image_Header {
                }
        }
        function colorDefault() {
                }
        }
        function colorDefault() {
-               pickColor('<?php echo HEADER_TEXTCOLOR; ?>');
+               pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
        }
 
        function hide_text() {
        }
 
        function hide_text() {
@@ -149,6 +129,50 @@ Event.observe( window, 'load', hide_text );
 <?php
        }
 
 <?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">
        function step_1() {
                if ( $_GET['updated'] ) { ?>
 <div id="message" class="updated fade">
@@ -165,11 +189,11 @@ Event.observe( window, 'load', hide_text );
 <div id="desc"><?php bloginfo('description');?></div>
 </div>
 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
 <div id="desc"><?php bloginfo('description');?></div>
 </div>
 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
-<form method="post" action="<?php echo get_option('siteurl') ?>/wp-admin/themes.php?page=custom-header&amp;updated=true">
+<form method="post" action="<?php echo admin_url('themes.php?page=custom-header&amp;updated=true') ?>">
 <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="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 &raquo;'); ?>" /></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>
 <?php } ?>
 
 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
@@ -183,7 +207,7 @@ Event.observe( window, 'load', hide_text );
 <input type="hidden" name="action" value="save" />
 <?php wp_nonce_field('custom-header') ?>
 <p class="submit">
 <input type="hidden" name="action" value="save" />
 <?php wp_nonce_field('custom-header') ?>
 <p class="submit">
-<input type="submit" value="<?php _e('Upload &raquo;'); ?>" />
+<input type="submit" value="<?php _e('Upload'); ?>" />
 </p>
 </form>
 
 </p>
 </form>
 
@@ -211,6 +235,7 @@ Event.observe( window, 'load', hide_text );
                die( $file['error'] );
 
                $url = $file['url'];
                die( $file['error'] );
 
                $url = $file['url'];
+               $type = $file['type'];
                $file = $file['file'];
                $filename = basename($file);
 
                $file = $file['file'];
                $filename = basename($file);
 
@@ -218,19 +243,20 @@ Event.observe( window, 'load', hide_text );
                $object = array(
                'post_title' => $filename,
                'post_content' => $url,
                $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);
 
                '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 ) {
                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));
                        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;
                        return $this->finished();
                } elseif ( $width > HEADER_IMAGE_WIDTH ) {
                        $oitar = $width / HEADER_IMAGE_WIDTH;
@@ -250,7 +276,7 @@ Event.observe( window, 'load', hide_text );
 <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>
 <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>
 
 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
 </div>
 
@@ -264,7 +290,7 @@ Event.observe( window, 'load', hide_text );
 <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="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 &raquo;'); ?>" />
+<input type="submit" value="<?php _e('Crop Header'); ?>" />
 </p>
 
 </form>
 </p>
 
 </form>
@@ -281,22 +307,34 @@ Event.observe( window, 'load', hide_text );
                        $_POST['height'] = $_POST['height'] * $_POST['oitar'];
                }
 
                        $_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;
                $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
 
                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 ) );
                @unlink( apply_filters( 'wp_delete_file', $medium ) );
-               wp_delete_attachment( $_POST['attachment_id'] );
+               @unlink( apply_filters( 'wp_delete_file', $original ) );
 
                return $this->finished();
        }
 
                return $this->finished();
        }
@@ -313,19 +351,13 @@ Event.observe( window, 'load', hide_text );
        }
 
        function admin_page() {
        }
 
        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();
                        $this->step_1();
-               } elseif ( 2 == $step ) {
+               elseif ( 2 == $step )
                        $this->step_2();
                        $this->step_2();
-               } elseif ( 3 == $step ) {
+               elseif ( 3 == $step )
                        $this->step_3();
                        $this->step_3();
-               }
-
        }
 
 }
        }
 
 }