Make WordPress Core

Ticket #17242: 17242-height-width.3.diff

File 17242-height-width.3.diff, 14.8 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 *
     1464 * @access private
     1465 *
    14641466 * @return string Path to header image
    14651467 */
    1466 function get_random_header_image() {
    1467         global $_wp_default_headers;
    14681468
    1469         $header_image_mod = get_theme_mod( 'header_image', '' );
    1470         $headers = array();
     1469function _get_random_header_data() {
     1470        static $_wp_random_header;
    14711471
    1472         if ( 'random-uploaded-image' == $header_image_mod )
    1473                 $headers = get_uploaded_header_images();
    1474         elseif ( ! empty( $_wp_default_headers ) ) {
    1475                 if ( 'random-default-image' == $header_image_mod ) {
    1476                         $headers = $_wp_default_headers;
    1477                 } else {
    1478                         $is_random = get_theme_support( 'custom-header' );
    1479                         if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
     1472        if ( empty( $_wp_random_header ) ) {
     1473                global $_wp_default_headers;
     1474                $header_image_mod = get_theme_mod( 'header_image', '' );
     1475                $headers = array();
     1476
     1477                if ( 'random-uploaded-image' == $header_image_mod )
     1478                        $headers = get_uploaded_header_images();
     1479                elseif ( ! empty( $_wp_default_headers ) ) {
     1480                        if ( 'random-default-image' == $header_image_mod ) {
    14801481                                $headers = $_wp_default_headers;
     1482                        } else {
     1483                                $is_random = get_theme_support( 'custom-header' );
     1484                                if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
     1485                                        $headers = $_wp_default_headers;
     1486                        }
    14811487                }
    1482         }
    14831488
    1484         if ( empty( $headers ) )
    1485                 return '';
     1489                if ( empty( $headers ) )
     1490                        return stdClass();
    14861491
    1487         $random_image = array_rand( $headers );
    1488         $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1492                $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
    14891493
    1490         return $header_url;
     1494                $_wp_random_header->url =  sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1495                $_wp_random_header->thumbnail_url =  sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1496        }
     1497        return $_wp_random_header;
    14911498}
    14921499
    14931500/**
     1501 * Get random header image url from registered images in theme.
     1502 *
     1503 * @since 3.2.0
     1504 *
     1505 * @return string Path to header image
     1506 */
     1507
     1508function get_random_header_image() {
     1509        $random_image = _get_random_header_data();
     1510        if ( empty( $random_image->url ) )
     1511                return '';
     1512        return $random_image->url;
     1513}
     1514
     1515/**
    14941516 * Check if random header image is in use.
    14951517 *
    14961518 * Always true if user expressly chooses the option in Appearance > Header.
     
    15471569
    15481570        foreach ( (array) $headers as $header ) {
    15491571                $url = esc_url_raw( $header->guid );
    1550                 $header = basename($url);
    1551                 $header_images[$header] = array();
    1552                 $header_images[$header]['url'] =  $url;
    1553                 $header_images[$header]['thumbnail_url'] =  $url;
    1554                 $header_images[$header]['uploaded'] = true;
     1572                $header_data = wp_get_attachment_metadata( $header->ID );
     1573                $header_index = basename($url);
     1574                $header_images[$header_index] = array();
     1575                $header_images[$header_index]['id'] =  $header->ID;
     1576                $header_images[$header_index]['url'] =  $url;
     1577                $header_images[$header_index]['thumbnail_url'] =  $url;
     1578                $header_images[$header_index]['uploaded'] = true;
     1579                $header_images[$header_index]['width'] = $header_data['width'];
     1580                $header_images[$header_index]['height'] = $header_data['height'];
    15551581        }
    15561582
    15571583        return $header_images;
    15581584}
    15591585
    15601586/**
     1587 * Get the header image data.
     1588 *
     1589 * @since 3.4.0
     1590 *
     1591 * @return object
     1592 */
     1593function get_current_header_data() {
     1594        return is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data', (object) array() );
     1595}
     1596
     1597/**
     1598 * Get the header image width.
     1599 *
     1600 * @since 3.4.0
     1601 *
     1602 * @return int
     1603 */
     1604function get_header_image_width() {
     1605        return empty( get_current_header_data()->width )? HEADER_IMAGE_WIDTH : get_current_header_data()->width;
     1606}
     1607
     1608/**
     1609 * Get the header image height.
     1610 *
     1611 * @since 3.4.0
     1612 *
     1613 * @return int
     1614 */
     1615function get_header_image_height() {
     1616        return empty( get_current_header_data()->height )? HEADER_IMAGE_HEIGHT : get_current_header_data()->height;
     1617}
     1618
     1619/**
    15611620 * Add callbacks for image header display.
    15621621 *
    15631622 * The parameter $header_callback callback will be required to display the
     
    19552014                        $post_format = $args[0];
    19562015                        return in_array( $post_format, $_wp_theme_features[$feature][0] );
    19572016                        break;
     2017
     2018                case 'custom-header':
     2019                        // specific custom header capabilities can be registered by passing
     2020                        // an array to add_theme_support()
     2021                        $header_support = $args[0];
     2022                        return in_array( $header_support, $_wp_theme_features[$feature][0] );
     2023                        break;
    19582024        }
    19592025
    19602026        return apply_filters('current_theme_supports-' . $feature, true, $args, $_wp_theme_features[$feature]);
  • 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
  • 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                        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                }
    194208
     
    225239                        } else {
    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                }
    234255        }
     
    419440        }
    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();
    426456                var yimg = jQuery('img#upload').height();
     
    438468                jQuery('img#upload').imgAreaSelect({
    439469                        handles: true,
    440470                        keys: true,
    441                         aspectRatio: xinit + ':' + yinit,
    442471                        show: true,
    443472                        x1: 0,
    444473                        y1: 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);
    451495                                jQuery('#height').val(yinit);
     
    492536          call_user_func( $this->admin_image_div_callback );
    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() )
    498542                        $style = ' style="display:none;"';
     
    510554<th scope="row"><?php _e( 'Upload Image' ); ?></th>
    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>
    516577                <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
     
    662723
    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                // If flexible height isn't supported and the image is the exact right size
     728                if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) && $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
    666729                        // Add the meta-data
    667730                        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    668731                        update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
     
    670733                        set_theme_mod('header_image', esc_url($url));
    671734                        do_action('wp_create_file_in_uploads', $file, $id); // For replication
    672735                        return $this->finished();
    673                 } elseif ( $width > HEADER_IMAGE_WIDTH ) {
     736                } elseif ( ! current_theme_supports( 'custom-header', 'flex-width' ) && $width > HEADER_IMAGE_WIDTH ) {
    674737                        $oitar = $width / HEADER_IMAGE_WIDTH;
    675738                        $image = wp_crop_image($file, 0, 0, $width, $height, HEADER_IMAGE_WIDTH, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
    676739                        if ( is_wp_error( $image ) )
     
    733796                $attachment_id = absint( $_POST['attachment_id'] );
    734797                $original = get_attached_file($attachment_id);
    735798
    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 );
     799                if ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) )
     800                        $dst_height = (int) $_POST['height'] * ( HEADER_IMAGE_WIDTH / $_POST['width'] );
     801                elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
     802                        $dst_height = (int) $_POST['height'];
     803                else
     804                        $dst_height = HEADER_IMAGE_HEIGHT;
     805
     806                if ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) )
     807                        $dst_width = (int) $_POST['width'] * ( HEADER_IMAGE_WIDTH / $_POST['height'] );
     808                if ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
     809                        $dst_width = (int) $_POST['width'];
     810                else
     811                        $dst_width = HEADER_IMAGE_HEIGHT;
     812
     813                $cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
    737814                if ( is_wp_error( $cropped ) )
    738815                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    739816
     
    760837
    761838                set_theme_mod('header_image', $url);
    762839
     840                $header_data                = new stdClass();
     841                $header_data->id            = $attachment_id;
     842                $header_data->url           = $url;
     843                $header_data->thumbnail_url = $url;
     844                $header_data->uploaded      = true;
     845                $header_data->width         = $dst_width;
     846                $header_data->height        = $dst_height;
     847
     848                set_theme_mod( 'header_image_data', $header_data );
     849
    763850                // cleanup
    764851                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
    765852                @unlink( apply_filters( 'wp_delete_file', $medium ) );