Make WordPress Core

Ticket #17242: 17242.4.diff

File 17242.4.diff, 5.2 KB (added by sabreuse, 11 years ago)

First pass at using a helper function

  • wp-admin/custom-header.php

     
    173173        }
    174174
    175175        /**
     176        * Get the header height if using a flexible header (should this be in theme.php?)
     177        * @since 3.4
     178        */
     179        function flex_header_height() {
     180                $header_support = get_theme_support( 'custom-header' );
     181
     182                if ( ! isset( $header_support[ 0 ] ) || empty( $header_support[ 0 ][ 'flex-height' ] ) || ! $header_support[ 0 ][ 'flex-height' ] ) {
     183                        return HEADER_IMAGE_HEIGHT;
     184                } else {
     185                        $header_image = get_header_image();
     186                        $header_image_post = get_page_by_title( substr( $header_image, strrpos( $header_image, '/') + 1 ), null, 'attachment' );
     187                        $header_image_info = wp_get_attachment_metadata( $header_image_post->ID );
     188                        return $header_image_info['height'];
     189                }
     190        }
     191        /**
    176192         * Execute custom header modification.
    177193         *
    178194         * @since 2.6.0
     
    438454                jQuery('img#upload').imgAreaSelect({
    439455                        handles: true,
    440456                        keys: true,
    441                         aspectRatio: xinit + ':' + yinit,
    442457                        show: true,
    443458                        x1: 0,
    444459                        y1: 0,
    445460                        x2: xinit,
    446461                        y2: yinit,
     462                        <?php
     463                        $header_support = get_theme_support( 'custom-header' );
     464                        if ( ! isset( $header_support[ 0 ] ) || empty( $header_support[ 0 ][ 'flex-height' ] ) || ! $header_support[ 0 ][ 'flex-height' ] ) {
     465                        ?>
     466                        aspectRatio: xinit + ':' + yinit,
    447467                        maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
     468                        <?php
     469                        }
     470                        ?>
    448471                        maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
    449472                        onInit: function () {
    450473                                jQuery('#width').val(xinit);
     
    490513<td >
    491514        <?php if ( $this->admin_image_div_callback ) {
    492515          call_user_func( $this->admin_image_div_callback );
    493         } else {
     516        } else { 
    494517        ?>
    495         <div id="headimg" style="max-width:<?php echo HEADER_IMAGE_WIDTH; ?>px;height:<?php echo HEADER_IMAGE_HEIGHT; ?>px;background-image:url(<?php esc_url ( header_image() ) ?>);">
     518        <div id="headimg" style="max-width:<?php echo HEADER_IMAGE_WIDTH; ?>px;height:<?php
     519        echo $this->flex_header_height(); ?>px;background-image:url(<?php esc_url ( header_image() ) ?>);">
    496520                <?php
    497521                if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
    498522                        $style = ' style="display:none;"';
     
    510534<th scope="row"><?php _e( 'Upload Image' ); ?></th>
    511535<td>
    512536        <p><?php _e( 'You can upload a custom header image to be shown at the top of your site instead of the default one. On the next screen you will be able to crop the image.' ); ?><br />
    513         <?php printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT ); ?></p>
     537        <?php
     538        $header_support = get_theme_support( 'custom-header' );
     539        if ( ! isset( $header_support[ 0 ] ) || empty( $header_support[ 0 ][ 'flex-height' ] ) || ! $header_support[ 0 ][ 'flex-height' ] ) {
     540                printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ), HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
     541        } else {
     542                printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ), HEADER_IMAGE_WIDTH );
     543                if ( ! empty( $header_support[ 0 ][ 'suggested-height' ] ) )
     544                        printf( __( ' Suggested height is <strong>%1$d pixels</strong>.' ), absint( $header_support[ 0 ][ 'suggested-height' ] ) );
     545        }
     546        ?></p>
    514547        <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
    515548        <p>
    516549                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     
    662695
    663696                list($width, $height, $type, $attr) = getimagesize( $file );
    664697
    665                 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
     698                $header_support = get_theme_support( 'custom-header' );
     699                // If flexible height isn't supported and the image is the exact right size
     700                if ( ( ! isset( $header_support[ 0 ] ) || empty( $header_support[ 0 ][ 'flex-height' ] ) || !empty( $header_support[ 0 ][ 'flex-height' ] ) ) && $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    666701                        // Add the meta-data
    667702                        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    668703                        update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
     
    733768                $attachment_id = absint( $_POST['attachment_id'] );
    734769                $original = get_attached_file($attachment_id);
    735770
    736                 $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
     771                $header_support = get_theme_support( 'custom-header' );
     772                if ( isset( $header_support[ 0 ] ) && ! empty( $header_support[ 0 ][ 'flex-height' ] ) )
     773                        $dst_height = (int) $_POST['height'] * ( HEADER_IMAGE_WIDTH / $_POST['width'] );
     774                else
     775                        $dst_height = HEADER_IMAGE_HEIGHT;
     776
     777                $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, $dst_height );
    737778                if ( is_wp_error( $cropped ) )
    738779                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    739780