Make WordPress Core

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

File 17242-height-width.5.diff, 16.2 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        $data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
     1595        $default = array(
     1596                'url'           => '',
     1597                'thumbnail_url' => '',
     1598                'width'         => '',
     1599                'height'        => '',
     1600        );
     1601        return (object) wp_parse_args( $data, $default );
     1602}
     1603
     1604/**
     1605 * Get the header image width.
     1606 *
     1607 * @since 3.4.0
     1608 *
     1609 * @return int
     1610 */
     1611function get_header_image_width() {
     1612        return empty( get_current_header_data()->width )? HEADER_IMAGE_WIDTH : get_current_header_data()->width;
     1613}
     1614
     1615/**
     1616 * Get the header image height.
     1617 *
     1618 * @since 3.4.0
     1619 *
     1620 * @return int
     1621 */
     1622function get_header_image_height() {
     1623        return empty( get_current_header_data()->height )? HEADER_IMAGE_HEIGHT : get_current_header_data()->height;
     1624}
     1625
     1626/**
    15611627 * Add callbacks for image header display.
    15621628 *
    15631629 * The parameter $header_callback callback will be required to display the
     
    19552021                        $post_format = $args[0];
    19562022                        return in_array( $post_format, $_wp_theme_features[$feature][0] );
    19572023                        break;
     2024
     2025                case 'custom-header':
     2026                        // specific custom header capabilities can be registered by passing
     2027                        // an array to add_theme_support()
     2028                        $header_support = $args[0];
     2029                        return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
     2030                        break;
    19582031        }
    19592032
    19602033        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                $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 ) );
    668741                        update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
     
    670743                        set_theme_mod('header_image', esc_url($url));
    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' ) );
    678751
     
    733806                $attachment_id = absint( $_POST['attachment_id'] );
    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' ) );
    739838
     
    760859
    761860                set_theme_mod('header_image', $url);
    762861
     862                $header_data                = new stdClass();
     863                $header_data->id            = $attachment_id;
     864                $header_data->url           = $url;
     865                $header_data->thumbnail_url = $url;
     866                $header_data->uploaded      = true;
     867                $header_data->width         = $dst_width;
     868                $header_data->height        = $dst_height;
     869
     870                set_theme_mod( 'header_image_data', $header_data );
     871
    763872                // cleanup
    764873                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
    765874                @unlink( apply_filters( 'wp_delete_file', $medium ) );