-
+ 3E4322C7D8F1757F883A9F11ABEC332B8C952D7B050C801E4BA60E72853509F0603E0EA63B093DACF4CA158FE309043984900EFA5EA0348C2D1CD9D0EB46388F
mp-wp/wp-admin/custom-header.php
(0 . 0)(1 . 470)
9114 <?php
9115 /**
9116 * The custom header image script.
9117 *
9118 * @package WordPress
9119 * @subpackage Administration
9120 */
9121
9122 /**
9123 * The custom header image class.
9124 *
9125 * @since unknown
9126 * @package WordPress
9127 * @subpackage Administration
9128 */
9129 class Custom_Image_Header {
9130
9131 /**
9132 * Callback for administration header.
9133 *
9134 * @var callback
9135 * @since unknown
9136 * @access private
9137 */
9138 var $admin_header_callback;
9139
9140 /**
9141 * PHP4 Constructor - Register administration header callback.
9142 *
9143 * @since unknown
9144 * @param callback $admin_header_callback
9145 * @return Custom_Image_Header
9146 */
9147 function Custom_Image_Header($admin_header_callback) {
9148 $this->admin_header_callback = $admin_header_callback;
9149 }
9150
9151 /**
9152 * Setup the hooks for the Custom Header admin page.
9153 *
9154 * @since unknown
9155 */
9156 function init() {
9157 $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
9158
9159 add_action("admin_print_scripts-$page", array(&$this, 'js_includes'));
9160 add_action("admin_print_styles-$page", array(&$this, 'css_includes'));
9161 add_action("admin_head-$page", array(&$this, 'take_action'), 50);
9162 add_action("admin_head-$page", array(&$this, 'js'), 50);
9163 add_action("admin_head-$page", $this->admin_header_callback, 51);
9164 }
9165
9166 /**
9167 * Get the current step.
9168 *
9169 * @since unknown
9170 *
9171 * @return int Current step
9172 */
9173 function step() {
9174 if ( ! isset( $_GET['step'] ) )
9175 return 1;
9176
9177 $step = (int) $_GET['step'];
9178 if ( $step < 1 || 3 < $step )
9179 $step = 1;
9180
9181 return $step;
9182 }
9183
9184 /**
9185 * Setup the enqueue for the JavaScript files.
9186 *
9187 * @since unknown
9188 */
9189 function js_includes() {
9190 $step = $this->step();
9191
9192 if ( 1 == $step )
9193 wp_enqueue_script('farbtastic');
9194 elseif ( 2 == $step )
9195 wp_enqueue_script('cropper');
9196 }
9197
9198 /**
9199 * Setup the enqueue for the CSS files
9200 *
9201 * @since 2.7
9202 */
9203 function css_includes() {
9204 $step = $this->step();
9205
9206 if ( 1 == $step ) {
9207 wp_enqueue_style('farbtastic');
9208 }
9209 }
9210
9211 /**
9212 * Execute custom header modification.
9213 *
9214 * @since unknown
9215 */
9216 function take_action() {
9217 if ( isset( $_POST['textcolor'] ) ) {
9218 check_admin_referer('custom-header');
9219 if ( 'blank' == $_POST['textcolor'] ) {
9220 set_theme_mod('header_textcolor', 'blank');
9221 } else {
9222 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
9223 if ( strlen($color) == 6 || strlen($color) == 3 )
9224 set_theme_mod('header_textcolor', $color);
9225 }
9226 }
9227 if ( isset($_POST['resetheader']) ) {
9228 check_admin_referer('custom-header');
9229 remove_theme_mods();
9230 }
9231 }
9232
9233 /**
9234 * Execute Javascript depending on step.
9235 *
9236 * @since unknown
9237 */
9238 function js() {
9239 $step = $this->step();
9240 if ( 1 == $step )
9241 $this->js_1();
9242 elseif ( 2 == $step )
9243 $this->js_2();
9244 }
9245
9246 /**
9247 * Display Javascript based on Step 1.
9248 *
9249 * @since unknown
9250 */
9251 function js_1() { ?>
9252 <script type="text/javascript">
9253 var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
9254 var farbtastic;
9255
9256 function pickColor(color) {
9257 jQuery('#name').css('color', color);
9258 jQuery('#desc').css('color', color);
9259 jQuery('#textcolor').val(color);
9260 farbtastic.setColor(color);
9261 }
9262
9263 jQuery(document).ready(function() {
9264 jQuery('#pickcolor').click(function() {
9265 jQuery('#colorPickerDiv').show();
9266 });
9267
9268 jQuery('#hidetext').click(function() {
9269 toggle_text();
9270 });
9271
9272 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
9273 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
9274
9275 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
9276 toggle_text();
9277 <?php } ?>
9278 });
9279
9280 jQuery(document).mousedown(function(){
9281 // Make the picker disappear, since we're using it in an independant div
9282 hide_picker();
9283 });
9284
9285 function colorDefault() {
9286 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
9287 }
9288
9289 function hide_picker(what) {
9290 var update = false;
9291 jQuery('#colorPickerDiv').each(function(){
9292 var id = jQuery(this).attr('id');
9293 if (id == what) {
9294 return;
9295 }
9296 var display = jQuery(this).css('display');
9297 if (display == 'block') {
9298 jQuery(this).fadeOut(2);
9299 }
9300 });
9301 }
9302
9303 function toggle_text(force) {
9304 if(jQuery('#textcolor').val() == 'blank') {
9305 //Show text
9306 jQuery( buttons.toString() ).show();
9307 jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
9308 jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
9309 }
9310 else {
9311 //Hide text
9312 jQuery( buttons.toString() ).hide();
9313 jQuery('#textcolor').val('blank');
9314 jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
9315 }
9316 }
9317
9318
9319
9320 </script>
9321 <?php
9322 }
9323
9324 /**
9325 * Display Javascript based on Step 2.
9326 *
9327 * @since unknown
9328 */
9329 function js_2() { ?>
9330 <script type="text/javascript">
9331 function onEndCrop( coords, dimensions ) {
9332 jQuery( '#x1' ).val(coords.x1);
9333 jQuery( '#y1' ).val(coords.y1);
9334 jQuery( '#x2' ).val(coords.x2);
9335 jQuery( '#y2' ).val(coords.y2);
9336 jQuery( '#width' ).val(dimensions.width);
9337 jQuery( '#height' ).val(dimensions.height);
9338 }
9339
9340 // with a supplied ratio
9341 jQuery(document).ready(function() {
9342 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
9343 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
9344 var ratio = xinit / yinit;
9345 var ximg = jQuery('#upload').width();
9346 var yimg = jQuery('#upload').height();
9347 if ( yimg < yinit || ximg < xinit ) {
9348 if ( ximg / yimg > ratio ) {
9349 yinit = yimg;
9350 xinit = yinit * ratio;
9351 } else {
9352 xinit = ximg;
9353 yinit = xinit / ratio;
9354 }
9355 }
9356 new Cropper.Img(
9357 'upload',
9358 {
9359 ratioDim: { x: xinit, y: yinit },
9360 displayOnInit: true,
9361 onEndCrop: onEndCrop
9362 }
9363 )
9364 });
9365 </script>
9366 <?php
9367 }
9368
9369 /**
9370 * Display first step of custom header image page.
9371 *
9372 * @since unknown
9373 */
9374 function step_1() {
9375 if ( $_GET['updated'] ) { ?>
9376 <div id="message" class="updated fade">
9377 <p><?php _e('Header updated.') ?></p>
9378 </div>
9379 <?php } ?>
9380
9381 <div class="wrap">
9382 <?php screen_icon(); ?>
9383 <h2><?php _e('Your Header Image'); ?></h2>
9384 <p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
9385
9386 <div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
9387 <h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
9388 <div id="desc"><?php bloginfo('description');?></div>
9389 </div>
9390 <?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
9391 <form method="post" action="<?php echo admin_url('themes.php?page=custom-header&updated=true') ?>">
9392 <input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
9393 <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" />
9394 <?php wp_nonce_field('custom-header') ?>
9395 <input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
9396 <?php } ?>
9397
9398 <div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
9399 </div>
9400 <div class="wrap">
9401 <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>
9402 <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>
9403
9404 <form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
9405 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
9406 <input type="hidden" name="action" value="save" />
9407 <?php wp_nonce_field('custom-header') ?>
9408 <p class="submit">
9409 <input type="submit" value="<?php _e('Upload'); ?>" />
9410 </p>
9411 </form>
9412
9413 </div>
9414
9415 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
9416 <div class="wrap">
9417 <h2><?php _e('Reset Header Image and Color'); ?></h2>
9418 <p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
9419 <form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
9420 <?php wp_nonce_field('custom-header'); ?>
9421 <input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
9422 </form>
9423 </div>
9424 <?php endif;
9425
9426 }
9427
9428 /**
9429 * Display second step of custom header image page.
9430 *
9431 * @since unknown
9432 */
9433 function step_2() {
9434 check_admin_referer('custom-header');
9435 $overrides = array('test_form' => false);
9436 $file = wp_handle_upload($_FILES['import'], $overrides);
9437
9438 if ( isset($file['error']) )
9439 die( $file['error'] );
9440
9441 $url = $file['url'];
9442 $type = $file['type'];
9443 $file = $file['file'];
9444 $filename = basename($file);
9445
9446 // Construct the object array
9447 $object = array(
9448 'post_title' => $filename,
9449 'post_content' => $url,
9450 'post_mime_type' => $type,
9451 'guid' => $url);
9452
9453 // Save the data
9454 $id = wp_insert_attachment($object, $file);
9455
9456 list($width, $height, $type, $attr) = getimagesize( $file );
9457
9458 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
9459 // Add the meta-data
9460 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
9461
9462 set_theme_mod('header_image', clean_url($url));
9463 do_action('wp_create_file_in_uploads', $file, $id); // For replication
9464 return $this->finished();
9465 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
9466 $oitar = $width / HEADER_IMAGE_WIDTH;
9467 $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
9468 $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
9469
9470 $url = str_replace(basename($url), basename($image), $url);
9471 $width = $width / $oitar;
9472 $height = $height / $oitar;
9473 } else {
9474 $oitar = 1;
9475 }
9476 ?>
9477
9478 <div class="wrap">
9479
9480 <form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
9481
9482 <p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
9483 <div id="testWrap" style="position: relative">
9484 <img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
9485 </div>
9486
9487 <p class="submit">
9488 <input type="hidden" name="x1" id="x1" />
9489 <input type="hidden" name="y1" id="y1" />
9490 <input type="hidden" name="x2" id="x2" />
9491 <input type="hidden" name="y2" id="y2" />
9492 <input type="hidden" name="width" id="width" />
9493 <input type="hidden" name="height" id="height" />
9494 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
9495 <input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
9496 <?php wp_nonce_field('custom-header') ?>
9497 <input type="submit" value="<?php _e('Crop Header'); ?>" />
9498 </p>
9499
9500 </form>
9501 </div>
9502 <?php
9503 }
9504
9505 /**
9506 * Display third step of custom header image page.
9507 *
9508 * @since unknown
9509 */
9510 function step_3() {
9511 check_admin_referer('custom-header');
9512 if ( $_POST['oitar'] > 1 ) {
9513 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
9514 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
9515 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
9516 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
9517 }
9518
9519 $original = get_attached_file( $_POST['attachment_id'] );
9520
9521 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
9522 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
9523
9524 $parent = get_post($_POST['attachment_id']);
9525 $parent_url = $parent->guid;
9526 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
9527
9528 // Construct the object array
9529 $object = array(
9530 'ID' => $_POST['attachment_id'],
9531 'post_title' => basename($cropped),
9532 'post_content' => $url,
9533 'post_mime_type' => 'image/jpeg',
9534 'guid' => $url
9535 );
9536
9537 // Update the attachment
9538 wp_insert_attachment($object, $cropped);
9539 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
9540
9541 set_theme_mod('header_image', $url);
9542
9543 // cleanup
9544 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
9545 @unlink( apply_filters( 'wp_delete_file', $medium ) );
9546 @unlink( apply_filters( 'wp_delete_file', $original ) );
9547
9548 return $this->finished();
9549 }
9550
9551 /**
9552 * Display last step of custom header image page.
9553 *
9554 * @since unknown
9555 */
9556 function finished() {
9557 ?>
9558 <div class="wrap">
9559 <h2><?php _e('Header complete!'); ?></h2>
9560
9561 <p><?php _e('Visit your site and you should see the new header now.'); ?></p>
9562
9563 </div>
9564 <?php
9565 }
9566
9567 /**
9568 * Display the page based on the current step.
9569 *
9570 * @since unknown
9571 */
9572 function admin_page() {
9573 $step = $this->step();
9574 if ( 1 == $step )
9575 $this->step_1();
9576 elseif ( 2 == $step )
9577 $this->step_2();
9578 elseif ( 3 == $step )
9579 $this->step_3();
9580 }
9581
9582 }
9583 ?>