-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix course cards links are duplicated (#2777)
* Add aria-hidden/tabindex/role attribute to post featured image block * Apply to all related card template parts * Apply to my courses page content pattern
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
wp-content/themes/pub/wporg-learn-2024/src/card-featured-image-a11y/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "wporg-learn/card-featured-image-a11y", | ||
"viewScript": "file:./view.js" | ||
} |
55 changes: 55 additions & 0 deletions
55
wp-content/themes/pub/wporg-learn-2024/src/card-featured-image-a11y/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Adds aria labels to the post featured image block in the card course template. | ||
*/ | ||
|
||
namespace WordPressdotorg\Theme\Learn_2024\Card_Featured_Image_A11y; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Actions and filters. | ||
*/ | ||
add_action( 'init', __NAMESPACE__ . '\init' ); | ||
|
||
/** | ||
* Add the scripts to update the post featured image block in the card course template. | ||
* | ||
* The dependencies are autogenerated in block.json, and can be read with | ||
* `wp_json_file_decode` & `register_block_script_handle. | ||
*/ | ||
function init() { | ||
$metadata_file = dirname( dirname( __DIR__ ) ) . '/build/card-featured-image-a11y/block.json'; | ||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); | ||
$metadata['file'] = $metadata_file; | ||
|
||
$script_handle = register_block_script_handle( $metadata, 'viewScript', 0 ); | ||
|
||
// Enqueue the assets when one of the card templates is on the page. | ||
add_action( | ||
'render_block_core/template-part', | ||
function( $block_content, $block ) use ( $script_handle ) { | ||
$slugs = array( 'card-course-h3', 'card-course', 'card-lesson-h3', 'card-lesson', 'card' ); | ||
if ( isset( $block['attrs']['slug'] ) && in_array( $block['attrs']['slug'], $slugs, true ) ) { | ||
wp_enqueue_script( $script_handle ); | ||
} | ||
return $block_content; | ||
}, | ||
10, | ||
2 | ||
); | ||
|
||
// Enqueue the assets when the my courses page content pattern is on the page. | ||
// The blocks composition of this pattern is similar to that of the card course. | ||
add_action( | ||
'render_block_core/pattern', | ||
function( $block_content, $block ) use ( $script_handle ) { | ||
if ( isset( $block['attrs']['slug'] ) && 'wporg-learn-2024/page-my-courses-content' === $block['attrs']['slug'] ) { | ||
wp_enqueue_script( $script_handle ); | ||
} | ||
return $block_content; | ||
}, | ||
10, | ||
2 | ||
); | ||
} |
26 changes: 26 additions & 0 deletions
26
wp-content/themes/pub/wporg-learn-2024/src/card-featured-image-a11y/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function init() { | ||
/** | ||
* Enhance the accessibility of the card course template. | ||
* | ||
* This function adds an `aria-hidden="true"` and `tabindex="-1"` | ||
* to the post featured image block in the card course template. | ||
*/ | ||
function enhanceCardCourseAccessibility() { | ||
document | ||
.querySelectorAll( '.wporg-learn-card-grid figure.wp-block-post-featured-image > a' ) | ||
.forEach( ( featuredImgLink ) => { | ||
featuredImgLink.setAttribute( 'aria-hidden', 'true' ); | ||
featuredImgLink.setAttribute( 'tabindex', '-1' ); | ||
|
||
const featuredImg = featuredImgLink.querySelector( 'img' ); | ||
if ( featuredImg ) { | ||
featuredImg.setAttribute( 'role', 'presentation' ); | ||
featuredImg.setAttribute( 'alt', '' ); | ||
} | ||
} ); | ||
} | ||
|
||
enhanceCardCourseAccessibility(); | ||
} | ||
|
||
document.addEventListener( 'DOMContentLoaded', init ); |