home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / custom-header.php < prev    next >
Encoding:
PHP Script  |  2008-05-27  |  11.4 KB  |  365 lines

  1. <?php
  2.  
  3. class Custom_Image_Header {
  4.     var $admin_header_callback;
  5.  
  6.     function Custom_Image_Header($admin_header_callback) {
  7.         $this->admin_header_callback = $admin_header_callback;
  8.     }
  9.  
  10.     function init() {
  11.         $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
  12.  
  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);
  17.     }
  18.  
  19.     function step() {
  20.         $step = (int) @$_GET['step'];
  21.         if ( $step < 1 || 3 < $step )
  22.             $step = 1;
  23.         return $step;
  24.     }
  25.  
  26.     function js_includes() {
  27.         $step = $this->step();
  28.         if ( 1 == $step )
  29.             wp_enqueue_script('colorpicker');
  30.         elseif ( 2 == $step )    
  31.             wp_enqueue_script('cropper');
  32.     }
  33.  
  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');
  39.             } else {
  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);
  43.             }
  44.         }
  45.         if ( isset($_POST['resetheader']) ) {
  46.             check_admin_referer('custom-header');
  47.             remove_theme_mods();
  48.         }
  49.     }
  50.  
  51.     function js() {
  52.         $step = $this->step();
  53.         if ( 1 == $step )
  54.             $this->js_1();
  55.         elseif ( 2 == $step )
  56.             $this->js_2();
  57.     }
  58.  
  59.     function js_1() { ?>
  60. <script type="text/javascript">
  61.     var cp = new ColorPicker();
  62.  
  63.     function pickColor(color) {
  64.         $('name').style.color = color;
  65.         $('desc').style.color = color;
  66.         $('textcolor').value = color;
  67.     }
  68.     function PopupWindow_hidePopup(magicword) {
  69.         if ( magicword != 'prettyplease' )
  70.             return false;
  71.         if (this.divName != null) {
  72.             if (this.use_gebi) {
  73.                 document.getElementById(this.divName).style.visibility = "hidden";
  74.             }
  75.             else if (this.use_css) {
  76.                 document.all[this.divName].style.visibility = "hidden";
  77.             }
  78.             else if (this.use_layers) {
  79.                 document.layers[this.divName].visibility = "hidden";
  80.             }
  81.         }
  82.         else {
  83.             if (this.popupWindow && !this.popupWindow.closed) {
  84.                 this.popupWindow.close();
  85.                 this.popupWindow = null;
  86.             }
  87.         }
  88.         return false;
  89.     }
  90.     function colorSelect(t,p) {
  91.         if ( cp.p == p && document.getElementById(cp.divName).style.visibility != "hidden" ) {
  92.             cp.hidePopup('prettyplease');
  93.         } else {
  94.             cp.p = p;
  95.             cp.select(t,p);
  96.         }
  97.     }
  98.     function colorDefault() {
  99.         pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
  100.     }
  101.  
  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 );
  111.     }
  112.  
  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 );
  122.     }
  123.  
  124.     <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
  125. Event.observe( window, 'load', hide_text );
  126.     <?php } ?>
  127.  
  128. </script>
  129. <?php
  130.     }
  131.  
  132.     function js_2() { ?>
  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;
  141.     }
  142.  
  143.     // with a supplied ratio
  144.     Event.observe(
  145.         window,
  146.         'load',
  147.         function() {
  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 ) {
  155.                     yinit = yimg;
  156.                     xinit = yinit * ratio;
  157.                 } else {
  158.                     xinit = ximg;
  159.                     yinit = xinit / ratio;
  160.                 }
  161.             }
  162.             new Cropper.Img(
  163.                 'upload',
  164.                 {
  165.                     ratioDim: { x: xinit, y: yinit },
  166.                     displayOnInit: true,
  167.                     onEndCrop: onEndCrop
  168.                 }
  169.             )
  170.         }
  171.     );
  172. </script>
  173. <?php
  174.     }
  175.  
  176.     function step_1() {
  177.         if ( $_GET['updated'] ) { ?>
  178. <div id="message" class="updated fade">
  179. <p><?php _e('Header updated.') ?></p>
  180. </div>
  181.         <?php } ?>
  182.  
  183. <div class="wrap">
  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>
  186.  
  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>
  190. </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>
  197. <?php } ?>
  198.  
  199. <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;visibility:hidden;"> </div>
  200. </div>
  201. <div class="wrap">
  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>
  204.  
  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') ?>
  209. <p class="submit">
  210. <input type="submit" value="<?php _e('Upload'); ?>" />
  211. </p>
  212. </form>
  213.  
  214. </div>
  215.  
  216.         <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
  217. <div class="wrap">
  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'); ?>" />
  223. </form>
  224. </div>
  225.         <?php endif;
  226.  
  227.     }
  228.  
  229.     function step_2() {
  230.         check_admin_referer('custom-header');
  231.         $overrides = array('test_form' => false);
  232.         $file = wp_handle_upload($_FILES['import'], $overrides);
  233.  
  234.         if ( isset($file['error']) )
  235.         die( $file['error'] );
  236.  
  237.         $url = $file['url'];
  238.         $type = $file['type'];
  239.         $file = $file['file'];
  240.         $filename = basename($file);
  241.  
  242.         // Construct the object array
  243.         $object = array(
  244.         'post_title' => $filename,
  245.         'post_content' => $url,
  246.         'post_mime_type' => $type,
  247.         'guid' => $url);
  248.  
  249.         // Save the data
  250.         $id = wp_insert_attachment($object, $file);
  251.  
  252.         list($width, $height, $type, $attr) = getimagesize( $file );
  253.  
  254.         if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
  255.             // Add the meta-data
  256.             wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  257.  
  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
  265.  
  266.             $url = str_replace(basename($url), basename($image), $url);
  267.             $width = $width / $oitar;
  268.             $height = $height / $oitar;
  269.         } else {
  270.             $oitar = 1;
  271.         }
  272.         ?>
  273.  
  274. <div class="wrap">
  275.  
  276. <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
  277.  
  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; ?>" />
  281. </div>
  282.  
  283. <p class="submit">
  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'); ?>" />
  294. </p>
  295.  
  296. </form>
  297. </div>
  298.         <?php
  299.     }
  300.  
  301.     function step_3() {
  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'];
  308.         }
  309.  
  310.         $original = get_attached_file( $_POST['attachment_id'] );
  311.  
  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
  314.  
  315.         $parent = get_post($_POST['attachment_id']);
  316.         $parent_url = $parent->guid;
  317.         $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
  318.  
  319.         // Construct the object array
  320.         $object = array(
  321.             'ID' => $_POST['attachment_id'],
  322.             'post_title' => basename($cropped),
  323.             'post_content' => $url,
  324.             'post_mime_type' => 'image/jpeg',
  325.             'guid' => $url
  326.         );
  327.  
  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 ) );
  331.  
  332.         set_theme_mod('header_image', $url);
  333.  
  334.         // cleanup
  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 ) );
  338.  
  339.         return $this->finished();
  340.     }
  341.  
  342.     function finished() {
  343.         ?>
  344. <div class="wrap">
  345. <h2><?php _e('Header complete!'); ?></h2>
  346.  
  347. <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
  348.  
  349. </div>
  350.         <?php
  351.     }
  352.  
  353.     function admin_page() {
  354.         $step = $this->step();
  355.         if ( 1 == $step )
  356.             $this->step_1();
  357.         elseif ( 2 == $step )
  358.             $this->step_2();
  359.         elseif ( 3 == $step )
  360.             $this->step_3();
  361.     }
  362.  
  363. }
  364. ?>
  365.