1 <?php |
1 <?php |
2 /** |
2 /** |
3 * The custom header image script. |
3 * Custom header image script. |
4 * |
4 * |
|
5 * This file is deprecated, use 'wp-admin/includes/class-custom-image-header.php' instead. |
|
6 * |
|
7 * @deprecated 5.3.0 |
5 * @package WordPress |
8 * @package WordPress |
6 * @subpackage Administration |
9 * @subpackage Administration |
7 */ |
10 */ |
8 |
11 |
9 /** |
12 _deprecated_file( basename( __FILE__ ), '5.3.0', 'wp-admin/includes/class-custom-image-header.php' ); |
10 * The custom header image class. |
|
11 * |
|
12 * @since 2.1.0 |
|
13 */ |
|
14 class Custom_Image_Header { |
|
15 |
13 |
16 /** |
14 /** Custom_Image_Header class */ |
17 * Callback for administration header. |
15 require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php'; |
18 * |
|
19 * @var callable |
|
20 * @since 2.1.0 |
|
21 */ |
|
22 public $admin_header_callback; |
|
23 |
|
24 /** |
|
25 * Callback for header div. |
|
26 * |
|
27 * @var callable |
|
28 * @since 3.0.0 |
|
29 */ |
|
30 public $admin_image_div_callback; |
|
31 |
|
32 /** |
|
33 * Holds default headers. |
|
34 * |
|
35 * @var array |
|
36 * @since 3.0.0 |
|
37 */ |
|
38 public $default_headers = array(); |
|
39 |
|
40 /** |
|
41 * Used to trigger a success message when settings updated and set to true. |
|
42 * |
|
43 * @since 3.0.0 |
|
44 * @var bool |
|
45 */ |
|
46 private $updated; |
|
47 |
|
48 /** |
|
49 * Constructor - Register administration header callback. |
|
50 * |
|
51 * @since 2.1.0 |
|
52 * @param callable $admin_header_callback |
|
53 * @param callable $admin_image_div_callback Optional custom image div output callback. |
|
54 */ |
|
55 public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) { |
|
56 $this->admin_header_callback = $admin_header_callback; |
|
57 $this->admin_image_div_callback = $admin_image_div_callback; |
|
58 |
|
59 add_action( 'admin_menu', array( $this, 'init' ) ); |
|
60 |
|
61 add_action( 'customize_save_after', array( $this, 'customize_set_last_used' ) ); |
|
62 add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) ); |
|
63 add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) ); |
|
64 add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) ); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Set up the hooks for the Custom Header admin page. |
|
69 * |
|
70 * @since 2.1.0 |
|
71 */ |
|
72 public function init() { |
|
73 $page = add_theme_page( __( 'Header' ), __( 'Header' ), 'edit_theme_options', 'custom-header', array( $this, 'admin_page' ) ); |
|
74 if ( ! $page ) { |
|
75 return; |
|
76 } |
|
77 |
|
78 add_action( "admin_print_scripts-$page", array( $this, 'js_includes' ) ); |
|
79 add_action( "admin_print_styles-$page", array( $this, 'css_includes' ) ); |
|
80 add_action( "admin_head-$page", array( $this, 'help' ) ); |
|
81 add_action( "admin_head-$page", array( $this, 'take_action' ), 50 ); |
|
82 add_action( "admin_head-$page", array( $this, 'js' ), 50 ); |
|
83 if ( $this->admin_header_callback ) { |
|
84 add_action( "admin_head-$page", $this->admin_header_callback, 51 ); |
|
85 } |
|
86 } |
|
87 |
|
88 /** |
|
89 * Adds contextual help. |
|
90 * |
|
91 * @since 3.0.0 |
|
92 */ |
|
93 public function help() { |
|
94 get_current_screen()->add_help_tab( |
|
95 array( |
|
96 'id' => 'overview', |
|
97 'title' => __( 'Overview' ), |
|
98 'content' => |
|
99 '<p>' . __( 'This screen is used to customize the header section of your theme.' ) . '</p>' . |
|
100 '<p>' . __( 'You can choose from the theme’s default header images, or use one of your own. You can also customize how your Site Title and Tagline are displayed.' ) . '<p>', |
|
101 ) |
|
102 ); |
|
103 |
|
104 get_current_screen()->add_help_tab( |
|
105 array( |
|
106 'id' => 'set-header-image', |
|
107 'title' => __( 'Header Image' ), |
|
108 'content' => |
|
109 '<p>' . __( 'You can set a custom image header for your site. Simply upload the image and crop it, and the new header will go live immediately. Alternatively, you can use an image that has already been uploaded to your Media Library by clicking the “Choose Image” button.' ) . '</p>' . |
|
110 '<p>' . __( 'Some themes come with additional header images bundled. If you see multiple images displayed, select the one you’d like and click the “Save Changes” button.' ) . '</p>' . |
|
111 '<p>' . __( 'If your theme has more than one default header image, or you have uploaded more than one custom header image, you have the option of having WordPress display a randomly different image on each page of your site. Click the “Random” radio button next to the Uploaded Images or Default Images section to enable this feature.' ) . '</p>' . |
|
112 '<p>' . __( 'If you don’t want a header image to be displayed on your site at all, click the “Remove Header Image” button at the bottom of the Header Image section of this page. If you want to re-enable the header image later, you just have to select one of the other image options and click “Save Changes”.' ) . '</p>', |
|
113 ) |
|
114 ); |
|
115 |
|
116 get_current_screen()->add_help_tab( |
|
117 array( |
|
118 'id' => 'set-header-text', |
|
119 'title' => __( 'Header Text' ), |
|
120 'content' => |
|
121 '<p>' . sprintf( __( 'For most themes, the header text is your Site Title and Tagline, as defined in the <a href="%1$s">General Settings</a> section.' ), admin_url( 'options-general.php' ) ) . '<p>' . |
|
122 '<p>' . __( 'In the Header Text section of this page, you can choose whether to display this text or hide it. You can also choose a color for the text by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' . |
|
123 '<p>' . __( 'Don’t forget to click “Save Changes” when you’re done!' ) . '</p>', |
|
124 ) |
|
125 ); |
|
126 |
|
127 get_current_screen()->set_help_sidebar( |
|
128 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . |
|
129 '<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Header_Screen">Documentation on Custom Header</a>' ) . '</p>' . |
|
130 '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>' |
|
131 ); |
|
132 } |
|
133 |
|
134 /** |
|
135 * Get the current step. |
|
136 * |
|
137 * @since 2.6.0 |
|
138 * |
|
139 * @return int Current step |
|
140 */ |
|
141 public function step() { |
|
142 if ( ! isset( $_GET['step'] ) ) { |
|
143 return 1; |
|
144 } |
|
145 |
|
146 $step = (int) $_GET['step']; |
|
147 if ( $step < 1 || 3 < $step || |
|
148 ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) || |
|
149 ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) ) |
|
150 ) { |
|
151 return 1; |
|
152 } |
|
153 |
|
154 return $step; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Set up the enqueue for the JavaScript files. |
|
159 * |
|
160 * @since 2.1.0 |
|
161 */ |
|
162 public function js_includes() { |
|
163 $step = $this->step(); |
|
164 |
|
165 if ( ( 1 == $step || 3 == $step ) ) { |
|
166 wp_enqueue_media(); |
|
167 wp_enqueue_script( 'custom-header' ); |
|
168 if ( current_theme_supports( 'custom-header', 'header-text' ) ) { |
|
169 wp_enqueue_script( 'wp-color-picker' ); |
|
170 } |
|
171 } elseif ( 2 == $step ) { |
|
172 wp_enqueue_script( 'imgareaselect' ); |
|
173 } |
|
174 } |
|
175 |
|
176 /** |
|
177 * Set up the enqueue for the CSS files |
|
178 * |
|
179 * @since 2.7.0 |
|
180 */ |
|
181 public function css_includes() { |
|
182 $step = $this->step(); |
|
183 |
|
184 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { |
|
185 wp_enqueue_style( 'wp-color-picker' ); |
|
186 } elseif ( 2 == $step ) { |
|
187 wp_enqueue_style( 'imgareaselect' ); |
|
188 } |
|
189 } |
|
190 |
|
191 /** |
|
192 * Execute custom header modification. |
|
193 * |
|
194 * @since 2.6.0 |
|
195 */ |
|
196 public function take_action() { |
|
197 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
198 return; |
|
199 } |
|
200 |
|
201 if ( empty( $_POST ) ) { |
|
202 return; |
|
203 } |
|
204 |
|
205 $this->updated = true; |
|
206 |
|
207 if ( isset( $_POST['resetheader'] ) ) { |
|
208 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
209 $this->reset_header_image(); |
|
210 return; |
|
211 } |
|
212 |
|
213 if ( isset( $_POST['removeheader'] ) ) { |
|
214 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
215 $this->remove_header_image(); |
|
216 return; |
|
217 } |
|
218 |
|
219 if ( isset( $_POST['text-color'] ) && ! isset( $_POST['display-header-text'] ) ) { |
|
220 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
221 set_theme_mod( 'header_textcolor', 'blank' ); |
|
222 } elseif ( isset( $_POST['text-color'] ) ) { |
|
223 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
224 $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] ); |
|
225 $color = preg_replace( '/[^0-9a-fA-F]/', '', $_POST['text-color'] ); |
|
226 if ( strlen( $color ) == 6 || strlen( $color ) == 3 ) { |
|
227 set_theme_mod( 'header_textcolor', $color ); |
|
228 } elseif ( ! $color ) { |
|
229 set_theme_mod( 'header_textcolor', 'blank' ); |
|
230 } |
|
231 } |
|
232 |
|
233 if ( isset( $_POST['default-header'] ) ) { |
|
234 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
235 $this->set_header_image( $_POST['default-header'] ); |
|
236 return; |
|
237 } |
|
238 } |
|
239 |
|
240 /** |
|
241 * Process the default headers |
|
242 * |
|
243 * @since 3.0.0 |
|
244 * |
|
245 * @global array $_wp_default_headers |
|
246 */ |
|
247 public function process_default_headers() { |
|
248 global $_wp_default_headers; |
|
249 |
|
250 if ( ! isset( $_wp_default_headers ) ) { |
|
251 return; |
|
252 } |
|
253 |
|
254 if ( ! empty( $this->default_headers ) ) { |
|
255 return; |
|
256 } |
|
257 |
|
258 $this->default_headers = $_wp_default_headers; |
|
259 $template_directory_uri = get_template_directory_uri(); |
|
260 $stylesheet_directory_uri = get_stylesheet_directory_uri(); |
|
261 foreach ( array_keys( $this->default_headers ) as $header ) { |
|
262 $this->default_headers[ $header ]['url'] = sprintf( $this->default_headers[ $header ]['url'], $template_directory_uri, $stylesheet_directory_uri ); |
|
263 $this->default_headers[ $header ]['thumbnail_url'] = sprintf( $this->default_headers[ $header ]['thumbnail_url'], $template_directory_uri, $stylesheet_directory_uri ); |
|
264 } |
|
265 } |
|
266 |
|
267 /** |
|
268 * Display UI for selecting one of several default headers. |
|
269 * |
|
270 * Show the random image option if this theme has multiple header images. |
|
271 * Random image option is on by default if no header has been set. |
|
272 * |
|
273 * @since 3.0.0 |
|
274 * |
|
275 * @param string $type The header type. One of 'default' (for the Uploaded Images control) |
|
276 * or 'uploaded' (for the Uploaded Images control). |
|
277 */ |
|
278 public function show_header_selector( $type = 'default' ) { |
|
279 if ( 'default' == $type ) { |
|
280 $headers = $this->default_headers; |
|
281 } else { |
|
282 $headers = get_uploaded_header_images(); |
|
283 $type = 'uploaded'; |
|
284 } |
|
285 |
|
286 if ( 1 < count( $headers ) ) { |
|
287 echo '<div class="random-header">'; |
|
288 echo '<label><input name="default-header" type="radio" value="random-' . $type . '-image"' . checked( is_random_header_image( $type ), true, false ) . ' />'; |
|
289 _e( '<strong>Random:</strong> Show a different image on each page.' ); |
|
290 echo '</label>'; |
|
291 echo '</div>'; |
|
292 } |
|
293 |
|
294 echo '<div class="available-headers">'; |
|
295 foreach ( $headers as $header_key => $header ) { |
|
296 $header_thumbnail = $header['thumbnail_url']; |
|
297 $header_url = $header['url']; |
|
298 $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text']; |
|
299 echo '<div class="default-header">'; |
|
300 echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />'; |
|
301 $width = ''; |
|
302 if ( ! empty( $header['attachment_id'] ) ) { |
|
303 $width = ' width="230"'; |
|
304 } |
|
305 echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>'; |
|
306 echo '</div>'; |
|
307 } |
|
308 echo '<div class="clear"></div></div>'; |
|
309 } |
|
310 |
|
311 /** |
|
312 * Execute JavaScript depending on step. |
|
313 * |
|
314 * @since 2.1.0 |
|
315 */ |
|
316 public function js() { |
|
317 $step = $this->step(); |
|
318 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { |
|
319 $this->js_1(); |
|
320 } elseif ( 2 == $step ) { |
|
321 $this->js_2(); |
|
322 } |
|
323 } |
|
324 |
|
325 /** |
|
326 * Display JavaScript based on Step 1 and 3. |
|
327 * |
|
328 * @since 2.6.0 |
|
329 */ |
|
330 public function js_1() { |
|
331 $default_color = ''; |
|
332 if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) { |
|
333 $default_color = get_theme_support( 'custom-header', 'default-text-color' ); |
|
334 if ( $default_color && false === strpos( $default_color, '#' ) ) { |
|
335 $default_color = '#' . $default_color; |
|
336 } |
|
337 } |
|
338 ?> |
|
339 <script type="text/javascript"> |
|
340 (function($){ |
|
341 var default_color = '<?php echo $default_color; ?>', |
|
342 header_text_fields; |
|
343 |
|
344 function pickColor(color) { |
|
345 $('#name').css('color', color); |
|
346 $('#desc').css('color', color); |
|
347 $('#text-color').val(color); |
|
348 } |
|
349 |
|
350 function toggle_text() { |
|
351 var checked = $('#display-header-text').prop('checked'), |
|
352 text_color; |
|
353 header_text_fields.toggle( checked ); |
|
354 if ( ! checked ) |
|
355 return; |
|
356 text_color = $('#text-color'); |
|
357 if ( '' == text_color.val().replace('#', '') ) { |
|
358 text_color.val( default_color ); |
|
359 pickColor( default_color ); |
|
360 } else { |
|
361 pickColor( text_color.val() ); |
|
362 } |
|
363 } |
|
364 |
|
365 $(document).ready(function() { |
|
366 var text_color = $('#text-color'); |
|
367 header_text_fields = $('.displaying-header-text'); |
|
368 text_color.wpColorPicker({ |
|
369 change: function( event, ui ) { |
|
370 pickColor( text_color.wpColorPicker('color') ); |
|
371 }, |
|
372 clear: function() { |
|
373 pickColor( '' ); |
|
374 } |
|
375 }); |
|
376 $('#display-header-text').click( toggle_text ); |
|
377 <?php if ( ! display_header_text() ) : ?> |
|
378 toggle_text(); |
|
379 <?php endif; ?> |
|
380 }); |
|
381 })(jQuery); |
|
382 </script> |
|
383 <?php |
|
384 } |
|
385 |
|
386 /** |
|
387 * Display JavaScript based on Step 2. |
|
388 * |
|
389 * @since 2.6.0 |
|
390 */ |
|
391 public function js_2() { |
|
392 |
|
393 ?> |
|
394 <script type="text/javascript"> |
|
395 function onEndCrop( coords ) { |
|
396 jQuery( '#x1' ).val(coords.x); |
|
397 jQuery( '#y1' ).val(coords.y); |
|
398 jQuery( '#width' ).val(coords.w); |
|
399 jQuery( '#height' ).val(coords.h); |
|
400 } |
|
401 |
|
402 jQuery(document).ready(function() { |
|
403 var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>; |
|
404 var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>; |
|
405 var ratio = xinit / yinit; |
|
406 var ximg = jQuery('img#upload').width(); |
|
407 var yimg = jQuery('img#upload').height(); |
|
408 |
|
409 if ( yimg < yinit || ximg < xinit ) { |
|
410 if ( ximg / yimg > ratio ) { |
|
411 yinit = yimg; |
|
412 xinit = yinit * ratio; |
|
413 } else { |
|
414 xinit = ximg; |
|
415 yinit = xinit / ratio; |
|
416 } |
|
417 } |
|
418 |
|
419 jQuery('img#upload').imgAreaSelect({ |
|
420 handles: true, |
|
421 keys: true, |
|
422 show: true, |
|
423 x1: 0, |
|
424 y1: 0, |
|
425 x2: xinit, |
|
426 y2: yinit, |
|
427 <?php |
|
428 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
429 ?> |
|
430 aspectRatio: xinit + ':' + yinit, |
|
431 <?php |
|
432 } |
|
433 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) { |
|
434 ?> |
|
435 maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>, |
|
436 <?php |
|
437 } |
|
438 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
439 ?> |
|
440 maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>, |
|
441 <?php |
|
442 } |
|
443 ?> |
|
444 onInit: function () { |
|
445 jQuery('#width').val(xinit); |
|
446 jQuery('#height').val(yinit); |
|
447 }, |
|
448 onSelectChange: function(img, c) { |
|
449 jQuery('#x1').val(c.x1); |
|
450 jQuery('#y1').val(c.y1); |
|
451 jQuery('#width').val(c.width); |
|
452 jQuery('#height').val(c.height); |
|
453 } |
|
454 }); |
|
455 }); |
|
456 </script> |
|
457 <?php |
|
458 } |
|
459 |
|
460 /** |
|
461 * Display first step of custom header image page. |
|
462 * |
|
463 * @since 2.1.0 |
|
464 */ |
|
465 public function step_1() { |
|
466 $this->process_default_headers(); |
|
467 ?> |
|
468 |
|
469 <div class="wrap"> |
|
470 <h1><?php _e( 'Custom Header' ); ?></h1> |
|
471 |
|
472 <?php if ( current_user_can( 'customize' ) ) { ?> |
|
473 <div class="notice notice-info hide-if-no-customize"> |
|
474 <p> |
|
475 <?php |
|
476 printf( |
|
477 __( 'You can now manage and live-preview Custom Header in the <a href="%1$s">Customizer</a>.' ), |
|
478 admin_url( 'customize.php?autofocus[control]=header_image' ) |
|
479 ); |
|
480 ?> |
|
481 </p> |
|
482 </div> |
|
483 <?php } ?> |
|
484 |
|
485 <?php if ( ! empty( $this->updated ) ) { ?> |
|
486 <div id="message" class="updated"> |
|
487 <p><?php printf( __( 'Header updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p> |
|
488 </div> |
|
489 <?php } ?> |
|
490 |
|
491 <h2><?php _e( 'Header Image' ); ?></h2> |
|
492 |
|
493 <table class="form-table" role="presentation"> |
|
494 <tbody> |
|
495 |
|
496 <?php if ( get_custom_header() || display_header_text() ) : ?> |
|
497 <tr> |
|
498 <th scope="row"><?php _e( 'Preview' ); ?></th> |
|
499 <td> |
|
500 <?php |
|
501 if ( $this->admin_image_div_callback ) { |
|
502 call_user_func( $this->admin_image_div_callback ); |
|
503 } else { |
|
504 $custom_header = get_custom_header(); |
|
505 $header_image = get_header_image(); |
|
506 |
|
507 if ( $header_image ) { |
|
508 $header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');'; |
|
509 } else { |
|
510 $header_image_style = ''; |
|
511 } |
|
512 |
|
513 if ( $custom_header->width ) { |
|
514 $header_image_style .= 'max-width:' . $custom_header->width . 'px;'; |
|
515 } |
|
516 if ( $custom_header->height ) { |
|
517 $header_image_style .= 'height:' . $custom_header->height . 'px;'; |
|
518 } |
|
519 ?> |
|
520 <div id="headimg" style="<?php echo $header_image_style; ?>"> |
|
521 <?php |
|
522 if ( display_header_text() ) { |
|
523 $style = ' style="color:#' . get_header_textcolor() . ';"'; |
|
524 } else { |
|
525 $style = ' style="display:none;"'; |
|
526 } |
|
527 ?> |
|
528 <h1><a id="name" class="displaying-header-text" <?php echo $style; ?> onclick="return false;" href="<?php bloginfo( 'url' ); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1> |
|
529 <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div> |
|
530 </div> |
|
531 <?php } ?> |
|
532 </td> |
|
533 </tr> |
|
534 <?php endif; ?> |
|
535 |
|
536 <?php if ( current_user_can( 'upload_files' ) && current_theme_supports( 'custom-header', 'uploads' ) ) : ?> |
|
537 <tr> |
|
538 <th scope="row"><?php _e( 'Select Image' ); ?></th> |
|
539 <td> |
|
540 <p><?php _e( 'You can select an image to be shown at the top of your site by uploading from your computer or choosing from your media library. After selecting an image you will be able to crop it.' ); ?><br /> |
|
541 <?php |
|
542 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
543 printf( __( 'Images of exactly <strong>%1$d × %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) ); |
|
544 } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) { |
|
545 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
546 printf( |
|
547 /* translators: %s: size in pixels */ |
|
548 __( 'Images should be at least %s wide.' ) . ' ', |
|
549 sprintf( |
|
550 /* translators: %d: custom header width */ |
|
551 '<strong>' . __( '%d pixels' ) . '</strong>', |
|
552 get_theme_support( 'custom-header', 'width' ) |
|
553 ) |
|
554 ); |
|
555 } |
|
556 } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
557 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) { |
|
558 printf( |
|
559 /* translators: %s: size in pixels */ |
|
560 __( 'Images should be at least %s tall.' ) . ' ', |
|
561 sprintf( |
|
562 /* translators: %d: custom header height */ |
|
563 '<strong>' . __( '%d pixels' ) . '</strong>', |
|
564 get_theme_support( 'custom-header', 'height' ) |
|
565 ) |
|
566 ); |
|
567 } |
|
568 } |
|
569 if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
570 if ( current_theme_supports( 'custom-header', 'width' ) ) { |
|
571 printf( |
|
572 /* translators: %s: size in pixels */ |
|
573 __( 'Suggested width is %s.' ) . ' ', |
|
574 sprintf( |
|
575 /* translators: %d: custom header width */ |
|
576 '<strong>' . __( '%d pixels' ) . '</strong>', |
|
577 get_theme_support( 'custom-header', 'width' ) |
|
578 ) |
|
579 ); |
|
580 } |
|
581 if ( current_theme_supports( 'custom-header', 'height' ) ) { |
|
582 printf( |
|
583 /* translators: %s: size in pixels */ |
|
584 __( 'Suggested height is %s.' ) . ' ', |
|
585 sprintf( |
|
586 /* translators: %d: custom header height */ |
|
587 '<strong>' . __( '%d pixels' ) . '</strong>', |
|
588 get_theme_support( 'custom-header', 'height' ) |
|
589 ) |
|
590 ); |
|
591 } |
|
592 } |
|
593 ?> |
|
594 </p> |
|
595 <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ); ?>"> |
|
596 <p> |
|
597 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> |
|
598 <input type="file" id="upload" name="import" /> |
|
599 <input type="hidden" name="action" value="save" /> |
|
600 <?php wp_nonce_field( 'custom-header-upload', '_wpnonce-custom-header-upload' ); ?> |
|
601 <?php submit_button( __( 'Upload' ), '', 'submit', false ); ?> |
|
602 </p> |
|
603 <?php |
|
604 $modal_update_href = esc_url( |
|
605 add_query_arg( |
|
606 array( |
|
607 'page' => 'custom-header', |
|
608 'step' => 2, |
|
609 '_wpnonce-custom-header-upload' => wp_create_nonce( 'custom-header-upload' ), |
|
610 ), |
|
611 admin_url( 'themes.php' ) |
|
612 ) |
|
613 ); |
|
614 ?> |
|
615 <p> |
|
616 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br /> |
|
617 <button id="choose-from-library-link" class="button" |
|
618 data-update-link="<?php echo esc_attr( $modal_update_href ); ?>" |
|
619 data-choose="<?php esc_attr_e( 'Choose a Custom Header' ); ?>" |
|
620 data-update="<?php esc_attr_e( 'Set as header' ); ?>"><?php _e( 'Choose Image' ); ?></button> |
|
621 </p> |
|
622 </form> |
|
623 </td> |
|
624 </tr> |
|
625 <?php endif; ?> |
|
626 </tbody> |
|
627 </table> |
|
628 |
|
629 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>"> |
|
630 <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?> |
|
631 <table class="form-table" role="presentation"> |
|
632 <tbody> |
|
633 <?php if ( get_uploaded_header_images() ) : ?> |
|
634 <tr> |
|
635 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th> |
|
636 <td> |
|
637 <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?></p> |
|
638 <?php |
|
639 $this->show_header_selector( 'uploaded' ); |
|
640 ?> |
|
641 </td> |
|
642 </tr> |
|
643 <?php |
|
644 endif; |
|
645 if ( ! empty( $this->default_headers ) ) : |
|
646 ?> |
|
647 <tr> |
|
648 <th scope="row"><?php _e( 'Default Images' ); ?></th> |
|
649 <td> |
|
650 <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?> |
|
651 <p><?php _e( 'If you don‘t want to upload your own image, you can use one of these cool headers, or show a random one.' ); ?></p> |
|
652 <?php else : ?> |
|
653 <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ); ?></p> |
|
654 <?php endif; ?> |
|
655 <?php |
|
656 $this->show_header_selector( 'default' ); |
|
657 ?> |
|
658 </td> |
|
659 </tr> |
|
660 <?php |
|
661 endif; |
|
662 if ( get_header_image() ) : |
|
663 ?> |
|
664 <tr> |
|
665 <th scope="row"><?php _e( 'Remove Image' ); ?></th> |
|
666 <td> |
|
667 <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ); ?></p> |
|
668 <?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?> |
|
669 </td> |
|
670 </tr> |
|
671 <?php |
|
672 endif; |
|
673 |
|
674 $default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() ); |
|
675 if ( $default_image && get_header_image() != $default_image ) : |
|
676 ?> |
|
677 <tr> |
|
678 <th scope="row"><?php _e( 'Reset Image' ); ?></th> |
|
679 <td> |
|
680 <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p> |
|
681 <?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?> |
|
682 </td> |
|
683 </tr> |
|
684 <?php endif; ?> |
|
685 </tbody> |
|
686 </table> |
|
687 |
|
688 <?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?> |
|
689 |
|
690 <h2><?php _e( 'Header Text' ); ?></h2> |
|
691 |
|
692 <table class="form-table" role="presentation"> |
|
693 <tbody> |
|
694 <tr> |
|
695 <th scope="row"><?php _e( 'Header Text' ); ?></th> |
|
696 <td> |
|
697 <p> |
|
698 <label><input type="checkbox" name="display-header-text" id="display-header-text"<?php checked( display_header_text() ); ?> /> <?php _e( 'Show header text with your image.' ); ?></label> |
|
699 </p> |
|
700 </td> |
|
701 </tr> |
|
702 |
|
703 <tr class="displaying-header-text"> |
|
704 <th scope="row"><?php _e( 'Text Color' ); ?></th> |
|
705 <td> |
|
706 <p> |
|
707 <?php |
|
708 $default_color = ''; |
|
709 if ( current_theme_supports( 'custom-header', 'default-text-color' ) ) { |
|
710 $default_color = get_theme_support( 'custom-header', 'default-text-color' ); |
|
711 if ( $default_color && false === strpos( $default_color, '#' ) ) { |
|
712 $default_color = '#' . $default_color; |
|
713 } |
|
714 } |
|
715 |
|
716 $default_color_attr = $default_color ? ' data-default-color="' . esc_attr( $default_color ) . '"' : ''; |
|
717 |
|
718 $header_textcolor = display_header_text() ? get_header_textcolor() : get_theme_support( 'custom-header', 'default-text-color' ); |
|
719 if ( $header_textcolor && false === strpos( $header_textcolor, '#' ) ) { |
|
720 $header_textcolor = '#' . $header_textcolor; |
|
721 } |
|
722 |
|
723 echo '<input type="text" name="text-color" id="text-color" value="' . esc_attr( $header_textcolor ) . '"' . $default_color_attr . ' />'; |
|
724 if ( $default_color ) { |
|
725 echo ' <span class="description hide-if-js">' . sprintf( _x( 'Default: %s', 'color' ), esc_html( $default_color ) ) . '</span>'; |
|
726 } |
|
727 ?> |
|
728 </p> |
|
729 </td> |
|
730 </tr> |
|
731 </tbody> |
|
732 </table> |
|
733 <?php |
|
734 endif; |
|
735 |
|
736 /** |
|
737 * Fires just before the submit button in the custom header options form. |
|
738 * |
|
739 * @since 3.1.0 |
|
740 */ |
|
741 do_action( 'custom_header_options' ); |
|
742 |
|
743 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); |
|
744 ?> |
|
745 |
|
746 <?php submit_button( null, 'primary', 'save-header-options' ); ?> |
|
747 </form> |
|
748 </div> |
|
749 |
|
750 <?php |
|
751 } |
|
752 |
|
753 /** |
|
754 * Display second step of custom header image page. |
|
755 * |
|
756 * @since 2.1.0 |
|
757 */ |
|
758 public function step_2() { |
|
759 check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' ); |
|
760 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { |
|
761 wp_die( |
|
762 '<h1>' . __( 'Something went wrong.' ) . '</h1>' . |
|
763 '<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>', |
|
764 403 |
|
765 ); |
|
766 } |
|
767 |
|
768 if ( empty( $_POST ) && isset( $_GET['file'] ) ) { |
|
769 $attachment_id = absint( $_GET['file'] ); |
|
770 $file = get_attached_file( $attachment_id, true ); |
|
771 $url = wp_get_attachment_image_src( $attachment_id, 'full' ); |
|
772 $url = $url[0]; |
|
773 } elseif ( isset( $_POST ) ) { |
|
774 $data = $this->step_2_manage_upload(); |
|
775 $attachment_id = $data['attachment_id']; |
|
776 $file = $data['file']; |
|
777 $url = $data['url']; |
|
778 } |
|
779 |
|
780 if ( file_exists( $file ) ) { |
|
781 list( $width, $height, $type, $attr ) = getimagesize( $file ); |
|
782 } else { |
|
783 $data = wp_get_attachment_metadata( $attachment_id ); |
|
784 $height = isset( $data['height'] ) ? $data['height'] : 0; |
|
785 $width = isset( $data['width'] ) ? $data['width'] : 0; |
|
786 unset( $data ); |
|
787 } |
|
788 |
|
789 $max_width = 0; |
|
790 // For flex, limit size of image displayed to 1500px unless theme says otherwise |
|
791 if ( current_theme_supports( 'custom-header', 'flex-width' ) ) { |
|
792 $max_width = 1500; |
|
793 } |
|
794 |
|
795 if ( current_theme_supports( 'custom-header', 'max-width' ) ) { |
|
796 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) ); |
|
797 } |
|
798 $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) ); |
|
799 |
|
800 // If flexible height isn't supported and the image is the exact right size |
|
801 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) |
|
802 && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) ) { |
|
803 // Add the meta-data |
|
804 if ( file_exists( $file ) ) { |
|
805 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); |
|
806 } |
|
807 |
|
808 $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) ); |
|
809 |
|
810 /** |
|
811 * Fires after the header image is set or an error is returned. |
|
812 * |
|
813 * @since 2.1.0 |
|
814 * |
|
815 * @param string $file Path to the file. |
|
816 * @param int $attachment_id Attachment ID. |
|
817 */ |
|
818 do_action( 'wp_create_file_in_uploads', $file, $attachment_id ); // For replication |
|
819 |
|
820 return $this->finished(); |
|
821 } elseif ( $width > $max_width ) { |
|
822 $oitar = $width / $max_width; |
|
823 $image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file ) ); |
|
824 if ( ! $image || is_wp_error( $image ) ) { |
|
825 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
|
826 } |
|
827 |
|
828 /** This filter is documented in wp-admin/custom-header.php */ |
|
829 $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication |
|
830 |
|
831 $url = str_replace( wp_basename( $url ), wp_basename( $image ), $url ); |
|
832 $width = $width / $oitar; |
|
833 $height = $height / $oitar; |
|
834 } else { |
|
835 $oitar = 1; |
|
836 } |
|
837 ?> |
|
838 |
|
839 <div class="wrap"> |
|
840 <h1><?php _e( 'Crop Header Image' ); ?></h1> |
|
841 |
|
842 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>"> |
|
843 <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p> |
|
844 <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p> |
|
845 |
|
846 <div id="crop_image" style="position: relative"> |
|
847 <img src="<?php echo esc_url( $url ); ?>" id="upload" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="" /> |
|
848 </div> |
|
849 |
|
850 <input type="hidden" name="x1" id="x1" value="0"/> |
|
851 <input type="hidden" name="y1" id="y1" value="0"/> |
|
852 <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/> |
|
853 <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/> |
|
854 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" /> |
|
855 <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" /> |
|
856 <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?> |
|
857 <input type="hidden" name="create-new-attachment" value="true" /> |
|
858 <?php } ?> |
|
859 <?php wp_nonce_field( 'custom-header-crop-image' ); ?> |
|
860 |
|
861 <p class="submit"> |
|
862 <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?> |
|
863 <?php |
|
864 if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) { |
|
865 submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false ); |
|
866 } |
|
867 ?> |
|
868 </p> |
|
869 </form> |
|
870 </div> |
|
871 <?php |
|
872 } |
|
873 |
|
874 |
|
875 /** |
|
876 * Upload the file to be cropped in the second step. |
|
877 * |
|
878 * @since 3.4.0 |
|
879 */ |
|
880 public function step_2_manage_upload() { |
|
881 $overrides = array( 'test_form' => false ); |
|
882 |
|
883 $uploaded_file = $_FILES['import']; |
|
884 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); |
|
885 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { |
|
886 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); |
|
887 } |
|
888 |
|
889 $file = wp_handle_upload( $uploaded_file, $overrides ); |
|
890 |
|
891 if ( isset( $file['error'] ) ) { |
|
892 wp_die( $file['error'], __( 'Image Upload Error' ) ); |
|
893 } |
|
894 |
|
895 $url = $file['url']; |
|
896 $type = $file['type']; |
|
897 $file = $file['file']; |
|
898 $filename = wp_basename( $file ); |
|
899 |
|
900 // Construct the object array |
|
901 $object = array( |
|
902 'post_title' => $filename, |
|
903 'post_content' => $url, |
|
904 'post_mime_type' => $type, |
|
905 'guid' => $url, |
|
906 'context' => 'custom-header', |
|
907 ); |
|
908 |
|
909 // Save the data |
|
910 $attachment_id = wp_insert_attachment( $object, $file ); |
|
911 return compact( 'attachment_id', 'file', 'filename', 'url', 'type' ); |
|
912 } |
|
913 |
|
914 /** |
|
915 * Display third step of custom header image page. |
|
916 * |
|
917 * @since 2.1.0 |
|
918 * @since 4.4.0 Switched to using wp_get_attachment_url() instead of the guid |
|
919 * for retrieving the header image URL. |
|
920 */ |
|
921 public function step_3() { |
|
922 check_admin_referer( 'custom-header-crop-image' ); |
|
923 |
|
924 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { |
|
925 wp_die( |
|
926 '<h1>' . __( 'Something went wrong.' ) . '</h1>' . |
|
927 '<p>' . __( 'The current theme does not support uploading a custom header image.' ) . '</p>', |
|
928 403 |
|
929 ); |
|
930 } |
|
931 |
|
932 if ( ! empty( $_POST['skip-cropping'] ) && ! ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) { |
|
933 wp_die( |
|
934 '<h1>' . __( 'Something went wrong.' ) . '</h1>' . |
|
935 '<p>' . __( 'The current theme does not support a flexible sized header image.' ) . '</p>', |
|
936 403 |
|
937 ); |
|
938 } |
|
939 |
|
940 if ( $_POST['oitar'] > 1 ) { |
|
941 $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; |
|
942 $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; |
|
943 $_POST['width'] = $_POST['width'] * $_POST['oitar']; |
|
944 $_POST['height'] = $_POST['height'] * $_POST['oitar']; |
|
945 } |
|
946 |
|
947 $attachment_id = absint( $_POST['attachment_id'] ); |
|
948 $original = get_attached_file( $attachment_id ); |
|
949 |
|
950 $dimensions = $this->get_header_dimensions( |
|
951 array( |
|
952 'height' => $_POST['height'], |
|
953 'width' => $_POST['width'], |
|
954 ) |
|
955 ); |
|
956 $height = $dimensions['dst_height']; |
|
957 $width = $dimensions['dst_width']; |
|
958 |
|
959 if ( empty( $_POST['skip-cropping'] ) ) { |
|
960 $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height ); |
|
961 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { |
|
962 $cropped = _copy_image_file( $attachment_id ); |
|
963 } else { |
|
964 $cropped = get_attached_file( $attachment_id ); |
|
965 } |
|
966 |
|
967 if ( ! $cropped || is_wp_error( $cropped ) ) { |
|
968 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
|
969 } |
|
970 |
|
971 /** This filter is documented in wp-admin/custom-header.php */ |
|
972 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication |
|
973 |
|
974 $object = $this->create_attachment_object( $cropped, $attachment_id ); |
|
975 |
|
976 if ( ! empty( $_POST['create-new-attachment'] ) ) { |
|
977 unset( $object['ID'] ); |
|
978 } |
|
979 |
|
980 // Update the attachment |
|
981 $attachment_id = $this->insert_attachment( $object, $cropped ); |
|
982 |
|
983 $url = wp_get_attachment_url( $attachment_id ); |
|
984 $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) ); |
|
985 |
|
986 // Cleanup. |
|
987 $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original ); |
|
988 if ( file_exists( $medium ) ) { |
|
989 wp_delete_file( $medium ); |
|
990 } |
|
991 |
|
992 if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) { |
|
993 wp_delete_file( $original ); |
|
994 } |
|
995 |
|
996 return $this->finished(); |
|
997 } |
|
998 |
|
999 /** |
|
1000 * Display last step of custom header image page. |
|
1001 * |
|
1002 * @since 2.1.0 |
|
1003 */ |
|
1004 public function finished() { |
|
1005 $this->updated = true; |
|
1006 $this->step_1(); |
|
1007 } |
|
1008 |
|
1009 /** |
|
1010 * Display the page based on the current step. |
|
1011 * |
|
1012 * @since 2.1.0 |
|
1013 */ |
|
1014 public function admin_page() { |
|
1015 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1016 wp_die( __( 'Sorry, you are not allowed to customize headers.' ) ); |
|
1017 } |
|
1018 $step = $this->step(); |
|
1019 if ( 2 == $step ) { |
|
1020 $this->step_2(); |
|
1021 } elseif ( 3 == $step ) { |
|
1022 $this->step_3(); |
|
1023 } else { |
|
1024 $this->step_1(); |
|
1025 } |
|
1026 } |
|
1027 |
|
1028 /** |
|
1029 * Unused since 3.5.0. |
|
1030 * |
|
1031 * @since 3.4.0 |
|
1032 * |
|
1033 * @param array $form_fields |
|
1034 * @return array $form_fields |
|
1035 */ |
|
1036 public function attachment_fields_to_edit( $form_fields ) { |
|
1037 return $form_fields; |
|
1038 } |
|
1039 |
|
1040 /** |
|
1041 * Unused since 3.5.0. |
|
1042 * |
|
1043 * @since 3.4.0 |
|
1044 * |
|
1045 * @param array $tabs |
|
1046 * @return array $tabs |
|
1047 */ |
|
1048 public function filter_upload_tabs( $tabs ) { |
|
1049 return $tabs; |
|
1050 } |
|
1051 |
|
1052 /** |
|
1053 * Choose a header image, selected from existing uploaded and default headers, |
|
1054 * or provide an array of uploaded header data (either new, or from media library). |
|
1055 * |
|
1056 * @since 3.4.0 |
|
1057 * |
|
1058 * @param mixed $choice Which header image to select. Allows for values of 'random-default-image', |
|
1059 * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling |
|
1060 * among the uploaded images; the key of a default image registered for that theme; and |
|
1061 * the key of an image uploaded for that theme (the attachment ID of the image). |
|
1062 * Or an array of arguments: attachment_id, url, width, height. All are required. |
|
1063 */ |
|
1064 final public function set_header_image( $choice ) { |
|
1065 if ( is_array( $choice ) || is_object( $choice ) ) { |
|
1066 $choice = (array) $choice; |
|
1067 if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) { |
|
1068 return; |
|
1069 } |
|
1070 |
|
1071 $choice['url'] = esc_url_raw( $choice['url'] ); |
|
1072 |
|
1073 $header_image_data = (object) array( |
|
1074 'attachment_id' => $choice['attachment_id'], |
|
1075 'url' => $choice['url'], |
|
1076 'thumbnail_url' => $choice['url'], |
|
1077 'height' => $choice['height'], |
|
1078 'width' => $choice['width'], |
|
1079 ); |
|
1080 |
|
1081 update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() ); |
|
1082 set_theme_mod( 'header_image', $choice['url'] ); |
|
1083 set_theme_mod( 'header_image_data', $header_image_data ); |
|
1084 return; |
|
1085 } |
|
1086 |
|
1087 if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) { |
|
1088 set_theme_mod( 'header_image', $choice ); |
|
1089 remove_theme_mod( 'header_image_data' ); |
|
1090 return; |
|
1091 } |
|
1092 |
|
1093 $uploaded = get_uploaded_header_images(); |
|
1094 if ( $uploaded && isset( $uploaded[ $choice ] ) ) { |
|
1095 $header_image_data = $uploaded[ $choice ]; |
|
1096 |
|
1097 } else { |
|
1098 $this->process_default_headers(); |
|
1099 if ( isset( $this->default_headers[ $choice ] ) ) { |
|
1100 $header_image_data = $this->default_headers[ $choice ]; |
|
1101 } else { |
|
1102 return; |
|
1103 } |
|
1104 } |
|
1105 |
|
1106 set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) ); |
|
1107 set_theme_mod( 'header_image_data', $header_image_data ); |
|
1108 } |
|
1109 |
|
1110 /** |
|
1111 * Remove a header image. |
|
1112 * |
|
1113 * @since 3.4.0 |
|
1114 */ |
|
1115 final public function remove_header_image() { |
|
1116 $this->set_header_image( 'remove-header' ); |
|
1117 } |
|
1118 |
|
1119 /** |
|
1120 * Reset a header image to the default image for the theme. |
|
1121 * |
|
1122 * This method does not do anything if the theme does not have a default header image. |
|
1123 * |
|
1124 * @since 3.4.0 |
|
1125 */ |
|
1126 final public function reset_header_image() { |
|
1127 $this->process_default_headers(); |
|
1128 $default = get_theme_support( 'custom-header', 'default-image' ); |
|
1129 |
|
1130 if ( ! $default ) { |
|
1131 $this->remove_header_image(); |
|
1132 return; |
|
1133 } |
|
1134 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); |
|
1135 |
|
1136 $default_data = array(); |
|
1137 foreach ( $this->default_headers as $header => $details ) { |
|
1138 if ( $details['url'] == $default ) { |
|
1139 $default_data = $details; |
|
1140 break; |
|
1141 } |
|
1142 } |
|
1143 |
|
1144 set_theme_mod( 'header_image', $default ); |
|
1145 set_theme_mod( 'header_image_data', (object) $default_data ); |
|
1146 } |
|
1147 |
|
1148 /** |
|
1149 * Calculate width and height based on what the currently selected theme supports. |
|
1150 * |
|
1151 * @since 3.9.0 |
|
1152 * |
|
1153 * @param array $dimensions |
|
1154 * @return array dst_height and dst_width of header image. |
|
1155 */ |
|
1156 final public function get_header_dimensions( $dimensions ) { |
|
1157 $max_width = 0; |
|
1158 $width = absint( $dimensions['width'] ); |
|
1159 $height = absint( $dimensions['height'] ); |
|
1160 $theme_height = get_theme_support( 'custom-header', 'height' ); |
|
1161 $theme_width = get_theme_support( 'custom-header', 'width' ); |
|
1162 $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' ); |
|
1163 $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' ); |
|
1164 $has_max_width = current_theme_supports( 'custom-header', 'max-width' ); |
|
1165 $dst = array( |
|
1166 'dst_height' => null, |
|
1167 'dst_width' => null, |
|
1168 ); |
|
1169 |
|
1170 // For flex, limit size of image displayed to 1500px unless theme says otherwise |
|
1171 if ( $has_flex_width ) { |
|
1172 $max_width = 1500; |
|
1173 } |
|
1174 |
|
1175 if ( $has_max_width ) { |
|
1176 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) ); |
|
1177 } |
|
1178 $max_width = max( $max_width, $theme_width ); |
|
1179 |
|
1180 if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) { |
|
1181 $dst['dst_height'] = absint( $height * ( $max_width / $width ) ); |
|
1182 } elseif ( $has_flex_height && $has_flex_width ) { |
|
1183 $dst['dst_height'] = $height; |
|
1184 } else { |
|
1185 $dst['dst_height'] = $theme_height; |
|
1186 } |
|
1187 |
|
1188 if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) { |
|
1189 $dst['dst_width'] = absint( $width * ( $max_width / $width ) ); |
|
1190 } elseif ( $has_flex_width && $has_flex_height ) { |
|
1191 $dst['dst_width'] = $width; |
|
1192 } else { |
|
1193 $dst['dst_width'] = $theme_width; |
|
1194 } |
|
1195 |
|
1196 return $dst; |
|
1197 } |
|
1198 |
|
1199 /** |
|
1200 * Create an attachment 'object'. |
|
1201 * |
|
1202 * @since 3.9.0 |
|
1203 * |
|
1204 * @param string $cropped Cropped image URL. |
|
1205 * @param int $parent_attachment_id Attachment ID of parent image. |
|
1206 * @return array Attachment object. |
|
1207 */ |
|
1208 final public function create_attachment_object( $cropped, $parent_attachment_id ) { |
|
1209 $parent = get_post( $parent_attachment_id ); |
|
1210 $parent_url = wp_get_attachment_url( $parent->ID ); |
|
1211 $url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url ); |
|
1212 |
|
1213 $size = @getimagesize( $cropped ); |
|
1214 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; |
|
1215 |
|
1216 $object = array( |
|
1217 'ID' => $parent_attachment_id, |
|
1218 'post_title' => wp_basename( $cropped ), |
|
1219 'post_mime_type' => $image_type, |
|
1220 'guid' => $url, |
|
1221 'context' => 'custom-header', |
|
1222 'post_parent' => $parent_attachment_id, |
|
1223 ); |
|
1224 |
|
1225 return $object; |
|
1226 } |
|
1227 |
|
1228 /** |
|
1229 * Insert an attachment and its metadata. |
|
1230 * |
|
1231 * @since 3.9.0 |
|
1232 * |
|
1233 * @param array $object Attachment object. |
|
1234 * @param string $cropped Cropped image URL. |
|
1235 * @return int Attachment ID. |
|
1236 */ |
|
1237 final public function insert_attachment( $object, $cropped ) { |
|
1238 $parent_id = isset( $object['post_parent'] ) ? $object['post_parent'] : null; |
|
1239 unset( $object['post_parent'] ); |
|
1240 |
|
1241 $attachment_id = wp_insert_attachment( $object, $cropped ); |
|
1242 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); |
|
1243 |
|
1244 // If this is a crop, save the original attachment ID as metadata. |
|
1245 if ( $parent_id ) { |
|
1246 $metadata['attachment_parent'] = $parent_id; |
|
1247 } |
|
1248 |
|
1249 /** |
|
1250 * Filters the header image attachment metadata. |
|
1251 * |
|
1252 * @since 3.9.0 |
|
1253 * |
|
1254 * @see wp_generate_attachment_metadata() |
|
1255 * |
|
1256 * @param array $metadata Attachment metadata. |
|
1257 */ |
|
1258 $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata ); |
|
1259 |
|
1260 wp_update_attachment_metadata( $attachment_id, $metadata ); |
|
1261 |
|
1262 return $attachment_id; |
|
1263 } |
|
1264 |
|
1265 /** |
|
1266 * Gets attachment uploaded by Media Manager, crops it, then saves it as a |
|
1267 * new object. Returns JSON-encoded object details. |
|
1268 * |
|
1269 * @since 3.9.0 |
|
1270 */ |
|
1271 public function ajax_header_crop() { |
|
1272 check_ajax_referer( 'image_editor-' . $_POST['id'], 'nonce' ); |
|
1273 |
|
1274 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1275 wp_send_json_error(); |
|
1276 } |
|
1277 |
|
1278 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { |
|
1279 wp_send_json_error(); |
|
1280 } |
|
1281 |
|
1282 $crop_details = $_POST['cropDetails']; |
|
1283 |
|
1284 $dimensions = $this->get_header_dimensions( |
|
1285 array( |
|
1286 'height' => $crop_details['height'], |
|
1287 'width' => $crop_details['width'], |
|
1288 ) |
|
1289 ); |
|
1290 |
|
1291 $attachment_id = absint( $_POST['id'] ); |
|
1292 |
|
1293 $cropped = wp_crop_image( |
|
1294 $attachment_id, |
|
1295 (int) $crop_details['x1'], |
|
1296 (int) $crop_details['y1'], |
|
1297 (int) $crop_details['width'], |
|
1298 (int) $crop_details['height'], |
|
1299 (int) $dimensions['dst_width'], |
|
1300 (int) $dimensions['dst_height'] |
|
1301 ); |
|
1302 |
|
1303 if ( ! $cropped || is_wp_error( $cropped ) ) { |
|
1304 wp_send_json_error( array( 'message' => __( 'Image could not be processed. Please go back and try again.' ) ) ); |
|
1305 } |
|
1306 |
|
1307 /** This filter is documented in wp-admin/custom-header.php */ |
|
1308 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication |
|
1309 |
|
1310 $object = $this->create_attachment_object( $cropped, $attachment_id ); |
|
1311 |
|
1312 $previous = $this->get_previous_crop( $object ); |
|
1313 |
|
1314 if ( $previous ) { |
|
1315 $object['ID'] = $previous; |
|
1316 } else { |
|
1317 unset( $object['ID'] ); |
|
1318 } |
|
1319 |
|
1320 $new_attachment_id = $this->insert_attachment( $object, $cropped ); |
|
1321 |
|
1322 $object['attachment_id'] = $new_attachment_id; |
|
1323 $object['url'] = wp_get_attachment_url( $new_attachment_id ); |
|
1324 |
|
1325 $object['width'] = $dimensions['dst_width']; |
|
1326 $object['height'] = $dimensions['dst_height']; |
|
1327 |
|
1328 wp_send_json_success( $object ); |
|
1329 } |
|
1330 |
|
1331 /** |
|
1332 * Given an attachment ID for a header image, updates its "last used" |
|
1333 * timestamp to now. |
|
1334 * |
|
1335 * Triggered when the user tries adds a new header image from the |
|
1336 * Media Manager, even if s/he doesn't save that change. |
|
1337 * |
|
1338 * @since 3.9.0 |
|
1339 */ |
|
1340 public function ajax_header_add() { |
|
1341 check_ajax_referer( 'header-add', 'nonce' ); |
|
1342 |
|
1343 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1344 wp_send_json_error(); |
|
1345 } |
|
1346 |
|
1347 $attachment_id = absint( $_POST['attachment_id'] ); |
|
1348 if ( $attachment_id < 1 ) { |
|
1349 wp_send_json_error(); |
|
1350 } |
|
1351 |
|
1352 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); |
|
1353 update_post_meta( $attachment_id, $key, time() ); |
|
1354 update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() ); |
|
1355 |
|
1356 wp_send_json_success(); |
|
1357 } |
|
1358 |
|
1359 /** |
|
1360 * Given an attachment ID for a header image, unsets it as a user-uploaded |
|
1361 * header image for the current theme. |
|
1362 * |
|
1363 * Triggered when the user clicks the overlay "X" button next to each image |
|
1364 * choice in the Customizer's Header tool. |
|
1365 * |
|
1366 * @since 3.9.0 |
|
1367 */ |
|
1368 public function ajax_header_remove() { |
|
1369 check_ajax_referer( 'header-remove', 'nonce' ); |
|
1370 |
|
1371 if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1372 wp_send_json_error(); |
|
1373 } |
|
1374 |
|
1375 $attachment_id = absint( $_POST['attachment_id'] ); |
|
1376 if ( $attachment_id < 1 ) { |
|
1377 wp_send_json_error(); |
|
1378 } |
|
1379 |
|
1380 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); |
|
1381 delete_post_meta( $attachment_id, $key ); |
|
1382 delete_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() ); |
|
1383 |
|
1384 wp_send_json_success(); |
|
1385 } |
|
1386 |
|
1387 /** |
|
1388 * Updates the last-used postmeta on a header image attachment after saving a new header image via the Customizer. |
|
1389 * |
|
1390 * @since 3.9.0 |
|
1391 * |
|
1392 * @param WP_Customize_Manager $wp_customize Customize manager. |
|
1393 */ |
|
1394 public function customize_set_last_used( $wp_customize ) { |
|
1395 |
|
1396 $header_image_data_setting = $wp_customize->get_setting( 'header_image_data' ); |
|
1397 if ( ! $header_image_data_setting ) { |
|
1398 return; |
|
1399 } |
|
1400 $data = $header_image_data_setting->post_value(); |
|
1401 |
|
1402 if ( ! isset( $data['attachment_id'] ) ) { |
|
1403 return; |
|
1404 } |
|
1405 |
|
1406 $attachment_id = $data['attachment_id']; |
|
1407 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); |
|
1408 update_post_meta( $attachment_id, $key, time() ); |
|
1409 } |
|
1410 |
|
1411 /** |
|
1412 * Gets the details of default header images if defined. |
|
1413 * |
|
1414 * @since 3.9.0 |
|
1415 * |
|
1416 * @return array Default header images. |
|
1417 */ |
|
1418 public function get_default_header_images() { |
|
1419 $this->process_default_headers(); |
|
1420 |
|
1421 // Get the default image if there is one. |
|
1422 $default = get_theme_support( 'custom-header', 'default-image' ); |
|
1423 |
|
1424 if ( ! $default ) { // If not, |
|
1425 return $this->default_headers; // easy peasy. |
|
1426 } |
|
1427 |
|
1428 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); |
|
1429 $already_has_default = false; |
|
1430 |
|
1431 foreach ( $this->default_headers as $k => $h ) { |
|
1432 if ( $h['url'] === $default ) { |
|
1433 $already_has_default = true; |
|
1434 break; |
|
1435 } |
|
1436 } |
|
1437 |
|
1438 if ( $already_has_default ) { |
|
1439 return $this->default_headers; |
|
1440 } |
|
1441 |
|
1442 // If the one true image isn't included in the default set, prepend it. |
|
1443 $header_images = array(); |
|
1444 $header_images['default'] = array( |
|
1445 'url' => $default, |
|
1446 'thumbnail_url' => $default, |
|
1447 'description' => 'Default', |
|
1448 ); |
|
1449 |
|
1450 // The rest of the set comes after. |
|
1451 return array_merge( $header_images, $this->default_headers ); |
|
1452 } |
|
1453 |
|
1454 /** |
|
1455 * Gets the previously uploaded header images. |
|
1456 * |
|
1457 * @since 3.9.0 |
|
1458 * |
|
1459 * @return array Uploaded header images. |
|
1460 */ |
|
1461 public function get_uploaded_header_images() { |
|
1462 $header_images = get_uploaded_header_images(); |
|
1463 $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); |
|
1464 $alt_text_key = '_wp_attachment_image_alt'; |
|
1465 |
|
1466 foreach ( $header_images as &$header_image ) { |
|
1467 $header_meta = get_post_meta( $header_image['attachment_id'] ); |
|
1468 $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : ''; |
|
1469 $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : ''; |
|
1470 } |
|
1471 |
|
1472 return $header_images; |
|
1473 } |
|
1474 |
|
1475 /** |
|
1476 * Get the ID of a previous crop from the same base image. |
|
1477 * |
|
1478 * @since 4.9.0 |
|
1479 * |
|
1480 * @param array $object A crop attachment object. |
|
1481 * @return int|false An attachment ID if one exists. False if none. |
|
1482 */ |
|
1483 public function get_previous_crop( $object ) { |
|
1484 $header_images = $this->get_uploaded_header_images(); |
|
1485 |
|
1486 // Bail early if there are no header images. |
|
1487 if ( empty( $header_images ) ) { |
|
1488 return false; |
|
1489 } |
|
1490 |
|
1491 $previous = false; |
|
1492 |
|
1493 foreach ( $header_images as $image ) { |
|
1494 if ( $image['attachment_parent'] === $object['post_parent'] ) { |
|
1495 $previous = $image['attachment_id']; |
|
1496 break; |
|
1497 } |
|
1498 } |
|
1499 |
|
1500 return $previous; |
|
1501 } |
|
1502 } |
|