From 181ecf1ec3bd079f3c2ab76363d9a8988f8e1b16 Mon Sep 17 00:00:00 2001 From: Andrea Fercia Date: Mon, 22 May 2017 20:28:43 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@40817 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 23 +++++++++-- tests/phpunit/tests/general/template.php | 50 +++++++++++++++++++++--- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 97468acf2a..687ee25f49 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -888,12 +888,27 @@ function get_custom_logo( $blog_id = 0 ) { // We have a logo. Logo is go. if ( $custom_logo_id ) { + $custom_logo_attr = array( + 'class' => 'custom-logo', + 'itemprop' => 'logo', + ); + + /* + * If the logo alt attribute is empty, get the site title and explicitly + * pass it to the attributes used by wp_get_attachment_image(). + */ + $image_alt = get_post_meta( $custom_logo_id, '_wp_attachment_image_alt', true ); + if ( empty( $image_alt ) ) { + $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' ); + } + + /* + * If the alt attribute is not empty, there's no need to explicitly pass + * it because wp_get_attachment_image() already adds the alt attribute. + */ $html = sprintf( '', esc_url( home_url( '/' ) ), - wp_get_attachment_image( $custom_logo_id, 'full', false, array( - 'class' => 'custom-logo', - 'itemprop' => 'logo', - ) ) + wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr ) ); } diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 4d47a4d46a..49b5327367 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -307,11 +307,20 @@ class Tests_General_Template extends WP_UnitTestCase { switch_to_blog( $blog_id ); $this->_set_custom_logo(); + + $custom_logo_attr = array( + 'class' => 'custom-logo', + 'itemprop' => 'logo', + ); + + // If the logo alt attribute is empty, use the site title. + $image_alt = get_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', true ); + if ( empty( $image_alt ) ) { + $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' ); + } + $home_url = get_home_url( $blog_id, '/' ); - $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, array( - 'class' => 'custom-logo', - 'itemprop' => 'logo', - ) ); + $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, $custom_logo_attr ); restore_current_blog(); $expected_custom_logo = ''; @@ -328,9 +337,38 @@ class Tests_General_Template extends WP_UnitTestCase { the_custom_logo(); $this->_set_custom_logo(); + + $custom_logo_attr = array( + 'class' => 'custom-logo', + 'itemprop' => 'logo', + ); + + // If the logo alt attribute is empty, use the site title. + $image_alt = get_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', true ); + if ( empty( $image_alt ) ) { + $custom_logo_attr['alt'] = get_bloginfo( 'name', 'display' ); + } + + $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, $custom_logo_attr ); + + $this->expectOutputString( '' ); + the_custom_logo(); + } + + /** + * @group custom_logo + * @ticket 38768 + */ + function test_the_custom_logo_with_alt() { + $this->_set_custom_logo(); + + $image_alt = 'My alt attribute'; + + update_post_meta( $this->custom_logo_id, '_wp_attachment_image_alt', $image_alt ); + $image = wp_get_attachment_image( $this->custom_logo_id, 'full', false, array( - 'class' => 'custom-logo', - 'itemprop' => 'logo', + 'class' => 'custom-logo', + 'itemprop' => 'logo', ) ); $this->expectOutputString( '' );