Changeset 42343 for trunk/src/wp-admin/custom-header.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-admin/custom-header.php (modified) (53 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/custom-header.php
r41937 r42343 53 53 * @param callable $admin_image_div_callback Optional custom image div output callback. 54 54 */ 55 public function __construct( $admin_header_callback, $admin_image_div_callback = '') {56 $this->admin_header_callback = $admin_header_callback;55 public function __construct( $admin_header_callback, $admin_image_div_callback = '' ) { 56 $this->admin_header_callback = $admin_header_callback; 57 57 $this->admin_image_div_callback = $admin_image_div_callback; 58 58 59 59 add_action( 'admin_menu', array( $this, 'init' ) ); 60 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' ) );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 65 } 66 66 … … 92 92 */ 93 93 public function help() { 94 get_current_screen()->add_help_tab( array( 95 'id' => 'overview', 96 'title' => __('Overview'), 97 'content' => 98 '<p>' . __( 'This screen is used to customize the header section of your theme.') . '</p>' . 99 '<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>' 100 ) ); 101 102 get_current_screen()->add_help_tab( array( 103 'id' => 'set-header-image', 104 'title' => __('Header Image'), 105 'content' => 106 '<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>' . 107 '<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>' . 108 '<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>' . 109 '<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>' 110 ) ); 111 112 get_current_screen()->add_help_tab( array( 113 'id' => 'set-header-text', 114 'title' => __('Header Text'), 115 'content' => 116 '<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>' . 117 '<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>' . 118 '<p>' . __( 'Don’t forget to click “Save Changes” when you’re done!') . '</p>' 119 ) ); 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 ); 120 126 121 127 get_current_screen()->set_help_sidebar( … … 134 140 */ 135 141 public function step() { 136 if ( ! isset( $_GET['step'] ) ) 142 if ( ! isset( $_GET['step'] ) ) { 137 143 return 1; 144 } 138 145 139 146 $step = (int) $_GET['step']; … … 141 148 ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) || 142 149 ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) ) 143 ) 150 ) { 144 151 return 1; 152 } 145 153 146 154 return $step; … … 158 166 wp_enqueue_media(); 159 167 wp_enqueue_script( 'custom-header' ); 160 if ( current_theme_supports( 'custom-header', 'header-text' ) ) 168 if ( current_theme_supports( 'custom-header', 'header-text' ) ) { 161 169 wp_enqueue_script( 'wp-color-picker' ); 170 } 162 171 } elseif ( 2 == $step ) { 163 wp_enqueue_script( 'imgareaselect');172 wp_enqueue_script( 'imgareaselect' ); 164 173 } 165 174 } … … 173 182 $step = $this->step(); 174 183 175 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) 184 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { 176 185 wp_enqueue_style( 'wp-color-picker' ); 177 elseif ( 2 == $step ) 178 wp_enqueue_style('imgareaselect'); 186 } elseif ( 2 == $step ) { 187 wp_enqueue_style( 'imgareaselect' ); 188 } 179 189 } 180 190 … … 185 195 */ 186 196 public function take_action() { 187 if ( ! current_user_can( 'edit_theme_options') )197 if ( ! current_user_can( 'edit_theme_options' ) ) { 188 198 return; 189 190 if ( empty( $_POST ) ) 199 } 200 201 if ( empty( $_POST ) ) { 191 202 return; 203 } 192 204 193 205 $this->updated = true; … … 211 223 check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' ); 212 224 $_POST['text-color'] = str_replace( '#', '', $_POST['text-color'] ); 213 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['text-color']);214 if ( strlen( $color) == 6 || strlen($color) == 3 )215 set_theme_mod( 'header_textcolor', $color);216 elseif ( ! $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 ) { 217 229 set_theme_mod( 'header_textcolor', 'blank' ); 230 } 218 231 } 219 232 … … 235 248 global $_wp_default_headers; 236 249 237 if ( ! isset($_wp_default_headers) )250 if ( ! isset( $_wp_default_headers ) ) { 238 251 return; 252 } 239 253 240 254 if ( ! empty( $this->default_headers ) ) { … … 242 256 } 243 257 244 $this->default_headers = $_wp_default_headers;245 $template_directory_uri = get_template_directory_uri();258 $this->default_headers = $_wp_default_headers; 259 $template_directory_uri = get_template_directory_uri(); 246 260 $stylesheet_directory_uri = get_stylesheet_directory_uri(); 247 foreach ( array_keys( $this->default_headers) as $header ) {248 $this->default_headers[ $header]['url'] = sprintf( $this->default_headers[$header]['url'], $template_directory_uri, $stylesheet_directory_uri );249 $this->default_headers[ $header]['thumbnail_url'] = sprintf( $this->default_headers[$header]['thumbnail_url'], $template_directory_uri, $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 ); 250 264 } 251 265 } … … 267 281 } else { 268 282 $headers = get_uploaded_header_images(); 269 $type = 'uploaded';283 $type = 'uploaded'; 270 284 } 271 285 … … 281 295 foreach ( $headers as $header_key => $header ) { 282 296 $header_thumbnail = $header['thumbnail_url']; 283 $header_url = $header['url'];284 $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text'];297 $header_url = $header['url']; 298 $header_alt_text = empty( $header['alt_text'] ) ? '' : $header['alt_text']; 285 299 echo '<div class="default-header">'; 286 300 echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />'; 287 301 $width = ''; 288 if ( ! empty( $header['attachment_id'] ) )302 if ( ! empty( $header['attachment_id'] ) ) { 289 303 $width = ' width="230"'; 290 echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) .'"' . $width . ' /></label>'; 304 } 305 echo '<img src="' . set_url_scheme( $header_thumbnail ) . '" alt="' . esc_attr( $header_alt_text ) . '"' . $width . ' /></label>'; 291 306 echo '</div>'; 292 307 } … … 301 316 public function js() { 302 317 $step = $this->step(); 303 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) 318 if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) { 304 319 $this->js_1(); 305 elseif ( 2 == $step )320 } elseif ( 2 == $step ) { 306 321 $this->js_2(); 322 } 307 323 } 308 324 … … 373 389 * @since 2.6.0 374 390 */ 375 public function js_2() { ?> 391 public function js_2() { 392 393 ?> 376 394 <script type="text/javascript"> 377 395 function onEndCrop( coords ) { … … 485 503 } else { 486 504 $custom_header = get_custom_header(); 487 $header_image = get_header_image();505 $header_image = get_header_image(); 488 506 489 507 if ( $header_image ) { 490 508 $header_image_style = 'background-image:url(' . esc_url( $header_image ) . ');'; 491 } else {509 } else { 492 510 $header_image_style = ''; 493 511 } 494 512 495 if ( $custom_header->width ) 513 if ( $custom_header->width ) { 496 514 $header_image_style .= 'max-width:' . $custom_header->width . 'px;'; 497 if ( $custom_header->height ) 515 } 516 if ( $custom_header->height ) { 498 517 $header_image_style .= 'height:' . $custom_header->height . 'px;'; 518 } 499 519 ?> 500 520 <div id="headimg" style="<?php echo $header_image_style; ?>"> 501 521 <?php 502 if ( display_header_text() ) 522 if ( display_header_text() ) { 503 523 $style = ' style="color:#' . get_header_textcolor() . ';"'; 504 else524 } else { 505 525 $style = ' style="display:none;"'; 526 } 506 527 ?> 507 <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>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> 508 529 <div id="desc" class="displaying-header-text" <?php echo $style; ?>><?php bloginfo( 'description' ); ?></div> 509 530 </div> … … 522 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' ) ); 523 544 } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) { 524 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) 545 if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) { 525 546 printf( 526 547 /* translators: %s: size in pixels */ … … 532 553 ) 533 554 ); 555 } 534 556 } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) { 535 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) 557 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) { 536 558 printf( 537 559 /* translators: %s: size in pixels */ … … 543 565 ) 544 566 ); 567 } 545 568 } 546 569 if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) { 547 if ( current_theme_supports( 'custom-header', 'width' ) ) 570 if ( current_theme_supports( 'custom-header', 'width' ) ) { 548 571 printf( 549 572 /* translators: %s: size in pixels */ … … 555 578 ) 556 579 ); 557 if ( current_theme_supports( 'custom-header', 'height' ) ) 580 } 581 if ( current_theme_supports( 'custom-header', 'height' ) ) { 558 582 printf( 559 583 /* translators: %s: size in pixels */ … … 565 589 ) 566 590 ); 567 } 568 ?></p> 569 <form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="<?php echo esc_url( add_query_arg( 'step', 2 ) ) ?>"> 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 ) ); ?>"> 570 596 <p> 571 597 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br /> … … 576 602 </p> 577 603 <?php 578 $modal_update_href = esc_url( add_query_arg( array( 579 'page' => 'custom-header', 580 'step' => 2, 581 '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'), 582 ), admin_url('themes.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 ), admin_url( 'themes.php' ) 611 ) 612 ); 583 613 ?> 584 614 <p> … … 596 626 </table> 597 627 598 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ) ?>">628 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 1 ) ); ?>"> 599 629 <?php submit_button( null, 'screen-reader-text', 'save-header-options', false ); ?> 600 630 <table class="form-table"> … … 604 634 <th scope="row"><?php _e( 'Uploaded Images' ); ?></th> 605 635 <td> 606 <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ) ?></p>636 <p><?php _e( 'You can choose one of your previously uploaded headers, or show a random one.' ); ?></p> 607 637 <?php 608 638 $this->show_header_selector( 'uploaded' ); … … 610 640 </td> 611 641 </tr> 612 <?php endif; 613 if ( ! empty( $this->default_headers ) ) : ?> 642 <?php 643 endif; 644 if ( ! empty( $this->default_headers ) ) : 645 ?> 614 646 <tr> 615 647 <th scope="row"><?php _e( 'Default Images' ); ?></th> 616 648 <td> 617 649 <?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?> 618 <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>619 <?php else : ?>620 <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>650 <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> 651 <?php else : ?> 652 <p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ); ?></p> 621 653 <?php endif; ?> 622 654 <?php 623 $this->show_header_selector( 'default' );655 $this->show_header_selector( 'default' ); 624 656 ?> 625 657 </td> 626 658 </tr> 627 <?php endif; 628 if ( get_header_image() ) : ?> 659 <?php 660 endif; 661 if ( get_header_image() ) : 662 ?> 629 663 <tr> 630 664 <th scope="row"><?php _e( 'Remove Image' ); ?></th> 631 665 <td> 632 <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ) ?></p>666 <p><?php _e( 'This will remove the header image. You will not be able to restore any customizations.' ); ?></p> 633 667 <?php submit_button( __( 'Remove Header Image' ), '', 'removeheader', false ); ?> 634 668 </td> 635 669 </tr> 636 <?php endif; 670 <?php 671 endif; 637 672 638 673 $default_image = sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() ); 639 if ( $default_image && get_header_image() != $default_image ) : ?> 674 if ( $default_image && get_header_image() != $default_image ) : 675 ?> 640 676 <tr> 641 677 <th scope="row"><?php _e( 'Reset Image' ); ?></th> 642 678 <td> 643 <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ) ?></p>679 <p><?php _e( 'This will restore the original header image. You will not be able to restore any customizations.' ); ?></p> 644 680 <?php submit_button( __( 'Restore Original Header Image' ), '', 'resetheader', false ); ?> 645 681 </td> … … 694 730 </tbody> 695 731 </table> 696 <?php endif; 732 <?php 733 endif; 697 734 698 735 /** … … 703 740 do_action( 'custom_header_options' ); 704 741 705 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> 742 wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); 743 ?> 706 744 707 745 <?php submit_button( null, 'primary', 'save-header-options' ); ?> … … 709 747 </div> 710 748 711 <?php } 749 <?php 750 } 712 751 713 752 /** … … 717 756 */ 718 757 public function step_2() { 719 check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload');758 check_admin_referer( 'custom-header-upload', '_wpnonce-custom-header-upload' ); 720 759 if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) { 721 760 wp_die( … … 728 767 if ( empty( $_POST ) && isset( $_GET['file'] ) ) { 729 768 $attachment_id = absint( $_GET['file'] ); 730 $file = get_attached_file( $attachment_id, true );731 $url = wp_get_attachment_image_src( $attachment_id, 'full' );732 $url = $url[0];769 $file = get_attached_file( $attachment_id, true ); 770 $url = wp_get_attachment_image_src( $attachment_id, 'full' ); 771 $url = $url[0]; 733 772 } elseif ( isset( $_POST ) ) { 734 $data = $this->step_2_manage_upload();773 $data = $this->step_2_manage_upload(); 735 774 $attachment_id = $data['attachment_id']; 736 $file = $data['file'];737 $url = $data['url'];775 $file = $data['file']; 776 $url = $data['url']; 738 777 } 739 778 … … 741 780 list( $width, $height, $type, $attr ) = getimagesize( $file ); 742 781 } else { 743 $data = wp_get_attachment_metadata( $attachment_id );744 $height = isset( $data[ 'height' ] ) ? $data[ 'height'] : 0;745 $width = isset( $data[ 'width' ] ) ? $data[ 'width'] : 0;782 $data = wp_get_attachment_metadata( $attachment_id ); 783 $height = isset( $data['height'] ) ? $data['height'] : 0; 784 $width = isset( $data['width'] ) ? $data['width'] : 0; 746 785 unset( $data ); 747 786 } … … 749 788 $max_width = 0; 750 789 // For flex, limit size of image displayed to 1500px unless theme says otherwise 751 if ( current_theme_supports( 'custom-header', 'flex-width' ) ) 790 if ( current_theme_supports( 'custom-header', 'flex-width' ) ) { 752 791 $max_width = 1500; 753 754 if ( current_theme_supports( 'custom-header', 'max-width' ) ) 792 } 793 794 if ( current_theme_supports( 'custom-header', 'max-width' ) ) { 755 795 $max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) ); 796 } 756 797 $max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) ); 757 798 758 799 // If flexible height isn't supported and the image is the exact right size 759 800 if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) 760 && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) ) 761 { 801 && $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) ) { 762 802 // Add the meta-data 763 if ( file_exists( $file ) ) 803 if ( file_exists( $file ) ) { 764 804 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 805 } 765 806 766 807 $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) ); … … 779 820 } elseif ( $width > $max_width ) { 780 821 $oitar = $width / $max_width; 781 $image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));782 if ( ! $image || is_wp_error( $image ) ) 822 $image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( basename( $file ), 'midsize-' . basename( $file ), $file ) ); 823 if ( ! $image || is_wp_error( $image ) ) { 783 824 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 825 } 784 826 785 827 /** This filter is documented in wp-admin/custom-header.php */ 786 828 $image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication 787 829 788 $url = str_replace(basename($url), basename($image), $url);789 $width = $width / $oitar;830 $url = str_replace( basename( $url ), basename( $image ), $url ); 831 $width = $width / $oitar; 790 832 $height = $height / $oitar; 791 833 } else { … … 797 839 <h1><?php _e( 'Crop Header Image' ); ?></h1> 798 840 799 <form method="post" action="<?php echo esc_url( add_query_arg('step', 3)); ?>">800 <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.'); ?></p>801 <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p>841 <form method="post" action="<?php echo esc_url( add_query_arg( 'step', 3 ) ); ?>"> 842 <p class="hide-if-no-js"><?php _e( 'Choose the part of the image you want to use as your header.' ); ?></p> 843 <p class="hide-if-js"><strong><?php _e( 'You need JavaScript to choose a part of the image.' ); ?></strong></p> 802 844 803 845 <div id="crop_image" style="position: relative"> … … 814 856 <input type="hidden" name="create-new-attachment" value="true" /> 815 857 <?php } ?> 816 <?php wp_nonce_field( 'custom-header-crop-image' ) ?>858 <?php wp_nonce_field( 'custom-header-crop-image' ); ?> 817 859 818 860 <p class="submit"> 819 861 <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?> 820 862 <?php 821 if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) 863 if ( isset( $oitar ) && 1 == $oitar && ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) ) { 822 864 submit_button( __( 'Skip Cropping, Publish Image as Is' ), '', 'skip-cropping', false ); 865 } 823 866 ?> 824 867 </p> … … 835 878 */ 836 879 public function step_2_manage_upload() { 837 $overrides = array( 'test_form' => false);880 $overrides = array( 'test_form' => false ); 838 881 839 882 $uploaded_file = $_FILES['import']; 840 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );841 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) 883 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); 884 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { 842 885 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); 843 844 $file = wp_handle_upload($uploaded_file, $overrides); 845 846 if ( isset($file['error']) ) 847 wp_die( $file['error'], __( 'Image Upload Error' ) ); 848 849 $url = $file['url']; 850 $type = $file['type']; 851 $file = $file['file']; 852 $filename = basename($file); 886 } 887 888 $file = wp_handle_upload( $uploaded_file, $overrides ); 889 890 if ( isset( $file['error'] ) ) { 891 wp_die( $file['error'], __( 'Image Upload Error' ) ); 892 } 893 894 $url = $file['url']; 895 $type = $file['type']; 896 $file = $file['file']; 897 $filename = basename( $file ); 853 898 854 899 // Construct the object array … … 858 903 'post_mime_type' => $type, 859 904 'guid' => $url, 860 'context' => 'custom-header' 905 'context' => 'custom-header', 861 906 ); 862 907 … … 893 938 894 939 if ( $_POST['oitar'] > 1 ) { 895 $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];896 $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];897 $_POST['width'] = $_POST['width'] * $_POST['oitar'];940 $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; 941 $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; 942 $_POST['width'] = $_POST['width'] * $_POST['oitar']; 898 943 $_POST['height'] = $_POST['height'] * $_POST['oitar']; 899 944 } 900 945 901 946 $attachment_id = absint( $_POST['attachment_id'] ); 902 $original = get_attached_file($attachment_id); 903 904 $dimensions = $this->get_header_dimensions( array( 905 'height' => $_POST['height'], 906 'width' => $_POST['width'], 907 ) ); 908 $height = $dimensions['dst_height']; 909 $width = $dimensions['dst_width']; 910 911 if ( empty( $_POST['skip-cropping'] ) ) 947 $original = get_attached_file( $attachment_id ); 948 949 $dimensions = $this->get_header_dimensions( 950 array( 951 'height' => $_POST['height'], 952 'width' => $_POST['width'], 953 ) 954 ); 955 $height = $dimensions['dst_height']; 956 $width = $dimensions['dst_width']; 957 958 if ( empty( $_POST['skip-cropping'] ) ) { 912 959 $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height ); 913 elseif ( ! empty( $_POST['create-new-attachment'] ) )960 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { 914 961 $cropped = _copy_image_file( $attachment_id ); 915 else962 } else { 916 963 $cropped = get_attached_file( $attachment_id ); 917 918 if ( ! $cropped || is_wp_error( $cropped ) ) 964 } 965 966 if ( ! $cropped || is_wp_error( $cropped ) ) { 919 967 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 968 } 920 969 921 970 /** This filter is documented in wp-admin/custom-header.php */ … … 924 973 $object = $this->create_attachment_object( $cropped, $attachment_id ); 925 974 926 if ( ! empty( $_POST['create-new-attachment'] ) ) 975 if ( ! empty( $_POST['create-new-attachment'] ) ) { 927 976 unset( $object['ID'] ); 977 } 928 978 929 979 // Update the attachment … … 962 1012 */ 963 1013 public function admin_page() { 964 if ( ! current_user_can('edit_theme_options') ) 965 wp_die(__('Sorry, you are not allowed to customize headers.')); 1014 if ( ! current_user_can( 'edit_theme_options' ) ) { 1015 wp_die( __( 'Sorry, you are not allowed to customize headers.' ) ); 1016 } 966 1017 $step = $this->step(); 967 if ( 2 == $step ) 1018 if ( 2 == $step ) { 968 1019 $this->step_2(); 969 elseif ( 3 == $step )1020 } elseif ( 3 == $step ) { 970 1021 $this->step_3(); 971 else1022 } else { 972 1023 $this->step_1(); 1024 } 973 1025 } 974 1026 … … 1004 1056 * 1005 1057 * @param mixed $choice Which header image to select. Allows for values of 'random-default-image', 1006 * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling1007 * among the uploaded images; the key of a default image registered for that theme; and1008 * the key of an image uploaded for that theme (the attachment ID of the image).1058 * for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling 1059 * among the uploaded images; the key of a default image registered for that theme; and 1060 * the key of an image uploaded for that theme (the attachment ID of the image). 1009 1061 * Or an array of arguments: attachment_id, url, width, height. All are required. 1010 1062 */ … … 1012 1064 if ( is_array( $choice ) || is_object( $choice ) ) { 1013 1065 $choice = (array) $choice; 1014 if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) 1066 if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) { 1015 1067 return; 1068 } 1016 1069 1017 1070 $choice['url'] = esc_url_raw( $choice['url'] ); … … 1043 1096 } else { 1044 1097 $this->process_default_headers(); 1045 if ( isset( $this->default_headers[ $choice ] ) ) 1098 if ( isset( $this->default_headers[ $choice ] ) ) { 1046 1099 $header_image_data = $this->default_headers[ $choice ]; 1047 else1100 } else { 1048 1101 return; 1102 } 1049 1103 } 1050 1104 … … 1100 1154 */ 1101 1155 final public function get_header_dimensions( $dimensions ) { 1102 $max_width = 0;1103 $width = absint( $dimensions['width'] );1104 $height = absint( $dimensions['height'] );1105 $theme_height = get_theme_support( 'custom-header', 'height' );1106 $theme_width = get_theme_support( 'custom-header', 'width' );1107 $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' );1156 $max_width = 0; 1157 $width = absint( $dimensions['width'] ); 1158 $height = absint( $dimensions['height'] ); 1159 $theme_height = get_theme_support( 'custom-header', 'height' ); 1160 $theme_width = get_theme_support( 'custom-header', 'width' ); 1161 $has_flex_width = current_theme_supports( 'custom-header', 'flex-width' ); 1108 1162 $has_flex_height = current_theme_supports( 'custom-header', 'flex-height' ); 1109 $has_max_width = current_theme_supports( 'custom-header', 'max-width' ) ; 1110 $dst = array( 'dst_height' => null, 'dst_width' => null ); 1163 $has_max_width = current_theme_supports( 'custom-header', 'max-width' ); 1164 $dst = array( 1165 'dst_height' => null, 1166 'dst_width' => null, 1167 ); 1111 1168 1112 1169 // For flex, limit size of image displayed to 1500px unless theme says otherwise … … 1122 1179 if ( $has_flex_height && ( ! $has_flex_width || $width > $max_width ) ) { 1123 1180 $dst['dst_height'] = absint( $height * ( $max_width / $width ) ); 1124 } 1125 elseif ( $has_flex_height && $has_flex_width ) { 1181 } elseif ( $has_flex_height && $has_flex_width ) { 1126 1182 $dst['dst_height'] = $height; 1127 } 1128 else { 1183 } else { 1129 1184 $dst['dst_height'] = $theme_height; 1130 1185 } … … 1132 1187 if ( $has_flex_width && ( ! $has_flex_height || $width > $max_width ) ) { 1133 1188 $dst['dst_width'] = absint( $width * ( $max_width / $width ) ); 1134 } 1135 elseif ( $has_flex_width && $has_flex_height ) { 1189 } elseif ( $has_flex_width && $has_flex_height ) { 1136 1190 $dst['dst_width'] = $width; 1137 } 1138 else { 1191 } else { 1139 1192 $dst['dst_width'] = $theme_width; 1140 1193 } … … 1153 1206 */ 1154 1207 final public function create_attachment_object( $cropped, $parent_attachment_id ) { 1155 $parent = get_post( $parent_attachment_id );1208 $parent = get_post( $parent_attachment_id ); 1156 1209 $parent_url = wp_get_attachment_url( $parent->ID ); 1157 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );1158 1159 $size = @getimagesize( $cropped );1210 $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 1211 1212 $size = @getimagesize( $cropped ); 1160 1213 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; 1161 1214 1162 1215 $object = array( 1163 'ID' => $parent_attachment_id,1164 'post_title' => basename($cropped),1216 'ID' => $parent_attachment_id, 1217 'post_title' => basename( $cropped ), 1165 1218 'post_mime_type' => $image_type, 1166 'guid' => $url,1167 'context' => 'custom-header',1168 'post_parent' => $parent_attachment_id,1219 'guid' => $url, 1220 'context' => 'custom-header', 1221 'post_parent' => $parent_attachment_id, 1169 1222 ); 1170 1223 … … 1186 1239 1187 1240 $attachment_id = wp_insert_attachment( $object, $cropped ); 1188 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );1241 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); 1189 1242 1190 1243 // If this is a crop, save the original attachment ID as metadata. … … 1228 1281 $crop_details = $_POST['cropDetails']; 1229 1282 1230 $dimensions = $this->get_header_dimensions( array( 1231 'height' => $crop_details['height'], 1232 'width' => $crop_details['width'], 1233 ) ); 1283 $dimensions = $this->get_header_dimensions( 1284 array( 1285 'height' => $crop_details['height'], 1286 'width' => $crop_details['width'], 1287 ) 1288 ); 1234 1289 1235 1290 $attachment_id = absint( $_POST['id'] ); … … 1265 1320 1266 1321 $object['attachment_id'] = $new_attachment_id; 1267 $object['url'] = wp_get_attachment_url( $new_attachment_id );; 1268 $object['width'] = $dimensions['dst_width']; 1269 $object['height'] = $dimensions['dst_height']; 1322 $object['url'] = wp_get_attachment_url( $new_attachment_id ); 1323 1324 $object['width'] = $dimensions['dst_width']; 1325 $object['height'] = $dimensions['dst_height']; 1270 1326 1271 1327 wp_send_json_success( $object ); … … 1348 1404 1349 1405 $attachment_id = $data['attachment_id']; 1350 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();1406 $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); 1351 1407 update_post_meta( $attachment_id, $key, time() ); 1352 1408 } … … 1369 1425 } 1370 1426 1371 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );1427 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); 1372 1428 $already_has_default = false; 1373 1429 … … 1384 1440 1385 1441 // If the one true image isn't included in the default set, prepend it. 1386 $header_images = array();1442 $header_images = array(); 1387 1443 $header_images['default'] = array( 1388 1444 'url' => $default, 1389 1445 'thumbnail_url' => $default, 1390 'description' => 'Default' 1446 'description' => 'Default', 1391 1447 ); 1392 1448 … … 1405 1461 $header_images = get_uploaded_header_images(); 1406 1462 $timestamp_key = '_wp_attachment_custom_header_last_used_' . get_stylesheet(); 1407 $alt_text_key = '_wp_attachment_image_alt';1463 $alt_text_key = '_wp_attachment_image_alt'; 1408 1464 1409 1465 foreach ( $header_images as &$header_image ) { 1410 $header_meta = get_post_meta( $header_image['attachment_id'] );1466 $header_meta = get_post_meta( $header_image['attachment_id'] ); 1411 1467 $header_image['timestamp'] = isset( $header_meta[ $timestamp_key ] ) ? $header_meta[ $timestamp_key ] : ''; 1412 $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : '';1468 $header_image['alt_text'] = isset( $header_meta[ $alt_text_key ] ) ? $header_meta[ $alt_text_key ] : ''; 1413 1469 } 1414 1470
Note: See TracChangeset
for help on using the changeset viewer.