Make WordPress Core

Ticket #17242: 17242-height-width.diff

File 17242-height-width.diff, 12.4 KB (added by aaroncampbell, 11 years ago)
  • wp-includes/theme.php

     
    14571457}
    14581458
    14591459/**
    1460  * Get random header image from registered images in theme.
     1460 * Get random header image data from registered images in theme.
    14611461 *
    1462  * @since 3.2.0
     1462 * @since 3.4.0
    14631463 *
    14641464 * @return string Path to header image
    14651465 */
    1466 function get_random_header_image() {
    1467         global $_wp_default_headers;
    14681466
     1467function get_random_header_data() {
     1468        global $_wp_default_headers, $_wp_random_header;
     1469
    14691470        $header_image_mod = get_theme_mod( 'header_image', '' );
    14701471        $headers = array();
    14711472
     
    14841485        if ( empty( $headers ) )
    14851486                return '';
    14861487
    1487         $random_image = array_rand( $headers );
    1488         $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1488        $_wp_random_header = $headers[ array_rand( $headers ) ];
    14891489
    1490         return $header_url;
     1490        $_wp_random_header['url'] =  sprintf( $_wp_random_header['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1491        $_wp_random_header['thumbnail_url'] =  sprintf( $_wp_random_header['thumbnail_url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1492
     1493        return $_wp_random_header;
    14911494}
    14921495
    14931496/**
     1497 * Get random header image url from registered images in theme.
     1498 *
     1499 * @since 3.2.0
     1500 *
     1501 * @return string Path to header image
     1502 */
     1503
     1504function get_random_header_image() {
     1505        $random_image = get_random_header_data();
     1506        if ( '' == $random_image )
     1507                return '';
     1508        return $random_image['url'];
     1509}
     1510
     1511/**
    14941512 * Check if random header image is in use.
    14951513 *
    14961514 * Always true if user expressly chooses the option in Appearance > Header.
     
    15471565
    15481566        foreach ( (array) $headers as $header ) {
    15491567                $url = esc_url_raw( $header->guid );
     1568                $header_data = wp_get_attachment_metadata( $header->ID );
    15501569                $header = basename($url);
    15511570                $header_images[$header] = array();
    15521571                $header_images[$header]['url'] =  $url;
    15531572                $header_images[$header]['thumbnail_url'] =  $url;
    15541573                $header_images[$header]['uploaded'] = true;
     1574                $header_images[$header]['width'] = $header_data['width'];
     1575                $header_images[$header]['height'] = $header_data['height'];
    15551576        }
    15561577
    15571578        return $header_images;
    15581579}
    15591580
    15601581/**
     1582 * Get the header image width.
     1583 *
     1584 * @since 3.4.0
     1585 *
     1586 * @return int
     1587 */
     1588function get_header_image_width() {
     1589        if ( is_random_header_image() ) {
     1590                global $_wp_random_header;
     1591                return empty( $_wp_random_header['width'] )? HEADER_IMAGE_WIDTH : $_wp_random_header['width'];
     1592        }
     1593
     1594        return get_theme_mod( 'header_image_width', HEADER_IMAGE_WIDTH );
     1595}
     1596
     1597/**
     1598 * Get the header image height.
     1599 *
     1600 * @since 3.4.0
     1601 *
     1602 * @return int
     1603 */
     1604function get_header_image_height() {
     1605        if ( is_random_header_image() ) {
     1606                global $_wp_random_header;
     1607                return empty( $_wp_random_header['height'] )? HEADER_IMAGE_HEIGHT : $_wp_random_header['height'];
     1608        }
     1609
     1610        return get_theme_mod( 'header_image_height', HEADER_IMAGE_HEIGHT );
     1611}
     1612
     1613/**
     1614 * Theme supports flex height headers.
     1615 *
     1616 * @since 3.4.0
     1617 *
     1618 * @return int
     1619 */
     1620function theme_supports_flex_height_headers() {
     1621        $header_support = get_theme_support( 'custom-header' );
     1622        return ( isset( $header_support[ 0 ] ) && ! empty( $header_support[ 0 ][ 'flex-height' ] ) && $header_support[ 0 ][ 'flex-height' ] );
     1623}
     1624
     1625/**
     1626 * Theme supports flex width headers.
     1627 *
     1628 * @since 3.4.0
     1629 *
     1630 * @return int
     1631 */
     1632function theme_supports_flex_width_headers() {
     1633        $header_support = get_theme_support( 'custom-header' );
     1634        return ( isset( $header_support[ 0 ] ) && ! empty( $header_support[ 0 ][ 'flex-width' ] ) && $header_support[ 0 ][ 'flex-width' ] );
     1635}
     1636
     1637/**
    15611638 * Add callbacks for image header display.
    15621639 *
    15631640 * The parameter $header_callback callback will be required to display the
  • wp-admin/custom-header.php

     
    188188
    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                        $width = empty( $default_data['width'] )? HEADER_IMAGE_WIDTH : $default_data['width'];
     202                        $height = empty( $default_data['height'] )? HEADER_IMAGE_HEIGHT : $default_data['height'];
     203                        set_theme_mod( 'header_image_width', $width );
     204                        set_theme_mod( 'header_image_height', $height );
    192205                        return;
    193206                }
    194207
     
    225238                        } else {
    226239                                $this->process_default_headers();
    227240                                $uploaded = get_uploaded_header_images();
    228                                 if ( isset( $uploaded[$_POST['default-header']] ) )
     241                                if ( isset( $uploaded[$_POST['default-header']] ) ) {
    229242                                        set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
    230                                 elseif ( isset( $this->default_headers[$_POST['default-header']] ) )
     243                                        set_theme_mod( 'header_image_width', $uploaded[$_POST['default-header']]['width'] );
     244                                        set_theme_mod( 'header_image_height', $uploaded[$_POST['default-header']]['height'] );
     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                                        $width = empty( $this->default_headers[$_POST['default-header']]['width'] )? HEADER_IMAGE_WIDTH : $this->default_headers[$_POST['default-header']]['width'];
     248                                        $height = empty( $this->default_headers[$_POST['default-header']]['height'] )? HEADER_IMAGE_HEIGHT : $this->default_headers[$_POST['default-header']]['height'];
     249                                        set_theme_mod( 'header_image_width', $width );
     250                                        set_theme_mod( 'header_image_height', $height );
     251                                }
    232252                        }
    233253                }
    234254        }
     
    438458                jQuery('img#upload').imgAreaSelect({
    439459                        handles: true,
    440460                        keys: true,
    441                         aspectRatio: xinit + ':' + yinit,
    442461                        show: true,
    443462                        x1: 0,
    444463                        y1: 0,
    445464                        x2: xinit,
    446465                        y2: yinit,
     466                        <?php
     467                        if ( ! theme_supports_flex_height_headers() && ! theme_supports_flex_width_headers() ) {
     468                        ?>
     469                        aspectRatio: xinit + ':' + yinit,
     470                        <?php
     471                        }
     472                        if ( ! theme_supports_flex_height_headers() ) {
     473                        ?>
    447474                        maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
     475                        <?php
     476                        }
     477                        if ( ! theme_supports_flex_width_headers() ) {
     478                        ?>
    448479                        maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
     480                        <?php
     481                        }
     482                        ?>
    449483                        onInit: function () {
    450484                                jQuery('#width').val(xinit);
    451485                                jQuery('#height').val(yinit);
     
    492526          call_user_func( $this->admin_image_div_callback );
    493527        } else {
    494528        ?>
    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() ) ?>);">
     529        <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;">
    496530                <?php
    497531                if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
    498532                        $style = ' style="display:none;"';
     
    510544<th scope="row"><?php _e( 'Upload Image' ); ?></th>
    511545<td>
    512546        <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>
     547        <?php
     548        if ( ! theme_supports_flex_height_headers() && ! theme_supports_flex_width_headers() ) {
     549                printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
     550        } elseif ( theme_supports_flex_height_headers() ) {
     551                if ( ! theme_supports_flex_width_headers() )
     552                        printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . '<br />', HEADER_IMAGE_WIDTH );
     553        } elseif ( theme_supports_flex_width_headers() ) {
     554                if ( ! theme_supports_flex_height_headers() )
     555                        printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . '<br />', HEADER_IMAGE_HEIGHT );
     556        }
     557        if ( theme_supports_flex_height_headers() || theme_supports_flex_width_headers() ) {
     558                $header_support = get_theme_support( 'custom-header' );
     559                if ( !empty( $header_support[ 0 ][ 'suggested-width' ] ) )
     560                        printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-width' ] ) );
     561                if ( !empty( $header_support[ 0 ][ 'suggested-height' ] ) )
     562                        printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-height' ] ) );
     563        }
     564        ?></p>
    514565        <form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
    515566        <p>
    516567                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     
    662713
    663714                list($width, $height, $type, $attr) = getimagesize( $file );
    664715
    665                 if ( $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
     716                $header_support = get_theme_support( 'custom-header' );
     717                // If flexible height isn't supported and the image is the exact right size
     718                if ( ! theme_supports_flex_height_headers() && ! theme_supports_flex_width_headers() && $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    666719                        // Add the meta-data
    667720                        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    668721                        update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
     
    670723                        set_theme_mod('header_image', esc_url($url));
    671724                        do_action('wp_create_file_in_uploads', $file, $id); // For replication
    672725                        return $this->finished();
    673                 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
     726                } elseif ( ! theme_supports_flex_width_headers() && $width > HEADER_IMAGE_WIDTH ) {
    674727                        $oitar = $width / HEADER_IMAGE_WIDTH;
    675728                        $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
    676729                        if ( is_wp_error( $image ) )
     
    733786                $attachment_id = absint( $_POST['attachment_id'] );
    734787                $original = get_attached_file($attachment_id);
    735788
    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 );
     789                if ( theme_supports_flex_height_headers() && ! theme_supports_flex_width_headers() )
     790                        $dst_height = (int) $_POST['height'] * ( HEADER_IMAGE_WIDTH / $_POST['width'] );
     791                elseif ( theme_supports_flex_height_headers() && theme_supports_flex_width_headers() )
     792                        $dst_height = (int) $_POST['height'];
     793                else
     794                        $dst_height = HEADER_IMAGE_HEIGHT;
     795
     796                if ( theme_supports_flex_width_headers() && ! theme_supports_flex_height_headers() )
     797                        $dst_width = (int) $_POST['width'] * ( HEADER_IMAGE_WIDTH / $_POST['height'] );
     798                if ( theme_supports_flex_width_headers() && theme_supports_flex_height_headers() )
     799                        $dst_width = (int) $_POST['width'];
     800                else
     801                        $dst_width = HEADER_IMAGE_HEIGHT;
     802
     803                $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
    737804                if ( is_wp_error( $cropped ) )
    738805                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    739806
     
    759826                update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
    760827
    761828                set_theme_mod('header_image', $url);
     829                set_theme_mod( 'header_image_width', $dst_width );
     830                set_theme_mod( 'header_image_height', $dst_height );
    762831
    763832                // cleanup
    764833                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
  • wp-admin/css/wp-admin.dev.css

     
    44944494
    44954495.appearance_page_custom-header #headimg {
    44964496        border: 1px solid #DFDFDF;
    4497         min-height: 100px;
    44984497        width: 100%;
    44994498}
    45004499