WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/02/2012 11:35:37 PM (10 years ago)
Author:
ryan
Message:

Allow flexible sizes for custom header uploads. Round 1. Props aaroncampbell, sabreuse. see #17242

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-header.php

    r19712 r19815  
    189189        if ( isset( $_POST['resetheader'] ) ) {
    190190            check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
    191             remove_theme_mod( 'header_image' );
     191            $this->process_default_headers();
     192            $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
     193            $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     194            foreach ( $this->default_headers as $header => $details ) {
     195                if ( $details['url'] == $default ) {
     196                    $default_data = $details;
     197                    break;
     198                }
     199            }
     200            set_theme_mod( 'header_image', $default );
     201            if ( empty( $default_data['width'] ) )
     202                $default_data['width'] = HEADER_IMAGE_WIDTH;
     203            if ( empty( $default_data['height'] ) )
     204                $default_data['height'] = HEADER_IMAGE_HEIGHT;
     205            set_theme_mod( 'header_image_data', (object) $default_data );
    192206            return;
    193207        }
     
    226240                $this->process_default_headers();
    227241                $uploaded = get_uploaded_header_images();
    228                 if ( isset( $uploaded[$_POST['default-header']] ) )
     242                if ( isset( $uploaded[$_POST['default-header']] ) ) {
    229243                    set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
    230                 elseif ( isset( $this->default_headers[$_POST['default-header']] ) )
     244                    set_theme_mod( 'header_image_data', (object) $uploaded[$_POST['default-header']] );
     245                } elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) {
    231246                    set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
     247                    if ( empty( $this->default_headers[$_POST['default-header']]['width'] ) )
     248                        $this->default_headers[$_POST['default-header']]['width'] = HEADER_IMAGE_WIDTH;
     249                    if ( empty( $this->default_headers[$_POST['default-header']]['height'] ) )
     250                        $this->default_headers[$_POST['default-header']]['height'] = HEADER_IMAGE_HEIGHT;
     251                    set_theme_mod( 'header_image_data', (object) $this->default_headers[$_POST['default-header']] );
     252                }
    232253            }
    233254        }
     
    290311            echo '<label><input name="default-header" type="radio" value="' . esc_attr( $header_key ) . '" ' . checked( $header_url, get_theme_mod( 'header_image' ), false ) . ' />';
    291312            $width = '';
    292             if ( !empty( $header['uploaded'] ) )
     313            if ( !empty( $header['attachment_id'] ) )
    293314                $width = ' width="230"';
    294315            echo '<img src="' . $header_thumbnail . '" alt="' . esc_attr( $header_desc ) .'" title="' . esc_attr( $header_desc ) . '"' . $width . ' /></label>';
     
    420441
    421442    jQuery(document).ready(function() {
    422         var xinit = <?php echo HEADER_IMAGE_WIDTH; ?>;
    423         var yinit = <?php echo HEADER_IMAGE_HEIGHT; ?>;
     443        <?php
     444        $xinit = HEADER_IMAGE_WIDTH;
     445        $yinit = HEADER_IMAGE_HEIGHT;
     446        $header_support = get_theme_support( 'custom-header' );
     447        if ( !empty( $header_support[ 0 ][ 'suggested-width' ] ) )
     448            $xinit = $header_support[ 0 ][ 'suggested-width' ];
     449        if ( !empty( $header_support[ 0 ][ 'suggested-height' ] ) )
     450            $yinit = $header_support[ 0 ][ 'suggested-height' ];
     451        ?>
     452        var xinit = <?php echo absint( $xinit ); ?>;
     453        var yinit = <?php echo absint( $yinit ); ?>;
    424454        var ratio = xinit / yinit;
    425455        var ximg = jQuery('img#upload').width();
     
    439469            handles: true,
    440470            keys: true,
    441             aspectRatio: xinit + ':' + yinit,
    442471            show: true,
    443472            x1: 0,
     
    445474            x2: xinit,
    446475            y2: yinit,
     476            <?php
     477            if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
     478            ?>
     479            aspectRatio: xinit + ':' + yinit,
     480            <?php
     481            }
     482            if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
     483            ?>
    447484            maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
     485            <?php
     486            }
     487            if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
     488            ?>
    448489            maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
     490            <?php
     491            }
     492            ?>
    449493            onInit: function () {
    450494                jQuery('#width').val(xinit);
     
    493537    } else {
    494538    ?>
    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() ) ?>);">
     539    <div id="headimg" style="background-image:url(<?php esc_url ( header_image() ) ?>);max-width:<?php echo get_header_image_width(); ?>px;height:<?php echo get_header_image_height(); ?>px;">
    496540        <?php
    497541        if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
     
    511555<td>
    512556    <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>
     557    <?php
     558    if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
     559        printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
     560    } elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
     561        if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
     562            printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . '<br />', HEADER_IMAGE_WIDTH );
     563    } elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
     564        if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
     565            printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . '<br />', HEADER_IMAGE_HEIGHT );
     566    }
     567    if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
     568        $header_support = get_theme_support( 'custom-header' );
     569        if ( !empty( $header_support[ 0 ][ 'suggested-width' ] ) )
     570            printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-width' ] ) );
     571        if ( !empty( $header_support[ 0 ][ 'suggested-height' ] ) )
     572            printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-height' ] ) );
     573    }
     574    ?></p>
    514575    <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
    515576    <p>
     
    663724        list($width, $height, $type, $attr) = getimagesize( $file );
    664725
    665         if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
     726        $header_support = get_theme_support( 'custom-header' );
     727        $max_width = 0;
     728        // For flex, limit size of image displayed to 1500px unless theme says otherwise
     729        if ( current_theme_supports( 'custom-header', 'flex-width' ) )
     730            $max_width = 1500;
     731
     732        if ( !empty( $header_support[ 0 ][ 'max-width' ] ) )
     733            $max_width = max( $max_width, absint( $header_support[ 0 ][ 'max-width' ] ) );
     734
     735        if ( defined( 'HEADER_IMAGE_WIDTH' ) )
     736            $max_width = max( $max_width, HEADER_IMAGE_WIDTH );
     737        // If flexible height isn't supported and the image is the exact right size
     738        if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) && $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    666739            // Add the meta-data
    667740            wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
     
    671744            do_action('wp_create_file_in_uploads', $file, $id); // For replication
    672745            return $this->finished();
    673         } elseif ( $width > HEADER_IMAGE_WIDTH ) {
    674             $oitar = $width / HEADER_IMAGE_WIDTH;
    675             $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
     746        } elseif ( $width > $max_width ) {
     747            $oitar = $width / $max_width;
     748            $image = wp_crop_image($file, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
    676749            if ( is_wp_error( $image ) )
    677750                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     
    734807        $original = get_attached_file($attachment_id);
    735808
    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 );
     809        $header_support = get_theme_support( 'custom-header' );
     810        $max_width = 0;
     811        // For flex, limit size of image displayed to 1500px unless theme says otherwise
     812        if ( current_theme_supports( 'custom-header', 'flex-width' ) )
     813            $max_width = 1500;
     814
     815        if ( !empty( $header_support[ 0 ][ 'max-width' ] ) )
     816            $max_width = max( $max_width, absint( $header_support[ 0 ][ 'max-width' ] ) );
     817
     818        if ( defined( 'HEADER_IMAGE_WIDTH' ) )
     819            $max_width = max( $max_width, HEADER_IMAGE_WIDTH );
     820
     821        if ( ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) || $_POST['width'] > $max_width )
     822            $dst_height = absint( $_POST['height'] * ( $max_width / $_POST['width'] ) );
     823        elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
     824            $dst_height = absint( $_POST['height'] );
     825        else
     826            $dst_height = HEADER_IMAGE_HEIGHT;
     827
     828        if ( ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) ) || $_POST['width'] > $max_width )
     829            $dst_width = absint( $_POST['width'] * ( $max_width / $_POST['width'] ) );
     830        elseif ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
     831            $dst_width = absint( $_POST['width'] );
     832        else
     833            $dst_width = HEADER_IMAGE_WIDTH;
     834
     835        $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
    737836        if ( is_wp_error( $cropped ) )
    738837            wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     
    760859
    761860        set_theme_mod('header_image', $url);
     861
     862        $header_data                = new stdClass();
     863        $header_data->attachment_id = $attachment_id;
     864        $header_data->url           = $url;
     865        $header_data->thumbnail_url = $url;
     866        $header_data->width         = $dst_width;
     867        $header_data->height        = $dst_height;
     868
     869        set_theme_mod( 'header_image_data', $header_data );
    762870
    763871        // cleanup
Note: See TracChangeset for help on using the changeset viewer.