Make WordPress Core

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

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

     
    13511351        if ( isset( $mods[ $name ] ) )
    13521352                return apply_filters( "theme_mod_$name", $mods[ $name ] );
    13531353
    1354         return apply_filters( "theme_mod_$name", sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ) );
     1354        if ( is_string( $default ) )
     1355                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1356
     1357        return apply_filters( "theme_mod_$name", $default );
    13551358}
    13561359
    13571360/**
     
    14571460}
    14581461
    14591462/**
    1460  * Get random header image from registered images in theme.
     1463 * Get random header image data from registered images in theme.
    14611464 *
    1462  * @since 3.2.0
     1465 * @since 3.4.0
    14631466 *
     1467 * @access private
     1468 *
    14641469 * @return string Path to header image
    14651470 */
    1466 function get_random_header_image() {
    1467         global $_wp_default_headers;
    14681471
    1469         $header_image_mod = get_theme_mod( 'header_image', '' );
    1470         $headers = array();
     1472function _get_random_header_data() {
     1473        static $_wp_random_header;
    14711474
    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' ] ) )
     1475        if ( empty( $_wp_random_header ) ) {
     1476                global $_wp_default_headers;
     1477                $header_image_mod = get_theme_mod( 'header_image', '' );
     1478                $headers = array();
     1479
     1480                if ( 'random-uploaded-image' == $header_image_mod )
     1481                        $headers = get_uploaded_header_images();
     1482                elseif ( ! empty( $_wp_default_headers ) ) {
     1483                        if ( 'random-default-image' == $header_image_mod ) {
    14801484                                $headers = $_wp_default_headers;
     1485                        } else {
     1486                                $is_random = get_theme_support( 'custom-header' );
     1487                                if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
     1488                                        $headers = $_wp_default_headers;
     1489                        }
    14811490                }
    1482         }
    14831491
    1484         if ( empty( $headers ) )
    1485                 return '';
     1492                if ( empty( $headers ) )
     1493                        return stdClass();
    14861494
    1487         $random_image = array_rand( $headers );
    1488         $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
     1495                $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
    14891496
    1490         return $header_url;
     1497                $_wp_random_header->url =  sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1498                $_wp_random_header->thumbnail_url =  sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1499        }
     1500        return $_wp_random_header;
    14911501}
    14921502
    14931503/**
     1504 * Get random header image url from registered images in theme.
     1505 *
     1506 * @since 3.2.0
     1507 *
     1508 * @return string Path to header image
     1509 */
     1510
     1511function get_random_header_image() {
     1512        $random_image = _get_random_header_data();
     1513        if ( empty( $random_image->url ) )
     1514                return '';
     1515        return $random_image->url;
     1516}
     1517
     1518/**
    14941519 * Check if random header image is in use.
    14951520 *
    14961521 * Always true if user expressly chooses the option in Appearance > Header.
     
    15471572
    15481573        foreach ( (array) $headers as $header ) {
    15491574                $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;
     1575                $header_data = wp_get_attachment_metadata( $header->ID );
     1576                $header_index = basename($url);
     1577                $header_images[$header_index] = array();
     1578                $header_images[$header_index]['attachment_id'] =  $header->ID;
     1579                $header_images[$header_index]['url'] =  $url;
     1580                $header_images[$header_index]['thumbnail_url'] =  $url;
     1581                $header_images[$header_index]['width'] = $header_data['width'];
     1582                $header_images[$header_index]['height'] = $header_data['height'];
    15551583        }
    15561584
    15571585        return $header_images;
    15581586}
    15591587
    15601588/**
     1589 * Get the header image data.
     1590 *
     1591 * @since 3.4.0
     1592 *
     1593 * @return object
     1594 */
     1595function get_current_header_data() {
     1596        $data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
     1597        $default = array(
     1598                'url'           => '',
     1599                'thumbnail_url' => '',
     1600                'width'         => '',
     1601                'height'        => '',
     1602        );
     1603        return (object) wp_parse_args( $data, $default );
     1604}
     1605
     1606/**
     1607 * Get the header image width.
     1608 *
     1609 * @since 3.4.0
     1610 *
     1611 * @return int
     1612 */
     1613function get_header_image_width() {
     1614        return empty( get_current_header_data()->width )? HEADER_IMAGE_WIDTH : get_current_header_data()->width;
     1615}
     1616
     1617/**
     1618 * Get the header image height.
     1619 *
     1620 * @since 3.4.0
     1621 *
     1622 * @return int
     1623 */
     1624function get_header_image_height() {
     1625        return empty( get_current_header_data()->height )? HEADER_IMAGE_HEIGHT : get_current_header_data()->height;
     1626}
     1627
     1628/**
    15611629 * Add callbacks for image header display.
    15621630 *
    15631631 * The parameter $header_callback callback will be required to display the
     
    19552023                        $post_format = $args[0];
    19562024                        return in_array( $post_format, $_wp_theme_features[$feature][0] );
    19572025                        break;
     2026
     2027                case 'custom-header':
     2028                        // specific custom header capabilities can be registered by passing
     2029                        // an array to add_theme_support()
     2030                        $header_support = $args[0];
     2031                        return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
     2032                        break;
    19582033        }
    19592034
    19602035        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        }
     
    289310                        echo '<div class="default-header">';
    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>';
    295316                        echo '</div>';
     
    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->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 );
     870
    763871                // cleanup
    764872                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
    765873                @unlink( apply_filters( 'wp_delete_file', $medium ) );