Projects : mp-wp : mp-wp_genesis

mp-wp/wp-admin/custom-header.php

Dir - Raw

1<?php
2/**
3 * The custom header image script.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/**
10 * The custom header image class.
11 *
12 * @since unknown
13 * @package WordPress
14 * @subpackage Administration
15 */
16class Custom_Image_Header {
17
18 /**
19 * Callback for administration header.
20 *
21 * @var callback
22 * @since unknown
23 * @access private
24 */
25 var $admin_header_callback;
26
27 /**
28 * PHP4 Constructor - Register administration header callback.
29 *
30 * @since unknown
31 * @param callback $admin_header_callback
32 * @return Custom_Image_Header
33 */
34 function Custom_Image_Header($admin_header_callback) {
35 $this->admin_header_callback = $admin_header_callback;
36 }
37
38 /**
39 * Setup the hooks for the Custom Header admin page.
40 *
41 * @since unknown
42 */
43 function init() {
44 $page = add_theme_page(__('Custom Image Header'), __('Custom Image Header'), 'edit_themes', 'custom-header', array(&$this, 'admin_page'));
45
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);
51 }
52
53 /**
54 * Get the current step.
55 *
56 * @since unknown
57 *
58 * @return int Current step
59 */
60 function step() {
61 if ( ! isset( $_GET['step'] ) )
62 return 1;
63
64 $step = (int) $_GET['step'];
65 if ( $step < 1 || 3 < $step )
66 $step = 1;
67
68 return $step;
69 }
70
71 /**
72 * Setup the enqueue for the JavaScript files.
73 *
74 * @since unknown
75 */
76 function js_includes() {
77 $step = $this->step();
78
79 if ( 1 == $step )
80 wp_enqueue_script('farbtastic');
81 elseif ( 2 == $step )
82 wp_enqueue_script('cropper');
83 }
84
85 /**
86 * Setup the enqueue for the CSS files
87 *
88 * @since 2.7
89 */
90 function css_includes() {
91 $step = $this->step();
92
93 if ( 1 == $step ) {
94 wp_enqueue_style('farbtastic');
95 }
96 }
97
98 /**
99 * Execute custom header modification.
100 *
101 * @since unknown
102 */
103 function take_action() {
104 if ( isset( $_POST['textcolor'] ) ) {
105 check_admin_referer('custom-header');
106 if ( 'blank' == $_POST['textcolor'] ) {
107 set_theme_mod('header_textcolor', 'blank');
108 } else {
109 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['textcolor']);
110 if ( strlen($color) == 6 || strlen($color) == 3 )
111 set_theme_mod('header_textcolor', $color);
112 }
113 }
114 if ( isset($_POST['resetheader']) ) {
115 check_admin_referer('custom-header');
116 remove_theme_mods();
117 }
118 }
119
120 /**
121 * Execute Javascript depending on step.
122 *
123 * @since unknown
124 */
125 function js() {
126 $step = $this->step();
127 if ( 1 == $step )
128 $this->js_1();
129 elseif ( 2 == $step )
130 $this->js_2();
131 }
132
133 /**
134 * Display Javascript based on Step 1.
135 *
136 * @since unknown
137 */
138 function js_1() { ?>
139<script type="text/javascript">
140 var buttons = ['#name', '#desc', '#pickcolor', '#defaultcolor'];
141 var farbtastic;
142
143 function pickColor(color) {
144 jQuery('#name').css('color', color);
145 jQuery('#desc').css('color', color);
146 jQuery('#textcolor').val(color);
147 farbtastic.setColor(color);
148 }
149
150 jQuery(document).ready(function() {
151 jQuery('#pickcolor').click(function() {
152 jQuery('#colorPickerDiv').show();
153 });
154
155 jQuery('#hidetext').click(function() {
156 toggle_text();
157 });
158
159 farbtastic = jQuery.farbtastic('#colorPickerDiv', function(color) { pickColor(color); });
160 pickColor('#<?php echo get_theme_mod('header_textcolor', HEADER_TEXTCOLOR); ?>');
161
162 <?php if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
163 toggle_text();
164 <?php } ?>
165 });
166
167 jQuery(document).mousedown(function(){
168 // Make the picker disappear, since we're using it in an independant div
169 hide_picker();
170 });
171
172 function colorDefault() {
173 pickColor('#<?php echo HEADER_TEXTCOLOR; ?>');
174 }
175
176 function hide_picker(what) {
177 var update = false;
178 jQuery('#colorPickerDiv').each(function(){
179 var id = jQuery(this).attr('id');
180 if (id == what) {
181 return;
182 }
183 var display = jQuery(this).css('display');
184 if (display == 'block') {
185 jQuery(this).fadeOut(2);
186 }
187 });
188 }
189
190 function toggle_text(force) {
191 if(jQuery('#textcolor').val() == 'blank') {
192 //Show text
193 jQuery( buttons.toString() ).show();
194 jQuery('#textcolor').val('<?php echo HEADER_TEXTCOLOR; ?>');
195 jQuery('#hidetext').val('<?php _e('Hide Text'); ?>');
196 }
197 else {
198 //Hide text
199 jQuery( buttons.toString() ).hide();
200 jQuery('#textcolor').val('blank');
201 jQuery('#hidetext').val('<?php _e('Show Text'); ?>');
202 }
203 }
204
205
206
207</script>
208<?php
209 }
210
211 /**
212 * Display Javascript based on Step 2.
213 *
214 * @since unknown
215 */
216 function js_2() { ?>
217<script type="text/javascript">
218 function onEndCrop( coords, dimensions ) {
219 jQuery( '#x1' ).val(coords.x1);
220 jQuery( '#y1' ).val(coords.y1);
221 jQuery( '#x2' ).val(coords.x2);
222 jQuery( '#y2' ).val(coords.y2);
223 jQuery( '#width' ).val(dimensions.width);
224 jQuery( '#height' ).val(dimensions.height);
225 }
226
227 // with a supplied ratio
228 jQuery(document).ready(function() {
229 var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
230 var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
231 var ratio = xinit / yinit;
232 var ximg = jQuery('#upload').width();
233 var yimg = jQuery('#upload').height();
234 if ( yimg < yinit || ximg < xinit ) {
235 if ( ximg / yimg > ratio ) {
236 yinit = yimg;
237 xinit = yinit * ratio;
238 } else {
239 xinit = ximg;
240 yinit = xinit / ratio;
241 }
242 }
243 new Cropper.Img(
244 'upload',
245 {
246 ratioDim: { x: xinit, y: yinit },
247 displayOnInit: true,
248 onEndCrop: onEndCrop
249 }
250 )
251 });
252</script>
253<?php
254 }
255
256 /**
257 * Display first step of custom header image page.
258 *
259 * @since unknown
260 */
261 function step_1() {
262 if ( $_GET['updated'] ) { ?>
263<div id="message" class="updated fade">
264<p><?php _e('Header updated.') ?></p>
265</div>
266 <?php } ?>
267
268<div class="wrap">
269<?php screen_icon(); ?>
270<h2><?php _e('Your Header Image'); ?></h2>
271<p><?php _e('This is your header image. You can change the text color or upload and crop a new image.'); ?></p>
272
273<div id="headimg" style="background-image: url(<?php clean_url(header_image()) ?>);">
274<h1><a onclick="return false;" href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>" id="name"><?php bloginfo('name'); ?></a></h1>
275<div id="desc"><?php bloginfo('description');?></div>
276</div>
277<?php if ( !defined( 'NO_HEADER_TEXT' ) ) { ?>
278<form method="post" action="<?php echo admin_url('themes.php?page=custom-header&amp;updated=true') ?>">
279<input type="button" value="<?php _e('Hide Text'); ?>" onclick="hide_text()" id="hidetext" />
280<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" />
281<?php wp_nonce_field('custom-header') ?>
282<input type="hidden" name="textcolor" id="textcolor" value="#<?php attribute_escape(header_textcolor()) ?>" /><input name="submit" type="submit" value="<?php _e('Save Changes'); ?>" /></form>
283<?php } ?>
284
285<div id="colorPickerDiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"> </div>
286</div>
287<div class="wrap">
288<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>
289<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>
290
291<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo attribute_escape(add_query_arg('step', 2)) ?>" style="margin: auto; width: 50%;">
292<label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
293<input type="hidden" name="action" value="save" />
294<?php wp_nonce_field('custom-header') ?>
295<p class="submit">
296<input type="submit" value="<?php _e('Upload'); ?>" />
297</p>
298</form>
299
300</div>
301
302 <?php if ( get_theme_mod('header_image') || get_theme_mod('header_textcolor') ) : ?>
303<div class="wrap">
304<h2><?php _e('Reset Header Image and Color'); ?></h2>
305<p><?php _e('This will restore the original header image and color. You will not be able to retrieve any customizations.') ?></p>
306<form method="post" action="<?php echo attribute_escape(add_query_arg('step', 1)) ?>">
307<?php wp_nonce_field('custom-header'); ?>
308<input type="submit" name="resetheader" value="<?php _e('Restore Original Header'); ?>" />
309</form>
310</div>
311 <?php endif;
312
313 }
314
315 /**
316 * Display second step of custom header image page.
317 *
318 * @since unknown
319 */
320 function step_2() {
321 check_admin_referer('custom-header');
322 $overrides = array('test_form' => false);
323 $file = wp_handle_upload($_FILES['import'], $overrides);
324
325 if ( isset($file['error']) )
326 die( $file['error'] );
327
328 $url = $file['url'];
329 $type = $file['type'];
330 $file = $file['file'];
331 $filename = basename($file);
332
333 // Construct the object array
334 $object = array(
335 'post_title' => $filename,
336 'post_content' => $url,
337 'post_mime_type' => $type,
338 'guid' => $url);
339
340 // Save the data
341 $id = wp_insert_attachment($object, $file);
342
343 list($width, $height, $type, $attr) = getimagesize( $file );
344
345 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
346 // Add the meta-data
347 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
348
349 set_theme_mod('header_image', clean_url($url));
350 do_action('wp_create_file_in_uploads', $file, $id); // For replication
351 return $this->finished();
352 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
353 $oitar = $width / HEADER_IMAGE_WIDTH;
354 $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
355 $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
356
357 $url = str_replace(basename($url), basename($image), $url);
358 $width = $width / $oitar;
359 $height = $height / $oitar;
360 } else {
361 $oitar = 1;
362 }
363 ?>
364
365<div class="wrap">
366
367<form method="POST" action="<?php echo attribute_escape(add_query_arg('step', 3)) ?>">
368
369<p><?php _e('Choose the part of the image you want to use as your header.'); ?></p>
370<div id="testWrap" style="position: relative">
371<img src="<?php echo $url; ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
372</div>
373
374<p class="submit">
375<input type="hidden" name="x1" id="x1" />
376<input type="hidden" name="y1" id="y1" />
377<input type="hidden" name="x2" id="x2" />
378<input type="hidden" name="y2" id="y2" />
379<input type="hidden" name="width" id="width" />
380<input type="hidden" name="height" id="height" />
381<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo $id; ?>" />
382<input type="hidden" name="oitar" id="oitar" value="<?php echo $oitar; ?>" />
383<?php wp_nonce_field('custom-header') ?>
384<input type="submit" value="<?php _e('Crop Header'); ?>" />
385</p>
386
387</form>
388</div>
389 <?php
390 }
391
392 /**
393 * Display third step of custom header image page.
394 *
395 * @since unknown
396 */
397 function step_3() {
398 check_admin_referer('custom-header');
399 if ( $_POST['oitar'] > 1 ) {
400 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
401 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
402 $_POST['width'] = $_POST['width'] * $_POST['oitar'];
403 $_POST['height'] = $_POST['height'] * $_POST['oitar'];
404 }
405
406 $original = get_attached_file( $_POST['attachment_id'] );
407
408 $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
409 $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']); // For replication
410
411 $parent = get_post($_POST['attachment_id']);
412 $parent_url = $parent->guid;
413 $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
414
415 // Construct the object array
416 $object = array(
417 'ID' => $_POST['attachment_id'],
418 'post_title' => basename($cropped),
419 'post_content' => $url,
420 'post_mime_type' => 'image/jpeg',
421 'guid' => $url
422 );
423
424 // Update the attachment
425 wp_insert_attachment($object, $cropped);
426 wp_update_attachment_metadata( $_POST['attachment_id'], wp_generate_attachment_metadata( $_POST['attachment_id'], $cropped ) );
427
428 set_theme_mod('header_image', $url);
429
430 // cleanup
431 $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
432 @unlink( apply_filters( 'wp_delete_file', $medium ) );
433 @unlink( apply_filters( 'wp_delete_file', $original ) );
434
435 return $this->finished();
436 }
437
438 /**
439 * Display last step of custom header image page.
440 *
441 * @since unknown
442 */
443 function finished() {
444 ?>
445<div class="wrap">
446<h2><?php _e('Header complete!'); ?></h2>
447
448<p><?php _e('Visit your site and you should see the new header now.'); ?></p>
449
450</div>
451 <?php
452 }
453
454 /**
455 * Display the page based on the current step.
456 *
457 * @since unknown
458 */
459 function admin_page() {
460 $step = $this->step();
461 if ( 1 == $step )
462 $this->step_1();
463 elseif ( 2 == $step )
464 $this->step_2();
465 elseif ( 3 == $step )
466 $this->step_3();
467 }
468
469}
470?>