WordPress.org

Make WordPress Core

Changeset 40817


Ignore:
Timestamp:
05/22/2017 08:28:43 PM (14 months ago)
Author:
afercia
Message:

Themes: Improve the theme Custom Logo accessibility.

Uses the Site title as fallback value for the Custom Logo alt attribute when the original alt attribute is empty.

Props sami.keijonen, joedolson, sstoqnov, nobremarcos, gma992, LiamMcArthur, jjcomack.
Fixes #38768.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r40626 r40817  
    889889    // We have a logo. Logo is go.
    890890    if ( $custom_logo_id ) {
     891        $custom_logo_attr = array(
     892            'class'    => 'custom-logo',
     893            'itemprop' => 'logo',
     894        );
     895
     896        /*
     897         * If the logo alt attribute is empty, get the site title and explicitly
     898         * pass it to the attributes used by wp_get_attachment_image().
     899         */
     900        $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true );
     901        if ( empty( $image_alt ) ) {
     902            $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
     903        }
     904
     905        /*
     906         * If the alt attribute is not empty, there's no need to explicitly pass
     907         * it because wp_get_attachment_image() already adds the alt attribute.
     908         */
    891909        $html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
    892910            esc_url( home_url( '/' ) ),
    893             wp_get_attachment_image( $custom_logo_id, 'full', false, array(
    894                 'class'    => 'custom-logo',
    895                 'itemprop' => 'logo',
    896             ) )
     911            wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr )
    897912        );
    898913    }
  • trunk/tests/phpunit/tests/general/template.php

    r40564 r40817  
    308308
    309309        $this->_set_custom_logo();
     310
     311        $custom_logo_attr = array(
     312            'class'    => 'custom-logo',
     313            'itemprop' => 'logo',
     314        );
     315
     316        // If the logo alt attribute is empty, use the site title.
     317        $image_alt = get_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', true );
     318        if ( empty( $image_alt ) ) {
     319            $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
     320        }
     321
    310322        $home_url = get_home_url( $blog_id, '/' );
    311         $image    = wp_get_attachment_image( $this->custom_logo_id, 'full', false, array(
    312             'class'     => 'custom-logo',
    313             'itemprop'  => 'logo',
    314         ) );
     323        $image    = wp_get_attachment_image( $this->custom_logo_id, 'full', false, $custom_logo_attr );
    315324        restore_current_blog();
    316325
     
    329338
    330339        $this->_set_custom_logo();
     340
     341        $custom_logo_attr = array(
     342            'class'    => 'custom-logo',
     343            'itemprop' => 'logo',
     344        );
     345
     346        // If the logo alt attribute is empty, use the site title.
     347        $image_alt = get_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', true );
     348        if ( empty( $image_alt ) ) {
     349            $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' );
     350        }
     351
     352        $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, $custom_logo_attr );
     353
     354        $this->expectOutputString( '<a href="http://' . WP_TESTS_DOMAIN . '/" class="custom-logo-link" rel="home" itemprop="url">' . $image . '</a>' );
     355        the_custom_logo();
     356    }
     357
     358    /**
     359     * @group custom_logo
     360     * @ticket 38768
     361     */
     362    function test_the_custom_logo_with_alt() {
     363        $this->_set_custom_logo();
     364
     365        $image_alt = 'My alt attribute';
     366
     367        update_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', $image_alt );
     368
    331369        $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, array(
    332             'class'     => 'custom-logo',
    333             'itemprop'  => 'logo',
     370            'class'    => 'custom-logo',
     371            'itemprop' => 'logo',
    334372        ) );
    335373
Note: See TracChangeset for help on using the changeset viewer.